MediaWiki master
FSFileBackend.php
Go to the documentation of this file.
1<?php
16// @phan-file-suppress UnusedPluginSuppression,UnusedPluginFileSuppression False positive on Windows only
17
18namespace Wikimedia\FileBackend;
19
20use Shellbox\Command\BoxedCommand;
21use Shellbox\Shellbox;
22use StatusValue;
29use Wikimedia\Timestamp\ConvertibleTimestamp;
30use Wikimedia\Timestamp\TimestampFormat as TS;
31
50 protected $usableDirCache;
51
53 protected $basePath;
54
56 protected $containerPaths;
57
59 protected $dirMode;
61 protected $fileMode;
63 protected $fileOwner;
65 protected $currentUser;
66
68 private $warningTrapStack = [];
70 private $isWindows;
71
82 public function __construct( array $config ) {
83 parent::__construct( $config );
84
85 // Remove any possible trailing slash from directories
86 if ( isset( $config['basePath'] ) ) {
87 $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
88 } else {
89 $this->basePath = null; // none; containers must have explicit paths
90 }
91
92 $this->containerPaths = [];
93 foreach ( ( $config['containerPaths'] ?? [] ) as $container => $fsPath ) {
94 $this->containerPaths[$container] = rtrim( $fsPath, '/' ); // remove trailing slash
95 }
96
97 $this->fileMode = $config['fileMode'] ?? 0o644;
98 $this->dirMode = $config['directoryMode'] ?? 0o777;
99 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
100 $this->fileOwner = $config['fileOwner'];
101 // Cache this, assuming it doesn't change
102 $this->currentUser = posix_getpwuid( posix_getuid() )['name'];
103 }
104
105 $this->usableDirCache = new MapCacheLRU( self::CACHE_CHEAP_SIZE );
106 $this->isWindows = ( PHP_OS_FAMILY === 'Windows' );
107 }
108
110 public function getFeatures() {
112 }
113
115 protected function resolveContainerPath( $container, $relStoragePath ) {
116 // Check that container has a root directory
117 if ( isset( $this->containerPaths[$container] ) || $this->basePath !== null ) {
118 // Check for sensible relative paths (assume the base paths are OK)
119 if ( $this->isLegalRelPath( $relStoragePath ) ) {
120 return $relStoragePath;
121 }
122 }
123
124 return null; // invalid
125 }
126
133 protected function isLegalRelPath( $fsPath ) {
134 // Check for file names longer than 255 chars
135 if ( preg_match( '![^/]{256}!', $fsPath ) ) { // ext3/NTFS
136 return false;
137 }
138 if ( $this->isWindows ) { // NTFS
139 return !preg_match( '![:*?"<>|]!', $fsPath );
140 } else {
141 return true;
142 }
143 }
144
153 protected function containerFSRoot( $shortCont, $fullCont ) {
154 if ( isset( $this->containerPaths[$shortCont] ) ) {
155 return $this->containerPaths[$shortCont];
156 } elseif ( $this->basePath !== null ) {
157 return "{$this->basePath}/{$fullCont}";
158 }
159
160 return null; // no container base path defined
161 }
162
169 protected function resolveToFSPath( $storagePath ) {
170 [ $fullCont, $relPath ] = $this->resolveStoragePathReal( $storagePath );
171 if ( $relPath === null ) {
172 return null; // invalid
173 }
174 [ , $shortCont, ] = FileBackend::splitStoragePath( $storagePath );
175 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
176 if ( $relPath != '' ) {
177 $fsPath .= "/{$relPath}";
178 }
179
180 return $fsPath;
181 }
182
184 public function isPathUsableInternal( $storagePath ) {
185 $fsPath = $this->resolveToFSPath( $storagePath );
186 if ( $fsPath === null ) {
187 return false; // invalid
188 }
189
190 if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) {
191 trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." );
192 return false;
193 }
194
195 $fsDirectory = dirname( $fsPath );
196 $usable = $this->usableDirCache->get( $fsDirectory, MapCacheLRU::TTL_PROC_SHORT );
197 if ( $usable === null ) {
198 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
199 $usable = @is_dir( $fsDirectory ) && @is_writable( $fsDirectory );
200 $this->usableDirCache->set( $fsDirectory, $usable ? 1 : 0 );
201 }
202
203 return $usable;
204 }
205
207 protected function doCreateInternal( array $params ) {
208 $status = $this->newStatus();
209
210 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
211 if ( $fsDstPath === null ) {
212 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
213
214 return $status;
215 }
216
217 if ( !empty( $params['async'] ) ) { // deferred
218 $tempFile = $this->newTempFileWithContent( $params );
219 if ( !$tempFile ) {
220 $status->fatal( 'backend-fail-create', $params['dst'] );
221
222 return $status;
223 }
224 $cmd = $this->makeCopyCommand( $tempFile->getPath(), $fsDstPath, false );
225 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
226 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
227 $status->fatal( 'backend-fail-create', $params['dst'] );
228 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
229 }
230 };
231 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
232 $tempFile->bind( $status->value );
233 } else { // immediate write
234 $created = false;
235 // Use fwrite+rename since (a) this clears xattrs, (b) threads still reading the old
236 // inode are unaffected since it writes to a new inode, and (c) new threads reading
237 // the file will either totally see the old version or totally see the new version
238 $fsStagePath = $this->makeStagingPath( $fsDstPath );
240 $stageHandle = fopen( $fsStagePath, 'xb' );
241 if ( $stageHandle ) {
242 $bytes = fwrite( $stageHandle, $params['content'] );
243 $created = ( $bytes === strlen( $params['content'] ) );
244 fclose( $stageHandle );
245 $created = $created ? rename( $fsStagePath, $fsDstPath ) : false;
246 }
247 $hadError = $this->untrapWarnings();
248 if ( $hadError || !$created ) {
249 $status->fatal( 'backend-fail-create', $params['dst'] );
250
251 return $status;
252 }
253 $this->chmod( $fsDstPath );
254 }
255
256 return $status;
257 }
258
260 protected function doStoreInternal( array $params ) {
261 $status = $this->newStatus();
262
263 $fsSrcPath = $params['src']; // file system path
264 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
265 if ( $fsDstPath === null ) {
266 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
267
268 return $status;
269 }
270
271 if ( $fsSrcPath === $fsDstPath ) {
272 $status->fatal( 'backend-fail-internal', $this->name );
273
274 return $status;
275 }
276
277 if ( !empty( $params['async'] ) ) { // deferred
278 $cmd = $this->makeCopyCommand( $fsSrcPath, $fsDstPath, false );
279 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
280 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
281 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
282 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
283 }
284 };
285 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
286 } else { // immediate write
287 $stored = false;
288 // Use fwrite+rename since (a) this clears xattrs, (b) threads still reading the old
289 // inode are unaffected since it writes to a new inode, and (c) new threads reading
290 // the file will either totally see the old version or totally see the new version
291 $fsStagePath = $this->makeStagingPath( $fsDstPath );
293 $srcHandle = fopen( $fsSrcPath, 'rb' );
294 if ( $srcHandle ) {
295 $stageHandle = fopen( $fsStagePath, 'xb' );
296 if ( $stageHandle ) {
297 $bytes = stream_copy_to_stream( $srcHandle, $stageHandle );
298 $stored = ( $bytes !== false && $bytes === fstat( $srcHandle )['size'] );
299 fclose( $stageHandle );
300 $stored = $stored ? rename( $fsStagePath, $fsDstPath ) : false;
301 }
302 fclose( $srcHandle );
303 }
304 $hadError = $this->untrapWarnings();
305 if ( $hadError || !$stored ) {
306 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
307
308 return $status;
309 }
310 $this->chmod( $fsDstPath );
311 }
312
313 return $status;
314 }
315
317 protected function doCopyInternal( array $params ) {
318 $status = $this->newStatus();
319
320 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
321 if ( $fsSrcPath === null ) {
322 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
323
324 return $status;
325 }
326
327 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
328 if ( $fsDstPath === null ) {
329 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
330
331 return $status;
332 }
333
334 if ( $fsSrcPath === $fsDstPath ) {
335 return $status; // no-op
336 }
337
338 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
339
340 if ( !empty( $params['async'] ) ) { // deferred
341 $cmd = $this->makeCopyCommand( $fsSrcPath, $fsDstPath, $ignoreMissing );
342 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
343 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
344 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
345 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
346 }
347 };
348 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
349 } else { // immediate write
350 $copied = false;
351 // Use fwrite+rename since (a) this clears xattrs, (b) threads still reading the old
352 // inode are unaffected since it writes to a new inode, and (c) new threads reading
353 // the file will either totally see the old version or totally see the new version
354 $fsStagePath = $this->makeStagingPath( $fsDstPath );
356 $srcHandle = fopen( $fsSrcPath, 'rb' );
357 if ( $srcHandle ) {
358 $stageHandle = fopen( $fsStagePath, 'xb' );
359 if ( $stageHandle ) {
360 $bytes = stream_copy_to_stream( $srcHandle, $stageHandle );
361 $copied = ( $bytes !== false && $bytes === fstat( $srcHandle )['size'] );
362 fclose( $stageHandle );
363 $copied = $copied ? rename( $fsStagePath, $fsDstPath ) : false;
364 }
365 fclose( $srcHandle );
366 }
367 $hadError = $this->untrapWarnings();
368 if ( $hadError || ( !$copied && !$ignoreMissing ) ) {
369 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
370
371 return $status;
372 }
373 if ( $copied ) {
374 $this->chmod( $fsDstPath );
375 }
376 }
377
378 return $status;
379 }
380
382 protected function doMoveInternal( array $params ) {
383 $status = $this->newStatus();
384
385 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
386 if ( $fsSrcPath === null ) {
387 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
388
389 return $status;
390 }
391
392 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
393 if ( $fsDstPath === null ) {
394 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
395
396 return $status;
397 }
398
399 if ( $fsSrcPath === $fsDstPath ) {
400 return $status; // no-op
401 }
402
403 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
404
405 if ( !empty( $params['async'] ) ) { // deferred
406 $cmd = $this->makeMoveCommand( $fsSrcPath, $fsDstPath, $ignoreMissing );
407 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
408 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
409 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
410 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
411 }
412 };
413 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
414 } else { // immediate write
415 // Use rename() here since (a) this clears xattrs, (b) any threads still reading the
416 // old inode are unaffected since it writes to a new inode, and (c) this is fast and
417 // atomic within a file system volume (as is normally the case)
419 $moved = rename( $fsSrcPath, $fsDstPath );
420 $hadError = $this->untrapWarnings();
421 if ( $hadError || ( !$moved && !$ignoreMissing ) ) {
422 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
423
424 return $status;
425 }
426 }
427
428 return $status;
429 }
430
432 protected function doDeleteInternal( array $params ) {
433 $status = $this->newStatus();
434
435 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
436 if ( $fsSrcPath === null ) {
437 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
438
439 return $status;
440 }
441
442 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
443
444 if ( !empty( $params['async'] ) ) { // deferred
445 $cmd = $this->makeUnlinkCommand( $fsSrcPath, $ignoreMissing );
446 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
447 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
448 $status->fatal( 'backend-fail-delete', $params['src'] );
449 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
450 }
451 };
452 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
453 } else { // immediate write
455 $deleted = unlink( $fsSrcPath );
456 $hadError = $this->untrapWarnings();
457 if ( $hadError || ( !$deleted && !$ignoreMissing ) ) {
458 $status->fatal( 'backend-fail-delete', $params['src'] );
459
460 return $status;
461 }
462 }
463
464 return $status;
465 }
466
470 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
471 $status = $this->newStatus();
472 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
473 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
474 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
475 // Create the directory and its parents as needed...
476 $created = false;
477 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
478 $alreadyExisted = @is_dir( $fsDirectory ); // already there?
479 if ( !$alreadyExisted ) {
480 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
481 $created = @mkdir( $fsDirectory, $this->dirMode, true );
482 if ( !$created ) {
483 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
484 $alreadyExisted = @is_dir( $fsDirectory ); // another thread made it?
485 }
486 }
487 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
488 $isWritable = $created ?: @is_writable( $fsDirectory ); // assume writable if created here
489 if ( !$alreadyExisted && !$created ) {
490 $this->logger->error( __METHOD__ . ": cannot create directory $fsDirectory" );
491 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
492 } elseif ( !$isWritable ) {
493 $this->logger->error( __METHOD__ . ": directory $fsDirectory is read-only" );
494 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
495 }
496 // Respect any 'noAccess' or 'noListing' flags...
497 if ( $created ) {
498 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
499 }
500
501 if ( $status->isGood() ) {
502 $this->usableDirCache->set( $fsDirectory, 1 );
503 }
504
505 return $status;
506 }
507
509 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
510 $status = $this->newStatus();
511 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
512 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
513 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
514 // Seed new directories with a blank index.html, to prevent crawling...
515 if ( !empty( $params['noListing'] ) && !is_file( "{$fsDirectory}/index.html" ) ) {
516 $this->trapWarnings();
517 $bytes = file_put_contents( "{$fsDirectory}/index.html", $this->indexHtmlPrivate() );
518 $this->untrapWarnings();
519 if ( $bytes === false ) {
520 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
521 }
522 }
523 // Add a .htaccess file to the root of the container...
524 if ( !empty( $params['noAccess'] ) && !is_file( "{$contRoot}/.htaccess" ) ) {
525 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
526 $bytes = @file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
527 if ( $bytes === false ) {
528 $storeDir = "mwstore://{$this->name}/{$shortCont}";
529 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
530 }
531 }
532
533 return $status;
534 }
535
537 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
538 $status = $this->newStatus();
539 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
540 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
541 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
542 // Unseed new directories with a blank index.html, to allow crawling...
543 if ( !empty( $params['listing'] ) && is_file( "{$fsDirectory}/index.html" ) ) {
544 $exists = ( file_get_contents( "{$fsDirectory}/index.html" ) === $this->indexHtmlPrivate() );
545 if ( $exists && !$this->unlink( "{$fsDirectory}/index.html" ) ) { // reverse secure()
546 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
547 }
548 }
549 // Remove the .htaccess file from the root of the container...
550 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
551 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
552 if ( $exists && !$this->unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
553 $storeDir = "mwstore://{$this->name}/{$shortCont}";
554 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
555 }
556 }
557
558 return $status;
559 }
560
562 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
563 $status = $this->newStatus();
564 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
565 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
566 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
567
568 $this->rmdir( $fsDirectory );
569
570 return $status;
571 }
572
574 protected function doGetFileStat( array $params ) {
575 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
576 if ( $fsSrcPath === null ) {
577 return self::RES_ERROR; // invalid storage path
578 }
579
580 $this->trapWarnings(); // don't trust 'false' if there were errors
581 $stat = is_file( $fsSrcPath ) ? stat( $fsSrcPath ) : false; // regular files only
582 $hadError = $this->untrapWarnings();
583
584 if ( is_array( $stat ) ) {
585 $ct = new ConvertibleTimestamp( $stat['mtime'] );
586
587 return [
588 'mtime' => $ct->getTimestamp( TS::MW ),
589 'size' => $stat['size']
590 ];
591 }
592
593 return $hadError ? self::RES_ERROR : self::RES_ABSENT;
594 }
595
597 protected function doClearCache( ?array $paths = null ) {
598 if ( is_array( $paths ) ) {
599 foreach ( $paths as $path ) {
600 $fsPath = $this->resolveToFSPath( $path );
601 if ( $fsPath !== null ) {
602 clearstatcache( true, $fsPath );
603 $this->usableDirCache->clear( $fsPath );
604 }
605 }
606 } else {
607 clearstatcache( true ); // clear the PHP file stat cache
608 $this->usableDirCache->clear();
609 }
610 }
611
613 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
614 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
615 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
616 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
617
618 $this->trapWarnings(); // don't trust 'false' if there were errors
619 $exists = is_dir( $fsDirectory );
620 $hadError = $this->untrapWarnings();
621
622 return $hadError ? self::RES_ERROR : $exists;
623 }
624
632 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
633 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
634 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
635 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
636
637 $list = new FSFileBackendDirList( $fsDirectory, $params );
638 $error = $list->getLastError();
639 if ( $error !== null ) {
640 if ( $this->isFileNotFoundError( $error ) ) {
641 $this->logger->info( __METHOD__ . ": non-existant directory: '$fsDirectory'" );
642
643 return []; // nothing under this dir
644 } elseif ( is_dir( $fsDirectory ) ) {
645 $this->logger->warning( __METHOD__ . ": unreadable directory: '$fsDirectory'" );
646
647 return self::RES_ERROR; // bad permissions?
648 } else {
649 $this->logger->warning( __METHOD__ . ": unreachable directory: '$fsDirectory'" );
650
651 return self::RES_ERROR;
652 }
653 }
654
655 return $list;
656 }
657
665 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
666 [ , $shortCont, ] = FileBackend::splitStoragePath( $params['dir'] );
667 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
668 $fsDirectory = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
669
670 $list = new FSFileBackendFileList( $fsDirectory, $params );
671 $error = $list->getLastError();
672 if ( $error !== null ) {
673 if ( $this->isFileNotFoundError( $error ) ) {
674 $this->logger->info( __METHOD__ . ": non-existent directory: '$fsDirectory'" );
675
676 return []; // nothing under this dir
677 } elseif ( is_dir( $fsDirectory ) ) {
678 $this->logger->warning( __METHOD__ .
679 ": unreadable directory: '$fsDirectory': $error" );
680
681 return self::RES_ERROR; // bad permissions?
682 } else {
683 $this->logger->warning( __METHOD__ .
684 ": unreachable directory: '$fsDirectory': $error" );
685
686 return self::RES_ERROR;
687 }
688 }
689
690 return $list;
691 }
692
694 protected function doGetLocalReferenceMulti( array $params ) {
695 $fsFiles = []; // (path => FSFile)
696
697 foreach ( $params['srcs'] as $src ) {
698 $source = $this->resolveToFSPath( $src );
699 if ( $source === null ) {
700 $fsFiles[$src] = self::RES_ERROR; // invalid path
701 continue;
702 }
703
704 $this->trapWarnings(); // don't trust 'false' if there were errors
705 $isFile = is_file( $source ); // regular files only
706 $hadError = $this->untrapWarnings();
707
708 if ( $isFile ) {
709 $fsFiles[$src] = new FSFile( $source );
710 } elseif ( $hadError ) {
711 $fsFiles[$src] = self::RES_ERROR;
712 } else {
713 $fsFiles[$src] = self::RES_ABSENT;
714 }
715 }
716
717 return $fsFiles;
718 }
719
721 protected function doGetLocalCopyMulti( array $params ) {
722 $tmpFiles = []; // (path => TempFSFile)
723
724 foreach ( $params['srcs'] as $src ) {
725 $source = $this->resolveToFSPath( $src );
726 if ( $source === null ) {
727 $tmpFiles[$src] = self::RES_ERROR; // invalid path
728 continue;
729 }
730 // Create a new temporary file with the same extension...
731 $ext = FileBackend::extensionFromPath( $src );
732 $tmpFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
733 if ( !$tmpFile ) {
734 $tmpFiles[$src] = self::RES_ERROR;
735 continue;
736 }
737
738 $tmpPath = $tmpFile->getPath();
739 // Copy the source file over the temp file
740 $this->trapWarnings(); // don't trust 'false' if there were errors
741 $isFile = is_file( $source ); // regular files only
742 $copySuccess = $isFile ? copy( $source, $tmpPath ) : false;
743 $hadError = $this->untrapWarnings();
744
745 if ( $copySuccess ) {
746 $this->chmod( $tmpPath );
747 $tmpFiles[$src] = $tmpFile;
748 } elseif ( $hadError ) {
749 $tmpFiles[$src] = self::RES_ERROR; // copy failed
750 } else {
751 $tmpFiles[$src] = self::RES_ABSENT;
752 }
753 }
754
755 return $tmpFiles;
756 }
757
759 public function addShellboxInputFile( BoxedCommand $command, string $boxedName,
760 array $params
761 ) {
762 $path = $this->resolveToFSPath( $params['src'] );
763 if ( $path === null ) {
764 return $this->newStatus( 'backend-fail-invalidpath', $params['src'] );
765 }
766 $command->inputFileFromFile( $boxedName, $path );
767 return $this->newStatus();
768 }
769
771 protected function directoriesAreVirtual() {
772 return false;
773 }
774
780 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
781 $statuses = [];
782
783 $pipes = [];
784 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
785 $pipes[$index] = popen( $fileOpHandle->cmd, 'r' );
786 }
787
788 $errs = [];
789 foreach ( $pipes as $index => $pipe ) {
790 // Result will be empty on success in *NIX. On Windows,
791 // it may be something like " 1 file(s) [copied|moved].".
792 $errs[$index] = stream_get_contents( $pipe );
793 fclose( $pipe );
794 }
795
796 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
797 $status = $this->newStatus();
798 $function = $fileOpHandle->callback;
799 $function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
800 $statuses[$index] = $status;
801 }
802
803 return $statuses;
804 }
805
810 private function makeStagingPath( $fsPath ) {
811 $time = dechex( time() ); // make it easy to find old orphans
812 $hash = \Wikimedia\base_convert( md5( basename( $fsPath ) ), 16, 36, 25 );
813 $unique = \Wikimedia\base_convert( bin2hex( random_bytes( 16 ) ), 16, 36, 25 );
814
815 return dirname( $fsPath ) . "/.{$time}_{$hash}_{$unique}.tmpfsfile";
816 }
817
824 private function makeCopyCommand( $fsSrcPath, $fsDstPath, $ignoreMissing ) {
825 // Use copy+rename since (a) this clears xattrs, (b) threads still reading the old
826 // inode are unaffected since it writes to a new inode, and (c) new threads reading
827 // the file will either totally see the old version or totally see the new version
828 $fsStagePath = $this->makeStagingPath( $fsDstPath );
829 $encSrc = Shellbox::escape( $this->cleanPathSlashes( $fsSrcPath ) );
830 $encStage = Shellbox::escape( $this->cleanPathSlashes( $fsStagePath ) );
831 $encDst = Shellbox::escape( $this->cleanPathSlashes( $fsDstPath ) );
832 if ( $this->isWindows ) {
833 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy
834 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/move
835 $cmdWrite = "COPY /B /Y $encSrc $encStage 2>&1 && MOVE /Y $encStage $encDst 2>&1";
836 $cmd = $ignoreMissing ? "IF EXIST $encSrc $cmdWrite" : $cmdWrite;
837 } else {
838 // https://manpages.debian.org/buster/coreutils/cp.1.en.html
839 // https://manpages.debian.org/buster/coreutils/mv.1.en.html
840 $cmdWrite = "cp $encSrc $encStage 2>&1 && mv $encStage $encDst 2>&1";
841 $cmd = $ignoreMissing ? "test -f $encSrc && $cmdWrite" : $cmdWrite;
842 // Clean up permissions on any newly created destination file
843 $octalPermissions = '0' . decoct( $this->fileMode );
844 if ( strlen( $octalPermissions ) == 4 ) {
845 $cmd .= " && chmod $octalPermissions $encDst 2>/dev/null";
846 }
847 }
848
849 return $cmd;
850 }
851
858 private function makeMoveCommand( $fsSrcPath, $fsDstPath, $ignoreMissing = false ) {
859 // https://manpages.debian.org/buster/coreutils/mv.1.en.html
860 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/move
861 $encSrc = Shellbox::escape( $this->cleanPathSlashes( $fsSrcPath ) );
862 $encDst = Shellbox::escape( $this->cleanPathSlashes( $fsDstPath ) );
863 if ( $this->isWindows ) {
864 $writeCmd = "MOVE /Y $encSrc $encDst 2>&1";
865 $cmd = $ignoreMissing ? "IF EXIST $encSrc $writeCmd" : $writeCmd;
866 } else {
867 $writeCmd = "mv -f $encSrc $encDst 2>&1";
868 $cmd = $ignoreMissing ? "test -f $encSrc && $writeCmd" : $writeCmd;
869 }
870
871 return $cmd;
872 }
873
879 private function makeUnlinkCommand( $fsPath, $ignoreMissing = false ) {
880 // https://manpages.debian.org/buster/coreutils/rm.1.en.html
881 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del
882 $encSrc = Shellbox::escape( $this->cleanPathSlashes( $fsPath ) );
883 if ( $this->isWindows ) {
884 $writeCmd = "DEL /Q $encSrc 2>&1";
885 $cmd = $ignoreMissing ? "IF EXIST $encSrc $writeCmd" : $writeCmd;
886 } else {
887 $cmd = $ignoreMissing ? "rm -f $encSrc 2>&1" : "rm $encSrc 2>&1";
888 }
889
890 return $cmd;
891 }
892
899 protected function chmod( $fsPath ) {
900 if ( $this->isWindows ) {
901 return true;
902 }
903
904 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
905 $ok = @chmod( $fsPath, $this->fileMode );
906
907 return $ok;
908 }
909
916 protected function unlink( $fsPath ) {
917 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
918 $ok = @unlink( $fsPath );
919 clearstatcache( true, $fsPath );
920
921 return $ok;
922 }
923
930 protected function rmdir( $fsDirectory ) {
931 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
932 $ok = @rmdir( $fsDirectory ); // remove directory if empty
933 clearstatcache( true, $fsDirectory );
934
935 return $ok;
936 }
937
942 protected function newTempFileWithContent( array $params ) {
943 $tempFile = $this->tmpFileFactory->newTempFSFile( 'create_', 'tmp' );
944 if ( !$tempFile ) {
945 return null;
946 }
947
948 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
949 if ( @file_put_contents( $tempFile->getPath(), $params['content'] ) === false ) {
950 $tempFile = null;
951 }
952
953 return $tempFile;
954 }
955
961 protected function indexHtmlPrivate() {
962 return '';
963 }
964
970 protected function htaccessPrivate() {
971 return "Require all denied\n" .
972 "Satisfy All\n";
973 }
974
981 protected function cleanPathSlashes( $fsPath ) {
982 return ( $this->isWindows ) ? strtr( $fsPath, '/', '\\' ) : $fsPath;
983 }
984
990 protected function trapWarnings( $regexIgnore = null ) {
991 $this->warningTrapStack[] = false;
992 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
993 if ( $regexIgnore === null || !preg_match( $regexIgnore, $errstr ) ) {
994 $this->logger->error( $errstr );
995 $this->warningTrapStack[count( $this->warningTrapStack ) - 1] = true;
996 }
997 return true; // suppress from PHP handler
998 }, E_WARNING );
999 }
1000
1004 protected function trapWarningsIgnoringNotFound() {
1005 $this->trapWarnings( $this->getFileNotFoundRegex() );
1006 }
1007
1013 protected function untrapWarnings() {
1014 restore_error_handler();
1015
1016 return array_pop( $this->warningTrapStack );
1017 }
1018
1024 protected function getFileNotFoundRegex() {
1025 static $regex;
1026 if ( $regex === null ) {
1027 // "No such file or directory": string literal in spl_directory.c etc.
1028 $alternatives = [ ': No such file or directory' ];
1029 if ( $this->isWindows ) {
1030 // 2 = The system cannot find the file specified.
1031 // 3 = The system cannot find the path specified.
1032 $alternatives[] = ' \‍(code: [23]\‍)';
1033 }
1034 if ( function_exists( 'pcntl_strerror' ) ) {
1035 $alternatives[] = preg_quote( ': ' . pcntl_strerror( 2 ), '/' );
1036 } elseif ( function_exists( 'socket_strerror' ) && defined( 'SOCKET_ENOENT' ) ) {
1037 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal False positive on Windows only
1038 $alternatives[] = preg_quote( ': ' . socket_strerror( SOCKET_ENOENT ), '/' );
1039 }
1040 $regex = '/(' . implode( '|', $alternatives ) . ')$/';
1041 }
1042 return $regex;
1043 }
1044
1051 protected function isFileNotFoundError( $error ) {
1052 return (bool)preg_match( $this->getFileNotFoundRegex(), $error );
1053 }
1054}
1055
1057class_alias( FSFileBackend::class, 'FSFileBackend' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Class for a file system (FS) based file backend.
doDirectoryExists( $fullCont, $dirRel, array $params)
FileBackendStore::directoryExists()bool|null
doDeleteInternal(array $params)
FileBackendStore::deleteInternal() StatusValue
addShellboxInputFile(BoxedCommand $command, string $boxedName, array $params)
Add a file to a Shellbox command as an input file.StatusValue 1.43
doStoreInternal(array $params)
FileBackendStore::storeInternal() StatusValue
isPathUsableInternal( $storagePath)
Check if a file can be created or changed at a given storage path in the backend.This quickly checks ...
doPrepareInternal( $fullCont, $dirRel, array $params)
FileBackendStore::doPrepare() to override StatusValue Good status without value for success,...
indexHtmlPrivate()
Return the text of an index.html file to hide directory listings.
doSecureInternal( $fullCont, $dirRel, array $params)
FileBackendStore::doSecure() to override StatusValue Good status without value for success,...
doCreateInternal(array $params)
FileBackendStore::createInternal() StatusValue
MapCacheLRU $usableDirCache
Cache for known prepared/usable directories.
string $fileOwner
Required OS username to own files.
htaccessPrivate()
Return the text of a .htaccess file to make a directory private.
getFeatures()
Get the a bitfield of extra features supported by the backend medium.to overrideint Bitfield of FileB...
isLegalRelPath( $fsPath)
Check a relative file system path for validity.
resolveToFSPath( $storagePath)
Get the absolute file system path for a storage path.
string $currentUser
OS username running this script.
trapWarningsIgnoringNotFound()
Track E_WARNING errors but ignore any that correspond to ENOENT "No such file or directory".
unlink( $fsPath)
Unlink a file, suppressing the warnings.
getDirectoryListInternal( $fullCont, $dirRel, array $params)
int $dirMode
Directory permission mode.
doExecuteOpHandlesInternal(array $fileOpHandles)
containerFSRoot( $shortCont, $fullCont)
Given the short (unresolved) and full (resolved) name of a container, return the file system path of ...
doGetFileStat(array $params)
FileBackendStore::getFileStat() array|false|null
doCopyInternal(array $params)
FileBackendStore::copyInternal() StatusValue
untrapWarnings()
Stop listening for E_WARNING errors and get whether any happened.
array< string, string > $containerPaths
Map of container names to root paths for custom container paths.
isFileNotFoundError( $error)
Determine whether a given error message is a file not found error.
trapWarnings( $regexIgnore=null)
Listen for E_WARNING errors and track whether any that happen.
doPublishInternal( $fullCont, $dirRel, array $params)
FileBackendStore::doPublish() to override StatusValue
string null $basePath
Directory holding the container directories.
cleanPathSlashes( $fsPath)
Clean up directory separators for the given OS.
doCleanInternal( $fullCont, $dirRel, array $params)
FileBackendStore::doClean() to override StatusValue
doMoveInternal(array $params)
FileBackendStore::moveInternal() StatusValue
doGetLocalCopyMulti(array $params)
FileBackendStore::getLocalCopyMulti() string[]|bool[]|null[] Map of (path => TempFSFile,...
doClearCache(?array $paths=null)
Clears any additional stat caches for storage paths.to overrideFileBackend::clearCache()
int $fileMode
File permission mode.
rmdir( $fsDirectory)
Remove an empty directory, suppressing the warnings.
directoriesAreVirtual()
Whether this a key/value store where directories are merely virtual.Virtual directories exists in so ...
doGetLocalReferenceMulti(array $params)
FileBackendStore::getLocalReferenceMulti() to override string[]|bool[]|null[] Map of (path => FSFile,...
resolveContainerPath( $container, $relStoragePath)
Resolve a relative storage path, checking if it's allowed by the backend.This is intended for interna...
chmod( $fsPath)
Chmod a file, suppressing the warnings.
getFileNotFoundRegex()
Get a regex matching file not found errors.
getFileListInternal( $fullCont, $dirRel, array $params)
Class representing a non-directory file on the file system.
Definition FSFile.php:20
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Base class for all backends using particular storage medium.
resolveStoragePathReal( $storagePath)
Like resolveStoragePath() except null values are returned if the container is sharded and the shard c...
static extensionFromPath( $path, $case='lowercase')
Get the final extension from a storage or FS path.
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
newStatus( $message=null,... $params)
Yields the result of the status wrapper callback on either:
copy(array $params, array $opts=[])
Performs a single copy operation.
Store key-value entries in a size-limited in-memory LRU cache.
$source