]> git.ipfire.org Git - pakfire.git/commitdiff
downloader: Allow overwriting the destination file straight away
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Apr 2021 17:32:41 +0000 (17:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Apr 2021 17:32:41 +0000 (17:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/downloader.c
src/libpakfire/include/pakfire/downloader.h

index b971cd0ecf3ca17a63dc6ea14872c30baeea8c3b..c0c1a5d9c70e71af8de4be3569dda54388dcf9d3 100644 (file)
@@ -276,6 +276,13 @@ static struct pakfire_transfer* pakfire_downloader_create_transfer(
        // Copy path
        pakfire_string_set(transfer->path, path);
 
+       // Open the desination file if we do not want a temporary file
+       if (flags & PAKFIRE_TRANSFER_NOTEMP) {
+               transfer->f = fopen(path, "w+");
+               if (!transfer->f)
+                       goto ERROR;
+       }
+
        // Copy baseurl
        if (baseurl)
                pakfire_string_set(transfer->baseurl, baseurl);
@@ -393,19 +400,22 @@ static const char* curl_http_version(long v) {
 static int pakfire_transfer_save(struct pakfire_downloader* downloader,
                struct pakfire_transfer* transfer) {
        struct utimbuf times;
+       int r;
 
        DEBUG(downloader->pakfire,
                "Download successful. Storing result in %s\n", transfer->path);
 
-       // Remove destination (if it exists)
-       unlink(transfer->path);
+       if (!(transfer->flags & PAKFIRE_TRANSFER_NOTEMP)) {
+               // Remove destination (if it exists)
+               unlink(transfer->path);
 
-       // Move the temporary file to its destination
-       int r = link(transfer->tempfile, transfer->path);
-       if (r) {
-               ERROR(downloader->pakfire, "Could not link destination file %s: %s\n",
-                       transfer->path, strerror(errno));
-               return r;
+               // Move the temporary file to its destination
+               r = link(transfer->tempfile, transfer->path);
+               if (r) {
+                       ERROR(downloader->pakfire, "Could not link destination file %s: %s\n",
+                               transfer->path, strerror(errno));
+                       return r;
+               }
        }
 
        // Filetime
@@ -589,10 +599,11 @@ static int pakfire_downloader_prepare_transfer(struct pakfire_downloader* downlo
                                transfer->tempfile, strerror(errno));
                        return 1;
                }
-
-               curl_easy_setopt(transfer->handle, CURLOPT_WRITEDATA, transfer->f);
        }
 
+       // Write all data to the allocated file descriptor
+       curl_easy_setopt(transfer->handle, CURLOPT_WRITEDATA, transfer->f);
+
        return 0;
 }
 
index 0044c040731bc150fc51d44c25afdadd57c7f1af..27448af6282e796876a54c0019798b4ff0b858b5 100644 (file)
@@ -31,6 +31,7 @@ struct pakfire_mirrorlist;
 enum pakfire_transfer_flags {
        PAKFIRE_TRANSFER_NONE                           = 0,
        PAKFIRE_TRANSFER_NOPROGRESS                     = (1 << 0),
+       PAKFIRE_TRANSFER_NOTEMP                         = (1 << 1),
 };
 
 int pakfire_downloader_create(struct pakfire_downloader** downloader, Pakfire pakfire);