-
-
Notifications
You must be signed in to change notification settings - Fork 176
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
File is deleted / empty when moved / copied onto itself #546
Comments
I'm not sure there's any reasonable way that that can be done consistently for different I guess something like from fs import open_fs
from fs.move import move_file
with open_fs("mem://") as currdir:
currdir.makedir("subdir")
currdir.writetext("subdir/test.txt", "Content")
with currdir.opendir("subdir") as subdir:
move_file(currdir, "subdir/test.txt", subdir, "test.txt")
assert subdir.exists("test.txt") might also trigger this "erroneous behaviour"? from fs import open_fs
from fs.move import move_file
with open_fs("mem://") as currdir:
currdir.makedir("subdir")
currdir.writetext("subdir/test.txt", "Content")
with open_fs("mem://") as subdir:
subdir.writetext("test.txt", "Content")
move_file(currdir, "subdir/test.txt", subdir, "test.txt")
assert subdir.exists("test.txt") 🤔 🤷♂️ Although I guess at least for the FTP case (i.e. where we can't get a syspath), you could try getting the download URL https://docs.pyfilesystem.org/en/latest/reference/base.html#fs.base.FS.geturl and use that as an attempt to see if two different |
The For Your second example works fine by the way, because you have two separate filesystems. |
Yes, that's what I was illustrating - the second example "works fine" and the first example "doesn't work"; but as far as the move_file(currdir, "subdir/test.txt", subdir, "test.txt") |
Ah I see, didn't think about that! So some additional checks are needed when encountering a SubFS? (compare to |
from fs import open_fs
with open_fs("mem://") as currdir:
currdir.writetext("test.txt", "Content")
currdir.copy("test.txt", "test.txt", overwrite=True)
assert currdir.readtext("test.txt") == "Content" |
Do similar problems also occur when trying to move or copy directories onto themselves? |
Yep. from fs import open_fs
with open_fs("mem://") as mem:
folder = mem.makedir("folder")
folder.writetext("file1.txt", "Hello1")
sub = folder.makedir("sub")
sub.writetext("file2.txt", "Hello2")
mem.movedir("folder", "folder")
mem.tree()
assert mem.readtext("folder/file1.txt") == "Hello1"
assert mem.readtext("folder/sub/file2.txt") == "Hello2"
from fs import open_fs
with open_fs("mem://") as mem:
folder = mem.makedir("folder")
folder.writetext("file1.txt", "Hello1")
sub = folder.makedir("sub")
sub.writetext("file2.txt", "Hello2")
mem.copydir("folder", "folder/sub/") |
Moving a folder into a subfolder of itself is also not a good idea at the moment. This should raise an exception. |
It hangs forever (gets stuck in an infinite loop) if you try copying a folder into a subfolder of itself, but if you do: from fs import open_fs
with open_fs("mem://") as mem:
folder = mem.makedir("folder")
folder.writetext("file1.txt", "Hello1")
sub = folder.makedir("sub")
sub.writetext("file2.txt", "Hello2")
mem.copydir("folder", "folder")
mem.tree()
assert mem.readtext("folder/file1.txt") == "Hello1"
assert mem.readtext("folder/sub/file2.txt") == "Hello2" then the files get truncated to being empty again. |
Seems like this |
…to-itself Bugfix: File deleted / emptied when moved / copied onto itself (#546)
As discussed in #542 a file is deleted in some cases when moved onto itself. Working example:
This happens in both
fs.base.FS.move
andfs.move.move_file
.Due to the optimization in #523 this does not happen on OSFS, but applies to other filesystems with shared connections. For example when moving a file from one connection to a ftp server onto itself in another connection to the same ftp server.
For a solution a function is needed to check whether two different
(fs, path)
tuples point to the same resource.The text was updated successfully, but these errors were encountered: