Skip to content

Fix SFTPFileSystem.makedirs to properly handle relative paths #1451

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

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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)