htsmsg_t * hts_settings_load(const char *pathfmt, ...) { htsmsg_t *ret = NULL; char fullpath[256]; va_list ap; /* Try normal path */ va_start(ap, pathfmt); _hts_settings_buildpath(fullpath, sizeof(fullpath), pathfmt, ap, settingspath); va_end(ap); ret = _hts_settings_load(fullpath); /* Try bundle path */ if (!ret && *pathfmt != '/') { va_start(ap, pathfmt); _hts_settings_buildpath(fullpath, sizeof(fullpath), pathfmt, ap, "data/conf"); va_end(ap); ret = _hts_settings_load(fullpath); } return ret; }
void hts_settings_save(htsmsg_t *record, const char *pathfmt, ...) { char path[PATH_MAX]; char tmppath[PATH_MAX]; int fd; va_list ap; htsbuf_queue_t hq; htsbuf_data_t *hd; int ok, r, pack; if(settingspath == NULL) return; /* Clean the path */ va_start(ap, pathfmt); _hts_settings_buildpath(path, sizeof(path), pathfmt, ap, settingspath); va_end(ap); /* Create directories */ if (hts_settings_makedirs(path)) return; tvhdebug("settings", "saving to %s", path); /* Create tmp file */ snprintf(tmppath, sizeof(tmppath), "%s.tmp", path); if((fd = tvh_open(tmppath, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR)) < 0) { tvhlog(LOG_ALERT, "settings", "Unable to create \"%s\" - %s", tmppath, strerror(errno)); return; } /* Store data */ #if ENABLE_ZLIB pack = strstr(path, "/muxes/") != NULL && /* ugly, redesign API */ strstr(path, "/networks/") != NULL && strstr(path, "/input/") != NULL; #else pack = 0; #endif ok = 1; if (!pack) { htsbuf_queue_init(&hq, 0); htsmsg_json_serialize(record, &hq, 1); TAILQ_FOREACH(hd, &hq.hq_q, hd_link) if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) { tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s", tmppath, strerror(errno)); ok = 0; break; } htsbuf_queue_flush(&hq); } else {
void hts_settings_save(htsmsg_t *record, const char *pathfmt, ...) { char path[256]; char tmppath[256]; int fd; va_list ap; htsbuf_queue_t hq; htsbuf_data_t *hd; int ok; if(settingspath == NULL) return; /* Clean the path */ va_start(ap, pathfmt); _hts_settings_buildpath(path, sizeof(path), pathfmt, ap, settingspath); va_end(ap); /* Create directories */ if (hts_settings_makedirs(path)) return; tvhdebug("settings", "saving to %s", path); /* Create tmp file */ snprintf(tmppath, sizeof(tmppath), "%s.tmp", path); if((fd = tvh_open(tmppath, O_CREAT | O_TRUNC | O_RDWR, 0700)) < 0) { tvhlog(LOG_ALERT, "settings", "Unable to create \"%s\" - %s", tmppath, strerror(errno)); return; } /* Store data */ ok = 1; htsbuf_queue_init(&hq, 0); htsmsg_json_serialize(record, &hq, 1); TAILQ_FOREACH(hd, &hq.hq_q, hd_link) if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) { tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s", tmppath, strerror(errno)); ok = 0; break; } close(fd); htsbuf_queue_flush(&hq); /* Move */ if(ok) { rename(tmppath, path); /* Delete tmp */ } else unlink(tmppath); }
int hts_settings_buildpath (char *dst, size_t dstsize, const char *fmt, ...) { va_list va; va_start(va, fmt); if (!settingspath) return 1; _hts_settings_buildpath(dst, dstsize, fmt, va, settingspath); return 0; }
static htsmsg_t * hts_settings_vload(const char *pathfmt, va_list ap, int depth) { htsmsg_t *ret = NULL; char fullpath[256]; va_list ap2; va_copy(ap2, ap); /* Try normal path */ _hts_settings_buildpath(fullpath, sizeof(fullpath), pathfmt, ap, settingspath); ret = hts_settings_load_path(fullpath, depth); /* Try bundle path */ if (!ret && *pathfmt != '/') { _hts_settings_buildpath(fullpath, sizeof(fullpath), pathfmt, ap2, "data/conf"); ret = hts_settings_load_path(fullpath, depth); } return ret; }
/* * Check if a path exists */ int hts_settings_exists ( const char *pathfmt, ... ) { va_list ap; char path[256]; struct stat st; /* Build path */ va_start(ap, pathfmt); _hts_settings_buildpath(path, sizeof(path), pathfmt, ap, settingspath); va_end(ap); return (stat(path, &st) == 0); }
void hts_settings_remove(const char *pathfmt, ...) { char fullpath[256]; va_list ap; struct stat st; va_start(ap, pathfmt); _hts_settings_buildpath(fullpath, sizeof(fullpath), pathfmt, ap, settingspath); va_end(ap); if (stat(fullpath, &st) == 0) { if (S_ISDIR(st.st_mode)) rmtree(fullpath); else unlink(fullpath); } }
int hts_settings_open_file(int for_write, const char *pathfmt, ...) { char path[256]; va_list ap; /* Build path */ va_start(ap, pathfmt); _hts_settings_buildpath(path, sizeof(path), pathfmt, ap, settingspath); va_end(ap); /* Create directories */ if (for_write) if (hts_settings_makedirs(path)) return -1; /* Open file */ int flags = for_write ? O_CREAT | O_TRUNC | O_WRONLY : O_RDONLY; return tvh_open(path, flags, 0700); }