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