From f30b6fdf5c40a703b5a8d6fb2a21002377ec9067 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 6 Aug 2024 20:41:53 +0200 Subject: Reduce local-anime/local-manga disk access for speed up on mechanical harddrives, add local_anime.recursive option to make local-anime much faster when local-anime directory is a downloads directory --- src/Storage.cpp | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'src/Storage.cpp') diff --git a/src/Storage.cpp b/src/Storage.cpp index dc6b6a2..7bb3be6 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -249,7 +249,7 @@ namespace QuickMedia { for(auto &p : std::filesystem::directory_iterator(path.data)) { std::error_code ec; const FileType file_type = p.is_directory(ec) ? FileType::DIRECTORY : FileType::REGULAR; - if(!callback(p.path().string(), file_type)) + if(!callback(p.path().string(), file_type, 0)) break; } } catch(const std::filesystem::filesystem_error &err) { @@ -258,19 +258,13 @@ namespace QuickMedia { } } - static std::filesystem::file_time_type file_get_filetime_or(const std::filesystem::directory_entry &path, std::filesystem::file_time_type default_value) { - try { - return path.last_write_time(); - } catch(const std::filesystem::filesystem_error &err) { - return default_value; - } - } - void for_files_in_dir_sort_last_modified(const Path &path, FileIteratorCallback callback, FileSortDirection sort_dir) { - std::vector paths; + std::vector> paths_with_last_modified; try { for(auto &p : std::filesystem::directory_iterator(path.data)) { - paths.push_back(p); + time_t last_modified = 0; + file_get_last_modified_time_seconds(p.path().c_str(), &last_modified); + paths_with_last_modified.push_back(std::make_pair(p, last_modified)); } } catch(const std::filesystem::filesystem_error &err) { fprintf(stderr, "Failed to list files in directory %s, error: %s\n", path.data.c_str(), err.what()); @@ -278,19 +272,19 @@ namespace QuickMedia { } if(sort_dir == FileSortDirection::ASC) { - std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) > file_get_filetime_or(path2, std::filesystem::file_time_type::min()); + std::sort(paths_with_last_modified.begin(), paths_with_last_modified.end(), [](const auto &path1, const auto &path2) { + return path1.second > path2.second; }); } else { - std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) < file_get_filetime_or(path2, std::filesystem::file_time_type::min()); + std::sort(paths_with_last_modified.begin(), paths_with_last_modified.end(), [](const auto &path1, const auto &path2) { + return path1.second < path2.second; }); } - for(auto &p : paths) { + for(auto &p : paths_with_last_modified) { std::error_code ec; - const FileType file_type = p.is_directory(ec) ? FileType::DIRECTORY : FileType::REGULAR; - if(!callback(p.path().string(), file_type)) + const FileType file_type = p.first.is_directory(ec) ? FileType::DIRECTORY : FileType::REGULAR; + if(!callback(p.first.path().string(), file_type, p.second)) break; } } @@ -319,7 +313,7 @@ namespace QuickMedia { for(auto &p : paths) { std::error_code ec; const FileType file_type = p.is_directory(ec) ? FileType::DIRECTORY : FileType::REGULAR; - if(!callback(p.path().string(), file_type)) + if(!callback(p.path().string(), file_type, 0)) break; } } -- cgit v1.2.3