#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)
{
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
// 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.
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
enum longopts {
DUMP_MANIFEST,
DUMP_RESULT,
- EXTRACT_RESULT,
EVICT_OLDER_THAN,
+ EXTRACT_RESULT,
HASH_FILE,
PRINT_STATS,
};
{"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'},
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 ...");
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, "-")) {
}
}
+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,
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);
-}
-// 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.
//
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,
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);
#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);
}
}
-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();