summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2011-08-19 14:22:14 -0400
committerDan McGee <dan@archlinux.org>2011-08-22 09:23:18 -0500
commitf3e285862143c09bc846fd62992cddd24aefd37e (patch)
tree1d2e3d4c6afa4f0bf6eeb008f9006535f2068e07
parentc65cea0dcbc2777bd72b13d5a69f89d99fa0b0d5 (diff)
downloadpacman-f3e285862143c09bc846fd62992cddd24aefd37e.tar.xz
dload: extract tempfile creation to its own function
Signed-off-by: Dave Reisner <dreisner@archlinux.org> Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--lib/libalpm/dload.c45
1 files changed, 28 insertions, 17 deletions
diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index b99e7d91..586e94f4 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -239,6 +239,30 @@ static void unmask_signal(int signal, struct sigaction sa)
sigaction(signal, &sa, NULL);
}
+static FILE *create_tempfile(struct dload_payload *payload, const char *localpath)
+{
+ int fd;
+ FILE *fp;
+ char randpath[PATH_MAX];
+ alpm_handle_t *handle = payload->handle;
+
+ /* create a random filename, which is opened with O_EXCL */
+ snprintf(randpath, PATH_MAX, "%salpmtmp.XXXXXX", localpath);
+ if((fd = mkstemp(randpath)) == -1 ||
+ !(fp = fdopen(fd, payload->tempfile_openmode))) {
+ unlink(randpath);
+ close(fd);
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("failed to create temporary file for download\n"));
+ return NULL;
+ }
+ /* fp now points to our alpmtmp.XXXXXX */
+ STRDUP(payload->tempfile_name, randpath, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
+ payload->remote_name = strrchr(randpath, '/') + 1;
+
+ return fp;
+}
+
static int curl_download_internal(struct dload_payload *payload,
const char *localpath, char **final_file)
{
@@ -272,27 +296,14 @@ static int curl_download_internal(struct dload_payload *payload,
goto cleanup;
}
} else {
- /* URL isn't to a file and ended with a slash */
- int fd;
- char randpath[PATH_MAX];
-
- /* we can't support resuming this kind of download, so a partial transfer
- * will be destroyed */
+ /* URL doesn't contain a filename, so make a tempfile. We can't support
+ * resuming this kind of download; partial transfers will be destroyed */
payload->unlink_on_fail = 1;
- /* create a random filename, which is opened with O_EXCL */
- snprintf(randpath, PATH_MAX, "%salpmtmp.XXXXXX", localpath);
- if((fd = mkstemp(randpath)) == -1 ||
- !(localf = fdopen(fd, payload->tempfile_openmode))) {
- unlink(randpath);
- close(fd);
- _alpm_log(handle, ALPM_LOG_ERROR,
- _("failed to create temporary file for download\n"));
+ localf = create_tempfile(payload, localpath);
+ if(localf == NULL) {
goto cleanup;
}
- /* localf now points to our alpmtmp.XXXXXX */
- STRDUP(payload->tempfile_name, randpath, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
- payload->remote_name = strrchr(randpath, '/') + 1;
}
curl_set_handle_opts(payload, error_buffer);