implement workaround for permission error when copying read-only files that have extended attributes set and using Python 3.6#4642
Conversation
|
I think this is the real problem that I thought I was solving with #4604 |
Co-authored-by: ocaisa <alan.ocais@cecam.org>
|
To me this now looks good, but can we add a test for it? It may not be very thorough, but you should just be able to create a file, change the permissions to read-only and then attempt a copy. I don't know if that is guaranteed to hit the LoC, but it is worth a shot. Should be a variation on easybuild-framework/test/framework/filetools.py Lines 1805 to 1810 in f7035e9 |
|
I have traced the underlying issue to: https://bugs.python.org/issue24538 So this should only appear in python < 3.7 |
|
since the |
|
Like everything, this was more work than it seemed initially! |
|
@ocaisa I've pushed another commit (or two) that actually use the upstream approach to ensure the extended attributes are truly copied as well. A cleaner fix imo |
| # re-enable user write permissions in target, copy xattrs, then remove write perms again | ||
| adjust_permissions(target_path, stat.S_IWUSR) | ||
| shutil._copyxattr(path, target_path) | ||
| adjust_permissions(target_path, stat.S_IWUSR, add=False) |
There was a problem hiding this comment.
Ha, this is correct but quite confusing (add=False means remove the permissions)
There was a problem hiding this comment.
yeah, hence the comment at the top of that block
it's not the most intuitive that add=False removes the permissions
There was a problem hiding this comment.
Should we open an issue to improve the API of adjust_permissions in EasyBuild 5.0?
|
There really should be a dedicated test that verifies whether the changes indeed fix the problem at hand. I puzzled one together, see jfgrimm#1 Unless I'm misunderstanding something, the extra test shows the fix isn't fully working as designed yet, since it fails on Python 3.6.8 in RHEL8... |
| if not os.stat(path).st_mode & stat.S_IWUSR: | ||
| # failure not due to read-only file | ||
| raise EasyBuildError("Failed to copy file %s to %s: %s", path, target_path, err) |
There was a problem hiding this comment.
This part doesn't make sense to me, I think it should be removed...
If we give up here if the source file is read-only, then why bother with the attempt to fix the issue when using Python 3.6 via shutil._copyxattr below?
implement test for `copy_file` to copy read-only file with extended attributes
Ran into this issue like this, when configuring EasyBuild with read-only-installdir:
Having taken a closer look, it seems that on our system
shutil.copy2()spits out aPermissionErrorwhen the file to be copied is read-only. However, the copy still succeeds (and has the correct contents).