Merge pull request #1416 from gradha/pr_copy_dir_permissions
Adds copyDirWithPermissions()
This commit is contained in:
commit
5c43fbc985
1 changed files with 53 additions and 12 deletions
|
|
@ -955,11 +955,12 @@ proc copyFile*(source, dest: string) {.rtl, extern: "nos$1",
|
||||||
##
|
##
|
||||||
## If this fails, `EOS` is raised. On the Windows platform this proc will
|
## If this fails, `EOS` is raised. On the Windows platform this proc will
|
||||||
## copy the source file's attributes into dest. On other platforms you need
|
## copy the source file's attributes into dest. On other platforms you need
|
||||||
## to use getFilePermissions and setFilePermissions to copy them by hand (or
|
## to use `getFilePermissions() <#getFilePermissions>`_ and
|
||||||
## use the convenience copyFileWithPermissions() proc), otherwise `dest` will
|
## `setFilePermissions() <#setFilePermissions>`_ to copy them by hand (or use
|
||||||
## inherit the default permissions of a newly created file for the user. If
|
## the convenience `copyFileWithPermissions() <#copyFileWithPermissions>`_
|
||||||
## `dest` already exists, the file attributes will be preserved and the
|
## proc), otherwise `dest` will inherit the default permissions of a newly
|
||||||
## content overwritten.
|
## created file for the user. If `dest` already exists, the file attributes
|
||||||
|
## will be preserved and the content overwritten.
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
when useWinUnicode:
|
when useWinUnicode:
|
||||||
let s = newWideCString(source)
|
let s = newWideCString(source)
|
||||||
|
|
@ -1363,7 +1364,13 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
|
||||||
|
|
||||||
proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
|
proc copyDir*(source, dest: string) {.rtl, extern: "nos$1",
|
||||||
tags: [FWriteIO, FReadIO].} =
|
tags: [FWriteIO, FReadIO].} =
|
||||||
## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised.
|
## Copies a directory from `source` to `dest`.
|
||||||
|
##
|
||||||
|
## If this fails, `EOS` is raised. On the Windows platform this proc will
|
||||||
|
## copy the attributes from `source` into `dest`. On other platforms created
|
||||||
|
## files and directories will inherit the default permissions of a newly
|
||||||
|
## created file/directory for the user. To preserve attributes recursively on
|
||||||
|
## these platforms use `copyDirWithPermissions() <#copyDirWithPermissions>`_.
|
||||||
createDir(dest)
|
createDir(dest)
|
||||||
for kind, path in walkDir(source):
|
for kind, path in walkDir(source):
|
||||||
var noSource = path.substr(source.len()+1)
|
var noSource = path.substr(source.len()+1)
|
||||||
|
|
@ -1507,14 +1514,17 @@ proc copyFileWithPermissions*(source, dest: string,
|
||||||
ignorePermissionErrors = true) =
|
ignorePermissionErrors = true) =
|
||||||
## Copies a file from `source` to `dest` preserving file permissions.
|
## Copies a file from `source` to `dest` preserving file permissions.
|
||||||
##
|
##
|
||||||
## This is a wrapper proc around copyFile, getFilePermissions and
|
## This is a wrapper proc around `copyFile() <#copyFile>`_,
|
||||||
## setFilePermissions on non Windows platform. On windows this proc is just a
|
## `getFilePermissions() <#getFilePermissions>`_ and `setFilePermissions()
|
||||||
## wrapper for copyFile since that proc already copies attributes.
|
## <#setFilePermissions>`_ on non Windows platform. On Windows this proc is
|
||||||
|
## just a wrapper for `copyFile() <#copyFile>`_ since that proc already
|
||||||
|
## copies attributes.
|
||||||
##
|
##
|
||||||
## On non windows systems permissions are copied after the file itself has
|
## On non Windows systems permissions are copied after the file itself has
|
||||||
## been copied, which won't happen atomically and could lead to a race
|
## been copied, which won't happen atomically and could lead to a race
|
||||||
## condition. If ignorePermissionErrors is true, errors while reading/setting
|
## condition. If `ignorePermissionErrors` is true, errors while
|
||||||
## file attributes will be ignored, otherwise will raise `OSError`.
|
## reading/setting file attributes will be ignored, otherwise will raise
|
||||||
|
## `OSError`.
|
||||||
copyFile(source, dest)
|
copyFile(source, dest)
|
||||||
when not defined(Windows):
|
when not defined(Windows):
|
||||||
try:
|
try:
|
||||||
|
|
@ -1523,6 +1533,37 @@ proc copyFileWithPermissions*(source, dest: string,
|
||||||
if not ignorePermissionErrors:
|
if not ignorePermissionErrors:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
proc copyDirWithPermissions*(source, dest: string,
|
||||||
|
ignorePermissionErrors = true) {.rtl, extern: "nos$1",
|
||||||
|
tags: [FWriteIO, FReadIO].} =
|
||||||
|
## Copies a directory from `source` to `dest` preserving file permissions.
|
||||||
|
##
|
||||||
|
## If this fails, `EOS` is raised. This is a wrapper proc around `copyDir()
|
||||||
|
## <#copyDir>`_ and `copyFileWithPermissions() <#copyFileWithPermissions>`_
|
||||||
|
## on non Windows platforms. On Windows this proc is just a wrapper for
|
||||||
|
## `copyDir() <#copyDir>`_ since that proc already copies attributes.
|
||||||
|
##
|
||||||
|
## On non Windows systems permissions are copied after the file or directory
|
||||||
|
## itself has been copied, which won't happen atomically and could lead to a
|
||||||
|
## race condition. If `ignorePermissionErrors` is true, errors while
|
||||||
|
## reading/setting file attributes will be ignored, otherwise will raise
|
||||||
|
## `OSError`.
|
||||||
|
createDir(dest)
|
||||||
|
when not defined(Windows):
|
||||||
|
try:
|
||||||
|
setFilePermissions(dest, getFilePermissions(source))
|
||||||
|
except:
|
||||||
|
if not ignorePermissionErrors:
|
||||||
|
raise
|
||||||
|
for kind, path in walkDir(source):
|
||||||
|
var noSource = path.substr(source.len()+1)
|
||||||
|
case kind
|
||||||
|
of pcFile:
|
||||||
|
copyFileWithPermissions(path, dest / noSource, ignorePermissionErrors)
|
||||||
|
of pcDir:
|
||||||
|
copyDirWithPermissions(path, dest / noSource, ignorePermissionErrors)
|
||||||
|
else: discard
|
||||||
|
|
||||||
proc inclFilePermissions*(filename: string,
|
proc inclFilePermissions*(filename: string,
|
||||||
permissions: set[TFilePermission]) {.
|
permissions: set[TFilePermission]) {.
|
||||||
rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} =
|
rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue