Issue 27687: Linux shutil.move between mountpoints as root does not retain ownership (original) (raw)
When using shutil.move() between mounts (i.e. when it does a copy/delete), running as root, ownership should be maintained.
To test: log in as root, create a file owned by a regular user. In python, use shutil.move() to move it to another mountpoint. Check that the ownership of the moved file is the same as the ownership of the original file.
And as it is documented, it would be a change against documentation. However as a stop-gap it is rather trivial to make your own copy function to fix this. copy2 returns the actual destination, so you could do
def copy_with_ownership(src, dest, *, follow_symlinks=True):
actual_dest = copy2(src, dest, follow_symlinks=follow_symlinks)
fix_ownership(src, actual_dest)
return actual_dest
implement fix_ownership to do what it needs to do, and pass copy_with_ownership as the copy_function argument to move.