Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/python/lftp/lftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def __init__(
self.__timeout = 10 # in seconds
self.__consecutive_status_errors = 0
self.__consecutive_timeouts = 0
# Set by __run_command: its "" on a timeout reads the same as no output
self.__last_command_timed_out = False
self.__settings_cache: dict[str, str] = {}

self.__log_command_output = False
Expand Down Expand Up @@ -289,6 +291,7 @@ def __run_command(self, command: str): # type: ignore[arg-type]
self.__process.expect(self.__expect_pattern, timeout=self.__timeout)
except pexpect.exceptions.TIMEOUT:
timed_out = True
self.__last_command_timed_out = timed_out
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if timed_out:
self.__consecutive_timeouts += 1
Expand Down Expand Up @@ -325,6 +328,8 @@ def __run_command(self, command: str): # type: ignore[arg-type]
except pexpect.exceptions.TIMEOUT:
self.logger.warning("Lftp timeout while consuming error output")
self.__pending_error = redact_credentials(error_out)
# Same as the first-read timeout: empty output must not parse as "no jobs"
self.__last_command_timed_out = True
return ""
before = self.__process.before
assert isinstance(before, bytes)
Expand Down Expand Up @@ -577,12 +582,15 @@ def xfer_verify_command(self, command: str):
def status(self) -> list[LftpJobStatus] | None:
"""
Return a status list of queued and running jobs.
Returns None when the status output could not be parsed, so callers
Returns None when the status output timed out or could not be parsed, so callers
can distinguish "no jobs" from "parse failed" and avoid false
completion signals.
:return:
"""
out = self.__run_command("jobs -v") # type: ignore[arg-type]
if self.__last_command_timed_out:
# Empty output here isn't "no jobs"; it would read as every job finished
return None
try:
statuses = self.__job_status_parser.parse(out)
self.__consecutive_status_errors = 0
Expand Down
5 changes: 4 additions & 1 deletion src/python/system/file_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def apply_umask_modes_to(base: str, entries: Iterable[tuple[str, bool]], umask:
dir_mode = 0o777 & ~umask
# Deepest first, for the same reason apply_umask_modes walks bottom-up.
for rel, is_dir in sorted(entries, key=lambda e: e[0].count(os.sep), reverse=True):
_chmod(os.path.join(base, rel), dir_mode if is_dir else file_mode, logger)
# A single file's entry is "." and "file/." is ENOTDIR. Don't normpath the
# join: it would collapse ".." lexically, which differs from the kernel's
# resolution when a symlink precedes it in base.
_chmod(base if rel == "." else os.path.join(base, rel), dir_mode if is_dir else file_mode, logger)


def relative_entries(root: str) -> list[tuple[str, bool]]:
Expand Down
18 changes: 18 additions & 0 deletions src/python/tests/unittests/test_controller/test_move_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,21 @@ def test_merge_into_existing_destination_leaves_other_files_alone(self):
self.assertEqual(0o600, stat.S_IMODE(os.stat(private).st_mode))
# The moved file gets the UMASK treatment
self.assertEqual(0o666, stat.S_IMODE(os.stat(os.path.join(dst_tree, "ep2.mkv")).st_mode))

def test_single_file_move_applies_umask_mode(self):
"""#811: a moved single file gets 0666 & ~umask. Its only entry is ".",
which used to make the chmod target "file/." and fail with ENOTDIR."""
import stat

src_file = os.path.join(self.src_dir, "movie.mkv")
with open(src_file, "w") as f:
f.write("x")
os.chmod(src_file, 0o664)

process = self._make_process(
source_path=self.src_dir, dest_path=self.dst_dir, file_name="movie.mkv", umask=0o027
)
self._run_process(process)

self.assertEqual([], process.pop_failed())
self.assertEqual(0o640, stat.S_IMODE(os.stat(os.path.join(self.dst_dir, "movie.mkv")).st_mode))
40 changes: 40 additions & 0 deletions src/python/tests/unittests/test_lftp/test_lftp_spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,43 @@ def test_existing_sftp_errors_still_detected(self):

def test_clean_output_not_flagged(self):
self.assertFalse(self._detect("`file' at 100 (50%) 1.2M/s eta:30s [Receiving data/TLS]"))


class TestLftpStatusTimeout(unittest.TestCase):
"""A timed-out 'jobs -v' reads back as empty output, the same as no jobs.
status() must report it as unavailable (None), or callers take every
running job for finished."""

def test_status_is_none_when_jobs_command_times_out(self):
lftp, fake = make_lftp(address="host.example.com", port=22, user="bob", password=None)
with patch.object(fake, "expect", side_effect=lftp_mod.pexpect.exceptions.TIMEOUT("timed out")):
self.assertIsNone(lftp.status())

def test_status_is_empty_list_when_there_are_no_jobs(self):
lftp, _fake = make_lftp(address="host.example.com", port=22, user="bob", password=None)
self.assertEqual([], lftp.status())

def test_status_recovers_after_a_timeout(self):
lftp, fake = make_lftp(address="host.example.com", port=22, user="bob", password=None)
with patch.object(fake, "expect", side_effect=lftp_mod.pexpect.exceptions.TIMEOUT("timed out")):
lftp.status()
self.assertEqual([], lftp.status())

def test_status_is_none_when_error_output_read_times_out(self):
"""When 'jobs -v' output matches an error pattern, a second read drains it.
A timeout on that read must also report the status as unavailable."""
lftp, fake = make_lftp(address="host.example.com", port=22, user="bob", password=None)
calls = {"n": 0}

def expect(pattern, timeout=None):
calls["n"] += 1
if calls["n"] == 1:
fake.before = b"pget: Access failed: Permission denied"
return 0
raise lftp_mod.pexpect.exceptions.TIMEOUT("timed out")

with patch.object(fake, "expect", side_effect=expect):
self.assertIsNone(lftp.status())
self.assertEqual(2, calls["n"])
with self.assertRaises(LftpError):
lftp.raise_pending_error()
37 changes: 37 additions & 0 deletions src/python/tests/unittests/test_system/test_file_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,40 @@ def test_apply_to_entries_only_touches_listed_paths(self):
self.assertEqual(0o666, _mode(os.path.join(self.root, "ep1.mkv")))
self.assertEqual(0o777, _mode(os.path.join(self.root, "Sample")))
self.assertEqual(0o777, _mode(self.root))

def test_apply_to_entries_single_file(self):
"""A single file's entries are [(".", False)]; "file/." must not be chmod'ed."""
from system.file_modes import apply_umask_modes_to, relative_entries

single = os.path.join(self.tmp.name, "single.mkv")
with open(single, "w"):
pass
os.chmod(single, 0o664)

with self.assertNoLogs(self.logger):
apply_umask_modes_to(single, relative_entries(single), 0o000, self.logger)

self.assertEqual(0o666, _mode(single))

def test_apply_to_entries_resolves_dotdot_like_the_kernel(self):
"""A "link/.." base resolves through the symlink target's parent, not lexically.
The permission pass must chmod the path the move wrote to, not the
lexically collapsed one."""
from system.file_modes import apply_umask_modes_to, relative_entries

real_parent = os.path.join(self.tmp.name, "real")
os.makedirs(os.path.join(real_parent, "sub"))
link = os.path.join(self.tmp.name, "link")
os.symlink(os.path.join(real_parent, "sub"), link)
kernel_target = os.path.join(real_parent, "single.mkv") # link/../single.mkv
lexical_target = os.path.join(self.tmp.name, "single.mkv") # normpath(link/../single.mkv)
for path, mode in ((kernel_target, 0o664), (lexical_target, 0o600)):
with open(path, "w"):
pass
os.chmod(path, mode)

base = os.path.join(link, "..", "single.mkv")
apply_umask_modes_to(base, relative_entries(base), 0o000, self.logger)

self.assertEqual(0o666, _mode(kernel_target))
self.assertEqual(0o600, _mode(lexical_target))
Loading