Merge "Added "maxPartitionsTry" option to JobQueueFederated"
[lhc/web/wiklou.git] / includes / filebackend / SwiftFileBackend.php
1 <?php
2 /**
3 * OpenStack Swift based file backend.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Russ Nelson
23 * @author Aaron Schulz
24 */
25
26 /**
27 * @brief Class for an OpenStack Swift (or Ceph RGW) based file backend.
28 *
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.
32 *
33 * Status messages should avoid mentioning the Swift account name.
34 * Likewise, error suppression should be used to avoid path disclosure.
35 *
36 * @ingroup FileBackend
37 * @since 1.19
38 */
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
48
49 // Rados Gateway specific options
50 protected $rgwS3AccessKey; // string; S3 access key
51 protected $rgwS3SecretKey; // string; S3 authentication key
52
53 /** @var CF_Connection */
54 protected $conn; // Swift connection handle
55 protected $sessionStarted = 0; // integer UNIX timestamp
56
57 /** @var CloudFilesException */
58 protected $connException;
59 protected $connErrorTime = 0; // UNIX timestamp
60
61 /** @var BagOStuff */
62 protected $srvCache;
63
64 /** @var ProcessCacheLRU */
65 protected $connContainerCache; // container object cache
66
67 /**
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.
104 */
105 public function __construct( array $config ) {
106 parent::__construct( $config );
107 if ( !class_exists( 'CF_Constants' ) ) {
108 throw new MWException( 'SwiftCloudFiles extension not installed.' );
109 }
110 // Required settings
111 $this->auth = new CF_Authentication(
112 $config['swiftUser'],
113 $config['swiftKey'],
114 null, // account; unused
115 $config['swiftAuthUrl']
116 );
117 // Optional settings
118 $this->authTTL = isset( $config['swiftAuthTTL'] )
119 ? $config['swiftAuthTTL']
120 : 5 * 60; // some sane number
121 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
122 ? $config['swiftAnonUser']
123 : '';
124 $this->swiftTempUrlKey = isset( $config['swiftTempUrlKey'] )
125 ? $config['swiftTempUrlKey']
126 : '';
127 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
128 ? $config['shardViaHashLevels']
129 : '';
130 $this->swiftUseCDN = isset( $config['swiftUseCDN'] )
131 ? $config['swiftUseCDN']
132 : false;
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']
138 : true;
139 $this->rgwS3AccessKey = isset( $config['rgwS3AccessKey'] )
140 ? $config['rgwS3AccessKey']
141 : '';
142 $this->rgwS3SecretKey = isset( $config['rgwS3SecretKey'] )
143 ? $config['rgwS3SecretKey']
144 : '';
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
153 } else {
154 try { // look for APC, XCache, WinCache, ect...
155 $this->srvCache = ObjectCache::newAccelerator( array() );
156 } catch ( Exception $e ) {}
157 }
158 }
159 $this->srvCache = $this->srvCache ? $this->srvCache : new EmptyBagOStuff();
160 }
161
162 /**
163 * @see FileBackendStore::resolveContainerPath()
164 * @return null
165 */
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
171 }
172 return $relStoragePath;
173 }
174
175 public function isPathUsableInternal( $storagePath ) {
176 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
177 if ( $rel === null ) {
178 return false; // invalid
179 }
180
181 try {
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 ) );
187 }
188
189 return false;
190 }
191
192 /**
193 * @param array $headers
194 * @return array
195 */
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'] );
200 }
201 return $headers;
202 }
203
204 /**
205 * @param string $disposition Content-Disposition header value
206 * @return string Truncated Content-Disposition header value to meet Swift limits
207 */
208 protected function truncDisp( $disposition ) {
209 $res = '';
210 foreach ( explode( ';', $disposition ) as $part ) {
211 $part = trim( $part );
212 $new = ( $res === '' ) ? $part : "{$res};{$part}";
213 if ( strlen( $new ) <= 255 ) {
214 $res = $new;
215 } else {
216 break; // too long; sigh
217 }
218 }
219 return $res;
220 }
221
222 protected function doCreateInternal( array $params ) {
223 $status = Status::newGood();
224
225 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
226 if ( $dstRel === null ) {
227 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
228 return $status;
229 }
230
231 // (a) Check the destination container and object
232 try {
233 $dContObj = $this->getContainer( $dstCont );
234 } catch ( NoSuchContainerException $e ) {
235 $status->fatal( 'backend-fail-create', $params['dst'] );
236 return $status;
237 } catch ( CloudFilesException $e ) { // some other exception?
238 $this->handleException( $e, $status, __METHOD__, $params );
239 return $status;
240 }
241
242 // (b) Get a SHA-1 hash of the object
243 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
244
245 // (c) Actually create the object
246 try {
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'] );
259 }
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 ) );
267 }
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 );
274 }
275
276 return $status;
277 }
278
279 /**
280 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
281 */
282 protected function _getResponseCreate( CF_Async_Op $cfOp, Status $status, array $params ) {
283 try {
284 $cfOp->getLastResponse();
285 } catch ( BadContentTypeException $e ) {
286 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
287 }
288 }
289
290 protected function doStoreInternal( array $params ) {
291 $status = Status::newGood();
292
293 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
294 if ( $dstRel === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
296 return $status;
297 }
298
299 // (a) Check the destination container and object
300 try {
301 $dContObj = $this->getContainer( $dstCont );
302 } catch ( NoSuchContainerException $e ) {
303 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
304 return $status;
305 } catch ( CloudFilesException $e ) { // some other exception?
306 $this->handleException( $e, $status, __METHOD__, $params );
307 return $status;
308 }
309
310 // (b) Get a SHA-1 hash of the object
311 wfSuppressWarnings();
312 $sha1Hash = sha1_file( $params['src'] );
313 wfRestoreWarnings();
314 if ( $sha1Hash === false ) { // source doesn't exist?
315 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
316 return $status;
317 }
318 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
319
320 // (c) Actually store the object
321 try {
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'] );
333 }
334 if ( !empty( $params['async'] ) ) { // deferred
335 wfSuppressWarnings();
336 $fp = fopen( $params['src'], 'rb' );
337 wfRestoreWarnings();
338 if ( !$fp ) {
339 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
340 } else {
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;
345 }
346 } else { // actually write the object in Swift
347 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
348 $this->purgeCDNCache( array( $obj ) );
349 }
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 );
358 }
359
360 return $status;
361 }
362
363 /**
364 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
365 */
366 protected function _getResponseStore( CF_Async_Op $cfOp, Status $status, array $params ) {
367 try {
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'] );
373 }
374 }
375
376 protected function doCopyInternal( array $params ) {
377 $status = Status::newGood();
378
379 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
380 if ( $srcRel === null ) {
381 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
382 return $status;
383 }
384
385 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
386 if ( $dstRel === null ) {
387 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
388 return $status;
389 }
390
391 // (a) Check the source/destination containers and destination object
392 try {
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'] );
398 }
399 return $status;
400 } catch ( CloudFilesException $e ) { // some other exception?
401 $this->handleException( $e, $status, __METHOD__, $params );
402 return $status;
403 }
404
405 // (b) Actually copy the file to the destination
406 try {
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'] );
412 }
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 ) );
420 }
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'] );
426 }
427 } catch ( CloudFilesException $e ) { // some other exception?
428 $this->handleException( $e, $status, __METHOD__, $params );
429 }
430
431 return $status;
432 }
433
434 /**
435 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
436 */
437 protected function _getResponseCopy( CF_Async_Op $cfOp, Status $status, array $params ) {
438 try {
439 $cfOp->getLastResponse();
440 } catch ( NoSuchObjectException $e ) { // source object does not exist
441 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
442 }
443 }
444
445 protected function doMoveInternal( array $params ) {
446 $status = Status::newGood();
447
448 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
449 if ( $srcRel === null ) {
450 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
451 return $status;
452 }
453
454 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
455 if ( $dstRel === null ) {
456 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
457 return $status;
458 }
459
460 // (a) Check the source/destination containers and destination object
461 try {
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'] );
467 }
468 return $status;
469 } catch ( CloudFilesException $e ) { // some other exception?
470 $this->handleException( $e, $status, __METHOD__, $params );
471 return $status;
472 }
473
474 // (b) Actually move the file to the destination
475 try {
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'] );
482 }
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 ) );
492 }
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'] );
498 }
499 } catch ( CloudFilesException $e ) { // some other exception?
500 $this->handleException( $e, $status, __METHOD__, $params );
501 }
502
503 return $status;
504 }
505
506 /**
507 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
508 */
509 protected function _getResponseMove( CF_Async_Op $cfOp, Status $status, array $params ) {
510 try {
511 $cfOp->getLastResponse();
512 } catch ( NoSuchObjectException $e ) { // source object does not exist
513 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
514 }
515 }
516
517 protected function doDeleteInternal( array $params ) {
518 $status = Status::newGood();
519
520 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
521 if ( $srcRel === null ) {
522 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
523 return $status;
524 }
525
526 try {
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 ) );
536 }
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'] );
542 }
543 } catch ( NoSuchObjectException $e ) {
544 if ( empty( $params['ignoreMissingSource'] ) ) {
545 $status->fatal( 'backend-fail-delete', $params['src'] );
546 }
547 } catch ( CloudFilesException $e ) { // some other exception?
548 $this->handleException( $e, $status, __METHOD__, $params );
549 }
550
551 return $status;
552 }
553
554 /**
555 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
556 */
557 protected function _getResponseDelete( CF_Async_Op $cfOp, Status $status, array $params ) {
558 try {
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'] );
565 }
566 }
567 }
568
569 protected function doDescribeInternal( array $params ) {
570 $status = Status::newGood();
571
572 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
573 if ( $srcRel === null ) {
574 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
575 return $status;
576 }
577
578 try {
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;
586 }
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 );
597 }
598
599 return $status;
600 }
601
602 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
603 $status = Status::newGood();
604
605 // (a) Check if container already exists
606 try {
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 );
614 return $status;
615 }
616
617 // (b) Create container as needed
618 try {
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 ) );
623 } else {
624 // Make container public to end-users...
625 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
626 }
627 if ( $this->swiftUseCDN ) { // Rackspace style CDN
628 $contObj->make_public( $this->swiftCDNExpiry );
629 }
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 );
634 return $status;
635 }
636
637 return $status;
638 }
639
640 /**
641 * @see FileBackendStore::doSecureInternal()
642 * @return Status
643 */
644 protected function doSecureInternal( $fullCont, $dir, array $params ) {
645 $status = Status::newGood();
646 if ( empty( $params['noAccess'] ) ) {
647 return $status; // nothing to do
648 }
649
650 // Restrict container from end-users...
651 try {
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
656
657 // Make container private to end-users...
658 $status->merge( $this->setContainerAccess(
659 $contObj,
660 array( $this->auth->username ), // read
661 array( $this->auth->username ) // write
662 ) );
663 if ( $this->swiftUseCDN && $contObj->is_public() ) { // Rackspace style CDN
664 $contObj->make_private();
665 }
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 );
670 }
671
672 return $status;
673 }
674
675 /**
676 * @see FileBackendStore::doPublishInternal()
677 * @return Status
678 */
679 protected function doPublishInternal( $fullCont, $dir, array $params ) {
680 $status = Status::newGood();
681
682 // Unrestrict container from end-users...
683 try {
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
688
689 // Make container public to end-users...
690 if ( $this->swiftAnonUser != '' ) {
691 $status->merge( $this->setContainerAccess(
692 $contObj,
693 array( $this->auth->username, $this->swiftAnonUser ), // read
694 array( $this->auth->username, $this->swiftAnonUser ) // write
695 ) );
696 } else {
697 $status->merge( $this->setContainerAccess(
698 $contObj,
699 array( $this->auth->username, '.r:*' ), // read
700 array( $this->auth->username ) // write
701 ) );
702 }
703 if ( $this->swiftUseCDN && !$contObj->is_public() ) { // Rackspace style CDN
704 $contObj->make_public();
705 }
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 );
710 }
711
712 return $status;
713 }
714
715 protected function doCleanInternal( $fullCont, $dir, array $params ) {
716 $status = Status::newGood();
717
718 // Only containers themselves can be removed, all else is virtual
719 if ( $dir != '' ) {
720 return $status; // nothing to do
721 }
722
723 // (a) Check the container
724 try {
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 );
730 return $status;
731 }
732
733 // (b) Delete the container if empty
734 if ( $contObj->object_count == 0 ) {
735 try {
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 );
743 return $status;
744 }
745 }
746
747 return $status;
748 }
749
750 protected function doGetFileStat( array $params ) {
751 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
752 if ( $srcRel === null ) {
753 return false; // invalid storage path
754 }
755
756 $stat = false;
757 try {
758 $contObj = $this->getContainer( $srcCont );
759 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
760 $this->addMissingMetadata( $srcObj, $params['src'] );
761 $stat = array(
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' )
766 );
767 } catch ( NoSuchContainerException $e ) {
768 } catch ( NoSuchObjectException $e ) {
769 } catch ( CloudFilesException $e ) { // some other exception?
770 $stat = null;
771 $this->handleException( $e, null, __METHOD__, $params );
772 }
773
774 return $stat;
775 }
776
777 /**
778 * Fill in any missing object metadata and save it to Swift
779 *
780 * @param CF_Object $obj
781 * @param string $path Storage path to object
782 * @return bool Success
783 * @throws Exception cloudfiles exceptions
784 */
785 protected function addMissingMetadata( CF_Object $obj, $path ) {
786 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
787 return true; // nothing to do
788 }
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 ) );
795 if ( $tmpFile ) {
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
802 }
803 }
804 }
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
809 }
810
811 protected function doGetFileContentsMulti( array $params ) {
812 $contents = array();
813
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)
819
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;
824 continue;
825 }
826 $data = false;
827 try {
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' );
832 if ( $handle ) {
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
837 } else {
838 $obj->stream( $handle, $headers );
839 rewind( $handle ); // start from the beginning
840 $data = stream_get_contents( $handle );
841 fclose( $handle );
842 }
843 } else {
844 $data = false;
845 }
846 } catch ( NoSuchContainerException $e ) {
847 $data = false;
848 } catch ( NoSuchObjectException $e ) {
849 $data = false;
850 } catch ( CloudFilesException $e ) { // some other exception?
851 $data = false;
852 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
853 }
854 $contents[$path] = $data;
855 }
856
857 $batch = new CF_Async_Op_Batch( $cfOps );
858 $cfOps = $batch->execute();
859 foreach ( $cfOps as $path => $cfOp ) {
860 try {
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 );
871 }
872 fclose( $cfOp->_file_handle ); // close open handle
873 }
874 }
875
876 return $contents;
877 }
878
879 /**
880 * @see FileBackendStore::doDirectoryExists()
881 * @return bool|null
882 */
883 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
884 try {
885 $container = $this->getContainer( $fullCont );
886 $prefix = ( $dir == '' ) ? null : "{$dir}/";
887 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
888 } catch ( NoSuchContainerException $e ) {
889 return false;
890 } catch ( CloudFilesException $e ) { // some other exception?
891 $this->handleException( $e, null, __METHOD__,
892 array( 'cont' => $fullCont, 'dir' => $dir ) );
893 }
894
895 return null; // error
896 }
897
898 /**
899 * @see FileBackendStore::getDirectoryListInternal()
900 * @return SwiftFileBackendDirList
901 */
902 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
903 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
904 }
905
906 /**
907 * @see FileBackendStore::getFileListInternal()
908 * @return SwiftFileBackendFileList
909 */
910 public function getFileListInternal( $fullCont, $dir, array $params ) {
911 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
912 }
913
914 /**
915 * Do not call this function outside of SwiftFileBackendFileList
916 *
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
924 */
925 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
926 $dirs = array();
927 if ( $after === INF ) {
928 return $dirs; // nothing more
929 }
930
931 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
932 try {
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 '/'
941 }
942 }
943 // Recursive: list all dirs under $dir and its subdirs
944 } else {
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 ) {
956 $pDir = $objectDir;
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
963 );
964 }
965 $lastDir = $objectDir;
966 }
967 }
968 }
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
972 } else {
973 $after = end( $objects ); // update last item
974 }
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." );
980 }
981
982 return $dirs;
983 }
984
985 protected function getParentDir( $path ) {
986 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
987 }
988
989 /**
990 * Do not call this function outside of SwiftFileBackendFileList
991 *
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
999 */
1000 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1001 $files = array();
1002 if ( $after === INF ) {
1003 return $files; // nothing more
1004 }
1005
1006 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
1007 try {
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, '/' ) );
1017 $files = $objects;
1018 } else {
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 '/'
1023 }
1024 }
1025 }
1026 // Recursive: list all files under $dir and its subdirs
1027 } else { // files
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 ) );
1032 } else {
1033 $objects = $container->list_objects( $limit, $after, $prefix );
1034 }
1035 $files = $objects;
1036 }
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
1040 } else {
1041 $after = end( $objects ); // update last item
1042 }
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." );
1048 }
1049
1050 return $files;
1051 }
1052
1053 /**
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.
1056 *
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
1061 */
1062 private function loadObjectListing( array $params, $dir, array $cfObjects ) {
1063 $names = array();
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 );
1072 $val = array(
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
1077 );
1078 $this->cheapCache->set( $path, 'stat', $val );
1079 $names[] = $object->name;
1080 }
1081 return array_reverse( $names ); // keep the paths in original order
1082 }
1083
1084 protected function doGetFileSha1base36( array $params ) {
1085 $stat = $this->getFileStat( $params );
1086 if ( $stat ) {
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 );
1091 }
1092 return $stat['sha1'];
1093 } else {
1094 return false;
1095 }
1096 }
1097
1098 protected function doStreamFile( array $params ) {
1099 $status = Status::newGood();
1100
1101 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1102 if ( $srcRel === null ) {
1103 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1104 }
1105
1106 try {
1107 $cont = $this->getContainer( $srcCont );
1108 } catch ( NoSuchContainerException $e ) {
1109 $status->fatal( 'backend-fail-stream', $params['src'] );
1110 return $status;
1111 } catch ( CloudFilesException $e ) { // some other exception?
1112 $this->handleException( $e, $status, __METHOD__, $params );
1113 return $status;
1114 }
1115
1116 try {
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 );
1124 }
1125
1126 return $status;
1127 }
1128
1129 protected function doGetLocalCopyMulti( array $params ) {
1130 $tmpFiles = array();
1131
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)
1137
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;
1142 continue;
1143 }
1144 $tmpFile = null;
1145 try {
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 );
1152 if ( $tmpFile ) {
1153 $handle = fopen( $tmpFile->getPath(), 'wb' );
1154 if ( $handle ) {
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
1159 } else {
1160 $obj->stream( $handle, $headers );
1161 fclose( $handle );
1162 }
1163 } else {
1164 $tmpFile = null;
1165 }
1166 }
1167 } catch ( NoSuchContainerException $e ) {
1168 $tmpFile = null;
1169 } catch ( NoSuchObjectException $e ) {
1170 $tmpFile = null;
1171 } catch ( CloudFilesException $e ) { // some other exception?
1172 $tmpFile = null;
1173 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1174 }
1175 $tmpFiles[$path] = $tmpFile;
1176 }
1177
1178 $batch = new CF_Async_Op_Batch( $cfOps );
1179 $cfOps = $batch->execute();
1180 foreach ( $cfOps as $path => $cfOp ) {
1181 try {
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 );
1190 }
1191 fclose( $cfOp->_file_handle ); // close open handle
1192 }
1193 }
1194
1195 return $tmpFiles;
1196 }
1197
1198 public function getFileHttpUrl( array $params ) {
1199 if ( $this->swiftTempUrlKey != '' ||
1200 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' ) )
1201 {
1202 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1203 if ( $srcRel === null ) {
1204 return null; // invalid path
1205 }
1206 try {
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(
1219 'sha1',
1220 "GET\n\n\n{$expires}\n{$spath}",
1221 $this->rgwS3SecretKey,
1222 true // raw
1223 ) );
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 ),
1229 array(
1230 'Signature' => $signature,
1231 'Expires' => $expires,
1232 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1233 );
1234 }
1235 } catch ( NoSuchContainerException $e ) {
1236 } catch ( CloudFilesException $e ) { // some other exception?
1237 $this->handleException( $e, null, __METHOD__, $params );
1238 }
1239 }
1240 return null;
1241 }
1242
1243 protected function directoriesAreVirtual() {
1244 return true;
1245 }
1246
1247 /**
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.
1251 *
1252 * @param array $params
1253 * @return Array
1254 */
1255 protected function headersFromParams( array $params ) {
1256 $hdrs = array();
1257 if ( !empty( $params['latest'] ) ) {
1258 $hdrs[] = 'X-Newest: true';
1259 }
1260 return $hdrs;
1261 }
1262
1263 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1264 $statuses = array();
1265
1266 $cfOps = array(); // list of CF_Async_Op objects
1267 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1268 $cfOps[$index] = $fileOpHandle->cfOp;
1269 }
1270 $batch = new CF_Async_Op_Batch( $cfOps );
1271
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 );
1282 }
1283 $statuses[$index] = $status;
1284 }
1285
1286 return $statuses;
1287 }
1288
1289 /**
1290 * Set read/write permissions for a Swift container.
1291 *
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.
1300 *
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
1304 *
1305 * @see http://swift.openstack.org/misc.html#acls
1306 *
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.
1309 *
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
1313 * @return Status
1314 */
1315 protected function setContainerAccess(
1316 CF_Container $contObj, array $readGrps, array $writeGrps
1317 ) {
1318 $creds = $contObj->cfs_auth->export_credentials();
1319
1320 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1321
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 ) );
1327
1328 return $req->execute(); // should return 204
1329 }
1330
1331 /**
1332 * Purge the CDN cache of affected objects if CDN caching is enabled.
1333 * This is for Rackspace/Akamai CDNs.
1334 *
1335 * @param array $objects List of CF_Object items
1336 * @return void
1337 */
1338 public function purgeCDNCache( array $objects ) {
1339 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1340 foreach ( $objects as $object ) {
1341 try {
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 ) );
1348 }
1349 }
1350 }
1351 }
1352
1353 /**
1354 * Get an authenticated connection handle to the Swift proxy
1355 *
1356 * @throws CloudFilesException
1357 * @throws CloudFilesException|Exception
1358 * @return CF_Connection|bool False on failure
1359 */
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;
1367 }
1368 }
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
1382 try {
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
1391 }
1392 }
1393 if ( $this->conn ) { // re-authorizing?
1394 $this->conn->close(); // close active cURL handles in CF_Http object
1395 }
1396 $this->conn = new CF_Connection( $this->auth );
1397 }
1398 return $this->conn;
1399 }
1400
1401 /**
1402 * Close the connection to the Swift proxy
1403 *
1404 * @return void
1405 */
1406 protected function closeConnection() {
1407 if ( $this->conn ) {
1408 $this->conn->close(); // close active cURL handles in CF_Http object
1409 $this->conn = null;
1410 $this->sessionStarted = 0;
1411 $this->connContainerCache->clear();
1412 }
1413 }
1414
1415 /**
1416 * Get the cache key for a container
1417 *
1418 * @param string $username
1419 * @return string
1420 */
1421 private function getCredsCacheKey( $username ) {
1422 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1423 }
1424
1425 /**
1426 * Get a Swift container object, possibly from process cache.
1427 * Use $reCache if the file count or byte count is needed.
1428 *
1429 * @param string $container Container name
1430 * @param bool $bypassCache Bypass all caches and load from Swift
1431 * @return CF_Container
1432 * @throws CloudFilesException
1433 */
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
1440 }
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 )
1448 );
1449 }
1450 }
1451 return $this->connContainerCache->get( $container, 'obj' );
1452 }
1453
1454 /**
1455 * Create a Swift container
1456 *
1457 * @param string $container Container name
1458 * @return CF_Container
1459 * @throws CloudFilesException
1460 */
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
1465 return $contObj;
1466 }
1467
1468 /**
1469 * Delete a Swift container
1470 *
1471 * @param string $container Container name
1472 * @return void
1473 * @throws CloudFilesException
1474 */
1475 protected function deleteContainer( $container ) {
1476 $conn = $this->getConnection(); // Swift proxy connection
1477 $this->connContainerCache->clear( $container ); // purge
1478 $conn->delete_container( $container );
1479 }
1480
1481 protected function doPrimeContainerCache( array $containerInfo ) {
1482 try {
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 );
1488 }
1489 } catch ( CloudFilesException $e ) { // some other exception?
1490 $this->handleException( $e, null, __METHOD__, array() );
1491 }
1492 }
1493
1494 /**
1495 * Log an unexpected exception for this backend.
1496 * This also sets the Status object to have a fatal error.
1497 *
1498 * @param Exception $e
1499 * @param Status $status|null
1500 * @param string $func
1501 * @param array $params
1502 * @return void
1503 */
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 );
1508 } else {
1509 $status->fatal( 'backend-fail-internal', $this->name );
1510 }
1511 }
1512 if ( $e->getMessage() ) {
1513 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1514 }
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
1518 }
1519 wfDebugLog( 'SwiftBackend',
1520 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1521 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1522 );
1523 }
1524 }
1525
1526 /**
1527 * @see FileBackendStoreOpHandle
1528 */
1529 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1530 /** @var CF_Async_Op */
1531 public $cfOp;
1532 /** @var Array */
1533 public $affectedObjects = array();
1534
1535 /**
1536 * @param SwiftFileBackend $backend
1537 * @param array $params
1538 * @param string $call
1539 * @param CF_Async_Op $cfOp
1540 */
1541 public function __construct(
1542 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1543 ) {
1544 $this->backend = $backend;
1545 $this->params = $params;
1546 $this->call = $call;
1547 $this->cfOp = $cfOp;
1548 }
1549 }
1550
1551 /**
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.
1555 *
1556 * @ingroup FileBackend
1557 */
1558 abstract class SwiftFileBackendList implements Iterator {
1559 /** @var Array */
1560 protected $bufferIter = array();
1561 protected $bufferAfter = null; // string; list items *after* this path
1562 protected $pos = 0; // integer
1563 /** @var Array */
1564 protected $params = array();
1565
1566 /** @var SwiftFileBackend */
1567 protected $backend;
1568 protected $container; // string; container name
1569 protected $dir; // string; storage directory
1570 protected $suffixStart; // integer
1571
1572 const PAGE_SIZE = 9000; // file listing buffer size
1573
1574 /**
1575 * @param SwiftFileBackend $backend
1576 * @param string $fullCont Resolved container name
1577 * @param string $dir Resolved directory relative to container
1578 * @param array $params
1579 */
1580 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1581 $this->backend = $backend;
1582 $this->container = $fullCont;
1583 $this->dir = $dir;
1584 if ( substr( $this->dir, -1 ) === '/' ) {
1585 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1586 }
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/"
1591 }
1592 $this->params = $params;
1593 }
1594
1595 /**
1596 * @see Iterator::key()
1597 * @return integer
1598 */
1599 public function key() {
1600 return $this->pos;
1601 }
1602
1603 /**
1604 * @see Iterator::next()
1605 * @return void
1606 */
1607 public function next() {
1608 // Advance to the next file in the page
1609 next( $this->bufferIter );
1610 ++$this->pos;
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
1617 }
1618 }
1619
1620 /**
1621 * @see Iterator::rewind()
1622 * @return void
1623 */
1624 public function rewind() {
1625 $this->pos = 0;
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
1630 }
1631
1632 /**
1633 * @see Iterator::valid()
1634 * @return bool
1635 */
1636 public function valid() {
1637 if ( $this->bufferIter === null ) {
1638 return false; // some failure?
1639 } else {
1640 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1641 }
1642 }
1643
1644 /**
1645 * Get the given list portion (page)
1646 *
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
1653 */
1654 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1655 }
1656
1657 /**
1658 * Iterator for listing directories
1659 */
1660 class SwiftFileBackendDirList extends SwiftFileBackendList {
1661 /**
1662 * @see Iterator::current()
1663 * @return string|bool String (relative path) or false
1664 */
1665 public function current() {
1666 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1667 }
1668
1669 /**
1670 * @see SwiftFileBackendList::pageFromList()
1671 * @return Array
1672 */
1673 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1674 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1675 }
1676 }
1677
1678 /**
1679 * Iterator for listing regular files
1680 */
1681 class SwiftFileBackendFileList extends SwiftFileBackendList {
1682 /**
1683 * @see Iterator::current()
1684 * @return string|bool String (relative path) or false
1685 */
1686 public function current() {
1687 return substr( current( $this->bufferIter ), $this->suffixStart );
1688 }
1689
1690 /**
1691 * @see SwiftFileBackendList::pageFromList()
1692 * @return Array
1693 */
1694 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1695 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1696 }
1697 }