File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -230,9 +230,10 @@ public function create($file = '')
230230 /**
231231 * Add a file to the current TAR archive using an existing file in the filesystem
232232 *
233- * @param string $file path to the original file
233+ * @param string $file path to the original file
234234 * @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
235- * @throws ArchiveIOException
235+ * @throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted
236+ * @throws ArchiveIOException there was trouble reading the given file, it was not added
236237 */
237238 public function addFile ($ file , $ fileinfo = '' )
238239 {
@@ -253,8 +254,10 @@ public function addFile($file, $fileinfo = '')
253254 $ this ->writeFileHeader ($ fileinfo );
254255
255256 // write data
257+ $ read = 0 ;
256258 while (!feof ($ fp )) {
257259 $ data = fread ($ fp , 512 );
260+ $ read += strlen ($ data );
258261 if ($ data === false ) {
259262 break ;
260263 }
@@ -265,6 +268,11 @@ public function addFile($file, $fileinfo = '')
265268 $ this ->writebytes ($ packed );
266269 }
267270 fclose ($ fp );
271+
272+ if ($ read != $ fileinfo ->getSize ()) {
273+ $ this ->close ();
274+ throw new ArchiveCorruptedException ("The size of $ file changed while reading, archive corrupted. read $ read expected " .$ fileinfo ->getSize ());
275+ }
268276 }
269277
270278 /**
You can’t perform that action at this time.
0 commit comments