filesystem_spec icon indicating copy to clipboard operation
filesystem_spec copied to clipboard

`LocalFileSystem` clobbers file permissions after cp_file

Open russell-horvath opened this issue 1 year ago • 4 comments

Hi,

I am trying to copy a local file with the following permissions:

-rwxrwxr-x 1 rhorvath rhorvath 7994 

after copying the file it becomes:

-rw-rw-r--  1 rhorvath rhorvath 7994

I have narrowed the reason for this down to the cp_file function in LocalFileSystem where shutil.copyfile(path1, path2) doesn't copy the permission mode. This could be solved by using shutil.copy() and preserving the source file's permission mode.

russell-horvath avatar Feb 06 '24 22:02 russell-horvath

could be solved by using shutil.copy() and preserving the source file's permission mode

Am I right in remembering that this does not do atomic operations, or that it operates differently across discs?

copyfile: "in the most efficient way possible" copy: "data and mode bits" (may copy into t directory, not to the specific path given)

martindurant avatar Feb 14 '24 21:02 martindurant

This really should use shutil.copy2: https://docs.python.org/3/library/shutil.html#shutil.copy2

https://stackoverflow.com/questions/20873723/is-pythons-shutil-copyfile-atomic copyfile does not do atomic operations either so nothing will be lost. I think the issue you are worried about is the semantics if the destination path is a directory, but that can be handled with an isdir check.

Alternatively, if you really want to keep shutil.copyfile call, just add a copystat call as well.

shutil.copyfile and shutil.copy can both copy between disks, while the relevant os calls cannot.

Skylion007 avatar Feb 22 '24 18:02 Skylion007

can be handled with an isdir check

We would rather not add checks if possible - it turns out that things can go really slowly with bulk file operations.

Would you like to propose the PR?

martindurant avatar Feb 29 '24 16:02 martindurant

This stackoverflow post gives a good behavior of what the different shutil calls do: https://stackoverflow.com/a/44996087

Skylion007 avatar Mar 05 '24 16:03 Skylion007