From 0b8c4ebfa873311350be7b329e06d3014b4661c7 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Tue, 8 Nov 2022 10:17:26 -0800 Subject: [PATCH] Store lz4 blocks as uncompressed if compression makes data larger This will change cow size estimation. But since we haven't put lz4 on SAC website yet, we can still change it. Bug: 228478555 Test: th Change-Id: Ifba7107954622ea04cbd25115700dfba50cec9c6 --- fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp | 8 +++++++- .../libsnapshot/libsnapshot_cow/cow_decompress.cpp | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp index e58f45ac7..0eb231bdf 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp +++ b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp @@ -84,7 +84,13 @@ std::basic_string CowWriter::Compress(const void* data, size_t length) << ", compression bound: " << bound << ", ret: " << compressed_size; return {}; } - buffer.resize(compressed_size); + // Don't run compression if the compressed output is larger + if (compressed_size >= length) { + buffer.resize(length); + memcpy(buffer.data(), data, length); + } else { + buffer.resize(compressed_size); + } return buffer; } default: diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/cow_decompress.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/cow_decompress.cpp index a4d22773a..139a29f22 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/cow_decompress.cpp +++ b/fs_mgr/libsnapshot/libsnapshot_cow/cow_decompress.cpp @@ -273,6 +273,18 @@ class Lz4Decompressor final : public IDecompressor { << actual_buffer_size << " bytes"; return false; } + // If input size is same as output size, then input is uncompressed. + if (stream_->Size() == output_size) { + size_t bytes_read = 0; + stream_->Read(output_buffer, output_size, &bytes_read); + if (bytes_read != output_size) { + LOG(ERROR) << "Failed to read all input at once. Expected: " << output_size + << " actual: " << bytes_read; + return false; + } + sink_->ReturnData(output_buffer, output_size); + return true; + } std::string input_buffer; input_buffer.resize(stream_->Size()); size_t bytes_read = 0;