Merge "Make sure that SQLite uses no prefix"
[lhc/web/wiklou.git] / includes / filebackend / FileBackendStore.php
index a29816f..97e49a5 100644 (file)
@@ -72,8 +72,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,12 +84,12 @@ 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
         *   - async         : Status will be returned immediately if supported.
         *                     If the status is OK, then its value field will be
@@ -106,9 +107,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,12 +121,12 @@ 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
         *   - async         : Status will be returned immediately if supported.
         *                     If the status is OK, then its value field will be
@@ -145,9 +144,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 +158,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 +178,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 +221,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 +242,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;
@@ -809,6 +804,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
@@ -834,6 +837,14 @@ abstract class FileBackendStore extends FileBackend {
                        $status = $this->doStreamFile( $params );
                        wfProfileOut( __METHOD__ . '-send-' . $this->name );
                        wfProfileOut( __METHOD__ . '-send' );
+                       if ( !$status->isOK() ) {
+                               // Per bug 41113, nasty things can happen if bad cache entries get
+                               // stuck in cache. It's also possible that this error can come up
+                               // 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'] );
                }
@@ -1007,6 +1018,7 @@ abstract class FileBackendStore extends FileBackend {
         * Get a list of storage paths to lock for a list of operations
         * Returns an array with 'sh' (shared) and 'ex' (exclusive) keys,
         * each corresponding to a list of storage paths to be locked.
+        * All returned paths are normalized.
         *
         * @param $performOps Array List of FileOp objects
         * @return Array ('sh' => list of paths, 'ex' => list of paths)
@@ -1176,6 +1188,8 @@ abstract class FileBackendStore extends FileBackend {
 
        /**
         * @see FileBackendStore::executeOpHandlesInternal()
+        * @param array $fileOpHandles
+        * @throws MWException
         * @return Array List of corresponding Status objects
         */
        protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
@@ -1473,6 +1487,7 @@ abstract class FileBackendStore extends FileBackend {
        /**
         * Do a batch lookup from cache for container stats for all containers
         * used in a list of container names, storage paths, or FileOp objects.
+        * This loads the persistent cache values into the process cache.
         *
         * @param $items Array
         * @return void
@@ -1529,7 +1544,7 @@ abstract class FileBackendStore extends FileBackend {
        /**
         * Get the cache key for a file path
         *
-        * @param $path string Storage path
+        * @param $path string Normalized storage path
         * @return string
         */
        private function fileCacheKey( $path ) {
@@ -1545,6 +1560,10 @@ abstract class FileBackendStore extends FileBackend {
         * @param $val mixed Information to cache
         */
        final protected function setFileCache( $path, $val ) {
+               $path = FileBackend::normalizeStoragePath( $path );
+               if ( $path === null ) {
+                       return; // invalid storage path
+               }
                $this->memCache->add( $this->fileCacheKey( $path ), $val, 7*86400 );
        }
 
@@ -1555,6 +1574,10 @@ abstract class FileBackendStore extends FileBackend {
         * @param $path string Storage path
         */
        final protected function deleteFileCache( $path ) {
+               $path = FileBackend::normalizeStoragePath( $path );
+               if ( $path === null ) {
+                       return; // invalid storage path
+               }
                if ( !$this->memCache->set( $this->fileCacheKey( $path ), 'PURGED', 300 ) ) {
                        trigger_error( "Unable to delete stat cache for file $path." );
                }
@@ -1563,6 +1586,7 @@ abstract class FileBackendStore extends FileBackend {
        /**
         * Do a batch lookup from cache for file stats for all paths
         * used in a list of storage paths or FileOp objects.
+        * This loads the persistent cache values into the process cache.
         *
         * @param $items Array List of storage paths or FileOps
         * @return void
@@ -1579,9 +1603,11 @@ abstract class FileBackendStore extends FileBackend {
                                $paths = array_merge( $paths, $item->storagePathsRead() );
                                $paths = array_merge( $paths, $item->storagePathsChanged() );
                        } elseif ( self::isStoragePath( $item ) ) {
-                               $paths[] = $item;
+                               $paths[] = FileBackend::normalizeStoragePath( $item );
                        }
                }
+               // Get rid of any paths that failed normalization...
+               $paths = array_filter( $paths, 'strlen' ); // remove nulls
                // Get all the corresponding cache keys for paths...
                foreach ( $paths as $path ) {
                        list( $cont, $rel, $s ) = $this->resolveStoragePath( $path );