Merge "Make validation for page more strict on djvu to take only numbers"
[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 $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
217 $tempFile->bind( $status->value );
218 } else { // immediate write
219 $this->trapWarnings();
220 $bytes = file_put_contents( $dest, $params['content'] );
221 $this->untrapWarnings();
222 if ( $bytes === false ) {
223 $status->fatal( 'backend-fail-create', $params['dst'] );
224
225 return $status;
226 }
227 $this->chmod( $dest );
228 }
229
230 return $status;
231 }
232
233 /**
234 * @see FSFileBackend::doExecuteOpHandlesInternal()
235 */
236 protected function getResponseCreate( $errors, Status $status, array $params, $cmd ) {
237 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
238 $status->fatal( 'backend-fail-create', $params['dst'] );
239 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
240 }
241 }
242
243 protected function doStoreInternal( array $params ) {
244 $status = Status::newGood();
245
246 $dest = $this->resolveToFSPath( $params['dst'] );
247 if ( $dest === null ) {
248 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
249
250 return $status;
251 }
252
253 if ( !empty( $params['async'] ) ) { // deferred
254 $cmd = implode( ' ', array(
255 wfIsWindows() ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
256 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
257 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
258 ) );
259 $status->value = new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
260 } else { // immediate write
261 $this->trapWarnings();
262 $ok = copy( $params['src'], $dest );
263 $this->untrapWarnings();
264 // In some cases (at least over NFS), copy() returns true when it fails
265 if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
266 if ( $ok ) { // PHP bug
267 unlink( $dest ); // remove broken file
268 trigger_error( __METHOD__ . ": copy() failed but returned true." );
269 }
270 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
271
272 return $status;
273 }
274 $this->chmod( $dest );
275 }
276
277 return $status;
278 }
279
280 /**
281 * @see FSFileBackend::doExecuteOpHandlesInternal()
282 */
283 protected function getResponseStore( $errors, Status $status, array $params, $cmd ) {
284 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
285 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
286 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
287 }
288 }
289
290 protected function doCopyInternal( array $params ) {
291 $status = Status::newGood();
292
293 $source = $this->resolveToFSPath( $params['src'] );
294 if ( $source === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
296
297 return $status;
298 }
299
300 $dest = $this->resolveToFSPath( $params['dst'] );
301 if ( $dest === null ) {
302 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
303
304 return $status;
305 }
306
307 if ( !is_file( $source ) ) {
308 if ( empty( $params['ignoreMissingSource'] ) ) {
309 $status->fatal( 'backend-fail-copy', $params['src'] );
310 }
311
312 return $status; // do nothing; either OK or bad status
313 }
314
315 if ( !empty( $params['async'] ) ) { // deferred
316 $cmd = implode( ' ', array(
317 wfIsWindows() ? 'COPY /B /Y' : 'cp', // (binary, overwrite)
318 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
319 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
320 ) );
321 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
322 } else { // immediate write
323 $this->trapWarnings();
324 $ok = ( $source === $dest ) ? true : copy( $source, $dest );
325 $this->untrapWarnings();
326 // In some cases (at least over NFS), copy() returns true when it fails
327 if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
328 if ( $ok ) { // PHP bug
329 $this->trapWarnings();
330 unlink( $dest ); // remove broken file
331 $this->untrapWarnings();
332 trigger_error( __METHOD__ . ": copy() failed but returned true." );
333 }
334 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
335
336 return $status;
337 }
338 $this->chmod( $dest );
339 }
340
341 return $status;
342 }
343
344 /**
345 * @see FSFileBackend::doExecuteOpHandlesInternal()
346 */
347 protected function getResponseCopy( $errors, Status $status, array $params, $cmd ) {
348 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
349 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
350 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
351 }
352 }
353
354 protected function doMoveInternal( array $params ) {
355 $status = Status::newGood();
356
357 $source = $this->resolveToFSPath( $params['src'] );
358 if ( $source === null ) {
359 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
360
361 return $status;
362 }
363
364 $dest = $this->resolveToFSPath( $params['dst'] );
365 if ( $dest === null ) {
366 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
367
368 return $status;
369 }
370
371 if ( !is_file( $source ) ) {
372 if ( empty( $params['ignoreMissingSource'] ) ) {
373 $status->fatal( 'backend-fail-move', $params['src'] );
374 }
375
376 return $status; // do nothing; either OK or bad status
377 }
378
379 if ( !empty( $params['async'] ) ) { // deferred
380 $cmd = implode( ' ', array(
381 wfIsWindows() ? 'MOVE /Y' : 'mv', // (overwrite)
382 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
383 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
384 ) );
385 $status->value = new FSFileOpHandle( $this, $params, 'Move', $cmd );
386 } else { // immediate write
387 $this->trapWarnings();
388 $ok = ( $source === $dest ) ? true : rename( $source, $dest );
389 $this->untrapWarnings();
390 clearstatcache(); // file no longer at source
391 if ( !$ok ) {
392 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
393
394 return $status;
395 }
396 }
397
398 return $status;
399 }
400
401 /**
402 * @see FSFileBackend::doExecuteOpHandlesInternal()
403 */
404 protected function getResponseMove( $errors, Status $status, array $params, $cmd ) {
405 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
406 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
407 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
408 }
409 }
410
411 protected function doDeleteInternal( array $params ) {
412 $status = Status::newGood();
413
414 $source = $this->resolveToFSPath( $params['src'] );
415 if ( $source === null ) {
416 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
417
418 return $status;
419 }
420
421 if ( !is_file( $source ) ) {
422 if ( empty( $params['ignoreMissingSource'] ) ) {
423 $status->fatal( 'backend-fail-delete', $params['src'] );
424 }
425
426 return $status; // do nothing; either OK or bad status
427 }
428
429 if ( !empty( $params['async'] ) ) { // deferred
430 $cmd = implode( ' ', array(
431 wfIsWindows() ? 'DEL' : 'unlink',
432 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
433 ) );
434 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd );
435 } else { // immediate write
436 $this->trapWarnings();
437 $ok = unlink( $source );
438 $this->untrapWarnings();
439 if ( !$ok ) {
440 $status->fatal( 'backend-fail-delete', $params['src'] );
441
442 return $status;
443 }
444 }
445
446 return $status;
447 }
448
449 /**
450 * @see FSFileBackend::doExecuteOpHandlesInternal()
451 */
452 protected function getResponseDelete( $errors, Status $status, array $params, $cmd ) {
453 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
454 $status->fatal( 'backend-fail-delete', $params['src'] );
455 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
456 }
457 }
458
459 /**
460 * @param string $fullCont
461 * @param string $dirRel
462 * @param array $params
463 * @return Status
464 */
465 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
466 $status = Status::newGood();
467 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
468 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
469 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
470 $existed = is_dir( $dir ); // already there?
471 // Create the directory and its parents as needed...
472 $this->trapWarnings();
473 if ( !wfMkdirParents( $dir ) ) {
474 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
475 } elseif ( !is_writable( $dir ) ) {
476 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
477 } elseif ( !is_readable( $dir ) ) {
478 $status->fatal( 'directorynotreadableerror', $params['dir'] );
479 }
480 $this->untrapWarnings();
481 // Respect any 'noAccess' or 'noListing' flags...
482 if ( is_dir( $dir ) && !$existed ) {
483 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
484 }
485
486 return $status;
487 }
488
489 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
490 $status = Status::newGood();
491 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
492 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
493 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
494 // Seed new directories with a blank index.html, to prevent crawling...
495 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
496 $this->trapWarnings();
497 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
498 $this->untrapWarnings();
499 if ( $bytes === false ) {
500 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
501 }
502 }
503 // Add a .htaccess file to the root of the container...
504 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
505 $this->trapWarnings();
506 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
507 $this->untrapWarnings();
508 if ( $bytes === false ) {
509 $storeDir = "mwstore://{$this->name}/{$shortCont}";
510 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
511 }
512 }
513
514 return $status;
515 }
516
517 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
518 $status = Status::newGood();
519 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
520 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
521 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
522 // Unseed new directories with a blank index.html, to allow crawling...
523 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
524 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
525 $this->trapWarnings();
526 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
527 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
528 }
529 $this->untrapWarnings();
530 }
531 // Remove the .htaccess file from the root of the container...
532 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
533 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
534 $this->trapWarnings();
535 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
536 $storeDir = "mwstore://{$this->name}/{$shortCont}";
537 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
538 }
539 $this->untrapWarnings();
540 }
541
542 return $status;
543 }
544
545 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
546 $status = Status::newGood();
547 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
548 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
549 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
550 $this->trapWarnings();
551 if ( is_dir( $dir ) ) {
552 rmdir( $dir ); // remove directory if empty
553 }
554 $this->untrapWarnings();
555
556 return $status;
557 }
558
559 protected function doGetFileStat( array $params ) {
560 $source = $this->resolveToFSPath( $params['src'] );
561 if ( $source === null ) {
562 return false; // invalid storage path
563 }
564
565 $this->trapWarnings(); // don't trust 'false' if there were errors
566 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
567 $hadError = $this->untrapWarnings();
568
569 if ( $stat ) {
570 return array(
571 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
572 'size' => $stat['size']
573 );
574 } elseif ( !$hadError ) {
575 return false; // file does not exist
576 } else {
577 return null; // failure
578 }
579 }
580
581 /**
582 * @see FileBackendStore::doClearCache()
583 */
584 protected function doClearCache( array $paths = null ) {
585 clearstatcache(); // clear the PHP file stat cache
586 }
587
588 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
589 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
590 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
591 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
592
593 $this->trapWarnings(); // don't trust 'false' if there were errors
594 $exists = is_dir( $dir );
595 $hadError = $this->untrapWarnings();
596
597 return $hadError ? null : $exists;
598 }
599
600 /**
601 * @see FileBackendStore::getDirectoryListInternal()
602 * @param string $fullCont
603 * @param string $dirRel
604 * @param array $params
605 * @return array|null
606 */
607 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
608 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
609 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
610 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
611 $exists = is_dir( $dir );
612 if ( !$exists ) {
613 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
614
615 return array(); // nothing under this dir
616 } elseif ( !is_readable( $dir ) ) {
617 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
618
619 return null; // bad permissions?
620 }
621
622 return new FSFileBackendDirList( $dir, $params );
623 }
624
625 /**
626 * @see FileBackendStore::getFileListInternal()
627 * @param string $fullCont
628 * @param string $dirRel
629 * @param array $params
630 * @return array|FSFileBackendFileList|null
631 */
632 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
633 list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] );
634 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
635 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
636 $exists = is_dir( $dir );
637 if ( !$exists ) {
638 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
639
640 return array(); // nothing under this dir
641 } elseif ( !is_readable( $dir ) ) {
642 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
643
644 return null; // bad permissions?
645 }
646
647 return new FSFileBackendFileList( $dir, $params );
648 }
649
650 protected function doGetLocalReferenceMulti( array $params ) {
651 $fsFiles = array(); // (path => FSFile)
652
653 foreach ( $params['srcs'] as $src ) {
654 $source = $this->resolveToFSPath( $src );
655 if ( $source === null || !is_file( $source ) ) {
656 $fsFiles[$src] = null; // invalid path or file does not exist
657 } else {
658 $fsFiles[$src] = new FSFile( $source );
659 }
660 }
661
662 return $fsFiles;
663 }
664
665 protected function doGetLocalCopyMulti( array $params ) {
666 $tmpFiles = array(); // (path => TempFSFile)
667
668 foreach ( $params['srcs'] as $src ) {
669 $source = $this->resolveToFSPath( $src );
670 if ( $source === null ) {
671 $tmpFiles[$src] = null; // invalid path
672 } else {
673 // Create a new temporary file with the same extension...
674 $ext = FileBackend::extensionFromPath( $src );
675 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
676 if ( !$tmpFile ) {
677 $tmpFiles[$src] = null;
678 } else {
679 $tmpPath = $tmpFile->getPath();
680 // Copy the source file over the temp file
681 $this->trapWarnings();
682 $ok = copy( $source, $tmpPath );
683 $this->untrapWarnings();
684 if ( !$ok ) {
685 $tmpFiles[$src] = null;
686 } else {
687 $this->chmod( $tmpPath );
688 $tmpFiles[$src] = $tmpFile;
689 }
690 }
691 }
692 }
693
694 return $tmpFiles;
695 }
696
697 protected function directoriesAreVirtual() {
698 return false;
699 }
700
701 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
702 $statuses = array();
703
704 $pipes = array();
705 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
706 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
707 }
708
709 $errs = array();
710 foreach ( $pipes as $index => $pipe ) {
711 // Result will be empty on success in *NIX. On Windows,
712 // it may be something like " 1 file(s) [copied|moved].".
713 $errs[$index] = stream_get_contents( $pipe );
714 fclose( $pipe );
715 }
716
717 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
718 $status = Status::newGood();
719 $function = 'getResponse' . $fileOpHandle->call;
720 $this->$function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
721 $statuses[$index] = $status;
722 if ( $status->isOK() && $fileOpHandle->chmodPath ) {
723 $this->chmod( $fileOpHandle->chmodPath );
724 }
725 }
726
727 clearstatcache(); // files changed
728 return $statuses;
729 }
730
731 /**
732 * Chmod a file, suppressing the warnings
733 *
734 * @param string $path Absolute file system path
735 * @return bool Success
736 */
737 protected function chmod( $path ) {
738 $this->trapWarnings();
739 $ok = chmod( $path, $this->fileMode );
740 $this->untrapWarnings();
741
742 return $ok;
743 }
744
745 /**
746 * Return the text of an index.html file to hide directory listings
747 *
748 * @return string
749 */
750 protected function indexHtmlPrivate() {
751 return '';
752 }
753
754 /**
755 * Return the text of a .htaccess file to make a directory private
756 *
757 * @return string
758 */
759 protected function htaccessPrivate() {
760 return "Deny from all\n";
761 }
762
763 /**
764 * Clean up directory separators for the given OS
765 *
766 * @param string $path FS path
767 * @return string
768 */
769 protected function cleanPathSlashes( $path ) {
770 return wfIsWindows() ? strtr( $path, '/', '\\' ) : $path;
771 }
772
773 /**
774 * Listen for E_WARNING errors and track whether any happen
775 */
776 protected function trapWarnings() {
777 $this->hadWarningErrors[] = false; // push to stack
778 set_error_handler( array( $this, 'handleWarning' ), E_WARNING );
779 }
780
781 /**
782 * Stop listening for E_WARNING errors and return true if any happened
783 *
784 * @return bool
785 */
786 protected function untrapWarnings() {
787 restore_error_handler(); // restore previous handler
788 return array_pop( $this->hadWarningErrors ); // pop from stack
789 }
790
791 /**
792 * @param int $errno
793 * @param string $errstr
794 * @return bool
795 * @access private
796 */
797 public function handleWarning( $errno, $errstr ) {
798 wfDebugLog( 'FSFileBackend', $errstr ); // more detailed error logging
799 $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
800
801 return true; // suppress from PHP handler
802 }
803 }
804
805 /**
806 * @see FileBackendStoreOpHandle
807 */
808 class FSFileOpHandle extends FileBackendStoreOpHandle {
809 public $cmd; // string; shell command
810 public $chmodPath; // string; file to chmod
811
812 /**
813 * @param FSFileBackend $backend
814 * @param array $params
815 * @param string $call
816 * @param string $cmd
817 * @param int|null $chmodPath
818 */
819 public function __construct(
820 FSFileBackend $backend, array $params, $call, $cmd, $chmodPath = null
821 ) {
822 $this->backend = $backend;
823 $this->params = $params;
824 $this->call = $call;
825 $this->cmd = $cmd;
826 $this->chmodPath = $chmodPath;
827 }
828 }
829
830 /**
831 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
832 * catches exception or does any custom behavoir that we may want.
833 * Do not use this class from places outside FSFileBackend.
834 *
835 * @ingroup FileBackend
836 */
837 abstract class FSFileBackendList implements Iterator {
838 /** @var Iterator */
839 protected $iter;
840
841 /** @var int */
842 protected $suffixStart;
843
844 /** @var int */
845 protected $pos = 0;
846
847 /** @var array */
848 protected $params = array();
849
850 /**
851 * @param string $dir file system directory
852 * @param array $params
853 */
854 public function __construct( $dir, array $params ) {
855 $path = realpath( $dir ); // normalize
856 if ( $path === false ) {
857 $path = $dir;
858 }
859 $this->suffixStart = strlen( $path ) + 1; // size of "path/to/dir/"
860 $this->params = $params;
861
862 try {
863 $this->iter = $this->initIterator( $path );
864 } catch ( UnexpectedValueException $e ) {
865 $this->iter = null; // bad permissions? deleted?
866 }
867 }
868
869 /**
870 * Return an appropriate iterator object to wrap
871 *
872 * @param string $dir file system directory
873 * @return Iterator
874 */
875 protected function initIterator( $dir ) {
876 if ( !empty( $this->params['topOnly'] ) ) { // non-recursive
877 # Get an iterator that will get direct sub-nodes
878 return new DirectoryIterator( $dir );
879 } else { // recursive
880 # Get an iterator that will return leaf nodes (non-directories)
881 # RecursiveDirectoryIterator extends FilesystemIterator.
882 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
883 $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
884
885 return new RecursiveIteratorIterator(
886 new RecursiveDirectoryIterator( $dir, $flags ),
887 RecursiveIteratorIterator::CHILD_FIRST // include dirs
888 );
889 }
890 }
891
892 /**
893 * @see Iterator::key()
894 * @return int
895 */
896 public function key() {
897 return $this->pos;
898 }
899
900 /**
901 * @see Iterator::current()
902 * @return string|bool String or false
903 */
904 public function current() {
905 return $this->getRelPath( $this->iter->current()->getPathname() );
906 }
907
908 /**
909 * @see Iterator::next()
910 * @throws FileBackendError
911 */
912 public function next() {
913 try {
914 $this->iter->next();
915 $this->filterViaNext();
916 } catch ( UnexpectedValueException $e ) { // bad permissions? deleted?
917 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
918 }
919 ++$this->pos;
920 }
921
922 /**
923 * @see Iterator::rewind()
924 * @throws FileBackendError
925 */
926 public function rewind() {
927 $this->pos = 0;
928 try {
929 $this->iter->rewind();
930 $this->filterViaNext();
931 } catch ( UnexpectedValueException $e ) { // bad permissions? deleted?
932 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
933 }
934 }
935
936 /**
937 * @see Iterator::valid()
938 * @return bool
939 */
940 public function valid() {
941 return $this->iter && $this->iter->valid();
942 }
943
944 /**
945 * Filter out items by advancing to the next ones
946 */
947 protected function filterViaNext() {
948 }
949
950 /**
951 * Return only the relative path and normalize slashes to FileBackend-style.
952 * Uses the "real path" since the suffix is based upon that.
953 *
954 * @param string $dir
955 * @return string
956 */
957 protected function getRelPath( $dir ) {
958 $path = realpath( $dir );
959 if ( $path === false ) {
960 $path = $dir;
961 }
962
963 return strtr( substr( $path, $this->suffixStart ), '\\', '/' );
964 }
965 }
966
967 class FSFileBackendDirList extends FSFileBackendList {
968 protected function filterViaNext() {
969 while ( $this->iter->valid() ) {
970 if ( $this->iter->current()->isDot() || !$this->iter->current()->isDir() ) {
971 $this->iter->next(); // skip non-directories and dot files
972 } else {
973 break;
974 }
975 }
976 }
977 }
978
979 class FSFileBackendFileList extends FSFileBackendList {
980 protected function filterViaNext() {
981 while ( $this->iter->valid() ) {
982 if ( !$this->iter->current()->isFile() ) {
983 $this->iter->next(); // skip non-files and dot files
984 } else {
985 break;
986 }
987 }
988 }
989 }