Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions fs/osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _check_copy(self, src_path, dst_path, overwrite=False):
if self.gettype(src_path) is not ResourceType.file:
raise errors.FileExpected(src_path)
# check dst_path does not exist if we are not overwriting
if not overwrite and self.exists(_dst_path):
if not overwrite and _src_path != _dst_path and self.exists(_dst_path):
raise errors.DestinationExists(dst_path)
# check parent dir of _dst_path exists and is a directory
if self.gettype(dirname(dst_path)) is not ResourceType.directory:
Expand Down Expand Up @@ -440,6 +440,9 @@ def copy(self, src_path, dst_path, overwrite=False, preserve_time=False):
self.getsyspath(_src_path),
self.getsyspath(_dst_path),
)
# exit early if we copy the file onto itself
if overwrite and _src_sys == _dst_sys:
return
Comment thread
tfeldmann marked this conversation as resolved.
Outdated
# attempt using sendfile
try:
# initialise variables to pass to sendfile
Expand Down Expand Up @@ -467,7 +470,14 @@ def copy(self, src_path, dst_path, overwrite=False, preserve_time=False):
# type: (Text, Text, bool, bool) -> None
with self._lock:
_src_path, _dst_path = self._check_copy(src_path, dst_path, overwrite)
shutil.copy2(self.getsyspath(_src_path), self.getsyspath(_dst_path))
_src_sys, _dst_sys = (
self.getsyspath(_src_path),
self.getsyspath(_dst_path),
)
# exit early if we copy the file onto itself
if overwrite and _src_sys == _dst_sys:
return
shutil.copy2(_src_sys, _dst_sys)

# --- Backport of os.scandir for Python < 3.5 ------------

Expand Down
22 changes: 22 additions & 0 deletions fs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,28 @@ def test_move_file_mem(self):
def test_move_file_temp(self):
self._test_move_file("temp://")

def test_move_file_onto_itself(self):
self.fs.writetext("file.txt", "Hello")
self.fs.move("file.txt", "file.txt", overwrite=True)
self.assert_text("file.txt", "Hello")

def test_move_file_onto_itself_relpath(self):
subdir = self.fs.makedir("sub")
subdir.writetext("file.txt", "Hello")
self.fs.move("sub/file.txt", "sub/../sub/file.txt", overwrite=True)
self.assert_text("sub/file.txt", "Hello")

def test_copy_file_onto_itself(self):
self.fs.writetext("file.txt", "Hello")
self.fs.copy("file.txt", "file.txt", overwrite=True)
self.assert_text("file.txt", "Hello")

def test_copy_file_onto_itself_relpath(self):
subdir = self.fs.makedir("sub")
subdir.writetext("file.txt", "Hello")
self.fs.copy("sub/file.txt", "sub/../sub/file.txt", overwrite=True)
self.assert_text("sub/file.txt", "Hello")

def test_copydir(self):
self.fs.makedirs("foo/bar/baz/egg")
self.fs.writetext("foo/bar/foofoo.txt", "Hello")
Expand Down