Merge "mediawiki.searchSuggest: Unbreak browser blacklist"
[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 Swift authentication handler */
41 protected $auth;
42
43 /** @var int TTL in seconds */
44 protected $authTTL;
45
46 /** @var string Shared secret value for making temp URLs */
47 protected $swiftTempUrlKey;
48
49 /** @var string Username to handle unauthenticated requests */
50 protected $swiftAnonUser;
51
52 /** @var bool Whether CloudFiles CDN is enabled */
53 protected $swiftUseCDN;
54
55 /** @var int How long to cache things in the CDN */
56 protected $swiftCDNExpiry;
57
58 /** @var bool Whether object CDN purging is enabled */
59 protected $swiftCDNPurgable;
60
61 // Rados Gateway specific options
62 /** @var string S3 access key */
63 protected $rgwS3AccessKey;
64
65 /** @var string S3 authentication key */
66 protected $rgwS3SecretKey;
67
68 /** @var CF_Connection Swift connection handle*/
69 protected $conn;
70
71 /** @var int UNIX timestamp */
72 protected $sessionStarted = 0;
73
74 /** @var CloudFilesException */
75 protected $connException;
76
77 /** @var int UNIX timestamp */
78 protected $connErrorTime = 0;
79
80 /** @var BagOStuff */
81 protected $srvCache;
82
83 /** @var ProcessCacheLRU */
84 protected $connContainerCache; // container object cache
85
86 /**
87 * @see FileBackendStore::__construct()
88 * Additional $config params include:
89 * - swiftAuthUrl : Swift authentication server URL
90 * - swiftUser : Swift user used by MediaWiki (account:username)
91 * - swiftKey : Swift authentication key for the above user
92 * - swiftAuthTTL : Swift authentication TTL (seconds)
93 * - swiftTempUrlKey : Swift "X-Account-Meta-Temp-URL-Key" value on the account.
94 * Do not set this until it has been set in the backend.
95 * - swiftAnonUser : Swift user used for end-user requests (account:username).
96 * If set, then views of public containers are assumed to go
97 * through this user. If not set, then public containers are
98 * accessible to unauthenticated requests via ".r:*" in the ACL.
99 * - swiftUseCDN : Whether a Cloud Files Content Delivery Network is set up
100 * - swiftCDNExpiry : How long (in seconds) to store content in the CDN.
101 * If files may likely change, this should probably not exceed
102 * a few days. For example, deletions may take this long to apply.
103 * If object purging is enabled, however, this is not an issue.
104 * - swiftCDNPurgable : Whether object purge requests are allowed by the CDN.
105 * - shardViaHashLevels : Map of container names to sharding config with:
106 * - base : base of hash characters, 16 or 36
107 * - levels : the number of hash levels (and digits)
108 * - repeat : hash subdirectories are prefixed with all the
109 * parent hash directory names (e.g. "a/ab/abc")
110 * - cacheAuthInfo : Whether to cache authentication tokens in APC, XCache, ect.
111 * If those are not available, then the main cache will be used.
112 * This is probably insecure in shared hosting environments.
113 * - rgwS3AccessKey : Ragos Gateway S3 "access key" value on the account.
114 * Do not set this until it has been set in the backend.
115 * This is used for generating expiring pre-authenticated URLs.
116 * Only use this when using rgw and to work around
117 * http://tracker.newdream.net/issues/3454.
118 * - rgwS3SecretKey : Ragos Gateway S3 "secret key" value on the account.
119 * Do not set this until it has been set in the backend.
120 * This is used for generating expiring pre-authenticated URLs.
121 * Only use this when using rgw and to work around
122 * http://tracker.newdream.net/issues/3454.
123 */
124 public function __construct( array $config ) {
125 parent::__construct( $config );
126 if ( !class_exists( 'CF_Constants' ) ) {
127 throw new FileBackendException( 'SwiftCloudFiles extension not installed.' );
128 }
129 // Required settings
130 $this->auth = new CF_Authentication(
131 $config['swiftUser'],
132 $config['swiftKey'],
133 null, // account; unused
134 $config['swiftAuthUrl']
135 );
136 // Optional settings
137 $this->authTTL = isset( $config['swiftAuthTTL'] )
138 ? $config['swiftAuthTTL']
139 : 5 * 60; // some sane number
140 $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
141 ? $config['swiftAnonUser']
142 : '';
143 $this->swiftTempUrlKey = isset( $config['swiftTempUrlKey'] )
144 ? $config['swiftTempUrlKey']
145 : '';
146 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
147 ? $config['shardViaHashLevels']
148 : '';
149 $this->swiftUseCDN = isset( $config['swiftUseCDN'] )
150 ? $config['swiftUseCDN']
151 : false;
152 $this->swiftCDNExpiry = isset( $config['swiftCDNExpiry'] )
153 ? $config['swiftCDNExpiry']
154 : 12 * 3600; // 12 hours is safe (tokens last 24 hours per http://docs.openstack.org)
155 $this->swiftCDNPurgable = isset( $config['swiftCDNPurgable'] )
156 ? $config['swiftCDNPurgable']
157 : true;
158 $this->rgwS3AccessKey = isset( $config['rgwS3AccessKey'] )
159 ? $config['rgwS3AccessKey']
160 : '';
161 $this->rgwS3SecretKey = isset( $config['rgwS3SecretKey'] )
162 ? $config['rgwS3SecretKey']
163 : '';
164 // Cache container information to mask latency
165 $this->memCache = wfGetMainCache();
166 // Process cache for container info
167 $this->connContainerCache = new ProcessCacheLRU( 300 );
168 // Cache auth token information to avoid RTTs
169 if ( !empty( $config['cacheAuthInfo'] ) ) {
170 if ( PHP_SAPI === 'cli' ) {
171 $this->srvCache = wfGetMainCache(); // preferrably memcached
172 } else {
173 try { // look for APC, XCache, WinCache, ect...
174 $this->srvCache = ObjectCache::newAccelerator( array() );
175 } catch ( Exception $e ) {
176 }
177 }
178 }
179 $this->srvCache = $this->srvCache ? $this->srvCache : new EmptyBagOStuff();
180 }
181
182 /**
183 * @see FileBackendStore::resolveContainerPath()
184 * @param string $container
185 * @param string $relStoragePath
186 * @return string|null Returns null when the URL encoded storage path is
187 * longer than 1024 characters or not UTF-8 encoded.
188 */
189 protected function resolveContainerPath( $container, $relStoragePath ) {
190 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) { // mb_string required by CF
191 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
192 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
193 return null; // too long for Swift
194 }
195
196 return $relStoragePath;
197 }
198
199 public function isPathUsableInternal( $storagePath ) {
200 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
201 if ( $rel === null ) {
202 return false; // invalid
203 }
204
205 try {
206 $this->getContainer( $container );
207
208 return true; // container exists
209 } catch ( NoSuchContainerException $e ) {
210 } catch ( CloudFilesException $e ) { // some other exception?
211 $this->handleException( $e, null, __METHOD__, array( 'path' => $storagePath ) );
212 }
213
214 return false;
215 }
216
217 /**
218 * @param array $headers
219 * @return array
220 */
221 protected function sanitizeHdrs( array $headers ) {
222 // By default, Swift has annoyingly low maximum header value limits
223 if ( isset( $headers['Content-Disposition'] ) ) {
224 $headers['Content-Disposition'] = $this->truncDisp( $headers['Content-Disposition'] );
225 }
226
227 return $headers;
228 }
229
230 /**
231 * @param string $disposition Content-Disposition header value
232 * @return string Truncated Content-Disposition header value to meet Swift limits
233 */
234 protected function truncDisp( $disposition ) {
235 $res = '';
236 foreach ( explode( ';', $disposition ) as $part ) {
237 $part = trim( $part );
238 $new = ( $res === '' ) ? $part : "{$res};{$part}";
239 if ( strlen( $new ) <= 255 ) {
240 $res = $new;
241 } else {
242 break; // too long; sigh
243 }
244 }
245
246 return $res;
247 }
248
249 protected function doCreateInternal( array $params ) {
250 $status = Status::newGood();
251
252 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
253 if ( $dstRel === null ) {
254 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
255
256 return $status;
257 }
258
259 // (a) Check the destination container and object
260 try {
261 $dContObj = $this->getContainer( $dstCont );
262 } catch ( NoSuchContainerException $e ) {
263 $status->fatal( 'backend-fail-create', $params['dst'] );
264
265 return $status;
266 } catch ( CloudFilesException $e ) { // some other exception?
267 $this->handleException( $e, $status, __METHOD__, $params );
268
269 return $status;
270 }
271
272 // (b) Get a SHA-1 hash of the object
273 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
274
275 // (c) Actually create the object
276 try {
277 // Create a fresh CF_Object with no fields preloaded.
278 // We don't want to preserve headers, metadata, and such.
279 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
280 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
281 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
282 // The MD5 here will be checked within Swift against its own MD5.
283 $obj->set_etag( md5( $params['content'] ) );
284 // Use the same content type as StreamFile for security
285 $obj->content_type = $this->getContentType( $params['dst'], $params['content'], null );
286 // Set any other custom headers if requested
287 if ( isset( $params['headers'] ) ) {
288 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
289 }
290 if ( !empty( $params['async'] ) ) { // deferred
291 $op = $obj->write_async( $params['content'] );
292 $status->value = new SwiftFileOpHandle( $this, $params, 'Create', $op );
293 $status->value->affectedObjects[] = $obj;
294 } else { // actually write the object in Swift
295 $obj->write( $params['content'] );
296 $this->purgeCDNCache( array( $obj ) );
297 }
298 } catch ( CDNNotEnabledException $e ) {
299 // CDN not enabled; nothing to see here
300 } catch ( BadContentTypeException $e ) {
301 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
302 } catch ( CloudFilesException $e ) { // some other exception?
303 $this->handleException( $e, $status, __METHOD__, $params );
304 }
305
306 return $status;
307 }
308
309 /**
310 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
311 */
312 protected function getResponseCreate( CF_Async_Op $cfOp, Status $status, array $params ) {
313 try {
314 $cfOp->getLastResponse();
315 } catch ( BadContentTypeException $e ) {
316 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
317 }
318 }
319
320 protected function doStoreInternal( array $params ) {
321 $status = Status::newGood();
322
323 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
324 if ( $dstRel === null ) {
325 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
326
327 return $status;
328 }
329
330 // (a) Check the destination container and object
331 try {
332 $dContObj = $this->getContainer( $dstCont );
333 } catch ( NoSuchContainerException $e ) {
334 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
335
336 return $status;
337 } catch ( CloudFilesException $e ) { // some other exception?
338 $this->handleException( $e, $status, __METHOD__, $params );
339
340 return $status;
341 }
342
343 // (b) Get a SHA-1 hash of the object
344 wfSuppressWarnings();
345 $sha1Hash = sha1_file( $params['src'] );
346 wfRestoreWarnings();
347 if ( $sha1Hash === false ) { // source doesn't exist?
348 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
349
350 return $status;
351 }
352 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
353
354 // (c) Actually store the object
355 try {
356 // Create a fresh CF_Object with no fields preloaded.
357 // We don't want to preserve headers, metadata, and such.
358 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
359 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
360 // The MD5 here will be checked within Swift against its own MD5.
361 $obj->set_etag( md5_file( $params['src'] ) );
362 // Use the same content type as StreamFile for security
363 $obj->content_type = $this->getContentType( $params['dst'], null, $params['src'] );
364 // Set any other custom headers if requested
365 if ( isset( $params['headers'] ) ) {
366 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
367 }
368 if ( !empty( $params['async'] ) ) { // deferred
369 wfSuppressWarnings();
370 $fp = fopen( $params['src'], 'rb' );
371 wfRestoreWarnings();
372 if ( !$fp ) {
373 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
374 } else {
375 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
376 $status->value = new SwiftFileOpHandle( $this, $params, 'Store', $op );
377 $status->value->resourcesToClose[] = $fp;
378 $status->value->affectedObjects[] = $obj;
379 }
380 } else { // actually write the object in Swift
381 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
382 $this->purgeCDNCache( array( $obj ) );
383 }
384 } catch ( CDNNotEnabledException $e ) {
385 // CDN not enabled; nothing to see here
386 } catch ( BadContentTypeException $e ) {
387 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
388 } catch ( IOException $e ) {
389 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
390 } catch ( CloudFilesException $e ) { // some other exception?
391 $this->handleException( $e, $status, __METHOD__, $params );
392 }
393
394 return $status;
395 }
396
397 /**
398 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
399 */
400 protected function getResponseStore( CF_Async_Op $cfOp, Status $status, array $params ) {
401 try {
402 $cfOp->getLastResponse();
403 } catch ( BadContentTypeException $e ) {
404 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
405 } catch ( IOException $e ) {
406 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
407 }
408 }
409
410 protected function doCopyInternal( array $params ) {
411 $status = Status::newGood();
412
413 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
414 if ( $srcRel === null ) {
415 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
416
417 return $status;
418 }
419
420 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
421 if ( $dstRel === null ) {
422 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
423
424 return $status;
425 }
426
427 // (a) Check the source/destination containers and destination object
428 try {
429 $sContObj = $this->getContainer( $srcCont );
430 $dContObj = $this->getContainer( $dstCont );
431 } catch ( NoSuchContainerException $e ) {
432 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
433 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
434 }
435
436 return $status;
437 } catch ( CloudFilesException $e ) { // some other exception?
438 $this->handleException( $e, $status, __METHOD__, $params );
439
440 return $status;
441 }
442
443 // (b) Actually copy the file to the destination
444 try {
445 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
446 $hdrs = array(); // source file headers to override with new values
447 // Set any other custom headers if requested
448 if ( isset( $params['headers'] ) ) {
449 $hdrs += $this->sanitizeHdrs( $params['headers'] );
450 }
451 if ( !empty( $params['async'] ) ) { // deferred
452 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
453 $status->value = new SwiftFileOpHandle( $this, $params, 'Copy', $op );
454 $status->value->affectedObjects[] = $dstObj;
455 } else { // actually write the object in Swift
456 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
457 $this->purgeCDNCache( array( $dstObj ) );
458 }
459 } catch ( CDNNotEnabledException $e ) {
460 // CDN not enabled; nothing to see here
461 } catch ( NoSuchObjectException $e ) { // source object does not exist
462 if ( empty( $params['ignoreMissingSource'] ) ) {
463 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
464 }
465 } catch ( CloudFilesException $e ) { // some other exception?
466 $this->handleException( $e, $status, __METHOD__, $params );
467 }
468
469 return $status;
470 }
471
472 /**
473 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
474 */
475 protected function getResponseCopy( CF_Async_Op $cfOp, Status $status, array $params ) {
476 try {
477 $cfOp->getLastResponse();
478 } catch ( NoSuchObjectException $e ) { // source object does not exist
479 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
480 }
481 }
482
483 protected function doMoveInternal( array $params ) {
484 $status = Status::newGood();
485
486 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
487 if ( $srcRel === null ) {
488 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
489
490 return $status;
491 }
492
493 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
494 if ( $dstRel === null ) {
495 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
496
497 return $status;
498 }
499
500 // (a) Check the source/destination containers and destination object
501 try {
502 $sContObj = $this->getContainer( $srcCont );
503 $dContObj = $this->getContainer( $dstCont );
504 } catch ( NoSuchContainerException $e ) {
505 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
506 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
507 }
508
509 return $status;
510 } catch ( CloudFilesException $e ) { // some other exception?
511 $this->handleException( $e, $status, __METHOD__, $params );
512
513 return $status;
514 }
515
516 // (b) Actually move the file to the destination
517 try {
518 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
519 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
520 $hdrs = array(); // source file headers to override with new values
521 // Set any other custom headers if requested
522 if ( isset( $params['headers'] ) ) {
523 $hdrs += $this->sanitizeHdrs( $params['headers'] );
524 }
525 if ( !empty( $params['async'] ) ) { // deferred
526 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
527 $status->value = new SwiftFileOpHandle( $this, $params, 'Move', $op );
528 $status->value->affectedObjects[] = $srcObj;
529 $status->value->affectedObjects[] = $dstObj;
530 } else { // actually write the object in Swift
531 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
532 $this->purgeCDNCache( array( $srcObj ) );
533 $this->purgeCDNCache( array( $dstObj ) );
534 }
535 } catch ( CDNNotEnabledException $e ) {
536 // CDN not enabled; nothing to see here
537 } catch ( NoSuchObjectException $e ) { // source object does not exist
538 if ( empty( $params['ignoreMissingSource'] ) ) {
539 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
540 }
541 } catch ( CloudFilesException $e ) { // some other exception?
542 $this->handleException( $e, $status, __METHOD__, $params );
543 }
544
545 return $status;
546 }
547
548 /**
549 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
550 */
551 protected function getResponseMove( CF_Async_Op $cfOp, Status $status, array $params ) {
552 try {
553 $cfOp->getLastResponse();
554 } catch ( NoSuchObjectException $e ) { // source object does not exist
555 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
556 }
557 }
558
559 protected function doDeleteInternal( array $params ) {
560 $status = Status::newGood();
561
562 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
563 if ( $srcRel === null ) {
564 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
565
566 return $status;
567 }
568
569 try {
570 $sContObj = $this->getContainer( $srcCont );
571 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
572 if ( !empty( $params['async'] ) ) { // deferred
573 $op = $sContObj->delete_object_async( $srcRel );
574 $status->value = new SwiftFileOpHandle( $this, $params, 'Delete', $op );
575 $status->value->affectedObjects[] = $srcObj;
576 } else { // actually write the object in Swift
577 $sContObj->delete_object( $srcRel );
578 $this->purgeCDNCache( array( $srcObj ) );
579 }
580 } catch ( CDNNotEnabledException $e ) {
581 // CDN not enabled; nothing to see here
582 } catch ( NoSuchContainerException $e ) {
583 if ( empty( $params['ignoreMissingSource'] ) ) {
584 $status->fatal( 'backend-fail-delete', $params['src'] );
585 }
586 } catch ( NoSuchObjectException $e ) {
587 if ( empty( $params['ignoreMissingSource'] ) ) {
588 $status->fatal( 'backend-fail-delete', $params['src'] );
589 }
590 } catch ( CloudFilesException $e ) { // some other exception?
591 $this->handleException( $e, $status, __METHOD__, $params );
592 }
593
594 return $status;
595 }
596
597 /**
598 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
599 */
600 protected function getResponseDelete( CF_Async_Op $cfOp, Status $status, array $params ) {
601 try {
602 $cfOp->getLastResponse();
603 } catch ( NoSuchContainerException $e ) {
604 $status->fatal( 'backend-fail-delete', $params['src'] );
605 } catch ( NoSuchObjectException $e ) {
606 if ( empty( $params['ignoreMissingSource'] ) ) {
607 $status->fatal( 'backend-fail-delete', $params['src'] );
608 }
609 }
610 }
611
612 protected function doDescribeInternal( array $params ) {
613 $status = Status::newGood();
614
615 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
616 if ( $srcRel === null ) {
617 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
618
619 return $status;
620 }
621
622 try {
623 $sContObj = $this->getContainer( $srcCont );
624 // Get the latest version of the current metadata
625 $srcObj = $sContObj->get_object( $srcRel,
626 $this->headersFromParams( array( 'latest' => true ) ) );
627 // Merge in the metadata updates...
628 if ( isset( $params['headers'] ) ) {
629 $srcObj->headers = $this->sanitizeHdrs( $params['headers'] ) + $srcObj->headers;
630 }
631 $srcObj->sync_metadata(); // save to Swift
632 $this->purgeCDNCache( array( $srcObj ) );
633 } catch ( CDNNotEnabledException $e ) {
634 // CDN not enabled; nothing to see here
635 } catch ( NoSuchContainerException $e ) {
636 $status->fatal( 'backend-fail-describe', $params['src'] );
637 } catch ( NoSuchObjectException $e ) {
638 $status->fatal( 'backend-fail-describe', $params['src'] );
639 } catch ( CloudFilesException $e ) { // some other exception?
640 $this->handleException( $e, $status, __METHOD__, $params );
641 }
642
643 return $status;
644 }
645
646 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
647 $status = Status::newGood();
648
649 // (a) Check if container already exists
650 try {
651 $this->getContainer( $fullCont );
652
653 // NoSuchContainerException not thrown: container must exist
654 return $status; // already exists
655 } catch ( NoSuchContainerException $e ) {
656 // NoSuchContainerException thrown: container does not exist
657 } catch ( CloudFilesException $e ) { // some other exception?
658 $this->handleException( $e, $status, __METHOD__, $params );
659
660 return $status;
661 }
662
663 // (b) Create container as needed
664 try {
665 $contObj = $this->createContainer( $fullCont );
666 if ( !empty( $params['noAccess'] ) ) {
667 // Make container private to end-users...
668 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
669 } else {
670 // Make container public to end-users...
671 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
672 }
673 if ( $this->swiftUseCDN ) { // Rackspace style CDN
674 $contObj->make_public( $this->swiftCDNExpiry );
675 }
676 } catch ( CDNNotEnabledException $e ) {
677 // CDN not enabled; nothing to see here
678 } catch ( CloudFilesException $e ) { // some other exception?
679 $this->handleException( $e, $status, __METHOD__, $params );
680
681 return $status;
682 }
683
684 return $status;
685 }
686
687 /**
688 * @see FileBackendStore::doSecureInternal()
689 * @param string $fullCont
690 * @param string $dir
691 * @param array $params
692 * @return Status
693 */
694 protected function doSecureInternal( $fullCont, $dir, array $params ) {
695 $status = Status::newGood();
696 if ( empty( $params['noAccess'] ) ) {
697 return $status; // nothing to do
698 }
699
700 // Restrict container from end-users...
701 try {
702 // doPrepareInternal() should have been called,
703 // so the Swift container should already exist...
704 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
705 // NoSuchContainerException not thrown: container must exist
706
707 // Make container private to end-users...
708 $status->merge( $this->setContainerAccess(
709 $contObj,
710 array( $this->auth->username ), // read
711 array( $this->auth->username ) // write
712 ) );
713 if ( $this->swiftUseCDN && $contObj->is_public() ) { // Rackspace style CDN
714 $contObj->make_private();
715 }
716 } catch ( CDNNotEnabledException $e ) {
717 // CDN not enabled; nothing to see here
718 } catch ( CloudFilesException $e ) { // some other exception?
719 $this->handleException( $e, $status, __METHOD__, $params );
720 }
721
722 return $status;
723 }
724
725 /**
726 * @see FileBackendStore::doPublishInternal()
727 * @param string $fullCont
728 * @param string $dir
729 * @param array $params
730 * @return Status
731 */
732 protected function doPublishInternal( $fullCont, $dir, array $params ) {
733 $status = Status::newGood();
734
735 // Unrestrict container from end-users...
736 try {
737 // doPrepareInternal() should have been called,
738 // so the Swift container should already exist...
739 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
740 // NoSuchContainerException not thrown: container must exist
741
742 // Make container public to end-users...
743 if ( $this->swiftAnonUser != '' ) {
744 $status->merge( $this->setContainerAccess(
745 $contObj,
746 array( $this->auth->username, $this->swiftAnonUser ), // read
747 array( $this->auth->username, $this->swiftAnonUser ) // write
748 ) );
749 } else {
750 $status->merge( $this->setContainerAccess(
751 $contObj,
752 array( $this->auth->username, '.r:*' ), // read
753 array( $this->auth->username ) // write
754 ) );
755 }
756 if ( $this->swiftUseCDN && !$contObj->is_public() ) { // Rackspace style CDN
757 $contObj->make_public();
758 }
759 } catch ( CDNNotEnabledException $e ) {
760 // CDN not enabled; nothing to see here
761 } catch ( CloudFilesException $e ) { // some other exception?
762 $this->handleException( $e, $status, __METHOD__, $params );
763 }
764
765 return $status;
766 }
767
768 protected function doCleanInternal( $fullCont, $dir, array $params ) {
769 $status = Status::newGood();
770
771 // Only containers themselves can be removed, all else is virtual
772 if ( $dir != '' ) {
773 return $status; // nothing to do
774 }
775
776 // (a) Check the container
777 try {
778 $contObj = $this->getContainer( $fullCont, true );
779 } catch ( NoSuchContainerException $e ) {
780 return $status; // ok, nothing to do
781 } catch ( CloudFilesException $e ) { // some other exception?
782 $this->handleException( $e, $status, __METHOD__, $params );
783
784 return $status;
785 }
786
787 // (b) Delete the container if empty
788 if ( $contObj->object_count == 0 ) {
789 try {
790 $this->deleteContainer( $fullCont );
791 } catch ( NoSuchContainerException $e ) {
792 return $status; // race?
793 } catch ( NonEmptyContainerException $e ) {
794 return $status; // race? consistency delay?
795 } catch ( CloudFilesException $e ) { // some other exception?
796 $this->handleException( $e, $status, __METHOD__, $params );
797
798 return $status;
799 }
800 }
801
802 return $status;
803 }
804
805 protected function doGetFileStat( array $params ) {
806 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
807 if ( $srcRel === null ) {
808 return false; // invalid storage path
809 }
810
811 $stat = false;
812 try {
813 $contObj = $this->getContainer( $srcCont );
814 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
815 $this->addMissingMetadata( $srcObj, $params['src'] );
816 $stat = array(
817 // Convert various random Swift dates to TS_MW
818 'mtime' => $this->convertSwiftDate( $srcObj->last_modified, TS_MW ),
819 'size' => (int)$srcObj->content_length,
820 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
821 );
822 } catch ( NoSuchContainerException $e ) {
823 } catch ( NoSuchObjectException $e ) {
824 } catch ( CloudFilesException $e ) { // some other exception?
825 $stat = null;
826 $this->handleException( $e, null, __METHOD__, $params );
827 }
828
829 return $stat;
830 }
831
832 /**
833 * Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
834 * Dates might also come in like "2013-05-11T07:37:27.678360" from Swift listings,
835 * missing the timezone suffix (though Ceph RGW does not appear to have this bug).
836 *
837 * @param string $ts
838 * @param int $format Output format (TS_* constant)
839 * @return string
840 * @throws FileBackendError
841 */
842 protected function convertSwiftDate( $ts, $format = TS_MW ) {
843 try {
844 $timestamp = new MWTimestamp( $ts );
845 return $timestamp->getTimestamp( $format );
846 } catch ( MWException $e ) {
847 throw new FileBackendError( $e->getMessage() );
848 }
849 }
850
851 /**
852 * Fill in any missing object metadata and save it to Swift
853 *
854 * @param CF_Object $obj
855 * @param string $path Storage path to object
856 * @return bool Success
857 * @throws Exception cloudfiles exceptions
858 */
859 protected function addMissingMetadata( CF_Object $obj, $path ) {
860 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
861 return true; // nothing to do
862 }
863 wfProfileIn( __METHOD__ );
864 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING );
865 $status = Status::newGood();
866 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager::LOCK_UW, $status );
867 if ( $status->isOK() ) {
868 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
869 if ( $tmpFile ) {
870 $hash = $tmpFile->getSha1Base36();
871 if ( $hash !== false ) {
872 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
873 $obj->sync_metadata(); // save to Swift
874 wfProfileOut( __METHOD__ );
875
876 return true; // success
877 }
878 }
879 }
880 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING );
881 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
882 wfProfileOut( __METHOD__ );
883
884 return false; // failed
885 }
886
887 protected function doGetFileContentsMulti( array $params ) {
888 $contents = array();
889
890 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
891 // Blindly create tmp files and stream to them, catching any exception if the file does
892 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
893 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
894 $cfOps = array(); // (path => CF_Async_Op)
895
896 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
897 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
898 if ( $srcRel === null ) {
899 $contents[$path] = false;
900 continue;
901 }
902 $data = false;
903 try {
904 $sContObj = $this->getContainer( $srcCont );
905 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
906 // Create a new temporary memory file...
907 $handle = fopen( 'php://temp', 'wb' );
908 if ( $handle ) {
909 $headers = $this->headersFromParams( $params );
910 if ( count( $pathBatch ) > 1 ) {
911 $cfOps[$path] = $obj->stream_async( $handle, $headers );
912 $cfOps[$path]->_file_handle = $handle; // close this later
913 } else {
914 $obj->stream( $handle, $headers );
915 rewind( $handle ); // start from the beginning
916 $data = stream_get_contents( $handle );
917 fclose( $handle );
918 }
919 } else {
920 $data = false;
921 }
922 } catch ( NoSuchContainerException $e ) {
923 $data = false;
924 } catch ( NoSuchObjectException $e ) {
925 $data = false;
926 } catch ( CloudFilesException $e ) { // some other exception?
927 $data = false;
928 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
929 }
930 $contents[$path] = $data;
931 }
932
933 $batch = new CF_Async_Op_Batch( $cfOps );
934 $cfOps = $batch->execute();
935 foreach ( $cfOps as $path => $cfOp ) {
936 try {
937 $cfOp->getLastResponse();
938 rewind( $cfOp->_file_handle ); // start from the beginning
939 $contents[$path] = stream_get_contents( $cfOp->_file_handle );
940 } catch ( NoSuchContainerException $e ) {
941 $contents[$path] = false;
942 } catch ( NoSuchObjectException $e ) {
943 $contents[$path] = false;
944 } catch ( CloudFilesException $e ) { // some other exception?
945 $contents[$path] = false;
946 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
947 }
948 fclose( $cfOp->_file_handle ); // close open handle
949 }
950 }
951
952 return $contents;
953 }
954
955 /**
956 * @see FileBackendStore::doDirectoryExists()
957 * @param string $fullCont
958 * @param string $dir
959 * @param array $params
960 * @return bool|null
961 */
962 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
963 try {
964 $container = $this->getContainer( $fullCont );
965 $prefix = ( $dir == '' ) ? null : "{$dir}/";
966
967 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
968 } catch ( NoSuchContainerException $e ) {
969 return false;
970 } catch ( CloudFilesException $e ) { // some other exception?
971 $this->handleException( $e, null, __METHOD__,
972 array( 'cont' => $fullCont, 'dir' => $dir ) );
973 }
974
975 return null; // error
976 }
977
978 /**
979 * @see FileBackendStore::getDirectoryListInternal()
980 * @param string $fullCont
981 * @param string $dir
982 * @param array $params
983 * @return SwiftFileBackendDirList
984 */
985 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
986 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
987 }
988
989 /**
990 * @see FileBackendStore::getFileListInternal()
991 * @param string $fullCont
992 * @param string $dir
993 * @param array $params
994 * @return SwiftFileBackendFileList
995 */
996 public function getFileListInternal( $fullCont, $dir, array $params ) {
997 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
998 }
999
1000 /**
1001 * Do not call this function outside of SwiftFileBackendFileList
1002 *
1003 * @param string $fullCont Resolved container name
1004 * @param string $dir Resolved storage directory with no trailing slash
1005 * @param string|null $after Resolved container relative path to list items after
1006 * @param int $limit Max number of items to list
1007 * @param array $params Parameters for getDirectoryList()
1008 * @return array List of container relative resolved paths of directories directly under $dir
1009 * @throws FileBackendError
1010 */
1011 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1012 $dirs = array();
1013 if ( $after === INF ) {
1014 return $dirs; // nothing more
1015 }
1016
1017 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
1018 try {
1019 $container = $this->getContainer( $fullCont );
1020 $prefix = ( $dir == '' ) ? null : "{$dir}/";
1021 // Non-recursive: only list dirs right under $dir
1022 if ( !empty( $params['topOnly'] ) ) {
1023 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1024 foreach ( $objects as $object ) { // files and directories
1025 if ( substr( $object, -1 ) === '/' ) {
1026 $dirs[] = $object; // directories end in '/'
1027 }
1028 }
1029 // Recursive: list all dirs under $dir and its subdirs
1030 } else {
1031 // Get directory from last item of prior page
1032 $lastDir = $this->getParentDir( $after ); // must be first page
1033 $objects = $container->list_objects( $limit, $after, $prefix );
1034 foreach ( $objects as $object ) { // files
1035 $objectDir = $this->getParentDir( $object ); // directory of object
1036 if ( $objectDir !== false && $objectDir !== $dir ) {
1037 // Swift stores paths in UTF-8, using binary sorting.
1038 // See function "create_container_table" in common/db.py.
1039 // If a directory is not "greater" than the last one,
1040 // then it was already listed by the calling iterator.
1041 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
1042 $pDir = $objectDir;
1043 do { // add dir and all its parent dirs
1044 $dirs[] = "{$pDir}/";
1045 $pDir = $this->getParentDir( $pDir );
1046 } while ( $pDir !== false // sanity
1047 && strcmp( $pDir, $lastDir ) > 0 // not done already
1048 && strlen( $pDir ) > strlen( $dir ) // within $dir
1049 );
1050 }
1051 $lastDir = $objectDir;
1052 }
1053 }
1054 }
1055 // Page on the unfiltered directory listing (what is returned may be filtered)
1056 if ( count( $objects ) < $limit ) {
1057 $after = INF; // avoid a second RTT
1058 } else {
1059 $after = end( $objects ); // update last item
1060 }
1061 } catch ( NoSuchContainerException $e ) {
1062 } catch ( CloudFilesException $e ) { // some other exception?
1063 $this->handleException( $e, null, __METHOD__,
1064 array( 'cont' => $fullCont, 'dir' => $dir ) );
1065 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1066 }
1067
1068 return $dirs;
1069 }
1070
1071 protected function getParentDir( $path ) {
1072 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
1073 }
1074
1075 /**
1076 * Do not call this function outside of SwiftFileBackendFileList
1077 *
1078 * @param string $fullCont Resolved container name
1079 * @param string $dir Resolved storage directory with no trailing slash
1080 * @param string|null $after Resolved container relative path of file to list items after
1081 * @param int $limit Max number of items to list
1082 * @param array $params Parameters for getDirectoryList()
1083 * @return array List of resolved container relative paths of files under $dir
1084 * @throws FileBackendError
1085 */
1086 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1087 $files = array(); // list of (path, stat array or null) entries
1088 if ( $after === INF ) {
1089 return $files; // nothing more
1090 }
1091
1092 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
1093 try {
1094 $container = $this->getContainer( $fullCont );
1095 $prefix = ( $dir == '' ) ? null : "{$dir}/";
1096
1097 // $objects will contain a list of unfiltered names or CF_Object items
1098 // Non-recursive: only list files right under $dir
1099 if ( !empty( $params['topOnly'] ) ) {
1100 if ( !empty( $params['adviseStat'] ) ) {
1101 // Note: get_objects() does not include directories
1102 $objects = $container->get_objects( $limit, $after, $prefix, null, '/' );
1103 } else {
1104 // Note: list_objects() includes directories here
1105 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1106 }
1107 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1108 // Recursive: list all files under $dir and its subdirs
1109 } else {
1110 // Note: get_objects()/list_objects() here only return file objects
1111 if ( !empty( $params['adviseStat'] ) ) {
1112 $objects = $container->get_objects( $limit, $after, $prefix );
1113 } else {
1114 $objects = $container->list_objects( $limit, $after, $prefix );
1115 }
1116 $files = $this->buildFileObjectListing( $params, $dir, $objects );
1117 }
1118 // Page on the unfiltered object listing (what is returned may be filtered)
1119 if ( count( $objects ) < $limit ) {
1120 $after = INF; // avoid a second RTT
1121 } else {
1122 $after = end( $objects ); // update last item
1123 $after = is_object( $after ) ? $after->name : $after;
1124 }
1125 } catch ( NoSuchContainerException $e ) {
1126 } catch ( CloudFilesException $e ) { // some other exception?
1127 $this->handleException( $e, null, __METHOD__,
1128 array( 'cont' => $fullCont, 'dir' => $dir ) );
1129 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1130 }
1131
1132 return $files;
1133 }
1134
1135 /**
1136 * Build a list of file objects, filtering out any directories
1137 * and extracting any stat info if provided in $objects (for CF_Objects)
1138 *
1139 * @param array $params Parameters for getDirectoryList()
1140 * @param string $dir Resolved container directory path
1141 * @param array $objects List of CF_Object items or object names
1142 * @return array List of (names,stat array or null) entries
1143 */
1144 private function buildFileObjectListing( array $params, $dir, array $objects ) {
1145 $names = array();
1146 foreach ( $objects as $object ) {
1147 if ( is_object( $object ) ) {
1148 $stat = array(
1149 // Convert various random Swift dates to TS_MW
1150 'mtime' => $this->convertSwiftDate( $object->last_modified, TS_MW ),
1151 'size' => (int)$object->content_length,
1152 'latest' => false // eventually consistent
1153 );
1154 $names[] = array( $object->name, $stat );
1155 } elseif ( substr( $object, -1 ) !== '/' ) {
1156 // Omit directories, which end in '/' in listings
1157 $names[] = array( $object, null );
1158 }
1159 }
1160
1161 return $names;
1162 }
1163
1164 /**
1165 * Do not call this function outside of SwiftFileBackendFileList
1166 *
1167 * @param string $path Storage path
1168 * @param array $val Stat value
1169 */
1170 public function loadListingStatInternal( $path, array $val ) {
1171 $this->cheapCache->set( $path, 'stat', $val );
1172 }
1173
1174 protected function doGetFileSha1base36( array $params ) {
1175 $stat = $this->getFileStat( $params );
1176 if ( $stat ) {
1177 if ( !isset( $stat['sha1'] ) ) {
1178 // Stat entries filled by file listings don't include SHA1
1179 $this->clearCache( array( $params['src'] ) );
1180 $stat = $this->getFileStat( $params );
1181 }
1182
1183 return $stat['sha1'];
1184 } else {
1185 return false;
1186 }
1187 }
1188
1189 protected function doStreamFile( array $params ) {
1190 $status = Status::newGood();
1191
1192 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1193 if ( $srcRel === null ) {
1194 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1195 }
1196
1197 try {
1198 $cont = $this->getContainer( $srcCont );
1199 } catch ( NoSuchContainerException $e ) {
1200 $status->fatal( 'backend-fail-stream', $params['src'] );
1201
1202 return $status;
1203 } catch ( CloudFilesException $e ) { // some other exception?
1204 $this->handleException( $e, $status, __METHOD__, $params );
1205
1206 return $status;
1207 }
1208
1209 try {
1210 $output = fopen( 'php://output', 'wb' );
1211 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1212 $obj->stream( $output, $this->headersFromParams( $params ) );
1213 } catch ( NoSuchObjectException $e ) {
1214 $status->fatal( 'backend-fail-stream', $params['src'] );
1215 } catch ( CloudFilesException $e ) { // some other exception?
1216 $this->handleException( $e, $status, __METHOD__, $params );
1217 }
1218
1219 return $status;
1220 }
1221
1222 protected function doGetLocalCopyMulti( array $params ) {
1223 $tmpFiles = array();
1224
1225 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1226 // Blindly create tmp files and stream to them, catching any exception if the file does
1227 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1228 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1229 $cfOps = array(); // (path => CF_Async_Op)
1230
1231 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1232 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1233 if ( $srcRel === null ) {
1234 $tmpFiles[$path] = null;
1235 continue;
1236 }
1237 $tmpFile = null;
1238 try {
1239 $sContObj = $this->getContainer( $srcCont );
1240 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1241 // Get source file extension
1242 $ext = FileBackend::extensionFromPath( $path );
1243 // Create a new temporary file...
1244 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
1245 if ( $tmpFile ) {
1246 $handle = fopen( $tmpFile->getPath(), 'wb' );
1247 if ( $handle ) {
1248 $headers = $this->headersFromParams( $params );
1249 if ( count( $pathBatch ) > 1 ) {
1250 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1251 $cfOps[$path]->_file_handle = $handle; // close this later
1252 } else {
1253 $obj->stream( $handle, $headers );
1254 fclose( $handle );
1255 }
1256 } else {
1257 $tmpFile = null;
1258 }
1259 }
1260 } catch ( NoSuchContainerException $e ) {
1261 $tmpFile = null;
1262 } catch ( NoSuchObjectException $e ) {
1263 $tmpFile = null;
1264 } catch ( CloudFilesException $e ) { // some other exception?
1265 $tmpFile = null;
1266 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1267 }
1268 $tmpFiles[$path] = $tmpFile;
1269 }
1270
1271 $batch = new CF_Async_Op_Batch( $cfOps );
1272 $cfOps = $batch->execute();
1273 foreach ( $cfOps as $path => $cfOp ) {
1274 try {
1275 $cfOp->getLastResponse();
1276 } catch ( NoSuchContainerException $e ) {
1277 $tmpFiles[$path] = null;
1278 } catch ( NoSuchObjectException $e ) {
1279 $tmpFiles[$path] = null;
1280 } catch ( CloudFilesException $e ) { // some other exception?
1281 $tmpFiles[$path] = null;
1282 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1283 }
1284 fclose( $cfOp->_file_handle ); // close open handle
1285 }
1286 }
1287
1288 return $tmpFiles;
1289 }
1290
1291 public function getFileHttpUrl( array $params ) {
1292 if ( $this->swiftTempUrlKey != '' ||
1293 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' )
1294 ) {
1295 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1296 if ( $srcRel === null ) {
1297 return null; // invalid path
1298 }
1299 try {
1300 $ttl = isset( $params['ttl'] ) ? $params['ttl'] : 86400;
1301 $sContObj = $this->getContainer( $srcCont );
1302 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1303 if ( $this->swiftTempUrlKey != '' ) {
1304 return $obj->get_temp_url( $this->swiftTempUrlKey, $ttl, "GET" );
1305 } else { // give S3 API URL for rgw
1306 $expires = time() + $ttl;
1307 // Path for signature starts with the bucket
1308 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1309 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1310 // Calculate the hash
1311 $signature = base64_encode( hash_hmac(
1312 'sha1',
1313 "GET\n\n\n{$expires}\n{$spath}",
1314 $this->rgwS3SecretKey,
1315 true // raw
1316 ) );
1317
1318 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1319 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1320 return wfAppendQuery(
1321 str_replace( '/swift/v1', '', // S3 API is the rgw default
1322 $sContObj->cfs_http->getStorageUrl() . $spath ),
1323 array(
1324 'Signature' => $signature,
1325 'Expires' => $expires,
1326 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1327 );
1328 }
1329 } catch ( NoSuchContainerException $e ) {
1330 } catch ( CloudFilesException $e ) { // some other exception?
1331 $this->handleException( $e, null, __METHOD__, $params );
1332 }
1333 }
1334
1335 return null;
1336 }
1337
1338 protected function directoriesAreVirtual() {
1339 return true;
1340 }
1341
1342 /**
1343 * Get headers to send to Swift when reading a file based
1344 * on a FileBackend params array, e.g. that of getLocalCopy().
1345 * $params is currently only checked for a 'latest' flag.
1346 *
1347 * @param array $params
1348 * @return array
1349 */
1350 protected function headersFromParams( array $params ) {
1351 $hdrs = array();
1352 if ( !empty( $params['latest'] ) ) {
1353 $hdrs[] = 'X-Newest: true';
1354 }
1355
1356 return $hdrs;
1357 }
1358
1359 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1360 $statuses = array();
1361
1362 $cfOps = array(); // list of CF_Async_Op objects
1363 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1364 $cfOps[$index] = $fileOpHandle->cfOp;
1365 }
1366 $batch = new CF_Async_Op_Batch( $cfOps );
1367
1368 $cfOps = $batch->execute();
1369 foreach ( $cfOps as $index => $cfOp ) {
1370 $status = Status::newGood();
1371 $function = 'getResponse' . $fileOpHandles[$index]->call;
1372 try { // catch exceptions; update status
1373 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params );
1374 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects );
1375 } catch ( CloudFilesException $e ) { // some other exception?
1376 $this->handleException( $e, $status,
1377 __CLASS__ . ":$function", $fileOpHandles[$index]->params );
1378 }
1379 $statuses[$index] = $status;
1380 }
1381
1382 return $statuses;
1383 }
1384
1385 /**
1386 * Set read/write permissions for a Swift container.
1387 *
1388 * @see http://swift.openstack.org/misc.html#acls
1389 *
1390 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1391 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1392 *
1393 * @param CF_Container $contObj Swift container
1394 * @param array $readGrps List of the possible criteria for a request to have
1395 * access to read a container. Each item is one of the following formats:
1396 * - account:user : Grants access if the request is by the given user
1397 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1398 * matches the expression and the request is not for a listing.
1399 * Setting this to '*' effectively makes a container public.
1400 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1401 * matches the expression and the request is for a listing.
1402 * @param array $writeGrps A list of the possible criteria for a request to have
1403 * access to write to a container. Each item is of the following format:
1404 * - account:user : Grants access if the request is by the given user
1405 * @return Status
1406 */
1407 protected function setContainerAccess(
1408 CF_Container $contObj, array $readGrps, array $writeGrps
1409 ) {
1410 $creds = $contObj->cfs_auth->export_credentials();
1411
1412 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1413
1414 // Note: 10 second timeout consistent with php-cloudfiles
1415 $req = MWHttpRequest::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1416 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1417 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1418 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1419
1420 return $req->execute(); // should return 204
1421 }
1422
1423 /**
1424 * Purge the CDN cache of affected objects if CDN caching is enabled.
1425 * This is for Rackspace/Akamai CDNs.
1426 *
1427 * @param array $objects List of CF_Object items
1428 */
1429 public function purgeCDNCache( array $objects ) {
1430 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1431 foreach ( $objects as $object ) {
1432 try {
1433 $object->purge_from_cdn();
1434 } catch ( CDNNotEnabledException $e ) {
1435 // CDN not enabled; nothing to see here
1436 } catch ( CloudFilesException $e ) {
1437 $this->handleException( $e, null, __METHOD__,
1438 array( 'cont' => $object->container->name, 'obj' => $object->name ) );
1439 }
1440 }
1441 }
1442 }
1443
1444 /**
1445 * Get an authenticated connection handle to the Swift proxy
1446 *
1447 * @throws CloudFilesException
1448 * @throws CloudFilesException|Exception
1449 * @return CF_Connection|bool False on failure
1450 */
1451 protected function getConnection() {
1452 if ( $this->connException instanceof CloudFilesException ) {
1453 if ( ( time() - $this->connErrorTime ) < 60 ) {
1454 throw $this->connException; // failed last attempt; don't bother
1455 } else { // actually retry this time
1456 $this->connException = null;
1457 $this->connErrorTime = 0;
1458 }
1459 }
1460 // Session keys expire after a while, so we renew them periodically
1461 $reAuth = ( ( time() - $this->sessionStarted ) > $this->authTTL );
1462 // Authenticate with proxy and get a session key...
1463 if ( !$this->conn || $reAuth ) {
1464 $this->sessionStarted = 0;
1465 $this->connContainerCache->clear();
1466 $cacheKey = $this->getCredsCacheKey( $this->auth->username );
1467 $creds = $this->srvCache->get( $cacheKey ); // credentials
1468 if ( is_array( $creds ) ) { // cache hit
1469 $this->auth->load_cached_credentials(
1470 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1471 $this->sessionStarted = time() - ceil( $this->authTTL / 2 ); // skew for worst case
1472 } else { // cache miss
1473 try {
1474 $this->auth->authenticate();
1475 $creds = $this->auth->export_credentials();
1476 $this->srvCache->add( $cacheKey, $creds, ceil( $this->authTTL / 2 ) ); // cache
1477 $this->sessionStarted = time();
1478 } catch ( CloudFilesException $e ) {
1479 $this->connException = $e; // don't keep re-trying
1480 $this->connErrorTime = time();
1481 throw $e; // throw it back
1482 }
1483 }
1484 if ( $this->conn ) { // re-authorizing?
1485 $this->conn->close(); // close active cURL handles in CF_Http object
1486 }
1487 $this->conn = new CF_Connection( $this->auth );
1488 }
1489
1490 return $this->conn;
1491 }
1492
1493 /**
1494 * Close the connection to the Swift proxy
1495 */
1496 protected function closeConnection() {
1497 if ( $this->conn ) {
1498 $this->conn->close(); // close active cURL handles in CF_Http object
1499 $this->conn = null;
1500 $this->sessionStarted = 0;
1501 $this->connContainerCache->clear();
1502 }
1503 }
1504
1505 /**
1506 * Get the cache key for a container
1507 *
1508 * @param string $username
1509 * @return string
1510 */
1511 private function getCredsCacheKey( $username ) {
1512 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1513 }
1514
1515 /**
1516 * Get a Swift container object, possibly from process cache.
1517 * Use $reCache if the file count or byte count is needed.
1518 *
1519 * @param string $container Container name
1520 * @param bool $bypassCache Bypass all caches and load from Swift
1521 * @return CF_Container
1522 * @throws CloudFilesException
1523 */
1524 protected function getContainer( $container, $bypassCache = false ) {
1525 $conn = $this->getConnection(); // Swift proxy connection
1526 if ( $bypassCache ) { // purge cache
1527 $this->connContainerCache->clear( $container );
1528 } elseif ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1529 $this->primeContainerCache( array( $container ) ); // check persistent cache
1530 }
1531 if ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1532 $contObj = $conn->get_container( $container );
1533 // NoSuchContainerException not thrown: container must exist
1534 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache it
1535 if ( !$bypassCache ) {
1536 $this->setContainerCache( $container, // update persistent cache
1537 array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
1538 );
1539 }
1540 }
1541
1542 return $this->connContainerCache->get( $container, 'obj' );
1543 }
1544
1545 /**
1546 * Create a Swift container
1547 *
1548 * @param string $container Container name
1549 * @return CF_Container
1550 * @throws CloudFilesException
1551 */
1552 protected function createContainer( $container ) {
1553 $conn = $this->getConnection(); // Swift proxy connection
1554 $contObj = $conn->create_container( $container );
1555 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache
1556 return $contObj;
1557 }
1558
1559 /**
1560 * Delete a Swift container
1561 *
1562 * @param string $container Container name
1563 * @throws CloudFilesException
1564 */
1565 protected function deleteContainer( $container ) {
1566 $conn = $this->getConnection(); // Swift proxy connection
1567 $this->connContainerCache->clear( $container ); // purge
1568 $conn->delete_container( $container );
1569 }
1570
1571 protected function doPrimeContainerCache( array $containerInfo ) {
1572 try {
1573 $conn = $this->getConnection(); // Swift proxy connection
1574 foreach ( $containerInfo as $container => $info ) {
1575 $contObj = new CF_Container( $conn->cfs_auth, $conn->cfs_http,
1576 $container, $info['count'], $info['bytes'] );
1577 $this->connContainerCache->set( $container, 'obj', $contObj );
1578 }
1579 } catch ( CloudFilesException $e ) { // some other exception?
1580 $this->handleException( $e, null, __METHOD__, array() );
1581 }
1582 }
1583
1584 /**
1585 * Log an unexpected exception for this backend.
1586 * This also sets the Status object to have a fatal error.
1587 *
1588 * @param Exception $e
1589 * @param Status $status null
1590 * @param string $func
1591 * @param array $params
1592 */
1593 protected function handleException( Exception $e, $status, $func, array $params ) {
1594 if ( $status instanceof Status ) {
1595 if ( $e instanceof AuthenticationException ) {
1596 $status->fatal( 'backend-fail-connect', $this->name );
1597 } else {
1598 $status->fatal( 'backend-fail-internal', $this->name );
1599 }
1600 }
1601 if ( $e->getMessage() ) {
1602 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1603 }
1604 if ( $e instanceof InvalidResponseException ) { // possibly a stale token
1605 $this->srvCache->delete( $this->getCredsCacheKey( $this->auth->username ) );
1606 $this->closeConnection(); // force a re-connect and re-auth next time
1607 }
1608 wfDebugLog( 'SwiftBackend',
1609 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1610 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1611 );
1612 }
1613 }
1614
1615 /**
1616 * @see FileBackendStoreOpHandle
1617 */
1618 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1619 /** @var CF_Async_Op */
1620 public $cfOp;
1621
1622 /** @var array */
1623 public $affectedObjects = array();
1624
1625 /**
1626 * @param SwiftFileBackend $backend
1627 * @param array $params
1628 * @param string $call
1629 * @param CF_Async_Op $cfOp
1630 */
1631 public function __construct(
1632 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1633 ) {
1634 $this->backend = $backend;
1635 $this->params = $params;
1636 $this->call = $call;
1637 $this->cfOp = $cfOp;
1638 }
1639 }
1640
1641 /**
1642 * SwiftFileBackend helper class to page through listings.
1643 * Swift also has a listing limit of 10,000 objects for sanity.
1644 * Do not use this class from places outside SwiftFileBackend.
1645 *
1646 * @ingroup FileBackend
1647 */
1648 abstract class SwiftFileBackendList implements Iterator {
1649 /** @var array List of path or (path,stat array) entries */
1650 protected $bufferIter = array();
1651
1652 /** @var string List items *after* this path */
1653 protected $bufferAfter = null;
1654
1655 /** @var int */
1656 protected $pos = 0;
1657
1658 /** @var array */
1659 protected $params = array();
1660
1661 /** @var SwiftFileBackend */
1662 protected $backend;
1663
1664 /** @var string Container name */
1665 protected $container;
1666
1667 /** @var string Storage directory */
1668 protected $dir;
1669
1670 /** @var int */
1671 protected $suffixStart;
1672
1673 const PAGE_SIZE = 9000; // file listing buffer size
1674
1675 /**
1676 * @param SwiftFileBackend $backend
1677 * @param string $fullCont Resolved container name
1678 * @param string $dir Resolved directory relative to container
1679 * @param array $params
1680 */
1681 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1682 $this->backend = $backend;
1683 $this->container = $fullCont;
1684 $this->dir = $dir;
1685 if ( substr( $this->dir, -1 ) === '/' ) {
1686 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1687 }
1688 if ( $this->dir == '' ) { // whole container
1689 $this->suffixStart = 0;
1690 } else { // dir within container
1691 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1692 }
1693 $this->params = $params;
1694 }
1695
1696 /**
1697 * @see Iterator::key()
1698 * @return int
1699 */
1700 public function key() {
1701 return $this->pos;
1702 }
1703
1704 /**
1705 * @see Iterator::next()
1706 */
1707 public function next() {
1708 // Advance to the next file in the page
1709 next( $this->bufferIter );
1710 ++$this->pos;
1711 // Check if there are no files left in this page and
1712 // advance to the next page if this page was not empty.
1713 if ( !$this->valid() && count( $this->bufferIter ) ) {
1714 $this->bufferIter = $this->pageFromList(
1715 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1716 ); // updates $this->bufferAfter
1717 }
1718 }
1719
1720 /**
1721 * @see Iterator::rewind()
1722 */
1723 public function rewind() {
1724 $this->pos = 0;
1725 $this->bufferAfter = null;
1726 $this->bufferIter = $this->pageFromList(
1727 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1728 ); // updates $this->bufferAfter
1729 }
1730
1731 /**
1732 * @see Iterator::valid()
1733 * @return bool
1734 */
1735 public function valid() {
1736 if ( $this->bufferIter === null ) {
1737 return false; // some failure?
1738 } else {
1739 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1740 }
1741 }
1742
1743 /**
1744 * Get the given list portion (page)
1745 *
1746 * @param string $container Resolved container name
1747 * @param string $dir Resolved path relative to container
1748 * @param string $after null
1749 * @param int $limit
1750 * @param array $params
1751 * @return Traversable|array
1752 */
1753 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1754 }
1755
1756 /**
1757 * Iterator for listing directories
1758 */
1759 class SwiftFileBackendDirList extends SwiftFileBackendList {
1760 /**
1761 * @see Iterator::current()
1762 * @return string|bool String (relative path) or false
1763 */
1764 public function current() {
1765 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1766 }
1767
1768 /**
1769 * @see SwiftFileBackendList::pageFromList()
1770 * @param string $container
1771 * @param string $dir
1772 * @param string $after
1773 * @param int $limit
1774 * @param array $params
1775 * @return array
1776 */
1777 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1778 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1779 }
1780 }
1781
1782 /**
1783 * Iterator for listing regular files
1784 */
1785 class SwiftFileBackendFileList extends SwiftFileBackendList {
1786 /**
1787 * @see Iterator::current()
1788 * @return string|bool String (relative path) or false
1789 */
1790 public function current() {
1791 list( $path, $stat ) = current( $this->bufferIter );
1792 $relPath = substr( $path, $this->suffixStart );
1793 if ( is_array( $stat ) ) {
1794 $storageDir = rtrim( $this->params['dir'], '/' );
1795 $this->backend->loadListingStatInternal( "$storageDir/$relPath", $stat );
1796 }
1797
1798 return $relPath;
1799 }
1800
1801 /**
1802 * @see SwiftFileBackendList::pageFromList()
1803 * @param string $container
1804 * @param string $dir
1805 * @param string $after
1806 * @param int $limit
1807 * @param array $params
1808 * @return array
1809 */
1810 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1811 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1812 }
1813 }