get a null lock manager by default
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Base class for all file backend classes (including multi-write backends).
10 * This class defines the methods as abstract that subclasses must implement.
11 * Outside callers can assume that all backends will have these functions.
12 *
13 * All "storage paths" are of the format "mwstore://backend/container/path".
14 * The paths use UNIX file system (FS) notation, though any particular backend may
15 * not actually be using a local filesystem. Therefore, the paths are only virtual.
16 *
17 * Backend contents are stored under wiki-specific container names by default.
18 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
19 * segregation must be done by setting the container paths appropriately.
20 *
21 * FS-based backends are somewhat more restrictive due to the existence of real
22 * directory files; a regular file cannot have the same name as a directory. Other
23 * backends with virtual directories may not have this limitation. Callers should
24 * store files in such a way that no files and directories are under the same path.
25 *
26 * Methods should avoid throwing exceptions at all costs.
27 * As a corollary, external dependencies should be kept to a minimum.
28 *
29 * @ingroup FileBackend
30 * @since 1.19
31 */
32 abstract class FileBackendBase {
33 protected $name; // unique backend name
34 protected $wikiId; // unique wiki name
35 protected $readOnly; // string
36 /** @var LockManager */
37 protected $lockManager;
38
39 /**
40 * Create a new backend instance from configuration.
41 * This should only be called from within FileBackendGroup.
42 *
43 * $config includes:
44 * 'name' : The unique name of this backend.
45 * 'wikiId' : Prefix to container names that is unique to this wiki.
46 * This should consist of alphanumberic, '-', and '_' chars.
47 * 'lockManager' : Registered name of a file lock manager to use.
48 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
49 * It should be an explanation for the backend being read-only.
50 *
51 * @param $config Array
52 */
53 public function __construct( array $config ) {
54 $defaultConfig = array(
55 'lockManager' => null,
56 );
57 $config = array_merge( $defaultConfig, $config );
58 $this->name = $config['name'];
59 $this->wikiId = isset( $config['wikiId'] )
60 ? $config['wikiId']
61 : wfWikiID(); // e.g. "my_wiki-en_"
62 $this->wikiId = $this->resolveWikiId( $this->wikiId );
63 $this->lockManager = LockManagerGroup::singleton()->get( $config['lockManager'] );
64 $this->readOnly = isset( $config['readOnly'] )
65 ? (string)$config['readOnly']
66 : '';
67 }
68
69 /**
70 * Normalize a wiki ID by replacing characters that are
71 * not supported by the backend as part of container names.
72 *
73 * @param $wikiId string
74 * @return string
75 */
76 protected function resolveWikiId( $wikiId ) {
77 return $wikiId;
78 }
79
80 /**
81 * Get the unique backend name.
82 * We may have multiple different backends of the same type.
83 * For example, we can have two Swift backends using different proxies.
84 *
85 * @return string
86 */
87 final public function getName() {
88 return $this->name;
89 }
90
91 /**
92 * This is the main entry point into the backend for write operations.
93 * Callers supply an ordered list of operations to perform as a transaction.
94 * If any serious errors occur, all attempted operations will be rolled back.
95 *
96 * $ops is an array of arrays. The outer array holds a list of operations.
97 * Each inner array is a set of key value pairs that specify an operation.
98 *
99 * Supported operations and their parameters:
100 * a) Create a new file in storage with the contents of a string
101 * array(
102 * 'op' => 'create',
103 * 'dst' => <storage path>,
104 * 'content' => <string of new file contents>,
105 * 'overwrite' => <boolean>,
106 * 'overwriteSame' => <boolean>
107 * )
108 * b) Copy a file system file into storage
109 * array(
110 * 'op' => 'store',
111 * 'src' => <file system path>,
112 * 'dst' => <storage path>,
113 * 'overwrite' => <boolean>,
114 * 'overwriteSame' => <boolean>
115 * )
116 * c) Copy a file within storage
117 * array(
118 * 'op' => 'copy',
119 * 'src' => <storage path>,
120 * 'dst' => <storage path>,
121 * 'overwrite' => <boolean>,
122 * 'overwriteSame' => <boolean>
123 * )
124 * d) Move a file within storage
125 * array(
126 * 'op' => 'move',
127 * 'src' => <storage path>,
128 * 'dst' => <storage path>,
129 * 'overwrite' => <boolean>,
130 * 'overwriteSame' => <boolean>
131 * )
132 * e) Delete a file within storage
133 * array(
134 * 'op' => 'delete',
135 * 'src' => <storage path>,
136 * 'ignoreMissingSource' => <boolean>
137 * )
138 * f) Do nothing (no-op)
139 * array(
140 * 'op' => 'null',
141 * )
142 *
143 * Boolean flags for operations (operation-specific):
144 * 'ignoreMissingSource' : The operation will simply succeed and do
145 * nothing if the source file does not exist.
146 * 'overwrite' : Any destination file will be overwritten.
147 * 'overwriteSame' : An error will not be given if a file already
148 * exists at the destination that has the same
149 * contents as the new contents to be written there.
150 *
151 * $opts is an associative of boolean flags, including:
152 * 'force' : Errors that would normally cause a rollback do not.
153 * The remaining operations are still attempted if any fail.
154 * 'nonLocking' : No locks are acquired for the operations.
155 * This can increase performance for non-critical writes.
156 * This has no effect unless the 'force' flag is set.
157 * 'allowStale' : Don't require the latest available data.
158 * This can increase performance for non-critical writes.
159 * This has no effect unless the 'force' flag is set.
160 *
161 * Remarks:
162 * File system paths given to operations should refer to files that are
163 * either locked or otherwise safe from modification from other processes.
164 * Normally these files will be new temp files, which should be adequate.
165 *
166 * Return value:
167 * This returns a Status, which contains all warnings and fatals that occured
168 * during the operation. The 'failCount', 'successCount', and 'success' members
169 * will reflect each operation attempted. The status will be "OK" unless any
170 * of the operations failed and the 'force' parameter was not set.
171 *
172 * @param $ops Array List of operations to execute in order
173 * @param $opts Array Batch operation options
174 * @return Status
175 */
176 final public function doOperations( array $ops, array $opts = array() ) {
177 if ( $this->readOnly != '' ) {
178 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
179 }
180 if ( empty( $opts['force'] ) ) { // sanity
181 unset( $opts['nonLocking'] );
182 unset( $opts['allowStale'] );
183 }
184 return $this->doOperationsInternal( $ops, $opts );
185 }
186
187 /**
188 * @see FileBackendBase::doOperations()
189 */
190 abstract protected function doOperationsInternal( array $ops, array $opts );
191
192 /**
193 * Same as doOperations() except it takes a single operation.
194 * If you are doing a batch of operations that should either
195 * all succeed or all fail, then use that function instead.
196 *
197 * @see FileBackendBase::doOperations()
198 *
199 * @param $op Array Operation
200 * @param $opts Array Operation options
201 * @return Status
202 */
203 final public function doOperation( array $op, array $opts = array() ) {
204 return $this->doOperations( array( $op ), $opts );
205 }
206
207 /**
208 * Performs a single create operation.
209 * This sets $params['op'] to 'create' and passes it to doOperation().
210 *
211 * @see FileBackendBase::doOperation()
212 *
213 * @param $params Array Operation parameters
214 * @param $opts Array Operation options
215 * @return Status
216 */
217 final public function create( array $params, array $opts = array() ) {
218 $params['op'] = 'create';
219 return $this->doOperation( $params, $opts );
220 }
221
222 /**
223 * Performs a single store operation.
224 * This sets $params['op'] to 'store' and passes it to doOperation().
225 *
226 * @see FileBackendBase::doOperation()
227 *
228 * @param $params Array Operation parameters
229 * @param $opts Array Operation options
230 * @return Status
231 */
232 final public function store( array $params, array $opts = array() ) {
233 $params['op'] = 'store';
234 return $this->doOperation( $params, $opts );
235 }
236
237 /**
238 * Performs a single copy operation.
239 * This sets $params['op'] to 'copy' and passes it to doOperation().
240 *
241 * @see FileBackendBase::doOperation()
242 *
243 * @param $params Array Operation parameters
244 * @param $opts Array Operation options
245 * @return Status
246 */
247 final public function copy( array $params, array $opts = array() ) {
248 $params['op'] = 'copy';
249 return $this->doOperation( $params, $opts );
250 }
251
252 /**
253 * Performs a single move operation.
254 * This sets $params['op'] to 'move' and passes it to doOperation().
255 *
256 * @see FileBackendBase::doOperation()
257 *
258 * @param $params Array Operation parameters
259 * @param $opts Array Operation options
260 * @return Status
261 */
262 final public function move( array $params, array $opts = array() ) {
263 $params['op'] = 'move';
264 return $this->doOperation( $params, $opts );
265 }
266
267 /**
268 * Performs a single delete operation.
269 * This sets $params['op'] to 'delete' and passes it to doOperation().
270 *
271 * @see FileBackendBase::doOperation()
272 *
273 * @param $params Array Operation parameters
274 * @param $opts Array Operation options
275 * @return Status
276 */
277 final public function delete( array $params, array $opts = array() ) {
278 $params['op'] = 'delete';
279 return $this->doOperation( $params, $opts );
280 }
281
282 /**
283 * Concatenate a list of storage files into a single file on the file system
284 * $params include:
285 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
286 * dst : file system path to 0-byte temp file
287 *
288 * @param $params Array Operation parameters
289 * @return Status
290 */
291 abstract public function concatenate( array $params );
292
293 /**
294 * Prepare a storage directory for usage.
295 * This will create any required containers and parent directories.
296 * Backends using key/value stores only need to create the container.
297 *
298 * $params include:
299 * dir : storage directory
300 *
301 * @param $params Array
302 * @return Status
303 */
304 final public function prepare( array $params ) {
305 if ( $this->readOnly != '' ) {
306 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
307 }
308 return $this->doPrepare( $params );
309 }
310
311 /**
312 * @see FileBackendBase::prepare()
313 */
314 abstract protected function doPrepare( array $params );
315
316 /**
317 * Take measures to block web access to a storage directory and
318 * the container it belongs to. FS backends might add .htaccess
319 * files whereas key/value store backends might restrict container
320 * access to the auth user that represents end-users in web request.
321 * This is not guaranteed to actually do anything.
322 *
323 * $params include:
324 * dir : storage directory
325 * noAccess : try to deny file access
326 * noListing : try to deny file listing
327 *
328 * @param $params Array
329 * @return Status
330 */
331 final public function secure( array $params ) {
332 if ( $this->readOnly != '' ) {
333 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
334 }
335 $status = $this->doPrepare( $params ); // dir must exist to restrict it
336 if ( $status->isOK() ) {
337 $status->merge( $this->doSecure( $params ) );
338 }
339 return $status;
340 }
341
342 /**
343 * @see FileBackendBase::secure()
344 */
345 abstract protected function doSecure( array $params );
346
347 /**
348 * Delete a storage directory if it is empty.
349 * Backends using key/value stores may do nothing unless the directory
350 * is that of an empty container, in which case it should be deleted.
351 *
352 * $params include:
353 * dir : storage directory
354 *
355 * @param $params Array
356 * @return Status
357 */
358 final public function clean( array $params ) {
359 if ( $this->readOnly != '' ) {
360 return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
361 }
362 return $this->doClean( $params );
363 }
364
365 /**
366 * @see FileBackendBase::clean()
367 */
368 abstract protected function doClean( array $params );
369
370 /**
371 * Check if a file exists at a storage path in the backend.
372 * This returns false if only a directory exists at the path.
373 *
374 * $params include:
375 * src : source storage path
376 * latest : use the latest available data
377 *
378 * @param $params Array
379 * @return bool|null Returns null on failure
380 */
381 abstract public function fileExists( array $params );
382
383 /**
384 * Get the last-modified timestamp of the file at a storage path.
385 *
386 * $params include:
387 * src : source storage path
388 * latest : use the latest available data
389 *
390 * @param $params Array
391 * @return string|false TS_MW timestamp or false on failure
392 */
393 abstract public function getFileTimestamp( array $params );
394
395 /**
396 * Get the contents of a file at a storage path in the backend.
397 * This should be avoided for potentially large files.
398 *
399 * $params include:
400 * src : source storage path
401 * latest : use the latest available data
402 *
403 * @param $params Array
404 * @return string|false Returns false on failure
405 */
406 abstract public function getFileContents( array $params );
407
408 /**
409 * Get the size (bytes) of a file at a storage path in the backend.
410 *
411 * $params include:
412 * src : source storage path
413 * latest : use the latest available data
414 *
415 * @param $params Array
416 * @return integer|false Returns false on failure
417 */
418 abstract public function getFileSize( array $params );
419
420 /**
421 * Get quick information about a file at a storage path in the backend.
422 * If the file does not exist, then this returns false.
423 * Otherwise, the result is an associative array that includes:
424 * mtime : the last-modified timestamp (TS_MW)
425 * size : the file size (bytes)
426 * Additional values may be included for internal use only.
427 *
428 * $params include:
429 * src : source storage path
430 * latest : use the latest available data
431 *
432 * @param $params Array
433 * @return Array|false|null Returns null on failure
434 */
435 abstract public function getFileStat( array $params );
436
437 /**
438 * Get a SHA-1 hash of the file at a storage path in the backend.
439 *
440 * $params include:
441 * src : source storage path
442 * latest : use the latest available data
443 *
444 * @param $params Array
445 * @return string|false Hash string or false on failure
446 */
447 abstract public function getFileSha1Base36( array $params );
448
449 /**
450 * Get the properties of the file at a storage path in the backend.
451 * Returns FSFile::placeholderProps() on failure.
452 *
453 * $params include:
454 * src : source storage path
455 * latest : use the latest available data
456 *
457 * @param $params Array
458 * @return Array
459 */
460 abstract public function getFileProps( array $params );
461
462 /**
463 * Stream the file at a storage path in the backend.
464 * If the file does not exists, a 404 error will be given.
465 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
466 * must be sent if streaming began, while none should be sent otherwise.
467 * Implementations should flush the output buffer before sending data.
468 *
469 * $params include:
470 * src : source storage path
471 * headers : additional HTTP headers to send on success
472 * latest : use the latest available data
473 *
474 * @param $params Array
475 * @return Status
476 */
477 abstract public function streamFile( array $params );
478
479 /**
480 * Returns a file system file, identical to the file at a storage path.
481 * The file returned is either:
482 * a) A local copy of the file at a storage path in the backend.
483 * The temporary copy will have the same extension as the source.
484 * b) An original of the file at a storage path in the backend.
485 * Temporary files may be purged when the file object falls out of scope.
486 *
487 * Write operations should *never* be done on this file as some backends
488 * may do internal tracking or may be instances of FileBackendMultiWrite.
489 * In that later case, there are copies of the file that must stay in sync.
490 *
491 * $params include:
492 * src : source storage path
493 * latest : use the latest available data
494 *
495 * @param $params Array
496 * @return FSFile|null Returns null on failure
497 */
498 abstract public function getLocalReference( array $params );
499
500 /**
501 * Get a local copy on disk of the file at a storage path in the backend.
502 * The temporary copy will have the same file extension as the source.
503 * Temporary files may be purged when the file object falls out of scope.
504 *
505 * $params include:
506 * src : source storage path
507 * latest : use the latest available data
508 *
509 * @param $params Array
510 * @return TempFSFile|null Returns null on failure
511 */
512 abstract public function getLocalCopy( array $params );
513
514 /**
515 * Get an iterator to list out all stored files under a storage directory.
516 * If the directory is of the form "mwstore://container", then all items in
517 * the container should be listed. If of the form "mwstore://container/dir",
518 * then all items under that container directory should be listed.
519 * Results should be storage paths relative to the given directory.
520 *
521 * $params include:
522 * dir : storage path directory
523 *
524 * @return Traversable|Array|null Returns null on failure
525 */
526 abstract public function getFileList( array $params );
527
528 /**
529 * Invalidate any in-process file existence and property cache.
530 * If $paths is given, then only the cache for those files will be cleared.
531 *
532 * @param $paths Array Storage paths (optional)
533 * @return void
534 */
535 abstract public function clearCache( array $paths = null );
536
537 /**
538 * Lock the files at the given storage paths in the backend.
539 * This will either lock all the files or none (on failure).
540 *
541 * Callers should consider using getScopedFileLocks() instead.
542 *
543 * @param $paths Array Storage paths
544 * @param $type integer LockManager::LOCK_* constant
545 * @return Status
546 */
547 final public function lockFiles( array $paths, $type ) {
548 return $this->lockManager->lock( $paths, $type );
549 }
550
551 /**
552 * Unlock the files at the given storage paths in the backend.
553 *
554 * @param $paths Array Storage paths
555 * @param $type integer LockManager::LOCK_* constant
556 * @return Status
557 */
558 final public function unlockFiles( array $paths, $type ) {
559 return $this->lockManager->unlock( $paths, $type );
560 }
561
562 /**
563 * Lock the files at the given storage paths in the backend.
564 * This will either lock all the files or none (on failure).
565 * On failure, the status object will be updated with errors.
566 *
567 * Once the return value goes out scope, the locks will be released and
568 * the status updated. Unlock fatals will not change the status "OK" value.
569 *
570 * @param $paths Array Storage paths
571 * @param $type integer LockManager::LOCK_* constant
572 * @param $status Status Status to update on lock/unlock
573 * @return ScopedLock|null Returns null on failure
574 */
575 final public function getScopedFileLocks( array $paths, $type, Status $status ) {
576 return ScopedLock::factory( $this->lockManager, $paths, $type, $status );
577 }
578 }
579
580 /**
581 * Base class for all single-write backends.
582 * This class defines the methods as abstract that subclasses must implement.
583 * Callers outside of FileBackend and its helper classes, such as FileOp,
584 * should only call functions that are present in FileBackendBase.
585 *
586 * The FileBackendBase operations are implemented using primitive functions
587 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
588 * This class is also responsible for path resolution and sanitization.
589 *
590 * @ingroup FileBackend
591 * @since 1.19
592 */
593 abstract class FileBackend extends FileBackendBase {
594 /** @var Array */
595 protected $cache = array(); // (storage path => key => value)
596 protected $maxCacheSize = 75; // integer; max paths with entries
597 /** @var Array */
598 protected $shardViaHashLevels = array(); // (container name => integer)
599
600 protected $maxFileSize = 1000000000; // integer bytes (1GB)
601
602 /**
603 * Get the maximum allowable file size given backend
604 * medium restrictions and basic performance constraints.
605 * Do not call this function from places outside FileBackend and FileOp.
606 *
607 * @return integer Bytes
608 */
609 final public function maxFileSizeInternal() {
610 return $this->maxFileSize;
611 }
612
613 /**
614 * Check if a file can be created at a given storage path.
615 * FS backends should check if the parent directory exists and the file is writable.
616 * Backends using key/value stores should check if the container exists.
617 *
618 * @param $storagePath string
619 * @return bool
620 */
621 abstract public function isPathUsableInternal( $storagePath );
622
623 /**
624 * Create a file in the backend with the given contents.
625 * Do not call this function from places outside FileBackend and FileOp.
626 *
627 * $params include:
628 * content : the raw file contents
629 * dst : destination storage path
630 * overwrite : overwrite any file that exists at the destination
631 *
632 * @param $params Array
633 * @return Status
634 */
635 final public function createInternal( array $params ) {
636 wfProfileIn( __METHOD__ );
637 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
638 $status = Status::newFatal( 'backend-fail-create', $params['dst'] );
639 } else {
640 $status = $this->doCreateInternal( $params );
641 $this->clearCache( array( $params['dst'] ) );
642 }
643 wfProfileOut( __METHOD__ );
644 return $status;
645 }
646
647 /**
648 * @see FileBackend::createInternal()
649 */
650 abstract protected function doCreateInternal( array $params );
651
652 /**
653 * Store a file into the backend from a file on disk.
654 * Do not call this function from places outside FileBackend and FileOp.
655 *
656 * $params include:
657 * src : source path on disk
658 * dst : destination storage path
659 * overwrite : overwrite any file that exists at the destination
660 *
661 * @param $params Array
662 * @return Status
663 */
664 final public function storeInternal( array $params ) {
665 wfProfileIn( __METHOD__ );
666 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
667 $status = Status::newFatal( 'backend-fail-store', $params['dst'] );
668 } else {
669 $status = $this->doStoreInternal( $params );
670 $this->clearCache( array( $params['dst'] ) );
671 }
672 wfProfileOut( __METHOD__ );
673 return $status;
674 }
675
676 /**
677 * @see FileBackend::storeInternal()
678 */
679 abstract protected function doStoreInternal( array $params );
680
681 /**
682 * Copy a file from one storage path to another in the backend.
683 * Do not call this function from places outside FileBackend and FileOp.
684 *
685 * $params include:
686 * src : source storage path
687 * dst : destination storage path
688 * overwrite : overwrite any file that exists at the destination
689 *
690 * @param $params Array
691 * @return Status
692 */
693 final public function copyInternal( array $params ) {
694 wfProfileIn( __METHOD__ );
695 $status = $this->doCopyInternal( $params );
696 $this->clearCache( array( $params['dst'] ) );
697 wfProfileOut( __METHOD__ );
698 return $status;
699 }
700
701 /**
702 * @see FileBackend::copyInternal()
703 */
704 abstract protected function doCopyInternal( array $params );
705
706 /**
707 * Delete a file at the storage path.
708 * Do not call this function from places outside FileBackend and FileOp.
709 *
710 * $params include:
711 * src : source storage path
712 * ignoreMissingSource : do nothing if the source file does not exist
713 *
714 * @param $params Array
715 * @return Status
716 */
717 final public function deleteInternal( array $params ) {
718 wfProfileIn( __METHOD__ );
719 $status = $this->doDeleteInternal( $params );
720 $this->clearCache( array( $params['src'] ) );
721 wfProfileOut( __METHOD__ );
722 return $status;
723 }
724
725 /**
726 * @see FileBackend::deleteInternal()
727 */
728 abstract protected function doDeleteInternal( array $params );
729
730 /**
731 * Move a file from one storage path to another in the backend.
732 * Do not call this function from places outside FileBackend and FileOp.
733 *
734 * $params include:
735 * src : source storage path
736 * dst : destination storage path
737 * overwrite : overwrite any file that exists at the destination
738 *
739 * @param $params Array
740 * @return Status
741 */
742 final public function moveInternal( array $params ) {
743 wfProfileIn( __METHOD__ );
744 $status = $this->doMoveInternal( $params );
745 $this->clearCache( array( $params['src'], $params['dst'] ) );
746 wfProfileOut( __METHOD__ );
747 return $status;
748 }
749
750 /**
751 * @see FileBackend::moveInternal()
752 */
753 protected function doMoveInternal( array $params ) {
754 // Copy source to dest
755 $status = $this->copyInternal( $params );
756 if ( $status->isOK() ) {
757 // Delete source (only fails due to races or medium going down)
758 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
759 $status->setResult( true, $status->value ); // ignore delete() errors
760 }
761 return $status;
762 }
763
764 /**
765 * @see FileBackendBase::concatenate()
766 */
767 final public function concatenate( array $params ) {
768 wfProfileIn( __METHOD__ );
769 $status = Status::newGood();
770
771 // Try to lock the source files for the scope of this function
772 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
773 if ( $status->isOK() ) {
774 // Actually do the concatenation
775 $status->merge( $this->doConcatenate( $params ) );
776 }
777
778 wfProfileOut( __METHOD__ );
779 return $status;
780 }
781
782 /**
783 * @see FileBackend::concatenate()
784 */
785 protected function doConcatenate( array $params ) {
786 $status = Status::newGood();
787 $tmpPath = $params['dst']; // convenience
788
789 // Check that the specified temp file is valid...
790 wfSuppressWarnings();
791 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
792 wfRestoreWarnings();
793 if ( !$ok ) { // not present or not empty
794 $status->fatal( 'backend-fail-opentemp', $tmpPath );
795 return $status;
796 }
797
798 // Build up the temp file using the source chunks (in order)...
799 $tmpHandle = fopen( $tmpPath, 'a' );
800 if ( $tmpHandle === false ) {
801 $status->fatal( 'backend-fail-opentemp', $tmpPath );
802 return $status;
803 }
804 foreach ( $params['srcs'] as $virtualSource ) {
805 // Get a local FS version of the chunk
806 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
807 if ( !$tmpFile ) {
808 $status->fatal( 'backend-fail-read', $virtualSource );
809 return $status;
810 }
811 // Get a handle to the local FS version
812 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
813 if ( $sourceHandle === false ) {
814 fclose( $tmpHandle );
815 $status->fatal( 'backend-fail-read', $virtualSource );
816 return $status;
817 }
818 // Append chunk to file (pass chunk size to avoid magic quotes)
819 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
820 fclose( $sourceHandle );
821 fclose( $tmpHandle );
822 $status->fatal( 'backend-fail-writetemp', $tmpPath );
823 return $status;
824 }
825 fclose( $sourceHandle );
826 }
827 if ( !fclose( $tmpHandle ) ) {
828 $status->fatal( 'backend-fail-closetemp', $tmpPath );
829 return $status;
830 }
831
832 clearstatcache(); // temp file changed
833
834 return $status;
835 }
836
837 /**
838 * @see FileBackendBase::doPrepare()
839 */
840 final protected function doPrepare( array $params ) {
841 wfProfileIn( __METHOD__ );
842
843 $status = Status::newGood();
844 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
845 if ( $dir === null ) {
846 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
847 wfProfileOut( __METHOD__ );
848 return $status; // invalid storage path
849 }
850
851 if ( $shard !== null ) { // confined to a single container/shard
852 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
853 } else { // directory is on several shards
854 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
855 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
856 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
857 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
858 }
859 }
860
861 wfProfileOut( __METHOD__ );
862 return $status;
863 }
864
865 /**
866 * @see FileBackend::doPrepare()
867 */
868 protected function doPrepareInternal( $container, $dir, array $params ) {
869 return Status::newGood();
870 }
871
872 /**
873 * @see FileBackendBase::doSecure()
874 */
875 final protected function doSecure( array $params ) {
876 wfProfileIn( __METHOD__ );
877 $status = Status::newGood();
878
879 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
880 if ( $dir === null ) {
881 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
882 wfProfileOut( __METHOD__ );
883 return $status; // invalid storage path
884 }
885
886 if ( $shard !== null ) { // confined to a single container/shard
887 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
888 } else { // directory is on several shards
889 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
890 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
891 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
892 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
893 }
894 }
895
896 wfProfileOut( __METHOD__ );
897 return $status;
898 }
899
900 /**
901 * @see FileBackend::doSecure()
902 */
903 protected function doSecureInternal( $container, $dir, array $params ) {
904 return Status::newGood();
905 }
906
907 /**
908 * @see FileBackendBase::doClean()
909 */
910 final protected function doClean( array $params ) {
911 wfProfileIn( __METHOD__ );
912 $status = Status::newGood();
913
914 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
915 if ( $dir === null ) {
916 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
917 wfProfileOut( __METHOD__ );
918 return $status; // invalid storage path
919 }
920
921 // Attempt to lock this directory...
922 $filesLockEx = array( $params['dir'] );
923 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
924 if ( !$status->isOK() ) {
925 wfProfileOut( __METHOD__ );
926 return $status; // abort
927 }
928
929 if ( $shard !== null ) { // confined to a single container/shard
930 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
931 } else { // directory is on several shards
932 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
933 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
934 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
935 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
936 }
937 }
938
939 wfProfileOut( __METHOD__ );
940 return $status;
941 }
942
943 /**
944 * @see FileBackend::doClean()
945 */
946 protected function doCleanInternal( $container, $dir, array $params ) {
947 return Status::newGood();
948 }
949
950 /**
951 * @see FileBackendBase::fileExists()
952 */
953 final public function fileExists( array $params ) {
954 wfProfileIn( __METHOD__ );
955 $stat = $this->getFileStat( $params );
956 wfProfileOut( __METHOD__ );
957 return ( $stat === null ) ? null : (bool)$stat; // null => failure
958 }
959
960 /**
961 * @see FileBackendBase::getFileTimestamp()
962 */
963 final public function getFileTimestamp( array $params ) {
964 wfProfileIn( __METHOD__ );
965 $stat = $this->getFileStat( $params );
966 wfProfileOut( __METHOD__ );
967 return $stat ? $stat['mtime'] : false;
968 }
969
970 /**
971 * @see FileBackendBase::getFileSize()
972 */
973 final public function getFileSize( array $params ) {
974 wfProfileIn( __METHOD__ );
975 $stat = $this->getFileStat( $params );
976 wfProfileOut( __METHOD__ );
977 return $stat ? $stat['size'] : false;
978 }
979
980 /**
981 * @see FileBackendBase::getFileStat()
982 */
983 final public function getFileStat( array $params ) {
984 wfProfileIn( __METHOD__ );
985 $path = $params['src'];
986 $latest = !empty( $params['latest'] );
987 if ( isset( $this->cache[$path]['stat'] ) ) {
988 // If we want the latest data, check that this cached
989 // value was in fact fetched with the latest available data.
990 if ( !$latest || $this->cache[$path]['stat']['latest'] ) {
991 wfProfileOut( __METHOD__ );
992 return $this->cache[$path]['stat'];
993 }
994 }
995 $stat = $this->doGetFileStat( $params );
996 if ( is_array( $stat ) ) { // don't cache negatives
997 $this->trimCache(); // limit memory
998 $this->cache[$path]['stat'] = $stat;
999 $this->cache[$path]['stat']['latest'] = $latest;
1000 }
1001 wfProfileOut( __METHOD__ );
1002 return $stat;
1003 }
1004
1005 /**
1006 * @see FileBackend::getFileStat()
1007 */
1008 abstract protected function doGetFileStat( array $params );
1009
1010 /**
1011 * @see FileBackendBase::getFileContents()
1012 */
1013 public function getFileContents( array $params ) {
1014 wfProfileIn( __METHOD__ );
1015 $tmpFile = $this->getLocalReference( $params );
1016 if ( !$tmpFile ) {
1017 wfProfileOut( __METHOD__ );
1018 return false;
1019 }
1020 wfSuppressWarnings();
1021 $data = file_get_contents( $tmpFile->getPath() );
1022 wfRestoreWarnings();
1023 wfProfileOut( __METHOD__ );
1024 return $data;
1025 }
1026
1027 /**
1028 * @see FileBackendBase::getFileSha1Base36()
1029 */
1030 final public function getFileSha1Base36( array $params ) {
1031 wfProfileIn( __METHOD__ );
1032 $path = $params['src'];
1033 if ( isset( $this->cache[$path]['sha1'] ) ) {
1034 wfProfileOut( __METHOD__ );
1035 return $this->cache[$path]['sha1'];
1036 }
1037 $hash = $this->doGetFileSha1Base36( $params );
1038 if ( $hash ) { // don't cache negatives
1039 $this->trimCache(); // limit memory
1040 $this->cache[$path]['sha1'] = $hash;
1041 }
1042 wfProfileOut( __METHOD__ );
1043 return $hash;
1044 }
1045
1046 /**
1047 * @see FileBackend::getFileSha1Base36()
1048 */
1049 protected function doGetFileSha1Base36( array $params ) {
1050 $fsFile = $this->getLocalReference( $params );
1051 if ( !$fsFile ) {
1052 return false;
1053 } else {
1054 return $fsFile->getSha1Base36();
1055 }
1056 }
1057
1058 /**
1059 * @see FileBackendBase::getFileProps()
1060 */
1061 final public function getFileProps( array $params ) {
1062 wfProfileIn( __METHOD__ );
1063 $fsFile = $this->getLocalReference( $params );
1064 $props = $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
1065 wfProfileOut( __METHOD__ );
1066 return $props;
1067 }
1068
1069 /**
1070 * @see FileBackendBase::getLocalReference()
1071 */
1072 public function getLocalReference( array $params ) {
1073 wfProfileIn( __METHOD__ );
1074 $path = $params['src'];
1075 if ( isset( $this->cache[$path]['localRef'] ) ) {
1076 wfProfileOut( __METHOD__ );
1077 return $this->cache[$path]['localRef'];
1078 }
1079 $tmpFile = $this->getLocalCopy( $params );
1080 if ( $tmpFile ) { // don't cache negatives
1081 $this->trimCache(); // limit memory
1082 $this->cache[$path]['localRef'] = $tmpFile;
1083 }
1084 wfProfileOut( __METHOD__ );
1085 return $tmpFile;
1086 }
1087
1088 /**
1089 * @see FileBackendBase::streamFile()
1090 */
1091 final public function streamFile( array $params ) {
1092 wfProfileIn( __METHOD__ );
1093 $status = Status::newGood();
1094
1095 $info = $this->getFileStat( $params );
1096 if ( !$info ) { // let StreamFile handle the 404
1097 $status->fatal( 'backend-fail-notexists', $params['src'] );
1098 }
1099
1100 // Set output buffer and HTTP headers for stream
1101 $extraHeaders = $params['headers'] ? $params['headers'] : array();
1102 $res = StreamFile::prepareForStream( $params['src'], $info, $extraHeaders );
1103 if ( $res == StreamFile::NOT_MODIFIED ) {
1104 // do nothing; client cache is up to date
1105 } elseif ( $res == StreamFile::READY_STREAM ) {
1106 $status = $this->doStreamFile( $params );
1107 } else {
1108 $status->fatal( 'backend-fail-stream', $params['src'] );
1109 }
1110
1111 wfProfileOut( __METHOD__ );
1112 return $status;
1113 }
1114
1115 /**
1116 * @see FileBackend::streamFile()
1117 */
1118 protected function doStreamFile( array $params ) {
1119 $status = Status::newGood();
1120
1121 $fsFile = $this->getLocalReference( $params );
1122 if ( !$fsFile ) {
1123 $status->fatal( 'backend-fail-stream', $params['src'] );
1124 } elseif ( !readfile( $fsFile->getPath() ) ) {
1125 $status->fatal( 'backend-fail-stream', $params['src'] );
1126 }
1127
1128 return $status;
1129 }
1130
1131 /**
1132 * @see FileBackendBase::getFileList()
1133 */
1134 final public function getFileList( array $params ) {
1135 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1136 if ( $dir === null ) { // invalid storage path
1137 return null;
1138 }
1139 if ( $shard !== null ) {
1140 // File listing is confined to a single container/shard
1141 return $this->getFileListInternal( $fullCont, $dir, $params );
1142 } else {
1143 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
1144 // File listing spans multiple containers/shards
1145 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
1146 return new FileBackendShardListIterator( $this,
1147 $fullCont, $this->getContainerSuffixes( $shortCont ), $params );
1148 }
1149 }
1150
1151 /**
1152 * Do not call this function from places outside FileBackend and ContainerFileListIterator
1153 *
1154 * @param $container string Resolved container name
1155 * @param $dir string Resolved path relative to container
1156 * @param $params Array
1157 * @see FileBackend::getFileList()
1158 */
1159 abstract public function getFileListInternal( $container, $dir, array $params );
1160
1161 /**
1162 * Get the list of supported operations and their corresponding FileOp classes.
1163 *
1164 * @return Array
1165 */
1166 protected function supportedOperations() {
1167 return array(
1168 'store' => 'StoreFileOp',
1169 'copy' => 'CopyFileOp',
1170 'move' => 'MoveFileOp',
1171 'delete' => 'DeleteFileOp',
1172 'create' => 'CreateFileOp',
1173 'null' => 'NullFileOp'
1174 );
1175 }
1176
1177 /**
1178 * Return a list of FileOp objects from a list of operations.
1179 * Do not call this function from places outside FileBackend.
1180 *
1181 * The result must have the same number of items as the input.
1182 * An exception is thrown if an unsupported operation is requested.
1183 *
1184 * @param $ops Array Same format as doOperations()
1185 * @return Array List of FileOp objects
1186 * @throws MWException
1187 */
1188 final public function getOperations( array $ops ) {
1189 $supportedOps = $this->supportedOperations();
1190
1191 $performOps = array(); // array of FileOp objects
1192 // Build up ordered array of FileOps...
1193 foreach ( $ops as $operation ) {
1194 $opName = $operation['op'];
1195 if ( isset( $supportedOps[$opName] ) ) {
1196 $class = $supportedOps[$opName];
1197 // Get params for this operation
1198 $params = $operation;
1199 // Append the FileOp class
1200 $performOps[] = new $class( $this, $params );
1201 } else {
1202 throw new MWException( "Operation `$opName` is not supported." );
1203 }
1204 }
1205
1206 return $performOps;
1207 }
1208
1209 /**
1210 * @see FileBackendBase::doOperationsInternal()
1211 */
1212 protected function doOperationsInternal( array $ops, array $opts ) {
1213 wfProfileIn( __METHOD__ );
1214 $status = Status::newGood();
1215
1216 // Build up a list of FileOps...
1217 $performOps = $this->getOperations( $ops );
1218
1219 // Acquire any locks as needed...
1220 if ( empty( $opts['nonLocking'] ) ) {
1221 // Build up a list of files to lock...
1222 $filesLockEx = $filesLockSh = array();
1223 foreach ( $performOps as $fileOp ) {
1224 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1225 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1226 }
1227 // Optimization: if doing an EX lock anyway, don't also set an SH one
1228 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1229 // Get a shared lock on the parent directory of each path changed
1230 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1231 // Try to lock those files for the scope of this function...
1232 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status );
1233 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
1234 if ( !$status->isOK() ) {
1235 wfProfileOut( __METHOD__ );
1236 return $status; // abort
1237 }
1238 }
1239
1240 // Clear any cache entries (after locks acquired)
1241 $this->clearCache();
1242
1243 // Actually attempt the operation batch...
1244 $subStatus = FileOp::attemptBatch( $performOps, $opts );
1245
1246 // Merge errors into status fields
1247 $status->merge( $subStatus );
1248 $status->success = $subStatus->success; // not done in merge()
1249
1250 wfProfileOut( __METHOD__ );
1251 return $status;
1252 }
1253
1254 /**
1255 * @see FileBackendBase::clearCache()
1256 */
1257 final public function clearCache( array $paths = null ) {
1258 if ( $paths === null ) {
1259 $this->cache = array();
1260 } else {
1261 foreach ( $paths as $path ) {
1262 unset( $this->cache[$path] );
1263 }
1264 }
1265 $this->doClearCache( $paths );
1266 }
1267
1268 /**
1269 * Clears any additional stat caches for storage paths
1270 *
1271 * @see FileBackendBase::clearCache()
1272 *
1273 * @param $paths Array Storage paths (optional)
1274 * @return void
1275 */
1276 protected function doClearCache( array $paths = null ) {}
1277
1278 /**
1279 * Prune the cache if it is too big to add an item
1280 *
1281 * @return void
1282 */
1283 protected function trimCache() {
1284 if ( count( $this->cache ) >= $this->maxCacheSize ) {
1285 reset( $this->cache );
1286 $key = key( $this->cache );
1287 unset( $this->cache[$key] );
1288 }
1289 }
1290
1291 /**
1292 * Get the parent storage directory of a storage path.
1293 * This returns a path like "mwstore://backend/container",
1294 * "mwstore://backend/container/...", or null if there is no parent.
1295 *
1296 * @param $storagePath string
1297 * @return string|null
1298 */
1299 final public static function parentStoragePath( $storagePath ) {
1300 $storagePath = dirname( $storagePath );
1301 list( $b, $cont, $rel ) = self::splitStoragePath( $storagePath );
1302 return ( $rel === null ) ? null : $storagePath;
1303 }
1304
1305 /**
1306 * Check if a given path is a mwstore:// path.
1307 * This does not do any actual validation or existence checks.
1308 *
1309 * @param $path string
1310 * @return bool
1311 */
1312 final public static function isStoragePath( $path ) {
1313 return ( strpos( $path, 'mwstore://' ) === 0 );
1314 }
1315
1316 /**
1317 * Split a storage path (e.g. "mwstore://backend/container/path/to/object")
1318 * into a backend name, a container name, and a relative object path.
1319 *
1320 * @param $storagePath string
1321 * @return Array (backend, container, rel object) or (null, null, null)
1322 */
1323 final public static function splitStoragePath( $storagePath ) {
1324 if ( self::isStoragePath( $storagePath ) ) {
1325 // Note: strlen( 'mwstore://' ) = 10
1326 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
1327 if ( count( $parts ) == 3 ) {
1328 return $parts; // e.g. "backend/container/path"
1329 } elseif ( count( $parts ) == 2 ) {
1330 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
1331 }
1332 }
1333 return array( null, null, null );
1334 }
1335
1336 /**
1337 * Check if a container name is valid.
1338 * This checks for for length and illegal characters.
1339 *
1340 * @param $container string
1341 * @return bool
1342 */
1343 final protected static function isValidContainerName( $container ) {
1344 // This accounts for Swift and S3 restrictions while leaving room
1345 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1346 // Note that matching strings URL encode to the same string;
1347 // in Swift, the length restriction is *after* URL encoding.
1348 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1349 }
1350
1351 /**
1352 * Validate and normalize a relative storage path.
1353 * Null is returned if the path involves directory traversal.
1354 * Traversal is insecure for FS backends and broken for others.
1355 *
1356 * @param $path string Storage path relative to a container
1357 * @return string|null
1358 */
1359 final protected static function normalizeContainerPath( $path ) {
1360 // Normalize directory separators
1361 $path = strtr( $path, '\\', '/' );
1362 // Collapse consecutive directory separators
1363 $path = preg_replace( '![/]{2,}!', '/', $path );
1364 // Use the same traversal protection as Title::secureAndSplit()
1365 if ( strpos( $path, '.' ) !== false ) {
1366 if (
1367 $path === '.' ||
1368 $path === '..' ||
1369 strpos( $path, './' ) === 0 ||
1370 strpos( $path, '../' ) === 0 ||
1371 strpos( $path, '/./' ) !== false ||
1372 strpos( $path, '/../' ) !== false
1373 ) {
1374 return null;
1375 }
1376 }
1377 return $path;
1378 }
1379
1380 /**
1381 * Splits a storage path into an internal container name,
1382 * an internal relative file name, and a container shard suffix.
1383 * Any shard suffix is already appended to the internal container name.
1384 * This also checks that the storage path is valid and within this backend.
1385 *
1386 * If the container is sharded but a suffix could not be determined,
1387 * this means that the path can only refer to a directory and can only
1388 * be scanned by looking in all the container shards.
1389 *
1390 * @param $storagePath string
1391 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1392 */
1393 final protected function resolveStoragePath( $storagePath ) {
1394 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
1395 if ( $backend === $this->name ) { // must be for this backend
1396 $relPath = self::normalizeContainerPath( $relPath );
1397 if ( $relPath !== null ) {
1398 // Get shard for the normalized path if this container is sharded
1399 $cShard = $this->getContainerShard( $container, $relPath );
1400 // Validate and sanitize the relative path (backend-specific)
1401 $relPath = $this->resolveContainerPath( $container, $relPath );
1402 if ( $relPath !== null ) {
1403 // Prepend any wiki ID prefix to the container name
1404 $container = $this->fullContainerName( $container );
1405 if ( self::isValidContainerName( $container ) ) {
1406 // Validate and sanitize the container name (backend-specific)
1407 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1408 if ( $container !== null ) {
1409 return array( $container, $relPath, $cShard );
1410 }
1411 }
1412 }
1413 }
1414 }
1415 return array( null, null, null );
1416 }
1417
1418 /**
1419 * Like resolveStoragePath() except null values are returned if
1420 * the container is sharded and the shard could not be determined.
1421 *
1422 * @see FileBackend::resolveStoragePath()
1423 *
1424 * @param $storagePath string
1425 * @return Array (container, path) or (null, null) if invalid
1426 */
1427 final protected function resolveStoragePathReal( $storagePath ) {
1428 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1429 if ( $cShard !== null ) {
1430 return array( $container, $relPath );
1431 }
1432 return array( null, null );
1433 }
1434
1435 /**
1436 * Get the container name shard suffix for a given path.
1437 * Any empty suffix means the container is not sharded.
1438 *
1439 * @param $container string Container name
1440 * @param $relStoragePath string Storage path relative to the container
1441 * @return string|null Returns null if shard could not be determined
1442 */
1443 final protected function getContainerShard( $container, $relPath ) {
1444 $hashLevels = $this->getContainerHashLevels( $container );
1445 if ( $hashLevels === 1 ) { // 16 shards per container
1446 $hashDirRegex = '(?P<shard>[0-9a-f])';
1447 } elseif ( $hashLevels === 2 ) { // 256 shards per container
1448 $hashDirRegex = '[0-9a-f]/(?P<shard>[0-9a-f]{2})';
1449 } else {
1450 return ''; // no sharding
1451 }
1452 // Allow certain directories to be above the hash dirs so as
1453 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1454 // They must be 2+ chars to avoid any hash directory ambiguity.
1455 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1456 return '.' . str_pad( $m['shard'], $hashLevels, '0', STR_PAD_LEFT );
1457 }
1458 return null; // failed to match
1459 }
1460
1461 /**
1462 * Get the number of hash levels for a container.
1463 * If greater than 0, then all file storage paths within
1464 * the container are required to be hashed accordingly.
1465 *
1466 * @param $container string
1467 * @return integer
1468 */
1469 final protected function getContainerHashLevels( $container ) {
1470 if ( isset( $this->shardViaHashLevels[$container] ) ) {
1471 $hashLevels = (int)$this->shardViaHashLevels[$container];
1472 if ( $hashLevels >= 0 && $hashLevels <= 2 ) {
1473 return $hashLevels;
1474 }
1475 }
1476 return 0; // no sharding
1477 }
1478
1479 /**
1480 * Get a list of full container shard suffixes for a container
1481 *
1482 * @param $container string
1483 * @return Array
1484 */
1485 final protected function getContainerSuffixes( $container ) {
1486 $shards = array();
1487 $digits = $this->getContainerHashLevels( $container );
1488 if ( $digits > 0 ) {
1489 $numShards = 1 << ( $digits * 4 );
1490 for ( $index = 0; $index < $numShards; $index++ ) {
1491 $shards[] = '.' . str_pad( dechex( $index ), $digits, '0', STR_PAD_LEFT );
1492 }
1493 }
1494 return $shards;
1495 }
1496
1497 /**
1498 * Get the full container name, including the wiki ID prefix
1499 *
1500 * @param $container string
1501 * @return string
1502 */
1503 final protected function fullContainerName( $container ) {
1504 if ( $this->wikiId != '' ) {
1505 return "{$this->wikiId}-$container";
1506 } else {
1507 return $container;
1508 }
1509 }
1510
1511 /**
1512 * Resolve a container name, checking if it's allowed by the backend.
1513 * This is intended for internal use, such as encoding illegal chars.
1514 * Subclasses can override this to be more restrictive.
1515 *
1516 * @param $container string
1517 * @return string|null
1518 */
1519 protected function resolveContainerName( $container ) {
1520 return $container;
1521 }
1522
1523 /**
1524 * Resolve a relative storage path, checking if it's allowed by the backend.
1525 * This is intended for internal use, such as encoding illegal chars or perhaps
1526 * getting absolute paths (e.g. FS based backends). Note that the relative path
1527 * may be the empty string (e.g. the path is simply to the container).
1528 *
1529 * @param $container string Container name
1530 * @param $relStoragePath string Storage path relative to the container
1531 * @return string|null Path or null if not valid
1532 */
1533 protected function resolveContainerPath( $container, $relStoragePath ) {
1534 return $relStoragePath;
1535 }
1536
1537 /**
1538 * Get the final extension from a storage or FS path
1539 *
1540 * @param $path string
1541 * @return string
1542 */
1543 final public static function extensionFromPath( $path ) {
1544 $i = strrpos( $path, '.' );
1545 return strtolower( $i ? substr( $path, $i + 1 ) : '' );
1546 }
1547 }
1548
1549 /**
1550 * FileBackend helper function to handle file listings that span container shards.
1551 * Do not use this class from places outside of FileBackend.
1552 *
1553 * @ingroup FileBackend
1554 */
1555 class FileBackendShardListIterator implements Iterator {
1556 /* @var FileBackend */
1557 protected $backend;
1558 /* @var Array */
1559 protected $params;
1560 /* @var Array */
1561 protected $shardSuffixes;
1562 protected $container; // string
1563 protected $directory; // string
1564
1565 /* @var Traversable */
1566 protected $iter;
1567 protected $curShard = 0; // integer
1568 protected $pos = 0; // integer
1569
1570 /**
1571 * @param $backend FileBackend
1572 * @param $container string Full storage container name
1573 * @param $dir string Storage directory relative to container
1574 * @param $suffixes Array List of container shard suffixes
1575 * @param $params Array
1576 */
1577 public function __construct(
1578 FileBackend $backend, $container, $dir, array $suffixes, array $params
1579 ) {
1580 $this->backend = $backend;
1581 $this->container = $container;
1582 $this->directory = $dir;
1583 $this->shardSuffixes = $suffixes;
1584 $this->params = $params;
1585 }
1586
1587 public function current() {
1588 if ( is_array( $this->iter ) ) {
1589 return current( $this->iter );
1590 } else {
1591 return $this->iter->current();
1592 }
1593 }
1594
1595 public function key() {
1596 return $this->pos;
1597 }
1598
1599 public function next() {
1600 ++$this->pos;
1601 if ( is_array( $this->iter ) ) {
1602 next( $this->iter );
1603 } else {
1604 $this->iter->next();
1605 }
1606 // Find the next non-empty shard if no elements are left
1607 $this->nextShardIteratorIfNotValid();
1608 }
1609
1610 /**
1611 * If the iterator for this container shard is out of items,
1612 * then move on to the next container that has items.
1613 * If there are none, then it advances to the last container.
1614 */
1615 protected function nextShardIteratorIfNotValid() {
1616 while ( !$this->valid() ) {
1617 if ( ++$this->curShard >= count( $this->shardSuffixes ) ) {
1618 break; // no more container shards
1619 }
1620 $this->setIteratorFromCurrentShard();
1621 }
1622 }
1623
1624 protected function setIteratorFromCurrentShard() {
1625 $suffix = $this->shardSuffixes[$this->curShard];
1626 $this->iter = $this->backend->getFileListInternal(
1627 "{$this->container}{$suffix}", $this->directory, $this->params );
1628 }
1629
1630 public function rewind() {
1631 $this->pos = 0;
1632 $this->curShard = 0;
1633 $this->setIteratorFromCurrentShard();
1634 // Find the next non-empty shard if this one has no elements
1635 $this->nextShardIteratorIfNotValid();
1636 }
1637
1638 public function valid() {
1639 if ( $this->iter == null ) {
1640 return false; // some failure?
1641 } elseif ( is_array( $this->iter ) ) {
1642 return ( current( $this->iter ) !== false ); // no paths can have this value
1643 } else {
1644 return $this->iter->valid();
1645 }
1646 }
1647 }