From: Flole Date: Tue, 7 Oct 2025 17:09:25 +0000 (+0200) Subject: Refactor EPGDB save logic for error handling X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;ds=inline;p=thirdparty%2Ftvheadend.git Refactor EPGDB save logic for error handling --- diff --git a/src/epgdb.c b/src/epgdb.c index e8e21025f..02b59c5c9 100644 --- a/src/epgdb.c +++ b/src/epgdb.c @@ -302,33 +302,44 @@ static void epg_save_tsk_callback ( void *p, int dearmed ) int fd, r; tvhinfo(LS_EPGDB, "save start"); - hts_settings_buildpath(tmppath, sizeof(path), "epgdb.v%d", EPG_DB_VERSION); + if(hts_settings_buildpath(tmppath, sizeof(tmppath), "epgdb.v%d", EPG_DB_VERSION)) { + tvhinfo(LS_EPGDB, "No config dir, not saving EPG"); + goto done; + } + if (!realpath(tmppath, path)) strlcpy(path, tmppath, sizeof(path)); snprintf(tmppath, sizeof(tmppath), "%s.tmp", path); - if (hts_settings_makedirs(tmppath)) - fd = -1; - else - fd = tvh_open(tmppath, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); - if (fd >= 0) { + if (hts_settings_makedirs(tmppath)) { + tvherror(LS_EPGDB, "Failed to create tmp directories for %s", tmppath); + goto done; + } + + fd = tvh_open(tmppath, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); + if (fd < 0) { + tvherror(LS_EPGDB, "unable to open epgdb file"); + goto done; + } + #if ENABLE_ZLIB - if (config.epg_compress) { - r = tvh_gzip_deflate_fd_header(fd, sb->sb_data, size, &orig, 3, "01") < 0; - } else -#endif - r = tvh_write(fd, sb->sb_data, orig = size); - close(fd); - if (r) { - tvherror(LS_EPGDB, "write error (size %zd)", orig); - if (remove(tmppath)) - tvherror(LS_EPGDB, "unable to remove file %s", tmppath); - } else { - tvhinfo(LS_EPGDB, "stored (size %zd)", orig); - if (rename(tmppath, path)) - tvherror(LS_EPGDB, "unable to rename file %s to %s", tmppath, path); - } + if (config.epg_compress) { + r = tvh_gzip_deflate_fd_header(fd, sb->sb_data, size, &orig, 3, "01") < 0; } else - tvherror(LS_EPGDB, "unable to open epgdb file"); +#endif + r = tvh_write(fd, sb->sb_data, orig = size); + + close(fd); + if (r) { + tvherror(LS_EPGDB, "write error (size %zd)", orig); + if (remove(tmppath)) + tvherror(LS_EPGDB, "unable to remove file %s", tmppath); + } else { + tvhinfo(LS_EPGDB, "stored (size %zd)", orig); + if (rename(tmppath, path)) + tvherror(LS_EPGDB, "unable to rename file %s to %s", tmppath, path); + } + +done: sbuf_free(sb); free(sb); }