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