r111574 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111573‎ | r111574 | r111575 >
Date:20:24, 15 February 2012
Author:aaron
Status:ok (Comments)
Tags:
Comment:
Delete the temp cdb file when an exception is thrown so they don't take up /tmp space
Modified paths:
  • /trunk/phase3/includes/Cdb_PHP.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Cdb_PHP.php
@@ -277,15 +277,14 @@
278278 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
279279 $this->handle = fopen( $this->tmpFileName, 'wb' );
280280 if ( !$this->handle ) {
281 - throw new MWException(
 281+ $this->throwException(
282282 'Unable to open CDB file "' . $this->tmpFileName . '" for write.' );
283283 }
284284 $this->hplist = array();
285285 $this->numentries = 0;
286286 $this->pos = 2048; // leaving space for the pointer array, 256 * 8
287287 if ( fseek( $this->handle, $this->pos ) == -1 ) {
288 - throw new MWException(
289 - 'fseek failed in file "' . $this->tmpFileName . '".' );
 288+ $this->throwException( 'fseek failed in file "' . $this->tmpFileName . '".' );
290289 }
291290 }
292291
@@ -323,7 +322,7 @@
324323 unlink( $this->realFileName );
325324 }
326325 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
327 - throw new MWException( 'Unable to move the new CDB file into place.' );
 326+ $this->throwException( 'Unable to move the new CDB file into place.' );
328327 }
329328 unset( $this->handle );
330329 }
@@ -335,7 +334,7 @@
336335 protected function write( $buf ) {
337336 $len = fwrite( $this->handle, $buf );
338337 if ( $len !== strlen( $buf ) ) {
339 - throw new MWException( 'Error writing to CDB file "'.$this->tmpFileName.'".' );
 338+ $this->throwException( 'Error writing to CDB file "'.$this->tmpFileName.'".' );
340339 }
341340 }
342341
@@ -346,7 +345,7 @@
347346 protected function posplus( $len ) {
348347 $newpos = $this->pos + $len;
349348 if ( $newpos > 0x7fffffff ) {
350 - throw new MWException(
 349+ $this->throwException(
351350 'A value in the CDB file "'.$this->tmpFileName.'" is too large.' );
352351 }
353352 $this->pos = $newpos;
@@ -376,10 +375,10 @@
377376 */
378377 protected function addbegin( $keylen, $datalen ) {
379378 if ( $keylen > 0x7fffffff ) {
380 - throw new MWException( 'Key length too long in file "'.$this->tmpFileName.'".' );
 379+ $this->throwException( 'Key length too long in file "'.$this->tmpFileName.'".' );
381380 }
382381 if ( $datalen > 0x7fffffff ) {
383 - throw new MWException( 'Data length too long in file "'.$this->tmpFileName.'".' );
 382+ $this->throwException( 'Data length too long in file "'.$this->tmpFileName.'".' );
384383 }
385384 $buf = pack( 'VV', $keylen, $datalen );
386385 $this->write( $buf );
@@ -454,8 +453,22 @@
455454 // Write the pointer array at the start of the file
456455 rewind( $this->handle );
457456 if ( ftell( $this->handle ) != 0 ) {
458 - throw new MWException( 'Error rewinding to start of file "'.$this->tmpFileName.'".' );
 457+ $this->throwException( 'Error rewinding to start of file "'.$this->tmpFileName.'".' );
459458 }
460459 $this->write( $final );
461460 }
 461+
 462+ /**
 463+ * Clean up the temp file and throw an exception
 464+ *
 465+ * @param $msg string
 466+ * @throws MWException
 467+ */
 468+ protected function throwException( $msg ) {
 469+ if ( $this->handle ) {
 470+ fclose( $this->handle );
 471+ unlink( $this->tmpFileName );
 472+ }
 473+ throw new MWException( $msg );
 474+ }
462475 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r111581MFT r111043, r111484, r111571, r111574, r111575reedy20:46, 15 February 2012
r111770MFT r111478, r111571, r111574, r111597, r111658reedy18:15, 17 February 2012

Comments

#Comment by Catrope (talk | contribs)   20:29, 15 February 2012
+			unlink( $this->tmpFileName );

Does unlink() gracefully handle nonexistent file names or null inputs?

#Comment by Reedy (talk | contribs)   20:33, 15 February 2012

No...

reedy@ubuntu64-web-esxi:~$ php test.php
PHP Warning:  unlink(foobarfile.php): No such file or directory in /home/reedy/test.php on line 3
PHP Stack trace:
PHP   1. {main}() /home/reedy/test.php:0
PHP   2. unlink() /home/reedy/test.php:3
#Comment by Catrope (talk | contribs)   20:40, 15 February 2012

Never mind, per IRC convo this doesn't matter since $this->handle refers to $this->tmpFileName (at least in the writer class), so if we have an open handle we can definitely delete the file.

Status & tagging log