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 $swiftTempUrlKey; // string; shared secret value for making temp urls
44 protected $swiftAnonUser; // string; username to handle unauthenticated requests
45 protected $swiftUseCDN; // boolean; whether CloudFiles CDN is enabled
46 protected $swiftCDNExpiry; // integer; how long to cache things in the CDN
47 protected $swiftCDNPurgable; // boolean; whether object CDN purging is enabled
49 // Rados Gateway specific options
50 protected $rgwS3AccessKey; // string; S3 access key
51 protected $rgwS3SecretKey; // string; S3 authentication key
53 /** @var CF_Connection */
54 protected $conn; // Swift connection handle
55 protected $sessionStarted = 0; // integer UNIX timestamp
57 /** @var CloudFilesException */
58 protected $connException;
59 protected $connErrorTime = 0; // UNIX timestamp
64 /** @var ProcessCacheLRU */
65 protected $connContainerCache; // container object cache
68 * @see FileBackendStore::__construct()
69 * Additional $config params include:
70 * - swiftAuthUrl : Swift authentication server URL
71 * - swiftUser : Swift user used by MediaWiki (account:username)
72 * - swiftKey : Swift authentication key for the above user
73 * - swiftAuthTTL : Swift authentication TTL (seconds)
74 * - swiftTempUrlKey : Swift "X-Account-Meta-Temp-URL-Key" value on the account.
75 * Do not set this until it has been set in the backend.
76 * - swiftAnonUser : Swift user used for end-user requests (account:username).
77 * If set, then views of public containers are assumed to go
78 * through this user. If not set, then public containers are
79 * accessible to unauthenticated requests via ".r:*" in the ACL.
80 * - swiftUseCDN : Whether a Cloud Files Content Delivery Network is set up
81 * - swiftCDNExpiry : How long (in seconds) to store content in the CDN.
82 * If files may likely change, this should probably not exceed
83 * a few days. For example, deletions may take this long to apply.
84 * If object purging is enabled, however, this is not an issue.
85 * - swiftCDNPurgable : Whether object purge requests are allowed by the CDN.
86 * - shardViaHashLevels : Map of container names to sharding config with:
87 * - base : base of hash characters, 16 or 36
88 * - levels : the number of hash levels (and digits)
89 * - repeat : hash subdirectories are prefixed with all the
90 * parent hash directory names (e.g. "a/ab/abc")
91 * - cacheAuthInfo : Whether to cache authentication tokens in APC, XCache, ect.
92 * If those are not available, then the main cache will be used.
93 * This is probably insecure in shared hosting environments.
94 * - rgwS3AccessKey : Ragos Gateway S3 "access key" value on the account.
95 * Do not set this until it has been set in the backend.
96 * This is used for generating expiring pre-authenticated URLs.
97 * Only use this when using rgw and to work around
98 * http://tracker.newdream.net/issues/3454.
99 * - rgwS3SecretKey : Ragos Gateway S3 "secret key" value on the account.
100 * Do not set this until it has been set in the backend.
101 * This is used for generating expiring pre-authenticated URLs.
102 * Only use this when using rgw and to work around
103 * http://tracker.newdream.net/issues/3454.
105 public function __construct( array $config ) {
106 parent
::__construct( $config );
107 if ( !MWInit
::classExists( 'CF_Constants' ) ) {
108 throw new MWException( 'SwiftCloudFiles extension not installed.' );
111 $this->auth
= new CF_Authentication(
112 $config['swiftUser'],
114 null, // account; unused
115 $config['swiftAuthUrl']
118 $this->authTTL
= isset( $config['swiftAuthTTL'] )
119 ?
$config['swiftAuthTTL']
120 : 5 * 60; // some sane number
121 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
122 ?
$config['swiftAnonUser']
124 $this->swiftTempUrlKey
= isset( $config['swiftTempUrlKey'] )
125 ?
$config['swiftTempUrlKey']
127 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
128 ?
$config['shardViaHashLevels']
130 $this->swiftUseCDN
= isset( $config['swiftUseCDN'] )
131 ?
$config['swiftUseCDN']
133 $this->swiftCDNExpiry
= isset( $config['swiftCDNExpiry'] )
134 ?
$config['swiftCDNExpiry']
135 : 12*3600; // 12 hours is safe (tokens last 24 hours per http://docs.openstack.org)
136 $this->swiftCDNPurgable
= isset( $config['swiftCDNPurgable'] )
137 ?
$config['swiftCDNPurgable']
139 $this->rgwS3AccessKey
= isset( $config['rgwS3AccessKey'] )
140 ?
$config['rgwS3AccessKey']
142 $this->rgwS3SecretKey
= isset( $config['rgwS3SecretKey'] )
143 ?
$config['rgwS3SecretKey']
145 // Cache container information to mask latency
146 $this->memCache
= wfGetMainCache();
147 // Process cache for container info
148 $this->connContainerCache
= new ProcessCacheLRU( 300 );
149 // Cache auth token information to avoid RTTs
150 if ( !empty( $config['cacheAuthInfo'] ) ) {
151 if ( php_sapi_name() === 'cli' ) {
152 $this->srvCache
= wfGetMainCache(); // preferrably memcached
154 try { // look for APC, XCache, WinCache, ect...
155 $this->srvCache
= ObjectCache
::newAccelerator( array() );
156 } catch ( Exception
$e ) {}
159 $this->srvCache
= $this->srvCache ?
$this->srvCache
: new EmptyBagOStuff();
163 * @see FileBackendStore::resolveContainerPath()
166 protected function resolveContainerPath( $container, $relStoragePath ) {
167 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) { // mb_string required by CF
168 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
169 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
170 return null; // too long for Swift
172 return $relStoragePath;
176 * @see FileBackendStore::isPathUsableInternal()
179 public function isPathUsableInternal( $storagePath ) {
180 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
181 if ( $rel === null ) {
182 return false; // invalid
186 $this->getContainer( $container );
187 return true; // container exists
188 } catch ( NoSuchContainerException
$e ) {
189 } catch ( CloudFilesException
$e ) { // some other exception?
190 $this->handleException( $e, null, __METHOD__
, array( 'path' => $storagePath ) );
197 * @param $disposition string Content-Disposition header value
198 * @return string Truncated Content-Disposition header value to meet Swift limits
200 protected function truncDisp( $disposition ) {
202 foreach ( explode( ';', $disposition ) as $part ) {
203 $part = trim( $part );
204 $new = ( $res === '' ) ?
$part : "{$res};{$part}";
205 if ( strlen( $new ) <= 255 ) {
208 break; // too long; sigh
215 * @see FileBackendStore::doCreateInternal()
218 protected function doCreateInternal( array $params ) {
219 $status = Status
::newGood();
221 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
222 if ( $dstRel === null ) {
223 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
227 // (a) Check the destination container and object
229 $dContObj = $this->getContainer( $dstCont );
230 } catch ( NoSuchContainerException
$e ) {
231 $status->fatal( 'backend-fail-create', $params['dst'] );
233 } catch ( CloudFilesException
$e ) { // some other exception?
234 $this->handleException( $e, $status, __METHOD__
, $params );
238 // (b) Get a SHA-1 hash of the object
239 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
241 // (c) Actually create the object
243 // Create a fresh CF_Object with no fields preloaded.
244 // We don't want to preserve headers, metadata, and such.
245 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
246 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
247 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
248 // The MD5 here will be checked within Swift against its own MD5.
249 $obj->set_etag( md5( $params['content'] ) );
250 // Use the same content type as StreamFile for security
251 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
252 if ( !strlen( $obj->content_type
) ) { // special case
253 $obj->content_type
= 'unknown/unknown';
255 // Set the Content-Disposition header if requested
256 if ( isset( $params['disposition'] ) ) {
257 $obj->headers
['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
259 // Set any other custom headers if requested
260 if ( isset( $params['headers'] ) ) {
261 $obj->headers +
= $params['headers'];
263 if ( !empty( $params['async'] ) ) { // deferred
264 $op = $obj->write_async( $params['content'] );
265 $status->value
= new SwiftFileOpHandle( $this, $params, 'Create', $op );
266 $status->value
->affectedObjects
[] = $obj;
267 } else { // actually write the object in Swift
268 $obj->write( $params['content'] );
269 $this->purgeCDNCache( array( $obj ) );
271 } catch ( CDNNotEnabledException
$e ) {
272 // CDN not enabled; nothing to see here
273 } catch ( BadContentTypeException
$e ) {
274 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
275 } catch ( CloudFilesException
$e ) { // some other exception?
276 $this->handleException( $e, $status, __METHOD__
, $params );
283 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
285 protected function _getResponseCreate( CF_Async_Op
$cfOp, Status
$status, array $params ) {
287 $cfOp->getLastResponse();
288 } catch ( BadContentTypeException
$e ) {
289 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
294 * @see FileBackendStore::doStoreInternal()
297 protected function doStoreInternal( array $params ) {
298 $status = Status
::newGood();
300 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
301 if ( $dstRel === null ) {
302 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
306 // (a) Check the destination container and object
308 $dContObj = $this->getContainer( $dstCont );
309 } catch ( NoSuchContainerException
$e ) {
310 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
312 } catch ( CloudFilesException
$e ) { // some other exception?
313 $this->handleException( $e, $status, __METHOD__
, $params );
317 // (b) Get a SHA-1 hash of the object
318 $sha1Hash = sha1_file( $params['src'] );
319 if ( $sha1Hash === false ) { // source doesn't exist?
320 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
323 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
325 // (c) Actually store the object
327 // Create a fresh CF_Object with no fields preloaded.
328 // We don't want to preserve headers, metadata, and such.
329 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
330 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
331 // The MD5 here will be checked within Swift against its own MD5.
332 $obj->set_etag( md5_file( $params['src'] ) );
333 // Use the same content type as StreamFile for security
334 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
335 if ( !strlen( $obj->content_type
) ) { // special case
336 $obj->content_type
= 'unknown/unknown';
338 // Set the Content-Disposition header if requested
339 if ( isset( $params['disposition'] ) ) {
340 $obj->headers
['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
342 // Set any other custom headers if requested
343 if ( isset( $params['headers'] ) ) {
344 $obj->headers +
= $params['headers'];
346 if ( !empty( $params['async'] ) ) { // deferred
347 wfSuppressWarnings();
348 $fp = fopen( $params['src'], 'rb' );
351 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
353 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
354 $status->value
= new SwiftFileOpHandle( $this, $params, 'Store', $op );
355 $status->value
->resourcesToClose
[] = $fp;
356 $status->value
->affectedObjects
[] = $obj;
358 } else { // actually write the object in Swift
359 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
360 $this->purgeCDNCache( array( $obj ) );
362 } catch ( CDNNotEnabledException
$e ) {
363 // CDN not enabled; nothing to see here
364 } catch ( BadContentTypeException
$e ) {
365 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
366 } catch ( IOException
$e ) {
367 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
368 } catch ( CloudFilesException
$e ) { // some other exception?
369 $this->handleException( $e, $status, __METHOD__
, $params );
376 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
378 protected function _getResponseStore( CF_Async_Op
$cfOp, Status
$status, array $params ) {
380 $cfOp->getLastResponse();
381 } catch ( BadContentTypeException
$e ) {
382 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
383 } catch ( IOException
$e ) {
384 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
389 * @see FileBackendStore::doCopyInternal()
392 protected function doCopyInternal( array $params ) {
393 $status = Status
::newGood();
395 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
396 if ( $srcRel === null ) {
397 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
401 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
402 if ( $dstRel === null ) {
403 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
407 // (a) Check the source/destination containers and destination object
409 $sContObj = $this->getContainer( $srcCont );
410 $dContObj = $this->getContainer( $dstCont );
411 } catch ( NoSuchContainerException
$e ) {
412 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
414 } catch ( CloudFilesException
$e ) { // some other exception?
415 $this->handleException( $e, $status, __METHOD__
, $params );
419 // (b) Actually copy the file to the destination
421 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
422 $hdrs = array(); // source file headers to override with new values
423 if ( isset( $params['disposition'] ) ) {
424 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
426 if ( !empty( $params['async'] ) ) { // deferred
427 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
428 $status->value
= new SwiftFileOpHandle( $this, $params, 'Copy', $op );
429 $status->value
->affectedObjects
[] = $dstObj;
430 } else { // actually write the object in Swift
431 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
432 $this->purgeCDNCache( array( $dstObj ) );
434 } catch ( CDNNotEnabledException
$e ) {
435 // CDN not enabled; nothing to see here
436 } catch ( NoSuchObjectException
$e ) { // source object does not exist
437 if ( empty( $params['ignoreMissingSource'] ) ) {
438 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
440 } catch ( CloudFilesException
$e ) { // some other exception?
441 $this->handleException( $e, $status, __METHOD__
, $params );
448 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
450 protected function _getResponseCopy( CF_Async_Op
$cfOp, Status
$status, array $params ) {
452 $cfOp->getLastResponse();
453 } catch ( NoSuchObjectException
$e ) { // source object does not exist
454 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
459 * @see FileBackendStore::doMoveInternal()
462 protected function doMoveInternal( array $params ) {
463 $status = Status
::newGood();
465 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
466 if ( $srcRel === null ) {
467 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
471 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
472 if ( $dstRel === null ) {
473 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
477 // (a) Check the source/destination containers and destination object
479 $sContObj = $this->getContainer( $srcCont );
480 $dContObj = $this->getContainer( $dstCont );
481 } catch ( NoSuchContainerException
$e ) {
482 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
484 } catch ( CloudFilesException
$e ) { // some other exception?
485 $this->handleException( $e, $status, __METHOD__
, $params );
489 // (b) Actually move the file to the destination
491 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
492 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
493 $hdrs = array(); // source file headers to override with new values
494 if ( isset( $params['disposition'] ) ) {
495 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
497 if ( !empty( $params['async'] ) ) { // deferred
498 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
499 $status->value
= new SwiftFileOpHandle( $this, $params, 'Move', $op );
500 $status->value
->affectedObjects
[] = $srcObj;
501 $status->value
->affectedObjects
[] = $dstObj;
502 } else { // actually write the object in Swift
503 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
504 $this->purgeCDNCache( array( $srcObj ) );
505 $this->purgeCDNCache( array( $dstObj ) );
507 } catch ( CDNNotEnabledException
$e ) {
508 // CDN not enabled; nothing to see here
509 } catch ( NoSuchObjectException
$e ) { // source object does not exist
510 if ( empty( $params['ignoreMissingSource'] ) ) {
511 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
513 } catch ( CloudFilesException
$e ) { // some other exception?
514 $this->handleException( $e, $status, __METHOD__
, $params );
521 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
523 protected function _getResponseMove( CF_Async_Op
$cfOp, Status
$status, array $params ) {
525 $cfOp->getLastResponse();
526 } catch ( NoSuchObjectException
$e ) { // source object does not exist
527 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
532 * @see FileBackendStore::doDeleteInternal()
535 protected function doDeleteInternal( array $params ) {
536 $status = Status
::newGood();
538 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
539 if ( $srcRel === null ) {
540 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
545 $sContObj = $this->getContainer( $srcCont );
546 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
547 if ( !empty( $params['async'] ) ) { // deferred
548 $op = $sContObj->delete_object_async( $srcRel );
549 $status->value
= new SwiftFileOpHandle( $this, $params, 'Delete', $op );
550 $status->value
->affectedObjects
[] = $srcObj;
551 } else { // actually write the object in Swift
552 $sContObj->delete_object( $srcRel );
553 $this->purgeCDNCache( array( $srcObj ) );
555 } catch ( CDNNotEnabledException
$e ) {
556 // CDN not enabled; nothing to see here
557 } catch ( NoSuchContainerException
$e ) {
558 $status->fatal( 'backend-fail-delete', $params['src'] );
559 } catch ( NoSuchObjectException
$e ) {
560 if ( empty( $params['ignoreMissingSource'] ) ) {
561 $status->fatal( 'backend-fail-delete', $params['src'] );
563 } catch ( CloudFilesException
$e ) { // some other exception?
564 $this->handleException( $e, $status, __METHOD__
, $params );
571 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
573 protected function _getResponseDelete( CF_Async_Op
$cfOp, Status
$status, array $params ) {
575 $cfOp->getLastResponse();
576 } catch ( NoSuchContainerException
$e ) {
577 $status->fatal( 'backend-fail-delete', $params['src'] );
578 } catch ( NoSuchObjectException
$e ) {
579 if ( empty( $params['ignoreMissingSource'] ) ) {
580 $status->fatal( 'backend-fail-delete', $params['src'] );
586 * @see FileBackendStore::doDescribeInternal()
589 protected function doDescribeInternal( array $params ) {
590 $status = Status
::newGood();
592 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
593 if ( $srcRel === null ) {
594 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
598 $hdrs = isset( $params['headers'] ) ?
$params['headers'] : array();
599 // Set the Content-Disposition header if requested
600 if ( isset( $params['disposition'] ) ) {
601 $hdrs['Content-Disposition'] = $this->truncDisp( $params['disposition'] );
605 $sContObj = $this->getContainer( $srcCont );
606 // Get the latest version of the current metadata
607 $srcObj = $sContObj->get_object( $srcRel,
608 $this->headersFromParams( array( 'latest' => true ) ) );
609 // Merge in the metadata updates...
610 $srcObj->headers
= $hdrs +
$srcObj->headers
;
611 $srcObj->sync_metadata(); // save to Swift
612 $this->purgeCDNCache( array( $srcObj ) );
613 } catch ( CDNNotEnabledException
$e ) {
614 // CDN not enabled; nothing to see here
615 } catch ( NoSuchContainerException
$e ) {
616 $status->fatal( 'backend-fail-describe', $params['src'] );
617 } catch ( NoSuchObjectException
$e ) {
618 $status->fatal( 'backend-fail-describe', $params['src'] );
619 } catch ( CloudFilesException
$e ) { // some other exception?
620 $this->handleException( $e, $status, __METHOD__
, $params );
627 * @see FileBackendStore::doPrepareInternal()
630 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
631 $status = Status
::newGood();
633 // (a) Check if container already exists
635 $contObj = $this->getContainer( $fullCont );
636 // NoSuchContainerException not thrown: container must exist
637 return $status; // already exists
638 } catch ( NoSuchContainerException
$e ) {
639 // NoSuchContainerException thrown: container does not exist
640 } catch ( CloudFilesException
$e ) { // some other exception?
641 $this->handleException( $e, $status, __METHOD__
, $params );
645 // (b) Create container as needed
647 $contObj = $this->createContainer( $fullCont );
648 if ( !empty( $params['noAccess'] ) ) {
649 // Make container private to end-users...
650 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
652 // Make container public to end-users...
653 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
655 if ( $this->swiftUseCDN
) { // Rackspace style CDN
656 $contObj->make_public( $this->swiftCDNExpiry
);
658 } catch ( CDNNotEnabledException
$e ) {
659 // CDN not enabled; nothing to see here
660 } catch ( CloudFilesException
$e ) { // some other exception?
661 $this->handleException( $e, $status, __METHOD__
, $params );
669 * @see FileBackendStore::doSecureInternal()
672 protected function doSecureInternal( $fullCont, $dir, array $params ) {
673 $status = Status
::newGood();
674 if ( empty( $params['noAccess'] ) ) {
675 return $status; // nothing to do
678 // Restrict container from end-users...
680 // doPrepareInternal() should have been called,
681 // so the Swift container should already exist...
682 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
683 // NoSuchContainerException not thrown: container must exist
685 // Make container private to end-users...
686 $status->merge( $this->setContainerAccess(
688 array( $this->auth
->username
), // read
689 array( $this->auth
->username
) // write
691 if ( $this->swiftUseCDN
&& $contObj->is_public() ) { // Rackspace style CDN
692 $contObj->make_private();
694 } catch ( CDNNotEnabledException
$e ) {
695 // CDN not enabled; nothing to see here
696 } catch ( CloudFilesException
$e ) { // some other exception?
697 $this->handleException( $e, $status, __METHOD__
, $params );
704 * @see FileBackendStore::doPublishInternal()
707 protected function doPublishInternal( $fullCont, $dir, array $params ) {
708 $status = Status
::newGood();
710 // Unrestrict container from end-users...
712 // doPrepareInternal() should have been called,
713 // so the Swift container should already exist...
714 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
715 // NoSuchContainerException not thrown: container must exist
717 // Make container public to end-users...
718 if ( $this->swiftAnonUser
!= '' ) {
719 $status->merge( $this->setContainerAccess(
721 array( $this->auth
->username
, $this->swiftAnonUser
), // read
722 array( $this->auth
->username
, $this->swiftAnonUser
) // write
725 $status->merge( $this->setContainerAccess(
727 array( $this->auth
->username
, '.r:*' ), // read
728 array( $this->auth
->username
) // write
731 if ( $this->swiftUseCDN
&& !$contObj->is_public() ) { // Rackspace style CDN
732 $contObj->make_public();
734 } catch ( CDNNotEnabledException
$e ) {
735 // CDN not enabled; nothing to see here
736 } catch ( CloudFilesException
$e ) { // some other exception?
737 $this->handleException( $e, $status, __METHOD__
, $params );
744 * @see FileBackendStore::doCleanInternal()
747 protected function doCleanInternal( $fullCont, $dir, array $params ) {
748 $status = Status
::newGood();
750 // Only containers themselves can be removed, all else is virtual
752 return $status; // nothing to do
755 // (a) Check the container
757 $contObj = $this->getContainer( $fullCont, true );
758 } catch ( NoSuchContainerException
$e ) {
759 return $status; // ok, nothing to do
760 } catch ( CloudFilesException
$e ) { // some other exception?
761 $this->handleException( $e, $status, __METHOD__
, $params );
765 // (b) Delete the container if empty
766 if ( $contObj->object_count
== 0 ) {
768 $this->deleteContainer( $fullCont );
769 } catch ( NoSuchContainerException
$e ) {
770 return $status; // race?
771 } catch ( NonEmptyContainerException
$e ) {
772 return $status; // race? consistency delay?
773 } catch ( CloudFilesException
$e ) { // some other exception?
774 $this->handleException( $e, $status, __METHOD__
, $params );
783 * @see FileBackendStore::doFileExists()
784 * @return array|bool|null
786 protected function doGetFileStat( array $params ) {
787 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
788 if ( $srcRel === null ) {
789 return false; // invalid storage path
794 $contObj = $this->getContainer( $srcCont );
795 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
796 $this->addMissingMetadata( $srcObj, $params['src'] );
798 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
799 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
800 'size' => (int)$srcObj->content_length
,
801 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
803 } catch ( NoSuchContainerException
$e ) {
804 } catch ( NoSuchObjectException
$e ) {
805 } catch ( CloudFilesException
$e ) { // some other exception?
807 $this->handleException( $e, null, __METHOD__
, $params );
814 * Fill in any missing object metadata and save it to Swift
816 * @param $obj CF_Object
817 * @param $path string Storage path to object
818 * @return bool Success
819 * @throws Exception cloudfiles exceptions
821 protected function addMissingMetadata( CF_Object
$obj, $path ) {
822 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
823 return true; // nothing to do
825 wfProfileIn( __METHOD__
);
826 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING
);
827 $status = Status
::newGood();
828 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
829 if ( $status->isOK() ) {
830 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
832 $hash = $tmpFile->getSha1Base36();
833 if ( $hash !== false ) {
834 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
835 $obj->sync_metadata(); // save to Swift
836 wfProfileOut( __METHOD__
);
837 return true; // success
841 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING
);
842 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
843 wfProfileOut( __METHOD__
);
844 return false; // failed
848 * @see FileBackendStore::doGetFileContentsMulti()
851 protected function doGetFileContentsMulti( array $params ) {
854 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
855 // Blindly create tmp files and stream to them, catching any exception if the file does
856 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
857 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
858 $cfOps = array(); // (path => CF_Async_Op)
860 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
861 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
862 if ( $srcRel === null ) {
863 $contents[$path] = false;
868 $sContObj = $this->getContainer( $srcCont );
869 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
870 // Get source file extension
871 $ext = FileBackend
::extensionFromPath( $path );
872 // Create a new temporary memory file...
873 $handle = fopen( 'php://temp', 'wb' );
875 $headers = $this->headersFromParams( $params );
876 if ( count( $pathBatch ) > 1 ) {
877 $cfOps[$path] = $obj->stream_async( $handle, $headers );
878 $cfOps[$path]->_file_handle
= $handle; // close this later
880 $obj->stream( $handle, $headers );
881 rewind( $handle ); // start from the beginning
882 $data = stream_get_contents( $handle );
888 } catch ( NoSuchContainerException
$e ) {
890 } catch ( NoSuchObjectException
$e ) {
892 } catch ( CloudFilesException
$e ) { // some other exception?
894 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
896 $contents[$path] = $data;
899 $batch = new CF_Async_Op_Batch( $cfOps );
900 $cfOps = $batch->execute();
901 foreach ( $cfOps as $path => $cfOp ) {
903 $cfOp->getLastResponse();
904 rewind( $cfOp->_file_handle
); // start from the beginning
905 $contents[$path] = stream_get_contents( $cfOp->_file_handle
);
906 } catch ( NoSuchContainerException
$e ) {
907 $contents[$path] = false;
908 } catch ( NoSuchObjectException
$e ) {
909 $contents[$path] = false;
910 } catch ( CloudFilesException
$e ) { // some other exception?
911 $contents[$path] = false;
912 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
914 fclose( $cfOp->_file_handle
); // close open handle
922 * @see FileBackendStore::doDirectoryExists()
925 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
927 $container = $this->getContainer( $fullCont );
928 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
929 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
930 } catch ( NoSuchContainerException
$e ) {
932 } catch ( CloudFilesException
$e ) { // some other exception?
933 $this->handleException( $e, null, __METHOD__
,
934 array( 'cont' => $fullCont, 'dir' => $dir ) );
937 return null; // error
941 * @see FileBackendStore::getDirectoryListInternal()
942 * @return SwiftFileBackendDirList
944 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
945 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
949 * @see FileBackendStore::getFileListInternal()
950 * @return SwiftFileBackendFileList
952 public function getFileListInternal( $fullCont, $dir, array $params ) {
953 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
957 * Do not call this function outside of SwiftFileBackendFileList
959 * @param $fullCont string Resolved container name
960 * @param $dir string Resolved storage directory with no trailing slash
961 * @param $after string|null Storage path of file to list items after
962 * @param $limit integer Max number of items to list
963 * @param $params Array Includes flag for 'topOnly'
964 * @return Array List of relative paths of dirs directly under $dir
966 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
968 if ( $after === INF
) {
969 return $dirs; // nothing more
971 wfProfileIn( __METHOD__
. '-' . $this->name
);
974 $container = $this->getContainer( $fullCont );
975 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
976 // Non-recursive: only list dirs right under $dir
977 if ( !empty( $params['topOnly'] ) ) {
978 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
979 foreach ( $objects as $object ) { // files and dirs
980 if ( substr( $object, -1 ) === '/' ) {
981 $dirs[] = $object; // directories end in '/'
984 // Recursive: list all dirs under $dir and its subdirs
986 // Get directory from last item of prior page
987 $lastDir = $this->getParentDir( $after ); // must be first page
988 $objects = $container->list_objects( $limit, $after, $prefix );
989 foreach ( $objects as $object ) { // files
990 $objectDir = $this->getParentDir( $object ); // directory of object
991 if ( $objectDir !== false && $objectDir !== $dir ) {
992 // Swift stores paths in UTF-8, using binary sorting.
993 // See function "create_container_table" in common/db.py.
994 // If a directory is not "greater" than the last one,
995 // then it was already listed by the calling iterator.
996 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
998 do { // add dir and all its parent dirs
999 $dirs[] = "{$pDir}/";
1000 $pDir = $this->getParentDir( $pDir );
1001 } while ( $pDir !== false // sanity
1002 && strcmp( $pDir, $lastDir ) > 0 // not done already
1003 && strlen( $pDir ) > strlen( $dir ) // within $dir
1006 $lastDir = $objectDir;
1010 if ( count( $objects ) < $limit ) {
1011 $after = INF
; // avoid a second RTT
1013 $after = end( $objects ); // update last item
1015 } catch ( NoSuchContainerException
$e ) {
1016 } catch ( CloudFilesException
$e ) { // some other exception?
1017 $this->handleException( $e, null, __METHOD__
,
1018 array( 'cont' => $fullCont, 'dir' => $dir ) );
1021 wfProfileOut( __METHOD__
. '-' . $this->name
);
1025 protected function getParentDir( $path ) {
1026 return ( strpos( $path, '/' ) !== false ) ?
dirname( $path ) : false;
1030 * Do not call this function outside of SwiftFileBackendFileList
1032 * @param $fullCont string Resolved container name
1033 * @param $dir string Resolved storage directory with no trailing slash
1034 * @param $after string|null Storage path of file to list items after
1035 * @param $limit integer Max number of items to list
1036 * @param $params Array Includes flag for 'topOnly'
1037 * @return Array List of relative paths of files under $dir
1039 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1041 if ( $after === INF
) {
1042 return $files; // nothing more
1044 wfProfileIn( __METHOD__
. '-' . $this->name
);
1047 $container = $this->getContainer( $fullCont );
1048 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
1049 // Non-recursive: only list files right under $dir
1050 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1051 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1052 foreach ( $objects as $object ) {
1053 if ( substr( $object, -1 ) !== '/' ) {
1054 $files[] = $object; // directories end in '/'
1057 // Recursive: list all files under $dir and its subdirs
1059 $objects = $container->list_objects( $limit, $after, $prefix );
1062 if ( count( $objects ) < $limit ) {
1063 $after = INF
; // avoid a second RTT
1065 $after = end( $objects ); // update last item
1067 } catch ( NoSuchContainerException
$e ) {
1068 } catch ( CloudFilesException
$e ) { // some other exception?
1069 $this->handleException( $e, null, __METHOD__
,
1070 array( 'cont' => $fullCont, 'dir' => $dir ) );
1073 wfProfileOut( __METHOD__
. '-' . $this->name
);
1078 * @see FileBackendStore::doGetFileSha1base36()
1081 protected function doGetFileSha1base36( array $params ) {
1082 $stat = $this->getFileStat( $params );
1084 return $stat['sha1'];
1091 * @see FileBackendStore::doStreamFile()
1094 protected function doStreamFile( array $params ) {
1095 $status = Status
::newGood();
1097 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1098 if ( $srcRel === null ) {
1099 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1103 $cont = $this->getContainer( $srcCont );
1104 } catch ( NoSuchContainerException
$e ) {
1105 $status->fatal( 'backend-fail-stream', $params['src'] );
1107 } catch ( CloudFilesException
$e ) { // some other exception?
1108 $this->handleException( $e, $status, __METHOD__
, $params );
1113 $output = fopen( 'php://output', 'wb' );
1114 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1115 $obj->stream( $output, $this->headersFromParams( $params ) );
1116 } catch ( NoSuchObjectException
$e ) {
1117 $status->fatal( 'backend-fail-stream', $params['src'] );
1118 } catch ( CloudFilesException
$e ) { // some other exception?
1119 $this->handleException( $e, $status, __METHOD__
, $params );
1126 * @see FileBackendStore::doGetLocalCopyMulti()
1127 * @return null|TempFSFile
1129 protected function doGetLocalCopyMulti( array $params ) {
1130 $tmpFiles = array();
1132 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1133 // Blindly create tmp files and stream to them, catching any exception if the file does
1134 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1135 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1136 $cfOps = array(); // (path => CF_Async_Op)
1138 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1139 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1140 if ( $srcRel === null ) {
1141 $tmpFiles[$path] = null;
1146 $sContObj = $this->getContainer( $srcCont );
1147 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1148 // Get source file extension
1149 $ext = FileBackend
::extensionFromPath( $path );
1150 // Create a new temporary file...
1151 $tmpFile = TempFSFile
::factory( 'localcopy_', $ext );
1153 $handle = fopen( $tmpFile->getPath(), 'wb' );
1155 $headers = $this->headersFromParams( $params );
1156 if ( count( $pathBatch ) > 1 ) {
1157 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1158 $cfOps[$path]->_file_handle
= $handle; // close this later
1160 $obj->stream( $handle, $headers );
1167 } catch ( NoSuchContainerException
$e ) {
1169 } catch ( NoSuchObjectException
$e ) {
1171 } catch ( CloudFilesException
$e ) { // some other exception?
1173 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1175 $tmpFiles[$path] = $tmpFile;
1178 $batch = new CF_Async_Op_Batch( $cfOps );
1179 $cfOps = $batch->execute();
1180 foreach ( $cfOps as $path => $cfOp ) {
1182 $cfOp->getLastResponse();
1183 } catch ( NoSuchContainerException
$e ) {
1184 $tmpFiles[$path] = null;
1185 } catch ( NoSuchObjectException
$e ) {
1186 $tmpFiles[$path] = null;
1187 } catch ( CloudFilesException
$e ) { // some other exception?
1188 $tmpFiles[$path] = null;
1189 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1191 fclose( $cfOp->_file_handle
); // close open handle
1199 * @see FileBackendStore::getFileHttpUrl()
1200 * @return string|null
1202 public function getFileHttpUrl( array $params ) {
1203 if ( $this->swiftTempUrlKey
!= '' ||
1204 ( $this->rgwS3AccessKey
!= '' && $this->rgwS3SecretKey
!= '' ) )
1206 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1207 if ( $srcRel === null ) {
1208 return null; // invalid path
1211 $sContObj = $this->getContainer( $srcCont );
1212 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1213 if ( $this->swiftTempUrlKey
!= '' ) {
1214 return $obj->get_temp_url( $this->swiftTempUrlKey
, 86400, "GET" );
1215 } else { // give S3 API URL for rgw
1216 $expires = time() +
86400;
1217 // Path for signature starts with the bucket
1218 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1219 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1220 // Calculate the hash
1221 $signature = base64_encode( hash_hmac(
1223 "GET\n\n\n{$expires}\n{$spath}",
1224 $this->rgwS3SecretKey
,
1227 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1228 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1229 return wfAppendQuery(
1230 str_replace( '/swift/v1', '', // S3 API is the rgw default
1231 $sContObj->cfs_http
->getStorageUrl() . $spath ),
1233 'Signature' => $signature,
1234 'Expires' => $expires,
1235 'AWSAccessKeyId' => $this->rgwS3AccessKey
)
1238 } catch ( NoSuchContainerException
$e ) {
1239 } catch ( CloudFilesException
$e ) { // some other exception?
1240 $this->handleException( $e, null, __METHOD__
, $params );
1247 * @see FileBackendStore::directoriesAreVirtual()
1250 protected function directoriesAreVirtual() {
1255 * Get headers to send to Swift when reading a file based
1256 * on a FileBackend params array, e.g. that of getLocalCopy().
1257 * $params is currently only checked for a 'latest' flag.
1259 * @param $params Array
1262 protected function headersFromParams( array $params ) {
1264 if ( !empty( $params['latest'] ) ) {
1265 $hdrs[] = 'X-Newest: true';
1271 * @see FileBackendStore::doExecuteOpHandlesInternal()
1272 * @return Array List of corresponding Status objects
1274 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1275 $statuses = array();
1277 $cfOps = array(); // list of CF_Async_Op objects
1278 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1279 $cfOps[$index] = $fileOpHandle->cfOp
;
1281 $batch = new CF_Async_Op_Batch( $cfOps );
1283 $cfOps = $batch->execute();
1284 foreach ( $cfOps as $index => $cfOp ) {
1285 $status = Status
::newGood();
1286 $function = '_getResponse' . $fileOpHandles[$index]->call
;
1287 try { // catch exceptions; update status
1288 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params
);
1289 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects
);
1290 } catch ( CloudFilesException
$e ) { // some other exception?
1291 $this->handleException( $e, $status,
1292 __CLASS__
. ":$function", $fileOpHandles[$index]->params
);
1294 $statuses[$index] = $status;
1301 * Set read/write permissions for a Swift container.
1303 * $readGrps is a list of the possible criteria for a request to have
1304 * access to read a container. Each item is one of the following formats:
1305 * - account:user : Grants access if the request is by the given user
1306 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1307 * matches the expression and the request is not for a listing.
1308 * Setting this to '*' effectively makes a container public.
1309 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1310 * matches the expression and the request for a listing.
1312 * $writeGrps is a list of the possible criteria for a request to have
1313 * access to write to a container. Each item is of the following format:
1314 * - account:user : Grants access if the request is by the given user
1316 * @see http://swift.openstack.org/misc.html#acls
1318 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1319 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1321 * @param $contObj CF_Container Swift container
1322 * @param $readGrps Array List of read access routes
1323 * @param $writeGrps Array List of write access routes
1326 protected function setContainerAccess(
1327 CF_Container
$contObj, array $readGrps, array $writeGrps
1329 $creds = $contObj->cfs_auth
->export_credentials();
1331 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
1333 // Note: 10 second timeout consistent with php-cloudfiles
1334 $req = MWHttpRequest
::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1335 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1336 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1337 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1339 return $req->execute(); // should return 204
1343 * Purge the CDN cache of affected objects if CDN caching is enabled.
1344 * This is for Rackspace/Akamai CDNs.
1346 * @param $objects Array List of CF_Object items
1349 public function purgeCDNCache( array $objects ) {
1350 if ( $this->swiftUseCDN
&& $this->swiftCDNPurgable
) {
1351 foreach ( $objects as $object ) {
1353 $object->purge_from_cdn();
1354 } catch ( CDNNotEnabledException
$e ) {
1355 // CDN not enabled; nothing to see here
1356 } catch ( CloudFilesException
$e ) {
1357 $this->handleException( $e, null, __METHOD__
,
1358 array( 'cont' => $object->container
->name
, 'obj' => $object->name
) );
1365 * Get an authenticated connection handle to the Swift proxy
1367 * @throws CloudFilesException
1368 * @throws CloudFilesException|Exception
1369 * @return CF_Connection|bool False on failure
1371 protected function getConnection() {
1372 if ( $this->connException
instanceof CloudFilesException
) {
1373 if ( ( time() - $this->connErrorTime
) < 60 ) {
1374 throw $this->connException
; // failed last attempt; don't bother
1375 } else { // actually retry this time
1376 $this->connException
= null;
1377 $this->connErrorTime
= 0;
1380 // Session keys expire after a while, so we renew them periodically
1381 $reAuth = ( ( time() - $this->sessionStarted
) > $this->authTTL
);
1382 // Authenticate with proxy and get a session key...
1383 if ( !$this->conn ||
$reAuth ) {
1384 $this->sessionStarted
= 0;
1385 $this->connContainerCache
->clear();
1386 $cacheKey = $this->getCredsCacheKey( $this->auth
->username
);
1387 $creds = $this->srvCache
->get( $cacheKey ); // credentials
1388 if ( is_array( $creds ) ) { // cache hit
1389 $this->auth
->load_cached_credentials(
1390 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1391 $this->sessionStarted
= time() - ceil( $this->authTTL
/2 ); // skew for worst case
1392 } else { // cache miss
1394 $this->auth
->authenticate();
1395 $creds = $this->auth
->export_credentials();
1396 $this->srvCache
->add( $cacheKey, $creds, ceil( $this->authTTL
/2 ) ); // cache
1397 $this->sessionStarted
= time();
1398 } catch ( CloudFilesException
$e ) {
1399 $this->connException
= $e; // don't keep re-trying
1400 $this->connErrorTime
= time();
1401 throw $e; // throw it back
1404 if ( $this->conn
) { // re-authorizing?
1405 $this->conn
->close(); // close active cURL handles in CF_Http object
1407 $this->conn
= new CF_Connection( $this->auth
);
1413 * Close the connection to the Swift proxy
1417 protected function closeConnection() {
1418 if ( $this->conn
) {
1419 $this->conn
->close(); // close active cURL handles in CF_Http object
1421 $this->sessionStarted
= 0;
1422 $this->connContainerCache
->clear();
1427 * Get the cache key for a container
1429 * @param $username string
1432 private function getCredsCacheKey( $username ) {
1433 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1437 * Get a Swift container object, possibly from process cache.
1438 * Use $reCache if the file count or byte count is needed.
1440 * @param $container string Container name
1441 * @param $bypassCache bool Bypass all caches and load from Swift
1442 * @return CF_Container
1443 * @throws CloudFilesException
1445 protected function getContainer( $container, $bypassCache = false ) {
1446 $conn = $this->getConnection(); // Swift proxy connection
1447 if ( $bypassCache ) { // purge cache
1448 $this->connContainerCache
->clear( $container );
1449 } elseif ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1450 $this->primeContainerCache( array( $container ) ); // check persistent cache
1452 if ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1453 $contObj = $conn->get_container( $container );
1454 // NoSuchContainerException not thrown: container must exist
1455 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache it
1456 if ( !$bypassCache ) {
1457 $this->setContainerCache( $container, // update persistent cache
1458 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
1462 return $this->connContainerCache
->get( $container, 'obj' );
1466 * Create a Swift container
1468 * @param $container string Container name
1469 * @return CF_Container
1470 * @throws CloudFilesException
1472 protected function createContainer( $container ) {
1473 $conn = $this->getConnection(); // Swift proxy connection
1474 $contObj = $conn->create_container( $container );
1475 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache
1480 * Delete a Swift container
1482 * @param $container string Container name
1484 * @throws CloudFilesException
1486 protected function deleteContainer( $container ) {
1487 $conn = $this->getConnection(); // Swift proxy connection
1488 $this->connContainerCache
->clear( $container ); // purge
1489 $conn->delete_container( $container );
1493 * @see FileBackendStore::doPrimeContainerCache()
1496 protected function doPrimeContainerCache( array $containerInfo ) {
1498 $conn = $this->getConnection(); // Swift proxy connection
1499 foreach ( $containerInfo as $container => $info ) {
1500 $contObj = new CF_Container( $conn->cfs_auth
, $conn->cfs_http
,
1501 $container, $info['count'], $info['bytes'] );
1502 $this->connContainerCache
->set( $container, 'obj', $contObj );
1504 } catch ( CloudFilesException
$e ) { // some other exception?
1505 $this->handleException( $e, null, __METHOD__
, array() );
1510 * Log an unexpected exception for this backend.
1511 * This also sets the Status object to have a fatal error.
1513 * @param $e Exception
1514 * @param $status Status|null
1515 * @param $func string
1516 * @param $params Array
1519 protected function handleException( Exception
$e, $status, $func, array $params ) {
1520 if ( $status instanceof Status
) {
1521 if ( $e instanceof AuthenticationException
) {
1522 $status->fatal( 'backend-fail-connect', $this->name
);
1524 $status->fatal( 'backend-fail-internal', $this->name
);
1527 if ( $e->getMessage() ) {
1528 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING
);
1530 if ( $e instanceof InvalidResponseException
) { // possibly a stale token
1531 $this->srvCache
->delete( $this->getCredsCacheKey( $this->auth
->username
) );
1532 $this->closeConnection(); // force a re-connect and re-auth next time
1534 wfDebugLog( 'SwiftBackend',
1535 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
1536 ( $e->getMessage() ?
": {$e->getMessage()}" : "" )
1542 * @see FileBackendStoreOpHandle
1544 class SwiftFileOpHandle
extends FileBackendStoreOpHandle
{
1545 /** @var CF_Async_Op */
1548 public $affectedObjects = array();
1550 public function __construct( $backend, array $params, $call, CF_Async_Op
$cfOp ) {
1551 $this->backend
= $backend;
1552 $this->params
= $params;
1553 $this->call
= $call;
1554 $this->cfOp
= $cfOp;
1559 * SwiftFileBackend helper class to page through listings.
1560 * Swift also has a listing limit of 10,000 objects for sanity.
1561 * Do not use this class from places outside SwiftFileBackend.
1563 * @ingroup FileBackend
1565 abstract class SwiftFileBackendList
implements Iterator
{
1567 protected $bufferIter = array();
1568 protected $bufferAfter = null; // string; list items *after* this path
1569 protected $pos = 0; // integer
1571 protected $params = array();
1573 /** @var SwiftFileBackend */
1575 protected $container; // string; container name
1576 protected $dir; // string; storage directory
1577 protected $suffixStart; // integer
1579 const PAGE_SIZE
= 9000; // file listing buffer size
1582 * @param $backend SwiftFileBackend
1583 * @param $fullCont string Resolved container name
1584 * @param $dir string Resolved directory relative to container
1585 * @param $params Array
1587 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
1588 $this->backend
= $backend;
1589 $this->container
= $fullCont;
1591 if ( substr( $this->dir
, -1 ) === '/' ) {
1592 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
1594 if ( $this->dir
== '' ) { // whole container
1595 $this->suffixStart
= 0;
1596 } else { // dir within container
1597 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
1599 $this->params
= $params;
1603 * @see Iterator::key()
1606 public function key() {
1611 * @see Iterator::next()
1614 public function next() {
1615 // Advance to the next file in the page
1616 next( $this->bufferIter
);
1618 // Check if there are no files left in this page and
1619 // advance to the next page if this page was not empty.
1620 if ( !$this->valid() && count( $this->bufferIter
) ) {
1621 $this->bufferIter
= $this->pageFromList(
1622 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1623 ); // updates $this->bufferAfter
1628 * @see Iterator::rewind()
1631 public function rewind() {
1633 $this->bufferAfter
= null;
1634 $this->bufferIter
= $this->pageFromList(
1635 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1636 ); // updates $this->bufferAfter
1640 * @see Iterator::valid()
1643 public function valid() {
1644 if ( $this->bufferIter
=== null ) {
1645 return false; // some failure?
1647 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
1652 * Get the given list portion (page)
1654 * @param $container string Resolved container name
1655 * @param $dir string Resolved path relative to container
1656 * @param $after string|null
1657 * @param $limit integer
1658 * @param $params Array
1659 * @return Traversable|Array|null Returns null on failure
1661 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1665 * Iterator for listing directories
1667 class SwiftFileBackendDirList
extends SwiftFileBackendList
{
1669 * @see Iterator::current()
1670 * @return string|bool String (relative path) or false
1672 public function current() {
1673 return substr( current( $this->bufferIter
), $this->suffixStart
, -1 );
1677 * @see SwiftFileBackendList::pageFromList()
1678 * @return Array|null
1680 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1681 return $this->backend
->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1686 * Iterator for listing regular files
1688 class SwiftFileBackendFileList
extends SwiftFileBackendList
{
1690 * @see Iterator::current()
1691 * @return string|bool String (relative path) or false
1693 public function current() {
1694 return substr( current( $this->bufferIter
), $this->suffixStart
);
1698 * @see SwiftFileBackendList::pageFromList()
1699 * @return Array|null
1701 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1702 return $this->backend
->getFileListPageInternal( $container, $dir, $after, $limit, $params );