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