| 1 |
import os |
| 2 |
import tempfile |
| 3 |
from os.path import abspath, curdir, dirname, join, normcase, sep |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
from django.core.exceptions import SuspiciousFileOperation |
| 7 |
|
| 8 |
|
| 9 |
# Copied verbatim (minus `os.path` fixes) from: |
| 10 |
# https://github.com/python/cpython/pull/23901. |
| 11 |
# Python versions >= PY315 may include this fix, so periodic checks are needed |
| 12 |
# to remove this vendored copy of `makedirs` once solved upstream. |
| 13 |
def makedirs(name, mode=0o777, exist_ok=False, *, parent_mode=None): |
| 14 |
"""makedirs(name [, mode=0o777][, exist_ok=False][, parent_mode=None]) |
| 15 |
|
| 16 |
Super-mkdir; create a leaf directory and all intermediate ones. Works like |
| 17 |
mkdir, except that any intermediate path segment (not just the rightmost) |
| 18 |
will be created if it does not exist. If the target directory already |
| 19 |
exists, raise an OSError if exist_ok is False. Otherwise no exception is |
| 20 |
raised. If parent_mode is not None, it will be used as the mode for any |
| 21 |
newly-created, intermediate-level directories. Otherwise, intermediate |
| 22 |
directories are created with the default permissions (respecting umask). |
| 23 |
This is recursive. |
| 24 |
|
| 25 |
""" |
| 26 |
head, tail = os.path.split(name) |
| 27 |
if not tail: |
| 28 |
head, tail = os.path.split(head) |
| 29 |
if head and tail and not os.path.exists(head): |
| 30 |
try: |
| 31 |
if parent_mode is not None: |
| 32 |
makedirs( |
| 33 |
head, mode=parent_mode, exist_ok=exist_ok, parent_mode=parent_mode |
| 34 |
) |
| 35 |
else: |
| 36 |
makedirs(head, exist_ok=exist_ok) |
| 37 |
except FileExistsError: |
| 38 |
# Defeats race condition when another thread created the path |
| 39 |
pass |
| 40 |
cdir = curdir |
| 41 |
if isinstance(tail, bytes): |
| 42 |
cdir = bytes(curdir, "ASCII") |
| 43 |
if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists |
| 44 |
return |
| 45 |
try: |
| 46 |
os.mkdir(name, mode) |
| 47 |
# PY315: The call to `chmod()` is not in the CPython proposed code. |
| 48 |
# Apply `chmod()` after `mkdir()` to enforce the exact requested |
| 49 |
# permissions, since the kernel masks the mode argument with the |
| 50 |
# process umask. This guarantees consistent directory permissions |
| 51 |
# without mutating global umask state. |
| 52 |
os.chmod(name, mode) |
| 53 |
except OSError: |
| 54 |
# Cannot rely on checking for EEXIST, since the operating system |
| 55 |
# could give priority to other errors like EACCES or EROFS |
| 56 |
if not exist_ok or not os.path.isdir(name): |
| 57 |
raise |
| 58 |
|
| 59 |
|
| 60 |
def safe_makedirs(name, mode=0o777, exist_ok=False): |
| 61 |
"""Create directories recursively with explicit `mode` on each level.""" |
| 62 |
makedirs(name=name, mode=mode, exist_ok=exist_ok, parent_mode=mode) |
| 63 |
|
| 64 |
|
| 65 |
def safe_join(base, *paths): |
| 66 |
""" |
| 67 |
Join one or more path components to the base path component intelligently. |
| 68 |
Return a normalized, absolute version of the final path. |
| 69 |
|
| 70 |
Raise SuspiciousFileOperation if the final path isn't located inside of the |
| 71 |
base path component. |
| 72 |
""" |
| 73 |
final_path = abspath(join(base, *paths)) |
| 74 |
base_path = abspath(base) |
| 75 |
# Ensure final_path starts with base_path (using normcase to ensure we |
| 76 |
# don't false-negative on case insensitive operating systems like Windows), |
| 77 |
# further, one of the following conditions must be true: |
| 78 |
# a) The next character is the path separator (to prevent conditions like |
| 79 |
# safe_join("/dir", "/../d")) |
| 80 |
# b) The final path must be the same as the base path. |
| 81 |
# c) The base path must be the most root path (meaning either "/" or "C:\\") |
| 82 |
if ( |
| 83 |
not normcase(final_path).startswith(normcase(base_path + sep)) |
| 84 |
and normcase(final_path) != normcase(base_path) |
| 85 |
and dirname(normcase(base_path)) != normcase(base_path) |
| 86 |
): |
| 87 |
raise SuspiciousFileOperation( |
| 88 |
"The joined path ({}) is located outside of the base path " |
| 89 |
"component ({})".format(final_path, base_path) |
| 90 |
) |
| 91 |
return final_path |
| 92 |
|
| 93 |
|
| 94 |
def symlinks_supported(): |
| 95 |
""" |
| 96 |
Return whether or not creating symlinks are supported in the host platform |
| 97 |
and/or if they are allowed to be created (e.g. on Windows it requires admin |
| 98 |
permissions). |
| 99 |
""" |
| 100 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 101 |
original_path = os.path.join(temp_dir, "original") |
| 102 |
symlink_path = os.path.join(temp_dir, "symlink") |
| 103 |
os.makedirs(original_path) |
| 104 |
try: |
| 105 |
os.symlink(original_path, symlink_path) |
| 106 |
supported = True |
| 107 |
except (OSError, NotImplementedError): |
| 108 |
supported = False |
| 109 |
return supported |
| 110 |
|
| 111 |
|
| 112 |
def to_path(value): |
| 113 |
"""Convert value to a pathlib.Path instance, if not already a Path.""" |
| 114 |
if isinstance(value, Path): |
| 115 |
return value |
| 116 |
elif not isinstance(value, str): |
| 117 |
raise TypeError("Invalid path type: %s" % type(value).__name__) |
| 118 |
return Path(value) |