Merge "[FileBackend] Clear the stat cache in doQuickOperations() for sanity."
[lhc/web/wiklou.git] / includes / filebackend / FileBackendStore.php
index 39cfc2d..2d7fa07 100644 (file)
@@ -48,6 +48,8 @@ abstract class FileBackendStore extends FileBackend {
 
        protected $maxFileSize = 4294967296; // integer bytes (4GiB)
 
+       const CACHE_TTL = 10; // integer; TTL in seconds for process cache entries
+
        /**
         * @see FileBackend::__construct()
         *
@@ -72,8 +74,9 @@ abstract class FileBackendStore extends FileBackend {
        }
 
        /**
-        * Check if a file can be created at a given storage path.
-        * FS backends should check if the parent directory exists and the file is writable.
+        * Check if a file can be created or changed at a given storage path.
+        * FS backends should check if the parent directory exists, files can be
+        * written under it, and that any file already there is writable.
         * Backends using key/value stores should check if the container exists.
         *
         * @param $storagePath string
@@ -83,13 +86,14 @@ abstract class FileBackendStore extends FileBackend {
 
        /**
         * Create a file in the backend with the given contents.
+        * This will overwrite any file that exists at the destination.
         * Do not call this function from places outside FileBackend and FileOp.
         *
         * $params include:
         *   - content       : the raw file contents
         *   - dst           : destination storage path
-        *   - overwrite     : overwrite any file that exists at the destination
         *   - disposition   : Content-Disposition header value for the destination
+        *   - headers       : HTTP header name/value map
         *   - async         : Status will be returned immediately if supported.
         *                     If the status is OK, then its value field will be
         *                     set to a FileBackendStoreOpHandle object.
@@ -106,9 +110,7 @@ abstract class FileBackendStore extends FileBackend {
                } else {
                        $status = $this->doCreateInternal( $params );
                        $this->clearCache( array( $params['dst'] ) );
-                       if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
-                               $this->deleteFileCache( $params['dst'] ); // persistent cache
-                       }
+                       $this->deleteFileCache( $params['dst'] ); // persistent cache
                }
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
@@ -122,13 +124,14 @@ abstract class FileBackendStore extends FileBackend {
 
        /**
         * Store a file into the backend from a file on disk.
+        * This will overwrite any file that exists at the destination.
         * Do not call this function from places outside FileBackend and FileOp.
         *
         * $params include:
         *   - src           : source path on disk
         *   - dst           : destination storage path
-        *   - overwrite     : overwrite any file that exists at the destination
         *   - disposition   : Content-Disposition header value for the destination
+        *   - headers       : HTTP header name/value map
         *   - async         : Status will be returned immediately if supported.
         *                     If the status is OK, then its value field will be
         *                     set to a FileBackendStoreOpHandle object.
@@ -145,9 +148,7 @@ abstract class FileBackendStore extends FileBackend {
                } else {
                        $status = $this->doStoreInternal( $params );
                        $this->clearCache( array( $params['dst'] ) );
-                       if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
-                               $this->deleteFileCache( $params['dst'] ); // persistent cache
-                       }
+                       $this->deleteFileCache( $params['dst'] ); // persistent cache
                }
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
@@ -161,16 +162,17 @@ abstract class FileBackendStore extends FileBackend {
 
        /**
         * Copy a file from one storage path to another in the backend.
+        * This will overwrite any file that exists at the destination.
         * Do not call this function from places outside FileBackend and FileOp.
         *
         * $params include:
-        *   - src           : source storage path
-        *   - dst           : destination storage path
-        *   - overwrite     : overwrite any file that exists at the destination
-        *   - disposition   : Content-Disposition header value for the destination
-        *   - async         : Status will be returned immediately if supported.
-        *                     If the status is OK, then its value field will be
-        *                     set to a FileBackendStoreOpHandle object.
+        *   - src                 : source storage path
+        *   - dst                 : destination storage path
+        *   - ignoreMissingSource : do nothing if the source file does not exist
+        *   - disposition         : Content-Disposition header value for the destination
+        *   - async               : Status will be returned immediately if supported.
+        *                           If the status is OK, then its value field will be
+        *                           set to a FileBackendStoreOpHandle object.
         *
         * @param $params Array
         * @return Status
@@ -180,9 +182,7 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ . '-' . $this->name );
                $status = $this->doCopyInternal( $params );
                $this->clearCache( array( $params['dst'] ) );
-               if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
-                       $this->deleteFileCache( $params['dst'] ); // persistent cache
-               }
+               $this->deleteFileCache( $params['dst'] ); // persistent cache
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
                return $status;
@@ -225,16 +225,17 @@ abstract class FileBackendStore extends FileBackend {
 
        /**
         * Move a file from one storage path to another in the backend.
+        * This will overwrite any file that exists at the destination.
         * Do not call this function from places outside FileBackend and FileOp.
         *
         * $params include:
-        *   - src           : source storage path
-        *   - dst           : destination storage path
-        *   - overwrite     : overwrite any file that exists at the destination
-        *   - disposition   : Content-Disposition header value for the destination
-        *   - async         : Status will be returned immediately if supported.
-        *                     If the status is OK, then its value field will be
-        *                     set to a FileBackendStoreOpHandle object.
+        *   - src                 : source storage path
+        *   - dst                 : destination storage path
+        *   - ignoreMissingSource : do nothing if the source file does not exist
+        *   - disposition         : Content-Disposition header value for the destination
+        *   - async               : Status will be returned immediately if supported.
+        *                           If the status is OK, then its value field will be
+        *                           set to a FileBackendStoreOpHandle object.
         *
         * @param $params Array
         * @return Status
@@ -245,9 +246,7 @@ abstract class FileBackendStore extends FileBackend {
                $status = $this->doMoveInternal( $params );
                $this->clearCache( array( $params['src'], $params['dst'] ) );
                $this->deleteFileCache( $params['src'] ); // persistent cache
-               if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
-                       $this->deleteFileCache( $params['dst'] ); // persistent cache
-               }
+               $this->deleteFileCache( $params['dst'] ); // persistent cache
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
                return $status;
@@ -510,6 +509,7 @@ abstract class FileBackendStore extends FileBackend {
                                        $subDir = $params['dir'] . "/{$subDirRel}"; // full path
                                        $status->merge( $this->doClean( array( 'dir' => $subDir ) + $params ) );
                                }
+                               unset( $subDirsRel ); // free directory for rmdir() on Windows (for FS backends)
                        }
                }
 
@@ -606,14 +606,15 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ );
                wfProfileIn( __METHOD__ . '-' . $this->name );
                $latest = !empty( $params['latest'] ); // use latest data?
-               if ( !$this->cheapCache->has( $path, 'stat' ) ) {
+               if ( !$this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
                        $this->primeFileCache( array( $path ) ); // check persistent cache
                }
-               if ( $this->cheapCache->has( $path, 'stat' ) ) {
+               if ( $this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
                        $stat = $this->cheapCache->get( $path, 'stat' );
                        // If we want the latest data, check that this cached
-                       // value was in fact fetched with the latest available data.
-                       if ( !$latest || $stat['latest'] ) {
+                       // value was in fact fetched with the latest available data
+                       // (the process cache is ignored if it contains a negative).
+                       if ( !$latest || ( is_array( $stat ) && $stat['latest'] ) ) {
                                wfProfileOut( __METHOD__ . '-' . $this->name );
                                wfProfileOut( __METHOD__ );
                                return $stat;
@@ -624,7 +625,7 @@ abstract class FileBackendStore extends FileBackend {
                $stat = $this->doGetFileStat( $params );
                wfProfileOut( __METHOD__ . '-miss-' . $this->name );
                wfProfileOut( __METHOD__ . '-miss' );
-               if ( is_array( $stat ) ) { // don't cache negatives
+               if ( is_array( $stat ) ) { // file exists
                        $stat['latest'] = $latest;
                        $this->cheapCache->set( $path, 'stat', $stat );
                        $this->setFileCache( $path, $stat ); // update persistent cache
@@ -632,8 +633,11 @@ abstract class FileBackendStore extends FileBackend {
                                $this->cheapCache->set( $path, 'sha1',
                                        array( 'hash' => $stat['sha1'], 'latest' => $latest ) );
                        }
-               } else {
+               } elseif ( $stat === false ) { // file does not exist
+                       $this->cheapCache->set( $path, 'stat', false );
                        wfDebug( __METHOD__ . ": File $path does not exist.\n" );
+               } else { // an error occurred
+                       wfDebug( __METHOD__ . ": Could not stat file $path.\n" );
                }
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
@@ -687,7 +691,7 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ );
                wfProfileIn( __METHOD__ . '-' . $this->name );
                $latest = !empty( $params['latest'] ); // use latest data?
-               if ( $this->cheapCache->has( $path, 'sha1' ) ) {
+               if ( $this->cheapCache->has( $path, 'sha1', self::CACHE_TTL ) ) {
                        $stat = $this->cheapCache->get( $path, 'sha1' );
                        // If we want the latest data, check that this cached
                        // value was in fact fetched with the latest available data.
@@ -809,6 +813,14 @@ abstract class FileBackendStore extends FileBackend {
         */
        abstract protected function doGetLocalCopyMulti( array $params );
 
+       /**
+        * @see FileBackend::getFileHttpUrl()
+        * @return string|null
+        */
+       public function getFileHttpUrl( array $params ) {
+               return null; // not supported
+       }
+
        /**
         * @see FileBackend::streamFile()
         * @return Status
@@ -840,6 +852,7 @@ abstract class FileBackendStore extends FileBackend {
                                // with simple race conditions. Clear out the stat cache to be safe.
                                $this->clearCache( array( $params['src'] ) );
                                $this->deleteFileCache( $params['src'] );
+                               trigger_error( "Bad stat cache or race condition for file {$params['src']}." );
                        }
                } else {
                        $status->fatal( 'backend-fail-stream', $params['src'] );
@@ -1055,6 +1068,9 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ . '-' . $this->name );
                $status = Status::newGood();
 
+               // Fix up custom header name/value pairs...
+               $ops = array_map( array( $this, 'stripInvalidHeadersFromOp' ), $ops );
+
                // Build up a list of FileOps...
                $performOps = $this->getOperationsInternal( $ops );
 
@@ -1104,6 +1120,12 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ . '-' . $this->name );
                $status = Status::newGood();
 
+               // Fix up custom header name/value pairs...
+               $ops = array_map( array( $this, 'stripInvalidHeadersFromOp' ), $ops );
+
+               // Clear any file cache entries
+               $this->clearCache();
+
                $supportedOps = array( 'create', 'store', 'copy', 'move', 'delete', 'null' );
                $async = ( $this->parallelize === 'implicit' );
                $maxConcurrency = $this->concurrency; // throttle
@@ -1195,6 +1217,24 @@ abstract class FileBackendStore extends FileBackend {
                return array();
        }
 
+       /**
+        * Strip long HTTP headers from a file operation
+        *
+        * @param $op array Same format as doOperation()
+        * @return Array
+        */
+       protected function stripInvalidHeadersFromOp( array $op ) {
+               if ( isset( $op['headers'] ) ) {
+                       foreach ( $op['headers'] as $name => $value ) {
+                               if ( strlen( $name ) > 255 || strlen( $value ) > 255 ) {
+                                       trigger_error( "Header '$name: $value' is too long." );
+                                       unset( $op['headers'][$name] );
+                               }
+                       }
+               }
+               return $op;
+       }
+
        /**
         * @see FileBackend::preloadCache()
         */