From: Andreas Ă–man Date: Sat, 8 Jan 2011 12:36:48 +0000 (+0100) Subject: Add hts_settings_open_file() X-Git-Tag: 2.99~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad2ab1ee04ea04e396d65272b7e99b16b9c8c356;p=thirdparty%2Ftvheadend.git Add hts_settings_open_file() --- diff --git a/src/settings.c b/src/settings.c index 1c5128b3f..ca3c72701 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 6be8cffea..1713d9d26 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__ */