#include <curl/curl.h>
#include <curl/easy.h>
+#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)
#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];
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 */
process_curl_messages();
}
}
+#endif
while (slot != NULL && slot->in_use) {
slot = slot->next;
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);
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 &&
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;
&excfds, &select_timeout);
}
}
+#else
+ slot->curl_result = curl_easy_perform(slot->curl);
+ active_requests--;
+#endif
}
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",
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. */
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);
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));
free(request);
}
+#ifdef USE_CURL_MULTI
void process_curl_messages()
{
int num_messages;
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)
{
}
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;
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,
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)
struct alt_base *tail = alt;
struct active_request_slot *slot;
- if (got_alternates)
- return 0;
+
data = xmalloc(4096);
buffer.size = 4095;
buffer.posn = 0;
}
i = posn + 1;
}
- got_alternates = 1;
return ret;
}
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,
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)
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) {
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);
alt->base);
}
+static inline int needs_quote(int ch)
+{
+ switch (ch) {
+ case '/': case '-': case '.':
+ case 'A'...'Z': case 'a'...'z': case '0'...'9':
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+static inline int hex(int v)
+{
+ if (v < 10) return '0' + v;
+ else return 'A' + v - 10;
+}
+
+static char *quote_ref_url(const char *base, const char *ref)
+{
+ const char *cp;
+ char *dp, *qref;
+ int len, baselen, ch;
+
+ baselen = strlen(base);
+ len = baselen + 6; /* "refs/" + NUL */
+ for (cp = ref; (ch = *cp) != 0; cp++, len++)
+ if (needs_quote(ch))
+ len += 2; /* extra two hex plus replacement % */
+ qref = xmalloc(len);
+ memcpy(qref, base, baselen);
+ memcpy(qref + baselen, "refs/", 5);
+ for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
+ if (needs_quote(ch)) {
+ *dp++ = '%';
+ *dp++ = hex((ch >> 4) & 0xF);
+ *dp++ = hex(ch & 0xF);
+ }
+ else
+ *dp++ = ch;
+ }
+ *dp = 0;
+
+ return qref;
+}
+
int fetch_ref(char *ref, unsigned char *sha1)
{
- char *url, *posn;
+ char *url;
char hex[42];
struct buffer buffer;
char *base = alt->base;
buffer.buffer = hex;
hex[41] = '\0';
- url = xmalloc(strlen(base) + 6 + strlen(ref));
- strcpy(url, base);
- posn = url + strlen(base);
- strcpy(posn, "refs/");
- posn += 5;
- strcpy(posn, ref);
-
+ url = quote_ref_url(base, ref);
slot = get_active_slot();
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
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++;
}
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:");
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);
curl_easy_cleanup(slot->curl);
slot = slot->next;
}
+#ifdef USE_CURL_MULTI
curl_multi_cleanup(curlm);
+#endif
curl_global_cleanup();
return 0;
}