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 (or Ceph RGW) 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 ( !class_exists( '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
=== '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;
175 public function isPathUsableInternal( $storagePath ) {
176 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
177 if ( $rel === null ) {
178 return false; // invalid
182 $this->getContainer( $container );
183 return true; // container exists
184 } catch ( NoSuchContainerException
$e ) {
185 } catch ( CloudFilesException
$e ) { // some other exception?
186 $this->handleException( $e, null, __METHOD__
, array( 'path' => $storagePath ) );
193 * @param array $headers
196 protected function sanitizeHdrs( array $headers ) {
197 // By default, Swift has annoyingly low maximum header value limits
198 if ( isset( $headers['Content-Disposition'] ) ) {
199 $headers['Content-Disposition'] = $this->truncDisp( $headers['Content-Disposition'] );
205 * @param string $disposition Content-Disposition header value
206 * @return string Truncated Content-Disposition header value to meet Swift limits
208 protected function truncDisp( $disposition ) {
210 foreach ( explode( ';', $disposition ) as $part ) {
211 $part = trim( $part );
212 $new = ( $res === '' ) ?
$part : "{$res};{$part}";
213 if ( strlen( $new ) <= 255 ) {
216 break; // too long; sigh
222 protected function doCreateInternal( array $params ) {
223 $status = Status
::newGood();
225 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
226 if ( $dstRel === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
231 // (a) Check the destination container and object
233 $dContObj = $this->getContainer( $dstCont );
234 } catch ( NoSuchContainerException
$e ) {
235 $status->fatal( 'backend-fail-create', $params['dst'] );
237 } catch ( CloudFilesException
$e ) { // some other exception?
238 $this->handleException( $e, $status, __METHOD__
, $params );
242 // (b) Get a SHA-1 hash of the object
243 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
245 // (c) Actually create the object
247 // Create a fresh CF_Object with no fields preloaded.
248 // We don't want to preserve headers, metadata, and such.
249 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
250 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
251 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
252 // The MD5 here will be checked within Swift against its own MD5.
253 $obj->set_etag( md5( $params['content'] ) );
254 // Use the same content type as StreamFile for security
255 $obj->content_type
= $this->getContentType( $params['dst'], $params['content'], null );
256 // Set any other custom headers if requested
257 if ( isset( $params['headers'] ) ) {
258 $obj->headers +
= $this->sanitizeHdrs( $params['headers'] );
260 if ( !empty( $params['async'] ) ) { // deferred
261 $op = $obj->write_async( $params['content'] );
262 $status->value
= new SwiftFileOpHandle( $this, $params, 'Create', $op );
263 $status->value
->affectedObjects
[] = $obj;
264 } else { // actually write the object in Swift
265 $obj->write( $params['content'] );
266 $this->purgeCDNCache( array( $obj ) );
268 } catch ( CDNNotEnabledException
$e ) {
269 // CDN not enabled; nothing to see here
270 } catch ( BadContentTypeException
$e ) {
271 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
272 } catch ( CloudFilesException
$e ) { // some other exception?
273 $this->handleException( $e, $status, __METHOD__
, $params );
280 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
282 protected function _getResponseCreate( CF_Async_Op
$cfOp, Status
$status, array $params ) {
284 $cfOp->getLastResponse();
285 } catch ( BadContentTypeException
$e ) {
286 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
290 protected function doStoreInternal( array $params ) {
291 $status = Status
::newGood();
293 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
294 if ( $dstRel === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
299 // (a) Check the destination container and object
301 $dContObj = $this->getContainer( $dstCont );
302 } catch ( NoSuchContainerException
$e ) {
303 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
305 } catch ( CloudFilesException
$e ) { // some other exception?
306 $this->handleException( $e, $status, __METHOD__
, $params );
310 // (b) Get a SHA-1 hash of the object
311 wfSuppressWarnings();
312 $sha1Hash = sha1_file( $params['src'] );
314 if ( $sha1Hash === false ) { // source doesn't exist?
315 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
318 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
320 // (c) Actually store the object
322 // Create a fresh CF_Object with no fields preloaded.
323 // We don't want to preserve headers, metadata, and such.
324 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
325 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
326 // The MD5 here will be checked within Swift against its own MD5.
327 $obj->set_etag( md5_file( $params['src'] ) );
328 // Use the same content type as StreamFile for security
329 $obj->content_type
= $this->getContentType( $params['dst'], null, $params['src'] );
330 // Set any other custom headers if requested
331 if ( isset( $params['headers'] ) ) {
332 $obj->headers +
= $this->sanitizeHdrs( $params['headers'] );
334 if ( !empty( $params['async'] ) ) { // deferred
335 wfSuppressWarnings();
336 $fp = fopen( $params['src'], 'rb' );
339 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
341 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
342 $status->value
= new SwiftFileOpHandle( $this, $params, 'Store', $op );
343 $status->value
->resourcesToClose
[] = $fp;
344 $status->value
->affectedObjects
[] = $obj;
346 } else { // actually write the object in Swift
347 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
348 $this->purgeCDNCache( array( $obj ) );
350 } catch ( CDNNotEnabledException
$e ) {
351 // CDN not enabled; nothing to see here
352 } catch ( BadContentTypeException
$e ) {
353 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
354 } catch ( IOException
$e ) {
355 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
356 } catch ( CloudFilesException
$e ) { // some other exception?
357 $this->handleException( $e, $status, __METHOD__
, $params );
364 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
366 protected function _getResponseStore( CF_Async_Op
$cfOp, Status
$status, array $params ) {
368 $cfOp->getLastResponse();
369 } catch ( BadContentTypeException
$e ) {
370 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
371 } catch ( IOException
$e ) {
372 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
376 protected function doCopyInternal( array $params ) {
377 $status = Status
::newGood();
379 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
380 if ( $srcRel === null ) {
381 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
385 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
386 if ( $dstRel === null ) {
387 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
391 // (a) Check the source/destination containers and destination object
393 $sContObj = $this->getContainer( $srcCont );
394 $dContObj = $this->getContainer( $dstCont );
395 } catch ( NoSuchContainerException
$e ) {
396 if ( empty( $params['ignoreMissingSource'] ) ||
isset( $sContObj ) ) {
397 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
400 } catch ( CloudFilesException
$e ) { // some other exception?
401 $this->handleException( $e, $status, __METHOD__
, $params );
405 // (b) Actually copy the file to the destination
407 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
408 $hdrs = array(); // source file headers to override with new values
409 // Set any other custom headers if requested
410 if ( isset( $params['headers'] ) ) {
411 $hdrs +
= $this->sanitizeHdrs( $params['headers'] );
413 if ( !empty( $params['async'] ) ) { // deferred
414 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
415 $status->value
= new SwiftFileOpHandle( $this, $params, 'Copy', $op );
416 $status->value
->affectedObjects
[] = $dstObj;
417 } else { // actually write the object in Swift
418 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
419 $this->purgeCDNCache( array( $dstObj ) );
421 } catch ( CDNNotEnabledException
$e ) {
422 // CDN not enabled; nothing to see here
423 } catch ( NoSuchObjectException
$e ) { // source object does not exist
424 if ( empty( $params['ignoreMissingSource'] ) ) {
425 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
427 } catch ( CloudFilesException
$e ) { // some other exception?
428 $this->handleException( $e, $status, __METHOD__
, $params );
435 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
437 protected function _getResponseCopy( CF_Async_Op
$cfOp, Status
$status, array $params ) {
439 $cfOp->getLastResponse();
440 } catch ( NoSuchObjectException
$e ) { // source object does not exist
441 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
445 protected function doMoveInternal( array $params ) {
446 $status = Status
::newGood();
448 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
449 if ( $srcRel === null ) {
450 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
454 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
455 if ( $dstRel === null ) {
456 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
460 // (a) Check the source/destination containers and destination object
462 $sContObj = $this->getContainer( $srcCont );
463 $dContObj = $this->getContainer( $dstCont );
464 } catch ( NoSuchContainerException
$e ) {
465 if ( empty( $params['ignoreMissingSource'] ) ||
isset( $sContObj ) ) {
466 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
469 } catch ( CloudFilesException
$e ) { // some other exception?
470 $this->handleException( $e, $status, __METHOD__
, $params );
474 // (b) Actually move the file to the destination
476 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
477 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
478 $hdrs = array(); // source file headers to override with new values
479 // Set any other custom headers if requested
480 if ( isset( $params['headers'] ) ) {
481 $hdrs +
= $this->sanitizeHdrs( $params['headers'] );
483 if ( !empty( $params['async'] ) ) { // deferred
484 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
485 $status->value
= new SwiftFileOpHandle( $this, $params, 'Move', $op );
486 $status->value
->affectedObjects
[] = $srcObj;
487 $status->value
->affectedObjects
[] = $dstObj;
488 } else { // actually write the object in Swift
489 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
490 $this->purgeCDNCache( array( $srcObj ) );
491 $this->purgeCDNCache( array( $dstObj ) );
493 } catch ( CDNNotEnabledException
$e ) {
494 // CDN not enabled; nothing to see here
495 } catch ( NoSuchObjectException
$e ) { // source object does not exist
496 if ( empty( $params['ignoreMissingSource'] ) ) {
497 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
499 } catch ( CloudFilesException
$e ) { // some other exception?
500 $this->handleException( $e, $status, __METHOD__
, $params );
507 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
509 protected function _getResponseMove( CF_Async_Op
$cfOp, Status
$status, array $params ) {
511 $cfOp->getLastResponse();
512 } catch ( NoSuchObjectException
$e ) { // source object does not exist
513 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
517 protected function doDeleteInternal( array $params ) {
518 $status = Status
::newGood();
520 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
521 if ( $srcRel === null ) {
522 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
527 $sContObj = $this->getContainer( $srcCont );
528 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
529 if ( !empty( $params['async'] ) ) { // deferred
530 $op = $sContObj->delete_object_async( $srcRel );
531 $status->value
= new SwiftFileOpHandle( $this, $params, 'Delete', $op );
532 $status->value
->affectedObjects
[] = $srcObj;
533 } else { // actually write the object in Swift
534 $sContObj->delete_object( $srcRel );
535 $this->purgeCDNCache( array( $srcObj ) );
537 } catch ( CDNNotEnabledException
$e ) {
538 // CDN not enabled; nothing to see here
539 } catch ( NoSuchContainerException
$e ) {
540 if ( empty( $params['ignoreMissingSource'] ) ) {
541 $status->fatal( 'backend-fail-delete', $params['src'] );
543 } catch ( NoSuchObjectException
$e ) {
544 if ( empty( $params['ignoreMissingSource'] ) ) {
545 $status->fatal( 'backend-fail-delete', $params['src'] );
547 } catch ( CloudFilesException
$e ) { // some other exception?
548 $this->handleException( $e, $status, __METHOD__
, $params );
555 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
557 protected function _getResponseDelete( CF_Async_Op
$cfOp, Status
$status, array $params ) {
559 $cfOp->getLastResponse();
560 } catch ( NoSuchContainerException
$e ) {
561 $status->fatal( 'backend-fail-delete', $params['src'] );
562 } catch ( NoSuchObjectException
$e ) {
563 if ( empty( $params['ignoreMissingSource'] ) ) {
564 $status->fatal( 'backend-fail-delete', $params['src'] );
569 protected function doDescribeInternal( array $params ) {
570 $status = Status
::newGood();
572 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
573 if ( $srcRel === null ) {
574 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
579 $sContObj = $this->getContainer( $srcCont );
580 // Get the latest version of the current metadata
581 $srcObj = $sContObj->get_object( $srcRel,
582 $this->headersFromParams( array( 'latest' => true ) ) );
583 // Merge in the metadata updates...
584 if ( isset( $params['headers'] ) ) {
585 $srcObj->headers
= $this->sanitizeHdrs( $params['headers'] ) +
$srcObj->headers
;
587 $srcObj->sync_metadata(); // save to Swift
588 $this->purgeCDNCache( array( $srcObj ) );
589 } catch ( CDNNotEnabledException
$e ) {
590 // CDN not enabled; nothing to see here
591 } catch ( NoSuchContainerException
$e ) {
592 $status->fatal( 'backend-fail-describe', $params['src'] );
593 } catch ( NoSuchObjectException
$e ) {
594 $status->fatal( 'backend-fail-describe', $params['src'] );
595 } catch ( CloudFilesException
$e ) { // some other exception?
596 $this->handleException( $e, $status, __METHOD__
, $params );
602 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
603 $status = Status
::newGood();
605 // (a) Check if container already exists
607 $this->getContainer( $fullCont );
608 // NoSuchContainerException not thrown: container must exist
609 return $status; // already exists
610 } catch ( NoSuchContainerException
$e ) {
611 // NoSuchContainerException thrown: container does not exist
612 } catch ( CloudFilesException
$e ) { // some other exception?
613 $this->handleException( $e, $status, __METHOD__
, $params );
617 // (b) Create container as needed
619 $contObj = $this->createContainer( $fullCont );
620 if ( !empty( $params['noAccess'] ) ) {
621 // Make container private to end-users...
622 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
624 // Make container public to end-users...
625 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
627 if ( $this->swiftUseCDN
) { // Rackspace style CDN
628 $contObj->make_public( $this->swiftCDNExpiry
);
630 } catch ( CDNNotEnabledException
$e ) {
631 // CDN not enabled; nothing to see here
632 } catch ( CloudFilesException
$e ) { // some other exception?
633 $this->handleException( $e, $status, __METHOD__
, $params );
641 * @see FileBackendStore::doSecureInternal()
644 protected function doSecureInternal( $fullCont, $dir, array $params ) {
645 $status = Status
::newGood();
646 if ( empty( $params['noAccess'] ) ) {
647 return $status; // nothing to do
650 // Restrict container from end-users...
652 // doPrepareInternal() should have been called,
653 // so the Swift container should already exist...
654 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
655 // NoSuchContainerException not thrown: container must exist
657 // Make container private to end-users...
658 $status->merge( $this->setContainerAccess(
660 array( $this->auth
->username
), // read
661 array( $this->auth
->username
) // write
663 if ( $this->swiftUseCDN
&& $contObj->is_public() ) { // Rackspace style CDN
664 $contObj->make_private();
666 } catch ( CDNNotEnabledException
$e ) {
667 // CDN not enabled; nothing to see here
668 } catch ( CloudFilesException
$e ) { // some other exception?
669 $this->handleException( $e, $status, __METHOD__
, $params );
676 * @see FileBackendStore::doPublishInternal()
679 protected function doPublishInternal( $fullCont, $dir, array $params ) {
680 $status = Status
::newGood();
682 // Unrestrict container from end-users...
684 // doPrepareInternal() should have been called,
685 // so the Swift container should already exist...
686 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
687 // NoSuchContainerException not thrown: container must exist
689 // Make container public to end-users...
690 if ( $this->swiftAnonUser
!= '' ) {
691 $status->merge( $this->setContainerAccess(
693 array( $this->auth
->username
, $this->swiftAnonUser
), // read
694 array( $this->auth
->username
, $this->swiftAnonUser
) // write
697 $status->merge( $this->setContainerAccess(
699 array( $this->auth
->username
, '.r:*' ), // read
700 array( $this->auth
->username
) // write
703 if ( $this->swiftUseCDN
&& !$contObj->is_public() ) { // Rackspace style CDN
704 $contObj->make_public();
706 } catch ( CDNNotEnabledException
$e ) {
707 // CDN not enabled; nothing to see here
708 } catch ( CloudFilesException
$e ) { // some other exception?
709 $this->handleException( $e, $status, __METHOD__
, $params );
715 protected function doCleanInternal( $fullCont, $dir, array $params ) {
716 $status = Status
::newGood();
718 // Only containers themselves can be removed, all else is virtual
720 return $status; // nothing to do
723 // (a) Check the container
725 $contObj = $this->getContainer( $fullCont, true );
726 } catch ( NoSuchContainerException
$e ) {
727 return $status; // ok, nothing to do
728 } catch ( CloudFilesException
$e ) { // some other exception?
729 $this->handleException( $e, $status, __METHOD__
, $params );
733 // (b) Delete the container if empty
734 if ( $contObj->object_count
== 0 ) {
736 $this->deleteContainer( $fullCont );
737 } catch ( NoSuchContainerException
$e ) {
738 return $status; // race?
739 } catch ( NonEmptyContainerException
$e ) {
740 return $status; // race? consistency delay?
741 } catch ( CloudFilesException
$e ) { // some other exception?
742 $this->handleException( $e, $status, __METHOD__
, $params );
750 protected function doGetFileStat( array $params ) {
751 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
752 if ( $srcRel === null ) {
753 return false; // invalid storage path
758 $contObj = $this->getContainer( $srcCont );
759 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
760 $this->addMissingMetadata( $srcObj, $params['src'] );
762 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
763 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
764 'size' => (int)$srcObj->content_length
,
765 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
767 } catch ( NoSuchContainerException
$e ) {
768 } catch ( NoSuchObjectException
$e ) {
769 } catch ( CloudFilesException
$e ) { // some other exception?
771 $this->handleException( $e, null, __METHOD__
, $params );
778 * Fill in any missing object metadata and save it to Swift
780 * @param CF_Object $obj
781 * @param string $path Storage path to object
782 * @return bool Success
783 * @throws Exception cloudfiles exceptions
785 protected function addMissingMetadata( CF_Object
$obj, $path ) {
786 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
787 return true; // nothing to do
789 wfProfileIn( __METHOD__
);
790 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING
);
791 $status = Status
::newGood();
792 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
793 if ( $status->isOK() ) {
794 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
796 $hash = $tmpFile->getSha1Base36();
797 if ( $hash !== false ) {
798 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
799 $obj->sync_metadata(); // save to Swift
800 wfProfileOut( __METHOD__
);
801 return true; // success
805 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING
);
806 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
807 wfProfileOut( __METHOD__
);
808 return false; // failed
811 protected function doGetFileContentsMulti( array $params ) {
814 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
815 // Blindly create tmp files and stream to them, catching any exception if the file does
816 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
817 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
818 $cfOps = array(); // (path => CF_Async_Op)
820 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
821 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
822 if ( $srcRel === null ) {
823 $contents[$path] = false;
828 $sContObj = $this->getContainer( $srcCont );
829 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
830 // Create a new temporary memory file...
831 $handle = fopen( 'php://temp', 'wb' );
833 $headers = $this->headersFromParams( $params );
834 if ( count( $pathBatch ) > 1 ) {
835 $cfOps[$path] = $obj->stream_async( $handle, $headers );
836 $cfOps[$path]->_file_handle
= $handle; // close this later
838 $obj->stream( $handle, $headers );
839 rewind( $handle ); // start from the beginning
840 $data = stream_get_contents( $handle );
846 } catch ( NoSuchContainerException
$e ) {
848 } catch ( NoSuchObjectException
$e ) {
850 } catch ( CloudFilesException
$e ) { // some other exception?
852 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
854 $contents[$path] = $data;
857 $batch = new CF_Async_Op_Batch( $cfOps );
858 $cfOps = $batch->execute();
859 foreach ( $cfOps as $path => $cfOp ) {
861 $cfOp->getLastResponse();
862 rewind( $cfOp->_file_handle
); // start from the beginning
863 $contents[$path] = stream_get_contents( $cfOp->_file_handle
);
864 } catch ( NoSuchContainerException
$e ) {
865 $contents[$path] = false;
866 } catch ( NoSuchObjectException
$e ) {
867 $contents[$path] = false;
868 } catch ( CloudFilesException
$e ) { // some other exception?
869 $contents[$path] = false;
870 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
872 fclose( $cfOp->_file_handle
); // close open handle
880 * @see FileBackendStore::doDirectoryExists()
883 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
885 $container = $this->getContainer( $fullCont );
886 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
887 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
888 } catch ( NoSuchContainerException
$e ) {
890 } catch ( CloudFilesException
$e ) { // some other exception?
891 $this->handleException( $e, null, __METHOD__
,
892 array( 'cont' => $fullCont, 'dir' => $dir ) );
895 return null; // error
899 * @see FileBackendStore::getDirectoryListInternal()
900 * @return SwiftFileBackendDirList
902 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
903 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
907 * @see FileBackendStore::getFileListInternal()
908 * @return SwiftFileBackendFileList
910 public function getFileListInternal( $fullCont, $dir, array $params ) {
911 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
915 * Do not call this function outside of SwiftFileBackendFileList
917 * @param string $fullCont Resolved container name
918 * @param string $dir Resolved storage directory with no trailing slash
919 * @param string|null $after Storage path of file to list items after
920 * @param integer $limit Max number of items to list
921 * @param array $params Parameters for getDirectoryList()
922 * @return Array List of resolved paths of directories directly under $dir
923 * @throws FileBackendError
925 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
927 if ( $after === INF
) {
928 return $dirs; // nothing more
931 $section = new ProfileSection( __METHOD__
. '-' . $this->name
);
933 $container = $this->getContainer( $fullCont );
934 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
935 // Non-recursive: only list dirs right under $dir
936 if ( !empty( $params['topOnly'] ) ) {
937 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
938 foreach ( $objects as $object ) { // files and directories
939 if ( substr( $object, -1 ) === '/' ) {
940 $dirs[] = $object; // directories end in '/'
943 // Recursive: list all dirs under $dir and its subdirs
945 // Get directory from last item of prior page
946 $lastDir = $this->getParentDir( $after ); // must be first page
947 $objects = $container->list_objects( $limit, $after, $prefix );
948 foreach ( $objects as $object ) { // files
949 $objectDir = $this->getParentDir( $object ); // directory of object
950 if ( $objectDir !== false && $objectDir !== $dir ) {
951 // Swift stores paths in UTF-8, using binary sorting.
952 // See function "create_container_table" in common/db.py.
953 // If a directory is not "greater" than the last one,
954 // then it was already listed by the calling iterator.
955 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
957 do { // add dir and all its parent dirs
958 $dirs[] = "{$pDir}/";
959 $pDir = $this->getParentDir( $pDir );
960 } while ( $pDir !== false // sanity
961 && strcmp( $pDir, $lastDir ) > 0 // not done already
962 && strlen( $pDir ) > strlen( $dir ) // within $dir
965 $lastDir = $objectDir;
969 // Page on the unfiltered directory listing (what is returned may be filtered)
970 if ( count( $objects ) < $limit ) {
971 $after = INF
; // avoid a second RTT
973 $after = end( $objects ); // update last item
975 } catch ( NoSuchContainerException
$e ) {
976 } catch ( CloudFilesException
$e ) { // some other exception?
977 $this->handleException( $e, null, __METHOD__
,
978 array( 'cont' => $fullCont, 'dir' => $dir ) );
979 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
985 protected function getParentDir( $path ) {
986 return ( strpos( $path, '/' ) !== false ) ?
dirname( $path ) : false;
990 * Do not call this function outside of SwiftFileBackendFileList
992 * @param string $fullCont Resolved container name
993 * @param string $dir Resolved storage directory with no trailing slash
994 * @param string|null $after Storage path of file to list items after
995 * @param integer $limit Max number of items to list
996 * @param array $params Parameters for getDirectoryList()
997 * @return Array List of resolved paths of files under $dir
998 * @throws FileBackendError
1000 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1002 if ( $after === INF
) {
1003 return $files; // nothing more
1006 $section = new ProfileSection( __METHOD__
. '-' . $this->name
);
1008 $container = $this->getContainer( $fullCont );
1009 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
1010 // Non-recursive: only list files right under $dir
1011 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1012 if ( !empty( $params['adviseStat'] ) ) {
1013 $limit = min( $limit, self
::CACHE_CHEAP_SIZE
);
1014 // Note: get_objects() does not include directories
1015 $objects = $this->loadObjectListing( $params, $dir,
1016 $container->get_objects( $limit, $after, $prefix, null, '/' ) );
1019 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1020 foreach ( $objects as $object ) { // files and directories
1021 if ( substr( $object, -1 ) !== '/' ) {
1022 $files[] = $object; // directories end in '/'
1026 // Recursive: list all files under $dir and its subdirs
1028 if ( !empty( $params['adviseStat'] ) ) {
1029 $limit = min( $limit, self
::CACHE_CHEAP_SIZE
);
1030 $objects = $this->loadObjectListing( $params, $dir,
1031 $container->get_objects( $limit, $after, $prefix ) );
1033 $objects = $container->list_objects( $limit, $after, $prefix );
1037 // Page on the unfiltered object listing (what is returned may be filtered)
1038 if ( count( $objects ) < $limit ) {
1039 $after = INF
; // avoid a second RTT
1041 $after = end( $objects ); // update last item
1043 } catch ( NoSuchContainerException
$e ) {
1044 } catch ( CloudFilesException
$e ) { // some other exception?
1045 $this->handleException( $e, null, __METHOD__
,
1046 array( 'cont' => $fullCont, 'dir' => $dir ) );
1047 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1054 * Load a list of objects that belong under $dir into stat cache
1055 * and return a list of the names of the objects in the same order.
1057 * @param array $params Parameters for getDirectoryList()
1058 * @param string $dir Resolved container directory path
1059 * @param array $cfObjects List of CF_Object items
1060 * @return array List of object names
1062 private function loadObjectListing( array $params, $dir, array $cfObjects ) {
1064 $storageDir = rtrim( $params['dir'], '/' );
1065 $suffixStart = ( $dir === '' ) ?
0 : strlen( $dir ) +
1; // size of "path/to/dir/"
1066 // Iterate over the list *backwards* as this primes the stat cache, which is LRU.
1067 // If this fills the cache and the caller stats an uncached file before stating
1068 // the ones on the listing, there would be zero cache hits if this went forwards.
1069 for ( end( $cfObjects ); key( $cfObjects ) !== null; prev( $cfObjects ) ) {
1070 $object = current( $cfObjects );
1071 $path = "{$storageDir}/" . substr( $object->name
, $suffixStart );
1073 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
1074 'mtime' => wfTimestamp( TS_MW
, $object->last_modified
),
1075 'size' => (int)$object->content_length
,
1076 'latest' => false // eventually consistent
1078 $this->cheapCache
->set( $path, 'stat', $val );
1079 $names[] = $object->name
;
1081 return array_reverse( $names ); // keep the paths in original order
1084 protected function doGetFileSha1base36( array $params ) {
1085 $stat = $this->getFileStat( $params );
1087 if ( !isset( $stat['sha1'] ) ) {
1088 // Stat entries filled by file listings don't include SHA1
1089 $this->clearCache( array( $params['src'] ) );
1090 $stat = $this->getFileStat( $params );
1092 return $stat['sha1'];
1098 protected function doStreamFile( array $params ) {
1099 $status = Status
::newGood();
1101 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1102 if ( $srcRel === null ) {
1103 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1107 $cont = $this->getContainer( $srcCont );
1108 } catch ( NoSuchContainerException
$e ) {
1109 $status->fatal( 'backend-fail-stream', $params['src'] );
1111 } catch ( CloudFilesException
$e ) { // some other exception?
1112 $this->handleException( $e, $status, __METHOD__
, $params );
1117 $output = fopen( 'php://output', 'wb' );
1118 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1119 $obj->stream( $output, $this->headersFromParams( $params ) );
1120 } catch ( NoSuchObjectException
$e ) {
1121 $status->fatal( 'backend-fail-stream', $params['src'] );
1122 } catch ( CloudFilesException
$e ) { // some other exception?
1123 $this->handleException( $e, $status, __METHOD__
, $params );
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
1198 public function getFileHttpUrl( array $params ) {
1199 if ( $this->swiftTempUrlKey
!= '' ||
1200 ( $this->rgwS3AccessKey
!= '' && $this->rgwS3SecretKey
!= '' ) )
1202 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1203 if ( $srcRel === null ) {
1204 return null; // invalid path
1207 $ttl = isset( $params['ttl'] ) ?
$params['ttl'] : 86400;
1208 $sContObj = $this->getContainer( $srcCont );
1209 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1210 if ( $this->swiftTempUrlKey
!= '' ) {
1211 return $obj->get_temp_url( $this->swiftTempUrlKey
, $ttl, "GET" );
1212 } else { // give S3 API URL for rgw
1213 $expires = time() +
$ttl;
1214 // Path for signature starts with the bucket
1215 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1216 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1217 // Calculate the hash
1218 $signature = base64_encode( hash_hmac(
1220 "GET\n\n\n{$expires}\n{$spath}",
1221 $this->rgwS3SecretKey
,
1224 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1225 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1226 return wfAppendQuery(
1227 str_replace( '/swift/v1', '', // S3 API is the rgw default
1228 $sContObj->cfs_http
->getStorageUrl() . $spath ),
1230 'Signature' => $signature,
1231 'Expires' => $expires,
1232 'AWSAccessKeyId' => $this->rgwS3AccessKey
)
1235 } catch ( NoSuchContainerException
$e ) {
1236 } catch ( CloudFilesException
$e ) { // some other exception?
1237 $this->handleException( $e, null, __METHOD__
, $params );
1243 protected function directoriesAreVirtual() {
1248 * Get headers to send to Swift when reading a file based
1249 * on a FileBackend params array, e.g. that of getLocalCopy().
1250 * $params is currently only checked for a 'latest' flag.
1252 * @param array $params
1255 protected function headersFromParams( array $params ) {
1257 if ( !empty( $params['latest'] ) ) {
1258 $hdrs[] = 'X-Newest: true';
1263 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1264 $statuses = array();
1266 $cfOps = array(); // list of CF_Async_Op objects
1267 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1268 $cfOps[$index] = $fileOpHandle->cfOp
;
1270 $batch = new CF_Async_Op_Batch( $cfOps );
1272 $cfOps = $batch->execute();
1273 foreach ( $cfOps as $index => $cfOp ) {
1274 $status = Status
::newGood();
1275 $function = '_getResponse' . $fileOpHandles[$index]->call
;
1276 try { // catch exceptions; update status
1277 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params
);
1278 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects
);
1279 } catch ( CloudFilesException
$e ) { // some other exception?
1280 $this->handleException( $e, $status,
1281 __CLASS__
. ":$function", $fileOpHandles[$index]->params
);
1283 $statuses[$index] = $status;
1290 * Set read/write permissions for a Swift container.
1292 * $readGrps is a list of the possible criteria for a request to have
1293 * access to read a container. Each item is one of the following formats:
1294 * - account:user : Grants access if the request is by the given user
1295 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1296 * matches the expression and the request is not for a listing.
1297 * Setting this to '*' effectively makes a container public.
1298 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1299 * matches the expression and the request is for a listing.
1301 * $writeGrps is a list of the possible criteria for a request to have
1302 * access to write to a container. Each item is of the following format:
1303 * - account:user : Grants access if the request is by the given user
1305 * @see http://swift.openstack.org/misc.html#acls
1307 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1308 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1310 * @param CF_Container $contObj Swift container
1311 * @param array $readGrps List of read access routes
1312 * @param array $writeGrps List of write access routes
1315 protected function setContainerAccess(
1316 CF_Container
$contObj, array $readGrps, array $writeGrps
1318 $creds = $contObj->cfs_auth
->export_credentials();
1320 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
1322 // Note: 10 second timeout consistent with php-cloudfiles
1323 $req = MWHttpRequest
::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1324 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1325 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1326 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1328 return $req->execute(); // should return 204
1332 * Purge the CDN cache of affected objects if CDN caching is enabled.
1333 * This is for Rackspace/Akamai CDNs.
1335 * @param array $objects List of CF_Object items
1338 public function purgeCDNCache( array $objects ) {
1339 if ( $this->swiftUseCDN
&& $this->swiftCDNPurgable
) {
1340 foreach ( $objects as $object ) {
1342 $object->purge_from_cdn();
1343 } catch ( CDNNotEnabledException
$e ) {
1344 // CDN not enabled; nothing to see here
1345 } catch ( CloudFilesException
$e ) {
1346 $this->handleException( $e, null, __METHOD__
,
1347 array( 'cont' => $object->container
->name
, 'obj' => $object->name
) );
1354 * Get an authenticated connection handle to the Swift proxy
1356 * @throws CloudFilesException
1357 * @throws CloudFilesException|Exception
1358 * @return CF_Connection|bool False on failure
1360 protected function getConnection() {
1361 if ( $this->connException
instanceof CloudFilesException
) {
1362 if ( ( time() - $this->connErrorTime
) < 60 ) {
1363 throw $this->connException
; // failed last attempt; don't bother
1364 } else { // actually retry this time
1365 $this->connException
= null;
1366 $this->connErrorTime
= 0;
1369 // Session keys expire after a while, so we renew them periodically
1370 $reAuth = ( ( time() - $this->sessionStarted
) > $this->authTTL
);
1371 // Authenticate with proxy and get a session key...
1372 if ( !$this->conn ||
$reAuth ) {
1373 $this->sessionStarted
= 0;
1374 $this->connContainerCache
->clear();
1375 $cacheKey = $this->getCredsCacheKey( $this->auth
->username
);
1376 $creds = $this->srvCache
->get( $cacheKey ); // credentials
1377 if ( is_array( $creds ) ) { // cache hit
1378 $this->auth
->load_cached_credentials(
1379 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1380 $this->sessionStarted
= time() - ceil( $this->authTTL
/ 2 ); // skew for worst case
1381 } else { // cache miss
1383 $this->auth
->authenticate();
1384 $creds = $this->auth
->export_credentials();
1385 $this->srvCache
->add( $cacheKey, $creds, ceil( $this->authTTL
/ 2 ) ); // cache
1386 $this->sessionStarted
= time();
1387 } catch ( CloudFilesException
$e ) {
1388 $this->connException
= $e; // don't keep re-trying
1389 $this->connErrorTime
= time();
1390 throw $e; // throw it back
1393 if ( $this->conn
) { // re-authorizing?
1394 $this->conn
->close(); // close active cURL handles in CF_Http object
1396 $this->conn
= new CF_Connection( $this->auth
);
1402 * Close the connection to the Swift proxy
1406 protected function closeConnection() {
1407 if ( $this->conn
) {
1408 $this->conn
->close(); // close active cURL handles in CF_Http object
1410 $this->sessionStarted
= 0;
1411 $this->connContainerCache
->clear();
1416 * Get the cache key for a container
1418 * @param string $username
1421 private function getCredsCacheKey( $username ) {
1422 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1426 * Get a Swift container object, possibly from process cache.
1427 * Use $reCache if the file count or byte count is needed.
1429 * @param string $container Container name
1430 * @param bool $bypassCache Bypass all caches and load from Swift
1431 * @return CF_Container
1432 * @throws CloudFilesException
1434 protected function getContainer( $container, $bypassCache = false ) {
1435 $conn = $this->getConnection(); // Swift proxy connection
1436 if ( $bypassCache ) { // purge cache
1437 $this->connContainerCache
->clear( $container );
1438 } elseif ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1439 $this->primeContainerCache( array( $container ) ); // check persistent cache
1441 if ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1442 $contObj = $conn->get_container( $container );
1443 // NoSuchContainerException not thrown: container must exist
1444 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache it
1445 if ( !$bypassCache ) {
1446 $this->setContainerCache( $container, // update persistent cache
1447 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
1451 return $this->connContainerCache
->get( $container, 'obj' );
1455 * Create a Swift container
1457 * @param string $container Container name
1458 * @return CF_Container
1459 * @throws CloudFilesException
1461 protected function createContainer( $container ) {
1462 $conn = $this->getConnection(); // Swift proxy connection
1463 $contObj = $conn->create_container( $container );
1464 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache
1469 * Delete a Swift container
1471 * @param string $container Container name
1473 * @throws CloudFilesException
1475 protected function deleteContainer( $container ) {
1476 $conn = $this->getConnection(); // Swift proxy connection
1477 $this->connContainerCache
->clear( $container ); // purge
1478 $conn->delete_container( $container );
1481 protected function doPrimeContainerCache( array $containerInfo ) {
1483 $conn = $this->getConnection(); // Swift proxy connection
1484 foreach ( $containerInfo as $container => $info ) {
1485 $contObj = new CF_Container( $conn->cfs_auth
, $conn->cfs_http
,
1486 $container, $info['count'], $info['bytes'] );
1487 $this->connContainerCache
->set( $container, 'obj', $contObj );
1489 } catch ( CloudFilesException
$e ) { // some other exception?
1490 $this->handleException( $e, null, __METHOD__
, array() );
1495 * Log an unexpected exception for this backend.
1496 * This also sets the Status object to have a fatal error.
1498 * @param Exception $e
1499 * @param Status $status|null
1500 * @param string $func
1501 * @param array $params
1504 protected function handleException( Exception
$e, $status, $func, array $params ) {
1505 if ( $status instanceof Status
) {
1506 if ( $e instanceof AuthenticationException
) {
1507 $status->fatal( 'backend-fail-connect', $this->name
);
1509 $status->fatal( 'backend-fail-internal', $this->name
);
1512 if ( $e->getMessage() ) {
1513 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING
);
1515 if ( $e instanceof InvalidResponseException
) { // possibly a stale token
1516 $this->srvCache
->delete( $this->getCredsCacheKey( $this->auth
->username
) );
1517 $this->closeConnection(); // force a re-connect and re-auth next time
1519 wfDebugLog( 'SwiftBackend',
1520 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
1521 ( $e->getMessage() ?
": {$e->getMessage()}" : "" )
1527 * @see FileBackendStoreOpHandle
1529 class SwiftFileOpHandle
extends FileBackendStoreOpHandle
{
1530 /** @var CF_Async_Op */
1533 public $affectedObjects = array();
1536 * @param SwiftFileBackend $backend
1537 * @param array $params
1538 * @param string $call
1539 * @param CF_Async_Op $cfOp
1541 public function __construct(
1542 SwiftFileBackend
$backend, array $params, $call, CF_Async_Op
$cfOp
1544 $this->backend
= $backend;
1545 $this->params
= $params;
1546 $this->call
= $call;
1547 $this->cfOp
= $cfOp;
1552 * SwiftFileBackend helper class to page through listings.
1553 * Swift also has a listing limit of 10,000 objects for sanity.
1554 * Do not use this class from places outside SwiftFileBackend.
1556 * @ingroup FileBackend
1558 abstract class SwiftFileBackendList
implements Iterator
{
1560 protected $bufferIter = array();
1561 protected $bufferAfter = null; // string; list items *after* this path
1562 protected $pos = 0; // integer
1564 protected $params = array();
1566 /** @var SwiftFileBackend */
1568 protected $container; // string; container name
1569 protected $dir; // string; storage directory
1570 protected $suffixStart; // integer
1572 const PAGE_SIZE
= 9000; // file listing buffer size
1575 * @param SwiftFileBackend $backend
1576 * @param string $fullCont Resolved container name
1577 * @param string $dir Resolved directory relative to container
1578 * @param array $params
1580 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
1581 $this->backend
= $backend;
1582 $this->container
= $fullCont;
1584 if ( substr( $this->dir
, -1 ) === '/' ) {
1585 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
1587 if ( $this->dir
== '' ) { // whole container
1588 $this->suffixStart
= 0;
1589 } else { // dir within container
1590 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
1592 $this->params
= $params;
1596 * @see Iterator::key()
1599 public function key() {
1604 * @see Iterator::next()
1607 public function next() {
1608 // Advance to the next file in the page
1609 next( $this->bufferIter
);
1611 // Check if there are no files left in this page and
1612 // advance to the next page if this page was not empty.
1613 if ( !$this->valid() && count( $this->bufferIter
) ) {
1614 $this->bufferIter
= $this->pageFromList(
1615 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1616 ); // updates $this->bufferAfter
1621 * @see Iterator::rewind()
1624 public function rewind() {
1626 $this->bufferAfter
= null;
1627 $this->bufferIter
= $this->pageFromList(
1628 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1629 ); // updates $this->bufferAfter
1633 * @see Iterator::valid()
1636 public function valid() {
1637 if ( $this->bufferIter
=== null ) {
1638 return false; // some failure?
1640 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
1645 * Get the given list portion (page)
1647 * @param string $container Resolved container name
1648 * @param string $dir Resolved path relative to container
1649 * @param string $after|null
1650 * @param integer $limit
1651 * @param array $params
1652 * @return Traversable|Array
1654 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1658 * Iterator for listing directories
1660 class SwiftFileBackendDirList
extends SwiftFileBackendList
{
1662 * @see Iterator::current()
1663 * @return string|bool String (relative path) or false
1665 public function current() {
1666 return substr( current( $this->bufferIter
), $this->suffixStart
, -1 );
1670 * @see SwiftFileBackendList::pageFromList()
1673 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1674 return $this->backend
->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1679 * Iterator for listing regular files
1681 class SwiftFileBackendFileList
extends SwiftFileBackendList
{
1683 * @see Iterator::current()
1684 * @return string|bool String (relative path) or false
1686 public function current() {
1687 return substr( current( $this->bufferIter
), $this->suffixStart
);
1691 * @see SwiftFileBackendList::pageFromList()
1694 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1695 return $this->backend
->getFileListPageInternal( $container, $dir, $after, $limit, $params );