Add hts_settings_open_file()

This commit is contained in:
Andreas Öman 2011-01-08 13:36:48 +01:00
parent ac4a046d25
commit ad2ab1ee04
2 changed files with 56 additions and 0 deletions

View 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);
}

View 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__ */