Index: trunk/phase3/includes/Cdb_PHP.php |
— | — | @@ -277,15 +277,14 @@ |
278 | 278 | $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); |
279 | 279 | $this->handle = fopen( $this->tmpFileName, 'wb' ); |
280 | 280 | if ( !$this->handle ) { |
281 | | - throw new MWException( |
| 281 | + $this->throwException( |
282 | 282 | 'Unable to open CDB file "' . $this->tmpFileName . '" for write.' ); |
283 | 283 | } |
284 | 284 | $this->hplist = array(); |
285 | 285 | $this->numentries = 0; |
286 | 286 | $this->pos = 2048; // leaving space for the pointer array, 256 * 8 |
287 | 287 | 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 . '".' ); |
290 | 289 | } |
291 | 290 | } |
292 | 291 | |
— | — | @@ -323,7 +322,7 @@ |
324 | 323 | unlink( $this->realFileName ); |
325 | 324 | } |
326 | 325 | 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.' ); |
328 | 327 | } |
329 | 328 | unset( $this->handle ); |
330 | 329 | } |
— | — | @@ -335,7 +334,7 @@ |
336 | 335 | protected function write( $buf ) { |
337 | 336 | $len = fwrite( $this->handle, $buf ); |
338 | 337 | 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.'".' ); |
340 | 339 | } |
341 | 340 | } |
342 | 341 | |
— | — | @@ -346,7 +345,7 @@ |
347 | 346 | protected function posplus( $len ) { |
348 | 347 | $newpos = $this->pos + $len; |
349 | 348 | if ( $newpos > 0x7fffffff ) { |
350 | | - throw new MWException( |
| 349 | + $this->throwException( |
351 | 350 | 'A value in the CDB file "'.$this->tmpFileName.'" is too large.' ); |
352 | 351 | } |
353 | 352 | $this->pos = $newpos; |
— | — | @@ -376,10 +375,10 @@ |
377 | 376 | */ |
378 | 377 | protected function addbegin( $keylen, $datalen ) { |
379 | 378 | 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.'".' ); |
381 | 380 | } |
382 | 381 | 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.'".' ); |
384 | 383 | } |
385 | 384 | $buf = pack( 'VV', $keylen, $datalen ); |
386 | 385 | $this->write( $buf ); |
— | — | @@ -454,8 +453,22 @@ |
455 | 454 | // Write the pointer array at the start of the file |
456 | 455 | rewind( $this->handle ); |
457 | 456 | 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.'".' ); |
459 | 458 | } |
460 | 459 | $this->write( $final ); |
461 | 460 | } |
| 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 | + } |
462 | 475 | } |