mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
gzip: Don't fail when trying to compress empty files
Given an empty file, gzip would try to create a zero-size memory mapping of that file, which would fail with EINVAL.
This commit is contained in:
parent
748218d5d0
commit
ba0d46e31a
1 changed files with 10 additions and 6 deletions
|
@ -43,14 +43,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
// We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression)
|
// We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression)
|
||||||
// TODO: automatically fallback to buffered streaming for very large files
|
// TODO: automatically fallback to buffered streaming for very large files
|
||||||
auto file = TRY(Core::MappedFile::map(input_filename));
|
RefPtr<Core::MappedFile> file;
|
||||||
|
ReadonlyBytes input_bytes;
|
||||||
|
if (TRY(Core::System::stat(input_filename)).st_size > 0) {
|
||||||
|
file = TRY(Core::MappedFile::map(input_filename));
|
||||||
|
input_bytes = file->bytes();
|
||||||
|
}
|
||||||
|
|
||||||
AK::Optional<ByteBuffer> output_bytes;
|
AK::Optional<ByteBuffer> output_bytes;
|
||||||
if (decompress) {
|
if (decompress)
|
||||||
output_bytes = Compress::GzipDecompressor::decompress_all(file->bytes());
|
output_bytes = Compress::GzipDecompressor::decompress_all(input_bytes);
|
||||||
} else {
|
else
|
||||||
output_bytes = Compress::GzipCompressor::compress_all(file->bytes());
|
output_bytes = Compress::GzipCompressor::compress_all(input_bytes);
|
||||||
}
|
|
||||||
|
|
||||||
if (!output_bytes.has_value()) {
|
if (!output_bytes.has_value()) {
|
||||||
warnln("Failed gzip {} input file", decompress ? "decompressing"sv : "compressing"sv);
|
warnln("Failed gzip {} input file", decompress ? "decompressing"sv : "compressing"sv);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue