Commit Graph

65 Commits

Author SHA1 Message Date
Tomohiro
db435a4a79 Fix searchExtPos so that it returns -1 when the path is not a file ext (#22245)
* Fix searchExtPos so that it returns -1 when the path is not a file ext

* fix comparision expression

* Remove splitDrive from searchExtPos
2023-08-04 20:00:43 +02:00
ringabout
4fa86422c0 stdlib tests now check refc too (#21664)
* stdlib tests now check refc too

* typo

* fixes line numbers

* disable cpp

* do not touch
2023-04-21 15:37:58 +02:00
Andrey Makarov
779b1cc5be Fix #20628 for Windows (#20667)
* Fix #20628 for Windows

* Move isRegular - !isSpecial and onlyRegular - skipSpecial

* Forgot to change it in 1 more place
2022-10-28 10:01:28 +02:00
Andrey Makarov
8ed2431db0 Implement Unix file regularity check (#20448) (#20628)
* Implement Unix file regularity check

* update std/dirs also
2022-10-25 07:42:47 +02:00
Juan Carlos
1e635bb539 Undeprecate isvalidfilename (#19643)
* Remove deprecated isvalidfilename
* https://github.com/nim-lang/Nim/pull/19643#issuecomment-1235102314
* https://github.com/nim-lang/Nim/pull/19643#issuecomment-1235102314
* https://github.com/nim-lang/Nim/pull/19643#issuecomment-1235102314
* Add unittests
* Add more

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2022-09-29 12:22:29 +02:00
ringabout
3d2f0e2c7c make more standard libraries work with nimPreviewSlimSystem (#20343)
* make more standard libraries work with `nimPreviewSlimSystem`

* typo

* part two

* Delete specutils.nim

* fixes more tests

* more fixes

* fixes tests

* fixes three more tests

* add formatfloat import

* fix

* last
2022-09-27 20:06:23 +02:00
havardjohn
68f92af17c Fix cannot create Windows directory in root (#20311)
* Fix cannot create Windows directory in root

Fixes #20306, a regression bug with `createDir` caused by
`23e0160af283bb0bb573a86145e6c1c792780d49`.

The issue is that, if the path consists only of a drive and a single
directory (e.g. "Y:\nimcache2" in the original issue), then no
directories will be created. This works fine if there are multiple
directories (e.g. "Y:\nimcache2\test").

In the case of "Y:\nimcache2", `omitNext` in `createDir` is `false` on
the last condition in `createDir`. This means that the "nimcache2"
directory will not be created, and no exception will be raised.

Fixed by refactoring to use `parentDirs` iterator instead of iterating
over the string characters. Motivation is reduced code complexity.

Will not test the specific "C:\test" `createDir` case, since there is no
standard Windows drive with write permissions in the root. Creating a
custom drive-mapping to Windows Temp is a non-option. That could mess
up some users running the test.

Added `parentDirs` tests since they are lacking on for POSIX paths.

* Fix `createDir("")` causing error

The change to `createDir` caused `createDir("")` to raise an error,
where it previously didn't. Fixed so `createDir("")` does not fail, and
added test case.
2022-09-11 16:51:39 -04:00
havardjohn
23e0160af2 Add improved Windows UNC path support in std/os (#20281)
* Add improved Windows UNC path support in std/os

Original issue: `std/os.createDir` tries to create every component of
the given path as a directory. The problem is that `createDir`
interprets every backslash/slash as a path separator. For a UNC path
this is incorrect. E.g. one UNC form is `\\Server\Volume\Path`. It's an
error to create the `\\Server` directory, as well as creating
`\\Server\Volume`.

Add `ntpath.nim` module with `splitDrive` proc. This implements UNC path
parsing as implemented in the Python `ntpath.py` module. The following
UNC forms are supported:

* `\\Server\Volume\Path`
* `\\?\Volume\Path`
* `\\?\UNC\Server\Volume\Path`

Improves support for UNC paths in various procs in `std/os`:
---

* pathnorm.addNormalizePath
  * Issue: This had incomplete support for UNC paths
    * The UNC prefix (first 2 characters of a UNC path) was assumed to
      be exactly `\\`, but it can be `//` and `\/`, etc. as well
    * Also, the UNC prefix must be normalized to the `dirSep` argument
      of `addNormalizePath`
  * Resolution: Changed to account for different UNC prefixes, and
    normalizing the prefixes according to `dirSep`
    * Affected procs that get tests: `relativePath`, `joinPath`
  * Issue: The server/volume part of UNC paths can be stripped when
    normalizing `..` path components
    * This error should be negligable, so ignoring this
* splitPath
  * Now make sure the UNC drive is not split; return the UNC drive as
    `head` if the UNC drive is the only component of the path
  * Consequently fixes `extractFilename`, `lastPathPart`
* parentDir / `/../`
  * Strip away drive before working on the path, prepending the drive
    after all work is done - prevents stripping UNC components
  * Return empty string if drive component is the only component; this
    is the behavior for POSIX paths as well
  * Alternative implementation: Just call something like
    `pathnorm.normalizePath(path & "/..")` for the whole proc - maybe
    too big of a change
* tailDir
  * If drive is present in path, just split that from path and return
    path
* parentDirs iterator
  * Uses `parentDir` for going backwards
  * When going forwards, first `splitDrive`, yield the drive field, and
    then iterate over path field as normal
* splitFile
  * Make sure path parsing stops at end of drive component
* createDir
  * Fixed by skipping drive part before creating directories
  * Alternative implementation: use `parentDirs` iterator instead of
    iterating over characters
    * Consequence is that it will try to create the root directory
* isRootDir
  * Changed to treat UNC drive alone as root (e.g. "//?/c:" is root)
  * This change prevents the empty string being yielded by the
    `parentDirs` iterator with `fromRoot = false`
* Internal `sameRoot`
  * The "root" refers to the drive, so `splitDrive` can be used here

This adds UNC path support to all procs that could use it in std/os. I
don't think any more work has to be done to support UNC paths. For the
future, I believe the path handling code can be refactored due to
duplicate code. There are multiple ways of manipulating paths, such as
manually searching string for path separator and also having a path
normalizer (pathnorm.nim). If all path manipulation used `pathnorm.nim`,
and path component splitting used `parentDirs` iterator, then a lot of
code could be removed.

Tests
---

Added test file for `pathnorm.nim` and `ntpath.nim`.
`pathnorm.normalizePath` has no tests, so I'm adding a few unit tests.
`ntpath.nim` contains tests copied from Python's test suite.

Added integration tests to `tos.nim` that tests UNC paths.

Removed incorrect `relativePath` runnableExamples from being tested on Windows:
---

`relativePath("/Users///me/bar//z.nim", "//Users/", '/') == "me/bar/z.nim"`

This is incorrect on Windows because the `/` and `//` are not the same
root. `/` (or `\`) is expanded to the drive in the current working
directory (e.g. `C:\`). `//` (or `\\`), however, are the first two
characters of a UNC path. The following holds true for normal Windows
installations:

* `dirExists("/Users") != dirExists("//Users")`
* `dirExists("\\Users") != dirExists("\\\\Users")`

Fixes #19103

Questions:
---

* Should the `splitDrive` proc be in `os.nim` instead with copyright
  notice above the proc?
* Is it fine to put most of the new tests into the `runnableExamples`
  section of the procs in std/os?

* [skipci] Apply suggestions from code review

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* [skip ci] Update lib/pure/os.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* Move runnableExamples tests in os.nim to tos.nim

* tests/topt_no_cursor: Change from using splitFile to splitDrive

`splitFile` can no longer be used in the test, because it generates
different ARC code on Windows and Linux. This replaces `splitFile` with
`splitDrive`, because it generates same ARC code on Windows and Linux,
and returns a tuple. I assume the test wants a proc that returns a
tuple.

* Drop copyright attribute to Python

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
2022-09-03 20:47:09 -04:00
Timothee Cour
5c1304a418 fix #18670 quoteShellCommand, quoteShell, quoteShellWindows on windows (#18671) 2021-08-12 16:50:08 +02:00
Timothee Cour
6b3c77e7f4 Remove tracking of environment from osenv.nim v2 (#18575)
* Remove unnecessary environment tracking

* try to fix windows

* fix delEnv

* make putEnv work on windows even with empty values; improve tests: add tests, add js, vm testing

* [skip ci] fix changelog

Co-authored-by: Caden Haustein <code@brightlysalty.33mail.com>
2021-07-29 23:05:26 +02:00
Andreas Rumpf
fa0209609d fixes #18565 (#18593)
* fixes #18565
2021-07-27 09:36:19 +02:00
Timothee Cour
5f7db65257 followup #18453 (#18582) 2021-07-25 12:55:33 -07:00
Caden Haustein
f62f4159f8 Replace calls to putenv with setenv (#18530)
* Replace calls to C `putenv` with C `setenv` to remove possible memory leaks

* Add test of correct behaviour on invalid input

* Fix style in tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Update tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Update tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Add comment with bug number to tests/stdlib/tos.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Fix possible msvc arch issues

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
2021-07-23 10:04:29 +02:00
Miran
8091545f95 Revert #17398 and #17402 (#18480)
* Revert "followup #17398: `getTempDir`, `getConfigDir` now do not have trailing DirSep (#17402)"

This reverts commit 2356d0603f.

* Revert "fix #17393 getHomeDir and expandTilde should not include trailing `/` (#17398)"

This reverts commit bebf2ce24a.

* fix test
2021-07-18 11:04:19 +02:00
Fröhlich A
7bf0404dd8 #18216 make moveDir work across partitions on windows (#18223)
* return false if AccessDeniedError in tryMoveFSObject - fixes #18216

* add moveDir & moveFile tests

* rename `isMoveDir` parameter to `isDir`
2021-06-10 14:28:00 +02:00
Timothee Cour
9559350e34 add os.getCacheDir (#18126)
* add `os.getCacheDir`

* fixup

* address comments
2021-05-31 22:16:33 +02:00
Timothee Cour
78e2d299df typo: nonexistant => nonexistent (#17918)
* typo: nonexistant => nonexistent

* fix test (ordering differs because of https://github.com/nim-lang/Nim/issues/17910)
2021-05-02 00:26:41 +02:00
Timothee Cour
2356d0603f followup #17398: getTempDir, getConfigDir now do not have trailing DirSep (#17402)
* followup #17398: `getTempDir`, `getConfigDir` now do not have trailing DirSep

* fix test
2021-03-18 13:25:56 +01:00
Roman Inflianskas
31424b3808 stdlib/os: add isAdmin (#17012)
* stdlib/os: add isAdmin

* uint8 -> cuchar, assert isAdmin on Azure

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Update lib/pure/os.nim docs

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Address comments on #17012

* Raise on errors in #17012

* Check the result of FreeSid in #17012

* Change case in #17012

* Fix memory leak in #17012

* Address comments in #17012

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
2021-03-07 22:40:16 +01:00
flywind
0e3ea16554 use lowercase --define switches (#17283) 2021-03-07 12:08:41 +01:00
Juan Carlos
4982d1c662 https://github.com/nim-lang/Nim/pull/15826/files#r585368355 (#17233) 2021-03-03 11:58:05 +01:00
Roman Inflianskas
e9b360c5df stdlib/os: handle symlinks in copy/move functions (#16709)
* stdlib/os: handle symlinks in copy/move functions

- Added optional `options` argument to `copyFile`, `copyFileToDir`, and
  `copyFileWithPermissions`. By default, symlinks are followed (copy files
  symlinks point to).
- `copyDir` and `copyDirWithPermissions` copy symlinks as symlinks (instead of
  skipping them as it was before).
- `moveFile` and `moveDir` move symlinks as symlinks (instead of skipping them
  sometimes as it was before).
- Added optional `followSymlinks` argument to `setFilePermissions`.

See also: https://github.com/nim-lang/RFCs/issues/319

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Address comments in #16709

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Address comments in #16709 (second iteration)

Skip symlinks on Windows.

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
2021-02-04 18:57:41 +01:00
Timothee Cour
917f12ae52 2.5x- 3x faster copyFile on osx (#16883) 2021-02-01 17:38:17 -08:00
flywind
6d442a40a6 use doAssert in tests (#16486) 2020-12-28 14:13:21 +01:00
Timothee Cour
3eac9b2344 os: add overload copyFile*(source, dest: string, isDir = false) (#15537)
* os: add overload copyFile*(source, dest: string, isDir = false)
* renamed to copyFileToDir
2020-10-11 20:43:01 +02:00
Timothee Cour
bf604c6829 normalizeExe (#14668) 2020-06-15 10:57:34 +02:00
Timothee Cour
d573581eb7 remove isMainModule from json,os,sequtils (#14572)
* move json.isMainModule => tjson

* move isMainModule => tos,tsequtils
2020-06-06 11:50:46 +02:00
Timothee Cour
7ce0358351 fix #13222: make relativePath more robust and flexible (#13451)
* * relativePath(foo) now works
* relativePath(rel, abs) and relativePath(abs, rel) now work (fixes #13222)
* relativePath, absolutePath, getCurrentDir now available in more targets (eg: vm, nodejs etc)
* fix bug: isAbsolutePath now works with -d:js; add tests
* workaround https://github.com/nim-lang/Nim/issues/13469
* remove `relativePath(path)` overload for now
* add back changelog after rebase
2020-04-21 23:53:55 +02:00
Timothee Cour
1d665adecd [RFC] 'walkDir' now has a new 'checkDir' flag, to mimic behaviour of other languages (#13642)
Co-authored-by: narimiran
2020-03-20 16:39:55 +01:00
Andreas Rumpf
62c113ebc7 fix #13579 joinPath("/foo/", "../a") is now /a (#13586) 2020-03-05 15:31:22 +01:00
Timothee Cour
0c312ad898 fix #13455 ; joinPath(a,b) now honors trailing slashes in b (or a if b = "") (#13467)
* fix #13455 ; joinPath(a,b) now honors trailing slashes in b (or a if b = "")
* fix test windows
2020-02-26 11:25:27 +01:00
Andrey Makarov
3dad130034 fix 3 minor bugs in joinPath (see #13455) (#13462) [backport] 2020-02-23 20:22:46 +01:00
Timothee Cour
a43583f9c8 relativePath("foo", "foo") is now ".", not "" (#13452) 2020-02-22 09:07:53 +01:00
Timothee Cour
3a5056dc70 fix lots of bugs with parentDir, refs #8734 (#13236) 2020-01-23 13:39:41 +01:00
Timothee Cour
7356bc29b7 new os.isRelativeTo (#13212) 2020-01-23 00:45:16 +01:00
Timothee Cour
b355ef2a72 fix #13211 relativePath("foo", ".") (#13213) 2020-01-21 15:48:19 +01:00
Tomohiro
509f53b782 On windows, os.relativePath returns path as is when roots are different (#12329)
* On windows, os.relativePath returns path as is when roots are different
* Implement os.sameRoot without windows API
* Fix compile error when compiling lib/nimhcr.nim
* Fix compile error when compiling lib/nimhcr.nim on Windows
2019-10-07 19:57:16 +02:00
Tomohiro
64d5e25821 Fix how relativePath handle case sensitiviy (#12312) [backport] 2019-10-01 09:09:59 +02:00
pgkos
296dfae8af Fixes splitfile (#11918) [bugfix] 2019-08-15 15:22:14 +02:00
Kaushal Modi
7182922622 [feature] Added os.delEnv; add delEnv support to nimscript too (#11466)
[feature] Fixes https://github.com/nim-lang/Nim/issues/11452.
2019-06-15 11:32:26 +02:00
Kaushal Modi
2334680b3d Use TMPDIR env var if available to get the temp dir name (#11443) [bugfix]
Additionally, use normalizePathEnd to suffix the dir name with "/" or
"\" as appropriate for the current OS.

Fixes https://github.com/nim-lang/Nim/issues/11439.
2019-06-10 19:59:51 +02:00
narimiran
f0be575ed1 move tests from tospaths to tos, fixes #9671
Also, change some of `echo`s to `doAssert`.
2019-01-23 22:04:00 +01:00
Timothee Cour
9af85fb69f fixes #10273 execShellCmd now returns nonzero when child killed with signal + other fixes (#10274)
* s/exitStatus(...)/exitStatusLikeShell(...)/
* fix #10273 execShellCmd now returns nonzero when child exits with signal
* test case for #10249 and explanation for the bug
* fix test failure
* add tests/nim.cfg
2019-01-13 09:00:39 +01:00
alaviss
b6257f3f21 os.walkDir: correctly evaluate paths when relative = true (#10057) [backport] 2018-12-21 14:12:48 +01:00
Timothee Cour
fc7df3283c fix test failure 2018-12-19 16:11:23 -08:00
Timothee Cour
656770402c fix #8255 numerous issues with splitFile 2018-12-19 16:11:23 -08:00
Timothee Cour
25d3539da7 [os] fix #10017 regression, fix #10025 regression (#10018)
* [os] fix #10017 regression
* [os] fix #10025 regression
2018-12-18 12:43:25 +01:00
Araq
ce9815bcf5 os.nim: use the new pathnorm.normalizePath implementation 2018-12-14 08:57:55 +01:00
Oscar Nihlgård
e5aae93bfe Add relative parameter to walkDirRec 2018-11-26 22:08:45 +01:00
Timothee Cour
3bef851143 fix #8225 os.isHidden was buggy on posix (#8315)
* fix #8225 isHidden was broken on posix

* scope rest of tos.nim under blocks to avoid variable scope bugs
2018-10-15 00:22:34 +02:00