[pacman-dev] [PATCH] Add a fetch callback to allow front-end download support

Sebastian Nowicki sebnow at gmail.com
Thu Feb 19 01:55:22 EST 2009


This allows a frontend to define its own download algorithm so that the
libfetch dependency can be omitted without using an external process.
The callback will be used when if it is defined, otherwise the old
behavior applies.

Signed-off-by: Sebastian Nowicki <sebnow at gmail.com>
---
 lib/libalpm/alpm.h   |   14 ++++++++++++++
 lib/libalpm/dload.c  |    9 ++++++---
 lib/libalpm/handle.c |   18 ++++++++++++++++++
 lib/libalpm/handle.h |    1 +
 4 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 7b7ca4e..266f7ff 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -83,6 +83,17 @@ int alpm_logaction(char *fmt, ...);
 typedef void (*alpm_cb_download)(const char *filename,
 		off_t xfered, off_t total);
 typedef void (*alpm_cb_totaldl)(off_t total);
+/** A callback for downloading files
+ * @param url the URL of the file to be downloaded
+ * @param localpath the directory to which the file should be downloaded
+ * @param mtimeold the modification time of the file previously downloaded
+ * @param mtimenew the modification time of the newly downloaded file.
+ * This should be set by the callback.
+ * @return 0 on success, 1 if the modification times are identical, -1 on
+ * error.
+ */
+typedef int (*alpm_cb_fetch)(const char *url, const char *localpath,
+		time_t mtimeold, time_t *mtimenew);
 
 /*
  * Options
@@ -94,6 +105,9 @@ void alpm_option_set_logcb(alpm_cb_log cb);
 alpm_cb_download alpm_option_get_dlcb();
 void alpm_option_set_dlcb(alpm_cb_download cb);
 
+alpm_cb_fetch alpm_option_get_fetchcb();
+void alpm_option_set_fetchcb(alpm_cb_fetch cb);
+
 alpm_cb_totaldl alpm_option_get_totaldlcb();
 void alpm_option_set_totaldlcb(alpm_cb_totaldl cb);
 
diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 5b0a691..a244999 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -352,12 +352,15 @@ static int download(const char *url, const char *localpath,
 	int ret;
 
 	/* We have a few things to take into account here.
-	 * 1. If we have both internal/external available, choose based on
+	 * 1. If the fetch callback is set, prefer it.
+	 * 2. If we have both internal/external available, choose based on
 	 * whether xfercommand is populated.
-	 * 2. If we only have external available, we should first check
+	 * 3. If we only have external available, we should first check
 	 * if a command was provided before we drop into download_external.
 	 */
-	if(handle->xfercommand == NULL) {
+	if(handle->fetchcb != NULL) {
+		ret = handle->fetchcb(url, localpath, mtimeold, mtimenew);
+	} else if(handle->xfercommand == NULL) {
 #if defined(INTERNAL_DOWNLOAD)
 		ret = download_internal(url, localpath, mtimeold, mtimenew);
 #else
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 813f439..f19bc3f 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -105,6 +105,15 @@ alpm_cb_download SYMEXPORT alpm_option_get_dlcb()
 	return handle->dlcb;
 }
 
+alpm_cb_fetch SYMEXPORT alpm_option_get_fetchcb()
+{
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return NULL;
+	}
+	return handle->fetchcb;
+}
+
 alpm_cb_totaldl SYMEXPORT alpm_option_get_totaldlcb()
 {
 	if (handle == NULL) {
@@ -258,6 +267,15 @@ void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb)
 	handle->dlcb = cb;
 }
 
+void alpm_option_set_fetchcb(alpm_cb_fetch cb)
+{
+	if (handle == NULL) {
+		pm_errno = PM_ERR_HANDLE_NULL;
+		return;
+	}
+	handle->fetchcb = cb;
+}
+
 void SYMEXPORT alpm_option_set_totaldlcb(alpm_cb_totaldl cb)
 {
 	if (handle == NULL) {
diff --git a/lib/libalpm/handle.h b/lib/libalpm/handle.h
index ad7666d..f895d85 100644
--- a/lib/libalpm/handle.h
+++ b/lib/libalpm/handle.h
@@ -40,6 +40,7 @@ typedef struct _pmhandle_t {
 	alpm_cb_log logcb;      /* Log callback function */
 	alpm_cb_download dlcb;  /* Download callback function */
 	alpm_cb_totaldl totaldlcb;  /* Total download callback function */
+	alpm_cb_fetch fetchcb; /* Download file callback function */
 
 	/* filesystem paths */
 	char *root;              /* Root path, default '/' */
-- 
1.6.1



More information about the pacman-dev mailing list