88#include " toml++/toml.hpp"
99#include " util.hpp"
1010
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)
11+ struct CdGuard
1712{
18- if (!path.empty ())
13+ fs::path saved;
14+ CdGuard (const fs::path& p) : saved(fs::current_path())
1915 {
20- old_pwd = fs::current_path ();
21- fs::current_path (path );
16+ if (!p. empty ())
17+ fs::current_path (p );
2218 }
23- }
24-
25- static void cd_back ()
26- {
27- if (!old_pwd.empty ())
28- fs::current_path (old_pwd);
29- }
19+ ~CdGuard () { fs::current_path (saved); }
20+ };
3021
3122// https://github.com/hyprwm/Hyprland/blob/2d2a5bebff72c73cd27db3b9e954b8fa2a7623e8/hyprpm/src/core/DataState.cpp#L24
3223static bool write_cache (const std::string& str, const std::string& to)
3324{
3425 // 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);
26+ const fs::path temp_state = (fs::temp_directory_path () / " .temp-state" );
27+ std::ofstream of (temp_state, std::ios::trunc);
3728 if (!of.good ())
3829 return false ;
3930
@@ -52,12 +43,12 @@ Cache::Cache(const std::string& cache_dir) : m_cache_dir_path(cache_dir)
5243 }
5344
5445 for (int i = 0 ; i < int (idx (CacheFilesEnum::COUNT)); ++i)
55- LoadCacheFile (g_cache_files [enum_<CacheFilesEnum>(i)]);
46+ LoadCacheFile (m_cache_entries [enum_<CacheFilesEnum>(i)]);
5647}
5748
5849Cache::~Cache ()
5950{
60- for (const auto & [_, cache] : g_cache_files )
51+ for (const auto & [_, cache] : m_cache_entries )
6152 GenerateCacheFile (cache);
6253}
6354
@@ -66,7 +57,7 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
6657 // Since the filename (default.theme-file) will be likely
6758 // related to relative path of the config directory, let's
6859 // snapshot and switch to that directory.
69- cd (m_cache_dir_path);
60+ CdGuard guard (m_cache_dir_path);
7061
7162 if (fs::exists (entry.file_path ))
7263 {
@@ -76,8 +67,6 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
7667 }
7768 catch (const toml::parse_error& err)
7869 {
79- // Snap back
80- cd_back ();
8170 return Err (
8271 fmt::format (" Parsing cache file '{}' failed:\n "
8372 " {}\n "
@@ -89,13 +78,12 @@ Result<> Cache::LoadCacheFile(cache_entry_t& entry)
8978 }
9079 }
9180
92- cd_back ();
9381 return Ok ();
9482}
9583
9684void Cache::GenerateCacheFile (const cache_entry_t & entry)
9785{
98- cd (m_cache_dir_path);
86+ CdGuard guard (m_cache_dir_path);
9987
10088 std::stringstream ss;
10189 ss << " # AUTO-GENERATED FILE. DO NOT EDIT THIS FILE.\n " ;
@@ -104,5 +92,4 @@ void Cache::GenerateCacheFile(const cache_entry_t& entry)
10492
10593 if (!write_cache (ss.str (), entry.file_path ))
10694 error (" Failed to write cache entry at path '{}'" , entry.file_path );
107- cd_back ();
10895}
0 commit comments