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