62 $backendGroup = $this->getServiceContainer()->getFileBackendGroup();
63 $src = $backendGroup->get( $this->getOption(
'src' ) );
64 $dst = $backendGroup->get( $this->getOption(
'dst' ) );
65 $containers = explode(
'|', $this->getOption(
'containers' ) );
66 $subDir = rtrim( $this->getOption(
'subdir',
'' ),
'/' );
68 $rateFile = $this->getOption(
'ratefile' );
70 foreach ( $containers as $container ) {
71 if ( $subDir !=
'' ) {
72 $backendRel =
"$container/$subDir";
73 $this->output(
"Doing container '$container', directory '$subDir'...\n" );
75 $backendRel = $container;
76 $this->output(
"Doing container '$container'...\n" );
79 if ( $this->hasOption(
'missingonly' ) ) {
80 $this->output(
"\tBuilding list of missing files..." );
82 $this->output( count( $srcPathsRel ) .
" file(s) need to be copied.\n" );
84 $srcPathsRel = $src->getFileList( [
85 'dir' => $src->getRootStoragePath() .
"/$backendRel",
88 if ( $srcPathsRel ===
null ) {
89 $this->fatalError(
"Could not list files in $container." );
93 if ( $this->getOption(
'prestat' ) && !$this->hasOption(
'missingonly' ) ) {
95 $this->output(
"\tBuilding destination stat cache..." );
96 $dstPathsRel = $dst->getFileList( [
97 'dir' => $dst->getRootStoragePath() .
"/$backendRel",
100 if ( $dstPathsRel ===
null ) {
101 $this->fatalError(
"Could not list files in $container." );
103 $this->statCache = [];
104 foreach ( $dstPathsRel as $dstPathRel ) {
105 $path = $dst->getRootStoragePath() .
"/$backendRel/$dstPathRel";
106 $this->statCache[sha1(
$path )] = $dst->getFileStat( [
'src' =>
$path ] );
108 $this->output(
"done [" . count( $this->statCache ) .
" file(s)]\n" );
111 $this->output(
"\tCopying file(s)...\n" );
114 foreach ( $srcPathsRel as $srcPathRel ) {
116 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
117 $this->setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
118 $this->output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
120 $batchPaths[$srcPathRel] = 1;
121 if ( count( $batchPaths ) >= $this->getBatchSize() ) {
122 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
127 if ( count( $batchPaths ) ) {
128 $this->
copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
130 $this->output(
"\tCopied $count file(s).\n" );
132 if ( $this->hasOption(
'syncviadelete' ) ) {
133 $this->output(
"\tBuilding list of excess destination files..." );
135 $this->output( count( $delPathsRel ) .
" file(s) need to be deleted.\n" );
137 $this->output(
"\tDeleting file(s)...\n" );
140 foreach ( $delPathsRel as $delPathRel ) {
142 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
143 $this->setBatchSize( max( 1, (
int)file_get_contents( $rateFile ) ) );
144 $this->output(
"\tBatch size is now {$this->getBatchSize()}.\n" );
146 $batchPaths[$delPathRel] = 1;
147 if ( count( $batchPaths ) >= $this->getBatchSize() ) {
148 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
153 if ( count( $batchPaths ) ) {
154 $this->
delFileBatch( array_keys( $batchPaths ), $backendRel, $dst );
157 $this->output(
"\tDeleted $count file(s).\n" );
160 if ( $subDir !=
'' ) {
161 $this->output(
"Finished container '$container', directory '$subDir'.\n" );
163 $this->output(
"Finished container '$container'.\n" );
167 $this->output(
"Done.\n" );
221 if ( $this->hasOption(
'missingonly' ) ) {
223 foreach ( $srcPathsRel as $srcPathRel ) {
226 $t_start = microtime(
true );
228 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
229 $this->output(
"\n\tDownloaded these file(s) [{$elapsed_ms}ms]:\n\t" .
230 implode(
"\n\t", $srcPaths ) .
"\n\n" );
234 foreach ( $srcPathsRel as $srcPathRel ) {
237 if ( $this->hasOption(
'utf8only' ) && !mb_check_encoding( $srcPath,
'UTF-8' ) ) {
238 $this->error(
"$domainId: Detected illegal (non-UTF8) path for $srcPath." );
240 } elseif ( !$this->hasOption(
'missingonly' )
241 && $this->
filesAreSame( $src, $dst, $srcPath, $dstPath )
243 $this->output(
"\tAlready have $srcPathRel.\n" );
246 $fsFile = array_key_exists( $srcPath, $fsFiles )
251 if ( $src->
fileExists( [
'src' => $srcPath,
'latest' => 1 ] ) ===
false ) {
252 $this->error(
"$domainId: File '$srcPath' was listed but does not exist." );
254 $this->error(
"$domainId: Could not get local copy of $srcPath." );
257 } elseif ( !$fsFile->exists() ) {
261 $this->error(
"$domainId: Detected possible illegal path for $srcPath." );
264 $fsFiles[] = $fsFile;
266 $status = $dst->
prepare( [
'dir' => dirname( $dstPath ),
'bypassReadOnly' =>
true ] );
267 if ( !$status->isOK() ) {
268 $this->error( $status );
269 $this->fatalError(
"$domainId: Could not copy $srcPath to $dstPath." );
271 $ops[] = [
'op' =>
'store',
272 'src' => $fsFile->getPath(),
'dst' => $dstPath,
'overwrite' => true ];
273 $copiedRel[] = $srcPathRel;
277 $t_start = microtime(
true );
279 if ( !$status->isOK() ) {
283 $elapsed_ms = floor( ( microtime(
true ) - $t_start ) * 1000 );
284 if ( !$status->isOK() ) {
285 $this->error( $status );
286 $this->fatalError(
"$domainId: Could not copy file batch." );
287 } elseif ( count( $copiedRel ) ) {
288 $this->output(
"\n\tCopied these file(s) [{$elapsed_ms}ms]:\n\t" .
289 implode(
"\n\t", $copiedRel ) .
"\n\n" );