3 * OpenStack Swift based file backend.
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
23 * @author Aaron Schulz
27 * @brief Class for an OpenStack Swift based file backend.
29 * This requires the SwiftCloudFiles MediaWiki extension, which includes
30 * the php-cloudfiles library (https://github.com/rackspace/php-cloudfiles).
31 * php-cloudfiles requires the curl, fileinfo, and mb_string PHP extensions.
33 * Status messages should avoid mentioning the Swift account name.
34 * Likewise, error suppression should be used to avoid path disclosure.
36 * @ingroup FileBackend
39 class SwiftFileBackend
extends FileBackendStore
{
40 /** @var CF_Authentication */
41 protected $auth; // Swift authentication handler
42 protected $authTTL; // integer seconds
43 protected $swiftAnonUser; // string; username to handle unauthenticated requests
44 protected $maxContCacheSize = 300; // integer; max containers with entries
46 /** @var CF_Connection */
47 protected $conn; // Swift connection handle
48 protected $connStarted = 0; // integer UNIX timestamp
49 protected $connContainers = array(); // container object cache
52 * @see FileBackendStore::__construct()
53 * Additional $config params include:
54 * swiftAuthUrl : Swift authentication server URL
55 * swiftUser : Swift user used by MediaWiki (account:username)
56 * swiftKey : Swift authentication key for the above user
57 * swiftAuthTTL : Swift authentication TTL (seconds)
58 * swiftAnonUser : Swift user used for end-user requests (account:username)
59 * shardViaHashLevels : Map of container names to sharding config with:
60 * 'base' : base of hash characters, 16 or 36
61 * 'levels' : the number of hash levels (and digits)
62 * 'repeat' : hash subdirectories are prefixed with all the
63 * parent hash directory names (e.g. "a/ab/abc")
65 public function __construct( array $config ) {
66 parent
::__construct( $config );
67 if ( !MWInit
::classExists( 'CF_Constants' ) ) {
68 throw new MWException( 'SwiftCloudFiles extension not installed.' );
71 $this->auth
= new CF_Authentication(
74 null, // account; unused
75 $config['swiftAuthUrl']
78 $this->authTTL
= isset( $config['swiftAuthTTL'] )
79 ?
$config['swiftAuthTTL']
80 : 5 * 60; // some sane number
81 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
82 ?
$config['swiftAnonUser']
84 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
85 ?
$config['shardViaHashLevels']
87 // Cache container info to mask latency
88 $this->memCache
= wfGetMainCache();
92 * @see FileBackendStore::resolveContainerPath()
95 protected function resolveContainerPath( $container, $relStoragePath ) {
96 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
97 return null; // too long for Swift
99 return $relStoragePath;
103 * @see FileBackendStore::isPathUsableInternal()
106 public function isPathUsableInternal( $storagePath ) {
107 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
108 if ( $rel === null ) {
109 return false; // invalid
113 $this->getContainer( $container );
114 return true; // container exists
115 } catch ( NoSuchContainerException
$e ) {
116 } catch ( CloudFilesException
$e ) { // some other exception?
117 $this->handleException( $e, null, __METHOD__
, array( 'path' => $storagePath ) );
124 * @see FileBackendStore::doCreateInternal()
127 protected function doCreateInternal( array $params ) {
128 $status = Status
::newGood();
130 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
131 if ( $dstRel === null ) {
132 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
136 // (a) Check the destination container and object
138 $dContObj = $this->getContainer( $dstCont );
139 if ( empty( $params['overwrite'] ) &&
140 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
142 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
145 } catch ( NoSuchContainerException
$e ) {
146 $status->fatal( 'backend-fail-create', $params['dst'] );
148 } catch ( CloudFilesException
$e ) { // some other exception?
149 $this->handleException( $e, $status, __METHOD__
, $params );
153 // (b) Get a SHA-1 hash of the object
154 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
156 // (c) Actually create the object
158 // Create a fresh CF_Object with no fields preloaded.
159 // We don't want to preserve headers, metadata, and such.
160 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
161 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
162 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
163 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
164 // The MD5 here will be checked within Swift against its own MD5.
165 $obj->set_etag( md5( $params['content'] ) );
166 // Use the same content type as StreamFile for security
167 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
168 if ( !empty( $params['async'] ) ) { // deferred
169 $handle = $obj->write_async( $params['content'] );
170 $status->value
= new SwiftFileOpHandle( $this, $params, 'Create', $handle );
171 } else { // actually write the object in Swift
172 $obj->write( $params['content'] );
174 } catch ( BadContentTypeException
$e ) {
175 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
176 } catch ( CloudFilesException
$e ) { // some other exception?
177 $this->handleException( $e, $status, __METHOD__
, $params );
184 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
186 protected function _getResponseCreate( CF_Async_Op
$cfOp, Status
$status, array $params ) {
188 $cfOp->getLastResponse();
189 } catch ( BadContentTypeException
$e ) {
190 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
195 * @see FileBackendStore::doStoreInternal()
198 protected function doStoreInternal( array $params ) {
199 $status = Status
::newGood();
201 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
202 if ( $dstRel === null ) {
203 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
207 // (a) Check the destination container and object
209 $dContObj = $this->getContainer( $dstCont );
210 if ( empty( $params['overwrite'] ) &&
211 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
213 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
216 } catch ( NoSuchContainerException
$e ) {
217 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
219 } catch ( CloudFilesException
$e ) { // some other exception?
220 $this->handleException( $e, $status, __METHOD__
, $params );
224 // (b) Get a SHA-1 hash of the object
225 $sha1Hash = sha1_file( $params['src'] );
226 if ( $sha1Hash === false ) { // source doesn't exist?
227 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
230 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
232 // (c) Actually store the object
234 // Create a fresh CF_Object with no fields preloaded.
235 // We don't want to preserve headers, metadata, and such.
236 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
237 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
238 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
239 // The MD5 here will be checked within Swift against its own MD5.
240 $obj->set_etag( md5_file( $params['src'] ) );
241 // Use the same content type as StreamFile for security
242 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
243 if ( !empty( $params['async'] ) ) { // deferred
244 wfSuppressWarnings();
245 $fp = fopen( $params['src'], 'rb' );
248 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
250 $handle = $obj->write_async( $fp, filesize( $params['src'] ), true );
251 $status->value
= new SwiftFileOpHandle( $this, $params, 'Store', $handle );
252 $status->value
->resourcesToClose
[] = $fp;
254 } else { // actually write the object in Swift
255 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
257 } catch ( BadContentTypeException
$e ) {
258 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
259 } catch ( IOException
$e ) {
260 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
261 } catch ( CloudFilesException
$e ) { // some other exception?
262 $this->handleException( $e, $status, __METHOD__
, $params );
269 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
271 protected function _getResponseStore( CF_Async_Op
$cfOp, Status
$status, array $params ) {
273 $cfOp->getLastResponse();
274 } catch ( BadContentTypeException
$e ) {
275 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
276 } catch ( IOException
$e ) {
277 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
282 * @see FileBackendStore::doCopyInternal()
285 protected function doCopyInternal( array $params ) {
286 $status = Status
::newGood();
288 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
289 if ( $srcRel === null ) {
290 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
294 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
295 if ( $dstRel === null ) {
296 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
300 // (a) Check the source/destination containers and destination object
302 $sContObj = $this->getContainer( $srcCont );
303 $dContObj = $this->getContainer( $dstCont );
304 if ( empty( $params['overwrite'] ) &&
305 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
307 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
310 } catch ( NoSuchContainerException
$e ) {
311 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
313 } catch ( CloudFilesException
$e ) { // some other exception?
314 $this->handleException( $e, $status, __METHOD__
, $params );
318 // (b) Actually copy the file to the destination
320 if ( !empty( $params['async'] ) ) { // deferred
321 $handle = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel );
322 $status->value
= new SwiftFileOpHandle( $this, $params, 'Copy', $handle );
323 } else { // actually write the object in Swift
324 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
326 } catch ( NoSuchObjectException
$e ) { // source object does not exist
327 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
328 } catch ( CloudFilesException
$e ) { // some other exception?
329 $this->handleException( $e, $status, __METHOD__
, $params );
336 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
338 protected function _getResponseCopy( CF_Async_Op
$cfOp, Status
$status, array $params ) {
340 $cfOp->getLastResponse();
341 } catch ( NoSuchObjectException
$e ) { // source object does not exist
342 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
347 * @see FileBackendStore::doMoveInternal()
350 protected function doMoveInternal( array $params ) {
351 $status = Status
::newGood();
353 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
354 if ( $srcRel === null ) {
355 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
359 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
360 if ( $dstRel === null ) {
361 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
365 // (a) Check the source/destination containers and destination object
367 $sContObj = $this->getContainer( $srcCont );
368 $dContObj = $this->getContainer( $dstCont );
369 if ( empty( $params['overwrite'] ) &&
370 $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
372 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
375 } catch ( NoSuchContainerException
$e ) {
376 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
378 } catch ( CloudFilesException
$e ) { // some other exception?
379 $this->handleException( $e, $status, __METHOD__
, $params );
383 // (b) Actually move the file to the destination
385 if ( !empty( $params['async'] ) ) { // deferred
386 $handle = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel );
387 $status->value
= new SwiftFileOpHandle( $this, $params, 'Move', $handle );
388 } else { // actually write the object in Swift
389 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel );
391 } catch ( NoSuchObjectException
$e ) { // source object does not exist
392 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
393 } catch ( CloudFilesException
$e ) { // some other exception?
394 $this->handleException( $e, $status, __METHOD__
, $params );
401 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
403 protected function _getResponseMove( CF_Async_Op
$cfOp, Status
$status, array $params ) {
405 $cfOp->getLastResponse();
406 } catch ( NoSuchObjectException
$e ) { // source object does not exist
407 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
412 * @see FileBackendStore::doDeleteInternal()
415 protected function doDeleteInternal( array $params ) {
416 $status = Status
::newGood();
418 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
419 if ( $srcRel === null ) {
420 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
425 $sContObj = $this->getContainer( $srcCont );
426 if ( !empty( $params['async'] ) ) { // deferred
427 $handle = $sContObj->delete_object_async( $srcRel );
428 $status->value
= new SwiftFileOpHandle( $this, $params, 'Delete', $handle );
429 } else { // actually write the object in Swift
430 $sContObj->delete_object( $srcRel );
432 } catch ( NoSuchContainerException
$e ) {
433 $status->fatal( 'backend-fail-delete', $params['src'] );
434 } catch ( NoSuchObjectException
$e ) {
435 if ( empty( $params['ignoreMissingSource'] ) ) {
436 $status->fatal( 'backend-fail-delete', $params['src'] );
438 } catch ( CloudFilesException
$e ) { // some other exception?
439 $this->handleException( $e, $status, __METHOD__
, $params );
446 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
448 protected function _getResponseDelete( CF_Async_Op
$cfOp, Status
$status, array $params ) {
450 $cfOp->getLastResponse();
451 } catch ( NoSuchContainerException
$e ) {
452 $status->fatal( 'backend-fail-delete', $params['src'] );
453 } catch ( NoSuchObjectException
$e ) {
454 if ( empty( $params['ignoreMissingSource'] ) ) {
455 $status->fatal( 'backend-fail-delete', $params['src'] );
461 * @see FileBackendStore::doPrepareInternal()
464 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
465 $status = Status
::newGood();
467 // (a) Check if container already exists
469 $contObj = $this->getContainer( $fullCont );
470 // NoSuchContainerException not thrown: container must exist
471 return $status; // already exists
472 } catch ( NoSuchContainerException
$e ) {
473 // NoSuchContainerException thrown: container does not exist
474 } catch ( CloudFilesException
$e ) { // some other exception?
475 $this->handleException( $e, $status, __METHOD__
, $params );
479 // (b) Create container as needed
481 $contObj = $this->createContainer( $fullCont );
482 if ( $this->swiftAnonUser
!= '' ) {
483 // Make container public to end-users...
484 $status->merge( $this->setContainerAccess(
486 array( $this->auth
->username
, $this->swiftAnonUser
), // read
487 array( $this->auth
->username
) // write
490 } catch ( CloudFilesException
$e ) { // some other exception?
491 $this->handleException( $e, $status, __METHOD__
, $params );
499 * @see FileBackendStore::doSecureInternal()
502 protected function doSecureInternal( $fullCont, $dir, array $params ) {
503 $status = Status
::newGood();
505 if ( $this->swiftAnonUser
!= '' ) {
506 // Restrict container from end-users...
508 // doPrepareInternal() should have been called,
509 // so the Swift container should already exist...
510 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
511 // NoSuchContainerException not thrown: container must exist
512 if ( !isset( $contObj->mw_wasSecured
) ) {
513 $status->merge( $this->setContainerAccess(
515 array( $this->auth
->username
), // read
516 array( $this->auth
->username
) // write
518 // @TODO: when php-cloudfiles supports container
519 // metadata, we can make use of that to avoid RTTs
520 $contObj->mw_wasSecured
= true; // avoid useless RTTs
522 } catch ( CloudFilesException
$e ) { // some other exception?
523 $this->handleException( $e, $status, __METHOD__
, $params );
531 * @see FileBackendStore::doCleanInternal()
534 protected function doCleanInternal( $fullCont, $dir, array $params ) {
535 $status = Status
::newGood();
537 // Only containers themselves can be removed, all else is virtual
539 return $status; // nothing to do
542 // (a) Check the container
544 $contObj = $this->getContainer( $fullCont, true );
545 } catch ( NoSuchContainerException
$e ) {
546 return $status; // ok, nothing to do
547 } catch ( CloudFilesException
$e ) { // some other exception?
548 $this->handleException( $e, $status, __METHOD__
, $params );
552 // (b) Delete the container if empty
553 if ( $contObj->object_count
== 0 ) {
555 $this->deleteContainer( $fullCont );
556 } catch ( NoSuchContainerException
$e ) {
557 return $status; // race?
558 } catch ( NonEmptyContainerException
$e ) {
559 return $status; // race? consistency delay?
560 } catch ( CloudFilesException
$e ) { // some other exception?
561 $this->handleException( $e, $status, __METHOD__
, $params );
570 * @see FileBackendStore::doFileExists()
571 * @return array|bool|null
573 protected function doGetFileStat( array $params ) {
574 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
575 if ( $srcRel === null ) {
576 return false; // invalid storage path
581 $contObj = $this->getContainer( $srcCont );
582 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
583 $this->addMissingMetadata( $srcObj, $params['src'] );
585 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
586 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
587 'size' => $srcObj->content_length
,
588 'sha1' => $srcObj->metadata
['Sha1base36']
590 } catch ( NoSuchContainerException
$e ) {
591 } catch ( NoSuchObjectException
$e ) {
592 } catch ( CloudFilesException
$e ) { // some other exception?
594 $this->handleException( $e, null, __METHOD__
, $params );
601 * Fill in any missing object metadata and save it to Swift
603 * @param $obj CF_Object
604 * @param $path string Storage path to object
605 * @return bool Success
606 * @throws Exception cloudfiles exceptions
608 protected function addMissingMetadata( CF_Object
$obj, $path ) {
609 if ( isset( $obj->metadata
['Sha1base36'] ) ) {
610 return true; // nothing to do
612 $status = Status
::newGood();
613 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
614 if ( $status->isOK() ) {
615 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
617 $hash = $tmpFile->getSha1Base36();
618 if ( $hash !== false ) {
619 $obj->metadata
['Sha1base36'] = $hash;
620 $obj->sync_metadata(); // save to Swift
621 return true; // success
625 $obj->metadata
['Sha1base36'] = false;
626 return false; // failed
630 * @see FileBackend::getFileContents()
631 * @return bool|null|string
633 public function getFileContents( array $params ) {
634 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
635 if ( $srcRel === null ) {
636 return false; // invalid storage path
639 if ( !$this->fileExists( $params ) ) {
645 $sContObj = $this->getContainer( $srcCont );
646 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD request
647 $data = $obj->read( $this->headersFromParams( $params ) );
648 } catch ( NoSuchContainerException
$e ) {
649 } catch ( CloudFilesException
$e ) { // some other exception?
650 $this->handleException( $e, null, __METHOD__
, $params );
657 * @see FileBackendStore::doDirectoryExists()
660 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
662 $container = $this->getContainer( $fullCont );
663 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
664 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
665 } catch ( NoSuchContainerException
$e ) {
667 } catch ( CloudFilesException
$e ) { // some other exception?
668 $this->handleException( $e, null, __METHOD__
,
669 array( 'cont' => $fullCont, 'dir' => $dir ) );
672 return null; // error
676 * @see FileBackendStore::getDirectoryListInternal()
677 * @return SwiftFileBackendDirList
679 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
680 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
684 * @see FileBackendStore::getFileListInternal()
685 * @return SwiftFileBackendFileList
687 public function getFileListInternal( $fullCont, $dir, array $params ) {
688 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
692 * Do not call this function outside of SwiftFileBackendFileList
694 * @param $fullCont string Resolved container name
695 * @param $dir string Resolved storage directory with no trailing slash
696 * @param $after string|null Storage path of file to list items after
697 * @param $limit integer Max number of items to list
698 * @param $params Array Includes flag for 'topOnly'
699 * @return Array List of relative paths of dirs directly under $dir
701 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
705 $container = $this->getContainer( $fullCont );
706 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
707 // Non-recursive: only list dirs right under $dir
708 if ( !empty( $params['topOnly'] ) ) {
709 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
710 foreach ( $objects as $object ) { // files and dirs
711 if ( substr( $object, -1 ) === '/' ) {
712 $dirs[] = $object; // directories end in '/'
714 $after = $object; // update last item
716 // Recursive: list all dirs under $dir and its subdirs
718 // Get directory from last item of prior page
719 $lastDir = $this->getParentDir( $after ); // must be first page
720 $objects = $container->list_objects( $limit, $after, $prefix );
721 foreach ( $objects as $object ) { // files
722 $objectDir = $this->getParentDir( $object ); // directory of object
723 if ( $objectDir !== false ) { // file has a parent dir
724 // Swift stores paths in UTF-8, using binary sorting.
725 // See function "create_container_table" in common/db.py.
726 // If a directory is not "greater" than the last one,
727 // then it was already listed by the calling iterator.
728 if ( $objectDir > $lastDir ) {
730 do { // add dir and all its parent dirs
731 $dirs[] = "{$pDir}/";
732 $pDir = $this->getParentDir( $pDir );
733 } while ( $pDir !== false // sanity
734 && $pDir > $lastDir // not done already
735 && strlen( $pDir ) > strlen( $dir ) // within $dir
738 $lastDir = $objectDir;
740 $after = $object; // update last item
743 } catch ( NoSuchContainerException
$e ) {
744 } catch ( CloudFilesException
$e ) { // some other exception?
745 $this->handleException( $e, null, __METHOD__
,
746 array( 'cont' => $fullCont, 'dir' => $dir ) );
752 protected function getParentDir( $path ) {
753 return ( strpos( $path, '/' ) !== false ) ?
dirname( $path ) : false;
757 * Do not call this function outside of SwiftFileBackendFileList
759 * @param $fullCont string Resolved container name
760 * @param $dir string Resolved storage directory with no trailing slash
761 * @param $after string|null Storage path of file to list items after
762 * @param $limit integer Max number of items to list
763 * @param $params Array Includes flag for 'topOnly'
764 * @return Array List of relative paths of files under $dir
766 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
770 $container = $this->getContainer( $fullCont );
771 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
772 // Non-recursive: only list files right under $dir
773 if ( !empty( $params['topOnly'] ) ) { // files and dirs
774 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
775 foreach ( $objects as $object ) {
776 if ( substr( $object, -1 ) !== '/' ) {
777 $files[] = $object; // directories end in '/'
780 // Recursive: list all files under $dir and its subdirs
782 $files = $container->list_objects( $limit, $after, $prefix );
784 $after = end( $files ); // update last item
785 reset( $files ); // reset pointer
786 } catch ( NoSuchContainerException
$e ) {
787 } catch ( CloudFilesException
$e ) { // some other exception?
788 $this->handleException( $e, null, __METHOD__
,
789 array( 'cont' => $fullCont, 'dir' => $dir ) );
796 * @see FileBackendStore::doGetFileSha1base36()
799 protected function doGetFileSha1base36( array $params ) {
800 $stat = $this->getFileStat( $params );
802 return $stat['sha1'];
809 * @see FileBackendStore::doStreamFile()
812 protected function doStreamFile( array $params ) {
813 $status = Status
::newGood();
815 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
816 if ( $srcRel === null ) {
817 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
821 $cont = $this->getContainer( $srcCont );
822 } catch ( NoSuchContainerException
$e ) {
823 $status->fatal( 'backend-fail-stream', $params['src'] );
825 } catch ( CloudFilesException
$e ) { // some other exception?
826 $this->handleException( $e, $status, __METHOD__
, $params );
831 $output = fopen( 'php://output', 'wb' );
832 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
833 $obj->stream( $output, $this->headersFromParams( $params ) );
834 } catch ( CloudFilesException
$e ) { // some other exception?
835 $this->handleException( $e, $status, __METHOD__
, $params );
842 * @see FileBackendStore::getLocalCopy()
843 * @return null|TempFSFile
845 public function getLocalCopy( array $params ) {
846 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
847 if ( $srcRel === null ) {
851 if ( !$this->fileExists( $params ) ) {
857 $sContObj = $this->getContainer( $srcCont );
858 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
859 // Get source file extension
860 $ext = FileBackend
::extensionFromPath( $srcRel );
861 // Create a new temporary file...
862 $tmpFile = TempFSFile
::factory( wfBaseName( $srcRel ) . '_', $ext );
864 $handle = fopen( $tmpFile->getPath(), 'wb' );
866 $obj->stream( $handle, $this->headersFromParams( $params ) );
869 $tmpFile = null; // couldn't open temp file
872 } catch ( NoSuchContainerException
$e ) {
874 } catch ( CloudFilesException
$e ) { // some other exception?
876 $this->handleException( $e, null, __METHOD__
, $params );
883 * @see FileBackendStore::directoriesAreVirtual()
886 protected function directoriesAreVirtual() {
891 * Get headers to send to Swift when reading a file based
892 * on a FileBackend params array, e.g. that of getLocalCopy().
893 * $params is currently only checked for a 'latest' flag.
895 * @param $params Array
898 protected function headersFromParams( array $params ) {
900 if ( !empty( $params['latest'] ) ) {
901 $hdrs[] = 'X-Newest: true';
907 * @see FileBackendStore::doExecuteOpHandlesInternal()
908 * @return Array List of corresponding Status objects
910 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
913 $cfOps = array(); // list of CF_Async_Op objects
914 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
915 $cfOps[$index] = $fileOpHandle->cfOp
;
917 $batch = new CF_Async_Op_Batch( $cfOps );
919 $cfOps = $batch->execute();
920 foreach ( $cfOps as $index => $cfOp ) {
921 $status = Status
::newGood();
922 try { // catch exceptions; update status
923 $function = '_getResponse' . $fileOpHandles[$index]->call
;
924 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params
);
925 } catch ( CloudFilesException
$e ) { // some other exception?
926 $this->handleException( $e, $status,
927 __CLASS__
. ":$function", $fileOpHandles[$index]->params
);
929 $statuses[$index] = $status;
932 foreach ( $fileOpHandles as $fileOpHandle ) {
933 $fileOpHandle->closeResources();
940 * Set read/write permissions for a Swift container
942 * @param $contObj CF_Container Swift container
943 * @param $readGrps Array Swift users who can read (account:user)
944 * @param $writeGrps Array Swift users who can write (account:user)
947 protected function setContainerAccess(
948 CF_Container
$contObj, array $readGrps, array $writeGrps
950 $creds = $contObj->cfs_auth
->export_credentials();
952 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
954 // Note: 10 second timeout consistent with php-cloudfiles
955 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
956 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
957 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
958 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
960 return $req->execute(); // should return 204
964 * Get a connection to the Swift proxy
966 * @return CF_Connection|bool False on failure
967 * @throws InvalidResponseException
969 protected function getConnection() {
970 if ( $this->conn
=== false ) {
971 throw new InvalidResponseException
; // failed last attempt
973 // Session keys expire after a while, so we renew them periodically
974 if ( $this->conn
&& ( time() - $this->connStarted
) > $this->authTTL
) {
975 $this->conn
->close(); // close active cURL connections
978 // Authenticate with proxy and get a session key...
979 if ( $this->conn
=== null ) {
980 $this->connContainers
= array();
982 $this->auth
->authenticate();
983 $this->conn
= new CF_Connection( $this->auth
);
984 $this->connStarted
= time();
985 } catch ( AuthenticationException
$e ) {
986 $this->conn
= false; // don't keep re-trying
987 } catch ( InvalidResponseException
$e ) {
988 $this->conn
= false; // don't keep re-trying
991 if ( !$this->conn
) {
992 throw new InvalidResponseException
; // auth/connection problem
998 * @see FileBackendStore::doClearCache()
1000 protected function doClearCache( array $paths = null ) {
1001 $this->connContainers
= array(); // clear container object cache
1005 * Get a Swift container object, possibly from process cache.
1006 * Use $reCache if the file count or byte count is needed.
1008 * @param $container string Container name
1009 * @param $bypassCache bool Bypass all caches and load from Swift
1010 * @return CF_Container
1011 * @throws NoSuchContainerException
1012 * @throws InvalidResponseException
1014 protected function getContainer( $container, $bypassCache = false ) {
1015 $conn = $this->getConnection(); // Swift proxy connection
1016 if ( $bypassCache ) { // purge cache
1017 unset( $this->connContainers
[$container] );
1018 } elseif ( !isset( $this->connContainers
[$container] ) ) {
1019 $this->primeContainerCache( array( $container ) ); // check persistent cache
1021 if ( !isset( $this->connContainers
[$container] ) ) {
1022 $contObj = $conn->get_container( $container );
1023 // NoSuchContainerException not thrown: container must exist
1024 if ( count( $this->connContainers
) >= $this->maxContCacheSize
) { // trim cache?
1025 reset( $this->connContainers
);
1026 unset( $this->connContainers
[key( $this->connContainers
)] );
1028 $this->connContainers
[$container] = $contObj; // cache it
1029 if ( !$bypassCache ) {
1030 $this->setContainerCache( $container, // update persistent cache
1031 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
1035 return $this->connContainers
[$container];
1039 * Create a Swift container
1041 * @param $container string Container name
1042 * @return CF_Container
1043 * @throws InvalidResponseException
1045 protected function createContainer( $container ) {
1046 $conn = $this->getConnection(); // Swift proxy connection
1047 $contObj = $conn->create_container( $container );
1048 $this->connContainers
[$container] = $contObj; // cache it
1053 * Delete a Swift container
1055 * @param $container string Container name
1057 * @throws InvalidResponseException
1059 protected function deleteContainer( $container ) {
1060 $conn = $this->getConnection(); // Swift proxy connection
1061 $conn->delete_container( $container );
1062 unset( $this->connContainers
[$container] ); // purge cache
1066 * @see FileBackendStore::doPrimeContainerCache()
1069 protected function doPrimeContainerCache( array $containerInfo ) {
1071 $conn = $this->getConnection(); // Swift proxy connection
1072 foreach ( $containerInfo as $container => $info ) {
1073 $this->connContainers
[$container] = new CF_Container(
1081 } catch ( CloudFilesException
$e ) { // some other exception?
1082 $this->handleException( $e, null, __METHOD__
, array() );
1087 * Log an unexpected exception for this backend.
1088 * This also sets the Status object to have a fatal error.
1090 * @param $e Exception
1091 * @param $status Status|null
1092 * @param $func string
1093 * @param $params Array
1096 protected function handleException( Exception
$e, $status, $func, array $params ) {
1097 if ( $status instanceof Status
) {
1098 if ( $e instanceof AuthenticationException
) {
1099 $status->fatal( 'backend-fail-connect', $this->name
);
1101 $status->fatal( 'backend-fail-internal', $this->name
);
1104 if ( $e->getMessage() ) {
1105 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING
);
1107 wfDebugLog( 'SwiftBackend',
1108 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
1109 ( $e->getMessage() ?
": {$e->getMessage()}" : "" )
1115 * @see FileBackendStoreOpHandle
1117 class SwiftFileOpHandle
extends FileBackendStoreOpHandle
{
1118 /** @var CF_Async_Op */
1121 public function __construct( $backend, array $params, $call, CF_Async_Op
$cfOp ) {
1122 $this->backend
= $backend;
1123 $this->params
= $params;
1124 $this->call
= $call;
1125 $this->cfOp
= $cfOp;
1130 * SwiftFileBackend helper class to page through listings.
1131 * Swift also has a listing limit of 10,000 objects for sanity.
1132 * Do not use this class from places outside SwiftFileBackend.
1134 * @ingroup FileBackend
1136 abstract class SwiftFileBackendList
implements Iterator
{
1138 protected $bufferIter = array();
1139 protected $bufferAfter = null; // string; list items *after* this path
1140 protected $pos = 0; // integer
1142 protected $params = array();
1144 /** @var SwiftFileBackend */
1146 protected $container; // string; container name
1147 protected $dir; // string; storage directory
1148 protected $suffixStart; // integer
1150 const PAGE_SIZE
= 5000; // file listing buffer size
1153 * @param $backend SwiftFileBackend
1154 * @param $fullCont string Resolved container name
1155 * @param $dir string Resolved directory relative to container
1156 * @param $params Array
1158 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
1159 $this->backend
= $backend;
1160 $this->container
= $fullCont;
1162 if ( substr( $this->dir
, -1 ) === '/' ) {
1163 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
1165 if ( $this->dir
== '' ) { // whole container
1166 $this->suffixStart
= 0;
1167 } else { // dir within container
1168 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
1170 $this->params
= $params;
1174 * @see Iterator::key()
1177 public function key() {
1182 * @see Iterator::next()
1185 public function next() {
1186 // Advance to the next file in the page
1187 next( $this->bufferIter
);
1189 // Check if there are no files left in this page and
1190 // advance to the next page if this page was not empty.
1191 if ( !$this->valid() && count( $this->bufferIter
) ) {
1192 $this->bufferIter
= $this->pageFromList(
1193 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1194 ); // updates $this->bufferAfter
1199 * @see Iterator::rewind()
1202 public function rewind() {
1204 $this->bufferAfter
= null;
1205 $this->bufferIter
= $this->pageFromList(
1206 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1207 ); // updates $this->bufferAfter
1211 * @see Iterator::valid()
1214 public function valid() {
1215 if ( $this->bufferIter
=== null ) {
1216 return false; // some failure?
1218 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
1223 * Get the given list portion (page)
1225 * @param $container string Resolved container name
1226 * @param $dir string Resolved path relative to container
1227 * @param $after string|null
1228 * @param $limit integer
1229 * @param $params Array
1230 * @return Traversable|Array|null Returns null on failure
1232 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1236 * Iterator for listing directories
1238 class SwiftFileBackendDirList
extends SwiftFileBackendList
{
1240 * @see Iterator::current()
1241 * @return string|bool String (relative path) or false
1243 public function current() {
1244 return substr( current( $this->bufferIter
), $this->suffixStart
, -1 );
1248 * @see SwiftFileBackendList::pageFromList()
1249 * @return Array|null
1251 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1252 return $this->backend
->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1257 * Iterator for listing regular files
1259 class SwiftFileBackendFileList
extends SwiftFileBackendList
{
1261 * @see Iterator::current()
1262 * @return string|bool String (relative path) or false
1264 public function current() {
1265 return substr( current( $this->bufferIter
), $this->suffixStart
);
1269 * @see SwiftFileBackendList::pageFromList()
1270 * @return Array|null
1272 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1273 return $this->backend
->getFileListPageInternal( $container, $dir, $after, $limit, $params );