From 6b1c1746fa0a6f9f68f0bc832892ddeda8db905c Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 19 Mar 2017 09:46:40 +0100 Subject: [PATCH] Throw an exception when a file changes during reading see discussion at #17 --- src/Tar.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Tar.php b/src/Tar.php index 56e8a1f..f9d7bfb 100644 --- a/src/Tar.php +++ b/src/Tar.php @@ -230,9 +230,10 @@ class Tar extends Archive /** * Add a file to the current TAR archive using an existing file in the filesystem * - * @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 - * @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 = '') { @@ -253,8 +254,10 @@ class Tar extends Archive $this->writeFileHeader($fileinfo); // write data + $read = 0; while (!feof($fp)) { $data = fread($fp, 512); + $read += strlen($data); if ($data === false) { break; } @@ -265,6 +268,11 @@ class Tar extends Archive $this->writebytes($packed); } 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()); + } } /**