Skip to content
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
23 changes: 22 additions & 1 deletion php/rtorrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -745,4 +761,9 @@ private static function directoryFailed()
self::$directoryRefused = true;
return(false);
}

private static function hasDirectory($directory)
{
return($directory && strlen($directory) > 0);
}
}
7 changes: 7 additions & 0 deletions plugins/datadir/util_setdir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/datadir/SetDirFastResumeLabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class SetDirFastResumeLabelTest extends TestCase
{
public const HASH = '0123456789ABCDEF0123456789ABCDEF01234567';
public const HASH = '9485646D4F5F05AC4428057712E46FC8189DD1DD';
public const USER = 'tester';

private $base;
Expand Down
91 changes: 75 additions & 16 deletions tests/plugins/datadir/SetDirFastResumeSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class SetDirFastResumeSessionTest extends TestCase
{
public const HASH = '0123456789ABCDEF0123456789ABCDEF01234567';
public const HASH = '9485646D4F5F05AC4428057712E46FC8189DD1DD';
public const USER = 'tester';

private $base;
Expand Down Expand Up @@ -57,57 +57,112 @@ 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, '<methodName>d.erase</methodName>') !== false, 'erase was attempted and refused');
} else {
$this->assertTrue(strpos($bodies, '<methodName>d.erase</methodName>') === 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, '<string>d.start</string>') !== false, $refusal . ' restores the running state');
}
return;
}
$this->assertTrue(
(strpos($bodies, '<methodName>d.erase</methodName>') !== false)
&& (strpos($bodies, '<string>load.raw_start</string>') !== false),
'and re-adds the download from ' . $copy . '; requests sent: ' . $bodies,
);
}

private function driver($code, $port)
private function driver($code, $port, $refusal = '')
{
$file = $this->base . '/drive.php';
file_put_contents($file, "<?php\n"
. '$_SERVER[\'REMOTE_USER\'] = ' . var_export(self::USER, true) . ';'
. ' $_ENV[\'RU_PROFILE_PATH\'] = ' . var_export($this->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"
Expand All @@ -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 {
Expand Down
Loading