]> git.ipfire.org Git - thirdparty/git.git/blame - http-walker.c
autoconf: Add tests for memmem, strtoumax and mkdtemp functions
[thirdparty/git.git] / http-walker.c
CommitLineData
6eb7ed54
DB
1#include "cache.h"
2#include "commit.h"
271421cd 3#include "pack.h"
30ae764b 4#include "walker.h"
29508e1e 5#include "http.h"
7baa3e86 6
49a0f240
NH
7#define PREV_BUF_SIZE 4096
8#define RANGE_HEADER_SIZE 30
9
b3661567
DB
10struct alt_base
11{
2afea3bc 12 char *base;
b3661567
DB
13 int got_indices;
14 struct packed_git *packs;
15 struct alt_base *next;
16};
17
e388ab74 18enum object_request_state {
1d389ab6
NH
19 WAITING,
20 ABORTED,
21 ACTIVE,
22 COMPLETE,
23};
6eb7ed54 24
e388ab74 25struct object_request
1d389ab6 26{
30ae764b 27 struct walker *walker;
1d389ab6
NH
28 unsigned char sha1[20];
29 struct alt_base *repo;
30 char *url;
31 char filename[PATH_MAX];
32 char tmpfile[PATH_MAX];
33 int local;
e388ab74 34 enum object_request_state state;
1d389ab6
NH
35 CURLcode curl_result;
36 char errorstr[CURL_ERROR_SIZE];
37 long http_code;
38 unsigned char real_sha1[20];
39 SHA_CTX c;
40 z_stream stream;
41 int zret;
42 int rename;
43 struct active_request_slot *slot;
e388ab74 44 struct object_request *next;
1d389ab6
NH
45};
46
e388ab74 47struct alternates_request {
30ae764b 48 struct walker *walker;
8e29f6a0 49 const char *base;
acc075a8
NH
50 char *url;
51 struct buffer *buffer;
52 struct active_request_slot *slot;
53 int http_specific;
54};
55
30ae764b
DB
56struct walker_data {
57 const char *url;
58 int got_alternates;
59 struct alt_base *alt;
60 struct curl_slist *no_pragma_header;
61};
62
96f1e58f 63static struct object_request *object_queue_head;
bc8f2652 64
182005b9
DB
65static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
66 void *data)
67{
bf0f910d 68 unsigned char expn[4096];
6eb7ed54
DB
69 size_t size = eltsize * nmemb;
70 int posn = 0;
e388ab74 71 struct object_request *obj_req = (struct object_request *)data;
6eb7ed54 72 do {
93822c22 73 ssize_t retval = xwrite(obj_req->local,
1d7f171c 74 (char *) ptr + posn, size - posn);
6eb7ed54
DB
75 if (retval < 0)
76 return posn;
77 posn += retval;
78 } while (posn < size);
79
e388ab74
NH
80 obj_req->stream.avail_in = size;
81 obj_req->stream.next_in = ptr;
6eb7ed54 82 do {
e388ab74
NH
83 obj_req->stream.next_out = expn;
84 obj_req->stream.avail_out = sizeof(expn);
85 obj_req->zret = inflate(&obj_req->stream, Z_SYNC_FLUSH);
86 SHA1_Update(&obj_req->c, expn,
87 sizeof(expn) - obj_req->stream.avail_out);
88 } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
1d389ab6 89 data_received++;
6eb7ed54
DB
90 return size;
91}
92
be4a015b
JH
93static int missing__target(int code, int result)
94{
95 return /* file:// URL -- do we ever use one??? */
96 (result == CURLE_FILE_COULDNT_READ_FILE) ||
97 /* http:// and https:// URL */
4adffc7b
JH
98 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
99 /* ftp:// URL */
100 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
be4a015b
JH
101 ;
102}
103
104#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
105
30ae764b 106static void fetch_alternates(struct walker *walker, const char *base);
1d389ab6 107
29508e1e 108static void process_object_response(void *callback_data);
1d389ab6 109
30ae764b
DB
110static void start_object_request(struct walker *walker,
111 struct object_request *obj_req)
1d389ab6 112{
e388ab74 113 char *hex = sha1_to_hex(obj_req->sha1);
1d389ab6
NH
114 char prevfile[PATH_MAX];
115 char *url;
116 char *posn;
117 int prevlocal;
118 unsigned char prev_buf[PREV_BUF_SIZE];
119 ssize_t prev_read = 0;
120 long prev_posn = 0;
121 char range[RANGE_HEADER_SIZE];
122 struct curl_slist *range_header = NULL;
123 struct active_request_slot *slot;
30ae764b 124 struct walker_data *data = walker->data;
1d389ab6 125
e388ab74 126 snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
1d389ab6 127 unlink(prevfile);
e388ab74
NH
128 rename(obj_req->tmpfile, prevfile);
129 unlink(obj_req->tmpfile);
1d389ab6 130
e388ab74
NH
131 if (obj_req->local != -1)
132 error("fd leakage in start: %d", obj_req->local);
133 obj_req->local = open(obj_req->tmpfile,
1d389ab6 134 O_WRONLY | O_CREAT | O_EXCL, 0666);
b721e01f
JH
135 /* This could have failed due to the "lazy directory creation";
136 * try to mkdir the last path component.
137 */
e388ab74
NH
138 if (obj_req->local < 0 && errno == ENOENT) {
139 char *dir = strrchr(obj_req->tmpfile, '/');
b721e01f
JH
140 if (dir) {
141 *dir = 0;
e388ab74 142 mkdir(obj_req->tmpfile, 0777);
b721e01f
JH
143 *dir = '/';
144 }
e388ab74 145 obj_req->local = open(obj_req->tmpfile,
b721e01f
JH
146 O_WRONLY | O_CREAT | O_EXCL, 0666);
147 }
148
e388ab74
NH
149 if (obj_req->local < 0) {
150 obj_req->state = ABORTED;
bd2afde8 151 error("Couldn't create temporary file %s for %s: %s",
e388ab74 152 obj_req->tmpfile, obj_req->filename, strerror(errno));
1d389ab6
NH
153 return;
154 }
155
e388ab74 156 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
1d389ab6 157
e388ab74 158 inflateInit(&obj_req->stream);
1d389ab6 159
e388ab74 160 SHA1_Init(&obj_req->c);
1d389ab6 161
2afea3bc
GP
162 url = xmalloc(strlen(obj_req->repo->base) + 51);
163 obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51);
e388ab74
NH
164 strcpy(url, obj_req->repo->base);
165 posn = url + strlen(obj_req->repo->base);
2afea3bc
GP
166 strcpy(posn, "/objects/");
167 posn += 9;
1d389ab6
NH
168 memcpy(posn, hex, 2);
169 posn += 2;
170 *(posn++) = '/';
171 strcpy(posn, hex + 2);
e388ab74 172 strcpy(obj_req->url, url);
1d389ab6
NH
173
174 /* If a previous temp file is present, process what was already
175 fetched. */
176 prevlocal = open(prevfile, O_RDONLY);
177 if (prevlocal != -1) {
178 do {
93d26e4c 179 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1d389ab6
NH
180 if (prev_read>0) {
181 if (fwrite_sha1_file(prev_buf,
182 1,
183 prev_read,
e388ab74 184 obj_req) == prev_read) {
1d389ab6
NH
185 prev_posn += prev_read;
186 } else {
187 prev_read = -1;
188 }
189 }
190 } while (prev_read > 0);
191 close(prevlocal);
192 }
193 unlink(prevfile);
194
195 /* Reset inflate/SHA1 if there was an error reading the previous temp
196 file; also rewind to the beginning of the local file. */
197 if (prev_read == -1) {
e388ab74
NH
198 memset(&obj_req->stream, 0, sizeof(obj_req->stream));
199 inflateInit(&obj_req->stream);
200 SHA1_Init(&obj_req->c);
1d389ab6
NH
201 if (prev_posn>0) {
202 prev_posn = 0;
b5da2467 203 lseek(obj_req->local, 0, SEEK_SET);
e388ab74 204 ftruncate(obj_req->local, 0);
1d389ab6
NH
205 }
206 }
207
208 slot = get_active_slot();
29508e1e 209 slot->callback_func = process_object_response;
e388ab74
NH
210 slot->callback_data = obj_req;
211 obj_req->slot = slot;
29508e1e 212
e388ab74 213 curl_easy_setopt(slot->curl, CURLOPT_FILE, obj_req);
1d389ab6 214 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
e388ab74 215 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr);
1d389ab6 216 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
30ae764b 217 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
1d389ab6
NH
218
219 /* If we have successfully processed data from a previous fetch
220 attempt, only fetch the data we don't already have. */
221 if (prev_posn>0) {
30ae764b 222 if (walker->get_verbosely)
1d389ab6
NH
223 fprintf(stderr,
224 "Resuming fetch of object %s at byte %ld\n",
225 hex, prev_posn);
226 sprintf(range, "Range: bytes=%ld-", prev_posn);
227 range_header = curl_slist_append(range_header, range);
228 curl_easy_setopt(slot->curl,
229 CURLOPT_HTTPHEADER, range_header);
230 }
231
a7a8d378 232 /* Try to get the request started, abort the request on error */
e388ab74 233 obj_req->state = ACTIVE;
1d389ab6 234 if (!start_active_slot(slot)) {
e388ab74
NH
235 obj_req->state = ABORTED;
236 obj_req->slot = NULL;
237 close(obj_req->local); obj_req->local = -1;
238 free(obj_req->url);
239 return;
1d389ab6 240 }
1d389ab6
NH
241}
242
e388ab74 243static void finish_object_request(struct object_request *obj_req)
1d389ab6 244{
50496b21
NH
245 struct stat st;
246
e388ab74
NH
247 fchmod(obj_req->local, 0444);
248 close(obj_req->local); obj_req->local = -1;
1d389ab6 249
e388ab74 250 if (obj_req->http_code == 416) {
1d389ab6 251 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
e388ab74
NH
252 } else if (obj_req->curl_result != CURLE_OK) {
253 if (stat(obj_req->tmpfile, &st) == 0)
50496b21 254 if (st.st_size == 0)
e388ab74 255 unlink(obj_req->tmpfile);
1d389ab6
NH
256 return;
257 }
258
e388ab74
NH
259 inflateEnd(&obj_req->stream);
260 SHA1_Final(obj_req->real_sha1, &obj_req->c);
261 if (obj_req->zret != Z_STREAM_END) {
262 unlink(obj_req->tmpfile);
1d389ab6
NH
263 return;
264 }
a89fccd2 265 if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
e388ab74 266 unlink(obj_req->tmpfile);
1d389ab6
NH
267 return;
268 }
e388ab74
NH
269 obj_req->rename =
270 move_temp_to_file(obj_req->tmpfile, obj_req->filename);
1d389ab6 271
e388ab74 272 if (obj_req->rename == 0)
30ae764b 273 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
1d389ab6
NH
274}
275
29508e1e
NH
276static void process_object_response(void *callback_data)
277{
e388ab74
NH
278 struct object_request *obj_req =
279 (struct object_request *)callback_data;
30ae764b
DB
280 struct walker *walker = obj_req->walker;
281 struct walker_data *data = walker->data;
282 struct alt_base *alt = data->alt;
29508e1e 283
e388ab74
NH
284 obj_req->curl_result = obj_req->slot->curl_result;
285 obj_req->http_code = obj_req->slot->http_code;
286 obj_req->slot = NULL;
287 obj_req->state = COMPLETE;
29508e1e
NH
288
289 /* Use alternates if necessary */
be4a015b 290 if (missing_target(obj_req)) {
30ae764b 291 fetch_alternates(walker, alt->base);
e388ab74
NH
292 if (obj_req->repo->next != NULL) {
293 obj_req->repo =
294 obj_req->repo->next;
295 close(obj_req->local);
296 obj_req->local = -1;
30ae764b 297 start_object_request(walker, obj_req);
29508e1e
NH
298 return;
299 }
300 }
301
e388ab74 302 finish_object_request(obj_req);
29508e1e
NH
303}
304
e388ab74 305static void release_object_request(struct object_request *obj_req)
1d389ab6 306{
e388ab74 307 struct object_request *entry = object_queue_head;
1d389ab6 308
e388ab74
NH
309 if (obj_req->local != -1)
310 error("fd leakage in release: %d", obj_req->local);
311 if (obj_req == object_queue_head) {
312 object_queue_head = obj_req->next;
1d389ab6 313 } else {
e388ab74 314 while (entry->next != NULL && entry->next != obj_req)
1d389ab6 315 entry = entry->next;
e388ab74 316 if (entry->next == obj_req)
1d389ab6
NH
317 entry->next = entry->next->next;
318 }
319
e388ab74
NH
320 free(obj_req->url);
321 free(obj_req);
1d389ab6
NH
322}
323
a7a8d378 324#ifdef USE_CURL_MULTI
30ae764b 325static int fill_active_slot(struct walker *walker)
1d389ab6 326{
45c17412 327 struct object_request *obj_req;
1d389ab6 328
45c17412 329 for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
e388ab74
NH
330 if (obj_req->state == WAITING) {
331 if (has_sha1_file(obj_req->sha1))
09db444f 332 obj_req->state = COMPLETE;
45c17412 333 else {
30ae764b 334 start_object_request(walker, obj_req);
45c17412
DB
335 return 1;
336 }
f1a906a3 337 }
8fcf7f9a 338 }
45c17412 339 return 0;
1d389ab6 340}
a7a8d378 341#endif
1d389ab6 342
30ae764b 343static void prefetch(struct walker *walker, unsigned char *sha1)
1d389ab6 344{
e388ab74
NH
345 struct object_request *newreq;
346 struct object_request *tail;
30ae764b 347 struct walker_data *data = walker->data;
1d389ab6
NH
348 char *filename = sha1_file_name(sha1);
349
350 newreq = xmalloc(sizeof(*newreq));
30ae764b 351 newreq->walker = walker;
e702496e 352 hashcpy(newreq->sha1, sha1);
30ae764b 353 newreq->repo = data->alt;
1d389ab6
NH
354 newreq->url = NULL;
355 newreq->local = -1;
356 newreq->state = WAITING;
357 snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename);
358 snprintf(newreq->tmpfile, sizeof(newreq->tmpfile),
359 "%s.temp", filename);
e8dff6ba 360 newreq->slot = NULL;
1d389ab6
NH
361 newreq->next = NULL;
362
e388ab74
NH
363 if (object_queue_head == NULL) {
364 object_queue_head = newreq;
1d389ab6 365 } else {
e388ab74 366 tail = object_queue_head;
1d389ab6
NH
367 while (tail->next != NULL) {
368 tail = tail->next;
369 }
370 tail->next = newreq;
371 }
29508e1e 372
a7a8d378 373#ifdef USE_CURL_MULTI
29508e1e
NH
374 fill_active_slots();
375 step_active_slots();
a7a8d378 376#endif
1d389ab6
NH
377}
378
30ae764b 379static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b9 380{
1d389ab6 381 char *hex = sha1_to_hex(sha1);
182005b9
DB
382 char *filename;
383 char *url;
49a0f240 384 char tmpfile[PATH_MAX];
49a0f240
NH
385 long prev_posn = 0;
386 char range[RANGE_HEADER_SIZE];
387 struct curl_slist *range_header = NULL;
30ae764b 388 struct walker_data *data = walker->data;
182005b9
DB
389
390 FILE *indexfile;
1d389ab6 391 struct active_request_slot *slot;
cb754fdf 392 struct slot_results results;
182005b9
DB
393
394 if (has_pack_index(sha1))
395 return 0;
396
30ae764b 397 if (walker->get_verbosely)
1d389ab6 398 fprintf(stderr, "Getting index for pack %s\n", hex);
8fcf7f9a 399
b3661567 400 url = xmalloc(strlen(repo->base) + 64);
1d389ab6 401 sprintf(url, "%s/objects/pack/pack-%s.idx", repo->base, hex);
8fcf7f9a 402
182005b9 403 filename = sha1_pack_index_name(sha1);
49a0f240
NH
404 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
405 indexfile = fopen(tmpfile, "a");
182005b9
DB
406 if (!indexfile)
407 return error("Unable to open local file %s for pack index",
408 filename);
409
1d389ab6 410 slot = get_active_slot();
c8568e13 411 slot->results = &results;
1d389ab6
NH
412 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
413 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
414 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
30ae764b 415 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
1d389ab6
NH
416 slot->local = indexfile;
417
49a0f240
NH
418 /* If there is data present from a previous transfer attempt,
419 resume where it left off */
420 prev_posn = ftell(indexfile);
421 if (prev_posn>0) {
30ae764b 422 if (walker->get_verbosely)
49a0f240
NH
423 fprintf(stderr,
424 "Resuming fetch of index for pack %s at byte %ld\n",
1d389ab6 425 hex, prev_posn);
49a0f240
NH
426 sprintf(range, "Range: bytes=%ld-", prev_posn);
427 range_header = curl_slist_append(range_header, range);
1d389ab6 428 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
49a0f240
NH
429 }
430
1d389ab6
NH
431 if (start_active_slot(slot)) {
432 run_active_slot(slot);
c8568e13 433 if (results.curl_result != CURLE_OK) {
1d389ab6
NH
434 fclose(indexfile);
435 return error("Unable to get pack index %s\n%s", url,
436 curl_errorstr);
437 }
438 } else {
313c4714 439 fclose(indexfile);
1d389ab6 440 return error("Unable to start request");
182005b9
DB
441 }
442
443 fclose(indexfile);
49a0f240 444
b721e01f 445 return move_temp_to_file(tmpfile, filename);
182005b9
DB
446}
447
30ae764b 448static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b9
DB
449{
450 struct packed_git *new_pack;
451 if (has_pack_file(sha1))
a9486b02 452 return 0; /* don't list this as something we can get */
182005b9 453
30ae764b 454 if (fetch_index(walker, repo, sha1))
182005b9
DB
455 return -1;
456
457 new_pack = parse_pack_index(sha1);
b3661567
DB
458 new_pack->next = repo->packs;
459 repo->packs = new_pack;
182005b9
DB
460 return 0;
461}
462
e388ab74 463static void process_alternates_response(void *callback_data)
b3661567 464{
e388ab74
NH
465 struct alternates_request *alt_req =
466 (struct alternates_request *)callback_data;
30ae764b
DB
467 struct walker *walker = alt_req->walker;
468 struct walker_data *cdata = walker->data;
acc075a8 469 struct active_request_slot *slot = alt_req->slot;
30ae764b 470 struct alt_base *tail = cdata->alt;
8e29f6a0 471 const char *base = alt_req->base;
bc8f2652 472 static const char null_byte = '\0';
acc075a8
NH
473 char *data;
474 int i = 0;
1d389ab6 475
acc075a8
NH
476 if (alt_req->http_specific) {
477 if (slot->curl_result != CURLE_OK ||
478 !alt_req->buffer->posn) {
479
480 /* Try reusing the slot to get non-http alternates */
481 alt_req->http_specific = 0;
482 sprintf(alt_req->url, "%s/objects/info/alternates",
483 base);
484 curl_easy_setopt(slot->curl, CURLOPT_URL,
485 alt_req->url);
486 active_requests++;
487 slot->in_use = 1;
c9826473
NH
488 if (slot->finished != NULL)
489 (*slot->finished) = 0;
a3f583cb 490 if (!start_active_slot(slot)) {
30ae764b 491 cdata->got_alternates = -1;
29508e1e 492 slot->in_use = 0;
c9826473
NH
493 if (slot->finished != NULL)
494 (*slot->finished) = 1;
1d389ab6 495 }
a3f583cb 496 return;
b3661567 497 }
acc075a8 498 } else if (slot->curl_result != CURLE_OK) {
be4a015b 499 if (!missing_target(slot)) {
30ae764b 500 cdata->got_alternates = -1;
acc075a8
NH
501 return;
502 }
b3661567
DB
503 }
504
29508e1e 505 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
acc075a8
NH
506 alt_req->buffer->posn--;
507 data = alt_req->buffer->buffer;
1b0c1e67 508
acc075a8 509 while (i < alt_req->buffer->posn) {
b3661567 510 int posn = i;
acc075a8 511 while (posn < alt_req->buffer->posn && data[posn] != '\n')
b3661567
DB
512 posn++;
513 if (data[posn] == '\n') {
1b0c1e67
DB
514 int okay = 0;
515 int serverlen = 0;
516 struct alt_base *newalt;
517 char *target = NULL;
b3661567 518 if (data[i] == '/') {
5df1e0d0
JH
519 /* This counts
520 * http://git.host/pub/scm/linux.git/
521 * -----------here^
522 * so memcpy(dst, base, serverlen) will
523 * copy up to "...git.host".
524 */
525 const char *colon_ss = strstr(base,"://");
526 if (colon_ss) {
527 serverlen = (strchr(colon_ss + 3, '/')
528 - base);
529 okay = 1;
530 }
1b0c1e67 531 } else if (!memcmp(data + i, "../", 3)) {
5df1e0d0
JH
532 /* Relative URL; chop the corresponding
533 * number of subpath from base (and ../
534 * from data), and concatenate the result.
535 *
536 * The code first drops ../ from data, and
537 * then drops one ../ from data and one path
538 * from base. IOW, one extra ../ is dropped
539 * from data than path is dropped from base.
540 *
541 * This is not wrong. The alternate in
542 * http://git.host/pub/scm/linux.git/
543 * to borrow from
544 * http://git.host/pub/scm/linus.git/
545 * is ../../linus.git/objects/. You need
546 * two ../../ to borrow from your direct
547 * neighbour.
548 */
1b0c1e67
DB
549 i += 3;
550 serverlen = strlen(base);
8fcf7f9a 551 while (i + 2 < posn &&
1b0c1e67
DB
552 !memcmp(data + i, "../", 3)) {
553 do {
554 serverlen--;
555 } while (serverlen &&
556 base[serverlen - 1] != '/');
557 i += 3;
558 }
a9486b02 559 /* If the server got removed, give up. */
8fcf7f9a 560 okay = strchr(base, ':') - base + 3 <
1b0c1e67 561 serverlen;
acc075a8 562 } else if (alt_req->http_specific) {
1b0c1e67
DB
563 char *colon = strchr(data + i, ':');
564 char *slash = strchr(data + i, '/');
565 if (colon && slash && colon < data + posn &&
566 slash < data + posn && colon < slash) {
567 okay = 1;
568 }
569 }
5df1e0d0 570 /* skip "objects\n" at end */
1b0c1e67
DB
571 if (okay) {
572 target = xmalloc(serverlen + posn - i - 6);
5df1e0d0
JH
573 memcpy(target, base, serverlen);
574 memcpy(target + serverlen, data + i,
575 posn - i - 7);
576 target[serverlen + posn - i - 7] = 0;
30ae764b 577 if (walker->get_verbosely)
8fcf7f9a 578 fprintf(stderr,
b3661567
DB
579 "Also look at %s\n", target);
580 newalt = xmalloc(sizeof(*newalt));
1d389ab6 581 newalt->next = NULL;
b3661567
DB
582 newalt->base = target;
583 newalt->got_indices = 0;
584 newalt->packs = NULL;
8d9fbe57 585
1d389ab6
NH
586 while (tail->next != NULL)
587 tail = tail->next;
588 tail->next = newalt;
b3661567
DB
589 }
590 }
591 i = posn + 1;
592 }
bc8f2652 593
30ae764b 594 cdata->got_alternates = 1;
acc075a8
NH
595}
596
30ae764b 597static void fetch_alternates(struct walker *walker, const char *base)
acc075a8
NH
598{
599 struct buffer buffer;
600 char *url;
601 char *data;
602 struct active_request_slot *slot;
cb754fdf 603 struct alternates_request alt_req;
30ae764b 604 struct walker_data *cdata = walker->data;
acc075a8
NH
605
606 /* If another request has already started fetching alternates,
607 wait for them to arrive and return to processing this request's
608 curl message */
29508e1e 609#ifdef USE_CURL_MULTI
30ae764b 610 while (cdata->got_alternates == 0) {
29508e1e 611 step_active_slots();
acc075a8 612 }
29508e1e 613#endif
acc075a8
NH
614
615 /* Nothing to do if they've already been fetched */
30ae764b 616 if (cdata->got_alternates == 1)
acc075a8
NH
617 return;
618
619 /* Start the fetch */
30ae764b 620 cdata->got_alternates = 0;
acc075a8
NH
621
622 data = xmalloc(4096);
623 buffer.size = 4096;
624 buffer.posn = 0;
625 buffer.buffer = data;
626
30ae764b 627 if (walker->get_verbosely)
acc075a8 628 fprintf(stderr, "Getting alternates list for %s\n", base);
8fcf7f9a 629
acc075a8
NH
630 url = xmalloc(strlen(base) + 31);
631 sprintf(url, "%s/objects/info/http-alternates", base);
632
633 /* Use a callback to process the result, since another request
634 may fail and need to have alternates loaded before continuing */
635 slot = get_active_slot();
e388ab74 636 slot->callback_func = process_alternates_response;
30ae764b 637 alt_req.walker = walker;
acc075a8
NH
638 slot->callback_data = &alt_req;
639
640 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
29508e1e 641 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
acc075a8
NH
642 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
643
644 alt_req.base = base;
645 alt_req.url = url;
646 alt_req.buffer = &buffer;
647 alt_req.http_specific = 1;
648 alt_req.slot = slot;
649
650 if (start_active_slot(slot))
651 run_active_slot(slot);
652 else
30ae764b 653 cdata->got_alternates = -1;
acc075a8
NH
654
655 free(data);
656 free(url);
b3661567
DB
657}
658
30ae764b 659static int fetch_indices(struct walker *walker, struct alt_base *repo)
182005b9
DB
660{
661 unsigned char sha1[20];
662 char *url;
663 struct buffer buffer;
664 char *data;
665 int i = 0;
666
1d389ab6 667 struct active_request_slot *slot;
cb754fdf 668 struct slot_results results;
1d389ab6 669
b3661567 670 if (repo->got_indices)
182005b9
DB
671 return 0;
672
673 data = xmalloc(4096);
674 buffer.size = 4096;
675 buffer.posn = 0;
676 buffer.buffer = data;
677
30ae764b 678 if (walker->get_verbosely)
6fd72e39 679 fprintf(stderr, "Getting pack list for %s\n", repo->base);
8fcf7f9a 680
b3661567
DB
681 url = xmalloc(strlen(repo->base) + 21);
682 sprintf(url, "%s/objects/info/packs", repo->base);
182005b9 683
1d389ab6 684 slot = get_active_slot();
c8568e13 685 slot->results = &results;
1d389ab6 686 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
29508e1e 687 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1d389ab6
NH
688 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
689 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
690 if (start_active_slot(slot)) {
691 run_active_slot(slot);
c8568e13 692 if (results.curl_result != CURLE_OK) {
be4a015b 693 if (missing_target(&results)) {
5e3a7691
NH
694 repo->got_indices = 1;
695 free(buffer.buffer);
696 return 0;
697 } else {
698 repo->got_indices = 0;
699 free(buffer.buffer);
700 return error("%s", curl_errorstr);
701 }
bc8f2652 702 }
1d389ab6 703 } else {
5e3a7691 704 repo->got_indices = 0;
bc8f2652 705 free(buffer.buffer);
1d389ab6
NH
706 return error("Unable to start request");
707 }
182005b9 708
bc8f2652 709 data = buffer.buffer;
b3661567 710 while (i < buffer.posn) {
182005b9
DB
711 switch (data[i]) {
712 case 'P':
713 i++;
455c161c 714 if (i + 52 <= buffer.posn &&
cc44c765 715 !prefixcmp(data + i, " pack-") &&
1968d77d 716 !prefixcmp(data + i + 46, ".pack\n")) {
182005b9 717 get_sha1_hex(data + i + 6, sha1);
30ae764b 718 setup_index(walker, repo, sha1);
182005b9
DB
719 i += 51;
720 break;
721 }
722 default:
455c161c 723 while (i < buffer.posn && data[i] != '\n')
182005b9
DB
724 i++;
725 }
726 i++;
b3661567 727 }
182005b9 728
bc8f2652 729 free(buffer.buffer);
b3661567 730 repo->got_indices = 1;
182005b9
DB
731 return 0;
732}
733
30ae764b 734static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
182005b9
DB
735{
736 char *url;
737 struct packed_git *target;
738 struct packed_git **lst;
739 FILE *packfile;
740 char *filename;
49a0f240
NH
741 char tmpfile[PATH_MAX];
742 int ret;
743 long prev_posn = 0;
744 char range[RANGE_HEADER_SIZE];
745 struct curl_slist *range_header = NULL;
30ae764b 746 struct walker_data *data = walker->data;
1d389ab6
NH
747
748 struct active_request_slot *slot;
cb754fdf 749 struct slot_results results;
182005b9 750
30ae764b 751 if (fetch_indices(walker, repo))
182005b9 752 return -1;
b3661567 753 target = find_sha1_pack(sha1, repo->packs);
182005b9 754 if (!target)
b3661567 755 return -1;
182005b9 756
30ae764b 757 if (walker->get_verbosely) {
182005b9
DB
758 fprintf(stderr, "Getting pack %s\n",
759 sha1_to_hex(target->sha1));
760 fprintf(stderr, " which contains %s\n",
761 sha1_to_hex(sha1));
762 }
763
b3661567 764 url = xmalloc(strlen(repo->base) + 65);
182005b9 765 sprintf(url, "%s/objects/pack/pack-%s.pack",
b3661567 766 repo->base, sha1_to_hex(target->sha1));
182005b9
DB
767
768 filename = sha1_pack_name(target->sha1);
49a0f240
NH
769 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
770 packfile = fopen(tmpfile, "a");
182005b9
DB
771 if (!packfile)
772 return error("Unable to open local file %s for pack",
773 filename);
774
1d389ab6 775 slot = get_active_slot();
c8568e13 776 slot->results = &results;
1d389ab6
NH
777 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
778 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
779 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
30ae764b 780 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
1d389ab6 781 slot->local = packfile;
1ddea77e 782
49a0f240
NH
783 /* If there is data present from a previous transfer attempt,
784 resume where it left off */
785 prev_posn = ftell(packfile);
786 if (prev_posn>0) {
30ae764b 787 if (walker->get_verbosely)
49a0f240
NH
788 fprintf(stderr,
789 "Resuming fetch of pack %s at byte %ld\n",
790 sha1_to_hex(target->sha1), prev_posn);
791 sprintf(range, "Range: bytes=%ld-", prev_posn);
792 range_header = curl_slist_append(range_header, range);
1d389ab6 793 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
49a0f240
NH
794 }
795
1d389ab6
NH
796 if (start_active_slot(slot)) {
797 run_active_slot(slot);
c8568e13 798 if (results.curl_result != CURLE_OK) {
1d389ab6
NH
799 fclose(packfile);
800 return error("Unable to get pack file %s\n%s", url,
801 curl_errorstr);
802 }
803 } else {
313c4714 804 fclose(packfile);
1d389ab6 805 return error("Unable to start request");
182005b9
DB
806 }
807
1c23d794 808 target->pack_size = ftell(packfile);
182005b9
DB
809 fclose(packfile);
810
b721e01f 811 ret = move_temp_to_file(tmpfile, filename);
49a0f240 812 if (ret)
b721e01f 813 return ret;
49a0f240 814
b3661567 815 lst = &repo->packs;
182005b9
DB
816 while (*lst != target)
817 lst = &((*lst)->next);
818 *lst = (*lst)->next;
819
271421cd
JH
820 if (verify_pack(target, 0))
821 return -1;
182005b9
DB
822 install_packed_git(target);
823
824 return 0;
825}
826
53f31389
MW
827static void abort_object_request(struct object_request *obj_req)
828{
829 if (obj_req->local >= 0) {
830 close(obj_req->local);
831 obj_req->local = -1;
832 }
833 unlink(obj_req->tmpfile);
834 if (obj_req->slot) {
a6080a0a 835 release_active_slot(obj_req->slot);
53f31389
MW
836 obj_req->slot = NULL;
837 }
838 release_object_request(obj_req);
839}
840
30ae764b 841static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
6eb7ed54
DB
842{
843 char *hex = sha1_to_hex(sha1);
29508e1e 844 int ret = 0;
e388ab74 845 struct object_request *obj_req = object_queue_head;
1d389ab6 846
a89fccd2 847 while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
e388ab74
NH
848 obj_req = obj_req->next;
849 if (obj_req == NULL)
1d389ab6
NH
850 return error("Couldn't find request for %s in the queue", hex);
851
e388ab74 852 if (has_sha1_file(obj_req->sha1)) {
53f31389 853 abort_object_request(obj_req);
11f0dafe
NH
854 return 0;
855 }
856
a7a8d378 857#ifdef USE_CURL_MULTI
e388ab74 858 while (obj_req->state == WAITING) {
29508e1e 859 step_active_slots();
1d389ab6 860 }
a7a8d378 861#else
30ae764b 862 start_object_request(walker, obj_req);
a7a8d378 863#endif
6eb7ed54 864
e388ab74
NH
865 while (obj_req->state == ACTIVE) {
866 run_active_slot(obj_req->slot);
a7a8d378 867 }
e388ab74
NH
868 if (obj_req->local != -1) {
869 close(obj_req->local); obj_req->local = -1;
313c4714 870 }
6eb7ed54 871
e388ab74 872 if (obj_req->state == ABORTED) {
29508e1e 873 ret = error("Request for %s aborted", hex);
e388ab74
NH
874 } else if (obj_req->curl_result != CURLE_OK &&
875 obj_req->http_code != 416) {
be4a015b 876 if (missing_target(obj_req))
e2029eb9
PB
877 ret = -1; /* Be silent, it is probably in a pack. */
878 else
879 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
e388ab74
NH
880 obj_req->errorstr, obj_req->curl_result,
881 obj_req->http_code, hex);
882 } else if (obj_req->zret != Z_STREAM_END) {
30ae764b 883 walker->corrupt_object_found++;
bd2afde8 884 ret = error("File %s (%s) corrupt", hex, obj_req->url);
a89fccd2 885 } else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
bd2afde8 886 ret = error("File %s has bad hash", hex);
e388ab74 887 } else if (obj_req->rename < 0) {
7b934ec0
MW
888 ret = error("unable to write sha1 filename %s",
889 obj_req->filename);
6eb7ed54 890 }
49a0f240 891
e388ab74 892 release_object_request(obj_req);
29508e1e 893 return ret;
6eb7ed54
DB
894}
895
30ae764b 896static int fetch(struct walker *walker, unsigned char *sha1)
b3661567 897{
30ae764b
DB
898 struct walker_data *data = walker->data;
899 struct alt_base *altbase = data->alt;
1d389ab6 900
30ae764b 901 if (!fetch_object(walker, altbase, sha1))
1d389ab6 902 return 0;
b3661567 903 while (altbase) {
30ae764b 904 if (!fetch_pack(walker, altbase, sha1))
b3661567 905 return 0;
30ae764b 906 fetch_alternates(walker, data->alt->base);
b3661567
DB
907 altbase = altbase->next;
908 }
bd2afde8 909 return error("Unable to find %s under %s", sha1_to_hex(sha1),
30ae764b 910 data->alt->base);
b3661567
DB
911}
912
94fa447a
JH
913static inline int needs_quote(int ch)
914{
cfd432e6
FF
915 if (((ch >= 'A') && (ch <= 'Z'))
916 || ((ch >= 'a') && (ch <= 'z'))
917 || ((ch >= '0') && (ch <= '9'))
918 || (ch == '/')
919 || (ch == '-')
920 || (ch == '.'))
94fa447a 921 return 0;
cfd432e6 922 return 1;
94fa447a
JH
923}
924
925static inline int hex(int v)
926{
927 if (v < 10) return '0' + v;
928 else return 'A' + v - 10;
929}
930
931static char *quote_ref_url(const char *base, const char *ref)
932{
933 const char *cp;
934 char *dp, *qref;
935 int len, baselen, ch;
936
937 baselen = strlen(base);
2afea3bc 938 len = baselen + 7; /* "/refs/" + NUL */
94fa447a
JH
939 for (cp = ref; (ch = *cp) != 0; cp++, len++)
940 if (needs_quote(ch))
941 len += 2; /* extra two hex plus replacement % */
942 qref = xmalloc(len);
943 memcpy(qref, base, baselen);
2afea3bc
GP
944 memcpy(qref + baselen, "/refs/", 6);
945 for (cp = ref, dp = qref + baselen + 6; (ch = *cp) != 0; cp++) {
94fa447a
JH
946 if (needs_quote(ch)) {
947 *dp++ = '%';
948 *dp++ = hex((ch >> 4) & 0xF);
949 *dp++ = hex(ch & 0xF);
950 }
951 else
952 *dp++ = ch;
953 }
954 *dp = 0;
955
956 return qref;
957}
958
30ae764b 959static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
cd541a68 960{
94fa447a 961 char *url;
fa3e0655
DB
962 char hex[42];
963 struct buffer buffer;
30ae764b
DB
964 struct walker_data *data = walker->data;
965 const char *base = data->alt->base;
1d389ab6 966 struct active_request_slot *slot;
cb754fdf 967 struct slot_results results;
fa3e0655
DB
968 buffer.size = 41;
969 buffer.posn = 0;
970 buffer.buffer = hex;
971 hex[41] = '\0';
8fcf7f9a 972
94fa447a 973 url = quote_ref_url(base, ref);
1d389ab6 974 slot = get_active_slot();
c8568e13 975 slot->results = &results;
1d389ab6
NH
976 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
977 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
978 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
979 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
980 if (start_active_slot(slot)) {
981 run_active_slot(slot);
c8568e13 982 if (results.curl_result != CURLE_OK)
1d389ab6
NH
983 return error("Couldn't get %s for %s\n%s",
984 url, ref, curl_errorstr);
985 } else {
986 return error("Unable to start request");
987 }
fa3e0655
DB
988
989 hex[40] = '\0';
990 get_sha1_hex(hex, sha1);
991 return 0;
cd541a68
DB
992}
993
30ae764b
DB
994static void cleanup(struct walker *walker)
995{
996 struct walker_data *data = walker->data;
997 http_cleanup();
998
999 curl_slist_free_all(data->no_pragma_header);
1000}
1001
1002struct walker *get_http_walker(const char *url)
6eb7ed54 1003{
9c880b3e 1004 char *s;
30ae764b
DB
1005 struct walker_data *data = xmalloc(sizeof(struct walker_data));
1006 struct walker *walker = xmalloc(sizeof(struct walker));
6eb7ed54 1007
29508e1e 1008 http_init();
d402d556 1009
30ae764b 1010 data->no_pragma_header = curl_slist_append(NULL, "Pragma:");
3dcb90f5 1011
30ae764b
DB
1012 data->alt = xmalloc(sizeof(*data->alt));
1013 data->alt->base = xmalloc(strlen(url) + 1);
1014 strcpy(data->alt->base, url);
1015 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
9c880b3e 1016 *s = 0;
6eb7ed54 1017
30ae764b
DB
1018 data->alt->got_indices = 0;
1019 data->alt->packs = NULL;
1020 data->alt->next = NULL;
1021 data->got_alternates = -1;
fc57b6aa 1022
30ae764b
DB
1023 walker->corrupt_object_found = 0;
1024 walker->fetch = fetch;
1025 walker->fetch_ref = fetch_ref;
1026 walker->prefetch = prefetch;
1027 walker->cleanup = cleanup;
1028 walker->data = data;
6eb7ed54 1029
30ae764b
DB
1030#ifdef USE_CURL_MULTI
1031 add_fill_function(walker, (int (*)(void *)) fill_active_slot);
1032#endif
8e29f6a0 1033
30ae764b 1034 return walker;
6eb7ed54 1035}