From 603d8970c357523ae4cc8e2cdf3ca01b475e8346 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 21 Apr 2014 11:18:39 -0700 Subject: [PATCH] Made TempFSFile try to purge files on fatals too Change-Id: I3e80bea0d869be989c951dfb35a218bb66c9de17 --- includes/filebackend/TempFSFile.php | 37 +++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/includes/filebackend/TempFSFile.php b/includes/filebackend/TempFSFile.php index 611fab9afd..1b68130f2d 100644 --- a/includes/filebackend/TempFSFile.php +++ b/includes/filebackend/TempFSFile.php @@ -34,6 +34,18 @@ class TempFSFile extends FSFile { /** @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. @@ -62,7 +74,7 @@ class TempFSFile extends FSFile { } } $tmpFile = new self( $path ); - $tmpFile->canDelete = true; // safely instantiated + $tmpFile->autocollect(); // safely instantiated wfProfileOut( __METHOD__ ); return $tmpFile; @@ -79,6 +91,8 @@ class TempFSFile extends FSFile { $ok = unlink( $this->path ); wfRestoreWarnings(); + unset( self::$pathsCollect[$this->path] ); + return $ok; } @@ -108,6 +122,8 @@ class TempFSFile extends FSFile { public function preserve() { $this->canDelete = false; + unset( self::$pathsCollect[$this->path] ); + return $this; } @@ -119,17 +135,30 @@ 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(); } } } -- 2.20.1