Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFileBackend.php
index 9839fa1..8157916 100644 (file)
@@ -1,5 +1,22 @@
 <?php
 /**
+ * File system based backend.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup FileBackend
  * @author Aaron Schulz
@@ -61,6 +78,8 @@ class FSFileBackend extends FileBackendStore {
 
        /**
         * @see FileBackendStore::resolveContainerPath()
+        * @param $container string
+        * @param $relStoragePath string
         * @return null|string
         */
        protected function resolveContainerPath( $container, $relStoragePath ) {
@@ -174,17 +193,39 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $ok = copy( $params['src'], $dest );
-               if ( !$ok ) {
-                       $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
-                       return $status;
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
+                               wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
+                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
+               } else { // immediate write
+                       $ok = copy( $params['src'], $dest );
+                       // In some cases (at least over NFS), copy() returns true when it fails
+                       if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
+                               if ( $ok ) { // PHP bug
+                                       unlink( $dest ); // remove broken file
+                                       trigger_error( __METHOD__ . ": copy() failed but returned true." );
+                               }
+                               $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
+                               return $status;
+                       }
+                       $this->chmod( $dest );
                }
 
-               $this->chmod( $dest );
-
                return $status;
        }
 
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseStore( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doCopyInternal()
         * @return Status
@@ -217,17 +258,39 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $ok = copy( $source, $dest );
-               if ( !$ok ) {
-                       $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
-                       return $status;
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
+                               wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
+                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
+               } else { // immediate write
+                       $ok = copy( $source, $dest );
+                       // In some cases (at least over NFS), copy() returns true when it fails
+                       if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
+                               if ( $ok ) { // PHP bug
+                                       unlink( $dest ); // remove broken file
+                                       trigger_error( __METHOD__ . ": copy() failed but returned true." );
+                               }
+                               $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
+                               return $status;
+                       }
+                       $this->chmod( $dest );
                }
 
