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