]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add function to copy all data from one fd to another
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Jun 2023 15:51:44 +0000 (15:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 14 Jun 2023 15:51:44 +0000 (15:51 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/pakfire.c
src/libpakfire/util.c

index c402b38f59f298c42a210ce8f67adcabaf576f33..622fa3d7c39b8444746ed646f32de15d6a802361 100644 (file)
@@ -128,6 +128,9 @@ int pakfire_b64encode(struct pakfire* pakfire, char** output,
 int pakfire_b64decode(struct pakfire* pakfire, void** output, size_t* length,
        const char* buffer);
 
+// Copy
+int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst);
+
 #endif
 
 #endif /* PAKFIRE_UTIL_H */
index 5094bb3601c90f48bbf65d9d40e1b1262ab8384c..b1022e925e8ec29a56a5c55b2ed819b6070e6fbf 100644 (file)
@@ -1207,7 +1207,7 @@ enum pakfire_copy_direction {
        PAKFIRE_COPY_OUT,
 };
 
-static int pakfire_copy(struct pakfire* pakfire, const char* src, const char* dst,
+static int __pakfire_copy(struct pakfire* pakfire, const char* src, const char* dst,
                enum pakfire_copy_direction direction) {
        struct archive* reader = NULL;
        struct archive* writer = NULL;
@@ -1292,7 +1292,7 @@ PAKFIRE_EXPORT int pakfire_copy_in(struct pakfire* pakfire, const char* src, con
        if (r)
                return r;
 
-       return pakfire_copy(pakfire, src, path, PAKFIRE_COPY_IN);
+       return __pakfire_copy(pakfire, src, path, PAKFIRE_COPY_IN);
 }
 
 PAKFIRE_EXPORT int pakfire_copy_out(struct pakfire* pakfire, const char* src, const char* dst) {
@@ -1308,7 +1308,7 @@ PAKFIRE_EXPORT int pakfire_copy_out(struct pakfire* pakfire, const char* src, co
        if (r)
                return r;
 
-       return pakfire_copy(pakfire, path, dst, PAKFIRE_COPY_OUT);
+       return __pakfire_copy(pakfire, path, dst, PAKFIRE_COPY_OUT);
 }
 
 PAKFIRE_EXPORT const char* pakfire_get_arch(struct pakfire* pakfire) {
index af58be28b8b2d82a932454916c9f50938657801c..bb325a8063f6035a2b3f439d369a42a6c359f8c5 100644 (file)
@@ -1106,3 +1106,31 @@ ERROR:
 
        return r;
 }
+
+// Copy
+
+int pakfire_copy(struct pakfire* pakfire, FILE* src, FILE* dst) {
+       char buffer[BUFFER_SIZE];
+       size_t bytes_read;
+       size_t bytes_written;
+
+       while (!feof(src)) {
+               // Read some data into the buffer
+               bytes_read = fread(buffer, 1, sizeof(buffer), src);
+
+               // Check for any errors
+               if (ferror(src)) {
+                       ERROR(pakfire, "Could not read data: %m\n");
+                       return 1;
+               }
+
+               // Write the data
+               bytes_written = fwrite(buffer, 1, bytes_read, dst);
+               if (bytes_written < bytes_read) {
+                       ERROR(pakfire, "Could not write data: %m\n");
+                       return 1;
+               }
+       }
+
+       return 0;
+}