2022-12-31 08:35:58 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-01-04 16:40:35 +00:00
|
|
|
#include <sys/stat.h>
|
2022-12-31 08:35:58 +00:00
|
|
|
#include <string>
|
2023-01-31 14:40:04 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2022-12-31 08:35:58 +00:00
|
|
|
|
2023-01-05 10:27:14 +00:00
|
|
|
#include "log.h"
|
2023-01-16 04:05:25 +00:00
|
|
|
#include "misc.h"
|
2022-12-31 08:35:58 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) {
|
|
|
|
FILE* file = fopen(path, mode);
|
2023-01-17 15:35:08 +00:00
|
|
|
if (!file && !(ignore_enoent && errno == ENOENT)) {
|
|
|
|
throw_system_error(std::string("fopen(") + quote(path) + ')');
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
return file;
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fclose_and_log(FILE* file) {
|
|
|
|
if (!fclose(file)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
log(std::string("Failed to close a file: fclose(): ") + strerror(errno));
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string get_config_folder() {
|
2022-12-31 08:35:58 +00:00
|
|
|
const char* path;
|
|
|
|
|
|
|
|
path = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (path) {
|
2023-01-04 16:40:35 +00:00
|
|
|
return std::string(path) + "/logmeow";
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
path = getenv("HOME");
|
|
|
|
if (path) {
|
2023-01-04 16:40:35 +00:00
|
|
|
return std::string(path) + "/.config/logmeow";
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("cannot find suitable config folder, please set XDG_CONFIG_HOME or HOME");
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
std::string get_config_file_path() {
|
2023-01-31 14:40:04 +00:00
|
|
|
return get_config_folder() + "/config.json";
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tries to create a directory with an empty string if it's just "/" but who cares
|
|
|
|
void create_config_folders_if_necessary() {
|
2023-01-05 10:27:14 +00:00
|
|
|
std::string full_path = get_config_folder();
|
|
|
|
std::string path;
|
2023-01-04 16:40:35 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
while (pos != std::string::npos) {
|
2023-01-05 10:27:14 +00:00
|
|
|
pos = full_path.find('/', pos);
|
2023-01-04 16:40:35 +00:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
pos++;
|
|
|
|
}
|
2023-01-05 10:27:14 +00:00
|
|
|
path = full_path.substr(0, pos);
|
|
|
|
if (!mkdir(path.c_str(), 0600)) {
|
2023-01-04 16:40:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
throw_system_error(std::string("mkdir(") + quote(path) + ')');
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-31 14:40:04 +00:00
|
|
|
void from_json(const nlohmann::json& j, Config& config) {
|
|
|
|
j.at("logcat_command").get_to(config.logcat_command);
|
|
|
|
j.at("normal_font_size").get_to(config.normal_font_size);
|
|
|
|
j.at("monospace_font_size").get_to(config.monospace_font_size);
|
|
|
|
j.at("filters").get_to(config.filters);
|
2023-02-01 15:22:08 +00:00
|
|
|
j.at("exclusions").get_to(config.exclusions);
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
Config load_config() {
|
|
|
|
std::string config_file_path = get_config_file_path();
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(config_file_path.c_str(), "r", true), fclose_and_log);
|
|
|
|
|
|
|
|
if (!config_file) {
|
|
|
|
return Config();
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:40:04 +00:00
|
|
|
return nlohmann::json::parse(config_file.get()).get<Config>();
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-31 14:40:04 +00:00
|
|
|
void to_json(nlohmann::json& j, const Config& config) {
|
|
|
|
j["logcat_command"] = config.logcat_command;
|
|
|
|
j["normal_font_size"] = config.normal_font_size;
|
|
|
|
j["monospace_font_size"] = config.monospace_font_size;
|
|
|
|
j["filters"] = config.filters;
|
2023-02-01 15:22:08 +00:00
|
|
|
j["exclusions"] = config.exclusions;
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_config(const Config& config) {
|
|
|
|
std::string config_file_path = get_config_file_path();
|
|
|
|
std::string tmp_config_file_path = config_file_path + ".tmp";
|
|
|
|
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(tmp_config_file_path.c_str(), "w", false), fclose_and_log);
|
2023-01-31 14:40:04 +00:00
|
|
|
nlohmann::json json_config = config;
|
|
|
|
std::string str_config = json_config.dump();
|
|
|
|
fwrite(str_config.data(), sizeof(char), str_config.size(), config_file.get());
|
2023-01-04 16:40:35 +00:00
|
|
|
config_file.reset();
|
|
|
|
|
|
|
|
if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
throw_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')');
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|