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 various random Swift dates to TS_MW
763 'mtime' => $this->convertSwiftDate( $srcObj->last_modified
, TS_MW
),
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 * Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
779 * Dates might also come in like "2013-05-11T07:37:27.678360" from Swift listings,
780 * missing the timezone suffix (though Ceph RGW does not appear to have this bug).
783 * @param int $format Output format (TS_* constant)
785 * @throws MWException
787 protected function convertSwiftDate( $ts, $format = TS_MW
) {
788 $timestamp = new MWTimestamp( $ts );
789 return $timestamp->getTimestamp( $format );
793 * Fill in any missing object metadata and save it to Swift
795 * @param CF_Object $obj
796 * @param string $path Storage path to object
797 * @return bool Success
798 * @throws Exception cloudfiles exceptions
800 protected function addMissingMetadata( CF_Object
$obj, $path ) {
801 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
802 return true; // nothing to do
804 wfProfileIn( __METHOD__
);
805 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING
);
806 $status = Status
::newGood();
807 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager
::LOCK_UW
, $status );
808 if ( $status->isOK() ) {
809 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
811 $hash = $tmpFile->getSha1Base36();
812 if ( $hash !== false ) {
813 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
814 $obj->sync_metadata(); // save to Swift
815 wfProfileOut( __METHOD__
);
816 return true; // success
820 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING
);
821 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
822 wfProfileOut( __METHOD__
);
823 return false; // failed
826 protected function doGetFileContentsMulti( array $params ) {
829 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
830 // Blindly create tmp files and stream to them, catching any exception if the file does
831 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
832 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
833 $cfOps = array(); // (path => CF_Async_Op)
835 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
836 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
837 if ( $srcRel === null ) {
838 $contents[$path] = false;
843 $sContObj = $this->getContainer( $srcCont );
844 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
845 // Create a new temporary memory file...
846 $handle = fopen( 'php://temp', 'wb' );
848 $headers = $this->headersFromParams( $params );
849 if ( count( $pathBatch ) > 1 ) {
850 $cfOps[$path] = $obj->stream_async( $handle, $headers );
851 $cfOps[$path]->_file_handle
= $handle; // close this later
853 $obj->stream( $handle, $headers );
854 rewind( $handle ); // start from the beginning
855 $data = stream_get_contents( $handle );
861 } catch ( NoSuchContainerException
$e ) {
863 } catch ( NoSuchObjectException
$e ) {
865 } catch ( CloudFilesException
$e ) { // some other exception?
867 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
869 $contents[$path] = $data;
872 $batch = new CF_Async_Op_Batch( $cfOps );
873 $cfOps = $batch->execute();
874 foreach ( $cfOps as $path => $cfOp ) {
876 $cfOp->getLastResponse();
877 rewind( $cfOp->_file_handle
); // start from the beginning
878 $contents[$path] = stream_get_contents( $cfOp->_file_handle
);
879 } catch ( NoSuchContainerException
$e ) {
880 $contents[$path] = false;
881 } catch ( NoSuchObjectException
$e ) {
882 $contents[$path] = false;
883 } catch ( CloudFilesException
$e ) { // some other exception?
884 $contents[$path] = false;
885 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
887 fclose( $cfOp->_file_handle
); // close open handle
895 * @see FileBackendStore::doDirectoryExists()
898 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
900 $container = $this->getContainer( $fullCont );
901 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
902 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
903 } catch ( NoSuchContainerException
$e ) {
905 } catch ( CloudFilesException
$e ) { // some other exception?
906 $this->handleException( $e, null, __METHOD__
,
907 array( 'cont' => $fullCont, 'dir' => $dir ) );
910 return null; // error
914 * @see FileBackendStore::getDirectoryListInternal()
915 * @return SwiftFileBackendDirList
917 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
918 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
922 * @see FileBackendStore::getFileListInternal()
923 * @return SwiftFileBackendFileList
925 public function getFileListInternal( $fullCont, $dir, array $params ) {
926 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
930 * Do not call this function outside of SwiftFileBackendFileList
932 * @param string $fullCont Resolved container name
933 * @param string $dir Resolved storage directory with no trailing slash
934 * @param string|null $after Resolved container relative path to list items after
935 * @param integer $limit Max number of items to list
936 * @param array $params Parameters for getDirectoryList()
937 * @return Array List of container relative resolved paths of directories directly under $dir
938 * @throws FileBackendError
940 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
942 if ( $after === INF
) {
943 return $dirs; // nothing more
946 $section = new ProfileSection( __METHOD__
. '-' . $this->name
);
948 $container = $this->getContainer( $fullCont );
949 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
950 // Non-recursive: only list dirs right under $dir
951 if ( !empty( $params['topOnly'] ) ) {
952 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
953 foreach ( $objects as $object ) { // files and directories
954 if ( substr( $object, -1 ) === '/' ) {
955 $dirs[] = $object; // directories end in '/'
958 // Recursive: list all dirs under $dir and its subdirs
960 // Get directory from last item of prior page
961 $lastDir = $this->getParentDir( $after ); // must be first page
962 $objects = $container->list_objects( $limit, $after, $prefix );
963 foreach ( $objects as $object ) { // files
964 $objectDir = $this->getParentDir( $object ); // directory of object
965 if ( $objectDir !== false && $objectDir !== $dir ) {
966 // Swift stores paths in UTF-8, using binary sorting.
967 // See function "create_container_table" in common/db.py.
968 // If a directory is not "greater" than the last one,
969 // then it was already listed by the calling iterator.
970 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
972 do { // add dir and all its parent dirs
973 $dirs[] = "{$pDir}/";
974 $pDir = $this->getParentDir( $pDir );
975 } while ( $pDir !== false // sanity
976 && strcmp( $pDir, $lastDir ) > 0 // not done already
977 && strlen( $pDir ) > strlen( $dir ) // within $dir
980 $lastDir = $objectDir;
984 // Page on the unfiltered directory listing (what is returned may be filtered)
985 if ( count( $objects ) < $limit ) {
986 $after = INF
; // avoid a second RTT
988 $after = end( $objects ); // update last item
990 } catch ( NoSuchContainerException
$e ) {
991 } catch ( CloudFilesException
$e ) { // some other exception?
992 $this->handleException( $e, null, __METHOD__
,
993 array( 'cont' => $fullCont, 'dir' => $dir ) );
994 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1000 protected function getParentDir( $path ) {
1001 return ( strpos( $path, '/' ) !== false ) ?
dirname( $path ) : false;
1005 * Do not call this function outside of SwiftFileBackendFileList
1007 * @param string $fullCont Resolved container name
1008 * @param string $dir Resolved storage directory with no trailing slash
1009 * @param string|null $after Resolved container relative path of file to list items after
1010 * @param integer $limit Max number of items to list
1011 * @param array $params Parameters for getDirectoryList()
1012 * @return Array List of resolved container relative paths of files under $dir
1013 * @throws FileBackendError
1015 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1016 $files = array(); // list of (path, stat array or null) entries
1017 if ( $after === INF
) {
1018 return $files; // nothing more
1021 $section = new ProfileSection( __METHOD__
. '-' . $this->name
);
1023 $container = $this->getContainer( $fullCont );
1024 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
1025 $objects = array(); // list of unfiltered names or CF_Object items
1026 // Non-recursive: only list files right under $dir
1027 if ( !empty( $params['topOnly'] ) ) {
1028 if ( !empty( $params['adviseStat'] ) ) {
1029 // Note: get_objects() does not include directories
1030 $objects = $container->get_objects( $limit, $after, $prefix, null, '/' );
1032 // Note: list_objects() includes directories here
1033 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1035 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1036 // Recursive: list all files under $dir and its subdirs
1038 // Note: get_objects()/list_objects() here only return file objects
1039 if ( !empty( $params['adviseStat'] ) ) {
1040 $objects = $container->get_objects( $limit, $after, $prefix );
1042 $objects = $container->list_objects( $limit, $after, $prefix );
1044 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1046 // Page on the unfiltered object listing (what is returned may be filtered)
1047 if ( count( $objects ) < $limit ) {
1048 $after = INF
; // avoid a second RTT
1050 $after = end( $objects ); // update last item
1051 $after = is_object( $after ) ?
$after->name
: $after;
1053 } catch ( NoSuchContainerException
$e ) {
1054 } catch ( CloudFilesException
$e ) { // some other exception?
1055 $this->handleException( $e, null, __METHOD__
,
1056 array( 'cont' => $fullCont, 'dir' => $dir ) );
1057 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1064 * Build a list of file objects, filtering out any directories
1065 * and extracting any stat info if provided in $objects (for CF_Objects)
1067 * @param array $params Parameters for getDirectoryList()
1068 * @param string $dir Resolved container directory path
1069 * @param array $objects List of CF_Object items or object names
1070 * @return array List of (names,stat array or null) entries
1072 private function buildFileObjectListing( array $params, $dir, array $objects ) {
1074 foreach ( $objects as $object ) {
1075 if ( is_object( $object ) ) {
1077 // Convert various random Swift dates to TS_MW
1078 'mtime' => $this->convertSwiftDate( $object->last_modified
, TS_MW
),
1079 'size' => (int)$object->content_length
,
1080 'latest' => false // eventually consistent
1082 $names[] = array( $object->name
, $stat );
1083 } elseif ( substr( $object, -1 ) !== '/' ) {
1084 // Omit directories, which end in '/' in listings
1085 $names[] = array( $object, null );
1092 * Do not call this function outside of SwiftFileBackendFileList
1094 * @param string $path Storage path
1095 * @param array $val Stat value
1098 public function loadListingStatInternal( $path, array $val ) {
1099 $this->cheapCache
->set( $path, 'stat', $val );
1102 protected function doGetFileSha1base36( array $params ) {
1103 $stat = $this->getFileStat( $params );
1105 if ( !isset( $stat['sha1'] ) ) {
1106 // Stat entries filled by file listings don't include SHA1
1107 $this->clearCache( array( $params['src'] ) );
1108 $stat = $this->getFileStat( $params );
1110 return $stat['sha1'];
1116 protected function doStreamFile( array $params ) {
1117 $status = Status
::newGood();
1119 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1120 if ( $srcRel === null ) {
1121 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1125 $cont = $this->getContainer( $srcCont );
1126 } catch ( NoSuchContainerException
$e ) {
1127 $status->fatal( 'backend-fail-stream', $params['src'] );
1129 } catch ( CloudFilesException
$e ) { // some other exception?
1130 $this->handleException( $e, $status, __METHOD__
, $params );
1135 $output = fopen( 'php://output', 'wb' );
1136 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1137 $obj->stream( $output, $this->headersFromParams( $params ) );
1138 } catch ( NoSuchObjectException
$e ) {
1139 $status->fatal( 'backend-fail-stream', $params['src'] );
1140 } catch ( CloudFilesException
$e ) { // some other exception?
1141 $this->handleException( $e, $status, __METHOD__
, $params );
1147 protected function doGetLocalCopyMulti( array $params ) {
1148 $tmpFiles = array();
1150 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1151 // Blindly create tmp files and stream to them, catching any exception if the file does
1152 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1153 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1154 $cfOps = array(); // (path => CF_Async_Op)
1156 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1157 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1158 if ( $srcRel === null ) {
1159 $tmpFiles[$path] = null;
1164 $sContObj = $this->getContainer( $srcCont );
1165 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1166 // Get source file extension
1167 $ext = FileBackend
::extensionFromPath( $path );
1168 // Create a new temporary file...
1169 $tmpFile = TempFSFile
::factory( 'localcopy_', $ext );
1171 $handle = fopen( $tmpFile->getPath(), 'wb' );
1173 $headers = $this->headersFromParams( $params );
1174 if ( count( $pathBatch ) > 1 ) {
1175 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1176 $cfOps[$path]->_file_handle
= $handle; // close this later
1178 $obj->stream( $handle, $headers );
1185 } catch ( NoSuchContainerException
$e ) {
1187 } catch ( NoSuchObjectException
$e ) {
1189 } catch ( CloudFilesException
$e ) { // some other exception?
1191 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1193 $tmpFiles[$path] = $tmpFile;
1196 $batch = new CF_Async_Op_Batch( $cfOps );
1197 $cfOps = $batch->execute();
1198 foreach ( $cfOps as $path => $cfOp ) {
1200 $cfOp->getLastResponse();
1201 } catch ( NoSuchContainerException
$e ) {
1202 $tmpFiles[$path] = null;
1203 } catch ( NoSuchObjectException
$e ) {
1204 $tmpFiles[$path] = null;
1205 } catch ( CloudFilesException
$e ) { // some other exception?
1206 $tmpFiles[$path] = null;
1207 $this->handleException( $e, null, __METHOD__
, array( 'src' => $path ) +
$ep );
1209 fclose( $cfOp->_file_handle
); // close open handle
1216 public function getFileHttpUrl( array $params ) {
1217 if ( $this->swiftTempUrlKey
!= '' ||
1218 ( $this->rgwS3AccessKey
!= '' && $this->rgwS3SecretKey
!= '' ) )
1220 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1221 if ( $srcRel === null ) {
1222 return null; // invalid path
1225 $ttl = isset( $params['ttl'] ) ?
$params['ttl'] : 86400;
1226 $sContObj = $this->getContainer( $srcCont );
1227 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1228 if ( $this->swiftTempUrlKey
!= '' ) {
1229 return $obj->get_temp_url( $this->swiftTempUrlKey
, $ttl, "GET" );
1230 } else { // give S3 API URL for rgw
1231 $expires = time() +
$ttl;
1232 // Path for signature starts with the bucket
1233 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1234 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1235 // Calculate the hash
1236 $signature = base64_encode( hash_hmac(
1238 "GET\n\n\n{$expires}\n{$spath}",
1239 $this->rgwS3SecretKey
,
1242 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1243 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1244 return wfAppendQuery(
1245 str_replace( '/swift/v1', '', // S3 API is the rgw default
1246 $sContObj->cfs_http
->getStorageUrl() . $spath ),
1248 'Signature' => $signature,
1249 'Expires' => $expires,
1250 'AWSAccessKeyId' => $this->rgwS3AccessKey
)
1253 } catch ( NoSuchContainerException
$e ) {
1254 } catch ( CloudFilesException
$e ) { // some other exception?
1255 $this->handleException( $e, null, __METHOD__
, $params );
1261 protected function directoriesAreVirtual() {
1266 * Get headers to send to Swift when reading a file based
1267 * on a FileBackend params array, e.g. that of getLocalCopy().
1268 * $params is currently only checked for a 'latest' flag.
1270 * @param array $params
1273 protected function headersFromParams( array $params ) {
1275 if ( !empty( $params['latest'] ) ) {
1276 $hdrs[] = 'X-Newest: true';
1281 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1282 $statuses = array();
1284 $cfOps = array(); // list of CF_Async_Op objects
1285 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1286 $cfOps[$index] = $fileOpHandle->cfOp
;
1288 $batch = new CF_Async_Op_Batch( $cfOps );
1290 $cfOps = $batch->execute();
1291 foreach ( $cfOps as $index => $cfOp ) {
1292 $status = Status
::newGood();
1293 $function = '_getResponse' . $fileOpHandles[$index]->call
;
1294 try { // catch exceptions; update status
1295 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params
);
1296 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects
);
1297 } catch ( CloudFilesException
$e ) { // some other exception?
1298 $this->handleException( $e, $status,
1299 __CLASS__
. ":$function", $fileOpHandles[$index]->params
);
1301 $statuses[$index] = $status;
1308 * Set read/write permissions for a Swift container.
1310 * $readGrps is a list of the possible criteria for a request to have
1311 * access to read a container. Each item is one of the following formats:
1312 * - account:user : Grants access if the request is by the given user
1313 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1314 * matches the expression and the request is not for a listing.
1315 * Setting this to '*' effectively makes a container public.
1316 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1317 * matches the expression and the request is for a listing.
1319 * $writeGrps is a list of the possible criteria for a request to have
1320 * access to write to a container. Each item is of the following format:
1321 * - account:user : Grants access if the request is by the given user
1323 * @see http://swift.openstack.org/misc.html#acls
1325 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1326 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1328 * @param CF_Container $contObj Swift container
1329 * @param array $readGrps List of read access routes
1330 * @param array $writeGrps List of write access routes
1333 protected function setContainerAccess(
1334 CF_Container
$contObj, array $readGrps, array $writeGrps
1336 $creds = $contObj->cfs_auth
->export_credentials();
1338 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
1340 // Note: 10 second timeout consistent with php-cloudfiles
1341 $req = MWHttpRequest
::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1342 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1343 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1344 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1346 return $req->execute(); // should return 204
1350 * Purge the CDN cache of affected objects if CDN caching is enabled.
1351 * This is for Rackspace/Akamai CDNs.
1353 * @param array $objects List of CF_Object items
1356 public function purgeCDNCache( array $objects ) {
1357 if ( $this->swiftUseCDN
&& $this->swiftCDNPurgable
) {
1358 foreach ( $objects as $object ) {
1360 $object->purge_from_cdn();
1361 } catch ( CDNNotEnabledException
$e ) {
1362 // CDN not enabled; nothing to see here
1363 } catch ( CloudFilesException
$e ) {
1364 $this->handleException( $e, null, __METHOD__
,
1365 array( 'cont' => $object->container
->name
, 'obj' => $object->name
) );
1372 * Get an authenticated connection handle to the Swift proxy
1374 * @throws CloudFilesException
1375 * @throws CloudFilesException|Exception
1376 * @return CF_Connection|bool False on failure
1378 protected function getConnection() {
1379 if ( $this->connException
instanceof CloudFilesException
) {
1380 if ( ( time() - $this->connErrorTime
) < 60 ) {
1381 throw $this->connException
; // failed last attempt; don't bother
1382 } else { // actually retry this time
1383 $this->connException
= null;
1384 $this->connErrorTime
= 0;
1387 // Session keys expire after a while, so we renew them periodically
1388 $reAuth = ( ( time() - $this->sessionStarted
) > $this->authTTL
);
1389 // Authenticate with proxy and get a session key...
1390 if ( !$this->conn ||
$reAuth ) {
1391 $this->sessionStarted
= 0;
1392 $this->connContainerCache
->clear();
1393 $cacheKey = $this->getCredsCacheKey( $this->auth
->username
);
1394 $creds = $this->srvCache
->get( $cacheKey ); // credentials
1395 if ( is_array( $creds ) ) { // cache hit
1396 $this->auth
->load_cached_credentials(
1397 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1398 $this->sessionStarted
= time() - ceil( $this->authTTL
/ 2 ); // skew for worst case
1399 } else { // cache miss
1401 $this->auth
->authenticate();
1402 $creds = $this->auth
->export_credentials();
1403 $this->srvCache
->add( $cacheKey, $creds, ceil( $this->authTTL
/ 2 ) ); // cache
1404 $this->sessionStarted
= time();
1405 } catch ( CloudFilesException
$e ) {
1406 $this->connException
= $e; // don't keep re-trying
1407 $this->connErrorTime
= time();
1408 throw $e; // throw it back
1411 if ( $this->conn
) { // re-authorizing?
1412 $this->conn
->close(); // close active cURL handles in CF_Http object
1414 $this->conn
= new CF_Connection( $this->auth
);
1420 * Close the connection to the Swift proxy
1424 protected function closeConnection() {
1425 if ( $this->conn
) {
1426 $this->conn
->close(); // close active cURL handles in CF_Http object
1428 $this->sessionStarted
= 0;
1429 $this->connContainerCache
->clear();
1434 * Get the cache key for a container
1436 * @param string $username
1439 private function getCredsCacheKey( $username ) {
1440 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1444 * Get a Swift container object, possibly from process cache.
1445 * Use $reCache if the file count or byte count is needed.
1447 * @param string $container Container name
1448 * @param bool $bypassCache Bypass all caches and load from Swift
1449 * @return CF_Container
1450 * @throws CloudFilesException
1452 protected function getContainer( $container, $bypassCache = false ) {
1453 $conn = $this->getConnection(); // Swift proxy connection
1454 if ( $bypassCache ) { // purge cache
1455 $this->connContainerCache
->clear( $container );
1456 } elseif ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1457 $this->primeContainerCache( array( $container ) ); // check persistent cache
1459 if ( !$this->connContainerCache
->has( $container, 'obj' ) ) {
1460 $contObj = $conn->get_container( $container );
1461 // NoSuchContainerException not thrown: container must exist
1462 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache it
1463 if ( !$bypassCache ) {
1464 $this->setContainerCache( $container, // update persistent cache
1465 array( 'bytes' => $contObj->bytes_used
, 'count' => $contObj->object_count
)
1469 return $this->connContainerCache
->get( $container, 'obj' );
1473 * Create a Swift container
1475 * @param string $container Container name
1476 * @return CF_Container
1477 * @throws CloudFilesException
1479 protected function createContainer( $container ) {
1480 $conn = $this->getConnection(); // Swift proxy connection
1481 $contObj = $conn->create_container( $container );
1482 $this->connContainerCache
->set( $container, 'obj', $contObj ); // cache
1487 * Delete a Swift container
1489 * @param string $container Container name
1491 * @throws CloudFilesException
1493 protected function deleteContainer( $container ) {
1494 $conn = $this->getConnection(); // Swift proxy connection
1495 $this->connContainerCache
->clear( $container ); // purge
1496 $conn->delete_container( $container );
1499 protected function doPrimeContainerCache( array $containerInfo ) {
1501 $conn = $this->getConnection(); // Swift proxy connection
1502 foreach ( $containerInfo as $container => $info ) {
1503 $contObj = new CF_Container( $conn->cfs_auth
, $conn->cfs_http
,
1504 $container, $info['count'], $info['bytes'] );
1505 $this->connContainerCache
->set( $container, 'obj', $contObj );
1507 } catch ( CloudFilesException
$e ) { // some other exception?
1508 $this->handleException( $e, null, __METHOD__
, array() );
1513 * Log an unexpected exception for this backend.
1514 * This also sets the Status object to have a fatal error.
1516 * @param Exception $e
1517 * @param Status $status|null
1518 * @param string $func
1519 * @param array $params
1522 protected function handleException( Exception
$e, $status, $func, array $params ) {
1523 if ( $status instanceof Status
) {
1524 if ( $e instanceof AuthenticationException
) {
1525 $status->fatal( 'backend-fail-connect', $this->name
);
1527 $status->fatal( 'backend-fail-internal', $this->name
);
1530 if ( $e->getMessage() ) {
1531 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING
);
1533 if ( $e instanceof InvalidResponseException
) { // possibly a stale token
1534 $this->srvCache
->delete( $this->getCredsCacheKey( $this->auth
->username
) );
1535 $this->closeConnection(); // force a re-connect and re-auth next time
1537 wfDebugLog( 'SwiftBackend',
1538 get_class( $e ) . " in '{$func}' (given '" . FormatJson
::encode( $params ) . "')" .
1539 ( $e->getMessage() ?
": {$e->getMessage()}" : "" )
1545 * @see FileBackendStoreOpHandle
1547 class SwiftFileOpHandle
extends FileBackendStoreOpHandle
{
1548 /** @var CF_Async_Op */
1551 public $affectedObjects = array();
1554 * @param SwiftFileBackend $backend
1555 * @param array $params
1556 * @param string $call
1557 * @param CF_Async_Op $cfOp
1559 public function __construct(
1560 SwiftFileBackend
$backend, array $params, $call, CF_Async_Op
$cfOp
1562 $this->backend
= $backend;
1563 $this->params
= $params;
1564 $this->call
= $call;
1565 $this->cfOp
= $cfOp;
1570 * SwiftFileBackend helper class to page through listings.
1571 * Swift also has a listing limit of 10,000 objects for sanity.
1572 * Do not use this class from places outside SwiftFileBackend.
1574 * @ingroup FileBackend
1576 abstract class SwiftFileBackendList
implements Iterator
{
1577 /** @var Array List of path or (path,stat array) entries */
1578 protected $bufferIter = array();
1579 protected $bufferAfter = null; // string; list items *after* this path
1580 protected $pos = 0; // integer
1582 protected $params = array();
1584 /** @var SwiftFileBackend */
1586 protected $container; // string; container name
1587 protected $dir; // string; storage directory
1588 protected $suffixStart; // integer
1590 const PAGE_SIZE
= 9000; // file listing buffer size
1593 * @param SwiftFileBackend $backend
1594 * @param string $fullCont Resolved container name
1595 * @param string $dir Resolved directory relative to container
1596 * @param array $params
1598 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
1599 $this->backend
= $backend;
1600 $this->container
= $fullCont;
1602 if ( substr( $this->dir
, -1 ) === '/' ) {
1603 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
1605 if ( $this->dir
== '' ) { // whole container
1606 $this->suffixStart
= 0;
1607 } else { // dir within container
1608 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
1610 $this->params
= $params;
1614 * @see Iterator::key()
1617 public function key() {
1622 * @see Iterator::next()
1625 public function next() {
1626 // Advance to the next file in the page
1627 next( $this->bufferIter
);
1629 // Check if there are no files left in this page and
1630 // advance to the next page if this page was not empty.
1631 if ( !$this->valid() && count( $this->bufferIter
) ) {
1632 $this->bufferIter
= $this->pageFromList(
1633 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1634 ); // updates $this->bufferAfter
1639 * @see Iterator::rewind()
1642 public function rewind() {
1644 $this->bufferAfter
= null;
1645 $this->bufferIter
= $this->pageFromList(
1646 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
1647 ); // updates $this->bufferAfter
1651 * @see Iterator::valid()
1654 public function valid() {
1655 if ( $this->bufferIter
=== null ) {
1656 return false; // some failure?
1658 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
1663 * Get the given list portion (page)
1665 * @param string $container Resolved container name
1666 * @param string $dir Resolved path relative to container
1667 * @param string $after|null
1668 * @param integer $limit
1669 * @param array $params
1670 * @return Traversable|Array
1672 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1676 * Iterator for listing directories
1678 class SwiftFileBackendDirList
extends SwiftFileBackendList
{
1680 * @see Iterator::current()
1681 * @return string|bool String (relative path) or false
1683 public function current() {
1684 return substr( current( $this->bufferIter
), $this->suffixStart
, -1 );
1688 * @see SwiftFileBackendList::pageFromList()
1691 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1692 return $this->backend
->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1697 * Iterator for listing regular files
1699 class SwiftFileBackendFileList
extends SwiftFileBackendList
{
1701 * @see Iterator::current()
1702 * @return string|bool String (relative path) or false
1704 public function current() {
1705 list( $path, $stat ) = current( $this->bufferIter
);
1706 $relPath = substr( $path, $this->suffixStart
);
1707 if ( is_array( $stat ) ) {
1708 $storageDir = rtrim( $this->params
['dir'], '/' );
1709 $this->backend
->loadListingStatInternal( "$storageDir/$relPath", $stat );
1715 * @see SwiftFileBackendList::pageFromList()
1718 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1719 return $this->backend
->getFileListPageInternal( $container, $dir, $after, $limit, $params );