Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / includes / libs / filebackend / SwiftFileBackend.php
blobd40e896488caf2d708727d6ba45828df792049ab
1 <?php
2 /**
3 * OpenStack Swift based file backend.
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.
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.
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
20 * @file
21 * @ingroup FileBackend
22 * @author Russ Nelson
23 * @author Aaron Schulz
26 /**
27 * @brief Class for an OpenStack Swift (or Ceph RGW) based file backend.
29 * StatusValue messages should avoid mentioning the Swift account name.
30 * Likewise, error suppression should be used to avoid path disclosure.
32 * @ingroup FileBackend
33 * @since 1.19
35 class SwiftFileBackend extends FileBackendStore {
36 /** @var MultiHttpClient */
37 protected $http;
39 /** @var int TTL in seconds */
40 protected $authTTL;
42 /** @var string Authentication base URL (without version) */
43 protected $swiftAuthUrl;
45 /** @var string Swift user (account:user) to authenticate as */
46 protected $swiftUser;
48 /** @var string Secret key for user */
49 protected $swiftKey;
51 /** @var string Shared secret value for making temp URLs */
52 protected $swiftTempUrlKey;
54 /** @var string S3 access key (RADOS Gateway) */
55 protected $rgwS3AccessKey;
57 /** @var string S3 authentication key (RADOS Gateway) */
58 protected $rgwS3SecretKey;
60 /** @var BagOStuff */
61 protected $srvCache;
63 /** @var ProcessCacheLRU Container stat cache */
64 protected $containerStatCache;
66 /** @var array */
67 protected $authCreds;
69 /** @var int UNIX timestamp */
70 protected $authSessionTimestamp = 0;
72 /** @var int UNIX timestamp */
73 protected $authErrorTimestamp = null;
75 /** @var bool Whether the server is an Ceph RGW */
76 protected $isRGW = false;
78 /**
79 * @see FileBackendStore::__construct()
80 * Additional $config params include:
81 * - swiftAuthUrl : Swift authentication server URL
82 * - swiftUser : Swift user used by MediaWiki (account:username)
83 * - swiftKey : Swift authentication key for the above user
84 * - swiftAuthTTL : Swift authentication TTL (seconds)
85 * - swiftTempUrlKey : Swift "X-Account-Meta-Temp-URL-Key" value on the account.
86 * Do not set this until it has been set in the backend.
87 * - shardViaHashLevels : Map of container names to sharding config with:
88 * - base : base of hash characters, 16 or 36
89 * - levels : the number of hash levels (and digits)
90 * - repeat : hash subdirectories are prefixed with all the
91 * parent hash directory names (e.g. "a/ab/abc")
92 * - cacheAuthInfo : Whether to cache authentication tokens in APC, XCache, ect.
93 * If those are not available, then the main cache will be used.
94 * This is probably insecure in shared hosting environments.
95 * - rgwS3AccessKey : Rados Gateway S3 "access key" value on the account.
96 * Do not set this until it has been set in the backend.
97 * This is used for generating expiring pre-authenticated URLs.
98 * Only use this when using rgw and to work around
99 * http://tracker.newdream.net/issues/3454.
100 * - rgwS3SecretKey : Rados Gateway S3 "secret key" value on the account.
101 * Do not set this until it has been set in the backend.
102 * This is used for generating expiring pre-authenticated URLs.
103 * Only use this when using rgw and to work around
104 * http://tracker.newdream.net/issues/3454.
106 public function __construct( array $config ) {
107 parent::__construct( $config );
108 // Required settings
109 $this->swiftAuthUrl = $config['swiftAuthUrl'];
110 $this->swiftUser = $config['swiftUser'];
111 $this->swiftKey = $config['swiftKey'];
112 // Optional settings
113 $this->authTTL = isset( $config['swiftAuthTTL'] )
114 ? $config['swiftAuthTTL']
115 : 15 * 60; // some sane number
116 $this->swiftTempUrlKey = isset( $config['swiftTempUrlKey'] )
117 ? $config['swiftTempUrlKey']
118 : '';
119 $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
120 ? $config['shardViaHashLevels']
121 : '';
122 $this->rgwS3AccessKey = isset( $config['rgwS3AccessKey'] )
123 ? $config['rgwS3AccessKey']
124 : '';
125 $this->rgwS3SecretKey = isset( $config['rgwS3SecretKey'] )
126 ? $config['rgwS3SecretKey']
127 : '';
128 // HTTP helper client
129 $this->http = new MultiHttpClient( [] );
130 // Cache container information to mask latency
131 if ( isset( $config['wanCache'] ) && $config['wanCache'] instanceof WANObjectCache ) {
132 $this->memCache = $config['wanCache'];
134 // Process cache for container info
135 $this->containerStatCache = new ProcessCacheLRU( 300 );
136 // Cache auth token information to avoid RTTs
137 if ( !empty( $config['cacheAuthInfo'] ) && isset( $config['srvCache'] ) ) {
138 $this->srvCache = $config['srvCache'];
139 } else {
140 $this->srvCache = new EmptyBagOStuff();
144 public function getFeatures() {
145 return ( FileBackend::ATTR_UNICODE_PATHS |
146 FileBackend::ATTR_HEADERS | FileBackend::ATTR_METADATA );
149 protected function resolveContainerPath( $container, $relStoragePath ) {
150 if ( !mb_check_encoding( $relStoragePath, 'UTF-8' ) ) {
151 return null; // not UTF-8, makes it hard to use CF and the swift HTTP API
152 } elseif ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
153 return null; // too long for Swift
156 return $relStoragePath;
159 public function isPathUsableInternal( $storagePath ) {
160 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
161 if ( $rel === null ) {
162 return false; // invalid
165 return is_array( $this->getContainerStat( $container ) );
169 * Sanitize and filter the custom headers from a $params array.
170 * Only allows certain "standard" Content- and X-Content- headers.
172 * @param array $params
173 * @return array Sanitized value of 'headers' field in $params
175 protected function sanitizeHdrs( array $params ) {
176 return isset( $params['headers'] )
177 ? $this->getCustomHeaders( $params['headers'] )
178 : [];
182 * @param array $rawHeaders
183 * @return array Custom non-metadata HTTP headers
185 protected function getCustomHeaders( array $rawHeaders ) {
186 $headers = [];
188 // Normalize casing, and strip out illegal headers
189 foreach ( $rawHeaders as $name => $value ) {
190 $name = strtolower( $name );
191 if ( preg_match( '/^content-(type|length)$/', $name ) ) {
192 continue; // blacklisted
193 } elseif ( preg_match( '/^(x-)?content-/', $name ) ) {
194 $headers[$name] = $value; // allowed
195 } elseif ( preg_match( '/^content-(disposition)/', $name ) ) {
196 $headers[$name] = $value; // allowed
199 // By default, Swift has annoyingly low maximum header value limits
200 if ( isset( $headers['content-disposition'] ) ) {
201 $disposition = '';
202 // @note: assume FileBackend::makeContentDisposition() already used
203 foreach ( explode( ';', $headers['content-disposition'] ) as $part ) {
204 $part = trim( $part );
205 $new = ( $disposition === '' ) ? $part : "{$disposition};{$part}";
206 if ( strlen( $new ) <= 255 ) {
207 $disposition = $new;
208 } else {
209 break; // too long; sigh
212 $headers['content-disposition'] = $disposition;
215 return $headers;
219 * @param array $rawHeaders
220 * @return array Custom metadata headers
222 protected function getMetadataHeaders( array $rawHeaders ) {
223 $headers = [];
224 foreach ( $rawHeaders as $name => $value ) {
225 $name = strtolower( $name );
226 if ( strpos( $name, 'x-object-meta-' ) === 0 ) {
227 $headers[$name] = $value;
231 return $headers;
235 * @param array $rawHeaders
236 * @return array Custom metadata headers with prefix removed
238 protected function getMetadata( array $rawHeaders ) {
239 $metadata = [];
240 foreach ( $this->getMetadataHeaders( $rawHeaders ) as $name => $value ) {
241 $metadata[substr( $name, strlen( 'x-object-meta-' ) )] = $value;
244 return $metadata;
247 protected function doCreateInternal( array $params ) {
248 $status = $this->newStatus();
250 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
251 if ( $dstRel === null ) {
252 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
254 return $status;
257 $sha1Hash = Wikimedia\base_convert( sha1( $params['content'] ), 16, 36, 31 );
258 $contentType = isset( $params['headers']['content-type'] )
259 ? $params['headers']['content-type']
260 : $this->getContentType( $params['dst'], $params['content'], null );
262 $reqs = [ [
263 'method' => 'PUT',
264 'url' => [ $dstCont, $dstRel ],
265 'headers' => [
266 'content-length' => strlen( $params['content'] ),
267 'etag' => md5( $params['content'] ),
268 'content-type' => $contentType,
269 'x-object-meta-sha1base36' => $sha1Hash
270 ] + $this->sanitizeHdrs( $params ),
271 'body' => $params['content']
272 ] ];
274 $method = __METHOD__;
275 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
276 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
277 if ( $rcode === 201 ) {
278 // good
279 } elseif ( $rcode === 412 ) {
280 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
281 } else {
282 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
286 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
287 if ( !empty( $params['async'] ) ) { // deferred
288 $status->value = $opHandle;
289 } else { // actually write the object in Swift
290 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
293 return $status;
296 protected function doStoreInternal( array $params ) {
297 $status = $this->newStatus();
299 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
300 if ( $dstRel === null ) {
301 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
303 return $status;
306 MediaWiki\suppressWarnings();
307 $sha1Hash = sha1_file( $params['src'] );
308 MediaWiki\restoreWarnings();
309 if ( $sha1Hash === false ) { // source doesn't exist?
310 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
312 return $status;
314 $sha1Hash = Wikimedia\base_convert( $sha1Hash, 16, 36, 31 );
315 $contentType = isset( $params['headers']['content-type'] )
316 ? $params['headers']['content-type']
317 : $this->getContentType( $params['dst'], null, $params['src'] );
319 $handle = fopen( $params['src'], 'rb' );
320 if ( $handle === false ) { // source doesn't exist?
321 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
323 return $status;
326 $reqs = [ [
327 'method' => 'PUT',
328 'url' => [ $dstCont, $dstRel ],
329 'headers' => [
330 'content-length' => filesize( $params['src'] ),
331 'etag' => md5_file( $params['src'] ),
332 'content-type' => $contentType,
333 'x-object-meta-sha1base36' => $sha1Hash
334 ] + $this->sanitizeHdrs( $params ),
335 'body' => $handle // resource
336 ] ];
338 $method = __METHOD__;
339 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
340 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
341 if ( $rcode === 201 ) {
342 // good
343 } elseif ( $rcode === 412 ) {
344 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
345 } else {
346 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
350 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
351 if ( !empty( $params['async'] ) ) { // deferred
352 $status->value = $opHandle;
353 } else { // actually write the object in Swift
354 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
357 return $status;
360 protected function doCopyInternal( array $params ) {
361 $status = $this->newStatus();
363 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
364 if ( $srcRel === null ) {
365 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
367 return $status;
370 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
371 if ( $dstRel === null ) {
372 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
374 return $status;
377 $reqs = [ [
378 'method' => 'PUT',
379 'url' => [ $dstCont, $dstRel ],
380 'headers' => [
381 'x-copy-from' => '/' . rawurlencode( $srcCont ) .
382 '/' . str_replace( "%2F", "/", rawurlencode( $srcRel ) )
383 ] + $this->sanitizeHdrs( $params ), // extra headers merged into object
384 ] ];
386 $method = __METHOD__;
387 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
388 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
389 if ( $rcode === 201 ) {
390 // good
391 } elseif ( $rcode === 404 ) {
392 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
393 } else {
394 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
398 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
399 if ( !empty( $params['async'] ) ) { // deferred
400 $status->value = $opHandle;
401 } else { // actually write the object in Swift
402 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
405 return $status;
408 protected function doMoveInternal( array $params ) {
409 $status = $this->newStatus();
411 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
412 if ( $srcRel === null ) {
413 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
415 return $status;
418 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
419 if ( $dstRel === null ) {
420 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
422 return $status;
425 $reqs = [
427 'method' => 'PUT',
428 'url' => [ $dstCont, $dstRel ],
429 'headers' => [
430 'x-copy-from' => '/' . rawurlencode( $srcCont ) .
431 '/' . str_replace( "%2F", "/", rawurlencode( $srcRel ) )
432 ] + $this->sanitizeHdrs( $params ) // extra headers merged into object
435 if ( "{$srcCont}/{$srcRel}" !== "{$dstCont}/{$dstRel}" ) {
436 $reqs[] = [
437 'method' => 'DELETE',
438 'url' => [ $srcCont, $srcRel ],
439 'headers' => []
443 $method = __METHOD__;
444 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
445 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
446 if ( $request['method'] === 'PUT' && $rcode === 201 ) {
447 // good
448 } elseif ( $request['method'] === 'DELETE' && $rcode === 204 ) {
449 // good
450 } elseif ( $rcode === 404 ) {
451 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
452 } else {
453 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
457 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
458 if ( !empty( $params['async'] ) ) { // deferred
459 $status->value = $opHandle;
460 } else { // actually move the object in Swift
461 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
464 return $status;
467 protected function doDeleteInternal( array $params ) {
468 $status = $this->newStatus();
470 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
471 if ( $srcRel === null ) {
472 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
474 return $status;
477 $reqs = [ [
478 'method' => 'DELETE',
479 'url' => [ $srcCont, $srcRel ],
480 'headers' => []
481 ] ];
483 $method = __METHOD__;
484 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
485 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
486 if ( $rcode === 204 ) {
487 // good
488 } elseif ( $rcode === 404 ) {
489 if ( empty( $params['ignoreMissingSource'] ) ) {
490 $status->fatal( 'backend-fail-delete', $params['src'] );
492 } else {
493 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
497 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
498 if ( !empty( $params['async'] ) ) { // deferred
499 $status->value = $opHandle;
500 } else { // actually delete the object in Swift
501 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
504 return $status;
507 protected function doDescribeInternal( array $params ) {
508 $status = $this->newStatus();
510 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
511 if ( $srcRel === null ) {
512 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
514 return $status;
517 // Fetch the old object headers/metadata...this should be in stat cache by now
518 $stat = $this->getFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
519 if ( $stat && !isset( $stat['xattr'] ) ) { // older cache entry
520 $stat = $this->doGetFileStat( [ 'src' => $params['src'], 'latest' => 1 ] );
522 if ( !$stat ) {
523 $status->fatal( 'backend-fail-describe', $params['src'] );
525 return $status;
528 // POST clears prior headers, so we need to merge the changes in to the old ones
529 $metaHdrs = [];
530 foreach ( $stat['xattr']['metadata'] as $name => $value ) {
531 $metaHdrs["x-object-meta-$name"] = $value;
533 $customHdrs = $this->sanitizeHdrs( $params ) + $stat['xattr']['headers'];
535 $reqs = [ [
536 'method' => 'POST',
537 'url' => [ $srcCont, $srcRel ],
538 'headers' => $metaHdrs + $customHdrs
539 ] ];
541 $method = __METHOD__;
542 $handler = function ( array $request, StatusValue $status ) use ( $method, $params ) {
543 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
544 if ( $rcode === 202 ) {
545 // good
546 } elseif ( $rcode === 404 ) {
547 $status->fatal( 'backend-fail-describe', $params['src'] );
548 } else {
549 $this->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
553 $opHandle = new SwiftFileOpHandle( $this, $handler, $reqs );
554 if ( !empty( $params['async'] ) ) { // deferred
555 $status->value = $opHandle;
556 } else { // actually change the object in Swift
557 $status->merge( current( $this->doExecuteOpHandlesInternal( [ $opHandle ] ) ) );
560 return $status;
563 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
564 $status = $this->newStatus();
566 // (a) Check if container already exists
567 $stat = $this->getContainerStat( $fullCont );
568 if ( is_array( $stat ) ) {
569 return $status; // already there
570 } elseif ( $stat === null ) {
571 $status->fatal( 'backend-fail-internal', $this->name );
572 $this->logger->error( __METHOD__ . ': cannot get container stat' );
574 return $status;
577 // (b) Create container as needed with proper ACLs
578 if ( $stat === false ) {
579 $params['op'] = 'prepare';
580 $status->merge( $this->createContainer( $fullCont, $params ) );
583 return $status;
586 protected function doSecureInternal( $fullCont, $dir, array $params ) {
587 $status = $this->newStatus();
588 if ( empty( $params['noAccess'] ) ) {
589 return $status; // nothing to do
592 $stat = $this->getContainerStat( $fullCont );
593 if ( is_array( $stat ) ) {
594 // Make container private to end-users...
595 $status->merge( $this->setContainerAccess(
596 $fullCont,
597 [ $this->swiftUser ], // read
598 [ $this->swiftUser ] // write
599 ) );
600 } elseif ( $stat === false ) {
601 $status->fatal( 'backend-fail-usable', $params['dir'] );
602 } else {
603 $status->fatal( 'backend-fail-internal', $this->name );
604 $this->logger->error( __METHOD__ . ': cannot get container stat' );
607 return $status;
610 protected function doPublishInternal( $fullCont, $dir, array $params ) {
611 $status = $this->newStatus();
613 $stat = $this->getContainerStat( $fullCont );
614 if ( is_array( $stat ) ) {
615 // Make container public to end-users...
616 $status->merge( $this->setContainerAccess(
617 $fullCont,
618 [ $this->swiftUser, '.r:*' ], // read
619 [ $this->swiftUser ] // write
620 ) );
621 } elseif ( $stat === false ) {
622 $status->fatal( 'backend-fail-usable', $params['dir'] );
623 } else {
624 $status->fatal( 'backend-fail-internal', $this->name );
625 $this->logger->error( __METHOD__ . ': cannot get container stat' );
628 return $status;
631 protected function doCleanInternal( $fullCont, $dir, array $params ) {
632 $status = $this->newStatus();
634 // Only containers themselves can be removed, all else is virtual
635 if ( $dir != '' ) {
636 return $status; // nothing to do
639 // (a) Check the container
640 $stat = $this->getContainerStat( $fullCont, true );
641 if ( $stat === false ) {
642 return $status; // ok, nothing to do
643 } elseif ( !is_array( $stat ) ) {
644 $status->fatal( 'backend-fail-internal', $this->name );
645 $this->logger->error( __METHOD__ . ': cannot get container stat' );
647 return $status;
650 // (b) Delete the container if empty
651 if ( $stat['count'] == 0 ) {
652 $params['op'] = 'clean';
653 $status->merge( $this->deleteContainer( $fullCont, $params ) );
656 return $status;
659 protected function doGetFileStat( array $params ) {
660 $params = [ 'srcs' => [ $params['src'] ], 'concurrency' => 1 ] + $params;
661 unset( $params['src'] );
662 $stats = $this->doGetFileStatMulti( $params );
664 return reset( $stats );
668 * Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT"/"2013-05-11T07:37:27.678360Z".
669 * Dates might also come in like "2013-05-11T07:37:27.678360" from Swift listings,
670 * missing the timezone suffix (though Ceph RGW does not appear to have this bug).
672 * @param string $ts
673 * @param int $format Output format (TS_* constant)
674 * @return string
675 * @throws FileBackendError
677 protected function convertSwiftDate( $ts, $format = TS_MW ) {
678 try {
679 $timestamp = new MWTimestamp( $ts );
681 return $timestamp->getTimestamp( $format );
682 } catch ( Exception $e ) {
683 throw new FileBackendError( $e->getMessage() );
688 * Fill in any missing object metadata and save it to Swift
690 * @param array $objHdrs Object response headers
691 * @param string $path Storage path to object
692 * @return array New headers
694 protected function addMissingMetadata( array $objHdrs, $path ) {
695 if ( isset( $objHdrs['x-object-meta-sha1base36'] ) ) {
696 return $objHdrs; // nothing to do
699 /** @noinspection PhpUnusedLocalVariableInspection */
700 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
701 $this->logger->error( __METHOD__ . ": $path was not stored with SHA-1 metadata." );
703 $objHdrs['x-object-meta-sha1base36'] = false;
705 $auth = $this->getAuthentication();
706 if ( !$auth ) {
707 return $objHdrs; // failed
710 // Find prior custom HTTP headers
711 $postHeaders = $this->getCustomHeaders( $objHdrs );
712 // Find prior metadata headers
713 $postHeaders += $this->getMetadataHeaders( $objHdrs );
715 $status = $this->newStatus();
716 /** @noinspection PhpUnusedLocalVariableInspection */
717 $scopeLockS = $this->getScopedFileLocks( [ $path ], LockManager::LOCK_UW, $status );
718 if ( $status->isOK() ) {
719 $tmpFile = $this->getLocalCopy( [ 'src' => $path, 'latest' => 1 ] );
720 if ( $tmpFile ) {
721 $hash = $tmpFile->getSha1Base36();
722 if ( $hash !== false ) {
723 $objHdrs['x-object-meta-sha1base36'] = $hash;
724 // Merge new SHA1 header into the old ones
725 $postHeaders['x-object-meta-sha1base36'] = $hash;
726 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
727 list( $rcode ) = $this->http->run( [
728 'method' => 'POST',
729 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
730 'headers' => $this->authTokenHeaders( $auth ) + $postHeaders
731 ] );
732 if ( $rcode >= 200 && $rcode <= 299 ) {
733 $this->deleteFileCache( $path );
735 return $objHdrs; // success
741 $this->logger->error( __METHOD__ . ": unable to set SHA-1 metadata for $path" );
743 return $objHdrs; // failed
746 protected function doGetFileContentsMulti( array $params ) {
747 $contents = [];
749 $auth = $this->getAuthentication();
751 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
752 // Blindly create tmp files and stream to them, catching any exception if the file does
753 // not exist. Doing stats here is useless and will loop infinitely in addMissingMetadata().
754 $reqs = []; // (path => op)
756 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
757 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
758 if ( $srcRel === null || !$auth ) {
759 $contents[$path] = false;
760 continue;
762 // Create a new temporary memory file...
763 $handle = fopen( 'php://temp', 'wb' );
764 if ( $handle ) {
765 $reqs[$path] = [
766 'method' => 'GET',
767 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
768 'headers' => $this->authTokenHeaders( $auth )
769 + $this->headersFromParams( $params ),
770 'stream' => $handle,
773 $contents[$path] = false;
776 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
777 $reqs = $this->http->runMulti( $reqs, $opts );
778 foreach ( $reqs as $path => $op ) {
779 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op['response'];
780 if ( $rcode >= 200 && $rcode <= 299 ) {
781 rewind( $op['stream'] ); // start from the beginning
782 $contents[$path] = stream_get_contents( $op['stream'] );
783 } elseif ( $rcode === 404 ) {
784 $contents[$path] = false;
785 } else {
786 $this->onError( null, __METHOD__,
787 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
789 fclose( $op['stream'] ); // close open handle
792 return $contents;
795 protected function doDirectoryExists( $fullCont, $dir, array $params ) {
796 $prefix = ( $dir == '' ) ? null : "{$dir}/";
797 $status = $this->objectListing( $fullCont, 'names', 1, null, $prefix );
798 if ( $status->isOK() ) {
799 return ( count( $status->value ) ) > 0;
802 return null; // error
806 * @see FileBackendStore::getDirectoryListInternal()
807 * @param string $fullCont
808 * @param string $dir
809 * @param array $params
810 * @return SwiftFileBackendDirList
812 public function getDirectoryListInternal( $fullCont, $dir, array $params ) {
813 return new SwiftFileBackendDirList( $this, $fullCont, $dir, $params );
817 * @see FileBackendStore::getFileListInternal()
818 * @param string $fullCont
819 * @param string $dir
820 * @param array $params
821 * @return SwiftFileBackendFileList
823 public function getFileListInternal( $fullCont, $dir, array $params ) {
824 return new SwiftFileBackendFileList( $this, $fullCont, $dir, $params );
828 * Do not call this function outside of SwiftFileBackendFileList
830 * @param string $fullCont Resolved container name
831 * @param string $dir Resolved storage directory with no trailing slash
832 * @param string|null $after Resolved container relative path to list items after
833 * @param int $limit Max number of items to list
834 * @param array $params Parameters for getDirectoryList()
835 * @return array List of container relative resolved paths of directories directly under $dir
836 * @throws FileBackendError
838 public function getDirListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
839 $dirs = [];
840 if ( $after === INF ) {
841 return $dirs; // nothing more
844 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
846 $prefix = ( $dir == '' ) ? null : "{$dir}/";
847 // Non-recursive: only list dirs right under $dir
848 if ( !empty( $params['topOnly'] ) ) {
849 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
850 if ( !$status->isOK() ) {
851 throw new FileBackendError( "Iterator page I/O error." );
853 $objects = $status->value;
854 foreach ( $objects as $object ) { // files and directories
855 if ( substr( $object, -1 ) === '/' ) {
856 $dirs[] = $object; // directories end in '/'
859 } else {
860 // Recursive: list all dirs under $dir and its subdirs
861 $getParentDir = function ( $path ) {
862 return ( strpos( $path, '/' ) !== false ) ? dirname( $path ) : false;
865 // Get directory from last item of prior page
866 $lastDir = $getParentDir( $after ); // must be first page
867 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
869 if ( !$status->isOK() ) {
870 throw new FileBackendError( "Iterator page I/O error." );
873 $objects = $status->value;
875 foreach ( $objects as $object ) { // files
876 $objectDir = $getParentDir( $object ); // directory of object
878 if ( $objectDir !== false && $objectDir !== $dir ) {
879 // Swift stores paths in UTF-8, using binary sorting.
880 // See function "create_container_table" in common/db.py.
881 // If a directory is not "greater" than the last one,
882 // then it was already listed by the calling iterator.
883 if ( strcmp( $objectDir, $lastDir ) > 0 ) {
884 $pDir = $objectDir;
885 do { // add dir and all its parent dirs
886 $dirs[] = "{$pDir}/";
887 $pDir = $getParentDir( $pDir );
888 } while ( $pDir !== false // sanity
889 && strcmp( $pDir, $lastDir ) > 0 // not done already
890 && strlen( $pDir ) > strlen( $dir ) // within $dir
893 $lastDir = $objectDir;
897 // Page on the unfiltered directory listing (what is returned may be filtered)
898 if ( count( $objects ) < $limit ) {
899 $after = INF; // avoid a second RTT
900 } else {
901 $after = end( $objects ); // update last item
904 return $dirs;
908 * Do not call this function outside of SwiftFileBackendFileList
910 * @param string $fullCont Resolved container name
911 * @param string $dir Resolved storage directory with no trailing slash
912 * @param string|null $after Resolved container relative path of file to list items after
913 * @param int $limit Max number of items to list
914 * @param array $params Parameters for getDirectoryList()
915 * @return array List of resolved container relative paths of files under $dir
916 * @throws FileBackendError
918 public function getFileListPageInternal( $fullCont, $dir, &$after, $limit, array $params ) {
919 $files = []; // list of (path, stat array or null) entries
920 if ( $after === INF ) {
921 return $files; // nothing more
924 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
926 $prefix = ( $dir == '' ) ? null : "{$dir}/";
927 // $objects will contain a list of unfiltered names or CF_Object items
928 // Non-recursive: only list files right under $dir
929 if ( !empty( $params['topOnly'] ) ) {
930 if ( !empty( $params['adviseStat'] ) ) {
931 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix, '/' );
932 } else {
933 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix, '/' );
935 } else {
936 // Recursive: list all files under $dir and its subdirs
937 if ( !empty( $params['adviseStat'] ) ) {
938 $status = $this->objectListing( $fullCont, 'info', $limit, $after, $prefix );
939 } else {
940 $status = $this->objectListing( $fullCont, 'names', $limit, $after, $prefix );
944 // Reformat this list into a list of (name, stat array or null) entries
945 if ( !$status->isOK() ) {
946 throw new FileBackendError( "Iterator page I/O error." );
949 $objects = $status->value;
950 $files = $this->buildFileObjectListing( $params, $dir, $objects );
952 // Page on the unfiltered object listing (what is returned may be filtered)
953 if ( count( $objects ) < $limit ) {
954 $after = INF; // avoid a second RTT
955 } else {
956 $after = end( $objects ); // update last item
957 $after = is_object( $after ) ? $after->name : $after;
960 return $files;
964 * Build a list of file objects, filtering out any directories
965 * and extracting any stat info if provided in $objects (for CF_Objects)
967 * @param array $params Parameters for getDirectoryList()
968 * @param string $dir Resolved container directory path
969 * @param array $objects List of CF_Object items or object names
970 * @return array List of (names,stat array or null) entries
972 private function buildFileObjectListing( array $params, $dir, array $objects ) {
973 $names = [];
974 foreach ( $objects as $object ) {
975 if ( is_object( $object ) ) {
976 if ( isset( $object->subdir ) || !isset( $object->name ) ) {
977 continue; // virtual directory entry; ignore
979 $stat = [
980 // Convert various random Swift dates to TS_MW
981 'mtime' => $this->convertSwiftDate( $object->last_modified, TS_MW ),
982 'size' => (int)$object->bytes,
983 'sha1' => null,
984 // Note: manifiest ETags are not an MD5 of the file
985 'md5' => ctype_xdigit( $object->hash ) ? $object->hash : null,
986 'latest' => false // eventually consistent
988 $names[] = [ $object->name, $stat ];
989 } elseif ( substr( $object, -1 ) !== '/' ) {
990 // Omit directories, which end in '/' in listings
991 $names[] = [ $object, null ];
995 return $names;
999 * Do not call this function outside of SwiftFileBackendFileList
1001 * @param string $path Storage path
1002 * @param array $val Stat value
1004 public function loadListingStatInternal( $path, array $val ) {
1005 $this->cheapCache->set( $path, 'stat', $val );
1008 protected function doGetFileXAttributes( array $params ) {
1009 $stat = $this->getFileStat( $params );
1010 if ( $stat ) {
1011 if ( !isset( $stat['xattr'] ) ) {
1012 // Stat entries filled by file listings don't include metadata/headers
1013 $this->clearCache( [ $params['src'] ] );
1014 $stat = $this->getFileStat( $params );
1017 return $stat['xattr'];
1018 } else {
1019 return false;
1023 protected function doGetFileSha1base36( array $params ) {
1024 $stat = $this->getFileStat( $params );
1025 if ( $stat ) {
1026 if ( !isset( $stat['sha1'] ) ) {
1027 // Stat entries filled by file listings don't include SHA1
1028 $this->clearCache( [ $params['src'] ] );
1029 $stat = $this->getFileStat( $params );
1032 return $stat['sha1'];
1033 } else {
1034 return false;
1038 protected function doStreamFile( array $params ) {
1039 $status = $this->newStatus();
1041 $flags = !empty( $params['headless'] ) ? StreamFile::STREAM_HEADLESS : 0;
1043 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1044 if ( $srcRel === null ) {
1045 StreamFile::send404Message( $params['src'], $flags );
1046 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
1048 return $status;
1051 $auth = $this->getAuthentication();
1052 if ( !$auth || !is_array( $this->getContainerStat( $srcCont ) ) ) {
1053 StreamFile::send404Message( $params['src'], $flags );
1054 $status->fatal( 'backend-fail-stream', $params['src'] );
1056 return $status;
1059 // If "headers" is set, we only want to send them if the file is there.
1060 // Do not bother checking if the file exists if headers are not set though.
1061 if ( $params['headers'] && !$this->fileExists( $params ) ) {
1062 StreamFile::send404Message( $params['src'], $flags );
1063 $status->fatal( 'backend-fail-stream', $params['src'] );
1065 return $status;
1068 // Send the requested additional headers
1069 foreach ( $params['headers'] as $header ) {
1070 header( $header ); // aways send
1073 if ( empty( $params['allowOB'] ) ) {
1074 // Cancel output buffering and gzipping if set
1075 call_user_func( $this->obResetFunc );
1078 $handle = fopen( 'php://output', 'wb' );
1079 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1080 'method' => 'GET',
1081 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1082 'headers' => $this->authTokenHeaders( $auth )
1083 + $this->headersFromParams( $params ) + $params['options'],
1084 'stream' => $handle,
1085 'flags' => [ 'relayResponseHeaders' => empty( $params['headless'] ) ]
1086 ] );
1088 if ( $rcode >= 200 && $rcode <= 299 ) {
1089 // good
1090 } elseif ( $rcode === 404 ) {
1091 $status->fatal( 'backend-fail-stream', $params['src'] );
1092 // Per bug 41113, nasty things can happen if bad cache entries get
1093 // stuck in cache. It's also possible that this error can come up
1094 // with simple race conditions. Clear out the stat cache to be safe.
1095 $this->clearCache( [ $params['src'] ] );
1096 $this->deleteFileCache( $params['src'] );
1097 } else {
1098 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1101 return $status;
1104 protected function doGetLocalCopyMulti( array $params ) {
1105 /** @var TempFSFile[] $tmpFiles */
1106 $tmpFiles = [];
1108 $auth = $this->getAuthentication();
1110 $ep = array_diff_key( $params, [ 'srcs' => 1 ] ); // for error logging
1111 // Blindly create tmp files and stream to them, catching any exception if the file does
1112 // not exist. Doing a stat here is useless causes infinite loops in addMissingMetadata().
1113 $reqs = []; // (path => op)
1115 foreach ( $params['srcs'] as $path ) { // each path in this concurrent batch
1116 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1117 if ( $srcRel === null || !$auth ) {
1118 $tmpFiles[$path] = null;
1119 continue;
1121 // Get source file extension
1122 $ext = FileBackend::extensionFromPath( $path );
1123 // Create a new temporary file...
1124 $tmpFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory );
1125 if ( $tmpFile ) {
1126 $handle = fopen( $tmpFile->getPath(), 'wb' );
1127 if ( $handle ) {
1128 $reqs[$path] = [
1129 'method' => 'GET',
1130 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1131 'headers' => $this->authTokenHeaders( $auth )
1132 + $this->headersFromParams( $params ),
1133 'stream' => $handle,
1135 } else {
1136 $tmpFile = null;
1139 $tmpFiles[$path] = $tmpFile;
1142 $isLatest = ( $this->isRGW || !empty( $params['latest'] ) );
1143 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
1144 $reqs = $this->http->runMulti( $reqs, $opts );
1145 foreach ( $reqs as $path => $op ) {
1146 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $op['response'];
1147 fclose( $op['stream'] ); // close open handle
1148 if ( $rcode >= 200 && $rcode <= 299 ) {
1149 $size = $tmpFiles[$path] ? $tmpFiles[$path]->getSize() : 0;
1150 // Double check that the disk is not full/broken
1151 if ( $size != $rhdrs['content-length'] ) {
1152 $tmpFiles[$path] = null;
1153 $rerr = "Got {$size}/{$rhdrs['content-length']} bytes";
1154 $this->onError( null, __METHOD__,
1155 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1157 // Set the file stat process cache in passing
1158 $stat = $this->getStatFromHeaders( $rhdrs );
1159 $stat['latest'] = $isLatest;
1160 $this->cheapCache->set( $path, 'stat', $stat );
1161 } elseif ( $rcode === 404 ) {
1162 $tmpFiles[$path] = false;
1163 } else {
1164 $tmpFiles[$path] = null;
1165 $this->onError( null, __METHOD__,
1166 [ 'src' => $path ] + $ep, $rerr, $rcode, $rdesc );
1170 return $tmpFiles;
1173 public function getFileHttpUrl( array $params ) {
1174 if ( $this->swiftTempUrlKey != '' ||
1175 ( $this->rgwS3AccessKey != '' && $this->rgwS3SecretKey != '' )
1177 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
1178 if ( $srcRel === null ) {
1179 return null; // invalid path
1182 $auth = $this->getAuthentication();
1183 if ( !$auth ) {
1184 return null;
1187 $ttl = isset( $params['ttl'] ) ? $params['ttl'] : 86400;
1188 $expires = time() + $ttl;
1190 if ( $this->swiftTempUrlKey != '' ) {
1191 $url = $this->storageUrl( $auth, $srcCont, $srcRel );
1192 // Swift wants the signature based on the unencoded object name
1193 $contPath = parse_url( $this->storageUrl( $auth, $srcCont ), PHP_URL_PATH );
1194 $signature = hash_hmac( 'sha1',
1195 "GET\n{$expires}\n{$contPath}/{$srcRel}",
1196 $this->swiftTempUrlKey
1199 return "{$url}?temp_url_sig={$signature}&temp_url_expires={$expires}";
1200 } else { // give S3 API URL for rgw
1201 // Path for signature starts with the bucket
1202 $spath = '/' . rawurlencode( $srcCont ) . '/' .
1203 str_replace( '%2F', '/', rawurlencode( $srcRel ) );
1204 // Calculate the hash
1205 $signature = base64_encode( hash_hmac(
1206 'sha1',
1207 "GET\n\n\n{$expires}\n{$spath}",
1208 $this->rgwS3SecretKey,
1209 true // raw
1210 ) );
1211 // See https://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html.
1212 // Note: adding a newline for empty CanonicalizedAmzHeaders does not work.
1213 // Note: S3 API is the rgw default; remove the /swift/ URL bit.
1214 return str_replace( '/swift/v1', '', $this->storageUrl( $auth ) . $spath ) .
1215 '?' .
1216 http_build_query( [
1217 'Signature' => $signature,
1218 'Expires' => $expires,
1219 'AWSAccessKeyId' => $this->rgwS3AccessKey
1220 ] );
1224 return null;
1227 protected function directoriesAreVirtual() {
1228 return true;
1232 * Get headers to send to Swift when reading a file based
1233 * on a FileBackend params array, e.g. that of getLocalCopy().
1234 * $params is currently only checked for a 'latest' flag.
1236 * @param array $params
1237 * @return array
1239 protected function headersFromParams( array $params ) {
1240 $hdrs = [];
1241 if ( !empty( $params['latest'] ) ) {
1242 $hdrs['x-newest'] = 'true';
1245 return $hdrs;
1249 * @param FileBackendStoreOpHandle[] $fileOpHandles
1251 * @return StatusValue[]
1253 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1254 /** @var $statuses StatusValue[] */
1255 $statuses = [];
1257 $auth = $this->getAuthentication();
1258 if ( !$auth ) {
1259 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1260 $statuses[$index] = $this->newStatus( 'backend-fail-connect', $this->name );
1263 return $statuses;
1266 // Split the HTTP requests into stages that can be done concurrently
1267 $httpReqsByStage = []; // map of (stage => index => HTTP request)
1268 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
1269 /** @var SwiftFileOpHandle $fileOpHandle */
1270 $reqs = $fileOpHandle->httpOp;
1271 // Convert the 'url' parameter to an actual URL using $auth
1272 foreach ( $reqs as $stage => &$req ) {
1273 list( $container, $relPath ) = $req['url'];
1274 $req['url'] = $this->storageUrl( $auth, $container, $relPath );
1275 $req['headers'] = isset( $req['headers'] ) ? $req['headers'] : [];
1276 $req['headers'] = $this->authTokenHeaders( $auth ) + $req['headers'];
1277 $httpReqsByStage[$stage][$index] = $req;
1279 $statuses[$index] = $this->newStatus();
1282 // Run all requests for the first stage, then the next, and so on
1283 $reqCount = count( $httpReqsByStage );
1284 for ( $stage = 0; $stage < $reqCount; ++$stage ) {
1285 $httpReqs = $this->http->runMulti( $httpReqsByStage[$stage] );
1286 foreach ( $httpReqs as $index => $httpReq ) {
1287 // Run the callback for each request of this operation
1288 $callback = $fileOpHandles[$index]->callback;
1289 call_user_func_array( $callback, [ $httpReq, $statuses[$index] ] );
1290 // On failure, abort all remaining requests for this operation
1291 // (e.g. abort the DELETE request if the COPY request fails for a move)
1292 if ( !$statuses[$index]->isOK() ) {
1293 $stages = count( $fileOpHandles[$index]->httpOp );
1294 for ( $s = ( $stage + 1 ); $s < $stages; ++$s ) {
1295 unset( $httpReqsByStage[$s][$index] );
1301 return $statuses;
1305 * Set read/write permissions for a Swift container.
1307 * @see http://docs.openstack.org/developer/swift/misc.html#acls
1309 * In general, we don't allow listings to end-users. It's not useful, isn't well-defined
1310 * (lists are truncated to 10000 item with no way to page), and is just a performance risk.
1312 * @param string $container Resolved Swift container
1313 * @param array $readGrps List of the possible criteria for a request to have
1314 * access to read a container. Each item is one of the following formats:
1315 * - account:user : Grants access if the request is by the given user
1316 * - ".r:<regex>" : Grants access if the request is from a referrer host that
1317 * matches the expression and the request is not for a listing.
1318 * Setting this to '*' effectively makes a container public.
1319 * -".rlistings:<regex>" : Grants access if the request is from a referrer host that
1320 * matches the expression and the request is for a listing.
1321 * @param array $writeGrps A list of the possible criteria for a request to have
1322 * access to write to a container. Each item is of the following format:
1323 * - account:user : Grants access if the request is by the given user
1324 * @return StatusValue
1326 protected function setContainerAccess( $container, array $readGrps, array $writeGrps ) {
1327 $status = $this->newStatus();
1328 $auth = $this->getAuthentication();
1330 if ( !$auth ) {
1331 $status->fatal( 'backend-fail-connect', $this->name );
1333 return $status;
1336 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1337 'method' => 'POST',
1338 'url' => $this->storageUrl( $auth, $container ),
1339 'headers' => $this->authTokenHeaders( $auth ) + [
1340 'x-container-read' => implode( ',', $readGrps ),
1341 'x-container-write' => implode( ',', $writeGrps )
1343 ] );
1345 if ( $rcode != 204 && $rcode !== 202 ) {
1346 $status->fatal( 'backend-fail-internal', $this->name );
1347 $this->logger->error( __METHOD__ . ': unexpected rcode value (' . $rcode . ')' );
1350 return $status;
1354 * Get a Swift container stat array, possibly from process cache.
1355 * Use $reCache if the file count or byte count is needed.
1357 * @param string $container Container name
1358 * @param bool $bypassCache Bypass all caches and load from Swift
1359 * @return array|bool|null False on 404, null on failure
1361 protected function getContainerStat( $container, $bypassCache = false ) {
1362 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1364 if ( $bypassCache ) { // purge cache
1365 $this->containerStatCache->clear( $container );
1366 } elseif ( !$this->containerStatCache->has( $container, 'stat' ) ) {
1367 $this->primeContainerCache( [ $container ] ); // check persistent cache
1369 if ( !$this->containerStatCache->has( $container, 'stat' ) ) {
1370 $auth = $this->getAuthentication();
1371 if ( !$auth ) {
1372 return null;
1375 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1376 'method' => 'HEAD',
1377 'url' => $this->storageUrl( $auth, $container ),
1378 'headers' => $this->authTokenHeaders( $auth )
1379 ] );
1381 if ( $rcode === 204 ) {
1382 $stat = [
1383 'count' => $rhdrs['x-container-object-count'],
1384 'bytes' => $rhdrs['x-container-bytes-used']
1386 if ( $bypassCache ) {
1387 return $stat;
1388 } else {
1389 $this->containerStatCache->set( $container, 'stat', $stat ); // cache it
1390 $this->setContainerCache( $container, $stat ); // update persistent cache
1392 } elseif ( $rcode === 404 ) {
1393 return false;
1394 } else {
1395 $this->onError( null, __METHOD__,
1396 [ 'cont' => $container ], $rerr, $rcode, $rdesc );
1398 return null;
1402 return $this->containerStatCache->get( $container, 'stat' );
1406 * Create a Swift container
1408 * @param string $container Container name
1409 * @param array $params
1410 * @return StatusValue
1412 protected function createContainer( $container, array $params ) {
1413 $status = $this->newStatus();
1415 $auth = $this->getAuthentication();
1416 if ( !$auth ) {
1417 $status->fatal( 'backend-fail-connect', $this->name );
1419 return $status;
1422 // @see SwiftFileBackend::setContainerAccess()
1423 if ( empty( $params['noAccess'] ) ) {
1424 $readGrps = [ '.r:*', $this->swiftUser ]; // public
1425 } else {
1426 $readGrps = [ $this->swiftUser ]; // private
1428 $writeGrps = [ $this->swiftUser ]; // sanity
1430 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1431 'method' => 'PUT',
1432 'url' => $this->storageUrl( $auth, $container ),
1433 'headers' => $this->authTokenHeaders( $auth ) + [
1434 'x-container-read' => implode( ',', $readGrps ),
1435 'x-container-write' => implode( ',', $writeGrps )
1437 ] );
1439 if ( $rcode === 201 ) { // new
1440 // good
1441 } elseif ( $rcode === 202 ) { // already there
1442 // this shouldn't really happen, but is OK
1443 } else {
1444 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1447 return $status;
1451 * Delete a Swift container
1453 * @param string $container Container name
1454 * @param array $params
1455 * @return StatusValue
1457 protected function deleteContainer( $container, array $params ) {
1458 $status = $this->newStatus();
1460 $auth = $this->getAuthentication();
1461 if ( !$auth ) {
1462 $status->fatal( 'backend-fail-connect', $this->name );
1464 return $status;
1467 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1468 'method' => 'DELETE',
1469 'url' => $this->storageUrl( $auth, $container ),
1470 'headers' => $this->authTokenHeaders( $auth )
1471 ] );
1473 if ( $rcode >= 200 && $rcode <= 299 ) { // deleted
1474 $this->containerStatCache->clear( $container ); // purge
1475 } elseif ( $rcode === 404 ) { // not there
1476 // this shouldn't really happen, but is OK
1477 } elseif ( $rcode === 409 ) { // not empty
1478 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc ); // race?
1479 } else {
1480 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1483 return $status;
1487 * Get a list of objects under a container.
1488 * Either just the names or a list of stdClass objects with details can be returned.
1490 * @param string $fullCont
1491 * @param string $type ('info' for a list of object detail maps, 'names' for names only)
1492 * @param int $limit
1493 * @param string|null $after
1494 * @param string|null $prefix
1495 * @param string|null $delim
1496 * @return StatusValue With the list as value
1498 private function objectListing(
1499 $fullCont, $type, $limit, $after = null, $prefix = null, $delim = null
1501 $status = $this->newStatus();
1503 $auth = $this->getAuthentication();
1504 if ( !$auth ) {
1505 $status->fatal( 'backend-fail-connect', $this->name );
1507 return $status;
1510 $query = [ 'limit' => $limit ];
1511 if ( $type === 'info' ) {
1512 $query['format'] = 'json';
1514 if ( $after !== null ) {
1515 $query['marker'] = $after;
1517 if ( $prefix !== null ) {
1518 $query['prefix'] = $prefix;
1520 if ( $delim !== null ) {
1521 $query['delimiter'] = $delim;
1524 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1525 'method' => 'GET',
1526 'url' => $this->storageUrl( $auth, $fullCont ),
1527 'query' => $query,
1528 'headers' => $this->authTokenHeaders( $auth )
1529 ] );
1531 $params = [ 'cont' => $fullCont, 'prefix' => $prefix, 'delim' => $delim ];
1532 if ( $rcode === 200 ) { // good
1533 if ( $type === 'info' ) {
1534 $status->value = FormatJson::decode( trim( $rbody ) );
1535 } else {
1536 $status->value = explode( "\n", trim( $rbody ) );
1538 } elseif ( $rcode === 204 ) {
1539 $status->value = []; // empty container
1540 } elseif ( $rcode === 404 ) {
1541 $status->value = []; // no container
1542 } else {
1543 $this->onError( $status, __METHOD__, $params, $rerr, $rcode, $rdesc );
1546 return $status;
1549 protected function doPrimeContainerCache( array $containerInfo ) {
1550 foreach ( $containerInfo as $container => $info ) {
1551 $this->containerStatCache->set( $container, 'stat', $info );
1555 protected function doGetFileStatMulti( array $params ) {
1556 $stats = [];
1558 $auth = $this->getAuthentication();
1560 $reqs = [];
1561 foreach ( $params['srcs'] as $path ) {
1562 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $path );
1563 if ( $srcRel === null ) {
1564 $stats[$path] = false;
1565 continue; // invalid storage path
1566 } elseif ( !$auth ) {
1567 $stats[$path] = null;
1568 continue;
1571 // (a) Check the container
1572 $cstat = $this->getContainerStat( $srcCont );
1573 if ( $cstat === false ) {
1574 $stats[$path] = false;
1575 continue; // ok, nothing to do
1576 } elseif ( !is_array( $cstat ) ) {
1577 $stats[$path] = null;
1578 continue;
1581 $reqs[$path] = [
1582 'method' => 'HEAD',
1583 'url' => $this->storageUrl( $auth, $srcCont, $srcRel ),
1584 'headers' => $this->authTokenHeaders( $auth ) + $this->headersFromParams( $params )
1588 $opts = [ 'maxConnsPerHost' => $params['concurrency'] ];
1589 $reqs = $this->http->runMulti( $reqs, $opts );
1591 foreach ( $params['srcs'] as $path ) {
1592 if ( array_key_exists( $path, $stats ) ) {
1593 continue; // some sort of failure above
1595 // (b) Check the file
1596 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $reqs[$path]['response'];
1597 if ( $rcode === 200 || $rcode === 204 ) {
1598 // Update the object if it is missing some headers
1599 $rhdrs = $this->addMissingMetadata( $rhdrs, $path );
1600 // Load the stat array from the headers
1601 $stat = $this->getStatFromHeaders( $rhdrs );
1602 if ( $this->isRGW ) {
1603 $stat['latest'] = true; // strong consistency
1605 } elseif ( $rcode === 404 ) {
1606 $stat = false;
1607 } else {
1608 $stat = null;
1609 $this->onError( null, __METHOD__, $params, $rerr, $rcode, $rdesc );
1611 $stats[$path] = $stat;
1614 return $stats;
1618 * @param array $rhdrs
1619 * @return array
1621 protected function getStatFromHeaders( array $rhdrs ) {
1622 // Fetch all of the custom metadata headers
1623 $metadata = $this->getMetadata( $rhdrs );
1624 // Fetch all of the custom raw HTTP headers
1625 $headers = $this->sanitizeHdrs( [ 'headers' => $rhdrs ] );
1627 return [
1628 // Convert various random Swift dates to TS_MW
1629 'mtime' => $this->convertSwiftDate( $rhdrs['last-modified'], TS_MW ),
1630 // Empty objects actually return no content-length header in Ceph
1631 'size' => isset( $rhdrs['content-length'] ) ? (int)$rhdrs['content-length'] : 0,
1632 'sha1' => isset( $metadata['sha1base36'] ) ? $metadata['sha1base36'] : null,
1633 // Note: manifiest ETags are not an MD5 of the file
1634 'md5' => ctype_xdigit( $rhdrs['etag'] ) ? $rhdrs['etag'] : null,
1635 'xattr' => [ 'metadata' => $metadata, 'headers' => $headers ]
1640 * @return array|null Credential map
1642 protected function getAuthentication() {
1643 if ( $this->authErrorTimestamp !== null ) {
1644 if ( ( time() - $this->authErrorTimestamp ) < 60 ) {
1645 return null; // failed last attempt; don't bother
1646 } else { // actually retry this time
1647 $this->authErrorTimestamp = null;
1650 // Session keys expire after a while, so we renew them periodically
1651 $reAuth = ( ( time() - $this->authSessionTimestamp ) > $this->authTTL );
1652 // Authenticate with proxy and get a session key...
1653 if ( !$this->authCreds || $reAuth ) {
1654 $this->authSessionTimestamp = 0;
1655 $cacheKey = $this->getCredsCacheKey( $this->swiftUser );
1656 $creds = $this->srvCache->get( $cacheKey ); // credentials
1657 // Try to use the credential cache
1658 if ( isset( $creds['auth_token'] ) && isset( $creds['storage_url'] ) ) {
1659 $this->authCreds = $creds;
1660 // Skew the timestamp for worst case to avoid using stale credentials
1661 $this->authSessionTimestamp = time() - ceil( $this->authTTL / 2 );
1662 } else { // cache miss
1663 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->http->run( [
1664 'method' => 'GET',
1665 'url' => "{$this->swiftAuthUrl}/v1.0",
1666 'headers' => [
1667 'x-auth-user' => $this->swiftUser,
1668 'x-auth-key' => $this->swiftKey
1670 ] );
1672 if ( $rcode >= 200 && $rcode <= 299 ) { // OK
1673 $this->authCreds = [
1674 'auth_token' => $rhdrs['x-auth-token'],
1675 'storage_url' => $rhdrs['x-storage-url']
1677 $this->srvCache->set( $cacheKey, $this->authCreds, ceil( $this->authTTL / 2 ) );
1678 $this->authSessionTimestamp = time();
1679 } elseif ( $rcode === 401 ) {
1680 $this->onError( null, __METHOD__, [], "Authentication failed.", $rcode );
1681 $this->authErrorTimestamp = time();
1683 return null;
1684 } else {
1685 $this->onError( null, __METHOD__, [], "HTTP return code: $rcode", $rcode );
1686 $this->authErrorTimestamp = time();
1688 return null;
1691 // Ceph RGW does not use <account> in URLs (OpenStack Swift uses "/v1/<account>")
1692 if ( substr( $this->authCreds['storage_url'], -3 ) === '/v1' ) {
1693 $this->isRGW = true; // take advantage of strong consistency in Ceph
1697 return $this->authCreds;
1701 * @param array $creds From getAuthentication()
1702 * @param string $container
1703 * @param string $object
1704 * @return string
1706 protected function storageUrl( array $creds, $container = null, $object = null ) {
1707 $parts = [ $creds['storage_url'] ];
1708 if ( strlen( $container ) ) {
1709 $parts[] = rawurlencode( $container );
1711 if ( strlen( $object ) ) {
1712 $parts[] = str_replace( "%2F", "/", rawurlencode( $object ) );
1715 return implode( '/', $parts );
1719 * @param array $creds From getAuthentication()
1720 * @return array
1722 protected function authTokenHeaders( array $creds ) {
1723 return [ 'x-auth-token' => $creds['auth_token'] ];
1727 * Get the cache key for a container
1729 * @param string $username
1730 * @return string
1732 private function getCredsCacheKey( $username ) {
1733 return 'swiftcredentials:' . md5( $username . ':' . $this->swiftAuthUrl );
1737 * Log an unexpected exception for this backend.
1738 * This also sets the StatusValue object to have a fatal error.
1740 * @param StatusValue|null $status
1741 * @param string $func
1742 * @param array $params
1743 * @param string $err Error string
1744 * @param int $code HTTP status
1745 * @param string $desc HTTP StatusValue description
1747 public function onError( $status, $func, array $params, $err = '', $code = 0, $desc = '' ) {
1748 if ( $status instanceof StatusValue ) {
1749 $status->fatal( 'backend-fail-internal', $this->name );
1751 if ( $code == 401 ) { // possibly a stale token
1752 $this->srvCache->delete( $this->getCredsCacheKey( $this->swiftUser ) );
1754 $this->logger->error(
1755 "HTTP $code ($desc) in '{$func}' (given '" . FormatJson::encode( $params ) . "')" .
1756 ( $err ? ": $err" : "" )
1762 * @see FileBackendStoreOpHandle
1764 class SwiftFileOpHandle extends FileBackendStoreOpHandle {
1765 /** @var array List of Requests for MultiHttpClient */
1766 public $httpOp;
1767 /** @var Closure */
1768 public $callback;
1771 * @param SwiftFileBackend $backend
1772 * @param Closure $callback Function that takes (HTTP request array, status)
1773 * @param array $httpOp MultiHttpClient op
1775 public function __construct( SwiftFileBackend $backend, Closure $callback, array $httpOp ) {
1776 $this->backend = $backend;
1777 $this->callback = $callback;
1778 $this->httpOp = $httpOp;
1783 * SwiftFileBackend helper class to page through listings.
1784 * Swift also has a listing limit of 10,000 objects for sanity.
1785 * Do not use this class from places outside SwiftFileBackend.
1787 * @ingroup FileBackend
1789 abstract class SwiftFileBackendList implements Iterator {
1790 /** @var array List of path or (path,stat array) entries */
1791 protected $bufferIter = [];
1793 /** @var string List items *after* this path */
1794 protected $bufferAfter = null;
1796 /** @var int */
1797 protected $pos = 0;
1799 /** @var array */
1800 protected $params = [];
1802 /** @var SwiftFileBackend */
1803 protected $backend;
1805 /** @var string Container name */
1806 protected $container;
1808 /** @var string Storage directory */
1809 protected $dir;
1811 /** @var int */
1812 protected $suffixStart;
1814 const PAGE_SIZE = 9000; // file listing buffer size
1817 * @param SwiftFileBackend $backend
1818 * @param string $fullCont Resolved container name
1819 * @param string $dir Resolved directory relative to container
1820 * @param array $params
1822 public function __construct( SwiftFileBackend $backend, $fullCont, $dir, array $params ) {
1823 $this->backend = $backend;
1824 $this->container = $fullCont;
1825 $this->dir = $dir;
1826 if ( substr( $this->dir, -1 ) === '/' ) {
1827 $this->dir = substr( $this->dir, 0, -1 ); // remove trailing slash
1829 if ( $this->dir == '' ) { // whole container
1830 $this->suffixStart = 0;
1831 } else { // dir within container
1832 $this->suffixStart = strlen( $this->dir ) + 1; // size of "path/to/dir/"
1834 $this->params = $params;
1838 * @see Iterator::key()
1839 * @return int
1841 public function key() {
1842 return $this->pos;
1846 * @see Iterator::next()
1848 public function next() {
1849 // Advance to the next file in the page
1850 next( $this->bufferIter );
1851 ++$this->pos;
1852 // Check if there are no files left in this page and
1853 // advance to the next page if this page was not empty.
1854 if ( !$this->valid() && count( $this->bufferIter ) ) {
1855 $this->bufferIter = $this->pageFromList(
1856 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1857 ); // updates $this->bufferAfter
1862 * @see Iterator::rewind()
1864 public function rewind() {
1865 $this->pos = 0;
1866 $this->bufferAfter = null;
1867 $this->bufferIter = $this->pageFromList(
1868 $this->container, $this->dir, $this->bufferAfter, self::PAGE_SIZE, $this->params
1869 ); // updates $this->bufferAfter
1873 * @see Iterator::valid()
1874 * @return bool
1876 public function valid() {
1877 if ( $this->bufferIter === null ) {
1878 return false; // some failure?
1879 } else {
1880 return ( current( $this->bufferIter ) !== false ); // no paths can have this value
1885 * Get the given list portion (page)
1887 * @param string $container Resolved container name
1888 * @param string $dir Resolved path relative to container
1889 * @param string $after
1890 * @param int $limit
1891 * @param array $params
1892 * @return Traversable|array
1894 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
1898 * Iterator for listing directories
1900 class SwiftFileBackendDirList extends SwiftFileBackendList {
1902 * @see Iterator::current()
1903 * @return string|bool String (relative path) or false
1905 public function current() {
1906 return substr( current( $this->bufferIter ), $this->suffixStart, -1 );
1909 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1910 return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
1915 * Iterator for listing regular files
1917 class SwiftFileBackendFileList extends SwiftFileBackendList {
1919 * @see Iterator::current()
1920 * @return string|bool String (relative path) or false
1922 public function current() {
1923 list( $path, $stat ) = current( $this->bufferIter );
1924 $relPath = substr( $path, $this->suffixStart );
1925 if ( is_array( $stat ) ) {
1926 $storageDir = rtrim( $this->params['dir'], '/' );
1927 $this->backend->loadListingStatInternal( "$storageDir/$relPath", $stat );
1930 return $relPath;
1933 protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
1934 return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );