Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFileBackend.php
index d9a9c59..8157916 100644 (file)
@@ -78,6 +78,8 @@ class FSFileBackend extends FileBackendStore {
 
        /**
         * @see FileBackendStore::resolveContainerPath()
+        * @param $container string
+        * @param $relStoragePath string
         * @return null|string
         */
        protected function resolveContainerPath( $container, $relStoragePath ) {
@@ -191,22 +193,39 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $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." );
+               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;
                        }
-                       $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
@@ -239,22 +258,39 @@ class FSFileBackend extends FileBackendStore {
                        }
                }
 
-               $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." );
+               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;
                        }
-                       $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
@@ -290,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
@@ -320,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
@@ -355,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
@@ -569,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
         *
@@ -583,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
         *
@@ -604,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.
@@ -627,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