Merge "Parse duplicate-defaultsort error message"
[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 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 = StreamFile::contentTypeFromPath( $params['dst'] );
256 if ( !strlen( $obj->content_type ) ) { // special case
257 $obj->content_type = 'unknown/unknown';
258 }
259 // Set any other custom headers if requested
260 if ( isset( $params['headers'] ) ) {
261 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
262 }
263 if ( !empty( $params['async'] ) ) { // deferred
264 $op = $obj->write_async( $params['content'] );
265 $status->value = new SwiftFileOpHandle( $this, $params, 'Create', $op );
266 $status->value->affectedObjects[] = $obj;
267 } else { // actually write the object in Swift
268 $obj->write( $params['content'] );
269 $this->purgeCDNCache( array( $obj ) );
270 }
271 } catch ( CDNNotEnabledException $e ) {
272 // CDN not enabled; nothing to see here
273 } catch ( BadContentTypeException $e ) {
274 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
275 } catch ( CloudFilesException $e ) { // some other exception?
276 $this->handleException( $e, $status, __METHOD__, $params );
277 }
278
279 return $status;
280 }
281
282 /**
283 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
284 */
285 protected function _getResponseCreate( CF_Async_Op $cfOp, Status $status, array $params ) {
286 try {
287 $cfOp->getLastResponse();
288 } catch ( BadContentTypeException $e ) {
289 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
290 }
291 }
292
293 protected function doStoreInternal( array $params ) {
294 $status = Status::newGood();
295
296 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
297 if ( $dstRel === null ) {
298 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
299 return $status;
300 }
301
302 // (a) Check the destination container and object
303 try {
304 $dContObj = $this->getContainer( $dstCont );
305 } catch ( NoSuchContainerException $e ) {
306 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
307 return $status;
308 } catch ( CloudFilesException $e ) { // some other exception?
309 $this->handleException( $e, $status, __METHOD__, $params );
310 return $status;
311 }
312
313 // (b) Get a SHA-1 hash of the object
314 wfSuppressWarnings();
315 $sha1Hash = sha1_file( $params['src'] );
316 wfRestoreWarnings();
317 if ( $sha1Hash === false ) { // source doesn't exist?
318 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
319 return $status;
320 }
321 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
322
323 // (c) Actually store the object
324 try {
325 // Create a fresh CF_Object with no fields preloaded.
326 // We don't want to preserve headers, metadata, and such.
327 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
328 $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
329 // The MD5 here will be checked within Swift against its own MD5.
330 $obj->set_etag( md5_file( $params['src'] ) );
331 // Use the same content type as StreamFile for security
332 $obj->content_type = StreamFile::contentTypeFromPath( $params['dst'] );
333 if ( !strlen( $obj->content_type ) ) { // special case
334 $obj->content_type = 'unknown/unknown';
335 }
336 // Set any other custom headers if requested
337 if ( isset( $params['headers'] ) ) {
338 $obj->headers += $this->sanitizeHdrs( $params['headers'] );
339 }
340 if ( !empty( $params['async'] ) ) { // deferred
341 wfSuppressWarnings();
342 $fp = fopen( $params['src'], 'rb' );
343 wfRestoreWarnings();
344 if ( !$fp ) {
345 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
346 } else {
347 $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
348 $status->value = new SwiftFileOpHandle( $this, $params, 'Store', $op );
349 $status->value->resourcesToClose[] = $fp;
350 $status->value->affectedObjects[] = $obj;
351 }
352 } else { // actually write the object in Swift
353 $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
354 $this->purgeCDNCache( array( $obj ) );
355 }
356 } catch ( CDNNotEnabledException $e ) {
357 // CDN not enabled; nothing to see here
358 } catch ( BadContentTypeException $e ) {
359 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
360 } catch ( IOException $e ) {
361 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
362 } catch ( CloudFilesException $e ) { // some other exception?
363 $this->handleException( $e, $status, __METHOD__, $params );
364 }
365
366 return $status;
367 }
368
369 /**
370 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
371 */
372 protected function _getResponseStore( CF_Async_Op $cfOp, Status $status, array $params ) {
373 try {
374 $cfOp->getLastResponse();
375 } catch ( BadContentTypeException $e ) {
376 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
377 } catch ( IOException $e ) {
378 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
379 }
380 }
381
382 protected function doCopyInternal( array $params ) {
383 $status = Status::newGood();
384
385 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
386 if ( $srcRel === null ) {
387 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
388 return $status;
389 }
390
391 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
392 if ( $dstRel === null ) {
393 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
394 return $status;
395 }
396
397 // (a) Check the source/destination containers and destination object
398 try {
399 $sContObj = $this->getContainer( $srcCont );
400 $dContObj = $this->getContainer( $dstCont );
401 } catch ( NoSuchContainerException $e ) {
402 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
403 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
404 }
405 return $status;
406 } catch ( CloudFilesException $e ) { // some other exception?
407 $this->handleException( $e, $status, __METHOD__, $params );
408 return $status;
409 }
410
411 // (b) Actually copy the file to the destination
412 try {
413 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
414 $hdrs = array(); // source file headers to override with new values
415 // Set any other custom headers if requested
416 if ( isset( $params['headers'] ) ) {
417 $hdrs += $this->sanitizeHdrs( $params['headers'] );
418 }
419 if ( !empty( $params['async'] ) ) { // deferred
420 $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
421 $status->value = new SwiftFileOpHandle( $this, $params, 'Copy', $op );
422 $status->value->affectedObjects[] = $dstObj;
423 } else { // actually write the object in Swift
424 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
425 $this->purgeCDNCache( array( $dstObj ) );
426 }
427 } catch ( CDNNotEnabledException $e ) {
428 // CDN not enabled; nothing to see here
429 } catch ( NoSuchObjectException $e ) { // source object does not exist
430 if ( empty( $params['ignoreMissingSource'] ) ) {
431 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
432 }
433 } catch ( CloudFilesException $e ) { // some other exception?
434 $this->handleException( $e, $status, __METHOD__, $params );
435 }
436
437 return $status;
438 }
439
440 /**
441 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
442 */
443 protected function _getResponseCopy( CF_Async_Op $cfOp, Status $status, array $params ) {
444 try {
445 $cfOp->getLastResponse();
446 } catch ( NoSuchObjectException $e ) { // source object does not exist
447 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
448 }
449 }
450
451 protected function doMoveInternal( array $params ) {
452 $status = Status::newGood();
453
454 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
455 if ( $srcRel === null ) {
456 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
457 return $status;
458 }
459
460 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
461 if ( $dstRel === null ) {
462 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
463 return $status;
464 }
465
466 // (a) Check the source/destination containers and destination object
467 try {
468 $sContObj = $this->getContainer( $srcCont );
469 $dContObj = $this->getContainer( $dstCont );
470 } catch ( NoSuchContainerException $e ) {
471 if ( empty( $params['ignoreMissingSource'] ) || isset( $sContObj ) ) {
472 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
473 }
474 return $status;
475 } catch ( CloudFilesException $e ) { // some other exception?
476 $this->handleException( $e, $status, __METHOD__, $params );
477 return $status;
478 }
479
480 // (b) Actually move the file to the destination
481 try {
482 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
483 $dstObj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
484 $hdrs = array(); // source file headers to override with new values
485 // Set any other custom headers if requested
486 if ( isset( $params['headers'] ) ) {
487 $hdrs += $this->sanitizeHdrs( $params['headers'] );
488 }
489 if ( !empty( $params['async'] ) ) { // deferred
490 $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
491 $status->value = new SwiftFileOpHandle( $this, $params, 'Move', $op );
492 $status->value->affectedObjects[] = $srcObj;
493 $status->value->affectedObjects[] = $dstObj;
494 } else { // actually write the object in Swift
495 $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
496 $this->purgeCDNCache( array( $srcObj ) );
497 $this->purgeCDNCache( array( $dstObj ) );
498 }
499 } catch ( CDNNotEnabledException $e ) {
500 // CDN not enabled; nothing to see here
501 } catch ( NoSuchObjectException $e ) { // source object does not exist
502 if ( empty( $params['ignoreMissingSource'] ) ) {
503 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
504 }
505 } catch ( CloudFilesException $e ) { // some other exception?
506 $this->handleException( $e, $status, __METHOD__, $params );
507 }
508
509 return $status;
510 }
511
512 /**
513 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
514 */
515 protected function _getResponseMove( CF_Async_Op $cfOp, Status $status, array $params ) {
516 try {
517 $cfOp->getLastResponse();
518 } catch ( NoSuchObjectException $e ) { // source object does not exist
519 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
520 }
521 }
522
523 protected function doDeleteInternal( array $params ) {
524 $status = Status::newGood();
525
526 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
527 if ( $srcRel === null ) {
528 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
529 return $status;
530 }
531
532 try {
533 $sContObj = $this->getContainer( $srcCont );
534 $srcObj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
535 if ( !empty( $params['async'] ) ) { // deferred
536 $op = $sContObj->delete_object_async( $srcRel );
537 $status->value = new SwiftFileOpHandle( $this, $params, 'Delete', $op );
538 $status->value->affectedObjects[] = $srcObj;
539 } else { // actually write the object in Swift
540 $sContObj->delete_object( $srcRel );
541 $this->purgeCDNCache( array( $srcObj ) );
542 }
543 } catch ( CDNNotEnabledException $e ) {
544 // CDN not enabled; nothing to see here
545 } catch ( NoSuchContainerException $e ) {
546 if ( empty( $params['ignoreMissingSource'] ) ) {
547 $status->fatal( 'backend-fail-delete', $params['src'] );
548 }
549 } catch ( NoSuchObjectException $e ) {
550 if ( empty( $params['ignoreMissingSource'] ) ) {
551 $status->fatal( 'backend-fail-delete', $params['src'] );
552 }
553 } catch ( CloudFilesException $e ) { // some other exception?
554 $this->handleException( $e, $status, __METHOD__, $params );
555 }
556
557 return $status;
558 }
559
560 /**
561 * @see SwiftFileBackend::doExecuteOpHandlesInternal()
562 */
563 protected function _getResponseDelete( CF_Async_Op $cfOp, Status $status, array $params ) {
564 try {
565 $cfOp->getLastResponse();
566 } catch ( NoSuchContainerException $e ) {
567 $status->fatal( 'backend-fail-delete', $params['src'] );
568 } catch ( NoSuchObjectException $e ) {
569 if ( empty( $params['ignoreMissingSource'] ) ) {
570 $status->fatal( 'backend-fail-delete', $params['src'] );
571 }
572 }
573 }
574
575 protected function doDescribeInternal( array $params ) {
576 $status = Status::newGood();
577
578 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
579 if ( $srcRel === null ) {
580 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
581 return $status;
582 }
583
584 try {
585 $sContObj = $this->getContainer( $srcCont );
586 // Get the latest version of the current metadata
587 $srcObj = $sContObj->get_object( $srcRel,
588 $this->headersFromParams( array( 'latest' => true ) ) );
589 // Merge in the metadata updates...
590 if ( isset( $params['headers'] ) ) {
591 $srcObj->headers = $this->sanitizeHdrs( $params['headers'] ) + $srcObj->headers;
592 }
593 $srcObj->sync_metadata(); // save to Swift
594 $this->purgeCDNCache( array( $srcObj ) );
595 } catch ( CDNNotEnabledException $e ) {
596 // CDN not enabled; nothing to see here
597 } catch ( NoSuchContainerException $e ) {
598 $status->fatal( 'backend-fail-describe', $params['src'] );
599 } catch ( NoSuchObjectException $e ) {
600 $status->fatal( 'backend-fail-describe', $params['src'] );
601 } catch ( CloudFilesException $e ) { // some other exception?
602 $this->handleException( $e, $status, __METHOD__, $params );
603 }
604
605 return $status;
606 }
607
608 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
609 $status = Status::newGood();
610
611 // (a) Check if container already exists
612 try {
613 $this->getContainer( $fullCont );
614 // NoSuchContainerException not thrown: container must exist
615 return $status; // already exists
616 } catch ( NoSuchContainerException $e ) {
617 // NoSuchContainerException thrown: container does not exist
618 } catch ( CloudFilesException $e ) { // some other exception?
619 $this->handleException( $e, $status, __METHOD__, $params );
620 return $status;
621 }
622
623 // (b) Create container as needed
624 try {
625 $contObj = $this->createContainer( $fullCont );
626 if ( !empty( $params['noAccess'] ) ) {
627 // Make container private to end-users...
628 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
629 } else {
630 // Make container public to end-users...
631 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
632 }
633 if ( $this->swiftUseCDN ) { // Rackspace style CDN
634 $contObj->make_public( $this->swiftCDNExpiry );
635 }
636 } catch ( CDNNotEnabledException $e ) {
637 // CDN not enabled; nothing to see here
638 } catch ( CloudFilesException $e ) { // some other exception?
639 $this->handleException( $e, $status, __METHOD__, $params );
640 return $status;
641 }
642
643 return $status;
644 }
645
646 /**
647 * @see FileBackendStore::doSecureInternal()
648 * @return Status
649 */
650 protected function doSecureInternal( $fullCont, $dir, array $params ) {
651 $status = Status::newGood();
652 if ( empty( $params['noAccess'] ) ) {
653 return $status; // nothing to do
654 }
655
656 // Restrict container from end-users...
657 try {
658 // doPrepareInternal() should have been called,
659 // so the Swift container should already exist...
660 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
661 // NoSuchContainerException not thrown: container must exist
662
663 // Make container private to end-users...
664 $status->merge( $this->setContainerAccess(
665 $contObj,
666 array( $this->auth->username ), // read
667 array( $this->auth->username ) // write
668 ) );
669 if ( $this->swiftUseCDN && $contObj->is_public() ) { // Rackspace style CDN
670 $contObj->make_private();
671 }
672 } catch ( CDNNotEnabledException $e ) {
673 // CDN not enabled; nothing to see here
674 } catch ( CloudFilesException $e ) { // some other exception?
675 $this->handleException( $e, $status, __METHOD__, $params );
676 }
677
678 return $status;
679 }
680
681 /**
682 * @see FileBackendStore::doPublishInternal()
683 * @return Status
684 */
685 protected function doPublishInternal( $fullCont, $dir, array $params ) {
686 $status = Status::newGood();
687
688 // Unrestrict container from end-users...
689 try {
690 // doPrepareInternal() should have been called,
691 // so the Swift container should already exist...
692 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
693 // NoSuchContainerException not thrown: container must exist
694
695 // Make container public to end-users...
696 if ( $this->swiftAnonUser != '' ) {
697 $status->merge( $this->setContainerAccess(
698 $contObj,
699 array( $this->auth->username, $this->swiftAnonUser ), // read
700 array( $this->auth->username, $this->swiftAnonUser ) // write
701 ) );
702 } else {
703 $status->merge( $this->setContainerAccess(
704 $contObj,
705 array( $this->auth->username, '.r:*' ), // read
706 array( $this->auth->username ) // write
707 ) );
708 }
709 if ( $this->swiftUseCDN && !$contObj->is_public() ) { // Rackspace style CDN
710 $contObj->make_public();
711 }
712 } catch ( CDNNotEnabledException $e ) {
713 // CDN not enabled; nothing to see here
714 } catch ( CloudFilesException $e ) { // some other exception?
715 $this->handleException( $e, $status, __METHOD__, $params );
716 }
717
718 return $status;
719 }
720
721 protected function doCleanInternal( $fullCont, $dir, array $params ) {
722 $status = Status::newGood();
723
724 // Only containers themselves can be removed, all else is virtual
725 if ( $dir != '' ) {
726 return $status; // nothing to do
727 }
728
729 // (a) Check the container
730 try {
731 $contObj = $this->getContainer( $fullCont, true );
732 } catch ( NoSuchContainerException $e ) {
733 return $status; // ok, nothing to do
734 } catch ( CloudFilesException $e ) { // some other exception?
735 $this->handleException( $e, $status, __METHOD__, $params );
736 return $status;
737 }
738
739 // (b) Delete the container if empty
740 if ( $contObj->object_count == 0 ) {
741 try {
742 $this->deleteContainer( $fullCont );
743 } catch ( NoSuchContainerException $e ) {
744 return $status; // race?
745 } catch ( NonEmptyContainerException $e ) {
746 return $status; // race? consistency delay?
747 } catch ( CloudFilesException $e ) { // some other exception?
748 $this->handleException( $e, $status, __METHOD__, $params );
749 return $status;
750 }
751 }
752
753 return $status;
754 }
755
756 protected function doGetFileStat( array $params ) {
757 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
758 if ( $srcRel === null ) {
759 return false; // invalid storage path
760 }
761
762 $stat = false;
763 try {
764 $contObj = $this->getContainer( $srcCont );
765 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
766 $this->addMissingMetadata( $srcObj, $params['src'] );
767 $stat = array(
768 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
769 'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
770 'size' => (int)$srcObj->content_length,
771 'sha1' => $srcObj->getMetadataValue( 'Sha1base36' )
772 );
773 } catch ( NoSuchContainerException $e ) {
774 } catch ( NoSuchObjectException $e ) {
775 } catch ( CloudFilesException $e ) { // some other exception?
776 $stat = null;
777 $this->handleException( $e, null, __METHOD__, $params );
778 }
779
780 return $stat;
781 }
782
783 /**
784 * Fill in any missing object metadata and save it to Swift
785 *
786 * @param CF_Object $obj
787 * @param string $path Storage path to object
788 * @return bool Success
789 * @throws Exception cloudfiles exceptions
790 */
791 protected function addMissingMetadata( CF_Object $obj, $path ) {
792 if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
793 return true; // nothing to do
794 }
795 wfProfileIn( __METHOD__ );
796 trigger_error( "$path was not stored with SHA-1 metadata.", E_USER_WARNING );
797 $status = Status::newGood();
798 $scopeLockS = $this->getScopedFileLocks( array( $path ), LockManager::LOCK_UW, $status );
799 if ( $status->isOK() ) {
800 $tmpFile = $this->getLocalCopy( array( 'src' => $path, 'latest' => 1 ) );
801 if ( $tmpFile ) {
802 $hash = $tmpFile->getSha1Base36();
803 if ( $hash !== false ) {
804 $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
805 $obj->sync_metadata(); // save to Swift
806 wfProfileOut( __METHOD__ );
807 return true; // success
808 }
809 }
810 }
811 trigger_error( "Unable to set SHA-1 metadata for $path", E_USER_WARNING );
812 $obj->setMetadataValues( array( 'Sha1base36' => false ) );
813 wfProfileOut( __METHOD__ );
814 return false; // failed
815 }
816
817 protected function doGetFileContentsMulti( array $params ) {
818 $contents = array();
819
820 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
821 // Blindly create tmp files and stream to them, catching any exception if the file does
822 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
823 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
824 $cfOps = array(); // (path => CF_Async_Op)
825
826 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
827 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
828 if ( $srcRel === null ) {
829 $contents[$path] = false;
830 continue;
831 }
832 $data = false;
833 try {
834 $sContObj = $this->getContainer( $srcCont );
835 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
836 // Create a new temporary memory file...
837 $handle = fopen( 'php://temp', 'wb' );
838 if ( $handle ) {
839 $headers = $this->headersFromParams( $params );
840 if ( count( $pathBatch ) > 1 ) {
841 $cfOps[$path] = $obj->stream_async( $handle, $headers );
842 $cfOps[$path]->_file_handle = $handle; // close this later
843 } else {
844 $obj->stream( $handle, $headers );
845 rewind( $handle ); // start from the beginning
846 $data = stream_get_contents( $handle );
847 fclose( $handle );
848 }
849 } else {
850 $data = false;
851 }
852 } catch ( NoSuchContainerException $e ) {
853 $data = false;
854 } catch ( NoSuchObjectException $e ) {
855 $data = false;
856 } catch ( CloudFilesException $e ) { // some other exception?
857 $data = false;
858 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
859 }
860 $contents[$path] = $data;
861 }
862
863 $batch = new CF_Async_Op_Batch( $cfOps );
864 $cfOps = $batch->execute();
865 foreach ( $cfOps as $path => $cfOp ) {
866 try {
867 $cfOp->getLastResponse();
868 rewind( $cfOp->_file_handle ); // start from the beginning
869 $contents[$path] = stream_get_contents( $cfOp->_file_handle );
870 } catch ( NoSuchContainerException $e ) {
871 $contents[$path] = false;
872 } catch ( NoSuchObjectException $e ) {
873 $contents[$path] = false;
874 } catch ( CloudFilesException $e ) { // some other exception?
875 $contents[$path] = false;
876 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
877 }
878 fclose( $cfOp->_file_handle ); // close open handle
879 }
880 }
881
882 return $contents;
883 }
884
885 /**
886 * @see FileBackendStore::doDirectoryExists()
887 * @return bool|null
888 */
889 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
890 try {
891 $container = $this->getContainer( $fullCont );
892 $prefix = ( $dir == '' ) ? null : "{$dir}/";
893 return ( count( $container->list_objects( 1, null, $prefix ) ) > 0 );
894 } catch ( NoSuchContainerException $e ) {
895 return false;
896 } catch ( CloudFilesException $e ) { // some other exception?
897 $this->handleException( $e, null, __METHOD__,
898 array( 'cont' => $fullCont, 'dir' => $dir ) );
899 }
900
901 return null; // error
902 }
903
904 /**
905 * @see FileBackendStore::getDirectoryListInternal()
906 * @return SwiftFileBackendDirList
907 */
908 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
909 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
910 }
911
912 /**
913 * @see FileBackendStore::getFileListInternal()
914 * @return SwiftFileBackendFileList
915 */
916 public function getFileListInternal( $fullCont, $dir, array $params ) {
917 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
918 }
919
920 /**
921 * Do not call this function outside of SwiftFileBackendFileList
922 *
923 * @param string $fullCont Resolved container name
924 * @param string $dir Resolved storage directory with no trailing slash
925 * @param string|null $after Storage path of file to list items after
926 * @param integer $limit Max number of items to list
927 * @param array $params Parameters for getDirectoryList()
928 * @return Array List of resolved paths of directories directly under $dir
929 * @throws FileBackendError
930 */
931 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
932 $dirs = array();
933 if ( $after === INF ) {
934 return $dirs; // nothing more
935 }
936
937 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
938 try {
939 $container = $this->getContainer( $fullCont );
940 $prefix = ( $dir == '' ) ? null : "{$dir}/";
941 // Non-recursive: only list dirs right under $dir
942 if ( !empty( $params['topOnly'] ) ) {
943 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
944 foreach ( $objects as $object ) { // files and directories
945 if ( substr( $object, -1 ) === '/' ) {
946 $dirs[] = $object; // directories end in '/'
947 }
948 }
949 // Recursive: list all dirs under $dir and its subdirs
950 } else {
951 // Get directory from last item of prior page
952 $lastDir = $this->getParentDir( $after ); // must be first page
953 $objects = $container->list_objects( $limit, $after, $prefix );
954 foreach ( $objects as $object ) { // files
955 $objectDir = $this->getParentDir( $object ); // directory of object
956 if ( $objectDir !== false && $objectDir !== $dir ) {
957 // Swift stores paths in UTF-8, using binary sorting.
958 // See function "create_container_table" in common/db.py.
959 // If a directory is not "greater" than the last one,
960 // then it was already listed by the calling iterator.
961 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
962 $pDir = $objectDir;
963 do { // add dir and all its parent dirs
964 $dirs[] = "{$pDir}/";
965 $pDir = $this->getParentDir( $pDir );
966 } while ( $pDir !== false // sanity
967 && strcmp( $pDir, $lastDir ) > 0 // not done already
968 && strlen( $pDir ) > strlen( $dir ) // within $dir
969 );
970 }
971 $lastDir = $objectDir;
972 }
973 }
974 }
975 // Page on the unfiltered directory listing (what is returned may be filtered)
976 if ( count( $objects ) < $limit ) {
977 $after = INF; // avoid a second RTT
978 } else {
979 $after = end( $objects ); // update last item
980 }
981 } catch ( NoSuchContainerException $e ) {
982 } catch ( CloudFilesException $e ) { // some other exception?
983 $this->handleException( $e, null, __METHOD__,
984 array( 'cont' => $fullCont, 'dir' => $dir ) );
985 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
986 }
987
988 return $dirs;
989 }
990
991 protected function getParentDir( $path ) {
992 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
993 }
994
995 /**
996 * Do not call this function outside of SwiftFileBackendFileList
997 *
998 * @param string $fullCont Resolved container name
999 * @param string $dir Resolved storage directory with no trailing slash
1000 * @param string|null $after Storage path of file to list items after
1001 * @param integer $limit Max number of items to list
1002 * @param array $params Parameters for getDirectoryList()
1003 * @return Array List of resolved paths of files under $dir
1004 * @throws FileBackendError
1005 */
1006 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
1007 $files = array();
1008 if ( $after === INF ) {
1009 return $files; // nothing more
1010 }
1011
1012 $section = new ProfileSection( __METHOD__ . '-' . $this->name );
1013 try {
1014 $container = $this->getContainer( $fullCont );
1015 $prefix = ( $dir == '' ) ? null : "{$dir}/";
1016 // Non-recursive: only list files right under $dir
1017 if ( !empty( $params['topOnly'] ) ) { // files and dirs
1018 if ( !empty( $params['adviseStat'] ) ) {
1019 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1020 // Note: get_objects() does not include directories
1021 $objects = $this->loadObjectListing( $params, $dir,
1022 $container->get_objects( $limit, $after, $prefix, null, '/' ) );
1023 $files = $objects;
1024 } else {
1025 $objects = $container->list_objects( $limit, $after, $prefix, null, '/' );
1026 foreach ( $objects as $object ) { // files and directories
1027 if ( substr( $object, -1 ) !== '/' ) {
1028 $files[] = $object; // directories end in '/'
1029 }
1030 }
1031 }
1032 // Recursive: list all files under $dir and its subdirs
1033 } else { // files
1034 if ( !empty( $params['adviseStat'] ) ) {
1035 $limit = min( $limit, self::CACHE_CHEAP_SIZE );
1036 $objects = $this->loadObjectListing( $params, $dir,
1037 $container->get_objects( $limit, $after, $prefix ) );
1038 } else {
1039 $objects = $container->list_objects( $limit, $after, $prefix );
1040 }
1041 $files = $objects;
1042 }
1043 // Page on the unfiltered object listing (what is returned may be filtered)
1044 if ( count( $objects ) < $limit ) {
1045 $after = INF; // avoid a second RTT
1046 } else {
1047 $after = end( $objects ); // update last item
1048 }
1049 } catch ( NoSuchContainerException $e ) {
1050 } catch ( CloudFilesException $e ) { // some other exception?
1051 $this->handleException( $e, null, __METHOD__,
1052 array( 'cont' => $fullCont, 'dir' => $dir ) );
1053 throw new FileBackendError( "Got " . get_class( $e ) . " exception." );
1054 }
1055
1056 return $files;
1057 }
1058
1059 /**
1060 * Load a list of objects that belong under $dir into stat cache
1061 * and return a list of the names of the objects in the same order.
1062 *
1063 * @param array $params Parameters for getDirectoryList()
1064 * @param string $dir Resolved container directory path
1065 * @param array $cfObjects List of CF_Object items
1066 * @return array List of object names
1067 */
1068 private function loadObjectListing( array $params, $dir, array $cfObjects ) {
1069 $names = array();
1070 $storageDir = rtrim( $params['dir'], '/' );
1071 $suffixStart = ( $dir === '' ) ? 0 : strlen( $dir ) + 1; // size of "path/to/dir/"
1072 // Iterate over the list *backwards* as this primes the stat cache, which is LRU.
1073 // If this fills the cache and the caller stats an uncached file before stating
1074 // the ones on the listing, there would be zero cache hits if this went forwards.
1075 for ( end( $cfObjects ); key( $cfObjects ) !== null; prev( $cfObjects ) ) {
1076 $object = current( $cfObjects );
1077 $path = "{$storageDir}/" . substr( $object->name, $suffixStart );
1078 $val = array(
1079 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
1080 'mtime' => wfTimestamp( TS_MW, $object->last_modified ),
1081 'size' => (int)$object->content_length,
1082 'latest' => false // eventually consistent
1083 );
1084 $this->cheapCache->set( $path, 'stat', $val );
1085 $names[] = $object->name;
1086 }
1087 return array_reverse( $names ); // keep the paths in original order
1088 }
1089
1090 protected function doGetFileSha1base36( array $params ) {
1091 $stat = $this->getFileStat( $params );
1092 if ( $stat ) {
1093 if ( !isset( $stat['sha1'] ) ) {
1094 // Stat entries filled by file listings don't include SHA1
1095 $this->clearCache( array( $params['src'] ) );
1096 $stat = $this->getFileStat( $params );
1097 }
1098 return $stat['sha1'];
1099 } else {
1100 return false;
1101 }
1102 }
1103
1104 protected function doStreamFile( array $params ) {
1105 $status = Status::newGood();
1106
1107 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1108 if ( $srcRel === null ) {
1109 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1110 }
1111
1112 try {
1113 $cont = $this->getContainer( $srcCont );
1114 } catch ( NoSuchContainerException $e ) {
1115 $status->fatal( 'backend-fail-stream', $params['src'] );
1116 return $status;
1117 } catch ( CloudFilesException $e ) { // some other exception?
1118 $this->handleException( $e, $status, __METHOD__, $params );
1119 return $status;
1120 }
1121
1122 try {
1123 $output = fopen( 'php://output', 'wb' );
1124 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD
1125 $obj->stream( $output, $this->headersFromParams( $params ) );
1126 } catch ( NoSuchObjectException $e ) {
1127 $status->fatal( 'backend-fail-stream', $params['src'] );
1128 } catch ( CloudFilesException $e ) { // some other exception?
1129 $this->handleException( $e, $status, __METHOD__, $params );
1130 }
1131
1132 return $status;
1133 }
1134
1135 protected function doGetLocalCopyMulti( array $params ) {
1136 $tmpFiles = array();
1137
1138 $ep = array_diff_key( $params, array( 'srcs' => 1 ) ); // for error logging
1139 // Blindly create tmp files and stream to them, catching any exception if the file does
1140 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1141 foreach ( array_chunk( $params['srcs'], $params['concurrency'] ) as $pathBatch ) {
1142 $cfOps = array(); // (path => CF_Async_Op)
1143
1144 foreach ( $pathBatch as $path ) { // each path in this concurrent batch
1145 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1146 if ( $srcRel === null ) {
1147 $tmpFiles[$path] = null;
1148 continue;
1149 }
1150 $tmpFile = null;
1151 try {
1152 $sContObj = $this->getContainer( $srcCont );
1153 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1154 // Get source file extension
1155 $ext = FileBackend::extensionFromPath( $path );
1156 // Create a new temporary file...
1157 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
1158 if ( $tmpFile ) {
1159 $handle = fopen( $tmpFile->getPath(), 'wb' );
1160 if ( $handle ) {
1161 $headers = $this->headersFromParams( $params );
1162 if ( count( $pathBatch ) > 1 ) {
1163 $cfOps[$path] = $obj->stream_async( $handle, $headers );
1164 $cfOps[$path]->_file_handle = $handle; // close this later
1165 } else {
1166 $obj->stream( $handle, $headers );
1167 fclose( $handle );
1168 }
1169 } else {
1170 $tmpFile = null;
1171 }
1172 }
1173 } catch ( NoSuchContainerException $e ) {
1174 $tmpFile = null;
1175 } catch ( NoSuchObjectException $e ) {
1176 $tmpFile = null;
1177 } catch ( CloudFilesException $e ) { // some other exception?
1178 $tmpFile = null;
1179 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1180 }
1181 $tmpFiles[$path] = $tmpFile;
1182 }
1183
1184 $batch = new CF_Async_Op_Batch( $cfOps );
1185 $cfOps = $batch->execute();
1186 foreach ( $cfOps as $path => $cfOp ) {
1187 try {
1188 $cfOp->getLastResponse();
1189 } catch ( NoSuchContainerException $e ) {
1190 $tmpFiles[$path] = null;
1191 } catch ( NoSuchObjectException $e ) {
1192 $tmpFiles[$path] = null;
1193 } catch ( CloudFilesException $e ) { // some other exception?
1194 $tmpFiles[$path] = null;
1195 $this->handleException( $e, null, __METHOD__, array( 'src' => $path ) + $ep );
1196 }
1197 fclose( $cfOp->_file_handle ); // close open handle
1198 }
1199 }
1200
1201 return $tmpFiles;
1202 }
1203
1204 public function getFileHttpUrl( array $params ) {
1205 if ( $this->swiftTempUrlKey != '' ||
1206 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' ) )
1207 {
1208 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1209 if ( $srcRel === null ) {
1210 return null; // invalid path
1211 }
1212 try {
1213 $ttl = isset( $params['ttl'] ) ? $params['ttl'] : 86400;
1214 $sContObj = $this->getContainer( $srcCont );
1215 $obj = new CF_Object( $sContObj, $srcRel, false, false ); // skip HEAD
1216 if ( $this->swiftTempUrlKey != '' ) {
1217 return $obj->get_temp_url( $this->swiftTempUrlKey, $ttl, "GET" );
1218 } else { // give S3 API URL for rgw
1219 $expires = time() + $ttl;
1220 // Path for signature starts with the bucket
1221 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1222 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1223 // Calculate the hash
1224 $signature = base64_encode( hash_hmac(
1225 'sha1',
1226 "GET\n\n\n{$expires}\n{$spath}",
1227 $this->rgwS3SecretKey,
1228 true // raw
1229 ) );
1230 // See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1231 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1232 return wfAppendQuery(
1233 str_replace( '/swift/v1', '', // S3 API is the rgw default
1234 $sContObj->cfs_http->getStorageUrl() . $spath ),
1235 array(
1236 'Signature' => $signature,
1237 'Expires' => $expires,
1238 'AWSAccessKeyId' => $this->rgwS3AccessKey )
1239 );
1240 }
1241 } catch ( NoSuchContainerException $e ) {
1242 } catch ( CloudFilesException $e ) { // some other exception?
1243 $this->handleException( $e, null, __METHOD__, $params );
1244 }
1245 }
1246 return null;
1247 }
1248
1249 protected function directoriesAreVirtual() {
1250 return true;
1251 }
1252
1253 /**
1254 * Get headers to send to Swift when reading a file based
1255 * on a FileBackend params array, e.g. that of getLocalCopy().
1256 * $params is currently only checked for a 'latest' flag.
1257 *
1258 * @param array $params
1259 * @return Array
1260 */
1261 protected function headersFromParams( array $params ) {
1262 $hdrs = array();
1263 if ( !empty( $params['latest'] ) ) {
1264 $hdrs[] = 'X-Newest: true';
1265 }
1266 return $hdrs;
1267 }
1268
1269 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1270 $statuses = array();
1271
1272 $cfOps = array(); // list of CF_Async_Op objects
1273 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1274 $cfOps[$index] = $fileOpHandle->cfOp;
1275 }
1276 $batch = new CF_Async_Op_Batch( $cfOps );
1277
1278 $cfOps = $batch->execute();
1279 foreach ( $cfOps as $index => $cfOp ) {
1280 $status = Status::newGood();
1281 $function = '_getResponse' . $fileOpHandles[$index]->call;
1282 try { // catch exceptions; update status
1283 $this->$function( $cfOp, $status, $fileOpHandles[$index]->params );
1284 $this->purgeCDNCache( $fileOpHandles[$index]->affectedObjects );
1285 } catch ( CloudFilesException $e ) { // some other exception?
1286 $this->handleException( $e, $status,
1287 __CLASS__ . ":$function", $fileOpHandles[$index]->params );
1288 }
1289 $statuses[$index] = $status;
1290 }
1291
1292 return $statuses;
1293 }
1294
1295 /**
1296 * Set read/write permissions for a Swift container.
1297 *
1298 * $readGrps is a list of the possible criteria for a request to have
1299 * access to read a container. Each item is one of the following formats:
1300 * - account:user : Grants access if the request is by the given user
1301 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1302 * matches the expression and the request is not for a listing.
1303 * Setting this to '*' effectively makes a container public.
1304 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1305 * matches the expression and the request is for a listing.
1306 *
1307 * $writeGrps is a list of the possible criteria for a request to have
1308 * access to write to a container. Each item is of the following format:
1309 * - account:user : Grants access if the request is by the given user
1310 *
1311 * @see http://swift.openstack.org/misc.html#acls
1312 *
1313 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1314 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1315 *
1316 * @param CF_Container $contObj Swift container
1317 * @param array $readGrps List of read access routes
1318 * @param array $writeGrps List of write access routes
1319 * @return Status
1320 */
1321 protected function setContainerAccess(
1322 CF_Container $contObj, array $readGrps, array $writeGrps
1323 ) {
1324 $creds = $contObj->cfs_auth->export_credentials();
1325
1326 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name );
1327
1328 // Note: 10 second timeout consistent with php-cloudfiles
1329 $req = MWHttpRequest::factory( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
1330 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
1331 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
1332 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
1333
1334 return $req->execute(); // should return 204
1335 }
1336
1337 /**
1338 * Purge the CDN cache of affected objects if CDN caching is enabled.
1339 * This is for Rackspace/Akamai CDNs.
1340 *
1341 * @param array $objects List of CF_Object items
1342 * @return void
1343 */
1344 public function purgeCDNCache( array $objects ) {
1345 if ( $this->swiftUseCDN && $this->swiftCDNPurgable ) {
1346 foreach ( $objects as $object ) {
1347 try {
1348 $object->purge_from_cdn();
1349 } catch ( CDNNotEnabledException $e ) {
1350 // CDN not enabled; nothing to see here
1351 } catch ( CloudFilesException $e ) {
1352 $this->handleException( $e, null, __METHOD__,
1353 array( 'cont' => $object->container->name, 'obj' => $object->name ) );
1354 }
1355 }
1356 }
1357 }
1358
1359 /**
1360 * Get an authenticated connection handle to the Swift proxy
1361 *
1362 * @throws CloudFilesException
1363 * @throws CloudFilesException|Exception
1364 * @return CF_Connection|bool False on failure
1365 */
1366 protected function getConnection() {
1367 if ( $this->connException instanceof CloudFilesException ) {
1368 if ( ( time() - $this->connErrorTime ) < 60 ) {
1369 throw $this->connException; // failed last attempt; don't bother
1370 } else { // actually retry this time
1371 $this->connException = null;
1372 $this->connErrorTime = 0;
1373 }
1374 }
1375 // Session keys expire after a while, so we renew them periodically
1376 $reAuth = ( ( time() - $this->sessionStarted ) > $this->authTTL );
1377 // Authenticate with proxy and get a session key...
1378 if ( !$this->conn || $reAuth ) {
1379 $this->sessionStarted = 0;
1380 $this->connContainerCache->clear();
1381 $cacheKey = $this->getCredsCacheKey( $this->auth->username );
1382 $creds = $this->srvCache->get( $cacheKey ); // credentials
1383 if ( is_array( $creds ) ) { // cache hit
1384 $this->auth->load_cached_credentials(
1385 $creds['auth_token'], $creds['storage_url'], $creds['cdnm_url'] );
1386 $this->sessionStarted = time() - ceil( $this->authTTL / 2 ); // skew for worst case
1387 } else { // cache miss
1388 try {
1389 $this->auth->authenticate();
1390 $creds = $this->auth->export_credentials();
1391 $this->srvCache->add( $cacheKey, $creds, ceil( $this->authTTL / 2 ) ); // cache
1392 $this->sessionStarted = time();
1393 } catch ( CloudFilesException $e ) {
1394 $this->connException = $e; // don't keep re-trying
1395 $this->connErrorTime = time();
1396 throw $e; // throw it back
1397 }
1398 }
1399 if ( $this->conn ) { // re-authorizing?
1400 $this->conn->close(); // close active cURL handles in CF_Http object
1401 }
1402 $this->conn = new CF_Connection( $this->auth );
1403 }
1404 return $this->conn;
1405 }
1406
1407 /**
1408 * Close the connection to the Swift proxy
1409 *
1410 * @return void
1411 */
1412 protected function closeConnection() {
1413 if ( $this->conn ) {
1414 $this->conn->close(); // close active cURL handles in CF_Http object
1415 $this->conn = null;
1416 $this->sessionStarted = 0;
1417 $this->connContainerCache->clear();
1418 }
1419 }
1420
1421 /**
1422 * Get the cache key for a container
1423 *
1424 * @param string $username
1425 * @return string
1426 */
1427 private function getCredsCacheKey( $username ) {
1428 return wfMemcKey( 'backend', $this->getName(), 'usercreds', $username );
1429 }
1430
1431 /**
1432 * Get a Swift container object, possibly from process cache.
1433 * Use $reCache if the file count or byte count is needed.
1434 *
1435 * @param string $container Container name
1436 * @param bool $bypassCache Bypass all caches and load from Swift
1437 * @return CF_Container
1438 * @throws CloudFilesException
1439 */
1440 protected function getContainer( $container, $bypassCache = false ) {
1441 $conn = $this->getConnection(); // Swift proxy connection
1442 if ( $bypassCache ) { // purge cache
1443 $this->connContainerCache->clear( $container );
1444 } elseif ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1445 $this->primeContainerCache( array( $container ) ); // check persistent cache
1446 }
1447 if ( !$this->connContainerCache->has( $container, 'obj' ) ) {
1448 $contObj = $conn->get_container( $container );
1449 // NoSuchContainerException not thrown: container must exist
1450 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache it
1451 if ( !$bypassCache ) {
1452 $this->setContainerCache( $container, // update persistent cache
1453 array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
1454 );
1455 }
1456 }
1457 return $this->connContainerCache->get( $container, 'obj' );
1458 }
1459
1460 /**
1461 * Create a Swift container
1462 *
1463 * @param string $container Container name
1464 * @return CF_Container
1465 * @throws CloudFilesException
1466 */
1467 protected function createContainer( $container ) {
1468 $conn = $this->getConnection(); // Swift proxy connection
1469 $contObj = $conn->create_container( $container );
1470 $this->connContainerCache->set( $container, 'obj', $contObj ); // cache
1471 return $contObj;
1472 }
1473
1474 /**
1475 * Delete a Swift container
1476 *
1477 * @param string $container Container name
1478 * @return void
1479 * @throws CloudFilesException
1480 */
1481 protected function deleteContainer( $container ) {
1482 $conn = $this->getConnection(); // Swift proxy connection
1483 $this->connContainerCache->clear( $container ); // purge
1484 $conn->delete_container( $container );
1485 }
1486
1487 protected function doPrimeContainerCache( array $containerInfo ) {
1488 try {
1489 $conn = $this->getConnection(); // Swift proxy connection
1490 foreach ( $containerInfo as $container => $info ) {
1491 $contObj = new CF_Container( $conn->cfs_auth, $conn->cfs_http,
1492 $container, $info['count'], $info['bytes'] );
1493 $this->connContainerCache->set( $container, 'obj', $contObj );
1494 }
1495 } catch ( CloudFilesException $e ) { // some other exception?
1496 $this->handleException( $e, null, __METHOD__, array() );
1497 }
1498 }
1499
1500 /**
1501 * Log an unexpected exception for this backend.
1502 * This also sets the Status object to have a fatal error.
1503 *
1504 * @param Exception $e
1505 * @param Status $status|null
1506 * @param string $func
1507 * @param array $params
1508 * @return void
1509 */
1510 protected function handleException( Exception $e, $status, $func, array $params ) {
1511 if ( $status instanceof Status ) {
1512 if ( $e instanceof AuthenticationException ) {
1513 $status->fatal( 'backend-fail-connect', $this->name );
1514 } else {
1515 $status->fatal( 'backend-fail-internal', $this->name );
1516 }
1517 }
1518 if ( $e->getMessage() ) {
1519 trigger_error( "$func: " . $e->getMessage(), E_USER_WARNING );
1520 }
1521 if ( $e instanceof InvalidResponseException ) { // possibly a stale token
1522 $this->srvCache->delete( $this->getCredsCacheKey( $this->auth->username ) );
1523 $this->closeConnection(); // force a re-connect and re-auth next time
1524 }
1525 wfDebugLog( 'SwiftBackend',
1526 get_class( $e ) . " in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1527 ( $e->getMessage() ? ": {$e->getMessage()}" : "" )
1528 );
1529 }
1530 }
1531
1532 /**
1533 * @see FileBackendStoreOpHandle
1534 */
1535 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1536 /** @var CF_Async_Op */
1537 public $cfOp;
1538 /** @var Array */
1539 public $affectedObjects = array();
1540
1541 /**
1542 * @param SwiftFileBackend $backend
1543 * @param array $params
1544 * @param string $call
1545 * @param CF_Async_Op $cfOp
1546 */
1547 public function __construct(
1548 SwiftFileBackend $backend, array $params, $call, CF_Async_Op $cfOp
1549 ) {
1550 $this->backend = $backend;
1551 $this->params = $params;
1552 $this->call = $call;
1553 $this->cfOp = $cfOp;
1554 }
1555 }
1556
1557 /**
1558 * SwiftFileBackend helper class to page through listings.
1559 * Swift also has a listing limit of 10,000 objects for sanity.
1560 * Do not use this class from places outside SwiftFileBackend.
1561 *
1562 * @ingroup FileBackend
1563 */
1564 abstract class SwiftFileBackendList implements Iterator {
1565 /** @var Array */
1566 protected $bufferIter = array();
1567 protected $bufferAfter = null; // string; list items *after* this path
1568 protected $pos = 0; // integer
1569 /** @var Array */
1570 protected $params = array();
1571
1572 /** @var SwiftFileBackend */
1573 protected $backend;
1574 protected $container; // string; container name
1575 protected $dir; // string; storage directory
1576 protected $suffixStart; // integer
1577
1578 const PAGE_SIZE = 9000; // file listing buffer size
1579
1580 /**
1581 * @param SwiftFileBackend $backend
1582 * @param string $fullCont Resolved container name
1583 * @param string $dir Resolved directory relative to container
1584 * @param array $params
1585 */
1586 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1587 $this->backend = $backend;
1588 $this->container = $fullCont;
1589 $this->dir = $dir;
1590 if ( substr( $this->dir, -1 ) === '/' ) {
1591 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1592 }
1593 if ( $this->dir == '' ) { // whole container
1594 $this->suffixStart = 0;
1595 } else { // dir within container
1596 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1597 }
1598 $this->params = $params;
1599 }
1600
1601 /**
1602 * @see Iterator::key()
1603 * @return integer
1604 */
1605 public function key() {
1606 return $this->pos;
1607 }
1608
1609 /**
1610 * @see Iterator::next()
1611 * @return void
1612 */
1613 public function next() {
1614 // Advance to the next file in the page
1615 next( $this->bufferIter );
1616 ++$this->pos;
1617 // Check if there are no files left in this page and
1618 // advance to the next page if this page was not empty.
1619 if ( !$this->valid() && count( $this->bufferIter ) ) {
1620 $this->bufferIter = $this->pageFromList(
1621 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1622 ); // updates $this->bufferAfter
1623 }
1624 }
1625
1626 /**
1627 * @see Iterator::rewind()
1628 * @return void
1629 */
1630 public function rewind() {
1631 $this->pos = 0;
1632 $this->bufferAfter = null;
1633 $this->bufferIter = $this->pageFromList(
1634 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1635 ); // updates $this->bufferAfter
1636 }
1637
1638 /**
1639 * @see Iterator::valid()
1640 * @return bool
1641 */
1642 public function valid() {
1643 if ( $this->bufferIter === null ) {
1644 return false; // some failure?
1645 } else {
1646 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1647 }
1648 }
1649
1650 /**
1651 * Get the given list portion (page)
1652 *
1653 * @param string $container Resolved container name
1654 * @param string $dir Resolved path relative to container
1655 * @param string $after|null
1656 * @param integer $limit
1657 * @param array $params
1658 * @return Traversable|Array
1659 */
1660 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1661 }
1662
1663 /**
1664 * Iterator for listing directories
1665 */
1666 class SwiftFileBackendDirList extends SwiftFileBackendList {
1667 /**
1668 * @see Iterator::current()
1669 * @return string|bool String (relative path) or false
1670 */
1671 public function current() {
1672 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1673 }
1674
1675 /**
1676 * @see SwiftFileBackendList::pageFromList()
1677 * @return Array
1678 */
1679 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1680 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1681 }
1682 }
1683
1684 /**
1685 * Iterator for listing regular files
1686 */
1687 class SwiftFileBackendFileList extends SwiftFileBackendList {
1688 /**
1689 * @see Iterator::current()
1690 * @return string|bool String (relative path) or false
1691 */
1692 public function current() {
1693 return substr( current( $this->bufferIter ), $this->suffixStart );
1694 }
1695
1696 /**
1697 * @see SwiftFileBackendList::pageFromList()
1698 * @return Array
1699 */
1700 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1701 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );
1702 }
1703 }