6 #define PREV_BUF_SIZE 4096
12 struct packed_git *packs;
13 struct alt_base *next;
16 enum object_request_state {
25 struct walker *walker;
26 unsigned char sha1[20];
27 struct alt_base *repo;
29 char filename[PATH_MAX];
30 char tmpfile[PATH_MAX];
32 enum object_request_state state;
34 char errorstr[CURL_ERROR_SIZE];
36 unsigned char real_sha1[20];
41 struct active_request_slot *slot;
42 struct object_request *next;
45 struct alternates_request {
46 struct walker *walker;
49 struct strbuf *buffer;
50 struct active_request_slot *slot;
60 static struct object_request *object_queue_head;
62 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
65 unsigned char expn[4096];
66 size_t size = eltsize * nmemb;
68 struct object_request *obj_req = (struct object_request *)data;
70 ssize_t retval = xwrite(obj_req->local,
71 (char *) ptr + posn, size - posn);
75 } while (posn < size);
77 obj_req->stream.avail_in = size;
78 obj_req->stream.next_in = ptr;
80 obj_req->stream.next_out = expn;
81 obj_req->stream.avail_out = sizeof(expn);
82 obj_req->zret = git_inflate(&obj_req->stream, Z_SYNC_FLUSH);
83 git_SHA1_Update(&obj_req->c, expn,
84 sizeof(expn) - obj_req->stream.avail_out);
85 } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
90 static void fetch_alternates(struct walker *walker, const char *base);
92 static void process_object_response(void *callback_data);
94 static void start_object_request(struct walker *walker,
95 struct object_request *obj_req)
97 char *hex = sha1_to_hex(obj_req->sha1);
98 char prevfile[PATH_MAX];
102 unsigned char prev_buf[PREV_BUF_SIZE];
103 ssize_t prev_read = 0;
105 char range[RANGE_HEADER_SIZE];
106 struct curl_slist *range_header = NULL;
107 struct active_request_slot *slot;
109 snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
110 unlink_or_warn(prevfile);
111 rename(obj_req->tmpfile, prevfile);
112 unlink_or_warn(obj_req->tmpfile);
114 if (obj_req->local != -1)
115 error("fd leakage in start: %d", obj_req->local);
116 obj_req->local = open(obj_req->tmpfile,
117 O_WRONLY | O_CREAT | O_EXCL, 0666);
119 * This could have failed due to the "lazy directory creation";
120 * try to mkdir the last path component.
122 if (obj_req->local < 0 && errno == ENOENT) {
123 char *dir = strrchr(obj_req->tmpfile, '/');
126 mkdir(obj_req->tmpfile, 0777);
129 obj_req->local = open(obj_req->tmpfile,
130 O_WRONLY | O_CREAT | O_EXCL, 0666);
133 if (obj_req->local < 0) {
134 obj_req->state = ABORTED;
135 error("Couldn't create temporary file %s for %s: %s",
136 obj_req->tmpfile, obj_req->filename, strerror(errno));
140 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
142 git_inflate_init(&obj_req->stream);
144 git_SHA1_Init(&obj_req->c);
146 url = xmalloc(strlen(obj_req->repo->base) + 51);
147 obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51);
148 strcpy(url, obj_req->repo->base);
149 posn = url + strlen(obj_req->repo->base);
150 strcpy(posn, "/objects/");
152 memcpy(posn, hex, 2);
155 strcpy(posn, hex + 2);
156 strcpy(obj_req->url, url);
159 * If a previous temp file is present, process what was already
162 prevlocal = open(prevfile, O_RDONLY);
163 if (prevlocal != -1) {
165 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
167 if (fwrite_sha1_file(prev_buf,
170 obj_req) == prev_read)
171 prev_posn += prev_read;
175 } while (prev_read > 0);
178 unlink_or_warn(prevfile);
181 * Reset inflate/SHA1 if there was an error reading the previous temp
182 * file; also rewind to the beginning of the local file.
184 if (prev_read == -1) {
185 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
186 git_inflate_init(&obj_req->stream);
187 git_SHA1_Init(&obj_req->c);
190 lseek(obj_req->local, 0, SEEK_SET);
191 ftruncate(obj_req->local, 0);
195 slot = get_active_slot();
196 slot->callback_func = process_object_response;
197 slot->callback_data = obj_req;
198 obj_req->slot = slot;
200 curl_easy_setopt(slot->curl, CURLOPT_FILE, obj_req);
201 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
202 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr);
203 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
204 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
207 * If we have successfully processed data from a previous fetch
208 * attempt, only fetch the data we don't already have.
211 if (walker->get_verbosely)
213 "Resuming fetch of object %s at byte %ld\n",
215 sprintf(range, "Range: bytes=%ld-", prev_posn);
216 range_header = curl_slist_append(range_header, range);
217 curl_easy_setopt(slot->curl,
218 CURLOPT_HTTPHEADER, range_header);
221 /* Try to get the request started, abort the request on error */
222 obj_req->state = ACTIVE;
223 if (!start_active_slot(slot)) {
224 obj_req->state = ABORTED;
225 obj_req->slot = NULL;
226 close(obj_req->local);
233 static void finish_object_request(struct object_request *obj_req)
237 close(obj_req->local);
240 if (obj_req->http_code == 416) {
241 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
242 } else if (obj_req->curl_result != CURLE_OK) {
243 if (stat(obj_req->tmpfile, &st) == 0)
245 unlink_or_warn(obj_req->tmpfile);
249 git_inflate_end(&obj_req->stream);
250 git_SHA1_Final(obj_req->real_sha1, &obj_req->c);
251 if (obj_req->zret != Z_STREAM_END) {
252 unlink_or_warn(obj_req->tmpfile);
255 if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
256 unlink_or_warn(obj_req->tmpfile);
260 move_temp_to_file(obj_req->tmpfile, obj_req->filename);
262 if (obj_req->rename == 0)
263 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
266 static void process_object_response(void *callback_data)
268 struct object_request *obj_req =
269 (struct object_request *)callback_data;
270 struct walker *walker = obj_req->walker;
271 struct walker_data *data = walker->data;
272 struct alt_base *alt = data->alt;
274 obj_req->curl_result = obj_req->slot->curl_result;
275 obj_req->http_code = obj_req->slot->http_code;
276 obj_req->slot = NULL;
277 obj_req->state = COMPLETE;
279 /* Use alternates if necessary */
280 if (missing_target(obj_req)) {
281 fetch_alternates(walker, alt->base);
282 if (obj_req->repo->next != NULL) {
285 close(obj_req->local);
287 start_object_request(walker, obj_req);
292 finish_object_request(obj_req);
295 static void release_object_request(struct object_request *obj_req)
297 struct object_request *entry = object_queue_head;
299 if (obj_req->local != -1)
300 error("fd leakage in release: %d", obj_req->local);
301 if (obj_req == object_queue_head) {
302 object_queue_head = obj_req->next;
304 while (entry->next != NULL && entry->next != obj_req)
306 if (entry->next == obj_req)
307 entry->next = entry->next->next;
314 #ifdef USE_CURL_MULTI
315 static int fill_active_slot(struct walker *walker)
317 struct object_request *obj_req;
319 for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
320 if (obj_req->state == WAITING) {
321 if (has_sha1_file(obj_req->sha1))
322 obj_req->state = COMPLETE;
324 start_object_request(walker, obj_req);
333 static void prefetch(struct walker *walker, unsigned char *sha1)
335 struct object_request *newreq;
336 struct object_request *tail;
337 struct walker_data *data = walker->data;
338 char *filename = sha1_file_name(sha1);
340 newreq = xmalloc(sizeof(*newreq));
341 newreq->walker = walker;
342 hashcpy(newreq->sha1, sha1);
343 newreq->repo = data->alt;
346 newreq->state = WAITING;
347 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
348 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
349 "%s.temp", filename);
353 http_is_verbose = walker->get_verbosely;
355 if (object_queue_head == NULL) {
356 object_queue_head = newreq;
358 tail = object_queue_head;
359 while (tail->next != NULL)
364 #ifdef USE_CURL_MULTI
370 static void process_alternates_response(void *callback_data)
372 struct alternates_request *alt_req =
373 (struct alternates_request *)callback_data;
374 struct walker *walker = alt_req->walker;
375 struct walker_data *cdata = walker->data;
376 struct active_request_slot *slot = alt_req->slot;
377 struct alt_base *tail = cdata->alt;
378 const char *base = alt_req->base;
379 static const char null_byte = '\0';
383 if (alt_req->http_specific) {
384 if (slot->curl_result != CURLE_OK ||
385 !alt_req->buffer->len) {
387 /* Try reusing the slot to get non-http alternates */
388 alt_req->http_specific = 0;
389 sprintf(alt_req->url, "%s/objects/info/alternates",
391 curl_easy_setopt(slot->curl, CURLOPT_URL,
395 if (slot->finished != NULL)
396 (*slot->finished) = 0;
397 if (!start_active_slot(slot)) {
398 cdata->got_alternates = -1;
400 if (slot->finished != NULL)
401 (*slot->finished) = 1;
405 } else if (slot->curl_result != CURLE_OK) {
406 if (!missing_target(slot)) {
407 cdata->got_alternates = -1;
412 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
413 alt_req->buffer->len--;
414 data = alt_req->buffer->buf;
416 while (i < alt_req->buffer->len) {
418 while (posn < alt_req->buffer->len && data[posn] != '\n')
420 if (data[posn] == '\n') {
423 struct alt_base *newalt;
425 if (data[i] == '/') {
428 * http://git.host/pub/scm/linux.git/
430 * so memcpy(dst, base, serverlen) will
431 * copy up to "...git.host".
433 const char *colon_ss = strstr(base,"://");
435 serverlen = (strchr(colon_ss + 3, '/')
439 } else if (!memcmp(data + i, "../", 3)) {
441 * Relative URL; chop the corresponding
442 * number of subpath from base (and ../
443 * from data), and concatenate the result.
445 * The code first drops ../ from data, and
446 * then drops one ../ from data and one path
447 * from base. IOW, one extra ../ is dropped
448 * from data than path is dropped from base.
450 * This is not wrong. The alternate in
451 * http://git.host/pub/scm/linux.git/
453 * http://git.host/pub/scm/linus.git/
454 * is ../../linus.git/objects/. You need
455 * two ../../ to borrow from your direct
459 serverlen = strlen(base);
460 while (i + 2 < posn &&
461 !memcmp(data + i, "../", 3)) {
464 } while (serverlen &&
465 base[serverlen - 1] != '/');
468 /* If the server got removed, give up. */
469 okay = strchr(base, ':') - base + 3 <
471 } else if (alt_req->http_specific) {
472 char *colon = strchr(data + i, ':');
473 char *slash = strchr(data + i, '/');
474 if (colon && slash && colon < data + posn &&
475 slash < data + posn && colon < slash) {
479 /* skip "objects\n" at end */
481 target = xmalloc(serverlen + posn - i - 6);
482 memcpy(target, base, serverlen);
483 memcpy(target + serverlen, data + i,
485 target[serverlen + posn - i - 7] = 0;
486 if (walker->get_verbosely)
488 "Also look at %s\n", target);
489 newalt = xmalloc(sizeof(*newalt));
491 newalt->base = target;
492 newalt->got_indices = 0;
493 newalt->packs = NULL;
495 while (tail->next != NULL)
503 cdata->got_alternates = 1;
506 static void fetch_alternates(struct walker *walker, const char *base)
508 struct strbuf buffer = STRBUF_INIT;
510 struct active_request_slot *slot;
511 struct alternates_request alt_req;
512 struct walker_data *cdata = walker->data;
515 * If another request has already started fetching alternates,
516 * wait for them to arrive and return to processing this request's
519 #ifdef USE_CURL_MULTI
520 while (cdata->got_alternates == 0) {
525 /* Nothing to do if they've already been fetched */
526 if (cdata->got_alternates == 1)
529 /* Start the fetch */
530 cdata->got_alternates = 0;
532 if (walker->get_verbosely)
533 fprintf(stderr, "Getting alternates list for %s\n", base);
535 url = xmalloc(strlen(base) + 31);
536 sprintf(url, "%s/objects/info/http-alternates", base);
539 * Use a callback to process the result, since another request
540 * may fail and need to have alternates loaded before continuing
542 slot = get_active_slot();
543 slot->callback_func = process_alternates_response;
544 alt_req.walker = walker;
545 slot->callback_data = &alt_req;
547 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
548 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
549 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
553 alt_req.buffer = &buffer;
554 alt_req.http_specific = 1;
557 if (start_active_slot(slot))
558 run_active_slot(slot);
560 cdata->got_alternates = -1;
562 strbuf_release(&buffer);
566 static int fetch_indices(struct walker *walker, struct alt_base *repo)
570 if (repo->got_indices)
573 if (walker->get_verbosely)
574 fprintf(stderr, "Getting pack list for %s\n", repo->base);
576 switch (http_get_info_packs(repo->base, &repo->packs)) {
578 case HTTP_MISSING_TARGET:
579 repo->got_indices = 1;
583 repo->got_indices = 0;
590 static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
592 struct packed_git *target;
594 struct slot_results results;
595 struct http_pack_request *preq;
597 if (fetch_indices(walker, repo))
599 target = find_sha1_pack(sha1, repo->packs);
603 if (walker->get_verbosely) {
604 fprintf(stderr, "Getting pack %s\n",
605 sha1_to_hex(target->sha1));
606 fprintf(stderr, " which contains %s\n",
610 preq = new_http_pack_request(target, repo->base);
613 preq->lst = &repo->packs;
614 preq->slot->results = &results;
616 if (start_active_slot(preq->slot)) {
617 run_active_slot(preq->slot);
618 if (results.curl_result != CURLE_OK) {
619 error("Unable to get pack file %s\n%s", preq->url,
624 error("Unable to start request");
628 ret = finish_http_pack_request(preq);
629 release_http_pack_request(preq);
639 static void abort_object_request(struct object_request *obj_req)
641 if (obj_req->local >= 0) {
642 close(obj_req->local);
645 unlink_or_warn(obj_req->tmpfile);
647 release_active_slot(obj_req->slot);
648 obj_req->slot = NULL;
650 release_object_request(obj_req);
653 static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
655 char *hex = sha1_to_hex(sha1);
657 struct object_request *obj_req = object_queue_head;
659 while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
660 obj_req = obj_req->next;
662 return error("Couldn't find request for %s in the queue", hex);
664 if (has_sha1_file(obj_req->sha1)) {
665 abort_object_request(obj_req);
669 #ifdef USE_CURL_MULTI
670 while (obj_req->state == WAITING)
673 start_object_request(walker, obj_req);
676 while (obj_req->state == ACTIVE)
677 run_active_slot(obj_req->slot);
679 if (obj_req->local != -1) {
680 close(obj_req->local);
684 if (obj_req->state == ABORTED) {
685 ret = error("Request for %s aborted", hex);
686 } else if (obj_req->curl_result != CURLE_OK &&
687 obj_req->http_code != 416) {
688 if (missing_target(obj_req))
689 ret = -1; /* Be silent, it is probably in a pack. */
691 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
692 obj_req->errorstr, obj_req->curl_result,
693 obj_req->http_code, hex);
694 } else if (obj_req->zret != Z_STREAM_END) {
695 walker->corrupt_object_found++;
696 ret = error("File %s (%s) corrupt", hex, obj_req->url);
697 } else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
698 ret = error("File %s has bad hash", hex);
699 } else if (obj_req->rename < 0) {
700 ret = error("unable to write sha1 filename %s",
704 release_object_request(obj_req);
708 static int fetch(struct walker *walker, unsigned char *sha1)
710 struct walker_data *data = walker->data;
711 struct alt_base *altbase = data->alt;
713 if (!fetch_object(walker, altbase, sha1))
716 if (!fetch_pack(walker, altbase, sha1))
718 fetch_alternates(walker, data->alt->base);
719 altbase = altbase->next;
721 return error("Unable to find %s under %s", sha1_to_hex(sha1),
725 static int fetch_ref(struct walker *walker, struct ref *ref)
727 struct walker_data *data = walker->data;
728 return http_fetch_ref(data->alt->base, ref);
731 static void cleanup(struct walker *walker)
736 struct walker *get_http_walker(const char *url, struct remote *remote)
739 struct walker_data *data = xmalloc(sizeof(struct walker_data));
740 struct walker *walker = xmalloc(sizeof(struct walker));
744 data->alt = xmalloc(sizeof(*data->alt));
745 data->alt->base = xmalloc(strlen(url) + 1);
746 strcpy(data->alt->base, url);
747 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
750 data->alt->got_indices = 0;
751 data->alt->packs = NULL;
752 data->alt->next = NULL;
753 data->got_alternates = -1;
755 walker->corrupt_object_found = 0;
756 walker->fetch = fetch;
757 walker->fetch_ref = fetch_ref;
758 walker->prefetch = prefetch;
759 walker->cleanup = cleanup;
762 #ifdef USE_CURL_MULTI
763 add_fill_function(walker, (int (*)(void *)) fill_active_slot);