From e1ca4e5beb9e36061d6bf9b976f1b1732cda4ff4 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 22 Nov 2026 10:08:29 +0000 Subject: [PATCH] datadir: check reload refusals before erasing Fast resume erased the download and only then asked the loader, so a refused addition or directory lost it, and a different .torrent replaced it. Ask first; a refusal is logged and takes the slow path. --- php/rtorrent.php | 23 ++++- plugins/datadir/util_setdir.php | 7 ++ .../datadir/SetDirFastResumeLabelTest.php | 2 +- .../datadir/SetDirFastResumeSessionTest.php | 91 +++++++++++++++---- 4 files changed, 105 insertions(+), 18 deletions(-) diff --git a/php/rtorrent.php b/php/rtorrent.php index 13edc6775..e11f0f5c5 100644 --- a/php/rtorrent.php +++ b/php/rtorrent.php @@ -172,6 +172,22 @@ public static function additionCommand($command, ...$values) return(getCmd($command . '=') . implode(',', $quoted)); } + /** Why a fast-resume reload of $hash would be refused, or false. Ask before the erase. */ + public static function reloadRefusal($torrent, $hash, $directory, $isAddPath, $addition) + { + if (!self::areValidAdditions($addition)) { + return('addition'); + } + if ($torrent->hash_info() !== strtoupper($hash)) { + return('hash'); + } + $dir = self::parseDirectory($directory, $isAddPath); + if (self::hasDirectory($dir) && !rTorrentSettings::get()->correctReportedDirectory($dir)) { + return('directory'); + } + return(false); + } + public static function sendTorrent($fname, $isStart, $isAddPath, $directory, $label, $saveTorrent, $isFast, $isNew = true, $addition = null) { self::$directoryRefused = false; @@ -225,7 +241,7 @@ public static function sendTorrent($fname, $isStart, $isAddPath, $directory, $la } $req = new rXMLRPCRequest(); $directory = self::parseDirectory($directory, $isAddPath); - if ($directory && (strlen($directory) > 0)) { + if (self::hasDirectory($directory)) { if (!self::prepareDirectory($directory, $isNew, $req)) { return(false); } @@ -745,4 +761,9 @@ private static function directoryFailed() self::$directoryRefused = true; return(false); } + + private static function hasDirectory($directory) + { + return($directory && strlen($directory) > 0); + } } diff --git a/plugins/datadir/util_setdir.php b/plugins/datadir/util_setdir.php index d65823607..d5a5af7c7 100644 --- a/plugins/datadir/util_setdir.php +++ b/plugins/datadir/util_setdir.php @@ -359,6 +359,13 @@ function rtSetDataDir($hash, $dest_path, $add_path, $move_files, $fast_resume, $ } $fast_resume = false; } else { + $refusal = rTorrent::reloadRefusal($torrent, $hash, $dest_path, $add_path, $addition); + if ($refusal !== false) { + FileUtil::toLog('datadir: fast resume refused (' . $refusal . ') ' . $hash); + $fast_resume = false; + } + } + if ($fast_resume) { $is_ok = $add_path ? rtExec("d.set_directory", [ $hash, $dest_path ], $dbg) : rtExec("d.set_directory_base", [ $hash, $dest_path ], $dbg); // for erasedata plugin diff --git a/tests/plugins/datadir/SetDirFastResumeLabelTest.php b/tests/plugins/datadir/SetDirFastResumeLabelTest.php index b85ba3cf5..4fa0df108 100644 --- a/tests/plugins/datadir/SetDirFastResumeLabelTest.php +++ b/tests/plugins/datadir/SetDirFastResumeLabelTest.php @@ -15,7 +15,7 @@ */ class SetDirFastResumeLabelTest extends TestCase { - public const HASH = '0123456789ABCDEF0123456789ABCDEF01234567'; + public const HASH = '9485646D4F5F05AC4428057712E46FC8189DD1DD'; public const USER = 'tester'; private $base; diff --git a/tests/plugins/datadir/SetDirFastResumeSessionTest.php b/tests/plugins/datadir/SetDirFastResumeSessionTest.php index 373ef1851..2def6fdaa 100644 --- a/tests/plugins/datadir/SetDirFastResumeSessionTest.php +++ b/tests/plugins/datadir/SetDirFastResumeSessionTest.php @@ -17,7 +17,7 @@ */ class SetDirFastResumeSessionTest extends TestCase { - public const HASH = '0123456789ABCDEF0123456789ABCDEF01234567'; + public const HASH = '9485646D4F5F05AC4428057712E46FC8189DD1DD'; public const USER = 'tester'; private $base; @@ -57,42 +57,95 @@ public function testAFastResumeReadsATiedFileWithAQuoteAndABackslashWhenTheSessi $this->assertFastResumed('session', 'ti"ed\\x/t.torrent'); } + public function testDifferentTorrentIsNotErased() + { + $this->assertRefused('hash'); + } + + public function testInvalidAdditionIsNotErased() + { + $this->assertRefused('addition'); + } + + public function testRefusedDirectoryIsNotErased() + { + $this->assertRefused('directory'); + } + + public function testEraseRefusalRestoresRunningState() + { + $this->assertFastResumed('session', null, 'erase', true); + } + + public function testLowercaseHashMatchesTheTorrent() + { + $this->assertFastResumed('session', null, 'lower-hash'); + } + + private function assertRefused($reason) + { + $this->assertFastResumed('session', null, $reason, true); + } + /** * The download's copy is in $sessionName, or, given $tiedName, only in * that file below the test's base. */ - private function assertFastResumed($sessionName, $tiedName = null) + private function assertFastResumed($sessionName, $tiedName = null, $refusal = '', $active = false) { TestTree::remove($this->base); $session = $this->base . '/' . $sessionName . '/'; $dest = $this->base . '/dest'; - $copy = ($tiedName === null) ? $session . self::HASH . '.torrent' : $this->base . '/' . $tiedName; + $bytes = $this->torrentBytes($refusal === 'hash' ? 'other' : 'subx', ['f.bin']); + $hash = self::HASH; + if ($refusal === 'lower-hash') { + $hash = strtolower($hash); + } + $copy = ($tiedName === null) ? $session . $hash . '.torrent' : $this->base . '/' . $tiedName; $tied = ($tiedName === null) ? '' : $copy; $this->makeDir($session); $this->makeDir($dest); $this->makeDir(dirname($copy)); - file_put_contents($copy, $this->torrentBytes('subx', ['f.bin'])); + file_put_contents($copy, $bytes); $this->assertTrue( RtorrentProbeFixture::warmSettingsCache($this->base . '/profile/users/' . self::USER, '', $this->base . '/dl', $session), 'the settings cache is warmed', ); - $this->daemon = new FakeRtorrentDaemon([ - // d.is_open, d.is_active - ['0', '0'], - // get_session, d.get_tied_to_file, d.get_custom1, d.get_connection_seed, d.get_throttle_name - [$session, $tied, '', '', ''], - // d.set_directory, then d.erase, then the load; or, after a - // failed fast resume, d.set_directory and d.open, d.close. - ['0'], ['0'], ['0'], ['0'], - ], $this->base . '/calls.log'); + $replies = [[$active ? '1' : '0', $active ? '1' : '0']]; + if ($active) { + $replies[] = ['0', '0']; + } + $replies[] = [$session, $tied, '', $refusal === 'addition' ? '$bad' : '', '']; + $replies[] = ['0']; + $replies[] = $refusal === 'erase' ? FakeRtorrentDaemon::fault('erase refused') : ['0']; + $replies[] = ['0', '0']; + $replies[] = ['0', '0']; + $this->daemon = new FakeRtorrentDaemon($replies, $this->base . '/calls.log'); $run = $this->runPhp($this->driver( - 'echo rtSetDataDir(' . var_export(self::HASH, true) . ', ' . var_export($dest, true) . ', true, false, true, false) ? "ok" : "failed";', + 'echo rtSetDataDir(' . var_export($hash, true) . ', ' . var_export($dest, true) . ', true, false, true, false) ? "ok" : "failed";', $this->daemon->port(), + $refusal, )); $bodies = implode("\n", $this->daemon->bodies()); $this->daemon->stop(); $this->daemon = null; $this->assertEquals('ok', $run['printed'], 'rtSetDataDir() succeeds: ' . $run['printed'] . $run['errors']); + if ($refusal !== '' && $refusal !== 'lower-hash') { + if ($refusal === 'erase') { + $this->assertTrue(strpos($bodies, 'd.erase') !== false, 'erase was attempted and refused'); + } else { + $this->assertTrue(strpos($bodies, 'd.erase') === false, $refusal . ' must not erase the download'); + $log = @file_get_contents($this->base . '/application.log'); + $this->assertTrue( + $log !== false && strpos($log, 'fast resume refused (' . $refusal . ') ' . $hash) !== false, + $refusal . ' is recorded in the application log', + ); + } + if ($active) { + $this->assertTrue(strpos($bodies, 'd.start') !== false, $refusal . ' restores the running state'); + } + return; + } $this->assertTrue( (strpos($bodies, 'd.erase') !== false) && (strpos($bodies, 'load.raw_start') !== false), @@ -100,14 +153,16 @@ private function assertFastResumed($sessionName, $tiedName = null) ); } - private function driver($code, $port) + private function driver($code, $port, $refusal = '') { $file = $this->base . '/drive.php'; file_put_contents($file, "base . '/profile', true) . ";\n" . 'require_once(' . var_export($this->repoRoot() . '/conf/config.php', true) . ");\n" + . '$log_file = ' . var_export($this->base . '/application.log', true) . ";\n" . '$scgi_host = "127.0.0.1"; $scgi_port = ' . (int) $port . '; $rpcTimeOut = 5; $rpcLogCalls = false;' . "\n" + . ($refusal === 'directory' ? '$topDirectory = ' . var_export($this->base . '/elsewhere/', true) . ';' : '') // util_setdir.php's own requires are relative to the plugin's directory. . 'chdir(' . var_export($this->repoRoot() . '/plugins/datadir', true) . ");\n" . 'require_once(' . var_export($this->repoRoot() . '/plugins/datadir/util_setdir.php', true) . ");\n" @@ -120,7 +175,11 @@ private function runPhp($file) $mask = umask(022); try { $run = PhpProcess::run( - array_merge(PhpProcess::sameIni(), ['-d', 'display_errors=stderr', $file]), + array_merge( + PhpProcess::sameIni(), + ['-d', 'display_errors=stderr'], + [$file], + ), ['env' => ['LC_ALL' => 'C']], ); } finally {