/** * Concatenate a list of files into a target file location. * * @param $srcPaths Array Ordered list of source virtual URLs/storage paths * @param $dstPath String Target file system path * @param $flags Integer: bitwise combination of the following flags: * self::DELETE_SOURCE Delete the source files * @return FileRepoStatus */ function concatenate($srcPaths, $dstPath, $flags = 0) { $status = $this->newGood(); $sources = array(); $deleteOperations = array(); // post-concatenate ops foreach ($srcPaths as $srcPath) { // Resolve source to a storage path if virtual $source = $this->resolveToStoragePath($srcPath); $sources[] = $source; // chunk to merge if ($flags & self::DELETE_SOURCE) { $deleteOperations[] = array('op' => 'delete', 'src' => $source); } } // Concatenate the chunks into one FS file $params = array('srcs' => $sources, 'dst' => $dstPath); $status->merge($this->backend->concatenate($params)); if (!$status->isOK()) { return $status; } // Delete the sources if required if ($deleteOperations) { $opts = array('force' => true); $status->merge($this->backend->doOperations($deleteOperations, $opts)); } // Make sure status is OK, despite any $deleteOperations fatals $status->setResult(true); return $status; }
private function doTestDoOperationsFailing() { $base = self::baseStorePath(); $fileA = "{$base}/unittest-cont2/a/b/fileA.txt"; $fileAContents = '3tqtmoeatmn4wg4qe-mg3qt3 tq'; $fileB = "{$base}/unittest-cont2/a/b/fileB.txt"; $fileBContents = 'g-jmq3gpqgt3qtg q3GT '; $fileC = "{$base}/unittest-cont2/a/b/fileC.txt"; $fileCContents = 'eigna[ogmewt 3qt g3qg flew[ag'; $fileD = "{$base}/unittest-cont2/a/b/fileD.txt"; $this->prepare(array('dir' => dirname($fileA))); $this->create(array('dst' => $fileA, 'content' => $fileAContents)); $this->prepare(array('dir' => dirname($fileB))); $this->create(array('dst' => $fileB, 'content' => $fileBContents)); $this->prepare(array('dir' => dirname($fileC))); $this->create(array('dst' => $fileC, 'content' => $fileCContents)); $status = $this->backend->doOperations(array(array('op' => 'copy', 'src' => $fileA, 'dst' => $fileC, 'overwrite' => 1), array('op' => 'copy', 'src' => $fileC, 'dst' => $fileA, 'overwriteSame' => 1), array('op' => 'copy', 'src' => $fileB, 'dst' => $fileD, 'overwrite' => 1), array('op' => 'move', 'src' => $fileC, 'dst' => $fileD), array('op' => 'move', 'src' => $fileB, 'dst' => $fileC, 'overwriteSame' => 1), array('op' => 'move', 'src' => $fileB, 'dst' => $fileA, 'overwrite' => 1), array('op' => 'delete', 'src' => $fileD), array('op' => 'null')), array('force' => 1)); $this->assertNotEquals(array(), $status->errors, "Operation had warnings"); $this->assertEquals(true, $status->isOK(), "Operation batch succeeded"); $this->assertEquals(8, count($status->success), "Operation batch has correct success array"); $this->assertEquals(false, $this->backend->fileExists(array('src' => $fileB)), "File does not exist at {$fileB}"); $this->assertEquals(false, $this->backend->fileExists(array('src' => $fileD)), "File does not exist at {$fileD}"); $this->assertEquals(true, $this->backend->fileExists(array('src' => $fileA)), "File does not exist at {$fileA}"); $this->assertEquals(true, $this->backend->fileExists(array('src' => $fileC)), "File exists at {$fileC}"); $this->assertEquals($fileBContents, $this->backend->getFileContents(array('src' => $fileA)), "Correct file contents of {$fileA}"); $this->assertEquals(strlen($fileBContents), $this->backend->getFileSize(array('src' => $fileA)), "Correct file size of {$fileA}"); $this->assertEquals(wfBaseConvert(sha1($fileBContents), 16, 36, 31), $this->backend->getFileSha1Base36(array('src' => $fileA)), "Correct file SHA-1 of {$fileA}"); }
/** * Deletes a batch of files. * Each file can be a (zone, rel) pair, virtual url, storage path. * It will try to delete each file, but ignores any errors that may occur. * * @param array $files List of files to delete * @param int $flags Bitwise combination of the following flags: * self::SKIP_LOCKING Skip any file locking when doing the deletions * @return FileRepoStatus */ public function cleanupBatch(array $files, $flags = 0) { $this->assertWritableRepo(); // fail out if read-only $status = $this->newGood(); $operations = array(); foreach ($files as $path) { if (is_array($path)) { // This is a pair, extract it list($zone, $rel) = $path; $path = $this->getZonePath($zone) . "/{$rel}"; } else { // Resolve source to a storage path if virtual $path = $this->resolveToStoragePath($path); } $operations[] = array('op' => 'delete', 'src' => $path); } // Actually delete files from storage... $opts = array('force' => true); if ($flags & self::SKIP_LOCKING) { $opts['nonLocking'] = true; } $status->merge($this->backend->doOperations($operations, $opts)); return $status; }
protected function runBatch(array $ops, FileBackend $be) { $this->output("Migrating file batch:\n"); foreach ($ops as $op) { $this->output("\"{$op['img']}\" (dest: {$op['dst']})\n"); } $status = $be->doOperations($ops); if (!$status->isOK()) { $this->output(print_r($status->getErrorsArray(), true)); } $this->output("Batch done\n\n"); }