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