3 * Base class for all backends using particular storage medium.
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.
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.
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
21 * @ingroup FileBackend
22 * @author Aaron Schulz
26 * @brief Base class for all backends using particular storage medium.
28 * This class defines the methods as abstract that subclasses must implement.
29 * Outside callers should *not* use functions with "Internal" in the name.
31 * The FileBackend operations are implemented using basic functions
32 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
33 * This class is also responsible for path resolution and sanitization.
35 * @ingroup FileBackend
38 abstract class FileBackendStore
extends FileBackend
{
42 /** @var Array Map of paths to small (RAM/disk) cache items */
43 protected $cache = array(); // (storage path => key => value)
44 protected $maxCacheSize = 300; // integer; max paths with entries
45 /** @var Array Map of paths to large (RAM/disk) cache items */
46 protected $expensiveCache = array(); // (storage path => key => value)
47 protected $maxExpensiveCacheSize = 5; // integer; max paths with entries
49 /** @var Array Map of container names to sharding settings */
50 protected $shardViaHashLevels = array(); // (container name => config array)
52 protected $maxFileSize = 4294967296; // integer bytes (4GiB)
55 * @see FileBackend::__construct()
57 * @param $config Array
59 public function __construct( array $config ) {
60 parent
::__construct( $config );
61 $this->memCache
= new EmptyBagOStuff(); // disabled by default
65 * Get the maximum allowable file size given backend
66 * medium restrictions and basic performance constraints.
67 * Do not call this function from places outside FileBackend and FileOp.
69 * @return integer Bytes
71 final public function maxFileSizeInternal() {
72 return $this->maxFileSize
;
76 * Check if a file can be created at a given storage path.
77 * FS backends should check if the parent directory exists and the file is writable.
78 * Backends using key/value stores should check if the container exists.
80 * @param $storagePath string
83 abstract public function isPathUsableInternal( $storagePath );
86 * Create a file in the backend with the given contents.
87 * Do not call this function from places outside FileBackend and FileOp.
90 * content : the raw file contents
91 * dst : destination storage path
92 * overwrite : overwrite any file that exists at the destination
94 * @param $params Array
97 final public function createInternal( array $params ) {
98 wfProfileIn( __METHOD__
);
99 wfProfileIn( __METHOD__
. '-' . $this->name
);
100 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
101 $status = Status
::newFatal( 'backend-fail-maxsize',
102 $params['dst'], $this->maxFileSizeInternal() );
104 $status = $this->doCreateInternal( $params );
105 $this->clearCache( array( $params['dst'] ) );
106 $this->deleteFileCache( $params['dst'] ); // persistent cache
108 wfProfileOut( __METHOD__
. '-' . $this->name
);
109 wfProfileOut( __METHOD__
);
114 * @see FileBackendStore::createInternal()
116 abstract protected function doCreateInternal( array $params );
119 * Store a file into the backend from a file on disk.
120 * Do not call this function from places outside FileBackend and FileOp.
123 * src : source path on disk
124 * dst : destination storage path
125 * overwrite : overwrite any file that exists at the destination
127 * @param $params Array
130 final public function storeInternal( array $params ) {
131 wfProfileIn( __METHOD__
);
132 wfProfileIn( __METHOD__
. '-' . $this->name
);
133 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
134 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
136 $status = $this->doStoreInternal( $params );
137 $this->clearCache( array( $params['dst'] ) );
138 $this->deleteFileCache( $params['dst'] ); // persistent cache
140 wfProfileOut( __METHOD__
. '-' . $this->name
);
141 wfProfileOut( __METHOD__
);
146 * @see FileBackendStore::storeInternal()
148 abstract protected function doStoreInternal( array $params );
151 * Copy a file from one storage path to another in the backend.
152 * Do not call this function from places outside FileBackend and FileOp.
155 * src : source storage path
156 * dst : destination storage path
157 * overwrite : overwrite any file that exists at the destination
159 * @param $params Array
162 final public function copyInternal( array $params ) {
163 wfProfileIn( __METHOD__
);
164 wfProfileIn( __METHOD__
. '-' . $this->name
);
165 $status = $this->doCopyInternal( $params );
166 $this->clearCache( array( $params['dst'] ) );
167 $this->deleteFileCache( $params['dst'] ); // persistent cache
168 wfProfileOut( __METHOD__
. '-' . $this->name
);
169 wfProfileOut( __METHOD__
);
174 * @see FileBackendStore::copyInternal()
176 abstract protected function doCopyInternal( array $params );
179 * Delete a file at the storage path.
180 * Do not call this function from places outside FileBackend and FileOp.
183 * src : source storage path
184 * ignoreMissingSource : do nothing if the source file does not exist
186 * @param $params Array
189 final public function deleteInternal( array $params ) {
190 wfProfileIn( __METHOD__
);
191 wfProfileIn( __METHOD__
. '-' . $this->name
);
192 $status = $this->doDeleteInternal( $params );
193 $this->clearCache( array( $params['src'] ) );
194 $this->deleteFileCache( $params['src'] ); // persistent cache
195 wfProfileOut( __METHOD__
. '-' . $this->name
);
196 wfProfileOut( __METHOD__
);
201 * @see FileBackendStore::deleteInternal()
203 abstract protected function doDeleteInternal( array $params );
206 * Move a file from one storage path to another in the backend.
207 * Do not call this function from places outside FileBackend and FileOp.
210 * src : source storage path
211 * dst : destination storage path
212 * overwrite : overwrite any file that exists at the destination
214 * @param $params Array
217 final public function moveInternal( array $params ) {
218 wfProfileIn( __METHOD__
);
219 wfProfileIn( __METHOD__
. '-' . $this->name
);
220 $status = $this->doMoveInternal( $params );
221 $this->clearCache( array( $params['src'], $params['dst'] ) );
222 $this->deleteFileCache( $params['src'] ); // persistent cache
223 $this->deleteFileCache( $params['dst'] ); // persistent cache
224 wfProfileOut( __METHOD__
. '-' . $this->name
);
225 wfProfileOut( __METHOD__
);
230 * @see FileBackendStore::moveInternal()
233 protected function doMoveInternal( array $params ) {
234 // Copy source to dest
235 $status = $this->copyInternal( $params );
236 if ( $status->isOK() ) {
237 // Delete source (only fails due to races or medium going down)
238 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
239 $status->setResult( true, $status->value
); // ignore delete() errors
245 * @see FileBackend::concatenate()
248 final public function concatenate( array $params ) {
249 wfProfileIn( __METHOD__
);
250 wfProfileIn( __METHOD__
. '-' . $this->name
);
251 $status = Status
::newGood();
253 // Try to lock the source files for the scope of this function
254 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
255 if ( $status->isOK() ) {
256 // Actually do the concatenation
257 $status->merge( $this->doConcatenate( $params ) );
260 wfProfileOut( __METHOD__
. '-' . $this->name
);
261 wfProfileOut( __METHOD__
);
266 * @see FileBackendStore::concatenate()
269 protected function doConcatenate( array $params ) {
270 $status = Status
::newGood();
271 $tmpPath = $params['dst']; // convenience
273 // Check that the specified temp file is valid...
274 wfSuppressWarnings();
275 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
277 if ( !$ok ) { // not present or not empty
278 $status->fatal( 'backend-fail-opentemp', $tmpPath );
282 // Build up the temp file using the source chunks (in order)...
283 $tmpHandle = fopen( $tmpPath, 'ab' );
284 if ( $tmpHandle === false ) {
285 $status->fatal( 'backend-fail-opentemp', $tmpPath );
288 foreach ( $params['srcs'] as $virtualSource ) {
289 // Get a local FS version of the chunk
290 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
292 $status->fatal( 'backend-fail-read', $virtualSource );
295 // Get a handle to the local FS version
296 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
297 if ( $sourceHandle === false ) {
298 fclose( $tmpHandle );
299 $status->fatal( 'backend-fail-read', $virtualSource );
302 // Append chunk to file (pass chunk size to avoid magic quotes)
303 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
304 fclose( $sourceHandle );
305 fclose( $tmpHandle );
306 $status->fatal( 'backend-fail-writetemp', $tmpPath );
309 fclose( $sourceHandle );
311 if ( !fclose( $tmpHandle ) ) {
312 $status->fatal( 'backend-fail-closetemp', $tmpPath );
316 clearstatcache(); // temp file changed
322 * @see FileBackend::doPrepare()
325 final protected function doPrepare( array $params ) {
326 wfProfileIn( __METHOD__
);
327 wfProfileIn( __METHOD__
. '-' . $this->name
);
329 $status = Status
::newGood();
330 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
331 if ( $dir === null ) {
332 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
333 wfProfileOut( __METHOD__
. '-' . $this->name
);
334 wfProfileOut( __METHOD__
);
335 return $status; // invalid storage path
338 if ( $shard !== null ) { // confined to a single container/shard
339 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
340 } else { // directory is on several shards
341 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
342 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
343 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
344 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
348 wfProfileOut( __METHOD__
. '-' . $this->name
);
349 wfProfileOut( __METHOD__
);
354 * @see FileBackendStore::doPrepare()
357 protected function doPrepareInternal( $container, $dir, array $params ) {
358 return Status
::newGood();
362 * @see FileBackend::doSecure()
365 final protected function doSecure( array $params ) {
366 wfProfileIn( __METHOD__
);
367 wfProfileIn( __METHOD__
. '-' . $this->name
);
368 $status = Status
::newGood();
370 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
371 if ( $dir === null ) {
372 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
373 wfProfileOut( __METHOD__
. '-' . $this->name
);
374 wfProfileOut( __METHOD__
);
375 return $status; // invalid storage path
378 if ( $shard !== null ) { // confined to a single container/shard
379 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
380 } else { // directory is on several shards
381 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
382 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
383 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
384 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
388 wfProfileOut( __METHOD__
. '-' . $this->name
);
389 wfProfileOut( __METHOD__
);
394 * @see FileBackendStore::doSecure()
397 protected function doSecureInternal( $container, $dir, array $params ) {
398 return Status
::newGood();
402 * @see FileBackend::doClean()
405 final protected function doClean( array $params ) {
406 wfProfileIn( __METHOD__
);
407 wfProfileIn( __METHOD__
. '-' . $this->name
);
408 $status = Status
::newGood();
410 // Recursive: first delete all empty subdirs recursively
411 if ( !empty( $params['recursive'] ) && !$this->directoriesAreVirtual() ) {
412 $subDirsRel = $this->getTopDirectoryList( array( 'dir' => $params['dir'] ) );
413 if ( $subDirsRel !== null ) { // no errors
414 foreach ( $subDirsRel as $subDirRel ) {
415 $subDir = $params['dir'] . "/{$subDirRel}"; // full path
416 $status->merge( $this->doClean( array( 'dir' => $subDir ) +
$params ) );
421 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
422 if ( $dir === null ) {
423 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
424 wfProfileOut( __METHOD__
. '-' . $this->name
);
425 wfProfileOut( __METHOD__
);
426 return $status; // invalid storage path
429 // Attempt to lock this directory...
430 $filesLockEx = array( $params['dir'] );
431 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
432 if ( !$status->isOK() ) {
433 wfProfileOut( __METHOD__
. '-' . $this->name
);
434 wfProfileOut( __METHOD__
);
435 return $status; // abort
438 if ( $shard !== null ) { // confined to a single container/shard
439 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
440 $this->deleteContainerCache( $fullCont ); // purge cache
441 } else { // directory is on several shards
442 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
443 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
444 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
445 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
446 $this->deleteContainerCache( "{$fullCont}{$suffix}" ); // purge cache
450 wfProfileOut( __METHOD__
. '-' . $this->name
);
451 wfProfileOut( __METHOD__
);
456 * @see FileBackendStore::doClean()
459 protected function doCleanInternal( $container, $dir, array $params ) {
460 return Status
::newGood();
464 * @see FileBackend::fileExists()
467 final public function fileExists( array $params ) {
468 wfProfileIn( __METHOD__
);
469 wfProfileIn( __METHOD__
. '-' . $this->name
);
470 $stat = $this->getFileStat( $params );
471 wfProfileOut( __METHOD__
. '-' . $this->name
);
472 wfProfileOut( __METHOD__
);
473 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
477 * @see FileBackend::getFileTimestamp()
480 final public function getFileTimestamp( array $params ) {
481 wfProfileIn( __METHOD__
);
482 wfProfileIn( __METHOD__
. '-' . $this->name
);
483 $stat = $this->getFileStat( $params );
484 wfProfileOut( __METHOD__
. '-' . $this->name
);
485 wfProfileOut( __METHOD__
);
486 return $stat ?
$stat['mtime'] : false;
490 * @see FileBackend::getFileSize()
493 final public function getFileSize( array $params ) {
494 wfProfileIn( __METHOD__
);
495 wfProfileIn( __METHOD__
. '-' . $this->name
);
496 $stat = $this->getFileStat( $params );
497 wfProfileOut( __METHOD__
. '-' . $this->name
);
498 wfProfileOut( __METHOD__
);
499 return $stat ?
$stat['size'] : false;
503 * @see FileBackend::getFileStat()
506 final public function getFileStat( array $params ) {
507 wfProfileIn( __METHOD__
);
508 wfProfileIn( __METHOD__
. '-' . $this->name
);
509 $path = self
::normalizeStoragePath( $params['src'] );
510 if ( $path === null ) {
511 wfProfileOut( __METHOD__
. '-' . $this->name
);
512 wfProfileOut( __METHOD__
);
513 return false; // invalid storage path
515 $latest = !empty( $params['latest'] ); // use latest data?
516 if ( !isset( $this->cache
[$path]['stat'] ) ) {
517 $this->primeFileCache( array( $path ) ); // check persistent cache
519 if ( isset( $this->cache
[$path]['stat'] ) ) {
520 // If we want the latest data, check that this cached
521 // value was in fact fetched with the latest available data.
522 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
523 $this->pingCache( $path ); // LRU
524 wfProfileOut( __METHOD__
. '-' . $this->name
);
525 wfProfileOut( __METHOD__
);
526 return $this->cache
[$path]['stat'];
529 wfProfileIn( __METHOD__
. '-miss' );
530 wfProfileIn( __METHOD__
. '-miss-' . $this->name
);
531 $stat = $this->doGetFileStat( $params );
532 wfProfileOut( __METHOD__
. '-miss-' . $this->name
);
533 wfProfileOut( __METHOD__
. '-miss' );
534 if ( is_array( $stat ) ) { // don't cache negatives
535 $stat['latest'] = $latest;
536 $this->trimCache(); // limit memory
537 $this->cache
[$path]['stat'] = $stat;
538 $this->setFileCache( $path, $stat ); // update persistent cache
540 wfProfileOut( __METHOD__
. '-' . $this->name
);
541 wfProfileOut( __METHOD__
);
546 * @see FileBackendStore::getFileStat()
548 abstract protected function doGetFileStat( array $params );
551 * @see FileBackend::getFileContents()
552 * @return bool|string
554 public function getFileContents( array $params ) {
555 wfProfileIn( __METHOD__
);
556 wfProfileIn( __METHOD__
. '-' . $this->name
);
557 $tmpFile = $this->getLocalReference( $params );
559 wfProfileOut( __METHOD__
. '-' . $this->name
);
560 wfProfileOut( __METHOD__
);
563 wfSuppressWarnings();
564 $data = file_get_contents( $tmpFile->getPath() );
566 wfProfileOut( __METHOD__
. '-' . $this->name
);
567 wfProfileOut( __METHOD__
);
572 * @see FileBackend::getFileSha1Base36()
573 * @return bool|string
575 final public function getFileSha1Base36( array $params ) {
576 wfProfileIn( __METHOD__
);
577 wfProfileIn( __METHOD__
. '-' . $this->name
);
578 $path = $params['src'];
579 if ( isset( $this->cache
[$path]['sha1'] ) ) {
580 $this->pingCache( $path ); // LRU
581 wfProfileOut( __METHOD__
. '-' . $this->name
);
582 wfProfileOut( __METHOD__
);
583 return $this->cache
[$path]['sha1'];
585 wfProfileIn( __METHOD__
. '-miss' );
586 wfProfileIn( __METHOD__
. '-miss-' . $this->name
);
587 $hash = $this->doGetFileSha1Base36( $params );
588 wfProfileOut( __METHOD__
. '-miss-' . $this->name
);
589 wfProfileOut( __METHOD__
. '-miss' );
590 if ( $hash ) { // don't cache negatives
591 $this->trimCache(); // limit memory
592 $this->cache
[$path]['sha1'] = $hash;
594 wfProfileOut( __METHOD__
. '-' . $this->name
);
595 wfProfileOut( __METHOD__
);
600 * @see FileBackendStore::getFileSha1Base36()
603 protected function doGetFileSha1Base36( array $params ) {
604 $fsFile = $this->getLocalReference( $params );
608 return $fsFile->getSha1Base36();
613 * @see FileBackend::getFileProps()
616 final public function getFileProps( array $params ) {
617 wfProfileIn( __METHOD__
);
618 wfProfileIn( __METHOD__
. '-' . $this->name
);
619 $fsFile = $this->getLocalReference( $params );
620 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
621 wfProfileOut( __METHOD__
. '-' . $this->name
);
622 wfProfileOut( __METHOD__
);
627 * @see FileBackend::getLocalReference()
628 * @return TempFSFile|null
630 public function getLocalReference( array $params ) {
631 wfProfileIn( __METHOD__
);
632 wfProfileIn( __METHOD__
. '-' . $this->name
);
633 $path = $params['src'];
634 if ( isset( $this->expensiveCache
[$path]['localRef'] ) ) {
635 $this->pingExpensiveCache( $path );
636 wfProfileOut( __METHOD__
. '-' . $this->name
);
637 wfProfileOut( __METHOD__
);
638 return $this->expensiveCache
[$path]['localRef'];
640 $tmpFile = $this->getLocalCopy( $params );
641 if ( $tmpFile ) { // don't cache negatives
642 $this->trimExpensiveCache(); // limit memory
643 $this->expensiveCache
[$path]['localRef'] = $tmpFile;
645 wfProfileOut( __METHOD__
. '-' . $this->name
);
646 wfProfileOut( __METHOD__
);
651 * @see FileBackend::streamFile()
654 final public function streamFile( array $params ) {
655 wfProfileIn( __METHOD__
);
656 wfProfileIn( __METHOD__
. '-' . $this->name
);
657 $status = Status
::newGood();
659 $info = $this->getFileStat( $params );
660 if ( !$info ) { // let StreamFile handle the 404
661 $status->fatal( 'backend-fail-notexists', $params['src'] );
664 // Set output buffer and HTTP headers for stream
665 $extraHeaders = isset( $params['headers'] ) ?
$params['headers'] : array();
666 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
667 if ( $res == StreamFile
::NOT_MODIFIED
) {
668 // do nothing; client cache is up to date
669 } elseif ( $res == StreamFile
::READY_STREAM
) {
670 wfProfileIn( __METHOD__
. '-send' );
671 wfProfileIn( __METHOD__
. '-send-' . $this->name
);
672 $status = $this->doStreamFile( $params );
673 wfProfileOut( __METHOD__
. '-send-' . $this->name
);
674 wfProfileOut( __METHOD__
. '-send' );
676 $status->fatal( 'backend-fail-stream', $params['src'] );
679 wfProfileOut( __METHOD__
. '-' . $this->name
);
680 wfProfileOut( __METHOD__
);
685 * @see FileBackendStore::streamFile()
688 protected function doStreamFile( array $params ) {
689 $status = Status
::newGood();
691 $fsFile = $this->getLocalReference( $params );
693 $status->fatal( 'backend-fail-stream', $params['src'] );
694 } elseif ( !readfile( $fsFile->getPath() ) ) {
695 $status->fatal( 'backend-fail-stream', $params['src'] );
702 * @see FileBackend::directoryExists()
705 final public function directoryExists( array $params ) {
706 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
707 if ( $dir === null ) {
708 return false; // invalid storage path
710 if ( $shard !== null ) { // confined to a single container/shard
711 return $this->doDirectoryExists( $fullCont, $dir, $params );
712 } else { // directory is on several shards
713 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
714 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
715 $res = false; // response
716 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
717 $exists = $this->doDirectoryExists( "{$fullCont}{$suffix}", $dir, $params );
721 } elseif ( $exists === null ) { // error?
722 $res = null; // if we don't find anything, it is indeterminate
730 * @see FileBackendStore::directoryExists()
732 * @param $container string Resolved container name
733 * @param $dir string Resolved path relative to container
734 * @param $params Array
737 abstract protected function doDirectoryExists( $container, $dir, array $params );
740 * @see FileBackend::getDirectoryList()
741 * @return Traversable|Array|null Returns null on failure
743 final public function getDirectoryList( array $params ) {
744 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
745 if ( $dir === null ) { // invalid storage path
748 if ( $shard !== null ) {
749 // File listing is confined to a single container/shard
750 return $this->getDirectoryListInternal( $fullCont, $dir, $params );
752 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
753 // File listing spans multiple containers/shards
754 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
755 return new FileBackendStoreShardDirIterator( $this,
756 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
761 * Do not call this function from places outside FileBackend
763 * @see FileBackendStore::getDirectoryList()
765 * @param $container string Resolved container name
766 * @param $dir string Resolved path relative to container
767 * @param $params Array
768 * @return Traversable|Array|null Returns null on failure
770 abstract public function getDirectoryListInternal( $container, $dir, array $params );
773 * @see FileBackend::getFileList()
774 * @return Traversable|Array|null Returns null on failure
776 final public function getFileList( array $params ) {
777 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
778 if ( $dir === null ) { // invalid storage path
781 if ( $shard !== null ) {
782 // File listing is confined to a single container/shard
783 return $this->getFileListInternal( $fullCont, $dir, $params );
785 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
786 // File listing spans multiple containers/shards
787 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
788 return new FileBackendStoreShardFileIterator( $this,
789 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
794 * Do not call this function from places outside FileBackend
796 * @see FileBackendStore::getFileList()
798 * @param $container string Resolved container name
799 * @param $dir string Resolved path relative to container
800 * @param $params Array
801 * @return Traversable|Array|null Returns null on failure
803 abstract public function getFileListInternal( $container, $dir, array $params );
806 * Get the list of supported operations and their corresponding FileOp classes.
810 protected function supportedOperations() {
812 'store' => 'StoreFileOp',
813 'copy' => 'CopyFileOp',
814 'move' => 'MoveFileOp',
815 'delete' => 'DeleteFileOp',
816 'create' => 'CreateFileOp',
817 'null' => 'NullFileOp'
822 * Return a list of FileOp objects from a list of operations.
823 * Do not call this function from places outside FileBackend.
825 * The result must have the same number of items as the input.
826 * An exception is thrown if an unsupported operation is requested.
828 * @param $ops Array Same format as doOperations()
829 * @return Array List of FileOp objects
830 * @throws MWException
832 final public function getOperationsInternal( array $ops ) {
833 $supportedOps = $this->supportedOperations();
835 $performOps = array(); // array of FileOp objects
836 // Build up ordered array of FileOps...
837 foreach ( $ops as $operation ) {
838 $opName = $operation['op'];
839 if ( isset( $supportedOps[$opName] ) ) {
840 $class = $supportedOps[$opName];
841 // Get params for this operation
842 $params = $operation;
843 // Append the FileOp class
844 $performOps[] = new $class( $this, $params );
846 throw new MWException( "Operation '$opName' is not supported." );
854 * Get a list of storage paths to lock for a list of operations
855 * Returns an array with 'sh' (shared) and 'ex' (exclusive) keys,
856 * each corresponding to a list of storage paths to be locked.
858 * @param $performOps Array List of FileOp objects
859 * @return Array ('sh' => list of paths, 'ex' => list of paths)
861 final public function getPathsToLockForOpsInternal( array $performOps ) {
862 // Build up a list of files to lock...
863 $paths = array( 'sh' => array(), 'ex' => array() );
864 foreach ( $performOps as $fileOp ) {
865 $paths['sh'] = array_merge( $paths['sh'], $fileOp->storagePathsRead() );
866 $paths['ex'] = array_merge( $paths['ex'], $fileOp->storagePathsChanged() );
868 // Optimization: if doing an EX lock anyway, don't also set an SH one
869 $paths['sh'] = array_diff( $paths['sh'], $paths['ex'] );
870 // Get a shared lock on the parent directory of each path changed
871 $paths['sh'] = array_merge( $paths['sh'], array_map( 'dirname', $paths['ex'] ) );
877 * @see FileBackend::doOperationsInternal()
880 protected function doOperationsInternal( array $ops, array $opts ) {
881 wfProfileIn( __METHOD__
);
882 wfProfileIn( __METHOD__
. '-' . $this->name
);
883 $status = Status
::newGood();
885 // Build up a list of FileOps...
886 $performOps = $this->getOperationsInternal( $ops );
888 // Acquire any locks as needed...
889 if ( empty( $opts['nonLocking'] ) ) {
890 // Build up a list of files to lock...
891 $paths = $this->getPathsToLockForOpsInternal( $performOps );
892 // Try to lock those files for the scope of this function...
893 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager
::LOCK_UW
, $status );
894 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager
::LOCK_EX
, $status );
895 if ( !$status->isOK() ) {
896 wfProfileOut( __METHOD__
. '-' . $this->name
);
897 wfProfileOut( __METHOD__
);
898 return $status; // abort
902 // Clear any file cache entries (after locks acquired)
905 // Load from the persistent file and container caches
906 $this->primeFileCache( $performOps );
907 $this->primeContainerCache( $performOps );
909 // Actually attempt the operation batch...
910 $subStatus = FileOp
::attemptBatch( $performOps, $opts, $this->fileJournal
);
912 // Merge errors into status fields
913 $status->merge( $subStatus );
914 $status->success
= $subStatus->success
; // not done in merge()
916 wfProfileOut( __METHOD__
. '-' . $this->name
);
917 wfProfileOut( __METHOD__
);
922 * @see FileBackend::clearCache()
924 final public function clearCache( array $paths = null ) {
925 if ( is_array( $paths ) ) {
926 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
927 $paths = array_filter( $paths, 'strlen' ); // remove nulls
929 if ( $paths === null ) {
930 $this->cache
= array();
931 $this->expensiveCache
= array();
933 foreach ( $paths as $path ) {
934 unset( $this->cache
[$path] );
935 unset( $this->expensiveCache
[$path] );
938 $this->doClearCache( $paths );
942 * Clears any additional stat caches for storage paths
944 * @see FileBackend::clearCache()
946 * @param $paths Array Storage paths (optional)
949 protected function doClearCache( array $paths = null ) {}
952 * Is this a key/value store where directories are just virtual?
953 * Virtual directories exists in so much as files exists that are
954 * prefixed with the directory path followed by a forward slash.
958 abstract protected function directoriesAreVirtual();
961 * Move a cache entry to the top (such as when accessed)
963 * @param $path string Storage path
966 protected function pingCache( $path ) {
967 if ( isset( $this->cache
[$path] ) ) {
968 $tmp = $this->cache
[$path];
969 unset( $this->cache
[$path] );
970 $this->cache
[$path] = $tmp;
975 * Prune the inexpensive cache if it is too big to add an item
979 protected function trimCache() {
980 if ( count( $this->cache
) >= $this->maxCacheSize
) {
981 reset( $this->cache
);
982 unset( $this->cache
[key( $this->cache
)] );
987 * Move a cache entry to the top (such as when accessed)
989 * @param $path string Storage path
992 protected function pingExpensiveCache( $path ) {
993 if ( isset( $this->expensiveCache
[$path] ) ) {
994 $tmp = $this->expensiveCache
[$path];
995 unset( $this->expensiveCache
[$path] );
996 $this->expensiveCache
[$path] = $tmp;
1001 * Prune the expensive cache if it is too big to add an item
1005 protected function trimExpensiveCache() {
1006 if ( count( $this->expensiveCache
) >= $this->maxExpensiveCacheSize
) {
1007 reset( $this->expensiveCache
);
1008 unset( $this->expensiveCache
[key( $this->expensiveCache
)] );
1013 * Check if a container name is valid.
1014 * This checks for for length and illegal characters.
1016 * @param $container string
1019 final protected static function isValidContainerName( $container ) {
1020 // This accounts for Swift and S3 restrictions while leaving room
1021 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1022 // This disallows directory separators or traversal characters.
1023 // Note that matching strings URL encode to the same string;
1024 // in Swift, the length restriction is *after* URL encoding.
1025 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1029 * Splits a storage path into an internal container name,
1030 * an internal relative file name, and a container shard suffix.
1031 * Any shard suffix is already appended to the internal container name.
1032 * This also checks that the storage path is valid and within this backend.
1034 * If the container is sharded but a suffix could not be determined,
1035 * this means that the path can only refer to a directory and can only
1036 * be scanned by looking in all the container shards.
1038 * @param $storagePath string
1039 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1041 final protected function resolveStoragePath( $storagePath ) {
1042 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1043 if ( $backend === $this->name
) { // must be for this backend
1044 $relPath = self
::normalizeContainerPath( $relPath );
1045 if ( $relPath !== null ) {
1046 // Get shard for the normalized path if this container is sharded
1047 $cShard = $this->getContainerShard( $container, $relPath );
1048 // Validate and sanitize the relative path (backend-specific)
1049 $relPath = $this->resolveContainerPath( $container, $relPath );
1050 if ( $relPath !== null ) {
1051 // Prepend any wiki ID prefix to the container name
1052 $container = $this->fullContainerName( $container );
1053 if ( self
::isValidContainerName( $container ) ) {
1054 // Validate and sanitize the container name (backend-specific)
1055 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1056 if ( $container !== null ) {
1057 return array( $container, $relPath, $cShard );
1063 return array( null, null, null );
1067 * Like resolveStoragePath() except null values are returned if
1068 * the container is sharded and the shard could not be determined.
1070 * @see FileBackendStore::resolveStoragePath()
1072 * @param $storagePath string
1073 * @return Array (container, path) or (null, null) if invalid
1075 final protected function resolveStoragePathReal( $storagePath ) {
1076 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1077 if ( $cShard !== null ) {
1078 return array( $container, $relPath );
1080 return array( null, null );
1084 * Get the container name shard suffix for a given path.
1085 * Any empty suffix means the container is not sharded.
1087 * @param $container string Container name
1088 * @param $relStoragePath string Storage path relative to the container
1089 * @return string|null Returns null if shard could not be determined
1091 final protected function getContainerShard( $container, $relPath ) {
1092 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1093 if ( $levels == 1 ||
$levels == 2 ) {
1094 // Hash characters are either base 16 or 36
1095 $char = ( $base == 36 ) ?
'[0-9a-z]' : '[0-9a-f]';
1096 // Get a regex that represents the shard portion of paths.
1097 // The concatenation of the captures gives us the shard.
1098 if ( $levels === 1 ) { // 16 or 36 shards per container
1099 $hashDirRegex = '(' . $char . ')';
1100 } else { // 256 or 1296 shards per container
1101 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1102 $hashDirRegex = $char . '/(' . $char . '{2})';
1103 } else { // short hash dir format (e.g. "a/b/c")
1104 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1107 // Allow certain directories to be above the hash dirs so as
1108 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1109 // They must be 2+ chars to avoid any hash directory ambiguity.
1111 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1112 return '.' . implode( '', array_slice( $m, 1 ) );
1114 return null; // failed to match
1116 return ''; // no sharding
1120 * Check if a storage path maps to a single shard.
1121 * Container dirs like "a", where the container shards on "x/xy",
1122 * can reside on several shards. Such paths are tricky to handle.
1124 * @param $storagePath string Storage path
1127 final public function isSingleShardPathInternal( $storagePath ) {
1128 list( $c, $r, $shard ) = $this->resolveStoragePath( $storagePath );
1129 return ( $shard !== null );
1133 * Get the sharding config for a container.
1134 * If greater than 0, then all file storage paths within
1135 * the container are required to be hashed accordingly.
1137 * @param $container string
1138 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1140 final protected function getContainerHashLevels( $container ) {
1141 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1142 $config = $this->shardViaHashLevels
[$container];
1143 $hashLevels = (int)$config['levels'];
1144 if ( $hashLevels == 1 ||
$hashLevels == 2 ) {
1145 $hashBase = (int)$config['base'];
1146 if ( $hashBase == 16 ||
$hashBase == 36 ) {
1147 return array( $hashLevels, $hashBase, $config['repeat'] );
1151 return array( 0, 0, false ); // no sharding
1155 * Get a list of full container shard suffixes for a container
1157 * @param $container string
1160 final protected function getContainerSuffixes( $container ) {
1162 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1163 if ( $digits > 0 ) {
1164 $numShards = pow( $base, $digits );
1165 for ( $index = 0; $index < $numShards; $index++
) {
1166 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1173 * Get the full container name, including the wiki ID prefix
1175 * @param $container string
1178 final protected function fullContainerName( $container ) {
1179 if ( $this->wikiId
!= '' ) {
1180 return "{$this->wikiId}-$container";
1187 * Resolve a container name, checking if it's allowed by the backend.
1188 * This is intended for internal use, such as encoding illegal chars.
1189 * Subclasses can override this to be more restrictive.
1191 * @param $container string
1192 * @return string|null
1194 protected function resolveContainerName( $container ) {
1199 * Resolve a relative storage path, checking if it's allowed by the backend.
1200 * This is intended for internal use, such as encoding illegal chars or perhaps
1201 * getting absolute paths (e.g. FS based backends). Note that the relative path
1202 * may be the empty string (e.g. the path is simply to the container).
1204 * @param $container string Container name
1205 * @param $relStoragePath string Storage path relative to the container
1206 * @return string|null Path or null if not valid
1208 protected function resolveContainerPath( $container, $relStoragePath ) {
1209 return $relStoragePath;
1213 * Get the cache key for a container
1215 * @param $container Resolved container name
1218 private function containerCacheKey( $container ) {
1219 return wfMemcKey( 'backend', $this->getName(), 'container', $container );
1223 * Set the cached info for a container
1225 * @param $container Resolved container name
1226 * @param $val mixed Information to cache
1229 final protected function setContainerCache( $container, $val ) {
1230 $this->memCache
->set( $this->containerCacheKey( $container ), $val, 14*86400 );
1234 * Delete the cached info for a container
1236 * @param $container Resolved container name
1239 final protected function deleteContainerCache( $container ) {
1240 for ( $attempts=1; $attempts <= 3; $attempts++
) {
1241 if ( $this->memCache
->delete( $this->containerCacheKey( $container ) ) ) {
1245 trigger_error( "Unable to delete stat cache for container $container." );
1249 * Do a batch lookup from cache for container stats for all containers
1250 * used in a list of container names, storage paths, or FileOp objects.
1252 * @param $items Array
1255 final protected function primeContainerCache( array $items ) {
1256 wfProfileIn( __METHOD__
);
1257 wfProfileIn( __METHOD__
. '-' . $this->name
);
1258 $paths = array(); // list of storage paths
1259 $contNames = array(); // (cache key => resolved container name)
1260 // Get all the paths/containers from the items...
1261 foreach ( $items as $item ) {
1262 if ( $item instanceof FileOp
) {
1263 $paths = array_merge( $paths, $item->storagePathsRead() );
1264 $paths = array_merge( $paths, $item->storagePathsChanged() );
1265 } elseif ( self
::isStoragePath( $item ) ) {
1267 } elseif ( is_string( $item ) ) { // full container name
1268 $contNames[$this->containerCacheKey( $item )] = $item;
1271 // Get all the corresponding cache keys for paths...
1272 foreach ( $paths as $path ) {
1273 list( $fullCont, $r, $s ) = $this->resolveStoragePath( $path );
1274 if ( $fullCont !== null ) { // valid path for this backend
1275 $contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
1279 $contInfo = array(); // (resolved container name => cache value)
1280 // Get all cache entries for these container cache keys...
1281 $values = $this->memCache
->getBatch( array_keys( $contNames ) );
1282 foreach ( $values as $cacheKey => $val ) {
1283 $contInfo[$contNames[$cacheKey]] = $val;
1286 // Populate the container process cache for the backend...
1287 $this->doPrimeContainerCache( array_filter( $contInfo, 'is_array' ) );
1288 wfProfileOut( __METHOD__
. '-' . $this->name
);
1289 wfProfileOut( __METHOD__
);
1293 * Fill the backend-specific process cache given an array of
1294 * resolved container names and their corresponding cached info.
1295 * Only containers that actually exist should appear in the map.
1297 * @param $containerInfo Array Map of resolved container names to cached info
1300 protected function doPrimeContainerCache( array $containerInfo ) {}
1303 * Get the cache key for a file path
1305 * @param $path Storage path
1308 private function fileCacheKey( $path ) {
1309 return wfMemcKey( 'backend', $this->getName(), 'file', sha1( $path ) );
1313 * Set the cached stat info for a file path
1315 * @param $path Storage path
1316 * @param $val mixed Information to cache
1319 final protected function setFileCache( $path, $val ) {
1320 $this->memCache
->set( $this->fileCacheKey( $path ), $val, 7*86400 );
1324 * Delete the cached stat info for a file path
1326 * @param $path Storage path
1329 final protected function deleteFileCache( $path ) {
1330 for ( $attempts=1; $attempts <= 3; $attempts++
) {
1331 if ( $this->memCache
->delete( $this->fileCacheKey( $path ) ) ) {
1335 trigger_error( "Unable to delete stat cache for file $path." );
1339 * Do a batch lookup from cache for file stats for all paths
1340 * used in a list of storage paths or FileOp objects.
1342 * @param $items Array List of storage paths or FileOps
1345 final protected function primeFileCache( array $items ) {
1346 wfProfileIn( __METHOD__
);
1347 wfProfileIn( __METHOD__
. '-' . $this->name
);
1348 $paths = array(); // list of storage paths
1349 $pathNames = array(); // (cache key => storage path)
1350 // Get all the paths/containers from the items...
1351 foreach ( $items as $item ) {
1352 if ( $item instanceof FileOp
) {
1353 $paths = array_merge( $paths, $item->storagePathsRead() );
1354 $paths = array_merge( $paths, $item->storagePathsChanged() );
1355 } elseif ( self
::isStoragePath( $item ) ) {
1359 // Get all the corresponding cache keys for paths...
1360 foreach ( $paths as $path ) {
1361 list( $cont, $rel, $s ) = $this->resolveStoragePath( $path );
1362 if ( $rel !== null ) { // valid path for this backend
1363 $pathNames[$this->fileCacheKey( $path )] = $path;
1366 // Get all cache entries for these container cache keys...
1367 $values = $this->memCache
->getBatch( array_keys( $pathNames ) );
1368 foreach ( $values as $cacheKey => $val ) {
1369 if ( is_array( $val ) ) {
1370 $this->trimCache(); // limit memory
1371 $this->cache
[$pathNames[$cacheKey]]['stat'] = $val;
1374 wfProfileOut( __METHOD__
. '-' . $this->name
);
1375 wfProfileOut( __METHOD__
);
1380 * FileBackendStore helper function to handle listings that span container shards.
1381 * Do not use this class from places outside of FileBackendStore.
1383 * @ingroup FileBackend
1385 abstract class FileBackendStoreShardListIterator
implements Iterator
{
1386 /** @var FileBackendStore */
1391 protected $shardSuffixes;
1392 protected $container; // string; full container name
1393 protected $directory; // string; resolved relative path
1395 /** @var Traversable */
1397 protected $curShard = 0; // integer
1398 protected $pos = 0; // integer
1401 protected $multiShardPaths = array(); // (rel path => 1)
1404 * @param $backend FileBackendStore
1405 * @param $container string Full storage container name
1406 * @param $dir string Storage directory relative to container
1407 * @param $suffixes Array List of container shard suffixes
1408 * @param $params Array
1410 public function __construct(
1411 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
1413 $this->backend
= $backend;
1414 $this->container
= $container;
1415 $this->directory
= $dir;
1416 $this->shardSuffixes
= $suffixes;
1417 $this->params
= $params;
1421 * @see Iterator::current()
1422 * @return string|bool String or false
1424 public function current() {
1425 if ( is_array( $this->iter
) ) {
1426 return current( $this->iter
);
1428 return $this->iter
->current();
1433 * @see Iterator::key()
1436 public function key() {
1441 * @see Iterator::next()
1444 public function next() {
1446 if ( is_array( $this->iter
) ) {
1447 next( $this->iter
);
1449 $this->iter
->next();
1451 // Filter out items that we already listed
1452 $this->filterViaNext();
1453 // Find the next non-empty shard if no elements are left
1454 $this->nextShardIteratorIfNotValid();
1458 * @see Iterator::rewind()
1461 public function rewind() {
1463 $this->curShard
= 0;
1464 $this->setIteratorFromCurrentShard();
1465 // Filter out items that we already listed
1466 $this->filterViaNext();
1467 // Find the next non-empty shard if this one has no elements
1468 $this->nextShardIteratorIfNotValid();
1472 * @see Iterator::valid()
1475 public function valid() {
1476 if ( $this->iter
=== null ) {
1477 return false; // some failure?
1478 } elseif ( is_array( $this->iter
) ) {
1479 return ( current( $this->iter
) !== false ); // no paths can have this value
1481 return $this->iter
->valid();
1486 * Filter out duplicate items by advancing to the next ones
1488 protected function filterViaNext() {
1489 while ( $this->iter
->valid() ) {
1490 $rel = $this->iter
->current(); // path relative to given directory
1491 $path = $this->params
['dir'] . "/{$rel}"; // full storage path
1492 if ( !$this->backend
->isSingleShardPathInternal( $path ) ) {
1493 // Don't keep listing paths that are on multiple shards
1494 if ( isset( $this->multiShardPaths
[$rel] ) ) {
1495 $this->iter
->next(); // we already listed this path
1497 $this->multiShardPaths
[$rel] = 1;
1505 * If the list iterator for this container shard is out of items,
1506 * then move on to the next container that has items.
1507 * If there are none, then it advances to the last container.
1509 protected function nextShardIteratorIfNotValid() {
1510 while ( !$this->valid() ) {
1511 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1512 break; // no more container shards
1514 $this->setIteratorFromCurrentShard();
1519 * Set the list iterator to that of the current container shard
1521 protected function setIteratorFromCurrentShard() {
1522 $suffix = $this->shardSuffixes
[$this->curShard
];
1523 $this->iter
= $this->listFromShard(
1524 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1528 * Get the list for a given container shard
1530 * @param $container string Resolved container name
1531 * @param $dir string Resolved path relative to container
1532 * @param $params Array
1533 * @return Traversable|Array|null
1535 abstract protected function listFromShard( $container, $dir, array $params );
1539 * Iterator for listing directories
1541 class FileBackendStoreShardDirIterator
extends FileBackendStoreShardListIterator
{
1542 protected function listFromShard( $container, $dir, array $params ) {
1543 return $this->backend
->getDirectoryListInternal( $container, $dir, $params );
1548 * Iterator for listing regular files
1550 class FileBackendStoreShardFileIterator
extends FileBackendStoreShardListIterator
{
1551 protected function listFromShard( $container, $dir, array $params ) {
1552 return $this->backend
->getFileListInternal( $container, $dir, $params );