Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutil.copystat fails with PermissionError in WSL #82814

Closed
peter-schmidbauer mannequin opened this issue Oct 29, 2019 · 5 comments
Closed

shutil.copystat fails with PermissionError in WSL #82814

peter-schmidbauer mannequin opened this issue Oct 29, 2019 · 5 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes easy OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@peter-schmidbauer
Copy link
Mannequin

peter-schmidbauer mannequin commented Oct 29, 2019

BPO 38633
Nosy @pfmoore, @tjguk, @zware, @zooba, @ben-spiller, @peter-schmidbauer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-10-29.14:34:38.648>
labels = ['3.7', '3.8', 'type-bug', 'library', 'OS-windows']
title = 'shutil.copystat fails with PermissionError in WSL'
updated_at = <Date 2022-01-20.01:11:33.016>
user = 'https://github.com/peter-schmidbauer'

bugs.python.org fields:

activity = <Date 2022-01-20.01:11:33.016>
actor = 'sam.park'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'Windows']
creation = <Date 2019-10-29.14:34:38.648>
creator = 'pspeter'
dependencies = []
files = []
hgrepos = []
issue_num = 38633
keywords = []
message_count = 3.0
messages = ['355655', '365377', '410998']
nosy_count = 7.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'steve.dower', 'benspiller', 'pspeter', 'sam.park']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue38633'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

Linked PRs

@peter-schmidbauer
Copy link
Mannequin Author

peter-schmidbauer mannequin commented Oct 29, 2019

Using shutil.copystat (and therefore also shutil.copytree) in WSL on any directories from a mounted Windows drive (i.e. /mnt/c/...) raises an shutil.Error "[Errno 13] Permission denied".

It seems to fail when copying the extended filesystem attribute "system.wsl_case_sensitive" using os.setxattr() here:

os.setxattr(dst, name, value, follow_symlinks=follow_symlinks)

Note that this only happens when both src and dst are on the mounted drive. If only one is on the drive and the other is e.g. in the home directory /home/username/, it will not raise an error.

Quick way to test:

cd /mnt/c/ && mkdir test1 && mkdir test2 && python -c "import shutil; shutil.copystat('test1', 'test2')"

@peter-schmidbauer peter-schmidbauer mannequin added 3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir OS-windows type-bug An unexpected behavior, bug, or error labels Oct 29, 2019
@ben-spiller
Copy link
Mannequin

ben-spiller mannequin commented Mar 31, 2020

Looks like on WSL the errno is errno.EACCES rather than EPERM, so we just need to change the shutil._copyxattr error handler to also cope with that error code:

             except OSError as e:
-                 if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA):
+                 if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA, errno.EACCES):
                     raise

If anyone needs a workaround until this is fixed in shutil itself, you can do it by monkey-patching _copyxattr:

import errno, shutil
# have to monkey patch to work with WSL as workaround for https://bugs.python.org/issue38633
orig_copyxattr = shutil._copyxattr
def patched_copyxattr(src, dst, *, follow_symlinks=True):
	try:
		orig_copyxattr(src, dst, follow_symlinks=follow_symlinks)
	except OSError as ex:
		if ex.errno != errno.EACCES: raise
shutil._copyxattr = patched_copyxattr

@sampark
Copy link
Mannequin

sampark mannequin commented Jan 20, 2022

FWIW I just ran into this today on Ubuntu 18.04 container on GKE 1.21.5-gke.1302 and on a Ubuntu-with-Docker underlying node (if that's relevant). Applying the monkeypatch solves the issue as well.

@zooba
Copy link
Member

zooba commented Jan 9, 2023

This seems like a nice easy contribution opportunity. Given the other errors that are silently handled, also handling EACCES seems fine to me.

@gpshead gpshead self-assigned this Apr 24, 2023
gpshead pushed a commit that referenced this issue Apr 25, 2023
gh-82814: Adds `errno.EACCES` to the list of ignored errors on
`_copyxattr`.  EPERM and EACCES are different constants but
in general should be treated the same.

News entry authored by: Gregory P. Smith <greg@krypto.org>
@ethanhs
Copy link
Contributor

ethanhs commented Jun 26, 2023

Looks like this was fixed in #103790 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes easy OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants