#include <pakfire/arch.h>
#include <pakfire/dist.h>
#include <pakfire/downloader.h>
+#include <pakfire/i18n.h>
#include <pakfire/linter.h>
#include <pakfire/logging.h>
#include <pakfire/package.h>
return r;
}
-static int pakfire_dist_create_downloader_and_mirrorlist(
- struct pakfire* pakfire, struct pakfire_parser* makefile,
- struct pakfire_downloader** downloader, struct pakfire_mirrorlist** mirrorlist) {
+static int pakfire_dist_get_mirrorlist(struct pakfire* pakfire,
+ struct pakfire_parser* makefile, struct pakfire_mirrorlist** list) {
+ struct pakfire_mirrorlist* m = NULL;
char* p = NULL;
-
- // Create the downloader
- int r = pakfire_downloader_create(downloader, pakfire);
- if (r) {
- ERROR(pakfire, "Could not initialize downloader\n");
- return r;
- }
+ int r;
// Fetch source_dl
char* source_dl = pakfire_parser_get(makefile, NULL, "source_dl");
return 0;
// Create mirrorlist
- r = pakfire_mirrorlist_create(mirrorlist, pakfire);
+ r = pakfire_mirrorlist_create(&m, pakfire);
if (r) {
ERROR(pakfire, "Could not create the mirrorlist\n");
goto ERROR;
// Add all mirrors
const char* mirror = strtok_r(source_dl, " ", &p);
-
while (mirror) {
- r = pakfire_mirrorlist_add_mirror(*mirrorlist, mirror);
+ r = pakfire_mirrorlist_add_mirror(m, mirror);
if (r)
goto ERROR;
}
// Success
- r = 0;
+ *list = pakfire_mirrorlist_ref(m);
ERROR:
+ if (m)
+ pakfire_mirrorlist_unref(m);
if (source_dl)
free(source_dl);
return r;
}
+static int pakfire_dist_download_source(struct pakfire_downloader* downloader,
+ struct pakfire_mirrorlist* mirrorlist, const char* filename, const char* cache_path) {
+ struct pakfire_transfer* transfer = NULL;
+ int r;
+
+ // Do not download if the file exists
+ if (pakfire_path_exists(cache_path))
+ return 0;
+
+ // Create a new transfer
+ r = pakfire_downloader_transfer_create(&transfer, downloader, filename);
+ if (r)
+ goto ERROR;
+
+ // Set the mirrorlist
+ r = pakfire_downloader_transfer_set_mirrorlist(transfer, mirrorlist);
+ if (r)
+ goto ERROR;
+
+ // Set the target
+ r = pakfire_downloader_transfer_set_target(transfer, cache_path);
+ if (r)
+ goto ERROR;
+
+ // Run the download
+ r = pakfire_downloader_transfer_run(transfer, 0);
+ if (r)
+ goto ERROR;
+
+ERROR:
+ if (transfer)
+ pakfire_downloader_transfer_unref(transfer);
+
+ return r;
+}
+
static int pakfire_dist_add_source(struct pakfire* pakfire, struct pakfire_packager* packager,
struct pakfire_package* pkg, struct pakfire_downloader* downloader,
struct pakfire_mirrorlist* mirrorlist, const char* filename) {
- int r;
char archive_path[PATH_MAX];
char cache_path[PATH_MAX];
+ int r;
const char* name = pakfire_package_get_string(pkg, PAKFIRE_PKG_NAME);
if (r)
return r;
- // Download the file if it does not exist in the cache
- if (access(cache_path, R_OK) != 0) {
- r = pakfire_downloader_retrieve(downloader, NULL, mirrorlist,
- NULL, filename, cache_path, 0, NULL, 0, 0);
- if (r)
- return r;
- }
+ // Download the source file
+ r = pakfire_dist_download_source(downloader, mirrorlist, filename, cache_path);
+ if (r)
+ return r;
r = pakfire_string_format(archive_path, "files/%s", filename);
if (r)
static int pakfire_dist_add_sources(struct pakfire* pakfire, struct pakfire_packager* packager,
struct pakfire_package* pkg, struct pakfire_parser* makefile) {
+ struct pakfire_downloader* downloader = NULL;
+ struct pakfire_mirrorlist* mirrorlist = NULL;
char* sources = NULL;
char* p = NULL;
int r;
if (!sources)
return 0;
- struct pakfire_downloader* downloader = NULL;
- struct pakfire_mirrorlist* mirrorlist = NULL;
+ // Create a downloader
+ r = pakfire_downloader_create(&downloader, pakfire);
+ if (r)
+ goto ERROR;
- // Create a downloader and mirrorlist
- int r = pakfire_dist_create_downloader_and_mirrorlist(pakfire, makefile,
- &downloader, &mirrorlist);
+ // Fetch the mirrorlist
+ r = pakfire_dist_get_mirrorlist(pakfire, makefile, &mirrorlist);
if (r)
goto ERROR;