From dbf973307ae34d8a7918b781b9f315ad51ef15a8 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Sat, 10 Jun 2023 15:42:03 +0200 Subject: [PATCH] dvr_storage: Use XDG spec directories The XDG spec has a way to determine the users preferred Video directory. This is important, because in different locales, this may be a different directory, preventing annoyance for users who have a localized home dir. With the newly added XDG helpers, this becomes a triviality. This change does mean, that the behavior is slightly changed, as XDG directories are probed first. However since this only affects the startup, after which these directories are stored in the config, the impact should be neglectable. Signed-off-by: Olliver Schinagl --- src/dvr/dvr_config.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/dvr/dvr_config.c b/src/dvr/dvr_config.c index 8c860fb08..88f4e9686 100644 --- a/src/dvr/dvr_config.c +++ b/src/dvr/dvr_config.c @@ -17,7 +17,10 @@ * along with this program. If not, see . */ +#define _GNU_SOURCE #include +#include +#include #include "settings.h" @@ -274,28 +277,40 @@ dvr_config_destroy(dvr_config_t *cfg, int delconf) static void dvr_config_storage_check(dvr_config_t *cfg) { - char buf[PATH_MAX]; + char home_dir[PATH_MAX + sizeof("/Videos")]; + char dvr_dir[PATH_MAX]; + char *xdg_dir; struct stat st; - const char *homedir; if(cfg->dvr_storage != NULL && cfg->dvr_storage[0]) return; /* Try to figure out a good place to put them videos */ - - homedir = getenv("HOME"); - - if(homedir != NULL) { - snprintf(buf, sizeof(buf), "%s/Videos", homedir); - if(stat(buf, &st) == 0 && S_ISDIR(st.st_mode)) - cfg->dvr_storage = strdup(buf); - - else if(stat(homedir, &st) == 0 && S_ISDIR(st.st_mode)) - cfg->dvr_storage = strdup(homedir); - else - cfg->dvr_storage = strdup(getcwd(buf, sizeof(buf))); + snprintf(home_dir, sizeof(home_dir), "%s/Videos", getenv("HOME")); + xdg_dir = hts_settings_get_xdg_dir_with_fallback("VIDEOS", home_dir); + if (xdg_dir != NULL) { + if (stat(xdg_dir, &st) == 0) { + if (S_ISLNK(st.st_mode)) { + char xdg_dir_link[PATH_MAX - sizeof('\0')]; + + if (readlink(xdg_dir, xdg_dir_link, sizeof(xdg_dir_link)) == -1) + tvhwarn(LS_DVR, "symlink '%s' error: %s\n", xdg_dir, strerror(errno)); + else + strncpy(dvr_dir, xdg_dir_link, sizeof(dvr_dir)); + } else if (S_ISDIR(st.st_mode)) { + strncpy(dvr_dir, xdg_dir, sizeof(dvr_dir) - sizeof('\0')); + } + } + free(xdg_dir); } + if((stat(dvr_dir, &st) == 0) && S_ISDIR(st.st_mode)) + cfg->dvr_storage = strndup(dvr_dir, PATH_MAX); + else if(stat(home_dir, &st) == 0 && S_ISDIR(st.st_mode)) + cfg->dvr_storage = strndup(home_dir, sizeof(home_dir)); + else + cfg->dvr_storage = get_current_dir_name(); + tvhwarn(LS_DVR, "Output directory for video recording is not yet configured " "for DVR configuration \"%s\". " -- 2.47.2