summary refs log tree commit diff stats
path: root/lib/std/files.nim
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-10-22 03:53:44 +0800
committerGitHub <noreply@github.com>2022-10-21 21:53:44 +0200
commit3c12b72168d9fe9916f471fa17d2c24c52b33066 (patch)
tree067a76c67e104b2d4d1e31bc9d0ea4b193824233 /lib/std/files.nim
parent66cbcaab8474f5ff3480e7a9bc55df249548a90c (diff)
downloadNim-3c12b72168d9fe9916f471fa17d2c24c52b33066.tar.gz
add typesafe `std/paths`, `std/files`, `std/dirs`, `std/symlinks` (#20582)
* split std/os; add typesafe std/paths
* add more files, dirs, paths
* add documentation
* add testcase
* remove tryRemoveFile
* clean up
* Delete test.nim
* apply changes
* add `add` and fixes
Diffstat (limited to 'lib/std/files.nim')
-rw-r--r--lib/std/files.nim45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/std/files.nim b/lib/std/files.nim
new file mode 100644
index 000000000..f7fbd5acd
--- /dev/null
+++ b/lib/std/files.nim
@@ -0,0 +1,45 @@
+from paths import Path, ReadDirEffect, WriteDirEffect
+
+from std/private/osfiles import fileExists, removeFile,
+                                moveFile
+
+
+proc fileExists*(filename: Path): bool {.inline, tags: [ReadDirEffect].} =
+  ## Returns true if `filename` exists and is a regular file or symlink.
+  ##
+  ## Directories, device files, named pipes and sockets return false.
+  result = fileExists(filename.string)
+
+proc removeFile*(file: Path) {.inline, tags: [WriteDirEffect].} =
+  ## Removes the `file`.
+  ##
+  ## If this fails, `OSError` is raised. This does not fail
+  ## if the file never existed in the first place.
+  ##
+  ## On Windows, ignores the read-only attribute.
+  ##
+  ## See also:
+  ## * `removeDir proc`_
+  ## * `copyFile proc`_
+  ## * `copyFileWithPermissions proc`_
+  ## * `moveFile proc`_
+  removeFile(file.string)
+
+proc moveFile*(source, dest: Path) {.inline,
+    tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect].} =
+  ## Moves a file from `source` to `dest`.
+  ##
+  ## Symlinks are not followed: if `source` is a symlink, it is itself moved,
+  ## not its target.
+  ##
+  ## If this fails, `OSError` is raised.
+  ## If `dest` already exists, it will be overwritten.
+  ##
+  ## Can be used to `rename files`:idx:.
+  ##
+  ## See also:
+  ## * `moveDir proc`_
+  ## * `copyFile proc`_
+  ## * `copyFileWithPermissions proc`_
+  ## * `removeFile proc`_
+  moveFile(source.string, dest.string)