Skip to content

Commit 6b1c174

Browse files
committed
Throw an exception when a file changes during reading
see discussion at #17
1 parent 7350294 commit 6b1c174

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/Tar.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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
/**

0 commit comments

Comments
 (0)