]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
dvr_storage: Use XDG spec directories
authorOlliver Schinagl <oliver@schinagl.nl>
Sat, 10 Jun 2023 13:42:03 +0000 (15:42 +0200)
committerFlole998 <Flole998@users.noreply.github.com>
Fri, 16 Jun 2023 11:11:41 +0000 (13:11 +0200)
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 <oliver@schinagl.nl>
src/dvr/dvr_config.c

index 8c860fb08f88e4a7dd9819048b4770254269999e..88f4e96864f10d413aa66ae2ae1deca21f558852 100644 (file)
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _GNU_SOURCE
 #include <sys/stat.h>
+#include <strings.h>
+#include <unistd.h>
 
 #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\". "