filebackend: clean up some comments and remove unused FileBackendStoreOpHandle field
[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 self::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 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
370 if ( $fsSrcPath === null ) {
371 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
372
373 return $status;
374 }
375
376 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
377 if ( $fsDstPath === null ) {
378 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
379
380 return $status;
381 }
382
383 if ( $fsSrcPath === $fsDstPath ) {
384 return $status; // no-op
385 }
386
387 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
388
389 if ( !empty( $params['async'] ) ) { // deferred
390 // https://manpages.debian.org/buster/coreutils/mv.1.en.html
391 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/move
392 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
393 $encDst = escapeshellarg( $this->cleanPathSlashes( $fsDstPath ) );
394 if ( $this->isWindows ) {
395 $writeCmd = "MOVE /Y $encSrc $encDst";
396 $cmd = $ignoreMissing ? "IF EXIST $encSrc $writeCmd" : $writeCmd;
397 } else {
398 $writeCmd = "mv -f $encSrc $encDst";
399 $cmd = $ignoreMissing ? "test -f $encSrc && $writeCmd" : $writeCmd;
400 }
401 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
402 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
403 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
404 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
405 }
406 };
407 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
408 } else { // immediate write
409 // Use rename() here since (a) this clears xattrs, (b) any threads still reading the
410 // old inode are unaffected since it writes to a new inode, and (c) this is fast and
411 // atomic within a file system volume (as is normally the case)
412 $this->trapWarnings( '/: No such file or directory$/' );
413 $moved = rename( $fsSrcPath, $fsDstPath );
414 $hadError = $this->untrapWarnings();
415 if ( $hadError || ( !$moved && !$ignoreMissing ) ) {
416 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
417
418 return $status;
419 }
420 }
421
422 return $status;
423 }
424
425 protected function doDeleteInternal( array $params ) {
426 $status = $this->newStatus();
427
428 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
429 if ( $fsSrcPath === null ) {
430 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
431
432 return $status;
433 }
434
435 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
436
437 if ( !empty( $params['async'] ) ) { // deferred
438 // https://manpages.debian.org/buster/coreutils/rm.1.en.html
439 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del
440 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
441 if ( $this->isWindows ) {
442 $writeCmd = "DEL /Q $encSrc";
443 $cmd = $ignoreMissing ? "IF EXIST $encSrc $writeCmd" : $writeCmd;
444 } else {
445 $cmd = $ignoreMissing ? "rm -f $encSrc" : "rm $encSrc";
446 }
447 $handler = function ( $errors, StatusValue $status, array $params, $cmd ) {
448 if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) {
449 $status->fatal( 'backend-fail-delete', $params['src'] );
450 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
451 }
452 };
453 $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd );
454 } else { // immediate write
455 $this->trapWarnings( '/: No such file or directory$/' );
456 $deleted = unlink( $fsSrcPath );
457 $hadError = $this->untrapWarnings();
458 if ( $hadError || ( !$deleted && !$ignoreMissing ) ) {
459 $status->fatal( 'backend-fail-delete', $params['src'] );
460
461 return $status;
462 }
463 }
464
465 return $status;
466 }
467
468 /**
469 * @param string $fullCont
470 * @param string $dirRel
471 * @param array $params
472 * @return StatusValue
473 */
474 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
475 $status = $this->newStatus();
476 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
477 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
478 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
479 $existed = is_dir( $dir ); // already there?
480 // Create the directory and its parents as needed...
481 AtEase::suppressWarnings();
482 if ( !$existed && !mkdir( $dir, $this->dirMode, true ) && !is_dir( $dir ) ) {
483 $this->logger->error( __METHOD__ . ": cannot create directory $dir" );
484 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
485 } elseif ( !is_writable( $dir ) ) {
486 $this->logger->error( __METHOD__ . ": directory $dir is read-only" );
487 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
488 } elseif ( !is_readable( $dir ) ) {
489 $this->logger->error( __METHOD__ . ": directory $dir is not readable" );
490 $status->fatal( 'directorynotreadableerror', $params['dir'] );
491 }
492 AtEase::restoreWarnings();
493 // Respect any 'noAccess' or 'noListing' flags...
494 if ( is_dir( $dir ) && !$existed ) {
495 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
496 }
497
498 return $status;
499 }
500
501 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
502 $status = $this->newStatus();
503 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
504 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
505 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
506 // Seed new directories with a blank index.html, to prevent crawling...
507 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
508 $this->trapWarnings();
509 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
510 $this->untrapWarnings();
511 if ( $bytes === false ) {
512 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
513 }
514 }
515 // Add a .htaccess file to the root of the container...
516 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
517 AtEase::suppressWarnings();
518 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
519 AtEase::restoreWarnings();
520 if ( $bytes === false ) {
521 $storeDir = "mwstore://{$this->name}/{$shortCont}";
522 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
523 }
524 }
525
526 return $status;
527 }
528
529 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
530 $status = $this->newStatus();
531 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
532 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
533 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
534 // Unseed new directories with a blank index.html, to allow crawling...
535 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
536 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
537 if ( $exists && !$this->unlink( "{$dir}/index.html" ) ) { // reverse secure()
538 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
539 }
540 }
541 // Remove the .htaccess file from the root of the container...
542 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
543 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
544 if ( $exists && !$this->unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
545 $storeDir = "mwstore://{$this->name}/{$shortCont}";
546 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
547 }
548 }
549
550 return $status;
551 }
552
553 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
554 $status = $this->newStatus();
555 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
556 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
557 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
558 AtEase::suppressWarnings();
559 if ( is_dir( $dir ) ) {
560 rmdir( $dir ); // remove directory if empty
561 }
562 AtEase::restoreWarnings();
563
564 return $status;
565 }
566
567 protected function doGetFileStat( array $params ) {
568 $source = $this->resolveToFSPath( $params['src'] );
569 if ( $source === null ) {
570 return self::$RES_ERROR; // invalid storage path
571 }
572
573 $this->trapWarnings(); // don't trust 'false' if there were errors
574 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
575 $hadError = $this->untrapWarnings();
576
577 if ( is_array( $stat ) ) {
578 $ct = new ConvertibleTimestamp( $stat['mtime'] );
579
580 return [
581 'mtime' => $ct->getTimestamp( TS_MW ),
582 'size' => $stat['size']
583 ];
584 }
585
586 return $hadError ? self::$RES_ERROR : self::$RES_ABSENT;
587 }
588
589 protected function doClearCache( array $paths = null ) {
590 clearstatcache(); // clear the PHP file stat cache
591 }
592
593 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
594 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
595 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
596 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
597
598 $this->trapWarnings(); // don't trust 'false' if there were errors
599 $exists = is_dir( $dir );
600 $hadError = $this->untrapWarnings();
601
602 return $hadError ? self::$RES_ERROR : $exists;
603 }
604
605 /**
606 * @see FileBackendStore::getDirectoryListInternal()
607 * @param string $fullCont
608 * @param string $dirRel
609 * @param array $params
610 * @return array|FSFileBackendDirList|null
611 */
612 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
613 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
614 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
615 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
616
617 $this->trapWarnings(); // don't trust 'false' if there were errors
618 $exists = is_dir( $dir );
619 $isReadable = $exists ? is_readable( $dir ) : false;
620 $hadError = $this->untrapWarnings();
621
622 if ( $isReadable ) {
623 return new FSFileBackendDirList( $dir, $params );
624 } elseif ( $exists ) {
625 $this->logger->warning( __METHOD__ . ": given directory is unreadable: '$dir'" );
626
627 return self::$RES_ERROR; // bad permissions?
628 } elseif ( $hadError ) {
629 $this->logger->warning( __METHOD__ . ": given directory was unreachable: '$dir'" );
630
631 return self::$RES_ERROR;
632 } else {
633 $this->logger->info( __METHOD__ . ": given directory does not exist: '$dir'" );
634
635 return []; // nothing under this dir
636 }
637 }
638
639 /**
640 * @see FileBackendStore::getFileListInternal()
641 * @param string $fullCont
642 * @param string $dirRel
643 * @param array $params
644 * @return array|FSFileBackendFileList|null
645 */
646 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
647 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
648 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
649 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
650
651 $this->trapWarnings(); // don't trust 'false' if there were errors
652 $exists = is_dir( $dir );
653 $isReadable = $exists ? is_readable( $dir ) : false;
654 $hadError = $this->untrapWarnings();
655
656 if ( $exists && $isReadable ) {
657 return new FSFileBackendFileList( $dir, $params );
658 } elseif ( $exists ) {
659 $this->logger->warning( __METHOD__ . ": given directory is unreadable: '$dir'\n" );
660
661 return self::$RES_ERROR; // bad permissions?
662 } elseif ( $hadError ) {
663 $this->logger->warning( __METHOD__ . ": given directory was unreachable: '$dir'\n" );
664
665 return self::$RES_ERROR;
666 } else {
667 $this->logger->info( __METHOD__ . ": given directory does not exist: '$dir'\n" );
668
669 return []; // nothing under this dir
670 }
671 }
672
673 protected function doGetLocalReferenceMulti( array $params ) {
674 $fsFiles = []; // (path => FSFile)
675
676 foreach ( $params['srcs'] as $src ) {
677 $source = $this->resolveToFSPath( $src );
678 if ( $source === null ) {
679 $fsFiles[$src] = self::$RES_ERROR; // invalid path
680 continue;
681 }
682
683 $this->trapWarnings(); // don't trust 'false' if there were errors
684 $isFile = is_file( $source ); // regular files only
685 $hadError = $this->untrapWarnings();
686
687 if ( $isFile ) {
688 $fsFiles[$src] = new FSFile( $source );
689 } elseif ( $hadError ) {
690 $fsFiles[$src] = self::$RES_ERROR;
691 } else {
692 $fsFiles[$src] = self::$RES_ABSENT;
693 }
694 }
695
696 return $fsFiles;
697 }
698
699 protected function doGetLocalCopyMulti( array $params ) {
700 $tmpFiles = []; // (path => TempFSFile)
701
702 foreach ( $params['srcs'] as $src ) {
703 $source = $this->resolveToFSPath( $src );
704 if ( $source === null ) {
705 $tmpFiles[$src] = self::$RES_ERROR; // invalid path
706 continue;
707 }
708 // Create a new temporary file with the same extension...
709 $ext = FileBackend::extensionFromPath( $src );
710 $tmpFile = $this->tmpFileFactory->newTempFSFile( 'localcopy_', $ext );
711 if ( !$tmpFile ) {
712 $tmpFiles[$src] = self::$RES_ERROR;
713 continue;
714 }
715
716 $tmpPath = $tmpFile->getPath();
717 // Copy the source file over the temp file
718 $this->trapWarnings(); // don't trust 'false' if there were errors
719 $isFile = is_file( $source ); // regular files only
720 $copySuccess = $isFile ? copy( $source, $tmpPath ) : false;
721 $hadError = $this->untrapWarnings();
722
723 if ( $copySuccess ) {
724 $this->chmod( $tmpPath );
725 $tmpFiles[$src] = $tmpFile;
726 } elseif ( $hadError ) {
727 $tmpFiles[$src] = self::$RES_ERROR; // copy failed
728 } else {
729 $tmpFiles[$src] = self::$RES_ABSENT;
730 }
731 }
732
733 return $tmpFiles;
734 }
735
736 protected function directoriesAreVirtual() {
737 return false;
738 }
739
740 /**
741 * @param FSFileOpHandle[] $fileOpHandles
742 *
743 * @return StatusValue[]
744 */
745 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
746 $statuses = [];
747
748 $pipes = [];
749 $octalPermissions = '0' . decoct( $this->fileMode );
750 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
751 $cmd = "{$fileOpHandle->cmd} 2>&1";
752 // Add a post-operation chmod command for permissions cleanup if applicable
753 if (
754 !$this->isWindows &&
755 $fileOpHandle->chmodPath !== null &&
756 strlen( $octalPermissions ) == 4
757 ) {
758 $encPath = escapeshellarg( $fileOpHandle->chmodPath );
759 $cmd .= " && chmod $octalPermissions $encPath 2>/dev/null";
760 }
761 $pipes[$index] = popen( $cmd, 'r' );
762 }
763
764 $errs = [];
765 foreach ( $pipes as $index => $pipe ) {
766 // Result will be empty on success in *NIX. On Windows,
767 // it may be something like " 1 file(s) [copied|moved].".
768 $errs[$index] = stream_get_contents( $pipe );
769 fclose( $pipe );
770 }
771
772 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
773 $status = $this->newStatus();
774 $function = $fileOpHandle->call;
775 $function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
776 $statuses[$index] = $status;
777 }
778
779 clearstatcache(); // files changed
780
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 if ( $this->isWindows ) {
792 return true;
793 }
794
795 AtEase::suppressWarnings();
796 $ok = chmod( $path, $this->fileMode );
797 AtEase::restoreWarnings();
798
799 return $ok;
800 }
801
802 /**
803 * Unlink a file, suppressing the warnings
804 *
805 * @param string $path Absolute file system path
806 * @return bool Success
807 */
808 protected function unlink( $path ) {
809 AtEase::suppressWarnings();
810 $ok = unlink( $path );
811 AtEase::restoreWarnings();
812
813 return $ok;
814 }
815
816 /**
817 * @param array $params Operation parameters with 'content' and 'headers' fields
818 * @return TempFSFile|null
819 */
820 protected function stageContentAsTempFile( array $params ) {
821 $content = $params['content'];
822 $tempFile = $this->tmpFileFactory->newTempFSFile( 'create_', 'tmp' );
823 if ( !$tempFile ) {
824 return null;
825 }
826
827 AtEase::suppressWarnings();
828 $tmpPath = $tempFile->getPath();
829 if ( file_put_contents( $tmpPath, $content ) === false ) {
830 $tempFile = null;
831 }
832 AtEase::restoreWarnings();
833
834 return $tempFile;
835 }
836
837 /**
838 * Return the text of an index.html file to hide directory listings
839 *
840 * @return string
841 */
842 protected function indexHtmlPrivate() {
843 return '';
844 }
845
846 /**
847 * Return the text of a .htaccess file to make a directory private
848 *
849 * @return string
850 */
851 protected function htaccessPrivate() {
852 return "Deny from all\n";
853 }
854
855 /**
856 * Clean up directory separators for the given OS
857 *
858 * @param string $path FS path
859 * @return string
860 */
861 protected function cleanPathSlashes( $path ) {
862 return $this->isWindows ? strtr( $path, '/', '\\' ) : $path;
863 }
864
865 /**
866 * Listen for E_WARNING errors and track whether any that happen
867 *
868 * @param string|null $regexIgnore Optional regex of errors to ignore
869 */
870 protected function trapWarnings( $regexIgnore = null ) {
871 $this->warningTrapStack[] = false;
872 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
873 if ( $regexIgnore === null || !preg_match( $regexIgnore, $errstr ) ) {
874 $this->logger->error( $errstr );
875 $this->warningTrapStack[count( $this->warningTrapStack ) - 1] = true;
876 }
877 return true; // suppress from PHP handler
878 }, E_WARNING );
879 }
880
881 /**
882 * Stop listening for E_WARNING errors and get whether any happened
883 *
884 * @return bool Whether any warnings happened
885 */
886 protected function untrapWarnings() {
887 restore_error_handler();
888
889 return array_pop( $this->warningTrapStack );
890 }
891 }