b5d2aaae5024118343f1a01e919e744b9043496c
10 * Class for an OpenStack Swift based file backend.
12 * This requires the SwiftCloudFiles MediaWiki extension, which includes
13 * the php-cloudfiles library (https://github.com/rackspace/php-cloudfiles).
14 * php-cloudfiles requires the curl, fileinfo, and mb_string PHP extensions.
16 * Status messages should avoid mentioning the Swift account name
17 * Likewise, error suppression should be used to avoid path disclosure.
19 * @ingroup FileBackend
22 class SwiftFileBackend
extends FileBackend
{
23 /** @var CF_Authentication */
24 protected $auth; // Swift authentication handler
25 protected $authTTL; // integer seconds
26 protected $swiftAnonUser; // string; username to handle unauthenticated requests
27 protected $maxContCacheSize = 20; // integer; max containers with entries
29 /** @var CF_Connection */
30 protected $conn; // Swift connection handle
31 protected $connStarted = 0; // integer UNIX timestamp
32 protected $connContainers = array(); // container object cache
35 * @see FileBackend::__construct()
36 * Additional $config params include:
37 * swiftAuthUrl : Swift authentication server URL
38 * swiftUser : Swift user used by MediaWiki (account:username)
39 * swiftKey : Swift authentication key for the above user
40 * swiftAuthTTL : Swift authentication TTL (seconds)
41 * swiftAnonUser : Swift user used for end-user requests (account:username)
42 * shardViaHashLevels : Map of container names to the number of hash levels
44 public function __construct( array $config ) {
45 parent
::__construct( $config );
47 $this->auth
= new CF_Authentication(
50 null, // account; unused
51 $config['swiftAuthUrl']
54 $this->authTTL
= isset( $config['swiftAuthTTL'] )
55 ?
$config['swiftAuthTTL']
56 : 120; // some sane number
57 $this->swiftAnonUser
= isset( $config['swiftAnonUser'] )
58 ?
$config['swiftAnonUser']
60 $this->shardViaHashLevels
= isset( $config['shardViaHashLevels'] )
61 ?
$config['shardViaHashLevels']
66 * @see FileBackend::resolveContainerPath()
68 protected function resolveContainerPath( $container, $relStoragePath ) {
69 if ( strlen( urlencode( $relStoragePath ) ) > 1024 ) {
70 return null; // too long for Swift
72 return $relStoragePath;
76 * @see FileBackend::isPathUsableInternal()
78 public function isPathUsableInternal( $storagePath ) {
79 list( $container, $rel ) = $this->resolveStoragePathReal( $storagePath );
80 if ( $rel === null ) {
81 return false; // invalid
85 $this->getContainer( $container );
86 return true; // container exists
87 } catch ( NoSuchContainerException
$e ) {
88 } catch ( InvalidResponseException
$e ) {
89 } catch ( Exception
$e ) { // some other exception?
90 $this->logException( $e, __METHOD__
, array( 'path' => $storagePath ) );
97 * @see FileBackend::doCopyInternal()
99 protected function doCreateInternal( array $params ) {
100 $status = Status
::newGood();
102 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
103 if ( $dstRel === null ) {
104 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
108 // (a) Check the destination container and object
110 $dContObj = $this->getContainer( $dstCont );
111 if ( empty( $params['overwrite'] ) ) {
112 $destObj = $dContObj->create_object( $dstRel );
113 // Check if the object already exists (fields populated)
114 if ( $destObj->last_modified
) {
115 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
119 } catch ( NoSuchContainerException
$e ) {
120 $status->fatal( 'backend-fail-create', $params['dst'] );
122 } catch ( InvalidResponseException
$e ) {
123 $status->fatal( 'backend-fail-connect', $this->name
);
125 } catch ( Exception
$e ) { // some other exception?
126 $status->fatal( 'backend-fail-internal', $this->name
);
127 $this->logException( $e, __METHOD__
, $params );
131 // (b) Get a SHA-1 hash of the object
132 $sha1Hash = wfBaseConvert( sha1( $params['content'] ), 16, 36, 31 );
134 // (c) Actually create the object
136 // Create a fresh CF_Object with no fields preloaded.
137 // We don't want to preserve headers, metadata, and such.
138 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
139 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
140 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
141 // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
142 // The MD5 here will be checked within Swift against its own MD5.
143 $obj->set_etag( md5( $params['content'] ) );
144 // Use the same content type as StreamFile for security
145 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
146 // Actually write the object in Swift
147 $obj->write( $params['content'] );
148 } catch ( BadContentTypeException
$e ) {
149 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
150 } catch ( InvalidResponseException
$e ) {
151 $status->fatal( 'backend-fail-connect', $this->name
);
152 } catch ( Exception
$e ) { // some other exception?
153 $status->fatal( 'backend-fail-internal', $this->name
);
154 $this->logException( $e, __METHOD__
, $params );
161 * @see FileBackend::doStoreInternal()
163 protected function doStoreInternal( array $params ) {
164 $status = Status
::newGood();
166 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
167 if ( $dstRel === null ) {
168 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
172 // (a) Check the destination container and object
174 $dContObj = $this->getContainer( $dstCont );
175 if ( empty( $params['overwrite'] ) ) {
176 $destObj = $dContObj->create_object( $dstRel );
177 // Check if the object already exists (fields populated)
178 if ( $destObj->last_modified
) {
179 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
183 } catch ( NoSuchContainerException
$e ) {
184 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
186 } catch ( InvalidResponseException
$e ) {
187 $status->fatal( 'backend-fail-connect', $this->name
);
189 } catch ( Exception
$e ) { // some other exception?
190 $status->fatal( 'backend-fail-internal', $this->name
);
191 $this->logException( $e, __METHOD__
, $params );
195 // (b) Get a SHA-1 hash of the object
196 $sha1Hash = sha1_file( $params['src'] );
197 if ( $sha1Hash === false ) { // source doesn't exist?
198 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
201 $sha1Hash = wfBaseConvert( $sha1Hash, 16, 36, 31 );
203 // (c) Actually store the object
205 // Create a fresh CF_Object with no fields preloaded.
206 // We don't want to preserve headers, metadata, and such.
207 $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
208 // Note: metadata keys stored as [Upper case char][[Lower case char]...]
209 $obj->metadata
= array( 'Sha1base36' => $sha1Hash );
210 // The MD5 here will be checked within Swift against its own MD5.
211 $obj->set_etag( md5_file( $params['src'] ) );
212 // Use the same content type as StreamFile for security
213 $obj->content_type
= StreamFile
::contentTypeFromPath( $params['dst'] );
214 // Actually write the object in Swift
215 $obj->load_from_filename( $params['src'], True ); // calls $obj->write()
216 } catch ( BadContentTypeException
$e ) {
217 $status->fatal( 'backend-fail-contenttype', $params['dst'] );
218 } catch ( IOException
$e ) {
219 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
220 } catch ( InvalidResponseException
$e ) {
221 $status->fatal( 'backend-fail-connect', $this->name
);
222 } catch ( Exception
$e ) { // some other exception?
223 $status->fatal( 'backend-fail-internal', $this->name
);
224 $this->logException( $e, __METHOD__
, $params );
231 * @see FileBackend::doCopyInternal()
233 protected function doCopyInternal( array $params ) {
234 $status = Status
::newGood();
236 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
237 if ( $srcRel === null ) {
238 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
242 list( $dstCont, $dstRel ) = $this->resolveStoragePathReal( $params['dst'] );
243 if ( $dstRel === null ) {
244 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
248 // (a) Check the source/destination containers and destination object
250 $sContObj = $this->getContainer( $srcCont );
251 $dContObj = $this->getContainer( $dstCont );
252 if ( empty( $params['overwrite'] ) ) {
253 $destObj = $dContObj->create_object( $dstRel );
254 // Check if the object already exists (fields populated)
255 if ( $destObj->last_modified
) {
256 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
260 } catch ( NoSuchContainerException
$e ) {
261 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
263 } catch ( InvalidResponseException
$e ) {
264 $status->fatal( 'backend-fail-connect', $this->name
);
266 } catch ( Exception
$e ) { // some other exception?
267 $status->fatal( 'backend-fail-internal', $this->name
);
268 $this->logException( $e, __METHOD__
, $params );
272 // (b) Actually copy the file to the destination
274 $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel );
275 } catch ( NoSuchObjectException
$e ) { // source object does not exist
276 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
277 } catch ( InvalidResponseException
$e ) {
278 $status->fatal( 'backend-fail-connect', $this->name
);
279 } catch ( Exception
$e ) { // some other exception?
280 $status->fatal( 'backend-fail-internal', $this->name
);
281 $this->logException( $e, __METHOD__
, $params );
288 * @see FileBackend::doDeleteInternal()
290 protected function doDeleteInternal( array $params ) {
291 $status = Status
::newGood();
293 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
294 if ( $srcRel === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
300 $sContObj = $this->getContainer( $srcCont );
301 $sContObj->delete_object( $srcRel );
302 } catch ( NoSuchContainerException
$e ) {
303 $status->fatal( 'backend-fail-delete', $params['src'] );
304 } catch ( NoSuchObjectException
$e ) {
305 if ( empty( $params['ignoreMissingSource'] ) ) {
306 $status->fatal( 'backend-fail-delete', $params['src'] );
308 } catch ( InvalidResponseException
$e ) {
309 $status->fatal( 'backend-fail-connect', $this->name
);
310 } catch ( Exception
$e ) { // some other exception?
311 $status->fatal( 'backend-fail-internal', $this->name
);
312 $this->logException( $e, __METHOD__
, $params );
319 * @see FileBackend::doPrepareInternal()
321 protected function doPrepareInternal( $fullCont, $dir, array $params ) {
322 $status = Status
::newGood();
324 // (a) Check if container already exists
326 $contObj = $this->getContainer( $fullCont );
327 // NoSuchContainerException not thrown: container must exist
328 return $status; // already exists
329 } catch ( NoSuchContainerException
$e ) {
330 // NoSuchContainerException thrown: container does not exist
331 } catch ( InvalidResponseException
$e ) {
332 $status->fatal( 'backend-fail-connect', $this->name
);
334 } catch ( Exception
$e ) { // some other exception?
335 $status->fatal( 'backend-fail-internal', $this->name
);
336 $this->logException( $e, __METHOD__
, $params );
340 // (b) Create container as needed
342 $contObj = $this->createContainer( $fullCont );
343 if ( $this->swiftAnonUser
!= '' ) {
344 // Make container public to end-users...
345 $status->merge( $this->setContainerAccess(
347 array( $this->auth
->username
, $this->swiftAnonUser
), // read
348 array( $this->auth
->username
) // write
351 } catch ( InvalidResponseException
$e ) {
352 $status->fatal( 'backend-fail-connect', $this->name
);
354 } catch ( Exception
$e ) { // some other exception?
355 $status->fatal( 'backend-fail-internal', $this->name
);
356 $this->logException( $e, __METHOD__
, $params );
364 * @see FileBackend::doSecureInternal()
366 protected function doSecureInternal( $fullCont, $dir, array $params ) {
367 $status = Status
::newGood();
369 if ( $this->swiftAnonUser
!= '' ) {
370 // Restrict container from end-users...
372 // doPrepareInternal() should have been called,
373 // so the Swift container should already exist...
374 $contObj = $this->getContainer( $fullCont ); // normally a cache hit
375 // NoSuchContainerException not thrown: container must exist
376 if ( !isset( $contObj->mw_wasSecured
) ) {
377 $status->merge( $this->setContainerAccess(
379 array( $this->auth
->username
), // read
380 array( $this->auth
->username
) // write
382 // @TODO: when php-cloudfiles supports container
383 // metadata, we can make use of that to avoid RTTs
384 $contObj->mw_wasSecured
= true; // avoid useless RTTs
386 } catch ( InvalidResponseException
$e ) {
387 $status->fatal( 'backend-fail-connect', $this->name
);
388 } catch ( Exception
$e ) { // some other exception?
389 $status->fatal( 'backend-fail-internal', $this->name
);
390 $this->logException( $e, __METHOD__
, $params );
398 * @see FileBackend::doCleanInternal()
400 protected function doCleanInternal( $fullCont, $dir, array $params ) {
401 $status = Status
::newGood();
403 // Only containers themselves can be removed, all else is virtual
405 return $status; // nothing to do
408 // (a) Check the container
410 $contObj = $this->getContainer( $fullCont, true );
411 } catch ( NoSuchContainerException
$e ) {
412 return $status; // ok, nothing to do
413 } catch ( InvalidResponseException
$e ) {
414 $status->fatal( 'backend-fail-connect', $this->name
);
416 } catch ( Exception
$e ) { // some other exception?
417 $status->fatal( 'backend-fail-internal', $this->name
);
418 $this->logException( $e, __METHOD__
, $params );
422 // (b) Delete the container if empty
423 if ( $contObj->object_count
== 0 ) {
425 $this->deleteContainer( $fullCont );
426 } catch ( NoSuchContainerException
$e ) {
427 return $status; // race?
428 } catch ( InvalidResponseException
$e ) {
429 $status->fatal( 'backend-fail-connect', $this->name
);
431 } catch ( Exception
$e ) { // some other exception?
432 $status->fatal( 'backend-fail-internal', $this->name
);
433 $this->logException( $e, __METHOD__
, $params );
442 * @see FileBackend::doFileExists()
444 protected function doGetFileStat( array $params ) {
445 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
446 if ( $srcRel === null ) {
447 return false; // invalid storage path
452 $contObj = $this->getContainer( $srcCont );
453 $srcObj = $contObj->get_object( $srcRel, $this->headersFromParams( $params ) );
455 // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
456 'mtime' => wfTimestamp( TS_MW
, $srcObj->last_modified
),
457 'size' => $srcObj->content_length
,
458 'sha1' => $srcObj->metadata
['Sha1base36']
460 } catch ( NoSuchContainerException
$e ) {
461 } catch ( NoSuchObjectException
$e ) {
462 } catch ( InvalidResponseException
$e ) {
464 } catch ( Exception
$e ) { // some other exception?
466 $this->logException( $e, __METHOD__
, $params );
473 * @see FileBackendBase::getFileContents()
475 public function getFileContents( array $params ) {
476 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
477 if ( $srcRel === null ) {
478 return false; // invalid storage path
483 $container = $this->getContainer( $srcCont );
484 $obj = $container->get_object( $srcRel );
485 $data = $obj->read( $this->headersFromParams( $params ) );
486 } catch ( NoSuchContainerException
$e ) {
487 } catch ( NoSuchObjectException
$e ) {
488 } catch ( InvalidResponseException
$e ) {
489 } catch ( Exception
$e ) { // some other exception?
490 $this->logException( $e, __METHOD__
, $params );
497 * @see FileBackend::getFileListInternal()
499 public function getFileListInternal( $fullCont, $dir, array $params ) {
500 return new SwiftFileBackendFileList( $this, $fullCont, $dir );
504 * Do not call this function outside of SwiftFileBackendFileList
506 * @param $fullCont string Resolved container name
507 * @param $dir string Resolved storage directory with no trailing slash
508 * @param $after string Storage path of file to list items after
509 * @param $limit integer Max number of items to list
512 public function getFileListPageInternal( $fullCont, $dir, $after, $limit ) {
516 $container = $this->getContainer( $fullCont );
517 $prefix = ( $dir == '' ) ?
null : "{$dir}/";
518 $files = $container->list_objects( $limit, $after, $prefix );
519 } catch ( NoSuchContainerException
$e ) {
520 } catch ( NoSuchObjectException
$e ) {
521 } catch ( InvalidResponseException
$e ) {
522 } catch ( Exception
$e ) { // some other exception?
523 $this->logException( $e, __METHOD__
, array( 'cont' => $fullCont, 'dir' => $dir ) );
530 * @see FileBackend::doGetFileSha1base36()
532 public function doGetFileSha1base36( array $params ) {
533 $stat = $this->getFileStat( $params );
535 return $stat['sha1'];
542 * @see FileBackend::doStreamFile()
544 protected function doStreamFile( array $params ) {
545 $status = Status
::newGood();
547 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
548 if ( $srcRel === null ) {
549 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
553 $cont = $this->getContainer( $srcCont );
554 } catch ( NoSuchContainerException
$e ) {
555 $status->fatal( 'backend-fail-stream', $params['src'] );
557 } catch ( InvalidResponseException
$e ) {
558 $status->fatal( 'backend-fail-connect', $this->name
);
560 } catch ( Exception
$e ) { // some other exception?
561 $status->fatal( 'backend-fail-stream', $params['src'] );
562 $this->logException( $e, __METHOD__
, $params );
567 $output = fopen( 'php://output', 'w' );
568 // FileBackend::streamFile() already checks existence
569 $obj = new CF_Object( $cont, $srcRel, false, false ); // skip HEAD request
570 $obj->stream( $output, $this->headersFromParams( $params ) );
571 } catch ( InvalidResponseException
$e ) { // 404? connection problem?
572 $status->fatal( 'backend-fail-stream', $params['src'] );
573 } catch ( Exception
$e ) { // some other exception?
574 $status->fatal( 'backend-fail-stream', $params['src'] );
575 $this->logException( $e, __METHOD__
, $params );
582 * @see FileBackend::getLocalCopy()
584 public function getLocalCopy( array $params ) {
585 list( $srcCont, $srcRel ) = $this->resolveStoragePathReal( $params['src'] );
586 if ( $srcRel === null ) {
592 $cont = $this->getContainer( $srcCont );
593 $obj = $cont->get_object( $srcRel );
594 // Get source file extension
595 $ext = FileBackend
::extensionFromPath( $srcRel );
596 // Create a new temporary file...
597 $tmpFile = TempFSFile
::factory( wfBaseName( $srcRel ) . '_', $ext );
599 $handle = fopen( $tmpFile->getPath(), 'wb' );
601 $obj->stream( $handle, $this->headersFromParams( $params ) );
604 $tmpFile = null; // couldn't open temp file
607 } catch ( NoSuchContainerException
$e ) {
609 } catch ( NoSuchObjectException
$e ) {
611 } catch ( InvalidResponseException
$e ) {
613 } catch ( Exception
$e ) { // some other exception?
615 $this->logException( $e, __METHOD__
, $params );
622 * Get headers to send to Swift when reading a file based
623 * on a FileBackend params array, e.g. that of getLocalCopy().
624 * $params is currently only checked for a 'latest' flag.
626 * @param $params Array
629 protected function headersFromParams( array $params ) {
631 if ( !empty( $params['latest'] ) ) {
632 $hdrs[] = 'X-Newest: true';
638 * Set read/write permissions for a Swift container
640 * @param $contObj CF_Container Swift container
641 * @param $readGrps Array Swift users who can read (account:user)
642 * @param $writeGrps Array Swift users who can write (account:user)
645 protected function setContainerAccess(
646 CF_Container
$contObj, array $readGrps, array $writeGrps
648 $creds = $contObj->cfs_auth
->export_credentials();
650 $url = $creds['storage_url'] . '/' . rawurlencode( $contObj->name
);
652 // Note: 10 second timeout consistent with php-cloudfiles
653 $req = new CurlHttpRequest( $url, array( 'method' => 'POST', 'timeout' => 10 ) );
654 $req->setHeader( 'X-Auth-Token', $creds['auth_token'] );
655 $req->setHeader( 'X-Container-Read', implode( ',', $readGrps ) );
656 $req->setHeader( 'X-Container-Write', implode( ',', $writeGrps ) );
658 return $req->execute(); // should return 204
662 * Get a connection to the Swift proxy
664 * @return CF_Connection|false
665 * @throws InvalidResponseException
667 protected function getConnection() {
668 if ( $this->conn
=== false ) {
669 throw new InvalidResponseException
; // failed last attempt
671 // Session keys expire after a while, so we renew them periodically
672 if ( $this->conn
&& ( time() - $this->connStarted
) > $this->authTTL
) {
673 $this->conn
->close(); // close active cURL connections
676 // Authenticate with proxy and get a session key...
677 if ( $this->conn
=== null ) {
678 $this->connContainers
= array();
680 $this->auth
->authenticate();
681 $this->conn
= new CF_Connection( $this->auth
);
682 $this->connStarted
= time();
683 } catch ( AuthenticationException
$e ) {
684 $this->conn
= false; // don't keep re-trying
685 } catch ( InvalidResponseException
$e ) {
686 $this->conn
= false; // don't keep re-trying
689 if ( !$this->conn
) {
690 throw new InvalidResponseException
; // auth/connection problem
696 * @see FileBackend::doClearCache()
698 protected function doClearCache( array $paths = null ) {
699 $this->connContainers
= array(); // clear container object cache
703 * Get a Swift container object, possibly from process cache.
704 * Use $reCache if the file count or byte count is needed.
706 * @param $container string Container name
707 * @param $reCache bool Refresh the process cache
708 * @return CF_Container
710 protected function getContainer( $container, $reCache = false ) {
711 $conn = $this->getConnection(); // Swift proxy connection
713 unset( $this->connContainers
[$container] ); // purge cache
715 if ( !isset( $this->connContainers
[$container] ) ) {
716 $contObj = $conn->get_container( $container );
717 // NoSuchContainerException not thrown: container must exist
718 if ( count( $this->connContainers
) >= $this->maxContCacheSize
) { // trim cache?
719 reset( $this->connContainers
);
720 $key = key( $this->connContainers
);
721 unset( $this->connContainers
[$key] );
723 $this->connContainers
[$container] = $contObj; // cache it
725 return $this->connContainers
[$container];
729 * Create a Swift container
731 * @param $container string Container name
732 * @return CF_Container
734 protected function createContainer( $container ) {
735 $conn = $this->getConnection(); // Swift proxy connection
736 $contObj = $conn->create_container( $container );
737 $this->connContainers
[$container] = $contObj; // cache it
742 * Delete a Swift container
744 * @param $container string Container name
747 protected function deleteContainer( $container ) {
748 $conn = $this->getConnection(); // Swift proxy connection
749 $conn->delete_container( $container );
750 unset( $this->connContainers
[$container] ); // purge cache
754 * Log an unexpected exception for this backend
756 * @param $e Exception
757 * @param $func string
758 * @param $params Array
761 protected function logException( Exception
$e, $func, array $params ) {
762 wfDebugLog( 'SwiftBackend',
763 get_class( $e ) . " in '{$this->name}': '{$func}' with " . serialize( $params )
769 * SwiftFileBackend helper class to page through object listings.
770 * Swift also has a listing limit of 10,000 objects for sanity.
771 * Do not use this class from places outside SwiftFileBackend.
773 * @ingroup FileBackend
775 class SwiftFileBackendFileList
implements Iterator
{
777 protected $bufferIter = array();
778 protected $bufferAfter = null; // string; list items *after* this path
779 protected $pos = 0; // integer
781 /** @var SwiftFileBackend */
783 protected $container; //
784 protected $dir; // string storage directory
785 protected $suffixStart; // integer
787 const PAGE_SIZE
= 5000; // file listing buffer size
790 * @param $backend SwiftFileBackend
791 * @param $fullCont string Resolved container name
792 * @param $dir string Resolved directory relative to container
794 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir ) {
795 $this->backend
= $backend;
796 $this->container
= $fullCont;
798 if ( substr( $this->dir
, -1 ) === '/' ) {
799 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
801 if ( $this->dir
== '' ) { // whole container
802 $this->suffixStart
= 0;
803 } else { // dir within container
804 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
808 public function current() {
809 return substr( current( $this->bufferIter
), $this->suffixStart
);
812 public function key() {
816 public function next() {
817 // Advance to the next file in the page
818 next( $this->bufferIter
);
820 // Check if there are no files left in this page and
821 // advance to the next page if this page was not empty.
822 if ( !$this->valid() && count( $this->bufferIter
) ) {
823 $this->bufferAfter
= end( $this->bufferIter
);
824 $this->bufferIter
= $this->backend
->getFileListPageInternal(
825 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
830 public function rewind() {
832 $this->bufferAfter
= null;
833 $this->bufferIter
= $this->backend
->getFileListPageInternal(
834 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
838 public function valid() {
839 return ( current( $this->bufferIter
) !== false ); // no paths can have this value