1
0
mirror of https://github.com/splitbrain/php-archive.git synced 2025-07-16 12:26:28 +02:00

Throw an exception when a file changes during reading

see discussion at #17
This commit is contained in:
Andreas Gohr
2017-03-19 09:46:40 +01:00
parent 735029407f
commit 6b1c1746fa

View File

@ -232,7 +232,8 @@ class Tar extends Archive
* *
* @param string $file path to the original file * @param string $file path to the original file
* @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
* @throws ArchiveIOException * @throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted
* @throws ArchiveIOException there was trouble reading the given file, it was not added
*/ */
public function addFile($file, $fileinfo = '') public function addFile($file, $fileinfo = '')
{ {
@ -253,8 +254,10 @@ class Tar extends Archive
$this->writeFileHeader($fileinfo); $this->writeFileHeader($fileinfo);
// write data // write data
$read = 0;
while (!feof($fp)) { while (!feof($fp)) {
$data = fread($fp, 512); $data = fread($fp, 512);
$read += strlen($data);
if ($data === false) { if ($data === false) {
break; break;
} }
@ -265,6 +268,11 @@ class Tar extends Archive
$this->writebytes($packed); $this->writebytes($packed);
} }
fclose($fp); fclose($fp);
if($read != $fileinfo->getSize()) {
$this->close();
throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
}
} }
/** /**