Skip to content

Commit 2668bfc

Browse files
committed
raise DestinationExists if overwrite is not set
1 parent b4f2dc4 commit 2668bfc

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

fs/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ def copy(
425425
with self._lock:
426426
_src_path = self.validatepath(src_path)
427427
_dst_path = self.validatepath(dst_path)
428-
if _src_path == _dst_path:
429-
raise errors.IllegalDestination(dst_path)
430428
if not overwrite and self.exists(_dst_path):
431429
raise errors.DestinationExists(dst_path)
430+
if _src_path == _dst_path:
431+
raise errors.IllegalDestination(dst_path)
432432
with closing(self.open(_src_path, "rb")) as read_file:
433433
# FIXME(@althonos): typing complains because open return IO
434434
self.upload(_dst_path, read_file) # type: ignore

fs/copy.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,13 @@ def copy_file_internal(
257257
lock (bool): Lock both filesystems before copying.
258258
259259
"""
260+
_src_path = src_fs.validatepath(src_path)
261+
_dst_path = dst_fs.validatepath(dst_path)
260262
if src_fs is dst_fs:
261-
# Same filesystem, so we can do a potentially optimized
262-
# copy
263+
# It's not allowed to copy a file onto itself
264+
if _src_path == _dst_path:
265+
raise IllegalDestination(dst_path)
266+
# Same filesystem, so we can do a potentially optimized copy
263267
src_fs.copy(src_path, dst_path, overwrite=True, preserve_time=preserve_time)
264268
return
265269

@@ -438,8 +442,6 @@ def copy_dir_if(
438442
dst_fs, create=True
439443
) as _dst_fs:
440444
with _src_fs.lock(), _dst_fs.lock():
441-
if src_fs == dst_fs and isbase(_src_path, _dst_path):
442-
raise IllegalDestination(dst_path)
443445
_thread_safe = is_thread_safe(_src_fs, _dst_fs)
444446
with Copier(
445447
num_workers=workers if _thread_safe else 0, preserve_time=preserve_time

fs/osfs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,12 @@ def _check_copy(self, src_path, dst_path, overwrite=False):
408408
# check src_path exists and is a file
409409
if self.gettype(src_path) is not ResourceType.file:
410410
raise errors.FileExpected(src_path)
411-
# it's not allowed to copy a file onto itself
412-
if _src_path == _dst_path:
413-
raise errors.IllegalDestination(dst_path)
414411
# check dst_path does not exist if we are not overwriting
415412
if not overwrite and self.exists(_dst_path):
416413
raise errors.DestinationExists(dst_path)
414+
# it's not allowed to copy a file onto itself
415+
if _src_path == _dst_path:
416+
raise errors.IllegalDestination(dst_path)
417417
# check parent dir of _dst_path exists and is a directory
418418
if self.gettype(dirname(dst_path)) is not ResourceType.directory:
419419
raise errors.DirectoryExpected(dirname(dst_path))

fs/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ def test_copy_file_onto_itself(self):
18321832
self.fs.writetext("file.txt", "Hello")
18331833
with self.assertRaises(errors.IllegalDestination):
18341834
self.fs.copy("file.txt", "file.txt", overwrite=True)
1835-
with self.assertRaises(errors.IllegalDestination):
1835+
with self.assertRaises(errors.DestinationExists):
18361836
self.fs.copy("file.txt", "file.txt", overwrite=False)
18371837
self.assert_text("file.txt", "Hello")
18381838

@@ -1841,7 +1841,7 @@ def test_copy_file_onto_itself_relpath(self):
18411841
subdir.writetext("file.txt", "Hello")
18421842
with self.assertRaises(errors.IllegalDestination):
18431843
self.fs.copy("sub/file.txt", "sub/../sub/file.txt", overwrite=True)
1844-
with self.assertRaises(errors.IllegalDestination):
1844+
with self.assertRaises(errors.DestinationExists):
18451845
self.fs.copy("sub/file.txt", "sub/../sub/file.txt", overwrite=False)
18461846
self.assert_text("sub/file.txt", "Hello")
18471847

fs/wrapfs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ def copy(self, src_path, dst_path, overwrite=False, preserve_time=False):
270270
_val_src_path = self.validatepath(_src_path)
271271
_val_dst_path = self.validatepath(_dst_path)
272272
with unwrap_errors({_src_path: src_path, _dst_path: dst_path}):
273-
if src_fs == dst_fs and _val_src_path == _val_dst_path:
274-
raise errors.IllegalDestination(_dst_path)
275273
if not overwrite and dst_fs.exists(_dst_path):
276274
raise errors.DestinationExists(_dst_path)
277275
copy_file(src_fs, _src_path, dst_fs, _dst_path, preserve_time=preserve_time)

0 commit comments

Comments
 (0)