diff options
Diffstat (limited to 'includes/filebackend/TempFSFile.php')
-rw-r--r-- | includes/filebackend/TempFSFile.php | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/includes/filebackend/TempFSFile.php b/includes/filebackend/TempFSFile.php index 8266e420..1b68130f 100644 --- a/includes/filebackend/TempFSFile.php +++ b/includes/filebackend/TempFSFile.php @@ -28,11 +28,24 @@ * @ingroup FileBackend */ class TempFSFile extends FSFile { - protected $canDelete = false; // bool; garbage collect the temp file + /** @var bool Garbage collect the temp file */ + protected $canDelete = false; - /** @var Array of active temp files to purge on shutdown */ + /** @var array Active temp files to purge on shutdown */ protected static $instances = array(); + /** @var array Map of (path => 1) for paths to delete on shutdown */ + protected static $pathsCollect = null; + + public function __construct( $path ) { + parent::__construct( $path ); + + if ( self::$pathsCollect === null ) { + self::$pathsCollect = array(); + register_shutdown_function( array( __CLASS__, 'purgeAllOnShutdown' ) ); + } + } + /** * Make a new temporary file on the file system. * Temporary files may be purged when the file object falls out of scope. @@ -56,12 +69,14 @@ class TempFSFile extends FSFile { } if ( $attempt >= 5 ) { wfProfileOut( __METHOD__ ); + return null; // give up } } $tmpFile = new self( $path ); - $tmpFile->canDelete = true; // safely instantiated + $tmpFile->autocollect(); // safely instantiated wfProfileOut( __METHOD__ ); + return $tmpFile; } @@ -75,13 +90,16 @@ class TempFSFile extends FSFile { wfSuppressWarnings(); $ok = unlink( $this->path ); wfRestoreWarnings(); + + unset( self::$pathsCollect[$this->path] ); + return $ok; } /** * Clean up the temporary file only after an object goes out of scope * - * @param Object $object + * @param stdClass $object * @return TempFSFile This object */ public function bind( $object ) { @@ -92,6 +110,7 @@ class TempFSFile extends FSFile { } $object->tempFSFileReferences[] = $this; } + return $this; } @@ -102,6 +121,9 @@ class TempFSFile extends FSFile { */ public function preserve() { $this->canDelete = false; + + unset( self::$pathsCollect[$this->path] ); + return $this; } @@ -112,17 +134,31 @@ class TempFSFile extends FSFile { */ public function autocollect() { $this->canDelete = true; + + self::$pathsCollect[$this->path] = 1; + return $this; } /** + * Try to make sure that all files are purged on error + * + * This method should only be called internally + */ + public static function purgeAllOnShutdown() { + foreach ( self::$pathsCollect as $path ) { + wfSuppressWarnings(); + unlink( $path ); + wfRestoreWarnings(); + } + } + + /** * Cleans up after the temporary file by deleting it */ function __destruct() { if ( $this->canDelete ) { - wfSuppressWarnings(); - unlink( $this->path ); - wfRestoreWarnings(); + $this->purge(); } } } |