From ad2ab1ee04ea04e396d65272b7e99b16b9c8c356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Sat, 8 Jan 2011 13:36:48 +0100 Subject: [PATCH] Add hts_settings_open_file() --- src/settings.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 2 ++ 2 files changed, 56 insertions(+) diff --git a/src/settings.c b/src/settings.c index 1c5128b3..ca3c7270 100644 --- a/src/settings.c +++ b/src/settings.c @@ -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); +} diff --git a/src/settings.h b/src/settings.h index 6be8cffe..1713d9d2 100644 --- a/src/settings.h +++ b/src/settings.h @@ -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__ */