Merge "SpecialRecentChanges: Remove '@todo Uses radio buttons (HASHAR)'"
[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 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' )
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 * 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).
781 *
782 * @param string $ts
783 * @param int $format Output format (TS_* constant)
784 * @return string
785 * @throws MWException
786 */
787 protected function convertSwiftDate( $ts, $format = TS_MW ) {
788 $timestamp = new MWTimestamp( $ts );
789 return $timestamp->getTimestamp( $format );
790 }
791
792 /**
793 * Fill in any missing object metadata and save it to Swift
794 *
795 * @param CF_Object $obj
796 * @param string $path Storage path to object
797 * @return bool Success
798 * @throws Exception cloudfiles exceptions
799 */
800 protected function addMissingMetadata( CF_Object $obj, $path ) {
801 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
802 return true; // nothing to do
803 }
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 ) );
810 if ( $tmpFile ) {
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
817 }
818 }
819 }
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
824 }
825
826 protected function doGetFileContentsMulti( array $params ) {
827 $contents = array();
828
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)
834
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;
839 continue;
840 }
841 $data = false;
842 try {
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' );
847 if ( $handle ) {
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
852 } else {
853 $obj->stream( $handle, $headers );
854 rewind( $handle ); // start from the beginning
855 $data = stream_get_contents( $handle );
856 fclose( $handle );
857 }
858 } else {
859 $data = false;
860 }
861 } catch ( NoSuchContainerException $e ) {
862 $data = false;
863 } catch ( NoSuchObjectException $e ) {
864 $data = false;
865 } catch ( CloudFilesException $e ) { // some other exception?
866 $data = false;
867 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
868 }
869 $contents[$path] = $data;
870 }
871
872 $batch = new CF_Async_Op_Batch( $cfOps );
873 $cfOps = $batch->execute();
874 foreach ( $cfOps as $path => $cfOp ) {
875 try {
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 );
886 }
887 fclose( $cfOp->_file_handle ); // close open handle
888 }
889 }
890
891 return $contents;
892 }
893
894 /**
895 * @see FileBackendStore::doDirectoryExists()
896 * @return bool|null
897 */
898 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
899 try {
900 $container = $this->getContainer( $fullCont );
901 $prefix = ( $dir == '' ) ? null : "{$dir}/";
902 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
903 } catch ( NoSuchContainerException $e ) {
904 return false;
905 } catch ( CloudFilesException $e ) { // some other exception?
906 $this->handleException( $e, null, __METHOD__,
907 array( 'cont' => $fullCont, 'dir' => $dir ) );
908 }
909
910 return null; // error
911 }
912
913 /**
914 * @see FileBackendStore::getDirectoryListInternal()
915 * @return SwiftFileBackendDirList
916 */
917 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
918 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
919 }
920
921 /**
922 * @see FileBackendStore::getFileListInternal()
923 * @return SwiftFileBackendFileList
924 */
925 public function getFileListInternal( $fullCont, $dir, array $params ) {
926 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
927 }
928
929 /**
930 * Do not call this function outside of SwiftFileBackendFileList
931 *
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
939 */
940 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
941 $dirs = array();
942 if ( $after === INF ) {
943 return $dirs; // nothing more
944 }
945
946 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
947 try {
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 '/'
956 }
957 }
958 // Recursive: list all dirs under $dir and its subdirs
959 } else {
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 ) {
971 $pDir = $objectDir;
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
978 );
979 }
980 $lastDir = $objectDir;
981 }
982 }
983 }
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
987 } else {
988 $after = end( $objects ); // update last item
989 }
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." );
995 }
996
997 return $dirs;
998 }
999
1000 protected function getParentDir( $path ) {
1001 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
1002 }
1003
1004 /**
1005 * Do not call this function outside of SwiftFileBackendFileList
1006 *
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
1014 */
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
1019 }
1020
1021 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
1022 try {
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, '/' );
1031 } else {
1032 // Note: list_objects() includes directories here
1033 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1034 }
1035 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1036 // Recursive: list all files under $dir and its subdirs
1037 } else {
1038 // Note: get_objects()/list_objects() here only return file objects
1039 if ( !empty( $params['adviseStat'] ) ) {
1040 $objects = $container->get_objects( $limit, $after, $prefix );
1041 } else {
1042 $objects = $container->list_objects( $limit, $after, $prefix );
1043 }
1044 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1045 }
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
1049 } else {
1050 $after = end( $objects ); // update last item
1051 $after = is_object( $after ) ? $after->name : $after;
1052 }
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." );
1058 }
1059
1060 return $files;
1061 }
1062
1063 /**
1064 * Build a list of file objects, filtering out any directories
1065 * and extracting any stat info if provided in $objects (for CF_Objects)
1066 *
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
1071 */
1072 private function buildFileObjectListing( array $params, $dir, array $objects ) {
1073 $names = array();
1074 foreach ( $objects as $object ) {
1075 if ( is_object( $object ) ) {
1076 $stat = array(
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
1081 );
1082 $names[] = array( $object->name, $stat );
1083 } elseif ( substr( $object, -1 ) !== '/' ) {
1084 // Omit directories, which end in '/' in listings
1085 $names[] = array( $object, null );
1086 }
1087 }
1088 return $names;
1089 }
1090
1091 /**
1092 * Do not call this function outside of SwiftFileBackendFileList
1093 *
1094 * @param string $path Storage path
1095 * @param array $val Stat value
1096 * @return void
1097 */
1098 public function loadListingStatInternal( $path, array $val ) {
1099 $this->cheapCache->set( $path, 'stat', $val );
1100 }
1101
1102 protected function doGetFileSha1base36( array $params ) {
1103 $stat = $this->getFileStat( $params );
1104 if ( $stat ) {
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 );
1109 }
1110 return $stat['sha1'];
1111 } else {
1112 return false;
1113 }
1114 }
1115
1116 protected function doStreamFile( array $params ) {
1117 $status = Status::newGood();
1118
1119 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1120 if ( $srcRel === null ) {
1121 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1122 }
1123
1124 try {
1125 $cont = $this->getContainer( $srcCont );
1126 } catch ( NoSuchContainerException $e ) {
1127 $status->fatal( 'backend-fail-stream', $params['src'] );
1128 return $status;
1129 } catch ( CloudFilesException $e ) { // some other exception?
1130 $this->handleException( $e, $status, __METHOD__, $params );
1131 return $status;
1132 }
1133
1134 try {
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 );
1142 }
1143
1144 return $status;
1145 }
1146
1147 protected function doGetLocalCopyMulti( array $params ) {
1148 $tmpFiles = array();
1149
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)
1155
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;
1160 continue;
1161 }
1162 $tmpFile = null;
1163 try {
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 );
1170 if ( $tmpFile ) {
1171 $handle = fopen( $tmpFile->getPath(), 'wb' );
1172 if ( $handle ) {
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
1177 } else {
1178 $obj->stream( $handle, $headers );
1179 fclose( $handle );
1180 }
1181 } else {
1182 $tmpFile = null;
1183 }
1184 }
1185 } catch ( NoSuchContainerException $e ) {
1186 $tmpFile = null;
1187 } catch ( NoSuchObjectException $e ) {
1188 $tmpFile = null;
1189 } catch ( CloudFilesException $e ) { // some other exception?
1190 $tmpFile = null;
1191 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1192 }
1193 $tmpFiles[$path] = $tmpFile;
1194 }
1195
1196 $batch = new CF_Async_Op_Batch( $cfOps );
1197 $cfOps = $batch->execute();
1198 foreach ( $cfOps as $path => $cfOp ) {
1199 try {
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 );
1208 }
1209 fclose( $cfOp->_file_handle ); // close open handle
1210 }
1211 }
1212
1213 return $tmpFiles;
1214 }
1215
1216 public function getFileHttpUrl( array $params ) {
1217 if ( $this->swiftTempUrlKey != '' ||
1218 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' ) )
1219 {
1220 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1221 if ( $srcRel === null ) {
1222 return null; // invalid path
1223 }
1224 try {
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(
1237 'sha1',
1238 "GET\n\n\n{$expires}\n{$spath}",
1239 $this->rgwS3SecretKey,
1240 true // raw
1241 ) );
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 ),
1247 array(
1248 'Signature' => $signature,
1249 'Expires' => $expires,
1250 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1251 );
1252 }
1253 } catch ( NoSuchContainerException $e ) {
1254 } catch ( CloudFilesException $e ) { // some other exception?
1255 $this->handleException( $e, null, __METHOD__, $params );
1256 }
1257 }
1258 return null;
1259 }
1260
1261 protected function directoriesAreVirtual() {
1262 return true;
1263 }
1264
1265 /**
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.
1269 *
1270 * @param array $params
1271 * @return Array
1272 */
1273 protected function headersFromParams( array $params ) {
1274 $hdrs = array();
1275 if ( !empty( $params['latest'] ) ) {
1276 $hdrs[] = 'X-Newest: true';
1277 }
1278 return $hdrs;
1279 }
1280
1281 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1282 $statuses = array();
1283
1284 $cfOps = array(); // list of CF_Async_Op objects
1285 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1286 $cfOps[$index] = $fileOpHandle->cfOp;
1287 }
1288 $batch = new CF_Async_Op_Batch( $cfOps );
1289
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 );
1300 }
1301 $statuses[$index] = $status;
1302 }
1303
1304 return $statuses;
1305 }
1306
1307 /**
1308 * Set read/write permissions for a Swift container.
1309 *
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.
1318 *
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
1322 *
1323 * @see http://swift.openstack.org/misc.html#acls
1324 *
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.
1327 *
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
1331 * @return Status
1332 */
1333 protected function setContainerAccess(
1334 CF_Container $contObj, array $readGrps, array $writeGrps
1335 ) {
1336 $creds = $contObj->cfs_auth->export_credentials();
1337
1338 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1339
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 ) );
1345
1346 return $req->execute(); // should return 204
1347 }
1348
1349 /**
1350 * Purge the CDN cache of affected objects if CDN caching is enabled.
1351 * This is for Rackspace/Akamai CDNs.
1352 *
1353 * @param array $objects List of CF_Object items
1354 * @return void
1355 */
1356 public function purgeCDNCache( array $objects ) {
1357 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1358 foreach ( $objects as $object ) {
1359 try {
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 ) );
1366 }
1367 }
1368 }
1369 }
1370
1371 /**
1372 * Get an authenticated connection handle to the Swift proxy
1373 *
1374 * @throws CloudFilesException
1375 * @throws CloudFilesException|Exception
1376 * @return CF_Connection|bool False on failure
1377 */
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;
1385 }
1386 }
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
1400 try {
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
1409 }
1410 }
1411 if ( $this->conn ) { // re-authorizing?
1412 $this->conn->close(); // close active cURL handles in CF_Http object
1413 }
1414 $this->conn = new CF_Connection( $this->auth );
1415 }
1416 return $this->conn;
1417 }
1418
1419 /**
1420 * Close the connection to the Swift proxy
1421 *
1422 * @return void
1423 */
1424 protected function closeConnection() {
1425 if ( $this->conn ) {
1426 $this->conn->close(); // close active cURL handles in CF_Http object
1427 $this->conn = null;
1428 $this->sessionStarted = 0;
1429 $this->connContainerCache->clear();
1430 }
1431 }
1432
1433 /**
1434 * Get the cache key for a container
1435 *
1436 * @param string $username
1437 * @return string
1438 */
1439 private function getCredsCacheKey( $username ) {
1440 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1441 }
1442
1443 /**
1444 * Get a Swift container object, possibly from process cache.
1445 * Use $reCache if the file count or byte count is needed.
1446 *
1447 * @param string $container Container name
1448 * @param bool $bypassCache Bypass all caches and load from Swift
1449 * @return CF_Container
1450 * @throws CloudFilesException
1451 */
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
1458 }
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 )
1466 );
1467 }
1468 }
1469 return $this->connContainerCache->get( $container, 'obj' );
1470 }
1471
1472 /**
1473 * Create a Swift container
1474 *
1475 * @param string $container Container name
1476 * @return CF_Container
1477 * @throws CloudFilesException
1478 */
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
1483 return $contObj;
1484 }
1485
1486 /**
1487 * Delete a Swift container
1488 *
1489 * @param string $container Container name
1490 * @return void
1491 * @throws CloudFilesException
1492 */
1493 protected function deleteContainer( $container ) {
1494 $conn = $this->getConnection(); // Swift proxy connection
1495 $this->connContainerCache->clear( $container ); // purge
1496 $conn->delete_container( $container );
1497 }
1498
1499 protected function doPrimeContainerCache( array $containerInfo ) {
1500 try {
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 );
1506 }
1507 } catch ( CloudFilesException $e ) { // some other exception?
1508 $this->handleException( $e, null, __METHOD__, array() );
1509 }
1510 }
1511
1512 /**
1513 * Log an unexpected exception for this backend.
1514 * This also sets the Status object to have a fatal error.
1515 *
1516 * @param Exception $e
1517 * @param Status $status|null
1518 * @param string $func
1519 * @param array $params
1520 * @return void
1521 */
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 );
1526 } else {
1527 $status->fatal( 'backend-fail-internal', $this->name );
1528 }
1529 }
1530 if ( $e->getMessage() ) {
1531 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1532 }
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
1536 }
1537 wfDebugLog( 'SwiftBackend',
1538 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1539 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1540 );
1541 }
1542 }
1543
1544 /**
1545 * @see FileBackendStoreOpHandle
1546 */
1547 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1548 /** @var CF_Async_Op */
1549 public $cfOp;
1550 /** @var Array */
1551 public $affectedObjects = array();
1552
1553 /**
1554 * @param SwiftFileBackend $backend
1555 * @param array $params
1556 * @param string $call
1557 * @param CF_Async_Op $cfOp
1558 */
1559 public function __construct(
1560 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1561 ) {
1562 $this->backend = $backend;
1563 $this->params = $params;
1564 $this->call = $call;
1565 $this->cfOp = $cfOp;
1566 }
1567 }
1568
1569 /**
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.
1573 *
1574 * @ingroup FileBackend
1575 */
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
1581 /** @var Array */
1582 protected $params = array();
1583
1584 /** @var SwiftFileBackend */
1585 protected $backend;
1586 protected $container; // string; container name
1587 protected $dir; // string; storage directory
1588 protected $suffixStart; // integer
1589
1590 const PAGE_SIZE = 9000; // file listing buffer size
1591
1592 /**
1593 * @param SwiftFileBackend $backend
1594 * @param string $fullCont Resolved container name
1595 * @param string $dir Resolved directory relative to container
1596 * @param array $params
1597 */
1598 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1599 $this->backend = $backend;
1600 $this->container = $fullCont;
1601 $this->dir = $dir;
1602 if ( substr( $this->dir, -1 ) === '/' ) {
1603 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1604 }
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/"
1609 }
1610 $this->params = $params;
1611 }
1612
1613 /**
1614 * @see Iterator::key()
1615 * @return integer
1616 */
1617 public function key() {
1618 return $this->pos;
1619 }
1620
1621 /**
1622 * @see Iterator::next()
1623 * @return void
1624 */
1625 public function next() {
1626 // Advance to the next file in the page
1627 next( $this->bufferIter );
1628 ++$this->pos;
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
1635 }
1636 }
1637
1638 /**
1639 * @see Iterator::rewind()
1640 * @return void
1641 */
1642 public function rewind() {
1643 $this->pos = 0;
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
1648 }
1649
1650 /**
1651 * @see Iterator::valid()
1652 * @return bool
1653 */
1654 public function valid() {
1655 if ( $this->bufferIter === null ) {
1656 return false; // some failure?
1657 } else {
1658 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1659 }
1660 }
1661
1662 /**
1663 * Get the given list portion (page)
1664 *
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
1671 */
1672 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1673 }
1674
1675 /**
1676 * Iterator for listing directories
1677 */
1678 class SwiftFileBackendDirList extends SwiftFileBackendList {
1679 /**
1680 * @see Iterator::current()
1681 * @return string|bool String (relative path) or false
1682 */
1683 public function current() {
1684 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1685 }
1686
1687 /**
1688 * @see SwiftFileBackendList::pageFromList()
1689 * @return Array
1690 */
1691 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1692 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1693 }
1694 }
1695
1696 /**
1697 * Iterator for listing regular files
1698 */
1699 class SwiftFileBackendFileList extends SwiftFileBackendList {
1700 /**
1701 * @see Iterator::current()
1702 * @return string|bool String (relative path) or false
1703 */
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 );
1710 }
1711 return $relPath;
1712 }
1713
1714 /**
1715 * @see SwiftFileBackendList::pageFromList()
1716 * @return Array
1717 */
1718 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1719 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1720 }
1721 }