Skip to content

Commit

Permalink
fallback to /tmp if conf and cache dir is not writable
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Sep 22, 2023
1 parent 17dd9c0 commit a9414dd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/app/app_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct app_settings_t {
char *ini_path;
char *condb_path;
char *key_dir;
bool conf_persistent;
} CONFIGURATION, *PCONFIGURATION, app_settings_t;

typedef struct audio_config_entry_t {
Expand Down
15 changes: 7 additions & 8 deletions src/app/backend/pcmanager/known_hosts.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "priv.h"
#include "pclist.h"
#include "app.h"

#include <ini.h>

Expand Down Expand Up @@ -50,10 +51,9 @@ static void hostlist_node_free(known_host_t *node);

void pcmanager_load_known_hosts(pcmanager_t *manager) {
commons_log_info("PCManager", "Load unknown hosts");
char *confdir = path_pref(), *conffile = path_join(confdir, CONF_NAME_HOSTS);
free(confdir);
char *conf_file = path_join(manager->app->settings.conf_dir, CONF_NAME_HOSTS);
known_host_t *hosts = NULL;
if (ini_parse(conffile, (ini_handler) known_hosts_parse, &hosts) != 0) {
if (ini_parse(conf_file, (ini_handler) known_hosts_parse, &hosts) != 0) {
goto cleanup;
}

Expand Down Expand Up @@ -87,14 +87,13 @@ void pcmanager_load_known_hosts(pcmanager_t *manager) {
}
cleanup:
hostlist_free(hosts, hostlist_node_free);
free(conffile);
free(conf_file);
}

void pcmanager_save_known_hosts(pcmanager_t *manager) {
char *confdir = path_pref(), *conffile = path_join(confdir, CONF_NAME_HOSTS);
free(confdir);
FILE *fp = fopen(conffile, "wb");
free(conffile);
char *conf_file = path_join(manager->app->settings.conf_dir, CONF_NAME_HOSTS);
FILE *fp = fopen(conf_file, "wb");
free(conf_file);
if (!fp) { return; }

bool selected_set = false;
Expand Down
3 changes: 2 additions & 1 deletion src/app/platform/sdl/path_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ char *path_assets() {
return SDL_GetBasePath();
}

char *path_pref() {
char *path_pref(bool *persistent) {
char *path = SDL_GetPrefPath("com.limelight", "moonlight-tv");
unsigned int len = SDL_strlen(path);
if (len && path[len - 1] == PATH_SEPARATOR) {
path[len - 1] = '\0';
}
*persistent = true;
return path;
}

Expand Down
13 changes: 10 additions & 3 deletions src/app/platform/webos/path_webos.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ char *path_assets() {
return path_join(basedir, "assets");
}

char *path_pref() {
char *path_pref(bool *persistent) {
const char *basedir = SDL_getenv("HOME");
char *confdir = path_join(basedir, "conf");
path_dir_ensure(confdir);
if (path_dir_ensure(confdir) == -1) {
free(confdir);
confdir = strdup("/tmp/moonlight-tv/conf");
path_dir_ensure(confdir);
*persistent = false;
} else {
*persistent = true;
}
return confdir;
}

Expand All @@ -19,7 +26,7 @@ char *path_cache() {
char *cachedir = path_join(basedir, "cache");
if (path_dir_ensure(cachedir) == -1) {
free(cachedir);
cachedir = strdup("/tmp/moonlight-tv-cache");
cachedir = strdup("/tmp/moonlight-tv/cache");
path_dir_ensure(cachedir);
}
return cachedir;
Expand Down
24 changes: 21 additions & 3 deletions src/app/ui/launcher/launcher.controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static void pcitem_set_selected(lv_obj_t *pcitem, bool selected);

static void show_decoder_error();

static void show_conf_persistent_error();

static void decoder_error_cb(lv_event_t *e);

static void populate_selected_host(launcher_fragment_t *controller);
Expand Down Expand Up @@ -188,9 +190,14 @@ static void launcher_view_init(lv_fragment_t *self, lv_obj_t *view) {
lv_obj_set_style_transition(fragment->detail, &fragment->tr_detail, LV_STATE_USER_1);
current_instance = fragment;

if (fragment->first_created && (fragment->global->ss4s.selection.video_module == NULL ||
fragment->global->ss4s.selection.audio_module == NULL)) {
show_decoder_error();
if (fragment->first_created) {
if (fragment->global->ss4s.selection.video_module == NULL ||
fragment->global->ss4s.selection.audio_module == NULL) {
show_decoder_error();
}
if (!app_configuration->conf_persistent) {
show_conf_persistent_error();
}
}
fragment->first_created = false;
}
Expand Down Expand Up @@ -466,6 +473,17 @@ static void show_decoder_error() {
lv_obj_center(msgbox);
}

static void show_conf_persistent_error() {
static const char *btn_txts[] = {translatable("OK"), ""};
lv_obj_t *msgbox = lv_msgbox_create_i18n(NULL, locstr("Can't save settings"), "placeholder", btn_txts, false);
lv_obj_add_event_cb(msgbox, decoder_error_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_t *msgview = lv_msgbox_get_text(msgbox);
lv_label_set_text_fmt(msgview, locstr("Can't find a writable directory to save settings. Settings and pairing "
"information will be lost when the TV is turned off.\n\n"
"(If you're using webOS 3.0 or newer, restart the TV may fix this issue.)"));
lv_obj_center(msgbox);
}

static void decoder_error_cb(lv_event_t *e) {
lv_obj_t *msgbox = lv_event_get_current_target(e);
lv_msgbox_close_async(msgbox);
Expand Down
2 changes: 2 additions & 0 deletions src/app/util/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#if __WIN32
#define MKDIR(path, perm) mkdir(path)
#define PATH_MAX 260
#else
#include <sys/stat.h>
#define MKDIR(path, perm) mkdir(path, perm)
#define PATH_MAX 4096
#endif
19 changes: 14 additions & 5 deletions src/app/util/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ void path_join_to(char *dest, size_t maxlen, const char *parent, const char *bas
}

int path_dir_ensure(const char *dir) {
if (access(dir, F_OK | W_OK) == -1) {
if (errno == ENOENT) {
return MKDIR(dir, 0755);
if (access(dir, F_OK | W_OK) == 0) {
return 0;
}
if (errno == ENOENT) {
char tmp[PATH_MAX];
char *last_slash = strrchr(dir, PATH_SEPARATOR);
if (last_slash && last_slash != dir) {
strncpy(tmp, dir, last_slash - dir);
tmp[last_slash - dir] = '\0';
if (path_dir_ensure(tmp) == -1) {
return -1;
}
}
return -1;
return MKDIR(dir, 0755);
}
return 0;
return -1;
}
3 changes: 2 additions & 1 deletion src/app/util/path.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stddef.h>
#include <stdbool.h>

#if __WIN32
#define PATH_SEPARATOR '\\'
Expand All @@ -14,7 +15,7 @@ void path_join_to(char *dest, size_t maxlen, const char *parent, const char *bas

char *path_assets();

char *path_pref();
char *path_pref(bool *persistent);

char *path_cache();

Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ int main(int argc, char *argv[]) {


static int settings_load(app_settings_t *settings) {
settings_initialize(settings, path_pref());
bool persistent = true;
settings_initialize(settings, path_pref(&persistent));
settings->conf_persistent = persistent;
if (!settings_read(settings)) {
commons_log_warn("Settings", "Failed to read settings %s", settings->ini_path);
}
Expand Down

0 comments on commit a9414dd

Please sign in to comment.