Merge branch 'ph/rebase-i-redo' into maint
[git] / http-walker.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "walker.h"
4 #include "http.h"
5
6 struct alt_base {
7         char *base;
8         int got_indices;
9         struct packed_git *packs;
10         struct alt_base *next;
11 };
12
13 enum object_request_state {
14         WAITING,
15         ABORTED,
16         ACTIVE,
17         COMPLETE
18 };
19
20 struct object_request {
21         struct walker *walker;
22         unsigned char sha1[20];
23         struct alt_base *repo;
24         enum object_request_state state;
25         struct http_object_request *req;
26         struct object_request *next;
27 };
28
29 struct alternates_request {
30         struct walker *walker;
31         const char *base;
32         char *url;
33         struct strbuf *buffer;
34         struct active_request_slot *slot;
35         int http_specific;
36 };
37
38 struct walker_data {
39         const char *url;
40         int got_alternates;
41         struct alt_base *alt;
42 };
43
44 static struct object_request *object_queue_head;
45
46 static void fetch_alternates(struct walker *walker, const char *base);
47
48 static void process_object_response(void *callback_data);
49
50 static void start_object_request(struct walker *walker,
51                                  struct object_request *obj_req)
52 {
53         struct active_request_slot *slot;
54         struct http_object_request *req;
55
56         req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
57         if (req == NULL) {
58                 obj_req->state = ABORTED;
59                 return;
60         }
61         obj_req->req = req;
62
63         slot = req->slot;
64         slot->callback_func = process_object_response;
65         slot->callback_data = obj_req;
66
67         /* Try to get the request started, abort the request on error */
68         obj_req->state = ACTIVE;
69         if (!start_active_slot(slot)) {
70                 obj_req->state = ABORTED;
71                 release_http_object_request(req);
72                 return;
73         }
74 }
75
76 static void finish_object_request(struct object_request *obj_req)
77 {
78         if (finish_http_object_request(obj_req->req))
79                 return;
80
81         if (obj_req->req->rename == 0)
82                 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
83 }
84
85 static void process_object_response(void *callback_data)
86 {
87         struct object_request *obj_req =
88                 (struct object_request *)callback_data;
89         struct walker *walker = obj_req->walker;
90         struct walker_data *data = walker->data;
91         struct alt_base *alt = data->alt;
92
93         process_http_object_request(obj_req->req);
94         obj_req->state = COMPLETE;
95
96         /* Use alternates if necessary */
97         if (missing_target(obj_req->req)) {
98                 fetch_alternates(walker, alt->base);
99                 if (obj_req->repo->next != NULL) {
100                         obj_req->repo =
101                                 obj_req->repo->next;
102                         release_http_object_request(obj_req->req);
103                         start_object_request(walker, obj_req);
104                         return;
105                 }
106         }
107
108         finish_object_request(obj_req);
109 }
110
111 static void release_object_request(struct object_request *obj_req)
112 {
113         struct object_request *entry = object_queue_head;
114
115         if (obj_req->req !=NULL && obj_req->req->localfile != -1)
116                 error("fd leakage in release: %d", obj_req->req->localfile);
117         if (obj_req == object_queue_head) {
118                 object_queue_head = obj_req->next;
119         } else {
120                 while (entry->next != NULL && entry->next != obj_req)
121                         entry = entry->next;
122                 if (entry->next == obj_req)
123                         entry->next = entry->next->next;
124         }
125
126         free(obj_req);
127 }
128
129 #ifdef USE_CURL_MULTI
130 static int fill_active_slot(struct walker *walker)
131 {
132         struct object_request *obj_req;
133
134         for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
135                 if (obj_req->state == WAITING) {
136                         if (has_sha1_file(obj_req->sha1))
137                                 obj_req->state = COMPLETE;
138                         else {
139                                 start_object_request(walker, obj_req);
140                                 return 1;
141                         }
142                 }
143         }
144         return 0;
145 }
146 #endif
147
148 static void prefetch(struct walker *walker, unsigned char *sha1)
149 {
150         struct object_request *newreq;
151         struct object_request *tail;
152         struct walker_data *data = walker->data;
153
154         newreq = xmalloc(sizeof(*newreq));
155         newreq->walker = walker;
156         hashcpy(newreq->sha1, sha1);
157         newreq->repo = data->alt;
158         newreq->state = WAITING;
159         newreq->req = NULL;
160         newreq->next = NULL;
161
162         http_is_verbose = walker->get_verbosely;
163
164         if (object_queue_head == NULL) {
165                 object_queue_head = newreq;
166         } else {
167                 tail = object_queue_head;
168                 while (tail->next != NULL)
169                         tail = tail->next;
170                 tail->next = newreq;
171         }
172
173 #ifdef USE_CURL_MULTI
174         fill_active_slots();
175         step_active_slots();
176 #endif
177 }
178
179 static void process_alternates_response(void *callback_data)
180 {
181         struct alternates_request *alt_req =
182                 (struct alternates_request *)callback_data;
183         struct walker *walker = alt_req->walker;
184         struct walker_data *cdata = walker->data;
185         struct active_request_slot *slot = alt_req->slot;
186         struct alt_base *tail = cdata->alt;
187         const char *base = alt_req->base;
188         const char null_byte = '\0';
189         char *data;
190         int i = 0;
191
192         if (alt_req->http_specific) {
193                 if (slot->curl_result != CURLE_OK ||
194                     !alt_req->buffer->len) {
195
196                         /* Try reusing the slot to get non-http alternates */
197                         alt_req->http_specific = 0;
198                         sprintf(alt_req->url, "%s/objects/info/alternates",
199                                 base);
200                         curl_easy_setopt(slot->curl, CURLOPT_URL,
201                                          alt_req->url);
202                         active_requests++;
203                         slot->in_use = 1;
204                         if (slot->finished != NULL)
205                                 (*slot->finished) = 0;
206                         if (!start_active_slot(slot)) {
207                                 cdata->got_alternates = -1;
208                                 slot->in_use = 0;
209                                 if (slot->finished != NULL)
210                                         (*slot->finished) = 1;
211                         }
212                         return;
213                 }
214         } else if (slot->curl_result != CURLE_OK) {
215                 if (!missing_target(slot)) {
216                         cdata->got_alternates = -1;
217                         return;
218                 }
219         }
220
221         fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
222         alt_req->buffer->len--;
223         data = alt_req->buffer->buf;
224
225         while (i < alt_req->buffer->len) {
226                 int posn = i;
227                 while (posn < alt_req->buffer->len && data[posn] != '\n')
228                         posn++;
229                 if (data[posn] == '\n') {
230                         int okay = 0;
231                         int serverlen = 0;
232                         struct alt_base *newalt;
233                         if (data[i] == '/') {
234                                 /*
235                                  * This counts
236                                  * http://git.host/pub/scm/linux.git/
237                                  * -----------here^
238                                  * so memcpy(dst, base, serverlen) will
239                                  * copy up to "...git.host".
240                                  */
241                                 const char *colon_ss = strstr(base,"://");
242                                 if (colon_ss) {
243                                         serverlen = (strchr(colon_ss + 3, '/')
244                                                      - base);
245                                         okay = 1;
246                                 }
247                         } else if (!memcmp(data + i, "../", 3)) {
248                                 /*
249                                  * Relative URL; chop the corresponding
250                                  * number of subpath from base (and ../
251                                  * from data), and concatenate the result.
252                                  *
253                                  * The code first drops ../ from data, and
254                                  * then drops one ../ from data and one path
255                                  * from base.  IOW, one extra ../ is dropped
256                                  * from data than path is dropped from base.
257                                  *
258                                  * This is not wrong.  The alternate in
259                                  *     http://git.host/pub/scm/linux.git/
260                                  * to borrow from
261                                  *     http://git.host/pub/scm/linus.git/
262                                  * is ../../linus.git/objects/.  You need
263                                  * two ../../ to borrow from your direct
264                                  * neighbour.
265                                  */
266                                 i += 3;
267                                 serverlen = strlen(base);
268                                 while (i + 2 < posn &&
269                                        !memcmp(data + i, "../", 3)) {
270                                         do {
271                                                 serverlen--;
272                                         } while (serverlen &&
273                                                  base[serverlen - 1] != '/');
274                                         i += 3;
275                                 }
276                                 /* If the server got removed, give up. */
277                                 okay = strchr(base, ':') - base + 3 <
278                                        serverlen;
279                         } else if (alt_req->http_specific) {
280                                 char *colon = strchr(data + i, ':');
281                                 char *slash = strchr(data + i, '/');
282                                 if (colon && slash && colon < data + posn &&
283                                     slash < data + posn && colon < slash) {
284                                         okay = 1;
285                                 }
286                         }
287                         /* skip "objects\n" at end */
288                         if (okay) {
289                                 struct strbuf target = STRBUF_INIT;
290                                 strbuf_add(&target, base, serverlen);
291                                 strbuf_add(&target, data + i, posn - i - 7);
292                                 if (walker->get_verbosely)
293                                         fprintf(stderr, "Also look at %s\n",
294                                                 target.buf);
295                                 newalt = xmalloc(sizeof(*newalt));
296                                 newalt->next = NULL;
297                                 newalt->base = strbuf_detach(&target, NULL);
298                                 newalt->got_indices = 0;
299                                 newalt->packs = NULL;
300
301                                 while (tail->next != NULL)
302                                         tail = tail->next;
303                                 tail->next = newalt;
304                         }
305                 }
306                 i = posn + 1;
307         }
308
309         cdata->got_alternates = 1;
310 }
311
312 static void fetch_alternates(struct walker *walker, const char *base)
313 {
314         struct strbuf buffer = STRBUF_INIT;
315         char *url;
316         struct active_request_slot *slot;
317         struct alternates_request alt_req;
318         struct walker_data *cdata = walker->data;
319
320         /*
321          * If another request has already started fetching alternates,
322          * wait for them to arrive and return to processing this request's
323          * curl message
324          */
325 #ifdef USE_CURL_MULTI
326         while (cdata->got_alternates == 0) {
327                 step_active_slots();
328         }
329 #endif
330
331         /* Nothing to do if they've already been fetched */
332         if (cdata->got_alternates == 1)
333                 return;
334
335         /* Start the fetch */
336         cdata->got_alternates = 0;
337
338         if (walker->get_verbosely)
339                 fprintf(stderr, "Getting alternates list for %s\n", base);
340
341         url = xstrfmt("%s/objects/info/http-alternates", base);
342
343         /*
344          * Use a callback to process the result, since another request
345          * may fail and need to have alternates loaded before continuing
346          */
347         slot = get_active_slot();
348         slot->callback_func = process_alternates_response;
349         alt_req.walker = walker;
350         slot->callback_data = &alt_req;
351
352         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
353         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
354         curl_easy_setopt(slot->curl, CURLOPT_URL, url);
355
356         alt_req.base = base;
357         alt_req.url = url;
358         alt_req.buffer = &buffer;
359         alt_req.http_specific = 1;
360         alt_req.slot = slot;
361
362         if (start_active_slot(slot))
363                 run_active_slot(slot);
364         else
365                 cdata->got_alternates = -1;
366
367         strbuf_release(&buffer);
368         free(url);
369 }
370
371 static int fetch_indices(struct walker *walker, struct alt_base *repo)
372 {
373         int ret;
374
375         if (repo->got_indices)
376                 return 0;
377
378         if (walker->get_verbosely)
379                 fprintf(stderr, "Getting pack list for %s\n", repo->base);
380
381         switch (http_get_info_packs(repo->base, &repo->packs)) {
382         case HTTP_OK:
383         case HTTP_MISSING_TARGET:
384                 repo->got_indices = 1;
385                 ret = 0;
386                 break;
387         default:
388                 repo->got_indices = 0;
389                 ret = -1;
390         }
391
392         return ret;
393 }
394
395 static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
396 {
397         struct packed_git *target;
398         int ret;
399         struct slot_results results;
400         struct http_pack_request *preq;
401
402         if (fetch_indices(walker, repo))
403                 return -1;
404         target = find_sha1_pack(sha1, repo->packs);
405         if (!target)
406                 return -1;
407
408         if (walker->get_verbosely) {
409                 fprintf(stderr, "Getting pack %s\n",
410                         sha1_to_hex(target->sha1));
411                 fprintf(stderr, " which contains %s\n",
412                         sha1_to_hex(sha1));
413         }
414
415         preq = new_http_pack_request(target, repo->base);
416         if (preq == NULL)
417                 goto abort;
418         preq->lst = &repo->packs;
419         preq->slot->results = &results;
420
421         if (start_active_slot(preq->slot)) {
422                 run_active_slot(preq->slot);
423                 if (results.curl_result != CURLE_OK) {
424                         error("Unable to get pack file %s\n%s", preq->url,
425                               curl_errorstr);
426                         goto abort;
427                 }
428         } else {
429                 error("Unable to start request");
430                 goto abort;
431         }
432
433         ret = finish_http_pack_request(preq);
434         release_http_pack_request(preq);
435         if (ret)
436                 return ret;
437
438         return 0;
439
440 abort:
441         return -1;
442 }
443
444 static void abort_object_request(struct object_request *obj_req)
445 {
446         release_object_request(obj_req);
447 }
448
449 static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
450 {
451         char *hex = sha1_to_hex(sha1);
452         int ret = 0;
453         struct object_request *obj_req = object_queue_head;
454         struct http_object_request *req;
455
456         while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
457                 obj_req = obj_req->next;
458         if (obj_req == NULL)
459                 return error("Couldn't find request for %s in the queue", hex);
460
461         if (has_sha1_file(obj_req->sha1)) {
462                 if (obj_req->req != NULL)
463                         abort_http_object_request(obj_req->req);
464                 abort_object_request(obj_req);
465                 return 0;
466         }
467
468 #ifdef USE_CURL_MULTI
469         while (obj_req->state == WAITING)
470                 step_active_slots();
471 #else
472         start_object_request(walker, obj_req);
473 #endif
474
475         /*
476          * obj_req->req might change when fetching alternates in the callback
477          * process_object_response; therefore, the "shortcut" variable, req,
478          * is used only after we're done with slots.
479          */
480         while (obj_req->state == ACTIVE)
481                 run_active_slot(obj_req->req->slot);
482
483         req = obj_req->req;
484
485         if (req->localfile != -1) {
486                 close(req->localfile);
487                 req->localfile = -1;
488         }
489
490         if (obj_req->state == ABORTED) {
491                 ret = error("Request for %s aborted", hex);
492         } else if (req->curl_result != CURLE_OK &&
493                    req->http_code != 416) {
494                 if (missing_target(req))
495                         ret = -1; /* Be silent, it is probably in a pack. */
496                 else
497                         ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
498                                     req->errorstr, req->curl_result,
499                                     req->http_code, hex);
500         } else if (req->zret != Z_STREAM_END) {
501                 walker->corrupt_object_found++;
502                 ret = error("File %s (%s) corrupt", hex, req->url);
503         } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
504                 ret = error("File %s has bad hash", hex);
505         } else if (req->rename < 0) {
506                 ret = error("unable to write sha1 filename %s",
507                             sha1_file_name(req->sha1));
508         }
509
510         release_http_object_request(req);
511         release_object_request(obj_req);
512         return ret;
513 }
514
515 static int fetch(struct walker *walker, unsigned char *sha1)
516 {
517         struct walker_data *data = walker->data;
518         struct alt_base *altbase = data->alt;
519
520         if (!fetch_object(walker, altbase, sha1))
521                 return 0;
522         while (altbase) {
523                 if (!http_fetch_pack(walker, altbase, sha1))
524                         return 0;
525                 fetch_alternates(walker, data->alt->base);
526                 altbase = altbase->next;
527         }
528         return error("Unable to find %s under %s", sha1_to_hex(sha1),
529                      data->alt->base);
530 }
531
532 static int fetch_ref(struct walker *walker, struct ref *ref)
533 {
534         struct walker_data *data = walker->data;
535         return http_fetch_ref(data->alt->base, ref);
536 }
537
538 static void cleanup(struct walker *walker)
539 {
540         struct walker_data *data = walker->data;
541         struct alt_base *alt, *alt_next;
542
543         if (data) {
544                 alt = data->alt;
545                 while (alt) {
546                         alt_next = alt->next;
547
548                         free(alt->base);
549                         free(alt);
550
551                         alt = alt_next;
552                 }
553                 free(data);
554                 walker->data = NULL;
555         }
556 }
557
558 struct walker *get_http_walker(const char *url)
559 {
560         char *s;
561         struct walker_data *data = xmalloc(sizeof(struct walker_data));
562         struct walker *walker = xmalloc(sizeof(struct walker));
563
564         data->alt = xmalloc(sizeof(*data->alt));
565         data->alt->base = xstrdup(url);
566         for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
567                 *s = 0;
568
569         data->alt->got_indices = 0;
570         data->alt->packs = NULL;
571         data->alt->next = NULL;
572         data->got_alternates = -1;
573
574         walker->corrupt_object_found = 0;
575         walker->fetch = fetch;
576         walker->fetch_ref = fetch_ref;
577         walker->prefetch = prefetch;
578         walker->cleanup = cleanup;
579         walker->data = data;
580
581 #ifdef USE_CURL_MULTI
582         add_fill_function(walker, (int (*)(void *)) fill_active_slot);
583 #endif
584
585         return walker;
586 }