Skip to content

Commit 59e8576

Browse files
bpo-42782: Fail fast for permission errors in shutil.move() (GH-24001)
* Fail fast in shutil.move() to avoid creating destination directories on failure. Co-authored-by: Zackery Spytz <[email protected]> (cherry picked from commit 132131b) Co-authored-by: Winson Luk <[email protected]>
1 parent 9c6c5da commit 59e8576

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Lib/shutil.py

+11
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,12 @@ def move(src, dst, copy_function=copy2):
798798
if _destinsrc(src, dst):
799799
raise Error("Cannot move a directory '%s' into itself"
800800
" '%s'." % (src, dst))
801+
if (_is_immutable(src)
802+
or (not os.access(src, os.W_OK) and os.listdir(src)
803+
and sys.platform == 'darwin')):
804+
raise PermissionError("Cannot move the non-empty directory "
805+
"'%s': Lacking write permission to '%s'."
806+
% (src, src))
801807
copytree(src, real_dst, copy_function=copy_function,
802808
symlinks=True)
803809
rmtree(src)
@@ -815,6 +821,11 @@ def _destinsrc(src, dst):
815821
dst += os.path.sep
816822
return dst.startswith(src)
817823

824+
def _is_immutable(src):
825+
st = _stat(src)
826+
immutable_states = [stat.UF_IMMUTABLE, stat.SF_IMMUTABLE]
827+
return hasattr(st, 'st_flags') and st.st_flags in immutable_states
828+
818829
def _get_gid(name):
819830
"""Returns a gid, given a group name."""
820831
if getgrnam is None or name is None:

Lib/test/test_shutil.py

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from test.support import TESTFN, FakePath
3434

3535
TESTFN2 = TESTFN + "2"
36+
TESTFN_SRC = TESTFN + "_SRC"
37+
TESTFN_DST = TESTFN + "_DST"
3638
MACOS = sys.platform.startswith("darwin")
3739
AIX = sys.platform[:3] == 'aix'
3840
try:
@@ -2053,6 +2055,41 @@ def _copy(src, dst):
20532055
self.assertEqual(len(moved), 3)
20542056

20552057

2058+
@unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0
2059+
and hasattr(os, 'lchflags')
2060+
and hasattr(stat, 'SF_IMMUTABLE')
2061+
and hasattr(stat, 'UF_OPAQUE'),
2062+
'root privileges required')
2063+
def test_move_dir_permission_denied(self):
2064+
# bpo-42782: shutil.move should not create destination directories
2065+
# if the source directory cannot be removed.
2066+
try:
2067+
os.mkdir(TESTFN_SRC)
2068+
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE)
2069+
2070+
# Testing on an empty immutable directory
2071+
# TESTFN_DST should not exist if shutil.move failed
2072+
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST)
2073+
self.assertFalse(TESTFN_DST in os.listdir())
2074+
2075+
# Create a file and keep the directory immutable
2076+
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE)
2077+
os_helper.create_empty_file(os.path.join(TESTFN_SRC, 'child'))
2078+
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE)
2079+
2080+
# Testing on a non-empty immutable directory
2081+
# TESTFN_DST should not exist if shutil.move failed
2082+
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST)
2083+
self.assertFalse(TESTFN_DST in os.listdir())
2084+
finally:
2085+
if os.path.exists(TESTFN_SRC):
2086+
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE)
2087+
os_helper.rmtree(TESTFN_SRC)
2088+
if os.path.exists(TESTFN_DST):
2089+
os.lchflags(TESTFN_DST, stat.UF_OPAQUE)
2090+
os_helper.rmtree(TESTFN_DST)
2091+
2092+
20562093
class TestCopyFile(unittest.TestCase):
20572094

20582095
_delete = False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fail fast in :func:`shutil.move()` to avoid creating destination directories on
2+
failure.

0 commit comments

Comments
 (0)