Skip to content

Commit

Permalink
fix SFTPFileSystem.makedirs to properly handle relative paths instead…
Browse files Browse the repository at this point in the history
… of creating directories with absolute paths (#1451)
  • Loading branch information
sasano8 committed Dec 7, 2023
1 parent 5cfec6f commit 30e479b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
9 changes: 5 additions & 4 deletions fsspec/implementations/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ def makedirs(self, path, exist_ok=False, mode=511):
raise FileExistsError(f"File exists: {path}")

parts = path.split("/")
path = ""
new_path = "/" if path[:1] == "/" else ""

for part in parts:
path += f"/{part}"
if not self.exists(path):
self.ftp.mkdir(path, mode)
if part:
new_path = f"{new_path}/{part}" if new_path else part
if not self.exists(new_path):
self.ftp.mkdir(new_path, mode)

def rmdir(self, path):
logger.debug("Removing folder %s", path)
Expand Down
29 changes: 17 additions & 12 deletions fsspec/implementations/tests/test_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,32 @@ def test_transaction(ssh, root_path):
f.rm(root_path, recursive=True)


def test_mkdir_create_parent(ssh):
@pytest.mark.parametrize("path", ["/a/b/c", "a/b/c"])
def test_mkdir_create_parent(ssh, path):
f = fsspec.get_filesystem_class("sftp")(**ssh)

with pytest.raises(FileNotFoundError):
f.mkdir("/a/b/c")
f.mkdir(path)

f.mkdir("/a/b/c", create_parents=True)
assert f.exists("/a/b/c")
f.mkdir(path, create_parents=True)
assert f.exists(path)

with pytest.raises(FileExistsError, match="/a/b/c"):
f.mkdir("/a/b/c")
with pytest.raises(FileExistsError, match=path):
f.mkdir(path)

f.rm("/a/b/c", recursive=True)
f.rm(path, recursive=True)
assert not f.exists(path)


def test_makedirs_exist_ok(ssh):
@pytest.mark.parametrize("path", ["/a/b/c", "a/b/c"])
def test_makedirs_exist_ok(ssh, path):
f = fsspec.get_filesystem_class("sftp")(**ssh)

f.makedirs("/a/b/c")
f.makedirs(path)

with pytest.raises(FileExistsError, match="/a/b/c"):
f.makedirs("/a/b/c", exist_ok=False)
with pytest.raises(FileExistsError, match=path):
f.makedirs(path, exist_ok=False)

f.makedirs("/a/b/c", exist_ok=True)
f.makedirs(path, exist_ok=True)
f.rm(path, recursive=True)
assert not f.exists(path)

0 comments on commit 30e479b

Please sign in to comment.