[pacman-dev] [PATCH 6/6] lib/dload: extract tempfile creation to its own function

Dave Reisner d at falconindy.com
Thu Aug 18 21:09:36 EDT 2011


Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
 lib/libalpm/dload.c |   46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 4b6ce74..7df66c0 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -253,6 +253,31 @@ static void unmask_signal(int signal, struct sigaction sa)
 	sigaction(signal, &sa, NULL);
 }
 
+static FILE *create_tempfile(struct dload_payload *payload,
+		const char *localpath, const char *open_mode, char **filename)
+{
+	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, open_mode))) {
+		unlink(randpath);
+		close(fd);
+		_alpm_log(handle, ALPM_LOG_ERROR,
+				_("failed to create temporary file for download\n"));
+		return NULL;
+	}
+
+	/* localf now points to our alpmtmp.XXXXXX */
+	STRDUP(*filename, randpath, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
+	payload->filename = strrchr(randpath, '/') + 1;
+
+	return fp;
+}
+
 static int curl_download_internal(struct dload_payload *payload,
 		const char *localpath, char **final_file)
 {
@@ -286,26 +311,13 @@ 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 temp file. 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, open_mode))) {
-			unlink(randpath);
-			close(fd);
-			_alpm_log(handle, ALPM_LOG_ERROR,
-					_("failed to create temporary file for download\n"));
+		localf = create_tempfile(payload, localpath, open_mode, &tempfile);
+		if(!localf) {
 			goto cleanup;
 		}
-		/* localf now points to our alpmtmp.XXXXXX */
-		STRDUP(tempfile, randpath, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
-		payload->filename = strrchr(randpath, '/') + 1;
 	}
 
 	if(curl_set_handle_opts(payload, destfile, tempfile, &localf, &open_mode,
-- 
1.7.6



More information about the pacman-dev mailing list