filebackend: optimize 'delete' for FSFileBackend to avoid is_file() calls
[lhc/web/wiklou.git] / includes / libs / filebackend / FSFileBackend.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup FileBackend
20 */
21
22 /**
23 * File system based backend.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 * http://www.gnu.org/copyleft/gpl.html
39 *
40 * @file
41 * @ingroup FileBackend
42 */
43 use Wikimedia\AtEase\AtEase;
44 use Wikimedia\Timestamp\ConvertibleTimestamp;
45
46 /**
47 * @brief Class for a file system (FS) based file backend.
48 *
49 * All "containers" each map to a directory under the backend's base directory.
50 * For backwards-compatibility, some container paths can be set to custom paths.
51 * The domain ID will not be used in any custom paths, so this should be avoided.
52 *
53 * Having directories with thousands of files will diminish performance.
54 * Sharding can be accomplished by using FileRepo-style hash paths.
55 *
56 * StatusValue messages should avoid mentioning the internal FS paths.
57 * PHP warnings are assumed to be logged rather than output.
58 *
59 * @ingroup FileBackend
60 * @since 1.19
61 */
62 class FSFileBackend extends FileBackendStore {
63 /** @var string Directory holding the container directories */
64 protected $basePath;
65
66 /** @var array Map of container names to root paths for custom container paths */
67 protected $containerPaths;
68
69 /** @var int Directory permission mode */
70 protected $dirMode;
71 /** @var int File permission mode */
72 protected $fileMode;
73 /** @var string Required OS username to own files */
74 protected $fileOwner;
75
76 /** @var bool Whether the OS is Windows (otherwise assumed Unix-like)*/
77 protected $isWindows;
78 /** @var string OS username running this script */
79 protected $currentUser;
80
81 /** @var bool[] Map of (stack index => whether a warning happened) */
82 private $warningTrapStack = [];
83
84 /**
85 * @see FileBackendStore::__construct()
86 * Additional $config params include:
87 * - basePath : File system directory that holds containers.
88 * - containerPaths : Map of container names to custom file system directories.
89 * This should only be used for backwards-compatibility.
90 * - fileMode : Octal UNIX file permissions to use on files stored.
91 * - directoryMode : Octal UNIX file permissions to use on directories created.
92 * @param array $config
93 */
94 public function __construct( array $config ) {
95 parent::__construct( $config );
96
97 $this->isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' );
98 // Remove any possible trailing slash from directories
99 if ( isset( $config['basePath'] ) ) {
100 $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
101 } else {
102 $this->basePath = null; // none; containers must have explicit paths
103 }
104
105 $this->containerPaths = [];
106 foreach ( ( $config['containerPaths'] ?? [] ) as $container => $path ) {
107 $this->containerPaths[$container] = rtrim( $path, '/' ); // remove trailing slash
108 }
109
110 $this->fileMode = $config['fileMode'] ?? 0644;
111 $this->dirMode = $config['directoryMode'] ?? 0777;
112 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
113 $this->fileOwner = $config['fileOwner'];
114 // cache this, assuming it doesn't change
115 $this->currentUser = posix_getpwuid( posix_getuid() )['name'];
116 }
117 }
118
119 public function getFeatures() {
120 if ( $this->isWindows && version_compare( PHP_VERSION, '7.1', 'lt' ) ) {
121 // PHP before 7.1 used 8-bit code page for filesystem paths on Windows;
122 // See https://www.php.net/manual/en/migration71.windows-support.php
123 return 0;
124 } else {
125 return FileBackend::ATTR_UNICODE_PATHS;
126 }
127 }
128
129 protected function resolveContainerPath( $container, $relStoragePath ) {
130 // Check that container has a root directory
131 if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
132 // Check for sane relative paths (assume the base paths are OK)
133 if ( $this->isLegalRelPath( $relStoragePath ) ) {
134 return $relStoragePath;
135 }
136 }
137
138 return null; // invalid
139 }
140
141 /**
142 * Sanity check a relative file system path for validity
143 *
144 * @param string $path Normalized relative path
145 * @return bool
146 */
147 protected function isLegalRelPath( $path ) {
148 // Check for file names longer than 255 chars
149 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
150 return false;
151 }
152 if ( $this->isWindows ) { // NTFS
153 return !preg_match( '![:*?"<>|]!', $path );
154 } else {
155 return true;
156 }
157 }
158
159 /**
160 * Given the short (unresolved) and full (resolved) name of
161 * a container, return the file system path of the container.
162 *
163 * @param string $shortCont
164 * @param string $fullCont
165 * @return string|null
166 */
167 protected function containerFSRoot( $shortCont, $fullCont ) {
168 if ( isset( $this->containerPaths[$shortCont] ) ) {
169 return $this->containerPaths[$shortCont];
170 } elseif ( isset( $this->basePath ) ) {
171 return "{$this->basePath}/{$fullCont}";
172 }
173
174 return null; // no container base path defined
175 }
176
177 /**
178 * Get the absolute file system path for a storage path
179 *
180 * @param string $storagePath Storage path
181 * @return string|null
182 */
183 protected function resolveToFSPath( $storagePath ) {
184 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
185 if ( $relPath === null ) {
186 return null; // invalid
187 }
188 list( , $shortCont, ) = FileBackend::splitStoragePath( $storagePath );
189 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
190 if ( $relPath != '' ) {
191 $fsPath .= "/{$relPath}";
192 }
193
194 return $fsPath;
195 }
196
197 public function isPathUsableInternal( $storagePath ) {
198 $fsPath = $this->resolveToFSPath( $storagePath );
199 if ( $fsPath === null ) {
200 return false; // invalid
201 }
202 $parentDir = dirname( $fsPath );
203
204 if ( file_exists( $fsPath ) ) {
205 $ok = is_file( $fsPath ) && is_writable( $fsPath );
206 } else {
207 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
208 }
209
210 if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) {
211 $ok = false;
212 trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." );
213 }
214
215 return $ok;
216 }
217
218 protected function doCreateInternal( array $params ) {
219 $status = $this->newStatus();
220
221 $dest = $this->resolveToFSPath( $params['dst'] );
222 if ( $dest === null ) {
223 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
224
225 return $status;
226 }
227
228 if ( !empty( $params['async'] ) ) { // deferred
229 $tempFile = $this->stageContentAsTempFile( $params );
230 if ( !$tempFile ) {
231 $status->fatal( 'backend-fail-create', $params['dst'] );
232
233 return $status;
234 }
235 $cmd = implode( ' ', [
236 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
237 escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
238 escapeshellarg( $this->cleanPathSlashes( $dest ) )
239 ] );
240 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
241 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
242 $status->fatal( 'backend-fail-create', $params['dst'] );
243 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
244 }
245 };
246 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
247 $tempFile->bind( $status->value );
248 } else { // immediate write
249 $this->trapWarnings();
250 $bytes = file_put_contents( $dest, $params['content'] );
251 $this->untrapWarnings();
252 if ( $bytes === false ) {
253 $status->fatal( 'backend-fail-create', $params['dst'] );
254
255 return $status;
256 }
257 $this->chmod( $dest );
258 }
259
260 return $status;
261 }
262
263 protected function doStoreInternal( array $params ) {
264 $status = $this->newStatus();
265
266 $dest = $this->resolveToFSPath( $params['dst'] );
267 if ( $dest === null ) {
268 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
269
270 return $status;
271 }
272
273 if ( !empty( $params['async'] ) ) { // deferred
274 $cmd = implode( ' ', [
275 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
276 escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ),
277 escapeshellarg( $this->cleanPathSlashes( $dest ) )
278 ] );
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, $dest );
286 } else { // immediate write
287 $this->trapWarnings();
288 $ok = copy( $params['src'], $dest );
289 $this->untrapWarnings();
290 // In some cases (at least over NFS), copy() returns true when it fails
291 if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
292 if ( $ok ) { // PHP bug
293 unlink( $dest ); // remove broken file
294 trigger_error( __METHOD__ . ": copy() failed but returned true." );
295 }
296 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
297
298 return $status;
299 }
300 $this->chmod( $dest );
301 }
302
303 return $status;
304 }
305
306 protected function doCopyInternal( array $params ) {
307 $status = $this->newStatus();
308
309 $source = $this->resolveToFSPath( $params['src'] );
310 if ( $source === null ) {
311 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
312
313 return $status;
314 }
315
316 $dest = $this->resolveToFSPath( $params['dst'] );
317 if ( $dest === null ) {
318 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
319
320 return $status;
321 }
322
323 if ( !is_file( $source ) ) {
324 if ( empty( $params['ignoreMissingSource'] ) ) {
325 $status->fatal( 'backend-fail-copy', $params['src'] );
326 }
327
328 return $status; // do nothing; either OK or bad status
329 }
330
331 if ( !empty( $params['async'] ) ) { // deferred
332 $cmd = implode( ' ', [
333 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
334 escapeshellarg( $this->cleanPathSlashes( $source ) ),
335 escapeshellarg( $this->cleanPathSlashes( $dest ) )
336 ] );
337 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
338 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
339 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
340 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
341 }
342 };
343 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
344 } else { // immediate write
345 $this->trapWarnings();
346 $ok = ( $source === $dest ) ? true : copy( $source, $dest );
347 $this->untrapWarnings();
348 // In some cases (at least over NFS), copy() returns true when it fails
349 if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
350 if ( $ok ) { // PHP bug
351 $this->trapWarnings();
352 unlink( $dest ); // remove broken file
353 $this->untrapWarnings();
354 trigger_error( __METHOD__ . ": copy() failed but returned true." );
355 }
356 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
357
358 return $status;
359 }
360 $this->chmod( $dest );
361 }
362
363 return $status;
364 }
365
366 protected function doMoveInternal( array $params ) {
367 $status = $this->newStatus();
368
369 $source = $this->resolveToFSPath( $params['src'] );
370 if ( $source === null ) {
371 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
372
373 return $status;
374 }
375
376 $dest = $this->resolveToFSPath( $params['dst'] );
377 if ( $dest === null ) {
378 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
379
380 return $status;
381 }
382
383 if ( !is_file( $source ) ) {
384 if ( empty( $params['ignoreMissingSource'] ) ) {
385 $status->fatal( 'backend-fail-move', $params['src'] );
386 }
387
388 return $status; // do nothing; either OK or bad status
389 }
390
391 if ( !empty( $params['async'] ) ) { // deferred
392 $cmd = implode( ' ', [
393 $this->isWindows ? 'MOVE /Y' : 'mv', // (overwrite)
394 escapeshellarg( $this->cleanPathSlashes( $source ) ),
395 escapeshellarg( $this->cleanPathSlashes( $dest ) )
396 ] );
397 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
398 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
399 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
400 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
401 }
402 };
403 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
404 } else { // immediate write
405 $this->trapWarnings();
406 $ok = ( $source === $dest ) ? true : rename( $source, $dest );
407 $this->untrapWarnings();
408 clearstatcache(); // file no longer at source
409 if ( !$ok ) {
410 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
411
412 return $status;
413 }
414 }
415
416 return $status;
417 }
418
419 protected function doDeleteInternal( array $params ) {
420 $status = $this->newStatus();
421
422 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
423 if ( $fsSrcPath === null ) {
424 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
425
426 return $status;
427 }
428
429 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
430
431 if ( !empty( $params['async'] ) ) { // deferred
432 // https://manpages.debian.org/buster/coreutils/rm.1.en.html
433 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del
434 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
435 if ( $this->isWindows ) {
436 $writeCmd = "DEL /Q $encSrc";
437 $cmd = $ignoreMissing ? "IF EXIST $encSrc $writeCmd" : $writeCmd;
438 } else {
439 $cmd = $ignoreMissing ? "rm -f $encSrc" : "rm $encSrc";
440 }
441 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
442 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
443 $status->fatal( 'backend-fail-delete', $params['src'] );
444 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
445 }
446 };
447 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
448 } else { // immediate write
449 $this->trapWarnings( '/: No such file or directory$/' );
450 $deleted = unlink( $fsSrcPath );
451 $hadError = $this->untrapWarnings();
452 if ( $hadError || ( !$deleted && !$ignoreMissing ) ) {
453 $status->fatal( 'backend-fail-delete', $params['src'] );
454
455 return $status;
456 }
457 }
458
459 return $status;
460 }
461
462 /**
463 * @param string $fullCont
464 * @param string $dirRel
465 * @param array $params
466 * @return StatusValue
467 */
468 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
469 $status = $this->newStatus();
470 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
471 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
472 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
473 $existed = is_dir( $dir ); // already there?
474 // Create the directory and its parents as needed...
475 AtEase::suppressWarnings();
476 if ( !$existed && !mkdir( $dir, $this->dirMode, true ) && !is_dir( $dir ) ) {
477 $this->logger->error( __METHOD__ . ": cannot create directory $dir" );
478 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
479 } elseif ( !is_writable( $dir ) ) {
480 $this->logger->error( __METHOD__ . ": directory $dir is read-only" );
481 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
482 } elseif ( !is_readable( $dir ) ) {
483 $this->logger->error( __METHOD__ . ": directory $dir is not readable" );
484 $status->fatal( 'directorynotreadableerror', $params['dir'] );
485 }
486 AtEase::restoreWarnings();
487 // Respect any 'noAccess' or 'noListing' flags...
488 if ( is_dir( $dir ) && !$existed ) {
489 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
490 }
491
492 return $status;
493 }
494
495 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
496 $status = $this->newStatus();
497 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
498 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
499 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
500 // Seed new directories with a blank index.html, to prevent crawling...
501 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
502 $this->trapWarnings();
503 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
504 $this->untrapWarnings();
505 if ( $bytes === false ) {
506 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
507 }
508 }
509 // Add a .htaccess file to the root of the container...
510 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
511 AtEase::suppressWarnings();
512 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
513 AtEase::restoreWarnings();
514 if ( $bytes === false ) {
515 $storeDir = "mwstore://{$this->name}/{$shortCont}";
516 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
517 }
518 }
519
520 return $status;
521 }
522
523 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
524 $status = $this->newStatus();
525 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
526 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
527 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
528 // Unseed new directories with a blank index.html, to allow crawling...
529 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
530 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
531 if ( $exists && !$this->unlink( "{$dir}/index.html" ) ) { // reverse secure()
532 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
533 }
534 }
535 // Remove the .htaccess file from the root of the container...
536 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
537 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
538 if ( $exists && !$this->unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
539 $storeDir = "mwstore://{$this->name}/{$shortCont}";
540 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
541 }
542 }
543
544 return $status;
545 }
546
547 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
548 $status = $this->newStatus();
549 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
550 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
551 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
552 AtEase::suppressWarnings();
553 if ( is_dir( $dir ) ) {
554 rmdir( $dir ); // remove directory if empty
555 }
556 AtEase::restoreWarnings();
557
558 return $status;
559 }
560
561 protected function doGetFileStat( array $params ) {
562 $source = $this->resolveToFSPath( $params['src'] );
563 if ( $source === null ) {
564 return self::$RES_ERROR; // invalid storage path
565 }
566
567 $this->trapWarnings(); // don't trust 'false' if there were errors
568 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
569 $hadError = $this->untrapWarnings();
570
571 if ( is_array( $stat ) ) {
572 $ct = new ConvertibleTimestamp( $stat['mtime'] );
573
574 return [
575 'mtime' => $ct->getTimestamp( TS_MW ),
576 'size' => $stat['size']
577 ];
578 }
579
580 return $hadError ? self::$RES_ERROR : self::$RES_ABSENT;
581 }
582
583 protected function doClearCache( array $paths = null ) {
584 clearstatcache(); // clear the PHP file stat cache
585 }
586
587 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
588 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
589 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
590 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
591
592 $this->trapWarnings(); // don't trust 'false' if there were errors
593 $exists = is_dir( $dir );
594 $hadError = $this->untrapWarnings();
595
596 return $hadError ? self::$RES_ERROR : $exists;
597 }
598
599 /**
600 * @see FileBackendStore::getDirectoryListInternal()
601 * @param string $fullCont
602 * @param string $dirRel
603 * @param array $params
604 * @return array|FSFileBackendDirList|null
605 */
606 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
607 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
608 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
609 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
610
611 $this->trapWarnings(); // don't trust 'false' if there were errors
612 $exists = is_dir( $dir );
613 $isReadable = $exists ? is_readable( $dir ) : false;
614 $hadError = $this->untrapWarnings();
615
616 if ( $isReadable ) {
617 return new FSFileBackendDirList( $dir, $params );
618 } elseif ( $exists ) {
619 $this->logger->warning( __METHOD__ . ": given directory is unreadable: '$dir'" );
620
621 return self::$RES_ERROR; // bad permissions?
622 } elseif ( $hadError ) {
623 $this->logger->warning( __METHOD__ . ": given directory was unreachable: '$dir'" );
624
625 return self::$RES_ERROR;
626 } else {
627 $this->logger->info( __METHOD__ . ": given directory does not exist: '$dir'" );
628
629 return []; // nothing under this dir
630 }
631 }
632
633 /**
634 * @see FileBackendStore::getFileListInternal()
635 * @param string $fullCont
636 * @param string $dirRel
637 * @param array $params
638 * @return array|FSFileBackendFileList|null
639 */
640 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
641 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
642 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
643 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
644
645 $this->trapWarnings(); // don't trust 'false' if there were errors
646 $exists = is_dir( $dir );
647 $isReadable = $exists ? is_readable( $dir ) : false;
648 $hadError = $this->untrapWarnings();
649
650 if ( $exists && $isReadable ) {
651 return new FSFileBackendFileList( $dir, $params );
652 } elseif ( $exists ) {
653 $this->logger->warning( __METHOD__ . ": given directory is unreadable: '$dir'\n" );
654
655 return self::$RES_ERROR; // bad permissions?
656 } elseif ( $hadError ) {
657 $this->logger->warning( __METHOD__ . ": given directory was unreachable: '$dir'\n" );
658
659 return self::$RES_ERROR;
660 } else {
661 $this->logger->info( __METHOD__ . ": given directory does not exist: '$dir'\n" );
662
663 return []; // nothing under this dir
664 }
665 }
666
667 protected function doGetLocalReferenceMulti( array $params ) {
668 $fsFiles = []; // (path => FSFile)
669
670 foreach ( $params['srcs'] as $src ) {
671 $source = $this->resolveToFSPath( $src );
672 if ( $source === null ) {
673 $fsFiles[$src] = self::$RES_ERROR; // invalid path
674 continue;
675 }
676
677 $this->trapWarnings(); // don't trust 'false' if there were errors
678 $isFile = is_file( $source ); // regular files only
679 $hadError = $this->untrapWarnings();
680
681 if ( $isFile ) {
682 $fsFiles[$src] = new FSFile( $source );
683 } elseif ( $hadError ) {
684 $fsFiles[$src] = self::$RES_ERROR;
685 } else {
686 $fsFiles[$src] = self::$RES_ABSENT;
687 }
688 }
689
690 return $fsFiles;
691 }
692
693 protected function doGetLocalCopyMulti( array $params ) {
694 $tmpFiles = []; // (path => TempFSFile)
695
696 foreach ( $params['srcs'] as $src ) {
697 $source = $this->resolveToFSPath( $src );
698 if ( $source === null ) {
699 $tmpFiles[$src] = self::$RES_ERROR; // invalid path
700 continue;
701 }
702 // Create a new temporary file with the same extension...
703 $ext = FileBackend::extensionFromPath( $src );
704 $tmpFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
705 if ( !$tmpFile ) {
706 $tmpFiles[$src] = self::$RES_ERROR;
707 continue;
708 }
709
710 $tmpPath = $tmpFile->getPath();
711 // Copy the source file over the temp file
712 $this->trapWarnings(); // don't trust 'false' if there were errors
713 $isFile = is_file( $source ); // regular files only
714 $copySuccess = $isFile ? copy( $source, $tmpPath ) : false;
715 $hadError = $this->untrapWarnings();
716
717 if ( $copySuccess ) {
718 $this->chmod( $tmpPath );
719 $tmpFiles[$src] = $tmpFile;
720 } elseif ( $hadError ) {
721 $tmpFiles[$src] = self::$RES_ERROR; // copy failed
722 } else {
723 $tmpFiles[$src] = self::$RES_ABSENT;
724 }
725 }
726
727 return $tmpFiles;
728 }
729
730 protected function directoriesAreVirtual() {
731 return false;
732 }
733
734 /**
735 * @param FSFileOpHandle[] $fileOpHandles
736 *
737 * @return StatusValue[]
738 */
739 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
740 $statuses = [];
741
742 $pipes = [];
743 $octalPermissions = '0' . decoct( $this->fileMode );
744 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
745 $cmd = "{$fileOpHandle->cmd} 2>&1";
746 // Add a post-operation chmod command for permissions cleanup if applicable
747 if (
748 !$this->isWindows &&
749 $fileOpHandle->chmodPath !== null &&
750 strlen( $octalPermissions ) == 4
751 ) {
752 $encPath = escapeshellarg( $fileOpHandle->chmodPath );
753 $cmd .= " && chmod $octalPermissions $encPath 2>/dev/null";
754 }
755 $pipes[$index] = popen( $cmd, 'r' );
756 }
757
758 $errs = [];
759 foreach ( $pipes as $index => $pipe ) {
760 // Result will be empty on success in *NIX. On Windows,
761 // it may be something like " 1 file(s) [copied|moved].".
762 $errs[$index] = stream_get_contents( $pipe );
763 fclose( $pipe );
764 }
765
766 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
767 $status = $this->newStatus();
768 $function = $fileOpHandle->call;
769 $function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
770 $statuses[$index] = $status;
771 }
772
773 clearstatcache(); // files changed
774
775 return $statuses;
776 }
777
778 /**
779 * Chmod a file, suppressing the warnings
780 *
781 * @param string $path Absolute file system path
782 * @return bool Success
783 */
784 protected function chmod( $path ) {
785 if ( $this->isWindows ) {
786 return true;
787 }
788
789 AtEase::suppressWarnings();
790 $ok = chmod( $path, $this->fileMode );
791 AtEase::restoreWarnings();
792
793 return $ok;
794 }
795
796 /**
797 * Unlink a file, suppressing the warnings
798 *
799 * @param string $path Absolute file system path
800 * @return bool Success
801 */
802 protected function unlink( $path ) {
803 AtEase::suppressWarnings();
804 $ok = unlink( $path );
805 AtEase::restoreWarnings();
806
807 return $ok;
808 }
809
810 /**
811 * @param array $params Operation parameters with 'content' and 'headers' fields
812 * @return TempFSFile|null
813 */
814 protected function stageContentAsTempFile( array $params ) {
815 $content = $params['content'];
816 $tempFile = $this->tmpFileFactory->newTempFSFile( 'create_', 'tmp' );
817 if ( !$tempFile ) {
818 return null;
819 }
820
821 AtEase::suppressWarnings();
822 $tmpPath = $tempFile->getPath();
823 if ( file_put_contents( $tmpPath, $content ) === false ) {
824 $tempFile = null;
825 }
826 AtEase::restoreWarnings();
827
828 return $tempFile;
829 }
830
831 /**
832 * Return the text of an index.html file to hide directory listings
833 *
834 * @return string
835 */
836 protected function indexHtmlPrivate() {
837 return '';
838 }
839
840 /**
841 * Return the text of a .htaccess file to make a directory private
842 *
843 * @return string
844 */
845 protected function htaccessPrivate() {
846 return "Deny from all\n";
847 }
848
849 /**
850 * Clean up directory separators for the given OS
851 *
852 * @param string $path FS path
853 * @return string
854 */
855 protected function cleanPathSlashes( $path ) {
856 return $this->isWindows ? strtr( $path, '/', '\\' ) : $path;
857 }
858
859 /**
860 * Listen for E_WARNING errors and track whether any that happen
861 *
862 * @param string|null $regexIgnore Optional regex of errors to ignore
863 */
864 protected function trapWarnings( $regexIgnore = null ) {
865 $this->warningTrapStack[] = false;
866 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
867 if ( $regexIgnore === null || !preg_match( $regexIgnore, $errstr ) ) {
868 $this->logger->error( $errstr );
869 $this->warningTrapStack[count( $this->warningTrapStack ) - 1] = true;
870 }
871 return true; // suppress from PHP handler
872 }, E_WARNING );
873 }
874
875 /**
876 * Stop listening for E_WARNING errors and get whether any happened
877 *
878 * @return bool Whether any warnings happened
879 */
880 protected function untrapWarnings() {
881 restore_error_handler();
882
883 return array_pop( $this->warningTrapStack );
884 }
885 }