-               $this->chmod( $dest );
-
                return $status;
        }
 
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseCopy( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doMoveInternal()
         * @return Status
@@ -263,16 +326,34 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $ok = rename( $source, $dest );
-               clearstatcache(); // file no longer at source
-               if ( !$ok ) {
-                       $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
-                       return $status;
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $cmd = implode( ' ', array( wfIsWindows() ? 'MOVE' : 'mv',
+                               wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
+                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Move', $cmd );
+               } else { // immediate write
+                       $ok = rename( $source, $dest );
+                       clearstatcache(); // file no longer at source
+                       if ( !$ok ) {
+                               $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
+                               return $status;
+                       }
                }
 
                return $status;
        }
 
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseMove( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doDeleteInternal()
         * @return Status
@@ -293,15 +374,32 @@ class FSFileBackend extends FileBackendStore {
                        return $status; // do nothing; either OK or bad status
                }
 
-               $ok = unlink( $source );
-               if ( !$ok ) {
-                       $status->fatal( 'backend-fail-delete', $params['src'] );
-                       return $status;
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $cmd = implode( ' ', array( wfIsWindows() ? 'DEL' : 'unlink',
+                               wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd );
+               } else { // immediate write
+                       $ok = unlink( $source );
+                       if ( !$ok ) {
+                               $status->fatal( 'backend-fail-delete', $params['src'] );
+                               return $status;
+                       }
                }
 
                return $status;
        }
 
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseDelete( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-delete', $params['src'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doCreateInternal()
         * @return Status
@@ -328,17 +426,45 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $bytes = file_put_contents( $dest, $params['content'] );
-               if ( $bytes === false ) {
-                       $status->fatal( 'backend-fail-create', $params['dst'] );
-                       return $status;
+               if ( !empty( $params['async'] ) ) { // deferred
+                       $tempFile = TempFSFile::factory( 'create_', 'tmp' );
+                       if ( !$tempFile ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
+                       if ( $bytes === false ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
+                               wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
+                               wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
+                       ) );
+                       $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
+                       $tempFile->bind( $status->value );
+               } else { // immediate write
+                       $bytes = file_put_contents( $dest, $params['content'] );
+                       if ( $bytes === false ) {
+                               $status->fatal( 'backend-fail-create', $params['dst'] );
+                               return $status;
+                       }
+                       $this->chmod( $dest );
                }
 
-               $this->chmod( $dest );
-
                return $status;
        }
 
+       /**
+        * @see FSFileBackend::doExecuteOpHandlesInternal()
+        */
+       protected function _getResponseCreate( $errors, Status $status, array $params, $cmd ) {
+               if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
+                       $status->fatal( 'backend-fail-create', $params['dst'] );
+                       trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
+               }
+       }
+
        /**
         * @see FileBackendStore::doPrepareInternal()
         * @return Status
@@ -542,6 +668,40 @@ class FSFileBackend extends FileBackendStore {
                return false;
        }
 
+       /**
+        * @see FileBackendStore::doExecuteOpHandlesInternal()
+        * @return Array List of corresponding Status objects
+        */
+       protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
+               $statuses = array();
+
+               $pipes = array();
+               foreach ( $fileOpHandles as $index => $fileOpHandle ) {
+                       $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
+               }
+
+               $errs = array();
+               foreach ( $pipes as $index => $pipe ) {
+                       // Result will be empty on success in *NIX. On Windows,
+                       // it may be something like "        1 file(s) [copied|moved].".
+                       $errs[$index] = stream_get_contents( $pipe );
+                       fclose( $pipe );
+               }
+
+               foreach ( $fileOpHandles as $index => $fileOpHandle ) {
+                       $status = Status::newGood();
+                       $function = '_getResponse' . $fileOpHandle->call;
+                       $this->$function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
+                       $statuses[$index] = $status;
+                       if ( $status->isOK() && $fileOpHandle->chmodPath ) {
+                               $this->chmod( $fileOpHandle->chmodPath );
+                       }
+               }
+
+               clearstatcache(); // files changed
+               return $statuses;
+       }
+
        /**
         * Chmod a file, suppressing the warnings
         *
@@ -556,6 +716,16 @@ class FSFileBackend extends FileBackendStore {
                return $ok;
        }
 
+       /**
+        * Clean up directory separators for the given OS
+        *
+        * @param $path string FS path
+        * @return string
+        */
+       protected function cleanPathSlashes( $path ) {
+               return wfIsWindows() ? strtr( $path, '/', '\\' ) : $path;
+       }
+
        /**
         * Listen for E_WARNING errors and track whether any happen
         *
@@ -577,12 +747,38 @@ class FSFileBackend extends FileBackendStore {
                return array_pop( $this->hadWarningErrors ); // pop from stack
        }
 
+       /**
+        * @return bool
+        */
        private function handleWarning() {
                $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
                return true; // suppress from PHP handler
        }
 }
 
+/**
+ * @see FileBackendStoreOpHandle
+ */
+class FSFileOpHandle extends FileBackendStoreOpHandle {
+       public $cmd; // string; shell command
+       public $chmodPath; // string; file to chmod
+
+       /**
+        * @param $backend
+        * @param $params array
+        * @param $call
+        * @param $cmd
+        * @param $chmodPath null
+        */
+       public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) {
+               $this->backend = $backend;
+               $this->params = $params;
+               $this->call = $call;
+               $this->cmd = $cmd;
+               $this->chmodPath = $chmodPath;
+       }
+}
+
 /**
  * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
  * catches exception or does any custom behavoir that we may want.
@@ -600,6 +796,7 @@ abstract class FSFileBackendList implements Iterator {
 
        /**
         * @param $dir string file system directory
+        * @param $params array
         */
        public function __construct( $dir, array $params ) {
                $dir = realpath( $dir ); // normalize
@@ -625,21 +822,13 @@ abstract class FSFileBackendList implements Iterator {
                        return new DirectoryIterator( $dir );
                } else { // recursive
                        # Get an iterator that will return leaf nodes (non-directories)
-                       if ( MWInit::classExists( 'FilesystemIterator' ) ) { // PHP >= 5.3
-                               # RecursiveDirectoryIterator extends FilesystemIterator.
-                               # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
-                               $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
-                               return new RecursiveIteratorIterator(
-                                       new RecursiveDirectoryIterator( $dir, $flags ),
-                                       RecursiveIteratorIterator::CHILD_FIRST // include dirs
-                               );
-                       } else { // PHP < 5.3
-                               # RecursiveDirectoryIterator extends DirectoryIterator
-                               return new RecursiveIteratorIterator(
-                                       new RecursiveDirectoryIterator( $dir ),
-                                       RecursiveIteratorIterator::CHILD_FIRST // include dirs
-                               );
-                       }
+                       # RecursiveDirectoryIterator extends FilesystemIterator.
+                       # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
+                       $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
+                       return new RecursiveIteratorIterator(
+                               new RecursiveDirectoryIterator( $dir, $flags ),
+                               RecursiveIteratorIterator::CHILD_FIRST // include dirs
+                       );
                }
        }