1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "repository.h"
10 #include "transport.h"
12 #include "object-store-ll.h"
17 struct packed_git
*packs
;
18 struct alt_base
*next
;
21 enum object_request_state
{
28 struct object_request
{
29 struct walker
*walker
;
31 struct alt_base
*repo
;
32 enum object_request_state state
;
33 struct http_object_request
*req
;
34 struct list_head node
;
37 struct alternates_request
{
38 struct walker
*walker
;
41 struct strbuf
*buffer
;
42 struct active_request_slot
*slot
;
52 static LIST_HEAD(object_queue_head
);
54 static void fetch_alternates(struct walker
*walker
, const char *base
);
56 static void process_object_response(void *callback_data
);
58 static void start_object_request(struct object_request
*obj_req
)
60 struct active_request_slot
*slot
;
61 struct http_object_request
*req
;
63 req
= new_http_object_request(obj_req
->repo
->base
, &obj_req
->oid
);
65 obj_req
->state
= ABORTED
;
71 slot
->callback_func
= process_object_response
;
72 slot
->callback_data
= obj_req
;
74 /* Try to get the request started, abort the request on error */
75 obj_req
->state
= ACTIVE
;
76 if (!start_active_slot(slot
)) {
77 obj_req
->state
= ABORTED
;
78 release_http_object_request(&req
);
83 static void finish_object_request(struct object_request
*obj_req
)
85 if (finish_http_object_request(obj_req
->req
))
88 if (obj_req
->req
->rename
== 0)
89 walker_say(obj_req
->walker
, "got %s\n", oid_to_hex(&obj_req
->oid
));
92 static void process_object_response(void *callback_data
)
94 struct object_request
*obj_req
=
95 (struct object_request
*)callback_data
;
96 struct walker
*walker
= obj_req
->walker
;
97 struct walker_data
*data
= walker
->data
;
98 struct alt_base
*alt
= data
->alt
;
100 process_http_object_request(obj_req
->req
);
101 obj_req
->state
= COMPLETE
;
103 normalize_curl_result(&obj_req
->req
->curl_result
,
104 obj_req
->req
->http_code
,
105 obj_req
->req
->errorstr
,
106 sizeof(obj_req
->req
->errorstr
));
108 /* Use alternates if necessary */
109 if (missing_target(obj_req
->req
)) {
110 fetch_alternates(walker
, alt
->base
);
111 if (obj_req
->repo
->next
) {
114 release_http_object_request(&obj_req
->req
);
115 start_object_request(obj_req
);
120 finish_object_request(obj_req
);
123 static void release_object_request(struct object_request
*obj_req
)
125 if (obj_req
->req
!=NULL
&& obj_req
->req
->localfile
!= -1)
126 error("fd leakage in release: %d", obj_req
->req
->localfile
);
128 list_del(&obj_req
->node
);
132 static int fill_active_slot(void *data UNUSED
)
134 struct object_request
*obj_req
;
135 struct list_head
*pos
, *tmp
, *head
= &object_queue_head
;
137 list_for_each_safe(pos
, tmp
, head
) {
138 obj_req
= list_entry(pos
, struct object_request
, node
);
139 if (obj_req
->state
== WAITING
) {
140 if (repo_has_object_file(the_repository
, &obj_req
->oid
))
141 obj_req
->state
= COMPLETE
;
143 start_object_request(obj_req
);
151 static void prefetch(struct walker
*walker
, const struct object_id
*oid
)
153 struct object_request
*newreq
;
154 struct walker_data
*data
= walker
->data
;
156 newreq
= xmalloc(sizeof(*newreq
));
157 newreq
->walker
= walker
;
158 oidcpy(&newreq
->oid
, oid
);
159 newreq
->repo
= data
->alt
;
160 newreq
->state
= WAITING
;
163 http_is_verbose
= walker
->get_verbosely
;
164 list_add_tail(&newreq
->node
, &object_queue_head
);
170 static int is_alternate_allowed(const char *url
)
172 const char *protocols
[] = {
173 "http", "https", "ftp", "ftps"
177 if (http_follow_config
!= HTTP_FOLLOW_ALWAYS
) {
178 warning("alternate disabled by http.followRedirects: %s", url
);
182 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
184 if (skip_prefix(url
, protocols
[i
], &end
) &&
185 starts_with(end
, "://"))
189 if (i
>= ARRAY_SIZE(protocols
)) {
190 warning("ignoring alternate with unknown protocol: %s", url
);
193 if (!is_transport_allowed(protocols
[i
], 0)) {
194 warning("ignoring alternate with restricted protocol: %s", url
);
201 static void process_alternates_response(void *callback_data
)
203 struct alternates_request
*alt_req
=
204 (struct alternates_request
*)callback_data
;
205 struct walker
*walker
= alt_req
->walker
;
206 struct walker_data
*cdata
= walker
->data
;
207 struct active_request_slot
*slot
= alt_req
->slot
;
208 struct alt_base
*tail
= cdata
->alt
;
209 const char *base
= alt_req
->base
;
210 const char null_byte
= '\0';
214 normalize_curl_result(&slot
->curl_result
, slot
->http_code
,
215 curl_errorstr
, sizeof(curl_errorstr
));
217 if (alt_req
->http_specific
) {
218 if (slot
->curl_result
!= CURLE_OK
||
219 !alt_req
->buffer
->len
) {
221 /* Try reusing the slot to get non-http alternates */
222 alt_req
->http_specific
= 0;
223 strbuf_reset(alt_req
->url
);
224 strbuf_addf(alt_req
->url
, "%s/objects/info/alternates",
226 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
231 (*slot
->finished
) = 0;
232 if (!start_active_slot(slot
)) {
233 cdata
->got_alternates
= -1;
236 (*slot
->finished
) = 1;
240 } else if (slot
->curl_result
!= CURLE_OK
) {
241 if (!missing_target(slot
)) {
242 cdata
->got_alternates
= -1;
247 fwrite_buffer((char *)&null_byte
, 1, 1, alt_req
->buffer
);
248 alt_req
->buffer
->len
--;
249 data
= alt_req
->buffer
->buf
;
251 while (i
< alt_req
->buffer
->len
) {
253 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
255 if (data
[posn
] == '\n') {
258 struct alt_base
*newalt
;
259 if (data
[i
] == '/') {
262 * http://git.host/pub/scm/linux.git/
264 * so memcpy(dst, base, serverlen) will
265 * copy up to "...git.host".
267 const char *colon_ss
= strstr(base
,"://");
269 serverlen
= (strchr(colon_ss
+ 3, '/')
273 } else if (!memcmp(data
+ i
, "../", 3)) {
275 * Relative URL; chop the corresponding
276 * number of subpath from base (and ../
277 * from data), and concatenate the result.
279 * The code first drops ../ from data, and
280 * then drops one ../ from data and one path
281 * from base. IOW, one extra ../ is dropped
282 * from data than path is dropped from base.
284 * This is not wrong. The alternate in
285 * http://git.host/pub/scm/linux.git/
287 * http://git.host/pub/scm/linus.git/
288 * is ../../linus.git/objects/. You need
289 * two ../../ to borrow from your direct
293 serverlen
= strlen(base
);
294 while (i
+ 2 < posn
&&
295 !memcmp(data
+ i
, "../", 3)) {
298 } while (serverlen
&&
299 base
[serverlen
- 1] != '/');
302 /* If the server got removed, give up. */
303 okay
= strchr(base
, ':') - base
+ 3 <
305 } else if (alt_req
->http_specific
) {
306 char *colon
= strchr(data
+ i
, ':');
307 char *slash
= strchr(data
+ i
, '/');
308 if (colon
&& slash
&& colon
< data
+ posn
&&
309 slash
< data
+ posn
&& colon
< slash
) {
314 struct strbuf target
= STRBUF_INIT
;
315 strbuf_add(&target
, base
, serverlen
);
316 strbuf_add(&target
, data
+ i
, posn
- i
);
317 if (!strbuf_strip_suffix(&target
, "objects")) {
318 warning("ignoring alternate that does"
319 " not end in 'objects': %s",
321 strbuf_release(&target
);
322 } else if (is_alternate_allowed(target
.buf
)) {
323 warning("adding alternate object store: %s",
325 newalt
= xmalloc(sizeof(*newalt
));
327 newalt
->base
= strbuf_detach(&target
, NULL
);
328 newalt
->got_indices
= 0;
329 newalt
->packs
= NULL
;
331 while (tail
->next
!= NULL
)
335 strbuf_release(&target
);
342 cdata
->got_alternates
= 1;
345 static void fetch_alternates(struct walker
*walker
, const char *base
)
347 struct strbuf buffer
= STRBUF_INIT
;
348 struct strbuf url
= STRBUF_INIT
;
349 struct active_request_slot
*slot
;
350 struct alternates_request alt_req
;
351 struct walker_data
*cdata
= walker
->data
;
354 * If another request has already started fetching alternates,
355 * wait for them to arrive and return to processing this request's
358 while (cdata
->got_alternates
== 0) {
362 /* Nothing to do if they've already been fetched */
363 if (cdata
->got_alternates
== 1)
366 /* Start the fetch */
367 cdata
->got_alternates
= 0;
369 if (walker
->get_verbosely
)
370 fprintf(stderr
, "Getting alternates list for %s\n", base
);
372 strbuf_addf(&url
, "%s/objects/info/http-alternates", base
);
375 * Use a callback to process the result, since another request
376 * may fail and need to have alternates loaded before continuing
378 slot
= get_active_slot();
379 slot
->callback_func
= process_alternates_response
;
380 alt_req
.walker
= walker
;
381 slot
->callback_data
= &alt_req
;
383 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, &buffer
);
384 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
385 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
.buf
);
389 alt_req
.buffer
= &buffer
;
390 alt_req
.http_specific
= 1;
393 if (start_active_slot(slot
))
394 run_active_slot(slot
);
396 cdata
->got_alternates
= -1;
398 strbuf_release(&buffer
);
399 strbuf_release(&url
);
402 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
406 if (repo
->got_indices
)
409 if (walker
->get_verbosely
)
410 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
412 switch (http_get_info_packs(repo
->base
, &repo
->packs
)) {
414 case HTTP_MISSING_TARGET
:
415 repo
->got_indices
= 1;
419 repo
->got_indices
= 0;
426 static int http_fetch_pack(struct walker
*walker
, struct alt_base
*repo
,
427 const struct object_id
*oid
)
429 struct packed_git
*target
;
431 struct slot_results results
;
432 struct http_pack_request
*preq
;
434 if (fetch_indices(walker
, repo
))
436 target
= find_oid_pack(oid
, repo
->packs
);
439 close_pack_index(target
);
441 if (walker
->get_verbosely
) {
442 fprintf(stderr
, "Getting pack %s\n",
443 hash_to_hex(target
->hash
));
444 fprintf(stderr
, " which contains %s\n",
448 preq
= new_http_pack_request(target
->hash
, repo
->base
);
451 preq
->slot
->results
= &results
;
453 if (start_active_slot(preq
->slot
)) {
454 run_active_slot(preq
->slot
);
455 if (results
.curl_result
!= CURLE_OK
) {
456 error("Unable to get pack file %s\n%s", preq
->url
,
461 error("Unable to start request");
465 ret
= finish_http_pack_request(preq
);
466 release_http_pack_request(preq
);
469 http_install_packfile(target
, &repo
->packs
);
477 static void abort_object_request(struct object_request
*obj_req
)
479 release_object_request(obj_req
);
482 static int fetch_object(struct walker
*walker
, const struct object_id
*oid
)
484 char *hex
= oid_to_hex(oid
);
486 struct object_request
*obj_req
= NULL
;
487 struct http_object_request
*req
;
488 struct list_head
*pos
, *head
= &object_queue_head
;
490 list_for_each(pos
, head
) {
491 obj_req
= list_entry(pos
, struct object_request
, node
);
492 if (oideq(&obj_req
->oid
, oid
))
496 return error("Couldn't find request for %s in the queue", hex
);
498 if (repo_has_object_file(the_repository
, &obj_req
->oid
)) {
500 abort_http_object_request(&obj_req
->req
);
501 abort_object_request(obj_req
);
505 while (obj_req
->state
== WAITING
)
509 * obj_req->req might change when fetching alternates in the callback
510 * process_object_response; therefore, the "shortcut" variable, req,
511 * is used only after we're done with slots.
513 while (obj_req
->state
== ACTIVE
)
514 run_active_slot(obj_req
->req
->slot
);
518 if (req
->localfile
!= -1) {
519 close(req
->localfile
);
523 normalize_curl_result(&req
->curl_result
, req
->http_code
,
524 req
->errorstr
, sizeof(req
->errorstr
));
526 if (obj_req
->state
== ABORTED
) {
527 ret
= error("Request for %s aborted", hex
);
528 } else if (req
->curl_result
!= CURLE_OK
&&
529 req
->http_code
!= 416) {
530 if (missing_target(req
))
531 ret
= -1; /* Be silent, it is probably in a pack. */
533 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
534 req
->errorstr
, req
->curl_result
,
535 req
->http_code
, hex
);
536 } else if (req
->zret
!= Z_STREAM_END
) {
537 walker
->corrupt_object_found
++;
538 ret
= error("File %s (%s) corrupt", hex
, req
->url
);
539 } else if (!oideq(&obj_req
->oid
, &req
->real_oid
)) {
540 ret
= error("File %s has bad hash", hex
);
541 } else if (req
->rename
< 0) {
542 struct strbuf buf
= STRBUF_INIT
;
543 loose_object_path(the_repository
, &buf
, &req
->oid
);
544 ret
= error("unable to write sha1 filename %s", buf
.buf
);
545 strbuf_release(&buf
);
548 release_http_object_request(&obj_req
->req
);
549 release_object_request(obj_req
);
553 static int fetch(struct walker
*walker
, const struct object_id
*oid
)
555 struct walker_data
*data
= walker
->data
;
556 struct alt_base
*altbase
= data
->alt
;
558 if (!fetch_object(walker
, oid
))
561 if (!http_fetch_pack(walker
, altbase
, oid
))
563 fetch_alternates(walker
, data
->alt
->base
);
564 altbase
= altbase
->next
;
566 return error("Unable to find %s under %s", oid_to_hex(oid
),
570 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
572 struct walker_data
*data
= walker
->data
;
573 return http_fetch_ref(data
->alt
->base
, ref
);
576 static void cleanup(struct walker
*walker
)
578 struct walker_data
*data
= walker
->data
;
579 struct alt_base
*alt
, *alt_next
;
584 struct packed_git
*pack
;
586 alt_next
= alt
->next
;
590 struct packed_git
*pack_next
= pack
->next
;
606 struct walker
*get_http_walker(const char *url
)
609 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
610 struct walker
*walker
= xmalloc(sizeof(struct walker
));
612 data
->alt
= xmalloc(sizeof(*data
->alt
));
613 data
->alt
->base
= xstrdup(url
);
614 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
617 data
->alt
->got_indices
= 0;
618 data
->alt
->packs
= NULL
;
619 data
->alt
->next
= NULL
;
620 data
->got_alternates
= -1;
622 walker
->corrupt_object_found
= 0;
623 walker
->fetch
= fetch
;
624 walker
->fetch_ref
= fetch_ref
;
625 walker
->prefetch
= prefetch
;
626 walker
->cleanup
= cleanup
;
629 add_fill_function(NULL
, fill_active_slot
);