You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filesystem.py 990B

5 years ago
123456789101112131415161718192021222324252627282930
  1. import os
  2. import os.path
  3. from pip._internal.utils.compat import get_path_uid
  4. def check_path_owner(path):
  5. # type: (str) -> bool
  6. # If we don't have a way to check the effective uid of this process, then
  7. # we'll just assume that we own the directory.
  8. if not hasattr(os, "geteuid"):
  9. return True
  10. previous = None
  11. while path != previous:
  12. if os.path.lexists(path):
  13. # Check if path is writable by current user.
  14. if os.geteuid() == 0:
  15. # Special handling for root user in order to handle properly
  16. # cases where users use sudo without -H flag.
  17. try:
  18. path_uid = get_path_uid(path)
  19. except OSError:
  20. return False
  21. return path_uid == 0
  22. else:
  23. return os.access(path, os.W_OK)
  24. else:
  25. previous, path = path, os.path.dirname(path)
  26. return False # assume we don't own the path