Skip to content

Commit 346a589

Browse files
authored
Do not swallow errors on mv (#1576)
1 parent 926ec19 commit 346a589

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The following libraries use ``fsspec`` internally for path and file handling:
5353
maintainable and modular data science code
5454
#. `pyxet`_, a Python library for mounting and
5555
accessing very large datasets from XetHub
56-
#. `Huggingface🤗 Datasets`_, a popular library to
56+
#. `Huggingface🤗 Datasets`_, a popular library to
5757
load&manipulate data for Deep Learning models
5858

5959
``fsspec`` filesystems are also supported by:

fsspec/spec.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,10 @@ def mv(self, path1, path2, recursive=False, maxdepth=None, **kwargs):
11771177
if path1 == path2:
11781178
logger.debug("%s mv: The paths are the same, so no files were moved.", self)
11791179
else:
1180-
self.copy(path1, path2, recursive=recursive, maxdepth=maxdepth)
1180+
# explicitly raise exception to prevent data corruption
1181+
self.copy(
1182+
path1, path2, recursive=recursive, maxdepth=maxdepth, onerror="raise"
1183+
)
11811184
self.rm(path1, recursive=recursive)
11821185

11831186
def rm_file(self, path):

fsspec/tests/abstract/mv.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
3+
import pytest
4+
5+
import fsspec
6+
7+
8+
def test_move_raises_error_with_tmpdir(tmpdir):
9+
# Create a file in the temporary directory
10+
source = tmpdir.join("source_file.txt")
11+
source.write("content")
12+
13+
# Define a destination that simulates a protected or invalid path
14+
destination = tmpdir.join("non_existent_directory/destination_file.txt")
15+
16+
# Instantiate the filesystem (assuming the local file system interface)
17+
fs = fsspec.filesystem("file")
18+
19+
# Use the actual file paths as string
20+
with pytest.raises(FileNotFoundError):
21+
fs.mv(str(source), str(destination))
22+
23+
24+
@pytest.mark.parametrize("recursive", (True, False))
25+
def test_move_raises_error_with_tmpdir_permission(recursive, tmpdir):
26+
# Create a file in the temporary directory
27+
source = tmpdir.join("source_file.txt")
28+
source.write("content")
29+
30+
# Create a protected directory (non-writable)
31+
protected_dir = tmpdir.mkdir("protected_directory")
32+
protected_path = str(protected_dir)
33+
34+
# Set the directory to read-only
35+
if os.name == "nt":
36+
os.system(f'icacls "{protected_path}" /deny Everyone:(W)')
37+
else:
38+
os.chmod(protected_path, 0o555) # Sets the directory to read-only
39+
40+
# Define a destination inside the protected directory
41+
destination = protected_dir.join("destination_file.txt")
42+
43+
# Instantiate the filesystem (assuming the local file system interface)
44+
fs = fsspec.filesystem("file")
45+
46+
# Try to move the file to the read-only directory, expecting a permission error
47+
with pytest.raises(PermissionError):
48+
fs.mv(str(source), str(destination), recursive=recursive)
49+
50+
# Assert the file was not created in the destination
51+
assert not os.path.exists(destination)
52+
53+
# Cleanup: Restore permissions so the directory can be cleaned up
54+
if os.name == "nt":
55+
os.system(f'icacls "{protected_path}" /remove:d Everyone')
56+
else:
57+
os.chmod(protected_path, 0o755) # Restore write permission for cleanup

0 commit comments

Comments
 (0)