From: Joel Rosdahl Date: Thu, 27 Aug 2020 08:35:59 +0000 (+0200) Subject: Support passing level 0 to “ccache -X” to use default level X-Git-Tag: v4.0~162 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2bb90086cf43ce88c00bc87f36b7b3bc1f837277;p=thirdparty%2Fccache.git Support passing level 0 to “ccache -X” to use default level --- diff --git a/src/ZstdCompressor.cpp b/src/ZstdCompressor.cpp index e1fa46d0e..4e054942a 100644 --- a/src/ZstdCompressor.cpp +++ b/src/ZstdCompressor.cpp @@ -23,13 +23,11 @@ using Logging::log; -const uint8_t k_default_zstd_compression_level = 1; - ZstdCompressor::ZstdCompressor(FILE* stream, int8_t compression_level) : m_stream(stream), m_zstd_stream(ZSTD_createCStream()) { if (compression_level == 0) { - compression_level = k_default_zstd_compression_level; + compression_level = default_compression_level; log("Using default compression level {}", compression_level); } diff --git a/src/ZstdCompressor.hpp b/src/ZstdCompressor.hpp index 3c1203235..58ca45342 100644 --- a/src/ZstdCompressor.hpp +++ b/src/ZstdCompressor.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -40,6 +40,8 @@ public: void write(const void* data, size_t count) override; void finalize() override; + constexpr static uint8_t default_compression_level = 1; + private: FILE* m_stream; ZSTD_CStream* m_zstd_stream; diff --git a/src/ccache.cpp b/src/ccache.cpp index c59f3fc95..c9e8ec82e 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2411,22 +2411,21 @@ handle_main_options(int argc, const char* const* argv) case 'X': // --recompress { - int level; + optional wanted_level; if (arg == "uncompressed") { - level = 0; + wanted_level = nullopt; } else { - level = Util::parse_int(arg); + int level = Util::parse_int(arg); if (level < -128 || level > 127) { throw Error("compression level must be between -128 and 127"); } - if (level == 0) { - level = ctx.config.compression_level(); - } + wanted_level = level; } ProgressBar progress_bar("Recompressing..."); - compress_recompress( - ctx, level, [&](double progress) { progress_bar.update(progress); }); + compress_recompress(ctx, wanted_level, [&](double progress) { + progress_bar.update(progress); + }); break; } diff --git a/src/compress.cpp b/src/compress.cpp index ebf8105a8..d05127e72 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -27,6 +27,7 @@ #include "Result.hpp" #include "StdMakeUnique.hpp" #include "ThreadPool.hpp" +#include "ZstdCompressor.hpp" #include "manifest.hpp" #include "stats.hpp" @@ -36,6 +37,7 @@ #include using Logging::log; +using nonstd::optional; namespace { @@ -154,29 +156,31 @@ recompress_file(Context& ctx, RecompressionStatistics& statistics, const std::string& stats_file, const CacheFile& cache_file, - int8_t level) + optional level) { auto file = open_file(cache_file.path(), "rb"); auto reader = create_reader(cache_file, file.get()); - int8_t current_level = reader->compression_type() == Compression::Type::none - ? 0 - : reader->compression_level(); auto old_stat = Stat::stat(cache_file.path(), Stat::OnError::log); uint64_t content_size = reader->content_size(); + int8_t wanted_level = + level ? (*level == 0 ? ZstdCompressor::default_compression_level : *level) + : 0; - if (current_level == level) { + if (reader->compression_level() == wanted_level) { statistics.update(content_size, old_stat.size(), old_stat.size(), 0); return; } - log("Recompressing {} to level {}", cache_file.path(), level); + log("Recompressing {} to {}", + cache_file.path(), + level ? fmt::format("level {}", wanted_level) : "uncompressed"); AtomicFile atomic_new_file(cache_file.path(), AtomicFile::Mode::binary); - auto writer = create_writer(atomic_new_file.stream(), - *reader, - level == 0 ? Compression::Type::none - : Compression::Type::zstd, - level); + auto writer = + create_writer(atomic_new_file.stream(), + *reader, + level ? Compression::Type::zstd : Compression::Type::none, + wanted_level); char buffer[READ_BUFFER_SIZE]; size_t bytes_left = reader->payload_size(); @@ -277,7 +281,7 @@ compress_stats(const Config& config, void compress_recompress(Context& ctx, - int8_t level, + optional level, const Util::ProgressReceiver& progress_receiver) { const size_t threads = std::thread::hardware_concurrency(); diff --git a/src/compress.hpp b/src/compress.hpp index 645f62943..91eb04d1b 100644 --- a/src/compress.hpp +++ b/src/compress.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -22,7 +22,9 @@ #include "Util.hpp" -struct Confix; +#include "third_party/nonstd/optional.hpp" + +class Config; class Context; void compress_stats(const Config& config, @@ -32,9 +34,9 @@ void compress_stats(const Config& config, // // Arguments: // - ctx: The context. -// - level: Target compression level (positive or negative value), or 0 for no -// compression. +// - level: Target compression level (positive or negative value for actual +// level, 0 for default level and nonstd::nullopt for no compression). // - progress_receiver: Function that will be called for progress updates. void compress_recompress(Context& ctx, - int8_t level, + nonstd::optional level, const Util::ProgressReceiver& progress_receiver);