Skip to content

Commit 2818611

Browse files
authored
FTP: preserve filenames containing whitespace in _mlsd2 (#2043)
1 parent c879384 commit 2818611

2 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎fsspec/implementations/ftp.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,21 @@ def _mlsd2(ftp, path="."):
416416
minfo = []
417417
ftp.dir(path, lines.append)
418418
for line in lines:
419-
split_line = line.split()
419+
split_line = line.split(maxsplit=8)
420420
if len(split_line) < 9:
421421
continue
422+
name = split_line[8]
423+
unix_mode = split_line[0]
424+
if unix_mode[0] == "l" and " -> " in name:
425+
# Symbolic link: "<name> -> <target>"; keep only the link name.
426+
name = name.split(" -> ", 1)[0]
422427
this = (
423-
split_line[-1],
428+
name,
424429
{
425430
"modify": " ".join(split_line[5:8]),
426431
"unix.owner": split_line[2],
427432
"unix.group": split_line[3],
428-
"unix.mode": split_line[0],
433+
"unix.mode": unix_mode,
429434
"size": split_line[4],
430435
},
431436
)

‎fsspec/implementations/tests/test_ftp.py‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import fsspec
1010
from fsspec import open_files
11-
from fsspec.implementations.ftp import FTPFileSystem, ImplicitFTPTLS
11+
from fsspec.implementations.ftp import FTPFileSystem, ImplicitFTPTLS, _mlsd2
1212

1313
ftplib = pytest.importorskip("ftplib")
1414
here = os.path.dirname(os.path.abspath(__file__))
@@ -246,3 +246,85 @@ def test_rm_get_recursive(ftp_writable, tmpdir):
246246

247247
fs.rm("/tmp/topdir", recursive=True)
248248
assert not fs.exists("/tmp/topdir")
249+
250+
251+
class _FakeDirFTP:
252+
"""Minimal stand-in for ftplib.FTP whose .dir(path, callback) replays
253+
a canned `ls -l` style listing — enough to exercise the _mlsd2 parser
254+
without needing an FTP server that disables MLSD."""
255+
256+
def __init__(self, lines):
257+
self._lines = lines
258+
259+
def dir(self, path, callback):
260+
for line in self._lines:
261+
callback(line)
262+
263+
264+
def test_mlsd2_parses_filename_without_spaces():
265+
ftp = _FakeDirFTP(["-rw-r--r-- 1 owner group 38283 Jan 12 07:08 plain.pdf"])
266+
out = _mlsd2(ftp, "/data")
267+
assert out == [
268+
(
269+
"plain.pdf",
270+
{
271+
"modify": "Jan 12 07:08",
272+
"unix.owner": "owner",
273+
"unix.group": "group",
274+
"unix.mode": "-rw-r--r--",
275+
"size": "38283",
276+
"type": "file",
277+
},
278+
)
279+
]
280+
281+
282+
def test_mlsd2_parses_filename_with_spaces():
283+
# Regression test: when the FTP server doesn't support MLSD, _mlsd2 falls
284+
# back to parsing `dir` output. Previously it took split_line[-1] as the
285+
# filename, which truncated names containing whitespace.
286+
ftp = _FakeDirFTP(
287+
[
288+
"-rw-r--r-- 1 owner group 38283 Jan 12 07:08 my file.pdf",
289+
"drwxr-xr-x 2 owner group 4096 Mar 15 10:30 a dir with spaces",
290+
"-rw-r--r-- 1 owner group 100 Feb 3 09:15 trailing spaces.txt",
291+
]
292+
)
293+
out = _mlsd2(ftp, "/data")
294+
names = [name for name, _ in out]
295+
assert names == [
296+
"my file.pdf",
297+
"a dir with spaces",
298+
"trailing spaces.txt",
299+
]
300+
assert out[0][1]["type"] == "file"
301+
# _mlsd2 itself reports "dir"; ls() promotes it to "directory" later.
302+
assert out[1][1]["type"] == "dir"
303+
assert out[2][1]["size"] == "100"
304+
305+
306+
def test_mlsd2_parses_symlink_name_without_target():
307+
# `ls -l` formats symlinks as "<name> -> <target>". Only the link's own
308+
# name should be returned; the previous parser took split_line[-1] and so
309+
# returned the target instead of the name.
310+
ftp = _FakeDirFTP(
311+
["lrwxrwxrwx 1 owner group 10 Jan 12 07:08 link name -> some/target"]
312+
)
313+
out = _mlsd2(ftp, "/data")
314+
assert len(out) == 1
315+
name, info = out[0]
316+
assert name == "link name"
317+
assert info["unix.mode"].startswith("l")
318+
319+
320+
def test_mlsd2_skips_short_lines():
321+
# The `total N` summary line emitted by some servers has fewer than 9
322+
# fields and should be ignored.
323+
ftp = _FakeDirFTP(
324+
[
325+
"total 4",
326+
"-rw-r--r-- 1 owner group 1 Jan 12 07:08 keep.txt",
327+
]
328+
)
329+
out = _mlsd2(ftp, "/data")
330+
assert [name for name, _ in out] == ["keep.txt"]

0 commit comments

Comments
 (0)