Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions ext_components/cp0_lvgl/src/cp0/cp0_lvgl_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../cp0_config_json.h"

#include <cstdlib>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <functional>
Expand All @@ -13,6 +14,7 @@
#include <string>
#include <utility>
#include <vector>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

Expand Down Expand Up @@ -76,11 +78,11 @@ class ConfigSystem
copy_cstr(entries_[idx].val, val ? val : "", VAL_MAX);
}

void save()
int save()
{
std::lock_guard<std::mutex> lock(mutex_);
ensure_loaded_locked();
save_locked();
return save_locked();
}

void api_call(arg_t arg, callback_t callback)
Expand Down Expand Up @@ -114,8 +116,8 @@ class ConfigSystem
set_str(key.c_str(), val.c_str());
report(callback, 0, "ok");
} else if (cmd == "Save") {
save();
report(callback, 0, "ok");
int ret = save();
report(callback, ret, ret == 0 ? "ok" : "save failed");
} else {
report(callback, -1, "unknown config api\n");
}
Expand Down Expand Up @@ -172,7 +174,7 @@ class ConfigSystem
add_entry_locked(e.first, e.second);
}

void save_locked()
int save_locked()
{
std::vector<std::pair<std::string, std::string>> kv;
kv.reserve(count_);
Expand All @@ -183,14 +185,34 @@ class ConfigSystem
const std::string dir = config_dir();
const char *home = std::getenv("HOME");
std::string base = (home && home[0]) ? std::string(home) : std::string("/root");
mkdir((base + "/.config").c_str(), 0755);
mkdir(dir.c_str(), 0755);

FILE *fp = std::fopen(config_file().c_str(), "w");
if (!fp) return;
std::fwrite(json.data(), 1, json.size(), fp);
if (!ensure_dir((base + "/.config").c_str()))
return -1;
if (!ensure_dir(dir.c_str()))
return -1;

const std::string path = config_file();
const std::string tmp_path = path + ".tmp";
FILE *fp = std::fopen(tmp_path.c_str(), "w");
if (!fp) return -1;
size_t written = std::fwrite(json.data(), 1, json.size(), fp);
if (written != json.size()) {
std::fclose(fp);
std::remove(tmp_path.c_str());
return -1;
}
if (std::fflush(fp) != 0 || fsync(fileno(fp)) != 0) {
std::fclose(fp);
std::remove(tmp_path.c_str());
return -1;
}
std::fclose(fp);
if (std::rename(tmp_path.c_str(), path.c_str()) != 0) {
std::remove(tmp_path.c_str());
return -1;
}
fsync_dir(dir.c_str());
sync();
return 0;
}

static bool read_file(const char *path, std::string &out)
Expand All @@ -205,6 +227,26 @@ class ConfigSystem
return true;
}

static bool ensure_dir(const char *path)
{
if (!path || !path[0]) return false;
if (mkdir(path, 0755) == 0) return true;
return errno == EEXIST;
}

static void fsync_dir(const char *path)
{
#ifdef O_DIRECTORY
int fd = open(path, O_RDONLY | O_DIRECTORY);
#else
int fd = open(path, O_RDONLY);
#endif
if (fd >= 0) {
fsync(fd);
close(fd);
}
}

int find_entry_locked(const char *key) const
{
if (!key || key[0] == '\0') return -1;
Expand Down
35 changes: 33 additions & 2 deletions ext_components/cp0_lvgl/src/sdl/sdl_lvgl_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utility>
Expand Down Expand Up @@ -131,11 +132,28 @@ class ConfigSystem {
for (int i = 0; i < count_; ++i)
kv.emplace_back(entries_[i].key, entries_[i].val);
const std::string json = cp0cfg::to_json(kv);
FILE *fp = std::fopen(join_path(dir, "config.json").c_str(), "w");
const std::string path = join_path(dir, "config.json");
const std::string tmp_path = path + ".tmp";
FILE *fp = std::fopen(tmp_path.c_str(), "w");
if (!fp)
return -1;
std::fwrite(json.data(), 1, json.size(), fp);
size_t written = std::fwrite(json.data(), 1, json.size(), fp);
if (written != json.size()) {
std::fclose(fp);
std::remove(tmp_path.c_str());
return -1;
}
if (std::fflush(fp) != 0 || fsync(fileno(fp)) != 0) {
std::fclose(fp);
std::remove(tmp_path.c_str());
return -1;
}
std::fclose(fp);
if (std::rename(tmp_path.c_str(), path.c_str()) != 0) {
std::remove(tmp_path.c_str());
return -1;
}
fsync_dir(dir.c_str());
return 0;
}

Expand Down Expand Up @@ -252,6 +270,19 @@ class ConfigSystem {
if (callback)
callback(code, data);
}

static void fsync_dir(const char *path)
{
#ifdef O_DIRECTORY
int fd = open(path, O_RDONLY | O_DIRECTORY);
#else
int fd = open(path, O_RDONLY);
#endif
if (fd >= 0) {
fsync(fd);
close(fd);
}
}
};

} // namespace
Expand Down
6 changes: 6 additions & 0 deletions projects/APPLaunch/main/ui/app_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void config_set_int(const char *key, int val)
cp0_signal_config_api({"SetInt", key ? std::string(key) : std::string(), std::to_string(val)}, nullptr);
}

void config_save()
{
cp0_signal_config_api({"Save"}, nullptr);
}

} // namespace

bool launcher_app_registry_is_enabled(const AppDescriptor &desc)
Expand All @@ -46,6 +51,7 @@ void launcher_app_registry_set_enabled(const AppDescriptor &desc, bool enabled)
if (desc.always_on || !desc.configurable)
enabled = true;
config_set_int(desc.config_key, enabled ? 1 : 0);
config_save();
}

void launcher_app_registry_set_changed_callback(LauncherAppRegistryChangedCallback callback,
Expand Down
Loading
Loading