]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Add hts_settings_open_file()
authorAndreas Öman <andreas@lonelycoder.com>
Sat, 8 Jan 2011 12:36:48 +0000 (13:36 +0100)
committerAndreas Öman <andreas@lonelycoder.com>
Sat, 8 Jan 2011 12:36:48 +0000 (13:36 +0100)
src/settings.c
src/settings.h

index 1c5128b3f6eaa57c1c9e43f97af6a3253220d69e..ca3c72701091803b6738063a93383f30c73857b6 100644 (file)
@@ -284,3 +284,57 @@ hts_settings_remove(const char *pathfmt, ...)
     return;
   unlink(fullpath);
 }
+
+
+/**
+ *
+ */
+int
+hts_settings_open_file(int for_write, const char *pathfmt, ...)
+{
+  char path[256];
+  char fullpath[256];
+  int x, l;
+  va_list ap;
+  struct stat st;
+  char *n;
+
+  if(settingspath == NULL)
+    return -1;
+
+  va_start(ap, pathfmt);
+  vsnprintf(path, sizeof(path), pathfmt, ap);
+  va_end(ap);
+
+  n = path;
+
+  while(*n) {
+    if(*n == ':' || *n == '?' || *n == '*' || *n > 127 || *n < 32)
+      *n = '_';
+    n++;
+  }
+
+  l = strlen(path);
+
+  for(x = 0; x < l; x++) {
+    if(path[x] == '/') {
+      /* It's a directory here */
+
+      path[x] = 0;
+      snprintf(fullpath, sizeof(fullpath), "%s/%s", settingspath, path);
+
+      if(stat(fullpath, &st) && mkdir(fullpath, 0700)) {
+       tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s",
+              fullpath, strerror(errno));
+       return -1;
+      }
+      path[x] = '/';
+    }
+  }
+
+  snprintf(fullpath, sizeof(fullpath), "%s/%s", settingspath, path);
+
+  int flags = for_write ? O_CREAT | O_TRUNC | O_WRONLY : O_RDONLY;
+
+  return tvh_open(fullpath, flags, 0700);
+}
index 6be8cffea32e12b361d0062704c3a953973bdb19..1713d9d268eba298d741362b63ec0c29cf48d265 100644 (file)
@@ -32,4 +32,6 @@ void hts_settings_remove(const char *pathfmt, ...);
 
 const char *hts_settings_get_root(void);
 
+int hts_settings_open_file(int for_write, const char *pathfmt, ...);
+
 #endif /* HTSSETTINGS_H__ */