]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
util: Add generic function to write data to files
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Aug 2022 10:02:55 +0000 (10:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 17 Aug 2022 10:07:43 +0000 (10:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index 4f5e3e79a3936dfca0d39fd88422a69c7d5f1888..a11d11d429ec2cf195760eaea2d533d9763199c2 100644 (file)
@@ -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);
 
index f20ab956f7f9f2c7e7ad8da9693e5e195587d7a3..8c65fc6c73ffa5caa3012fdf5877e107ccfe41d3 100644 (file)
@@ -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);
index 62fc32c3a76e398ebe7711e0ada285669914ee10..ff8523a7aad9737e72f97cbb8e2b897f592bd10f 100644 (file)
@@ -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);