Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: File deleted / emptied when moved / copied onto itself (#546) #547

Merged
Merged
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
@@ -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:
@@ -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
# attempt using sendfile
try:
# initialise variables to pass to sendfile
@@ -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 ------------

22 changes: 22 additions & 0 deletions fs/test.py
Original file line number Diff line number Diff line change
@@ -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")