24 require_once __DIR__ .
'/Maintenance.php';
42 parent::__construct();
44 $this->
addOption(
'src',
'Backend containing the source files',
true,
true );
45 $this->
addOption(
'dst',
'Backend where files should be copied to',
true,
true );
46 $this->
addOption(
'containers',
'Pipe separated list of containers',
true,
true );
47 $this->
addOption(
'subdir',
'Only do items in this child directory',
false,
true );
48 $this->
addOption(
'ratefile',
'File to check periodically for batch size',
false,
true );
49 $this->
addOption(
'prestat',
'Stat the destination files first (try to use listings)' );
50 $this->
addOption(
'skiphash',
'Skip SHA-1 sync checks for files' );
51 $this->
addOption(
'missingonly',
'Only copy files missing from destination listing' );
52 $this->
addOption(
'syncviadelete',
'Delete destination files missing from source listing' );
53 $this->
addOption(
'utf8only',
'Skip source files that do not have valid UTF-8 names' );
60 $containers = explode(
'|', $this->
getOption(
'containers' ) );
61 $subDir = rtrim( $this->
getOption(
'subdir',
'' ),
'/' );
63 $rateFile = $this->
getOption(
'ratefile' );
65 foreach ( $containers as $container ) {
66 if ( $subDir !=
'' ) {
67 $backendRel =
"$container/$subDir";
68 $this->
output(
"Doing container '$container', directory '$subDir'...\n" );
70 $backendRel = $container;
71 $this->
output(
"Doing container '$container'...\n" );
74 if ( $this->
hasOption(
'missingonly' ) ) {
75 $this->
output(
"\tBuilding list of missing files..." );
77 $this->
output( count( $srcPathsRel ) .
" file(s) need to be copied.\n" );
79 $srcPathsRel = $src->getFileList( [
80 'dir' => $src->getRootStoragePath() .
"/$backendRel",
83 if ( $srcPathsRel ===
null ) {
84 $this->
fatalError(
"Could not list files in $container." );
90 $this->
output(
"\tBuilding destination stat cache..." );
91 $dstPathsRel = $dst->getFileList( [
92 'dir' => $dst->getRootStoragePath() .
"/$backendRel",
95 if ( $dstPathsRel ===
null ) {
96 $this->
fatalError(
"Could not list files in $container." );
98 $this->statCache = [];
99 foreach ( $dstPathsRel as $dstPathRel ) {
100 $path = $dst->getRootStoragePath() .
"/$backendRel/$dstPathRel";
101 $this->statCache[sha1(
$path )] = $dst->getFileStat( [
'src' =>
$path ] );
103 $this->
output(
"done [" . count( $this->statCache ) .
" file(s)]\n" );
106 $this->
output(
"\tCopying file(s)...\n" );
109 foreach ( $srcPathsRel as $srcPathRel ) {
111 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
112 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
113 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
115 $batchPaths[$srcPathRel] = 1;
117 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
122 if ( count( $batchPaths ) ) {
123 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
125 $this->
output(
"\tCopied $count file(s).\n" );
127 if ( $this->
hasOption(
'syncviadelete' ) ) {
128 $this->
output(
"\tBuilding list of excess destination files..." );
130 $this->
output( count( $delPathsRel ) .
" file(s) need to be deleted.\n" );
132 $this->
output(
"\tDeleting file(s)...\n" );
135 foreach ( $delPathsRel as $delPathRel ) {
137 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
138 $this->
setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
139 $this->
output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
141 $batchPaths[$delPathRel] = 1;
143 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
148 if ( count( $batchPaths ) ) {
149 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
152 $this->
output(
"\tDeleted $count file(s).\n" );
155 if ( $subDir !=
'' ) {
156 $this->
output(
"Finished container '$container', directory '$subDir'.\n" );
158 $this->
output(
"Finished container '$container'.\n" );
162 $this->
output(
"Done.\n" );
174 if ( $srcPathsRel ===
null ) {
175 $this->
fatalError(
"Could not list files in source container." );
179 if ( $dstPathsRel ===
null ) {
180 $this->
fatalError(
"Could not list files in destination container." );
183 $relFilesDstSha1 = [];
184 foreach ( $dstPathsRel as $dstPathRel ) {
185 $relFilesDstSha1[sha1( $dstPathRel )] = 1;
187 unset( $dstPathsRel );
189 $missingPathsRel = [];
190 foreach ( $srcPathsRel as $srcPathRel ) {
191 if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
192 $missingPathsRel[] = $srcPathRel;
195 unset( $srcPathsRel );
197 return $missingPathsRel;
216 if ( $this->
hasOption(
'missingonly' ) ) {
218 foreach ( $srcPathsRel as $srcPathRel ) {
221 $t_start = microtime(
true );
223 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
224 $this->
output(
"\n\tDownloaded these file(s) [{$elapsed_ms}ms]:\n\t" .
225 implode(
"\n\t", $srcPaths ) .
"\n\n" );
229 foreach ( $srcPathsRel as $srcPathRel ) {
232 if ( $this->
hasOption(
'utf8only' ) && !mb_check_encoding( $srcPath,
'UTF-8' ) ) {
233 $this->
error(
"$domainId: Detected illegal (non-UTF8) path for $srcPath." );
235 } elseif ( !$this->
hasOption(
'missingonly' )
236 && $this->
filesAreSame( $src, $dst, $srcPath, $dstPath )
238 $this->
output(
"\tAlready have $srcPathRel.\n" );
241 $fsFile = array_key_exists( $srcPath, $fsFiles )
246 if ( $src->
fileExists( [
'src' => $srcPath,
'latest' => 1 ] ) === false ) {
247 $this->
error(
"$domainId: File '$srcPath' was listed but does not exist." );
249 $this->
error(
"$domainId: Could not get local copy of $srcPath." );
252 } elseif ( !$fsFile->exists() ) {
256 $this->
error(
"$domainId: Detected possible illegal path for $srcPath." );
259 $fsFiles[] = $fsFile;
261 $status = $dst->
prepare( [
'dir' => dirname( $dstPath ),
'bypassReadOnly' => 1 ] );
264 $this->
fatalError(
"$domainId: Could not copy $srcPath to $dstPath." );
266 $ops[] = [
'op' =>
'store',
267 'src' => $fsFile->getPath(),
'dst' => $dstPath,
'overwrite' => 1 ];
268 $copiedRel[] = $srcPathRel;
272 $t_start = microtime(
true );
278 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
281 $this->
fatalError(
"$domainId: Could not copy file batch." );
282 } elseif ( count( $copiedRel ) ) {
283 $this->
output(
"\n\tCopied these file(s) [{$elapsed_ms}ms]:\n\t" .
284 implode(
"\n\t", $copiedRel ) .
"\n\n" );
302 foreach ( $dstPathsRel as $dstPathRel ) {
304 $ops[] = [
'op' =>
'delete',
'src' => $dstPath ];
305 $deletedRel[] = $dstPathRel;
309 $t_start = microtime(
true );
315 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
318 $this->
fatalError(
"$domainId: Could not delete file batch." );
319 } elseif ( count( $deletedRel ) ) {
320 $this->
output(
"\n\tDeleted these file(s) [{$elapsed_ms}ms]:\n\t" .
321 implode(
"\n\t", $deletedRel ) .
"\n\n" );
333 $skipHash = $this->
hasOption(
'skiphash' );
334 $srcStat = $src->
getFileStat( [
'src' => $sPath ] );
335 $dPathSha1 = sha1( $dPath );
336 if ( $this->statCache !==
null ) {
338 $dstStat = $this->statCache[$dPathSha1] ??
false;
340 $dstStat = $dst->
getFileStat( [
'src' => $dPath ] );
345 && is_array( $dstStat )
346 && $srcStat[
'size'] === $dstStat[
'size']
351 } elseif ( isset( $srcStat[
'md5'] ) && isset( $dstStat[
'md5'] ) ) {
355 $same = ( $srcStat[
'md5'] === $dstStat[
'md5'] );
356 } elseif ( $skipHash ) {
362 $same = ( $srcStat[
'mtime'] <= $dstStat[
'mtime'] );