Corrected shadow copy logic to not use stream_copy_to_stream() as it causes unexpected results.

This commit is contained in:
Netkas 2023-09-30 02:20:01 -04:00
parent 9ded98c3e1
commit 770dde9ed2
No known key found for this signature in database
GPG key ID: 5DAF58535614062B

View file

@ -692,19 +692,41 @@
public function saveCopy(string $path): void public function saveCopy(string $path): void
{ {
$destination = fopen($path, 'wb'); $destination = fopen($path, 'wb');
if ($destination === false) if ($destination === false)
{ {
throw new IOException(sprintf('Failed to open file \'%s\'', $path)); throw new IOException(sprintf('Failed to open file \'%s\'', $path));
} }
// Copy the package file to the destination fseek($this->package_file, $this->package_offset);
if(stream_copy_to_stream($this->package_file, $destination, $this->package_length, $this->package_offset) === false) $remaining_bytes = $this->package_length;
while ($remaining_bytes > 0)
{ {
throw new IOException(sprintf('Failed to copy package file to \'%s\'', $path)); $bytes_to_read = min($remaining_bytes, 4096);
$data = fread($this->package_file, $bytes_to_read);
if ($data === false)
{
throw new IOException('Failed to read from package file');
}
$written_bytes = fwrite($destination, $data, $bytes_to_read);
if ($written_bytes === false)
{
throw new IOException(sprintf('Failed to write to file \'%s\'', $path));
}
$remaining_bytes -= $written_bytes;
} }
// Done!
fclose($destination); fclose($destination);
if((new PackageReader($path))->getChecksum() !== $this->getChecksum())
{
throw new IOException(sprintf('Failed to save package copy to \'%s\', checksum mismatch', $path));
}
} }
/** /**