Skip to content

Commit 30e479b

Browse files
authored
fix SFTPFileSystem.makedirs to properly handle relative paths instead of creating directories with absolute paths (#1451)
1 parent 5cfec6f commit 30e479b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

fsspec/implementations/sftp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ def makedirs(self, path, exist_ok=False, mode=511):
8080
raise FileExistsError(f"File exists: {path}")
8181

8282
parts = path.split("/")
83-
path = ""
83+
new_path = "/" if path[:1] == "/" else ""
8484

8585
for part in parts:
86-
path += f"/{part}"
87-
if not self.exists(path):
88-
self.ftp.mkdir(path, mode)
86+
if part:
87+
new_path = f"{new_path}/{part}" if new_path else part
88+
if not self.exists(new_path):
89+
self.ftp.mkdir(new_path, mode)
8990

9091
def rmdir(self, path):
9192
logger.debug("Removing folder %s", path)

fsspec/implementations/tests/test_sftp.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,27 +200,32 @@ def test_transaction(ssh, root_path):
200200
f.rm(root_path, recursive=True)
201201

202202

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

206207
with pytest.raises(FileNotFoundError):
207-
f.mkdir("/a/b/c")
208+
f.mkdir(path)
208209

209-
f.mkdir("/a/b/c", create_parents=True)
210-
assert f.exists("/a/b/c")
210+
f.mkdir(path, create_parents=True)
211+
assert f.exists(path)
211212

212-
with pytest.raises(FileExistsError, match="/a/b/c"):
213-
f.mkdir("/a/b/c")
213+
with pytest.raises(FileExistsError, match=path):
214+
f.mkdir(path)
214215

215-
f.rm("/a/b/c", recursive=True)
216+
f.rm(path, recursive=True)
217+
assert not f.exists(path)
216218

217219

218-
def test_makedirs_exist_ok(ssh):
220+
@pytest.mark.parametrize("path", ["/a/b/c", "a/b/c"])
221+
def test_makedirs_exist_ok(ssh, path):
219222
f = fsspec.get_filesystem_class("sftp")(**ssh)
220223

221-
f.makedirs("/a/b/c")
224+
f.makedirs(path)
222225

223-
with pytest.raises(FileExistsError, match="/a/b/c"):
224-
f.makedirs("/a/b/c", exist_ok=False)
226+
with pytest.raises(FileExistsError, match=path):
227+
f.makedirs(path, exist_ok=False)
225228

226-
f.makedirs("/a/b/c", exist_ok=True)
229+
f.makedirs(path, exist_ok=True)
230+
f.rm(path, recursive=True)
231+
assert not f.exists(path)

0 commit comments

Comments
 (0)