files
nimony/lib/std/files.nim
This module implements file handling on Path: removing, copying and moving files, and querying or changing their permissions.
See also:
- paths module for path manipulation
- dirs module for directories
- symlinks module for symbolic links
type CopyFlag = enum cfSymlinkAsIs = (0, "cfSymlinkAsIs") cfSymlinkFollow = (1, "cfSymlinkFollow") cfSymlinkIgnore = (2, "cfSymlinkIgnore")
func dollar`.CopyFlag(e: CopyFlag): stringproc tryRemoveFile(file: Path): ErrorCodeTries to remove the file
file. A symlink is removed itself, never its target. On Windows the read-only attribute is ignored, as in Nim.proc removeFile(file: Path)Removes the file
file. If the file does not exist, no error is raised.proc getFilePermissions(filename: Path): set[FilePermission]Retrieves the permissions of
filename, following symlinks.On Windows only the read-only attribute is checked: every other permission is reported as present.
See also:
setFilePermissions proc_
proc setFilePermissions(filename: Path; permissions: set[FilePermission]; followSymlinks: bool)Sets the permissions of
filename.If
followSymlinksis true (default) andfilenameis a symlink, the permissions of the file it points to are changed. WithfollowSymlinksset to false a symlink is left alone: symlink permissions are not observed on Linux, and this module binds nolchmod.On Windows only the read-only attribute is changed, following
fpUserWrite.See also:
getFilePermissions proc_
proc copyFile(source: Path; dest: Path; options: CopyFlag; bufferSize: int64)Copies a file from
sourcetodest, wheredest.parentDirmust exist.On non-Windows OSes,
optionsspecifies the way the file is copied; by default, ifsourceis a symlink, the file it points to is copied.optionsis ignored on Windows: symlinks are skipped.If
destalready exists, its permissions are kept and its content is overwritten. Otherwise on POSIXdestgets the default permissions of a new file (0o666less the umask); usecopyFileWithPermissions proc_ to carry over those ofsource. On WindowscopyFilealready copies the source file's attributes.bufferSizeis the size of the chunks the content is copied in.See also:
CopyFlag enum_copyFileWithPermissions proc_copyFileToDir proc_
proc copyFileToDir(source: Path; dir: Path; options: CopyFlag; bufferSize: int64)Copies a file
sourceinto directorydir, which must exist.optionsandbufferSizeare as forcopyFile proc_. An emptydirraisesValueErrorrather than meaning the current directory.See also:
copyFile proc_
proc copyFileWithPermissions(source: Path; dest: Path; ignorePermissionErrors: bool; options: CopyFlag)Copies a file from
sourcetodestpreserving its permissions.optionsis as forcopyFile proc_. On Windows this is justcopyFile, which already copies the attributes.On POSIX the permissions are applied after the content is copied, which does not happen atomically. If
ignorePermissionErrorsis true (default), errors while reading or setting them are ignored; otherwise they are raised. A symlink copied as is, or skipped, gets no permissions applied:chmodwould change the file it points to instead.See also:
copyFile proc_getFilePermissions proc_setFilePermissions proc_
proc moveFile(source: Path; dest: Path)Moves a file from
sourcetodest, wheredest.parentDirmust exist.Symlinks are not followed: if
sourceis a symlink, it is itself moved, not its target. Ifdestalready exists, it is overwritten.Can be used to rename files. Within a filesystem the move is a single atomic
rename. Across filesystems (EXDEV) POSIX falls back tocopyFileWithPermissions proc_ followed by removingsource; if that removal fails,destis removed again and the error is raised. Windows letsMoveFileExWcopy across volumes itself.See also:
copyFile proc_copyFileWithPermissions proc_removeFile proc_