From: Michael Tremer Date: Mon, 26 Apr 2021 17:32:41 +0000 (+0000) Subject: downloader: Allow overwriting the destination file straight away X-Git-Tag: 0.9.28~1285^2~232 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b53a9f84c00ac757e28c810ad9df6a7d5100fb8;p=pakfire.git downloader: Allow overwriting the destination file straight away Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/downloader.c b/src/libpakfire/downloader.c index b971cd0ec..c0c1a5d9c 100644 --- a/src/libpakfire/downloader.c +++ b/src/libpakfire/downloader.c @@ -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; } diff --git a/src/libpakfire/include/pakfire/downloader.h b/src/libpakfire/include/pakfire/downloader.h index 0044c0407..27448af62 100644 --- a/src/libpakfire/include/pakfire/downloader.h +++ b/src/libpakfire/include/pakfire/downloader.h @@ -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);