From: Michael Tremer Date: Wed, 17 Aug 2022 10:02:55 +0000 (+0000) Subject: util: Add generic function to write data to files X-Git-Tag: 0.9.28~478 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c28035611a1522e6e3cc603e56339f738493a4ad;p=pakfire.git util: Add generic function to write data to files Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 4f5e3e79a..a11d11d42 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -101,7 +101,6 @@ static int pakfire_build_has_flag(struct pakfire_build* build, int flag) { */ static int pakfire_build_enable_repos(struct pakfire_build* build) { char repopath[PATH_MAX]; - FILE* f = NULL; int r = 1; // Fetch repository configuration @@ -133,28 +132,14 @@ static int pakfire_build_enable_repos(struct pakfire_build* build) { if (r < 0) goto ERROR; - // Open configuration file for writing - f = fopen(repopath, "w"); - if (!f) { - ERROR(build->pakfire, "Could not open %s for writing: %m\n", path); - r = 1; - goto ERROR; - } - - // Write configuration - size_t bytes_written = fprintf(f, "%s", config); - if (bytes_written < strlen(config)) { + // Write repository configuration + r = pakfire_file_write(build->pakfire, repopath, 0, 0, 0644, "%s", config); + if (r) { ERROR(build->pakfire, "Could not write repository configuration: %m\n"); - r = 1; goto ERROR; } - // Success - r = 0; - ERROR: - if (f) - fclose(f); if (config) free(config); diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index f20ab956f..8c65fc6c7 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -83,6 +83,11 @@ int __pakfire_path_join(char* dest, size_t length, const char* first, const char* second); const char* pakfire_path_relpath(const char* root, const char* path); +// File stuff + +int pakfire_file_write(struct pakfire* pakfire, const char* path, + uid_t owner, gid_t group, mode_t mode, const char* format, ...); + int pakfire_touch(const char* path, mode_t mode); int pakfire_mkparentdir(const char* path, mode_t mode); int pakfire_mkdir(const char* path, mode_t mode); diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 62fc32c3a..ff8523a7a 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -754,6 +754,52 @@ time_t pakfire_path_age(const char* path) { return -1; } +int pakfire_file_write(struct pakfire* pakfire, const char* path, + uid_t owner, gid_t group, mode_t mode, const char* format, ...) { + va_list args; + int r = 1; + + // Open the destination file + FILE* f = fopen(path, "w"); + if (!f) + goto ERROR; + + // Fetch file-descriptor + int fd = fileno(f); + + // Set owner/group + if (owner && group) { + r = fchown(fd, owner, group); + if (r) + goto ERROR; + } + + // Set mode + if (mode) { + r = fchmod(fd, mode); + if (r) + goto ERROR; + } + + // Write data + va_start(args, format); + r = vfprintf(f, format, args); + va_end(args); + + // Check for writing error + if (r < 0) + goto ERROR; + + // Close the file + return fclose(f); + +ERROR: + if (f) + fclose(f); + + return r; +} + char* pakfire_basename(const char* path) { char* name = strdup(path);