From 770dde9ed2ae843aec3a9fac8acd5debf089e74f Mon Sep 17 00:00:00 2001 From: Netkas Date: Sat, 30 Sep 2023 02:20:01 -0400 Subject: [PATCH] Corrected shadow copy logic to not use stream_copy_to_stream() as it causes unexpected results. --- src/ncc/Classes/PackageReader.php | 32 ++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/ncc/Classes/PackageReader.php b/src/ncc/Classes/PackageReader.php index 7e0f1ca..eab31ca 100644 --- a/src/ncc/Classes/PackageReader.php +++ b/src/ncc/Classes/PackageReader.php @@ -692,19 +692,41 @@ public function saveCopy(string $path): void { $destination = fopen($path, 'wb'); - if($destination === false) + + if ($destination === false) { throw new IOException(sprintf('Failed to open file \'%s\'', $path)); } - // Copy the package file to the destination - if(stream_copy_to_stream($this->package_file, $destination, $this->package_length, $this->package_offset) === false) + fseek($this->package_file, $this->package_offset); + $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); + + if((new PackageReader($path))->getChecksum() !== $this->getChecksum()) + { + throw new IOException(sprintf('Failed to save package copy to \'%s\', checksum mismatch', $path)); + } } /**