Merge "mw.Map: add ability to map over an existing object other than 'window'"
[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 Storage path of file 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 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 Storage 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 paths of files under $dir
1013 * @throws FileBackendError
1014 */
1015 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1016 $files = array();
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 // Non-recursive: only list files right under $dir
1026 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1027 if ( !empty( $params['adviseStat'] ) ) {
1028 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1029 // Note: get_objects() does not include directories
1030 $objects = $this->loadObjectListing( $params, $dir,
1031 $container->get_objects( $limit, $after, $prefix, null, '/' ) );
1032 $files = $objects;
1033 } else {
1034 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1035 foreach ( $objects as $object ) { // files and directories
1036 if ( substr( $object, -1 ) !== '/' ) {
1037 $files[] = $object; // directories end in '/'
1038 }
1039 }
1040 }
1041 // Recursive: list all files under $dir and its subdirs
1042 } else { // files
1043 if ( !empty( $params['adviseStat'] ) ) {
1044 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1045 $objects = $this->loadObjectListing( $params, $dir,
1046 $container->get_objects( $limit, $after, $prefix ) );
1047 } else {
1048 $objects = $container->list_objects( $limit, $after, $prefix );
1049 }
1050 $files = $objects;
1051 }
1052 // Page on the unfiltered object listing (what is returned may be filtered)
1053 if ( count( $objects ) < $limit ) {
1054 $after = INF; // avoid a second RTT
1055 } else {
1056 $after = end( $objects ); // update last item
1057 }
1058 } catch ( NoSuchContainerException $e ) {
1059 } catch ( CloudFilesException $e ) { // some other exception?
1060 $this->handleException( $e, null, __METHOD__,
1061 array( 'cont' => $fullCont, 'dir' => $dir ) );
1062 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1063 }
1064
1065 return $files;
1066 }
1067
1068 /**
1069 * Load a list of objects that belong under $dir into stat cache
1070 * and return a list of the names of the objects in the same order.
1071 *
1072 * @param array $params Parameters for getDirectoryList()
1073 * @param string $dir Resolved container directory path
1074 * @param array $cfObjects List of CF_Object items
1075 * @return array List of object names
1076 */
1077 private function loadObjectListing( array $params, $dir, array $cfObjects ) {
1078 $names = array();
1079 $storageDir = rtrim( $params['dir'], '/' );
1080 $suffixStart = ( $dir === '' ) ? 0 : strlen( $dir ) + 1; // size of "path/to/dir/"
1081 // Iterate over the list *backwards* as this primes the stat cache, which is LRU.
1082 // If this fills the cache and the caller stats an uncached file before stating
1083 // the ones on the listing, there would be zero cache hits if this went forwards.
1084 for ( end( $cfObjects ); key( $cfObjects ) !== null; prev( $cfObjects ) ) {
1085 $object = current( $cfObjects );
1086 $path = "{$storageDir}/" . substr( $object->name, $suffixStart );
1087 $val = array(
1088 // Convert various random Swift dates to TS_MW
1089 'mtime' => $this->convertSwiftDate( $object->last_modified, TS_MW ),
1090 'size' => (int)$object->content_length,
1091 'latest' => false // eventually consistent
1092 );
1093 $this->cheapCache->set( $path, 'stat', $val );
1094 $names[] = $object->name;
1095 }
1096 return array_reverse( $names ); // keep the paths in original order
1097 }
1098
1099 protected function doGetFileSha1base36( array $params ) {
1100 $stat = $this->getFileStat( $params );
1101 if ( $stat ) {
1102 if ( !isset( $stat['sha1'] ) ) {
1103 // Stat entries filled by file listings don't include SHA1
1104 $this->clearCache( array( $params['src'] ) );
1105 $stat = $this->getFileStat( $params );
1106 }
1107 return $stat['sha1'];
1108 } else {
1109 return false;
1110 }
1111 }
1112
1113 protected function doStreamFile( array $params ) {
1114 $status = Status::newGood();
1115
1116 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1117 if ( $srcRel === null ) {
1118 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1119 }
1120
1121 try {
1122 $cont = $this->getContainer( $srcCont );
1123 } catch ( NoSuchContainerException $e ) {
1124 $status->fatal( 'backend-fail-stream', $params['src'] );
1125 return $status;
1126 } catch ( CloudFilesException $e ) { // some other exception?
1127 $this->handleException( $e, $status, __METHOD__, $params );
1128 return $status;
1129 }
1130
1131 try {
1132 $output = fopen( 'php://output', 'wb' );
1133 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1134 $obj->stream( $output, $this->headersFromParams( $params ) );
1135 } catch ( NoSuchObjectException $e ) {
1136 $status->fatal( 'backend-fail-stream', $params['src'] );
1137 } catch ( CloudFilesException $e ) { // some other exception?
1138 $this->handleException( $e, $status, __METHOD__, $params );
1139 }
1140
1141 return $status;
1142 }
1143
1144 protected function doGetLocalCopyMulti( array $params ) {
1145 $tmpFiles = array();
1146
1147 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1148 // Blindly create tmp files and stream to them, catching any exception if the file does
1149 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1150 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1151 $cfOps = array(); // (path => CF_Async_Op)
1152
1153 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1154 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1155 if ( $srcRel === null ) {
1156 $tmpFiles[$path] = null;
1157 continue;
1158 }
1159 $tmpFile = null;
1160 try {
1161 $sContObj = $this->getContainer( $srcCont );
1162 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1163 // Get source file extension
1164 $ext = FileBackend::extensionFromPath( $path );
1165 // Create a new temporary file...
1166 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
1167 if ( $tmpFile ) {
1168 $handle = fopen( $tmpFile->getPath(), 'wb' );
1169 if ( $handle ) {
1170 $headers = $this->headersFromParams( $params );
1171 if ( count( $pathBatch ) > 1 ) {
1172 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1173 $cfOps[$path]->_file_handle = $handle; // close this later
1174 } else {
1175 $obj->stream( $handle, $headers );
1176 fclose( $handle );
1177 }
1178 } else {
1179 $tmpFile = null;
1180 }
1181 }
1182 } catch ( NoSuchContainerException $e ) {
1183 $tmpFile = null;
1184 } catch ( NoSuchObjectException $e ) {
1185 $tmpFile = null;
1186 } catch ( CloudFilesException $e ) { // some other exception?
1187 $tmpFile = null;
1188 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1189 }
1190 $tmpFiles[$path] = $tmpFile;
1191 }
1192
1193 $batch = new CF_Async_Op_Batch( $cfOps );
1194 $cfOps = $batch->execute();
1195 foreach ( $cfOps as $path => $cfOp ) {
1196 try {
1197 $cfOp->getLastResponse();
1198 } catch ( NoSuchContainerException $e ) {
1199 $tmpFiles[$path] = null;
1200 } catch ( NoSuchObjectException $e ) {
1201 $tmpFiles[$path] = null;
1202 } catch ( CloudFilesException $e ) { // some other exception?
1203 $tmpFiles[$path] = null;
1204 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1205 }
1206 fclose( $cfOp->_file_handle ); // close open handle
1207 }
1208 }
1209
1210 return $tmpFiles;
1211 }
1212
1213 public function getFileHttpUrl( array $params ) {
1214 if ( $this->swiftTempUrlKey != '' ||
1215 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' ) )
1216 {
1217 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1218 if ( $srcRel === null ) {
1219 return null; // invalid path
1220 }
1221 try {
1222 $ttl = isset( $params['ttl'] ) ? $params['ttl'] : 86400;
1223 $sContObj = $this->getContainer( $srcCont );
1224 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1225 if ( $this->swiftTempUrlKey != '' ) {
1226 return $obj->get_temp_url( $this->swiftTempUrlKey, $ttl, "GET" );
1227 } else { // give S3 API URL for rgw
1228 $expires = time() + $ttl;
1229 // Path for signature starts with the bucket
1230 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1231 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1232 // Calculate the hash
1233 $signature = base64_encode( hash_hmac(
1234 'sha1',
1235 "GET\n\n\n{$expires}\n{$spath}",
1236 $this->rgwS3SecretKey,
1237 true // raw
1238 ) );
1239 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1240 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1241 return wfAppendQuery(
1242 str_replace( '/swift/v1', '', // S3 API is the rgw default
1243 $sContObj->cfs_http->getStorageUrl() . $spath ),
1244 array(
1245 'Signature' => $signature,
1246 'Expires' => $expires,
1247 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1248 );
1249 }
1250 } catch ( NoSuchContainerException $e ) {
1251 } catch ( CloudFilesException $e ) { // some other exception?
1252 $this->handleException( $e, null, __METHOD__, $params );
1253 }
1254 }
1255 return null;
1256 }
1257
1258 protected function directoriesAreVirtual() {
1259 return true;
1260 }
1261
1262 /**
1263 * Get headers to send to Swift when reading a file based
1264 * on a FileBackend params array, e.g. that of getLocalCopy().
1265 * $params is currently only checked for a 'latest' flag.
1266 *
1267 * @param array $params
1268 * @return Array
1269 */
1270 protected function headersFromParams( array $params ) {
1271 $hdrs = array();
1272 if ( !empty( $params['latest'] ) ) {
1273 $hdrs[] = 'X-Newest: true';
1274 }
1275 return $hdrs;
1276 }
1277
1278 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1279 $statuses = array();
1280
1281 $cfOps = array(); // list of CF_Async_Op objects
1282 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1283 $cfOps[$index] = $fileOpHandle->cfOp;
1284 }
1285 $batch = new CF_Async_Op_Batch( $cfOps );
1286
1287 $cfOps = $batch->execute();
1288 foreach ( $cfOps as $index => $cfOp ) {
1289 $status = Status::newGood();
1290 $function = '_getResponse' . $fileOpHandles[$index]->call;
1291 try { // catch exceptions; update status
1292 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params );
1293 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects );
1294 } catch ( CloudFilesException $e ) { // some other exception?
1295 $this->handleException( $e, $status,
1296 __CLASS__ . ":$function", $fileOpHandles[$index]->params );
1297 }
1298 $statuses[$index] = $status;
1299 }
1300
1301 return $statuses;
1302 }
1303
1304 /**
1305 * Set read/write permissions for a Swift container.
1306 *
1307 * $readGrps is a list of the possible criteria for a request to have
1308 * access to read a container. Each item is one of the following formats:
1309 * - account:user : Grants access if the request is by the given user
1310 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1311 * matches the expression and the request is not for a listing.
1312 * Setting this to '*' effectively makes a container public.
1313 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1314 * matches the expression and the request is for a listing.
1315 *
1316 * $writeGrps is a list of the possible criteria for a request to have
1317 * access to write to a container. Each item is of the following format:
1318 * - account:user : Grants access if the request is by the given user
1319 *
1320 * @see http://swift.openstack.org/misc.html#acls
1321 *
1322 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1323 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1324 *
1325 * @param CF_Container $contObj Swift container
1326 * @param array $readGrps List of read access routes
1327 * @param array $writeGrps List of write access routes
1328 * @return Status
1329 */
1330 protected function setContainerAccess(
1331 CF_Container $contObj, array $readGrps, array $writeGrps
1332 ) {
1333 $creds = $contObj->cfs_auth->export_credentials();
1334
1335 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1336
1337 // Note: 10 second timeout consistent with php-cloudfiles
1338 $req = MWHttpRequest::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1339 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1340 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1341 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1342
1343 return $req->execute(); // should return 204
1344 }
1345
1346 /**
1347 * Purge the CDN cache of affected objects if CDN caching is enabled.
1348 * This is for Rackspace/Akamai CDNs.
1349 *
1350 * @param array $objects List of CF_Object items
1351 * @return void
1352 */
1353 public function purgeCDNCache( array $objects ) {
1354 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1355 foreach ( $objects as $object ) {
1356 try {
1357 $object->purge_from_cdn();
1358 } catch ( CDNNotEnabledException $e ) {
1359 // CDN not enabled; nothing to see here
1360 } catch ( CloudFilesException $e ) {
1361 $this->handleException( $e, null, __METHOD__,
1362 array( 'cont' => $object->container->name, 'obj' => $object->name ) );
1363 }
1364 }
1365 }
1366 }
1367
1368 /**
1369 * Get an authenticated connection handle to the Swift proxy
1370 *
1371 * @throws CloudFilesException
1372 * @throws CloudFilesException|Exception
1373 * @return CF_Connection|bool False on failure
1374 */
1375 protected function getConnection() {
1376 if ( $this->connException instanceof CloudFilesException ) {
1377 if ( ( time() - $this->connErrorTime ) < 60 ) {
1378 throw $this->connException; // failed last attempt; don't bother
1379 } else { // actually retry this time
1380 $this->connException = null;
1381 $this->connErrorTime = 0;
1382 }
1383 }
1384 // Session keys expire after a while, so we renew them periodically
1385 $reAuth = ( ( time() - $this->sessionStarted ) > $this->authTTL );
1386 // Authenticate with proxy and get a session key...
1387 if ( !$this->conn || $reAuth ) {
1388 $this->sessionStarted = 0;
1389 $this->connContainerCache->clear();
1390 $cacheKey = $this->getCredsCacheKey( $this->auth->username );
1391 $creds = $this->srvCache->get( $cacheKey ); // credentials
1392 if ( is_array( $creds ) ) { // cache hit
1393 $this->auth->load_cached_credentials(
1394 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1395 $this->sessionStarted = time() - ceil( $this->authTTL / 2 ); // skew for worst case
1396 } else { // cache miss
1397 try {
1398 $this->auth->authenticate();
1399 $creds = $this->auth->export_credentials();
1400 $this->srvCache->add( $cacheKey, $creds, ceil( $this->authTTL / 2 ) ); // cache
1401 $this->sessionStarted = time();
1402 } catch ( CloudFilesException $e ) {
1403 $this->connException = $e; // don't keep re-trying
1404 $this->connErrorTime = time();
1405 throw $e; // throw it back
1406 }
1407 }
1408 if ( $this->conn ) { // re-authorizing?
1409 $this->conn->close(); // close active cURL handles in CF_Http object
1410 }
1411 $this->conn = new CF_Connection( $this->auth );
1412 }
1413 return $this->conn;
1414 }
1415
1416 /**
1417 * Close the connection to the Swift proxy
1418 *
1419 * @return void
1420 */
1421 protected function closeConnection() {
1422 if ( $this->conn ) {
1423 $this->conn->close(); // close active cURL handles in CF_Http object
1424 $this->conn = null;
1425 $this->sessionStarted = 0;
1426 $this->connContainerCache->clear();
1427 }
1428 }
1429
1430 /**
1431 * Get the cache key for a container
1432 *
1433 * @param string $username
1434 * @return string
1435 */
1436 private function getCredsCacheKey( $username ) {
1437 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1438 }
1439
1440 /**
1441 * Get a Swift container object, possibly from process cache.
1442 * Use $reCache if the file count or byte count is needed.
1443 *
1444 * @param string $container Container name
1445 * @param bool $bypassCache Bypass all caches and load from Swift
1446 * @return CF_Container
1447 * @throws CloudFilesException
1448 */
1449 protected function getContainer( $container, $bypassCache = false ) {
1450 $conn = $this->getConnection(); // Swift proxy connection
1451 if ( $bypassCache ) { // purge cache
1452 $this->connContainerCache->clear( $container );
1453 } elseif ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1454 $this->primeContainerCache( array( $container ) ); // check persistent cache
1455 }
1456 if ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1457 $contObj = $conn->get_container( $container );
1458 // NoSuchContainerException not thrown: container must exist
1459 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache it
1460 if ( !$bypassCache ) {
1461 $this->setContainerCache( $container, // update persistent cache
1462 array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
1463 );
1464 }
1465 }
1466 return $this->connContainerCache->get( $container, 'obj' );
1467 }
1468
1469 /**
1470 * Create a Swift container
1471 *
1472 * @param string $container Container name
1473 * @return CF_Container
1474 * @throws CloudFilesException
1475 */
1476 protected function createContainer( $container ) {
1477 $conn = $this->getConnection(); // Swift proxy connection
1478 $contObj = $conn->create_container( $container );
1479 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache
1480 return $contObj;
1481 }
1482
1483 /**
1484 * Delete a Swift container
1485 *
1486 * @param string $container Container name
1487 * @return void
1488 * @throws CloudFilesException
1489 */
1490 protected function deleteContainer( $container ) {
1491 $conn = $this->getConnection(); // Swift proxy connection
1492 $this->connContainerCache->clear( $container ); // purge
1493 $conn->delete_container( $container );
1494 }
1495
1496 protected function doPrimeContainerCache( array $containerInfo ) {
1497 try {
1498 $conn = $this->getConnection(); // Swift proxy connection
1499 foreach ( $containerInfo as $container => $info ) {
1500 $contObj = new CF_Container( $conn->cfs_auth, $conn->cfs_http,
1501 $container, $info['count'], $info['bytes'] );
1502 $this->connContainerCache->set( $container, 'obj', $contObj );
1503 }
1504 } catch ( CloudFilesException $e ) { // some other exception?
1505 $this->handleException( $e, null, __METHOD__, array() );
1506 }
1507 }
1508
1509 /**
1510 * Log an unexpected exception for this backend.
1511 * This also sets the Status object to have a fatal error.
1512 *
1513 * @param Exception $e
1514 * @param Status $status|null
1515 * @param string $func
1516 * @param array $params
1517 * @return void
1518 */
1519 protected function handleException( Exception $e, $status, $func, array $params ) {
1520 if ( $status instanceof Status ) {
1521 if ( $e instanceof AuthenticationException ) {
1522 $status->fatal( 'backend-fail-connect', $this->name );
1523 } else {
1524 $status->fatal( 'backend-fail-internal', $this->name );
1525 }
1526 }
1527 if ( $e->getMessage() ) {
1528 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1529 }
1530 if ( $e instanceof InvalidResponseException ) { // possibly a stale token
1531 $this->srvCache->delete( $this->getCredsCacheKey( $this->auth->username ) );
1532 $this->closeConnection(); // force a re-connect and re-auth next time
1533 }
1534 wfDebugLog( 'SwiftBackend',
1535 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1536 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1537 );
1538 }
1539 }
1540
1541 /**
1542 * @see FileBackendStoreOpHandle
1543 */
1544 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1545 /** @var CF_Async_Op */
1546 public $cfOp;
1547 /** @var Array */
1548 public $affectedObjects = array();
1549
1550 /**
1551 * @param SwiftFileBackend $backend
1552 * @param array $params
1553 * @param string $call
1554 * @param CF_Async_Op $cfOp
1555 */
1556 public function __construct(
1557 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1558 ) {
1559 $this->backend = $backend;
1560 $this->params = $params;
1561 $this->call = $call;
1562 $this->cfOp = $cfOp;
1563 }
1564 }
1565
1566 /**
1567 * SwiftFileBackend helper class to page through listings.
1568 * Swift also has a listing limit of 10,000 objects for sanity.
1569 * Do not use this class from places outside SwiftFileBackend.
1570 *
1571 * @ingroup FileBackend
1572 */
1573 abstract class SwiftFileBackendList implements Iterator {
1574 /** @var Array */
1575 protected $bufferIter = array();
1576 protected $bufferAfter = null; // string; list items *after* this path
1577 protected $pos = 0; // integer
1578 /** @var Array */
1579 protected $params = array();
1580
1581 /** @var SwiftFileBackend */
1582 protected $backend;
1583 protected $container; // string; container name
1584 protected $dir; // string; storage directory
1585 protected $suffixStart; // integer
1586
1587 const PAGE_SIZE = 9000; // file listing buffer size
1588
1589 /**
1590 * @param SwiftFileBackend $backend
1591 * @param string $fullCont Resolved container name
1592 * @param string $dir Resolved directory relative to container
1593 * @param array $params
1594 */
1595 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1596 $this->backend = $backend;
1597 $this->container = $fullCont;
1598 $this->dir = $dir;
1599 if ( substr( $this->dir, -1 ) === '/' ) {
1600 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1601 }
1602 if ( $this->dir == '' ) { // whole container
1603 $this->suffixStart = 0;
1604 } else { // dir within container
1605 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1606 }
1607 $this->params = $params;
1608 }
1609
1610 /**
1611 * @see Iterator::key()
1612 * @return integer
1613 */
1614 public function key() {
1615 return $this->pos;
1616 }
1617
1618 /**
1619 * @see Iterator::next()
1620 * @return void
1621 */
1622 public function next() {
1623 // Advance to the next file in the page
1624 next( $this->bufferIter );
1625 ++$this->pos;
1626 // Check if there are no files left in this page and
1627 // advance to the next page if this page was not empty.
1628 if ( !$this->valid() && count( $this->bufferIter ) ) {
1629 $this->bufferIter = $this->pageFromList(
1630 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1631 ); // updates $this->bufferAfter
1632 }
1633 }
1634
1635 /**
1636 * @see Iterator::rewind()
1637 * @return void
1638 */
1639 public function rewind() {
1640 $this->pos = 0;
1641 $this->bufferAfter = null;
1642 $this->bufferIter = $this->pageFromList(
1643 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1644 ); // updates $this->bufferAfter
1645 }
1646
1647 /**
1648 * @see Iterator::valid()
1649 * @return bool
1650 */
1651 public function valid() {
1652 if ( $this->bufferIter === null ) {
1653 return false; // some failure?
1654 } else {
1655 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1656 }
1657 }
1658
1659 /**
1660 * Get the given list portion (page)
1661 *
1662 * @param string $container Resolved container name
1663 * @param string $dir Resolved path relative to container
1664 * @param string $after|null
1665 * @param integer $limit
1666 * @param array $params
1667 * @return Traversable|Array
1668 */
1669 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1670 }
1671
1672 /**
1673 * Iterator for listing directories
1674 */
1675 class SwiftFileBackendDirList extends SwiftFileBackendList {
1676 /**
1677 * @see Iterator::current()
1678 * @return string|bool String (relative path) or false
1679 */
1680 public function current() {
1681 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1682 }
1683
1684 /**
1685 * @see SwiftFileBackendList::pageFromList()
1686 * @return Array
1687 */
1688 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1689 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1690 }
1691 }
1692
1693 /**
1694 * Iterator for listing regular files
1695 */
1696 class SwiftFileBackendFileList extends SwiftFileBackendList {
1697 /**
1698 * @see Iterator::current()
1699 * @return string|bool String (relative path) or false
1700 */
1701 public function current() {
1702 return substr( current( $this->bufferIter ), $this->suffixStart );
1703 }
1704
1705 /**
1706 * @see SwiftFileBackendList::pageFromList()
1707 * @return Array
1708 */
1709 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1710 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1711 }
1712 }