get_cache_files_internal(dir, 1, progress_receiver, files);
}
+void
+for_each_level_1_subdir(const std::string& cache_dir,
+ const SubdirVisitor& subdir_visitor,
+ const ProgressReceiver& progress_receiver)
+{
+ for (int i = 0; i <= 0xF; i++) {
+ double progress = 1.0 * i / 16;
+ progress_receiver(progress);
+ std::string subdir_path = fmt::format("{}/{:x}", cache_dir, i);
+ subdir_visitor(subdir_path, [&](double inner_progress) {
+ progress_receiver(progress + inner_progress / 16);
+ });
+ }
+ progress_receiver(1.0);
+}
+
std::string
read_file(const std::string& path)
{
typedef std::function<void(double)> ProgressReceiver;
typedef std::function<void(std::shared_ptr<CacheFile>)> CacheFileVisitor;
+typedef std::function<void(const std::string& /*dir_path*/,
+ const ProgressReceiver& /*progress_receiver*/)>
+ SubdirVisitor;
+
// Get base name of path.
std::string base_name(const std::string& path);
// Return true if suffix is a suffix of string.
bool ends_with(const std::string& string, const std::string& suffix);
+// Call a function for each subdir (0-9a-f) in the cache.
+//
+// Parameters:
+// - cache_dir: Path to the cache directory.
+// - visitor: Function to call with directory path and progress_receiver as
+// arguments.
+// - progress_receiver: Function that will be called for progress updates.
+void for_each_level_1_subdir(const std::string& cache_dir,
+ const SubdirVisitor& visitor,
+ const ProgressReceiver& progress_receiver);
+
// Get a list of files in a level 1 subdirectory of the cache.
//
// The function works under the assumption that directory entries with one
CHECK_FALSE(util::ends_with("x", "xy"));
}
+TEST_CASE("util::for_each_level_1_subdir")
+{
+ std::vector<std::string> actual;
+ util::for_each_level_1_subdir(
+ "cache_dir",
+ [&](const std::string& subdir, const util::ProgressReceiver&) {
+ actual.push_back(subdir);
+ },
+ [](double) {});
+
+ std::vector<std::string> expected = {
+ "cache_dir/0",
+ "cache_dir/1",
+ "cache_dir/2",
+ "cache_dir/3",
+ "cache_dir/4",
+ "cache_dir/5",
+ "cache_dir/6",
+ "cache_dir/7",
+ "cache_dir/8",
+ "cache_dir/9",
+ "cache_dir/a",
+ "cache_dir/b",
+ "cache_dir/c",
+ "cache_dir/d",
+ "cache_dir/e",
+ "cache_dir/f",
+ };
+ CHECK(actual == expected);
+}
+
TEST_CASE("util::get_level_1_files")
{
util::create_dir("e/m/p/t/y");