X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=http-fetch.c;h=e537591edb1853a62c4bc9d77c47bae5fce082e4;hb=d06b689a933f6d2130f8afdf1ac0ddb83eeb59ab;hp=0b01877b5c1b564b1fe65b7a960a73829a6479d6;hpb=1d389ab65dc6867d3059d60bb2b3951bc01a185e;p=git.git diff --git a/http-fetch.c b/http-fetch.c index 0b01877b..e537591e 100644 --- a/http-fetch.c +++ b/http-fetch.c @@ -6,7 +6,10 @@ #include #include +#if LIBCURL_VERSION_NUM >= 0x070908 +#define USE_CURL_MULTI #define DEFAULT_MAX_REQUESTS 5 +#endif #if LIBCURL_VERSION_NUM < 0x070704 #define curl_global_cleanup() do { /* nothing */ } while(0) @@ -18,12 +21,15 @@ #define PREV_BUF_SIZE 4096 #define RANGE_HEADER_SIZE 30 -static int max_requests = DEFAULT_MAX_REQUESTS; static int active_requests = 0; static int data_received; +#ifdef USE_CURL_MULTI +static int max_requests = DEFAULT_MAX_REQUESTS; static CURLM *curlm; +#endif static CURL *curl_default; +static struct curl_slist *pragma_header; static struct curl_slist *no_pragma_header; static struct curl_slist *no_range_header; static char curl_errorstr[CURL_ERROR_SIZE]; @@ -132,32 +138,17 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, return size; } -int relink_or_rename(char *old, char *new) { - int ret; - - ret = link(old, new); - if (ret < 0) { - /* Same Coda hack as in write_sha1_file(sha1_file.c) */ - ret = errno; - if (ret == EXDEV && !rename(old, new)) - return 0; - } - unlink(old); - if (ret) { - if (ret != EEXIST) - return ret; - } - - return 0; -} - +#ifdef USE_CURL_MULTI void process_curl_messages(); void process_request_queue(); +#endif struct active_request_slot *get_active_slot() { struct active_request_slot *slot = active_queue_head; struct active_request_slot *newslot; + +#ifdef USE_CURL_MULTI int num_transfers; /* Wait for a slot to open up if the queue is full */ @@ -167,6 +158,7 @@ struct active_request_slot *get_active_slot() process_curl_messages(); } } +#endif while (slot != NULL && slot->in_use) { slot = slot->next; @@ -193,7 +185,7 @@ struct active_request_slot *get_active_slot() slot->in_use = 1; slot->done = 0; slot->local = NULL; - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header); curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); @@ -202,6 +194,7 @@ struct active_request_slot *get_active_slot() int start_active_slot(struct active_request_slot *slot) { +#ifdef USE_CURL_MULTI CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl); if (curlm_result != CURLM_OK && @@ -210,12 +203,13 @@ int start_active_slot(struct active_request_slot *slot) slot->in_use = 0; return 0; } - +#endif return 1; } void run_active_slot(struct active_request_slot *slot) { +#ifdef USE_CURL_MULTI int num_transfers; long last_pos = 0; long current_pos; @@ -255,6 +249,10 @@ void run_active_slot(struct active_request_slot *slot) &excfds, &select_timeout); } } +#else + slot->curl_result = curl_easy_perform(slot->curl); + active_requests--; +#endif } void start_request(struct transfer_request *request) @@ -278,6 +276,20 @@ void start_request(struct transfer_request *request) request->local = open(request->tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0666); + /* This could have failed due to the "lazy directory creation"; + * try to mkdir the last path component. + */ + if (request->local < 0 && errno == ENOENT) { + char *dir = strrchr(request->tmpfile, '/'); + if (dir) { + *dir = 0; + mkdir(request->tmpfile, 0777); + *dir = '/'; + } + request->local = open(request->tmpfile, + O_WRONLY | O_CREAT | O_EXCL, 0666); + } + if (request->local < 0) { request->state = ABORTED; error("Couldn't create temporary file %s for %s: %s\n", @@ -342,6 +354,7 @@ void start_request(struct transfer_request *request) curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr); curl_easy_setopt(slot->curl, CURLOPT_URL, url); + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); /* If we have successfully processed data from a previous fetch attempt, only fetch the data we don't already have. */ @@ -356,7 +369,7 @@ void start_request(struct transfer_request *request) CURLOPT_HTTPHEADER, range_header); } - /* Try to add to multi handle, abort the request on error */ + /* Try to get the request started, abort the request on error */ if (!start_active_slot(slot)) { request->state = ABORTED; close(request->local); @@ -390,7 +403,7 @@ void finish_request(struct transfer_request *request) return; } request->rename = - relink_or_rename(request->tmpfile, request->filename); + move_temp_to_file(request->tmpfile, request->filename); if (request->rename == 0) pull_say("got %s\n", sha1_to_hex(request->sha1)); @@ -413,6 +426,7 @@ void release_request(struct transfer_request *request) free(request); } +#ifdef USE_CURL_MULTI void process_curl_messages() { int num_messages; @@ -472,12 +486,16 @@ void process_request_queue() while (active_requests < max_requests && request != NULL) { if (request->state == WAITING) { - start_request(request); + if (has_sha1_file(request->sha1)) + release_request(request); + else + start_request(request); curl_multi_perform(curlm, &num_transfers); } request = request->next; } } +#endif void prefetch(unsigned char *sha1) { @@ -505,19 +523,18 @@ void prefetch(unsigned char *sha1) } tail->next = newreq; } +#ifdef USE_CURL_MULTI process_request_queue(); process_curl_messages(); +#endif } -static int got_alternates = 0; - static int fetch_index(struct alt_base *repo, unsigned char *sha1) { char *hex = sha1_to_hex(sha1); char *filename; char *url; char tmpfile[PATH_MAX]; - int ret; long prev_posn = 0; char range[RANGE_HEADER_SIZE]; struct curl_slist *range_header = NULL; @@ -545,6 +562,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1) 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, @@ -573,12 +591,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1) fclose(indexfile); - ret = relink_or_rename(tmpfile, filename); - if (ret) - return error("unable to write index filename %s: %s", - filename, strerror(ret)); - - return 0; + return move_temp_to_file(tmpfile, filename); } static int setup_index(struct alt_base *repo, unsigned char *sha1) @@ -607,8 +620,7 @@ static int fetch_alternates(char *base) struct alt_base *tail = alt; struct active_request_slot *slot; - if (got_alternates) - return 0; + data = xmalloc(4096); buffer.size = 4095; buffer.posn = 0; @@ -706,7 +718,6 @@ static int fetch_alternates(char *base) } i = posn + 1; } - got_alternates = 1; return ret; } @@ -814,6 +825,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1) curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile); 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 = packfile; /* If there is data present from a previous transfer attempt, @@ -842,10 +854,9 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1) fclose(packfile); - ret = relink_or_rename(tmpfile, filename); + ret = move_temp_to_file(tmpfile, filename); if (ret) - return error("unable to write pack filename %s: %s", - filename, strerror(ret)); + return ret; lst = &repo->packs; while (*lst != target) @@ -864,13 +875,19 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1) char *hex = sha1_to_hex(sha1); int ret; struct transfer_request *request = request_queue_head; - int num_transfers; while (request != NULL && memcmp(request->sha1, sha1, 20)) request = request->next; if (request == NULL) return error("Couldn't find request for %s in the queue", hex); + if (has_sha1_file(request->sha1)) { + release_request(request); + return 0; + } + +#ifdef USE_CURL_MULTI + int num_transfers; while (request->state == WAITING) { curl_multi_perform(curlm, &num_transfers); if (num_transfers < active_requests) { @@ -878,9 +895,30 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1) process_request_queue(); } } +#else + start_request(request); +#endif - if (request->state == ACTIVE) + while (request->state == ACTIVE) { run_active_slot(request->slot); +#ifndef USE_CURL_MULTI + request->curl_result = request->slot->curl_result; + curl_easy_getinfo(request->slot->curl, + CURLINFO_HTTP_CODE, + &request->http_code); + request->slot = NULL; + + /* Use alternates if necessary */ + if (request->http_code == 404 && + request->repo->next != NULL) { + request->repo = request->repo->next; + start_request(request); + } else { + finish_request(request); + request->state = COMPLETE; + } +#endif + } if (request->state == ABORTED) { release_request(request); @@ -992,11 +1030,6 @@ int main(int argc, char **argv) arg++; } else if (!strcmp(argv[arg], "--recover")) { get_recover = 1; - } else if (argv[arg][1] == 'r') { - max_requests = atoi(argv[arg + 1]); - if (max_requests < 1) - max_requests = DEFAULT_MAX_REQUESTS; - arg++; } arg++; } @@ -1009,11 +1042,20 @@ int main(int argc, char **argv) curl_global_init(CURL_GLOBAL_ALL); +#ifdef USE_CURL_MULTI + char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS"); + if (http_max_requests != NULL) + max_requests = atoi(http_max_requests); + if (max_requests < 1) + max_requests = DEFAULT_MAX_REQUESTS; + curlm = curl_multi_init(); if (curlm == NULL) { fprintf(stderr, "Error creating curl multi handle.\n"); return 1; } +#endif + pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache"); no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); no_range_header = curl_slist_append(no_range_header, "Range:"); @@ -1053,6 +1095,7 @@ int main(int argc, char **argv) if (pull(commit_id)) return 1; + curl_slist_free_all(pragma_header); curl_slist_free_all(no_pragma_header); curl_slist_free_all(no_range_header); curl_easy_cleanup(curl_default); @@ -1061,7 +1104,9 @@ int main(int argc, char **argv) curl_easy_cleanup(slot->curl); slot = slot->next; } +#ifdef USE_CURL_MULTI curl_multi_cleanup(curlm); +#endif curl_global_cleanup(); return 0; }