[WIP Patch 11/12] Use the new http API in http-push.c:fetch_index()
Signed-off-by: Mike Hommey <mh@glandium.org>
---
http-push.c | 76 ++++++----------------------------------------------------
1 files changed, 8 insertions(+), 68 deletions(-)
diff --git a/http-push.c b/http-push.c
index b32def4..0812f58 100644
--- a/http-push.c
+++ b/http-push.c
@@ -898,32 +898,15 @@ static int fetch_index(unsigned char *sha1)
char *hex = sha1_to_hex(sha1);
char *filename;
char *url;
- char tmpfile[PATH_MAX];
- long prev_posn = 0;
- char range[RANGE_HEADER_SIZE];
- struct curl_slist *range_header = NULL;
-
- FILE *indexfile;
- struct active_request_slot *slot;
- struct slot_results results;
+ int ret = 0;
/* Don't use the index if the pack isn't there */
url = xmalloc(strlen(remote->url) + 64);
sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
- slot = get_active_slot();
- slot->results = &results;
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
- if (start_active_slot(slot)) {
- run_active_slot(slot);
- if (results.curl_result != CURLE_OK) {
- free(url);
- return error("Unable to verify pack %s is available",
- hex);
- }
- } else {
+ if (http_get_strbuf(url, NULL, 0)) {
free(url);
- return error("Unable to start request");
+ return error("Unable to verify pack %s is available",
+ hex);
}
if (has_pack_index(sha1)) {
@@ -937,55 +920,12 @@ static int fetch_index(unsigned char *sha1)
sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
filename = sha1_pack_index_name(sha1);
- snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
- indexfile = fopen(tmpfile, "a");
- if (!indexfile) {
- free(url);
- return error("Unable to open local file %s for pack index",
- tmpfile);
- }
-
- slot = get_active_slot();
- slot->results = &results;
- curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
- curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
- slot->local = indexfile;
-
- /* If there is data present from a previous transfer attempt,
- resume where it left off */
- prev_posn = ftell(indexfile);
- if (prev_posn>0) {
- if (push_verbosely)
- fprintf(stderr,
- "Resuming fetch of index for pack %s at byte %ld\n",
- hex, prev_posn);
- sprintf(range, "Range: bytes=%ld-", prev_posn);
- range_header = curl_slist_append(range_header, range);
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
- }
-
- if (start_active_slot(slot)) {
- run_active_slot(slot);
- if (results.curl_result != CURLE_OK) {
- free(url);
- fclose(indexfile);
- return error("Unable to get pack index %s\n%s", url,
- curl_errorstr);
- }
- } else {
- free(url);
- fclose(indexfile);
- return error("Unable to start request");
- }
+ if (http_get_file(url, filename, 0) != HTTP_OK)
+ ret = error("Unable to get pack index %s\n", url);
+ free(filename);
free(url);
- fclose(indexfile);
-
- return move_temp_to_file(tmpfile, filename);
+ return ret;
}
static int setup_index(unsigned char *sha1)
--
1.6.1.141.gb32a