|
| 1 | +#include "cache.hpp" |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <fstream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include "fmt/format.h" |
| 8 | +#include "toml++/toml.hpp" |
| 9 | +#include "util.hpp" |
| 10 | + |
| 11 | +std::unordered_map<CacheFilesEnum, cache_entry_t> g_cache_files = { |
| 12 | + { CacheFilesEnum::Colors, { "colors.toml", "cache.default-color-picker-color" } } |
| 13 | +}; |
| 14 | + |
| 15 | +static fs::path old_pwd; |
| 16 | +static void cd(const fs::path& path) |
| 17 | +{ |
| 18 | + if (!path.empty()) |
| 19 | + { |
| 20 | + old_pwd = fs::current_path(); |
| 21 | + fs::current_path(path); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +static void cd_back() |
| 26 | +{ |
| 27 | + if (!old_pwd.empty()) |
| 28 | + fs::current_path(old_pwd); |
| 29 | +} |
| 30 | + |
| 31 | +// https://github.com/hyprwm/Hyprland/blob/2d2a5bebff72c73cd27db3b9e954b8fa2a7623e8/hyprpm/src/core/DataState.cpp#L24 |
| 32 | +static bool write_cache(const std::string& str, const std::string& to) |
| 33 | +{ |
| 34 | + // create temp file in a safe temp root |
| 35 | + const fs::path& temp_state = (fs::temp_directory_path() / ".temp-state"); |
| 36 | + std::ofstream of(temp_state, std::ios::trunc); |
| 37 | + if (!of.good()) |
| 38 | + return false; |
| 39 | + |
| 40 | + of << str; |
| 41 | + of.close(); |
| 42 | + |
| 43 | + return fs::copy_file(temp_state, to, fs::copy_options::overwrite_existing); |
| 44 | +} |
| 45 | + |
| 46 | +Cache::Cache(const std::string& cache_dir) : m_cache_dir_path(cache_dir) |
| 47 | +{ |
| 48 | + if (!fs::exists(cache_dir)) |
| 49 | + { |
| 50 | + warn("oshot cache folder was not found, Creating folders at {}!", cache_dir); |
| 51 | + fs::create_directories(cache_dir); |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 0; i < int(idx(CacheFilesEnum::COUNT)); ++i) |
| 55 | + LoadCacheFile(g_cache_files[enum_<CacheFilesEnum>(i)]); |
| 56 | +} |
| 57 | + |
| 58 | +Cache::~Cache() |
| 59 | +{ |
| 60 | + for (const auto& [_, cache] : g_cache_files) |
| 61 | + GenerateCacheFile(cache); |
| 62 | +} |
| 63 | + |
| 64 | +Result<> Cache::LoadCacheFile(cache_entry_t& entry) |
| 65 | +{ |
| 66 | + // Since the filename (default.theme-file) will be likely |
| 67 | + // related to relative path of the config directory, let's |
| 68 | + // snapshot and switch to that directory. |
| 69 | + cd(m_cache_dir_path); |
| 70 | + |
| 71 | + if (fs::exists(entry.file_path)) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + entry.tbl = toml::parse_file(entry.file_path); |
| 76 | + } |
| 77 | + catch (const toml::parse_error& err) |
| 78 | + { |
| 79 | + // Snap back |
| 80 | + cd_back(); |
| 81 | + return Err( |
| 82 | + fmt::format("Parsing cache file '{}' failed:\n" |
| 83 | + "{}\n" |
| 84 | + "\t(error occurred at line {} column {})", |
| 85 | + entry.file_path, |
| 86 | + err.description(), |
| 87 | + err.source().begin.line, |
| 88 | + err.source().begin.column)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + cd_back(); |
| 93 | + return Ok(); |
| 94 | +} |
| 95 | + |
| 96 | +void Cache::GenerateCacheFile(const cache_entry_t& entry) |
| 97 | +{ |
| 98 | + cd(m_cache_dir_path); |
| 99 | + |
| 100 | + std::stringstream ss; |
| 101 | + ss << "# AUTO-GENERATED FILE. DO NOT EDIT THIS FILE.\n"; |
| 102 | + ss << "# YOU GONNA MESS SHIT UP. unless you know what you doing ofc\n"; |
| 103 | + ss << entry.tbl; |
| 104 | + |
| 105 | + if (!write_cache(ss.str(), entry.file_path)) |
| 106 | + error("Failed to write cache entry at path '{}'", entry.file_path); |
| 107 | + cd_back(); |
| 108 | +} |
0 commit comments