From: Joel Rosdahl Date: Sun, 26 Jul 2020 18:20:37 +0000 (+0200) Subject: Keep argument lists and function implementations sorted X-Git-Tag: v4.0~288 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=347d2f2db8d7d776597fb34db5ae1c05819c0020;p=thirdparty%2Fccache.git Keep argument lists and function implementations sorted --- diff --git a/src/Util.cpp b/src/Util.cpp index 05403aa46..774cfe8f4 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -654,6 +654,40 @@ normalize_absolute_path(string_view path) #endif } +unsigned +parse_duration_with_suffix_to_seconds(const std::string& value) +{ + size_t end; + long result; + bool failed = false; + + try { + result = std::stol(value, &end, 10); + } catch (std::exception&) { + failed = true; + } + + if (failed || result < 0) { + throw Error(fmt::format("invalid unsigned integer: \"{}\"", value)); + } + + if (end + 1 != value.size()) { + throw Error( + fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); + } + + switch (value[end]) { + case 'd': + result *= 24 * 3600; + case 's': + break; + default: + throw Error( + fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); + } + return result; +} + int parse_int(const std::string& value) { @@ -998,37 +1032,4 @@ write_file(const std::string& path, file << data; } -unsigned -parse_duration_with_suffix_to_seconds(const std::string& value) -{ - size_t end; - long result; - bool failed = false; - - try { - result = std::stol(value, &end, 10); - } catch (std::exception&) { - failed = true; - } - - if (failed || result < 0) { - throw Error(fmt::format("invalid unsigned integer: \"{}\"", value)); - } - - if (end + 1 != value.size()) { - throw Error( - fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); - } - - switch (value[end]) { - case 'd': - result *= 24 * 3600; - case 's': - break; - default: - throw Error( - fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); - } - return result; -} } // namespace Util diff --git a/src/Util.hpp b/src/Util.hpp index f3c23f043..3f3c519fd 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -260,6 +260,13 @@ bool matches_dir_prefix_or_file(nonstd::string_view dir_prefix_or_file, // On Windows: Backslashes are replaced with forward slashes. std::string normalize_absolute_path(nonstd::string_view path); +// Parse the given string into an unsigned integer. Then based on suffix +// provided convert the number to seconds possible suffixes = d(ays)/s(econds) +// +// Throws `Error` for any other suffix +// Throws `Error` if parse value is <0 +unsigned parse_duration_with_suffix_to_seconds(const std::string& value); + // Parse a string into an integer. // // Throws Error on error. @@ -353,11 +360,4 @@ void write_file(const std::string& path, const std::string& data, std::ios_base::openmode open_mode = std::ios::binary); -// Parse the given string into an unsigned integer. Then based on suffix -// provided convert the number to seconds possible suffixes = d(ays)/s(econds) -// -// Throws `Error` for any other suffix -// Throws `Error` if parse value is <0 -unsigned parse_duration_with_suffix_to_seconds(const std::string& value); - } // namespace Util diff --git a/src/ccache.cpp b/src/ccache.cpp index 7e23c5201..7584d1e33 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2200,8 +2200,8 @@ handle_main_options(int argc, const char* const* argv) enum longopts { DUMP_MANIFEST, DUMP_RESULT, - EXTRACT_RESULT, EVICT_OLDER_THAN, + EXTRACT_RESULT, HASH_FILE, PRINT_STATS, }; @@ -2210,8 +2210,8 @@ handle_main_options(int argc, const char* const* argv) {"clear", no_argument, nullptr, 'C'}, {"dump-manifest", required_argument, nullptr, DUMP_MANIFEST}, {"dump-result", required_argument, nullptr, DUMP_RESULT}, - {"extract-result", required_argument, nullptr, EXTRACT_RESULT}, {"evict-older-than", required_argument, nullptr, EVICT_OLDER_THAN}, + {"extract-result", required_argument, nullptr, EXTRACT_RESULT}, {"get-config", required_argument, nullptr, 'k'}, {"hash-file", required_argument, nullptr, HASH_FILE}, {"help", no_argument, nullptr, 'h'}, @@ -2251,16 +2251,6 @@ handle_main_options(int argc, const char* const* argv) return error ? EXIT_FAILURE : EXIT_SUCCESS; } - case EXTRACT_RESULT: { - ResultExtractor result_extractor("."); - Result::Reader result_reader(optarg); - auto error = result_reader.read(result_extractor); - if (error) { - fmt::print(stderr, "Error: {}\n", *error); - } - return error ? EXIT_FAILURE : EXIT_SUCCESS; - } - case EVICT_OLDER_THAN: { unsigned seconds = Util::parse_duration_with_suffix_to_seconds(optarg); ProgressBar progress_bar("Clearing ..."); @@ -2272,6 +2262,16 @@ handle_main_options(int argc, const char* const* argv) break; } + case EXTRACT_RESULT: { + ResultExtractor result_extractor("."); + Result::Reader result_reader(optarg); + auto error = result_reader.read(result_extractor); + if (error) { + fmt::print(stderr, "Error: {}\n", *error); + } + return error ? EXIT_FAILURE : EXIT_SUCCESS; + } + case HASH_FILE: { Hash hash; if (str_eq(optarg, "-")) { diff --git a/src/cleanup.cpp b/src/cleanup.cpp index 4b83ae1e6..5f5e9ab50 100644 --- a/src/cleanup.cpp +++ b/src/cleanup.cpp @@ -52,6 +52,20 @@ delete_file(const std::string& path, } } +void +clean_old(const Context& ctx, + const Util::ProgressReceiver& progress_receiver, + time_t max_age) +{ + Util::for_each_level_1_subdir( + ctx.config.cache_dir(), + [&](const std::string& subdir, + const Util::ProgressReceiver& sub_progress_receiver) { + clean_up_dir(subdir, 0, 0, max_age, sub_progress_receiver); + }, + progress_receiver); +} + // Clean up one cache subdirectory. void clean_up_dir(const std::string& subdir, @@ -205,17 +219,3 @@ wipe_all(const Context& ctx, const Util::ProgressReceiver& progress_receiver) ctx.inode_cache.drop(); #endif } - -void -clean_old(const Context& ctx, - const Util::ProgressReceiver& progress_receiver, - time_t max_age) -{ - Util::for_each_level_1_subdir( - ctx.config.cache_dir(), - [&](const std::string& subdir, - const Util::ProgressReceiver& sub_progress_receiver) { - clean_up_dir(subdir, 0, 0, max_age, sub_progress_receiver); - }, - progress_receiver); -} diff --git a/src/cleanup.hpp b/src/cleanup.hpp index 5148bf91e..750bcd47c 100644 --- a/src/cleanup.hpp +++ b/src/cleanup.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. // @@ -27,6 +27,10 @@ class Config; class Context; +void clean_old(const Context& ctx, + const Util::ProgressReceiver& progress_receiver, + time_t max_age); + void clean_up_dir(const std::string& subdir, uint64_t max_size, uint32_t max_files, @@ -38,7 +42,3 @@ void clean_up_all(const Config& config, void wipe_all(const Context& ctx, const Util::ProgressReceiver& progress_receiver); - -void clean_old(const Context& ctx, - const Util::ProgressReceiver& progress_receiver, - time_t max_age); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index a1644e01c..f299bad52 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -498,6 +498,20 @@ TEST_CASE("Util::normalize_absolute_path") #endif } +TEST_CASE("Util::parse_duration_with_suffix_to_seconds") +{ + CHECK(Util::parse_duration_with_suffix_to_seconds("0s") == 0); + CHECK(Util::parse_duration_with_suffix_to_seconds("2s") == 2); + CHECK(Util::parse_duration_with_suffix_to_seconds("1d") == 3600 * 24); + CHECK(Util::parse_duration_with_suffix_to_seconds("2d") == 2 * 3600 * 24); + CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("-2"), + "invalid unsigned integer: \"-2\""); + CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2x"), + "Invalid suffix, Supported: d(ay)/s(econd): \"2x\""); + CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2"), + "Invalid suffix, Supported: d(ay)/s(econd): \"2\""); +} + TEST_CASE("Util::parse_int") { CHECK(Util::parse_int("0") == 0); @@ -792,18 +806,4 @@ TEST_CASE("Util::wipe_path") } } -TEST_CASE("Util::parse_duration_with_suffix_to_seconds") -{ - CHECK(Util::parse_duration_with_suffix_to_seconds("0s") == 0); - CHECK(Util::parse_duration_with_suffix_to_seconds("2s") == 2); - CHECK(Util::parse_duration_with_suffix_to_seconds("1d") == 3600 * 24); - CHECK(Util::parse_duration_with_suffix_to_seconds("2d") == 2 * 3600 * 24); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("-2"), - "invalid unsigned integer: \"-2\""); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2x"), - "Invalid suffix, Supported: d(ay)/s(econd): \"2x\""); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2"), - "Invalid suffix, Supported: d(ay)/s(econd): \"2\""); -} - TEST_SUITE_END();