filebackend: optimize the chmod() calls in FSFileBackend
[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 File permission mode */
70 protected $fileMode;
71 /** @var int File permission mode */
72 protected $dirMode;
73
74 /** @var string Required OS username to own files */
75 protected $fileOwner;
76
77 /** @var bool Whether the OS is Windows (otherwise assumed Unix-like)*/
78 protected $isWindows;
79 /** @var string OS username running this script */
80 protected $currentUser;
81
82 /** @var array */
83 protected $hadWarningErrors = [];
84
85 /**
86 * @see FileBackendStore::__construct()
87 * Additional $config params include:
88 * - basePath : File system directory that holds containers.
89 * - containerPaths : Map of container names to custom file system directories.
90 * This should only be used for backwards-compatibility.
91 * - fileMode : Octal UNIX file permissions to use on files stored.
92 * - directoryMode : Octal UNIX file permissions to use on directories created.
93 * @param array $config
94 */
95 public function __construct( array $config ) {
96 parent::__construct( $config );
97
98 $this->isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' );
99 // Remove any possible trailing slash from directories
100 if ( isset( $config['basePath'] ) ) {
101 $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
102 } else {
103 $this->basePath = null; // none; containers must have explicit paths
104 }
105
106 $this->containerPaths = [];
107 foreach ( ( $config['containerPaths'] ?? [] ) as $container => $path ) {
108 $this->containerPaths[$container] = rtrim( $path, '/' ); // remove trailing slash
109 }
110
111 $this->fileMode = $config['fileMode'] ?? 0644;
112 $this->dirMode = $config['directoryMode'] ?? 0777;
113 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
114 $this->fileOwner = $config['fileOwner'];
115 // cache this, assuming it doesn't change
116 $this->currentUser = posix_getpwuid( posix_getuid() )['name'];
117 }
118 }
119
120 public function getFeatures() {
121 if ( $this->isWindows && version_compare( PHP_VERSION, '7.1', 'lt' ) ) {
122 // PHP before 7.1 used 8-bit code page for filesystem paths on Windows;
123 // See https://www.php.net/manual/en/migration71.windows-support.php
124 return 0;
125 } else {
126 return FileBackend::ATTR_UNICODE_PATHS;
127 }
128 }
129
130 protected function resolveContainerPath( $container, $relStoragePath ) {
131 // Check that container has a root directory
132 if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
133 // Check for sane relative paths (assume the base paths are OK)
134 if ( $this->isLegalRelPath( $relStoragePath ) ) {
135 return $relStoragePath;
136 }
137 }
138
139 return null; // invalid
140 }
141
142 /**
143 * Sanity check a relative file system path for validity
144 *
145 * @param string $path Normalized relative path
146 * @return bool
147 */
148 protected function isLegalRelPath( $path ) {
149 // Check for file names longer than 255 chars
150 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
151 return false;
152 }
153 if ( $this->isWindows ) { // NTFS
154 return !preg_match( '![:*?"<>|]!', $path );
155 } else {
156 return true;
157 }
158 }
159
160 /**
161 * Given the short (unresolved) and full (resolved) name of
162 * a container, return the file system path of the container.
163 *
164 * @param string $shortCont
165 * @param string $fullCont
166 * @return string|null
167 */
168 protected function containerFSRoot( $shortCont, $fullCont ) {
169 if ( isset( $this->containerPaths[$shortCont] ) ) {
170 return $this->containerPaths[$shortCont];
171 } elseif ( isset( $this->basePath ) ) {
172 return "{$this->basePath}/{$fullCont}";
173 }
174
175 return null; // no container base path defined
176 }
177
178 /**
179 * Get the absolute file system path for a storage path
180 *
181 * @param string $storagePath Storage path
182 * @return string|null
183 */
184 protected function resolveToFSPath( $storagePath ) {
185 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
186 if ( $relPath === null ) {
187 return null; // invalid
188 }
189 list( , $shortCont, ) = FileBackend::splitStoragePath( $storagePath );
190 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
191 if ( $relPath != '' ) {
192 $fsPath .= "/{$relPath}";
193 }
194
195 return $fsPath;
196 }
197
198 public function isPathUsableInternal( $storagePath ) {
199 $fsPath = $this->resolveToFSPath( $storagePath );
200 if ( $fsPath === null ) {
201 return false; // invalid
202 }
203 $parentDir = dirname( $fsPath );
204
205 if ( file_exists( $fsPath ) ) {
206 $ok = is_file( $fsPath ) && is_writable( $fsPath );
207 } else {
208 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
209 }
210
211 if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) {
212 $ok = false;
213 trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." );
214 }
215
216 return $ok;
217 }
218
219 protected function doCreateInternal( array $params ) {
220 $status = $this->newStatus();
221
222 $dest = $this->resolveToFSPath( $params['dst'] );
223 if ( $dest === null ) {
224 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
225
226 return $status;
227 }
228
229 if ( !empty( $params['async'] ) ) { // deferred
230 $tempFile = $this->stageContentAsTempFile( $params );
231 if ( !$tempFile ) {
232 $status->fatal( 'backend-fail-create', $params['dst'] );
233
234 return $status;
235 }
236 $cmd = implode( ' ', [
237 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
238 escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
239 escapeshellarg( $this->cleanPathSlashes( $dest ) )
240 ] );
241 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
242 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
243 $status->fatal( 'backend-fail-create', $params['dst'] );
244 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
245 }
246 };
247 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
248 $tempFile->bind( $status->value );
249 } else { // immediate write
250 $this->trapWarnings();
251 $bytes = file_put_contents( $dest, $params['content'] );
252 $this->untrapWarnings();
253 if ( $bytes === false ) {
254 $status->fatal( 'backend-fail-create', $params['dst'] );
255
256 return $status;
257 }
258 $this->chmod( $dest );
259 }
260
261 return $status;
262 }
263
264 protected function doStoreInternal( array $params ) {
265 $status = $this->newStatus();
266
267 $dest = $this->resolveToFSPath( $params['dst'] );
268 if ( $dest === null ) {
269 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
270
271 return $status;
272 }
273
274 if ( !empty( $params['async'] ) ) { // deferred
275 $cmd = implode( ' ', [
276 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
277 escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ),
278 escapeshellarg( $this->cleanPathSlashes( $dest ) )
279 ] );
280 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
281 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
282 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
283 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
284 }
285 };
286 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
287 } else { // immediate write
288 $this->trapWarnings();
289 $ok = copy( $params['src'], $dest );
290 $this->untrapWarnings();
291 // In some cases (at least over NFS), copy() returns true when it fails
292 if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
293 if ( $ok ) { // PHP bug
294 unlink( $dest ); // remove broken file
295 trigger_error( __METHOD__ . ": copy() failed but returned true." );
296 }
297 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
298
299 return $status;
300 }
301 $this->chmod( $dest );
302 }
303
304 return $status;
305 }
306
307 protected function doCopyInternal( array $params ) {
308 $status = $this->newStatus();
309
310 $source = $this->resolveToFSPath( $params['src'] );
311 if ( $source === null ) {
312 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
313
314 return $status;
315 }
316
317 $dest = $this->resolveToFSPath( $params['dst'] );
318 if ( $dest === null ) {
319 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
320
321 return $status;
322 }
323
324 if ( !is_file( $source ) ) {
325 if ( empty( $params['ignoreMissingSource'] ) ) {
326 $status->fatal( 'backend-fail-copy', $params['src'] );
327 }
328
329 return $status; // do nothing; either OK or bad status
330 }
331
332 if ( !empty( $params['async'] ) ) { // deferred
333 $cmd = implode( ' ', [
334 $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
335 escapeshellarg( $this->cleanPathSlashes( $source ) ),
336 escapeshellarg( $this->cleanPathSlashes( $dest ) )
337 ] );
338 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
339 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
340 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
341 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
342 }
343 };
344 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
345 } else { // immediate write
346 $this->trapWarnings();
347 $ok = ( $source === $dest ) ? true : copy( $source, $dest );
348 $this->untrapWarnings();
349 // In some cases (at least over NFS), copy() returns true when it fails
350 if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
351 if ( $ok ) { // PHP bug
352 $this->trapWarnings();
353 unlink( $dest ); // remove broken file
354 $this->untrapWarnings();
355 trigger_error( __METHOD__ . ": copy() failed but returned true." );
356 }
357 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
358
359 return $status;
360 }
361 $this->chmod( $dest );
362 }
363
364 return $status;
365 }
366
367 protected function doMoveInternal( array $params ) {
368 $status = $this->newStatus();
369
370 $source = $this->resolveToFSPath( $params['src'] );
371 if ( $source === null ) {
372 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
373
374 return $status;
375 }
376
377 $dest = $this->resolveToFSPath( $params['dst'] );
378 if ( $dest === null ) {
379 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
380
381 return $status;
382 }
383
384 if ( !is_file( $source ) ) {
385 if ( empty( $params['ignoreMissingSource'] ) ) {
386 $status->fatal( 'backend-fail-move', $params['src'] );
387 }
388
389 return $status; // do nothing; either OK or bad status
390 }
391
392 if ( !empty( $params['async'] ) ) { // deferred
393 $cmd = implode( ' ', [
394 $this->isWindows ? 'MOVE /Y' : 'mv', // (overwrite)
395 escapeshellarg( $this->cleanPathSlashes( $source ) ),
396 escapeshellarg( $this->cleanPathSlashes( $dest ) )
397 ] );
398 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
399 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
400 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
401 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
402 }
403 };
404 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
405 } else { // immediate write
406 $this->trapWarnings();
407 $ok = ( $source === $dest ) ? true : rename( $source, $dest );
408 $this->untrapWarnings();
409 clearstatcache(); // file no longer at source
410 if ( !$ok ) {
411 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
412
413 return $status;
414 }
415 }
416
417 return $status;
418 }
419
420 protected function doDeleteInternal( array $params ) {
421 $status = $this->newStatus();
422
423 $source = $this->resolveToFSPath( $params['src'] );
424 if ( $source === null ) {
425 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
426
427 return $status;
428 }
429
430 if ( !is_file( $source ) ) {
431 if ( empty( $params['ignoreMissingSource'] ) ) {
432 $status->fatal( 'backend-fail-delete', $params['src'] );
433 }
434
435 return $status; // do nothing; either OK or bad status
436 }
437
438 if ( !empty( $params['async'] ) ) { // deferred
439 $cmd = implode( ' ', [
440 $this->isWindows ? 'DEL' : 'unlink',
441 escapeshellarg( $this->cleanPathSlashes( $source ) )
442 ] );
443 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
444 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
445 $status->fatal( 'backend-fail-delete', $params['src'] );
446 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
447 }
448 };
449 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
450 } else { // immediate write
451 $ok = $this->unlink( $source );
452 if ( !$ok ) {
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();
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 happen
861 */
862 protected function trapWarnings() {
863 // push to stack
864 $this->hadWarningErrors[] = false;
865 set_error_handler( function ( $errno, $errstr ) {
866 // more detailed error logging
867 $this->logger->error( $errstr );
868 $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
869
870 // suppress from PHP handler
871 return true;
872 }, E_WARNING );
873 }
874
875 /**
876 * Stop listening for E_WARNING errors and return true if any happened
877 *
878 * @return bool
879 */
880 protected function untrapWarnings() {
881 // restore previous handler
882 restore_error_handler();
883 // pop from stack
884 return array_pop( $this->hadWarningErrors );
885 }
886 }