16741, “ben-albrecht”, “FileSystem.copyTree cannot copy files not owned by user”, “2020-11-18T23:43:29Z”
Summary of Problem
FileSystem.copyTree
fails when copying a file not owned by the user, such as a file owned by root. This is caused by the copyTree
implementation preserving file metadata in the copy calls (https://github.com/chapel-lang/chapel/blob/master/modules/standard/FileSystem.chpl#L562). This results in an attempt to chown
the files back to the original user ID and group ID (https://github.com/chapel-lang/chapel/blob/master/modules/standard/FileSystem.chpl#L343), which might be illegal depending on the user permissions.
As a sanity check, both the unix cp
command and python’s shutil.copytree()
allow copying files owned by root (assuming the user has read access), and do not preserve the metadata (the new files are owned by the user now).
The solution might be to stop preserving the metadata in copyTree()
.
Steps to Reproduce
Source Code:
use FileSystem;
proc main(args: [] string) {
if args.size != 3 {
writeln('usage: cp <src> <dest>');
exit(1);
}
FileSystem.copyTree(args[1], args[2]);
}
Compile command:
chpl cp.chpl
Execution command:
mkdir dir
sudo -i
touch dir/rootfile.txt
exit
./cp dir dir2
uncaught PermissionError: Operation not permitted (in chown with path "dir2/rootfile.txt")
cp.chpl:9: thrown here
cp.chpl:9: uncaught here
Configuration Information
- Output of
chpl --version
: chpl version 1.24.0 pre-release (f07210f6c7)