Implement fs::get_cache_dir

Win32: equal to config dir for now
Linux: respect XDG_CACHE_HOME if specified
OSX: possibly incomplete
This commit is contained in:
Nekotekina
2018-12-24 18:47:46 +03:00
parent 20efed66e9
commit bd9131ae1c
15 changed files with 69 additions and 28 deletions
+42 -4
View File
@@ -1403,8 +1403,8 @@ const std::string& fs::get_config_dir()
if (const char* home = ::getenv("HOME"))
dir = home + "/Library/Application Support"s;
#else
if (const char* home = ::getenv("XDG_CONFIG_HOME"))
dir = home;
if (const char* conf = ::getenv("XDG_CONFIG_HOME"))
dir = conf;
else if (const char* home = ::getenv("HOME"))
dir = home + "/.config"s;
#endif
@@ -1425,15 +1425,53 @@ const std::string& fs::get_config_dir()
return s_dir;
}
const std::string& fs::get_cache_dir()
{
static const std::string s_dir = []
{
std::string dir;
#ifdef _WIN32
dir = get_config_dir();
#else
#ifdef __APPLE__
if (const char* home = ::getenv("HOME"))
dir = home + "/Library/Caches"s;
#else
if (const char* cache = ::getenv("XDG_CACHE_HOME"))
dir = cache;
else if (const char* conf = ::getenv("XDG_CONFIG_HOME"))
dir = conf;
else if (const char* home = ::getenv("HOME"))
dir = home + "/.cache"s;
#endif
else // Just in case
dir = "./cache";
dir += "/rpcs3/";
if (!create_path(dir))
{
std::printf("Failed to create configuration directory '%s' (%d).\n", dir.c_str(), errno);
}
#endif
return dir;
}();
return s_dir;
}
std::string fs::get_data_dir(const std::string& prefix, const std::string& location, const std::string& suffix)
{
static const std::string s_dir = []
{
const std::string dir = get_config_dir() + "data/";
const std::string dir = get_cache_dir() + "data/";
if (!create_path(dir))
{
return get_config_dir();
return get_cache_dir();
}
return dir;
+3
View File
@@ -484,6 +484,9 @@ namespace fs
// Get configuration directory
const std::string& get_config_dir();
// Get common cache directory
const std::string& get_cache_dir();
// Get data/cache directory for specified prefix and suffix
std::string get_data_dir(const std::string& prefix, const std::string& location, const std::string& suffix);
+6 -6
View File
@@ -322,8 +322,8 @@ void logs::message::broadcast(const char* fmt, const fmt_type_info* sup, ...) co
logs::file_writer::file_writer(const std::string& name)
: m_name(name)
{
const std::string log_name = fs::get_config_dir() + name + ".log";
const std::string buf_name = fs::get_config_dir() + name + ".buf";
const std::string log_name = fs::get_cache_dir() + name + ".log";
const std::string buf_name = fs::get_cache_dir() + name + ".buf";
try
{
@@ -352,7 +352,7 @@ logs::file_writer::file_writer(const std::string& name)
// Check free space
fs::device_stat stats{};
if (!fs::statfs(fs::get_config_dir(), stats) || stats.avail_free < s_log_size * 8)
if (!fs::statfs(fs::get_cache_dir(), stats) || stats.avail_free < s_log_size * 8)
{
fmt::throw_exception("Not enough free space (%f KB)", stats.avail_free / 1000000.);
}
@@ -372,9 +372,9 @@ logs::file_writer::file_writer(const std::string& name)
verify(name.c_str()), m_fptr;
// Rotate backups (TODO)
fs::remove_file(fs::get_config_dir() + name + "1.log.gz");
fs::create_dir(fs::get_config_dir() + "old_logs");
fs::rename(fs::get_config_dir() + m_name + ".log.gz", fs::get_config_dir() + "old_logs/" + m_name + ".log.gz", true);
fs::remove_file(fs::get_cache_dir() + name + "1.log.gz");
fs::create_dir(fs::get_cache_dir() + "old_logs");
fs::rename(fs::get_cache_dir() + m_name + ".log.gz", fs::get_cache_dir() + "old_logs/" + m_name + ".log.gz", true);
// Actual log file (allowed to fail)
m_fout.open(log_name, fs::rewrite);