blob: 910fae539b89e6aea2af299b61dd64ff645b3578 [file] [log] [blame]
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07001#include "cache.h"
Stefan Bellercf78ae42018-03-23 18:21:10 +01002#include "repository.h"
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -07003#include "commit.h"
Daniel Barkalow30ae7642007-09-10 23:02:45 -04004#include "walker.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -08005#include "http.h"
Eric Wong94e99012016-07-11 20:51:31 +00006#include "list.h"
Jeff Kingabcbdc02016-12-14 14:39:55 -08007#include "transport.h"
Jonathan Tand6fe0032017-08-18 15:20:34 -07008#include "packfile.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +01009#include "object-store.h"
Johannes Schindelin7baa3e82005-10-15 11:10:46 -070010
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050011struct alt_base {
Gerrit Pape2afea3b2007-03-28 09:46:15 +000012 char *base;
Daniel Barkalowb3661562005-09-14 23:26:08 -040013 int got_indices;
14 struct packed_git *packs;
15 struct alt_base *next;
16};
17
Nick Hengevelde388ab72005-11-18 11:03:04 -080018enum object_request_state {
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070019 WAITING,
20 ABORTED,
21 ACTIVE,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000022 COMPLETE
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070023};
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -070024
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050025struct object_request {
Daniel Barkalow30ae7642007-09-10 23:02:45 -040026 struct walker *walker;
brian m. carlson16f07052018-03-12 02:27:31 +000027 struct object_id oid;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070028 struct alt_base *repo;
Nick Hengevelde388ab72005-11-18 11:03:04 -080029 enum object_request_state state;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080030 struct http_object_request *req;
Eric Wong94e99012016-07-11 20:51:31 +000031 struct list_head node;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070032};
33
Nick Hengevelde388ab72005-11-18 11:03:04 -080034struct alternates_request {
Daniel Barkalow30ae7642007-09-10 23:02:45 -040035 struct walker *walker;
Petr Baudis8e29f6a2006-07-27 23:56:22 +020036 const char *base;
Jeff King54ba4c52015-09-24 17:07:31 -040037 struct strbuf *url;
Mike Hommey028c2972007-12-09 20:30:59 +010038 struct strbuf *buffer;
Nick Hengeveldacc075a2005-11-12 09:11:32 -080039 struct active_request_slot *slot;
40 int http_specific;
41};
42
Daniel Barkalow30ae7642007-09-10 23:02:45 -040043struct walker_data {
44 const char *url;
45 int got_alternates;
46 struct alt_base *alt;
Daniel Barkalow30ae7642007-09-10 23:02:45 -040047};
48
Eric Wong94e99012016-07-11 20:51:31 +000049static LIST_HEAD(object_queue_head);
Sergey Vlasovbc8f2652005-10-13 10:49:53 -070050
Daniel Barkalow30ae7642007-09-10 23:02:45 -040051static void fetch_alternates(struct walker *walker, const char *base);
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070052
Nick Hengeveld29508e12005-11-18 11:02:58 -080053static void process_object_response(void *callback_data);
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070054
Daniel Barkalow30ae7642007-09-10 23:02:45 -040055static void start_object_request(struct walker *walker,
56 struct object_request *obj_req)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070057{
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070058 struct active_request_slot *slot;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080059 struct http_object_request *req;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070060
Jeff Kingf0be0db2019-01-07 03:34:40 -050061 req = new_http_object_request(obj_req->repo->base, &obj_req->oid);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080062 if (req == NULL) {
Nick Hengevelde388ab72005-11-18 11:03:04 -080063 obj_req->state = ABORTED;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070064 return;
65 }
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080066 obj_req->req = req;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070067
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080068 slot = req->slot;
Nick Hengeveld29508e12005-11-18 11:02:58 -080069 slot->callback_func = process_object_response;
Nick Hengevelde388ab72005-11-18 11:03:04 -080070 slot->callback_data = obj_req;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070071
Nick Hengevelda7a8d372005-10-10 23:22:01 -070072 /* Try to get the request started, abort the request on error */
Nick Hengevelde388ab72005-11-18 11:03:04 -080073 obj_req->state = ACTIVE;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070074 if (!start_active_slot(slot)) {
Nick Hengevelde388ab72005-11-18 11:03:04 -080075 obj_req->state = ABORTED;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080076 release_http_object_request(req);
Nick Hengevelde388ab72005-11-18 11:03:04 -080077 return;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070078 }
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070079}
80
Nick Hengevelde388ab72005-11-18 11:03:04 -080081static void finish_object_request(struct object_request *obj_req)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070082{
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080083 if (finish_http_object_request(obj_req->req))
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070084 return;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070085
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080086 if (obj_req->req->rename == 0)
brian m. carlson16f07052018-03-12 02:27:31 +000087 walker_say(obj_req->walker, "got %s\n", oid_to_hex(&obj_req->oid));
Nick Hengeveld1d389ab2005-10-10 23:22:01 -070088}
89
Nick Hengeveld29508e12005-11-18 11:02:58 -080090static void process_object_response(void *callback_data)
91{
Nick Hengevelde388ab72005-11-18 11:03:04 -080092 struct object_request *obj_req =
93 (struct object_request *)callback_data;
Daniel Barkalow30ae7642007-09-10 23:02:45 -040094 struct walker *walker = obj_req->walker;
95 struct walker_data *data = walker->data;
96 struct alt_base *alt = data->alt;
Nick Hengeveld29508e12005-11-18 11:02:58 -080097
Tay Ray Chuan5424bc52009-06-06 16:44:02 +080098 process_http_object_request(obj_req->req);
Nick Hengevelde388ab72005-11-18 11:03:04 -080099 obj_req->state = COMPLETE;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800100
Jeff Kingccbbd8b2019-03-24 08:09:46 -0400101 normalize_curl_result(&obj_req->req->curl_result,
102 obj_req->req->http_code,
103 obj_req->req->errorstr,
104 sizeof(obj_req->req->errorstr));
105
Nick Hengeveld29508e12005-11-18 11:02:58 -0800106 /* Use alternates if necessary */
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800107 if (missing_target(obj_req->req)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400108 fetch_alternates(walker, alt->base);
Nick Hengevelde388ab72005-11-18 11:03:04 -0800109 if (obj_req->repo->next != NULL) {
110 obj_req->repo =
111 obj_req->repo->next;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800112 release_http_object_request(obj_req->req);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400113 start_object_request(walker, obj_req);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800114 return;
115 }
116 }
117
Nick Hengevelde388ab72005-11-18 11:03:04 -0800118 finish_object_request(obj_req);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800119}
120
Nick Hengevelde388ab72005-11-18 11:03:04 -0800121static void release_object_request(struct object_request *obj_req)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700122{
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800123 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
124 error("fd leakage in release: %d", obj_req->req->localfile);
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700125
Eric Wong94e99012016-07-11 20:51:31 +0000126 list_del(&obj_req->node);
Nick Hengevelde388ab72005-11-18 11:03:04 -0800127 free(obj_req);
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700128}
129
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400130static int fill_active_slot(struct walker *walker)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700131{
Daniel Barkalow45c17412007-09-10 23:02:28 -0400132 struct object_request *obj_req;
Eric Wong94e99012016-07-11 20:51:31 +0000133 struct list_head *pos, *tmp, *head = &object_queue_head;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700134
Eric Wong94e99012016-07-11 20:51:31 +0000135 list_for_each_safe(pos, tmp, head) {
136 obj_req = list_entry(pos, struct object_request, node);
Nick Hengevelde388ab72005-11-18 11:03:04 -0800137 if (obj_req->state == WAITING) {
Jeff King98374a02019-01-07 03:37:54 -0500138 if (has_object_file(&obj_req->oid))
Mark Wooding09db4442006-02-01 11:44:28 +0000139 obj_req->state = COMPLETE;
Daniel Barkalow45c17412007-09-10 23:02:28 -0400140 else {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400141 start_object_request(walker, obj_req);
Daniel Barkalow45c17412007-09-10 23:02:28 -0400142 return 1;
143 }
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700144 }
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700145 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400146 return 0;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700147}
148
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400149static void prefetch(struct walker *walker, unsigned char *sha1)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700150{
Nick Hengevelde388ab72005-11-18 11:03:04 -0800151 struct object_request *newreq;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400152 struct walker_data *data = walker->data;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700153
154 newreq = xmalloc(sizeof(*newreq));
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400155 newreq->walker = walker;
brian m. carlson92e2cab2021-04-26 01:02:50 +0000156 oidread(&newreq->oid, sha1);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400157 newreq->repo = data->alt;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700158 newreq->state = WAITING;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800159 newreq->req = NULL;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700160
Tay Ray Chuane9176742009-06-06 16:43:41 +0800161 http_is_verbose = walker->get_verbosely;
Eric Wong94e99012016-07-11 20:51:31 +0000162 list_add_tail(&newreq->node, &object_queue_head);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800163
Nick Hengeveld29508e12005-11-18 11:02:58 -0800164 fill_active_slots();
165 step_active_slots();
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700166}
167
Jeff Kingabcbdc02016-12-14 14:39:55 -0800168static int is_alternate_allowed(const char *url)
169{
170 const char *protocols[] = {
171 "http", "https", "ftp", "ftps"
172 };
173 int i;
174
Eric Wongde461382017-03-04 08:36:45 +0000175 if (http_follow_config != HTTP_FOLLOW_ALWAYS) {
176 warning("alternate disabled by http.followRedirects: %s", url);
177 return 0;
178 }
179
Jeff Kingabcbdc02016-12-14 14:39:55 -0800180 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
181 const char *end;
182 if (skip_prefix(url, protocols[i], &end) &&
183 starts_with(end, "://"))
184 break;
185 }
186
187 if (i >= ARRAY_SIZE(protocols)) {
188 warning("ignoring alternate with unknown protocol: %s", url);
189 return 0;
190 }
191 if (!is_transport_allowed(protocols[i], 0)) {
192 warning("ignoring alternate with restricted protocol: %s", url);
193 return 0;
194 }
195
196 return 1;
197}
198
Nick Hengevelde388ab72005-11-18 11:03:04 -0800199static void process_alternates_response(void *callback_data)
Daniel Barkalowb3661562005-09-14 23:26:08 -0400200{
Nick Hengevelde388ab72005-11-18 11:03:04 -0800201 struct alternates_request *alt_req =
202 (struct alternates_request *)callback_data;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400203 struct walker *walker = alt_req->walker;
204 struct walker_data *cdata = walker->data;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800205 struct active_request_slot *slot = alt_req->slot;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400206 struct alt_base *tail = cdata->alt;
Petr Baudis8e29f6a2006-07-27 23:56:22 +0200207 const char *base = alt_req->base;
Dan McGeea04ff3e2011-05-03 23:47:27 +0800208 const char null_byte = '\0';
Daniel Barkalowb3661562005-09-14 23:26:08 -0400209 char *data;
210 int i = 0;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700211
Jeff Kingccbbd8b2019-03-24 08:09:46 -0400212 normalize_curl_result(&slot->curl_result, slot->http_code,
213 curl_errorstr, sizeof(curl_errorstr));
214
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800215 if (alt_req->http_specific) {
216 if (slot->curl_result != CURLE_OK ||
Mike Hommey028c2972007-12-09 20:30:59 +0100217 !alt_req->buffer->len) {
Junio C Hamanodc1b5ea2005-10-10 23:22:02 -0700218
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800219 /* Try reusing the slot to get non-http alternates */
220 alt_req->http_specific = 0;
Jeff King54ba4c52015-09-24 17:07:31 -0400221 strbuf_reset(alt_req->url);
222 strbuf_addf(alt_req->url, "%s/objects/info/alternates",
223 base);
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800224 curl_easy_setopt(slot->curl, CURLOPT_URL,
Jeff King54ba4c52015-09-24 17:07:31 -0400225 alt_req->url->buf);
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800226 active_requests++;
227 slot->in_use = 1;
Nick Hengeveldc9826472006-03-15 08:59:52 -0800228 if (slot->finished != NULL)
229 (*slot->finished) = 0;
Mark Woodinga3f583c2006-02-01 11:44:39 +0000230 if (!start_active_slot(slot)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400231 cdata->got_alternates = -1;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800232 slot->in_use = 0;
Nick Hengeveldc9826472006-03-15 08:59:52 -0800233 if (slot->finished != NULL)
234 (*slot->finished) = 1;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700235 }
Mark Woodinga3f583c2006-02-01 11:44:39 +0000236 return;
Daniel Barkalowb3661562005-09-14 23:26:08 -0400237 }
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800238 } else if (slot->curl_result != CURLE_OK) {
Junio C Hamanobe4a0152006-09-16 10:58:20 -0700239 if (!missing_target(slot)) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400240 cdata->got_alternates = -1;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800241 return;
242 }
Daniel Barkalowb3661562005-09-14 23:26:08 -0400243 }
244
Dan McGeea04ff3e2011-05-03 23:47:27 +0800245 fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
Mike Hommey028c2972007-12-09 20:30:59 +0100246 alt_req->buffer->len--;
247 data = alt_req->buffer->buf;
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400248
Mike Hommey028c2972007-12-09 20:30:59 +0100249 while (i < alt_req->buffer->len) {
Daniel Barkalowb3661562005-09-14 23:26:08 -0400250 int posn = i;
Mike Hommey028c2972007-12-09 20:30:59 +0100251 while (posn < alt_req->buffer->len && data[posn] != '\n')
Daniel Barkalowb3661562005-09-14 23:26:08 -0400252 posn++;
253 if (data[posn] == '\n') {
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400254 int okay = 0;
255 int serverlen = 0;
256 struct alt_base *newalt;
Daniel Barkalowb3661562005-09-14 23:26:08 -0400257 if (data[i] == '/') {
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800258 /*
259 * This counts
Junio C Hamano5df1e0d2006-09-12 23:53:27 -0700260 * http://git.host/pub/scm/linux.git/
261 * -----------here^
262 * so memcpy(dst, base, serverlen) will
263 * copy up to "...git.host".
264 */
265 const char *colon_ss = strstr(base,"://");
266 if (colon_ss) {
267 serverlen = (strchr(colon_ss + 3, '/')
268 - base);
269 okay = 1;
270 }
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400271 } else if (!memcmp(data + i, "../", 3)) {
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800272 /*
273 * Relative URL; chop the corresponding
Junio C Hamano5df1e0d2006-09-12 23:53:27 -0700274 * number of subpath from base (and ../
275 * from data), and concatenate the result.
276 *
277 * The code first drops ../ from data, and
278 * then drops one ../ from data and one path
279 * from base. IOW, one extra ../ is dropped
280 * from data than path is dropped from base.
281 *
282 * This is not wrong. The alternate in
283 * http://git.host/pub/scm/linux.git/
284 * to borrow from
285 * http://git.host/pub/scm/linus.git/
286 * is ../../linus.git/objects/. You need
287 * two ../../ to borrow from your direct
288 * neighbour.
289 */
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400290 i += 3;
291 serverlen = strlen(base);
Junio C Hamano8fcf7f92006-01-31 18:15:51 -0800292 while (i + 2 < posn &&
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400293 !memcmp(data + i, "../", 3)) {
294 do {
295 serverlen--;
296 } while (serverlen &&
297 base[serverlen - 1] != '/');
298 i += 3;
299 }
Pavel Roskina9486b02006-07-10 02:57:51 -0400300 /* If the server got removed, give up. */
Junio C Hamano8fcf7f92006-01-31 18:15:51 -0800301 okay = strchr(base, ':') - base + 3 <
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800302 serverlen;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800303 } else if (alt_req->http_specific) {
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400304 char *colon = strchr(data + i, ':');
305 char *slash = strchr(data + i, '/');
306 if (colon && slash && colon < data + posn &&
307 slash < data + posn && colon < slash) {
308 okay = 1;
309 }
310 }
Daniel Barkalow1b0c1e62005-09-18 14:14:19 -0400311 if (okay) {
René Scharfe59b82632014-08-30 17:55:45 +0200312 struct strbuf target = STRBUF_INIT;
313 strbuf_add(&target, base, serverlen);
Jeff Kingd61434a2017-03-13 10:04:45 -0400314 strbuf_add(&target, data + i, posn - i);
315 if (!strbuf_strip_suffix(&target, "objects")) {
316 warning("ignoring alternate that does"
317 " not end in 'objects': %s",
318 target.buf);
319 strbuf_release(&target);
320 } else if (is_alternate_allowed(target.buf)) {
Jeff Kingabcbdc02016-12-14 14:39:55 -0800321 warning("adding alternate object store: %s",
322 target.buf);
323 newalt = xmalloc(sizeof(*newalt));
324 newalt->next = NULL;
325 newalt->base = strbuf_detach(&target, NULL);
326 newalt->got_indices = 0;
327 newalt->packs = NULL;
328
329 while (tail->next != NULL)
330 tail = tail->next;
331 tail->next = newalt;
Eric Wong5cae73d2017-03-04 01:50:16 +0000332 } else {
333 strbuf_release(&target);
Jeff Kingabcbdc02016-12-14 14:39:55 -0800334 }
Daniel Barkalowb3661562005-09-14 23:26:08 -0400335 }
336 }
337 i = posn + 1;
338 }
Sergey Vlasovbc8f2652005-10-13 10:49:53 -0700339
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400340 cdata->got_alternates = 1;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800341}
342
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400343static void fetch_alternates(struct walker *walker, const char *base)
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800344{
Mike Hommey028c2972007-12-09 20:30:59 +0100345 struct strbuf buffer = STRBUF_INIT;
Jeff King54ba4c52015-09-24 17:07:31 -0400346 struct strbuf url = STRBUF_INIT;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800347 struct active_request_slot *slot;
Nick Hengeveldcb754fd2006-01-31 18:00:37 -0800348 struct alternates_request alt_req;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400349 struct walker_data *cdata = walker->data;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800350
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800351 /*
352 * If another request has already started fetching alternates,
353 * wait for them to arrive and return to processing this request's
354 * curl message
355 */
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400356 while (cdata->got_alternates == 0) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800357 step_active_slots();
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800358 }
359
360 /* Nothing to do if they've already been fetched */
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400361 if (cdata->got_alternates == 1)
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800362 return;
363
364 /* Start the fetch */
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400365 cdata->got_alternates = 0;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800366
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400367 if (walker->get_verbosely)
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800368 fprintf(stderr, "Getting alternates list for %s\n", base);
Junio C Hamano8fcf7f92006-01-31 18:15:51 -0800369
Jeff King54ba4c52015-09-24 17:07:31 -0400370 strbuf_addf(&url, "%s/objects/info/http-alternates", base);
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800371
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800372 /*
373 * Use a callback to process the result, since another request
374 * may fail and need to have alternates loaded before continuing
375 */
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800376 slot = get_active_slot();
Nick Hengevelde388ab72005-11-18 11:03:04 -0800377 slot->callback_func = process_alternates_response;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400378 alt_req.walker = walker;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800379 slot->callback_data = &alt_req;
380
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +0200381 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buffer);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800382 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
Jeff King54ba4c52015-09-24 17:07:31 -0400383 curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800384
385 alt_req.base = base;
Jeff King54ba4c52015-09-24 17:07:31 -0400386 alt_req.url = &url;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800387 alt_req.buffer = &buffer;
388 alt_req.http_specific = 1;
389 alt_req.slot = slot;
390
391 if (start_active_slot(slot))
392 run_active_slot(slot);
393 else
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400394 cdata->got_alternates = -1;
Nick Hengeveldacc075a2005-11-12 09:11:32 -0800395
Mike Hommey028c2972007-12-09 20:30:59 +0100396 strbuf_release(&buffer);
Jeff King54ba4c52015-09-24 17:07:31 -0400397 strbuf_release(&url);
Daniel Barkalowb3661562005-09-14 23:26:08 -0400398}
399
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400400static int fetch_indices(struct walker *walker, struct alt_base *repo)
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400401{
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800402 int ret;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700403
Daniel Barkalowb3661562005-09-14 23:26:08 -0400404 if (repo->got_indices)
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400405 return 0;
406
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400407 if (walker->get_verbosely)
Petr Baudis6fd72e32005-11-12 01:49:59 +0100408 fprintf(stderr, "Getting pack list for %s\n", repo->base);
Junio C Hamano8fcf7f92006-01-31 18:15:51 -0800409
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800410 switch (http_get_info_packs(repo->base, &repo->packs)) {
411 case HTTP_OK:
412 case HTTP_MISSING_TARGET:
413 repo->got_indices = 1;
414 ret = 0;
415 break;
416 default:
Nick Hengeveld5e3a7692005-11-18 11:03:11 -0800417 repo->got_indices = 0;
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800418 ret = -1;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700419 }
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400420
Mike Hommey3a462bc2007-12-10 22:36:11 +0100421 return ret;
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400422}
423
Michael Haggerty07c19e72012-09-09 08:19:38 +0200424static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400425{
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400426 struct packed_git *target;
Nick Hengeveld49a0f242005-09-28 10:14:04 -0700427 int ret;
Nick Hengeveldcb754fd2006-01-31 18:00:37 -0800428 struct slot_results results;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800429 struct http_pack_request *preq;
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400430
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400431 if (fetch_indices(walker, repo))
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400432 return -1;
Daniel Barkalowb3661562005-09-14 23:26:08 -0400433 target = find_sha1_pack(sha1, repo->packs);
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400434 if (!target)
Daniel Barkalowb3661562005-09-14 23:26:08 -0400435 return -1;
Jonathan Taneb053492020-06-10 13:57:16 -0700436 close_pack_index(target);
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400437
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400438 if (walker->get_verbosely) {
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400439 fprintf(stderr, "Getting pack %s\n",
brian m. carlson2bf1db72019-02-19 00:05:12 +0000440 hash_to_hex(target->hash));
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400441 fprintf(stderr, " which contains %s\n",
brian m. carlson2bf1db72019-02-19 00:05:12 +0000442 hash_to_hex(sha1));
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400443 }
444
Jonathan Taneb053492020-06-10 13:57:16 -0700445 preq = new_http_pack_request(target->hash, repo->base);
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800446 if (preq == NULL)
447 goto abort;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800448 preq->slot->results = &results;
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400449
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800450 if (start_active_slot(preq->slot)) {
451 run_active_slot(preq->slot);
Nick Hengeveldc8568e12006-01-31 11:06:55 -0800452 if (results.curl_result != CURLE_OK) {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800453 error("Unable to get pack file %s\n%s", preq->url,
454 curl_errorstr);
455 goto abort;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700456 }
457 } else {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800458 error("Unable to start request");
459 goto abort;
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400460 }
461
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800462 ret = finish_http_pack_request(preq);
463 release_http_pack_request(preq);
Nick Hengeveld49a0f242005-09-28 10:14:04 -0700464 if (ret)
Junio C Hamanob721e012005-10-10 23:22:01 -0700465 return ret;
Jonathan Taneb053492020-06-10 13:57:16 -0700466 http_install_packfile(target, &repo->packs);
Nick Hengeveld49a0f242005-09-28 10:14:04 -0700467
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400468 return 0;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800469
470abort:
471 return -1;
barkalow@iabervon.org182005b2005-07-31 20:54:17 -0400472}
473
Mark Wooding53f31382006-02-07 10:07:39 +0000474static void abort_object_request(struct object_request *obj_req)
475{
Mark Wooding53f31382006-02-07 10:07:39 +0000476 release_object_request(obj_req);
477}
478
brian m. carlson2bf1db72019-02-19 00:05:12 +0000479static int fetch_object(struct walker *walker, unsigned char *hash)
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700480{
brian m. carlson2bf1db72019-02-19 00:05:12 +0000481 char *hex = hash_to_hex(hash);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800482 int ret = 0;
Eric Wong94e99012016-07-11 20:51:31 +0000483 struct object_request *obj_req = NULL;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800484 struct http_object_request *req;
Eric Wong94e99012016-07-11 20:51:31 +0000485 struct list_head *pos, *head = &object_queue_head;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700486
Eric Wong94e99012016-07-11 20:51:31 +0000487 list_for_each(pos, head) {
488 obj_req = list_entry(pos, struct object_request, node);
brian m. carlson2bf1db72019-02-19 00:05:12 +0000489 if (hasheq(obj_req->oid.hash, hash))
Eric Wong94e99012016-07-11 20:51:31 +0000490 break;
491 }
Nick Hengevelde388ab72005-11-18 11:03:04 -0800492 if (obj_req == NULL)
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700493 return error("Couldn't find request for %s in the queue", hex);
Junio C Hamano271421c2005-09-30 00:07:39 -0700494
Jeff King98374a02019-01-07 03:37:54 -0500495 if (has_object_file(&obj_req->oid)) {
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800496 if (obj_req->req != NULL)
497 abort_http_object_request(obj_req->req);
Mark Wooding53f31382006-02-07 10:07:39 +0000498 abort_object_request(obj_req);
Nick Hengeveld11f0daf2005-10-10 23:22:01 -0700499 return 0;
500 }
501
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800502 while (obj_req->state == WAITING)
Nick Hengeveld29508e12005-11-18 11:02:58 -0800503 step_active_slots();
Nick Hengeveld49a0f242005-09-28 10:14:04 -0700504
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800505 /*
506 * obj_req->req might change when fetching alternates in the callback
507 * process_object_response; therefore, the "shortcut" variable, req,
508 * is used only after we're done with slots.
509 */
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800510 while (obj_req->state == ACTIVE)
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800511 run_active_slot(obj_req->req->slot);
Tay Ray Chuan4c42aa12009-06-06 16:43:33 +0800512
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800513 req = obj_req->req;
514
515 if (req->localfile != -1) {
516 close(req->localfile);
517 req->localfile = -1;
Petr Baudis313c4712005-11-12 00:55:16 +0100518 }
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700519
Jeff King3d10f722019-03-24 08:13:16 -0400520 normalize_curl_result(&req->curl_result, req->http_code,
521 req->errorstr, sizeof(req->errorstr));
Eric Wong17966c02016-07-11 20:51:30 +0000522
Nick Hengevelde388ab72005-11-18 11:03:04 -0800523 if (obj_req->state == ABORTED) {
Nick Hengeveld29508e12005-11-18 11:02:58 -0800524 ret = error("Request for %s aborted", hex);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800525 } else if (req->curl_result != CURLE_OK &&
526 req->http_code != 416) {
527 if (missing_target(req))
Petr Baudise2029eb2005-10-21 18:18:46 +0200528 ret = -1; /* Be silent, it is probably in a pack. */
529 else
530 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800531 req->errorstr, req->curl_result,
532 req->http_code, hex);
533 } else if (req->zret != Z_STREAM_END) {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400534 walker->corrupt_object_found++;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800535 ret = error("File %s (%s) corrupt", hex, req->url);
Jeff Kingf0be0db2019-01-07 03:34:40 -0500536 } else if (!oideq(&obj_req->oid, &req->real_oid)) {
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800537 ret = error("File %s has bad hash", hex);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800538 } else if (req->rename < 0) {
Christian Couderea657732018-01-17 18:54:54 +0100539 struct strbuf buf = STRBUF_INIT;
Jeff King514c5fd2019-01-07 03:35:42 -0500540 loose_object_path(the_repository, &buf, &req->oid);
Christian Couderea657732018-01-17 18:54:54 +0100541 ret = error("unable to write sha1 filename %s", buf.buf);
542 strbuf_release(&buf);
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700543 }
544
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800545 release_http_object_request(req);
Nick Hengevelde388ab72005-11-18 11:03:04 -0800546 release_object_request(obj_req);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800547 return ret;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700548}
549
brian m. carlson2bf1db72019-02-19 00:05:12 +0000550static int fetch(struct walker *walker, unsigned char *hash)
Daniel Barkalowb3661562005-09-14 23:26:08 -0400551{
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400552 struct walker_data *data = walker->data;
553 struct alt_base *altbase = data->alt;
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700554
brian m. carlson2bf1db72019-02-19 00:05:12 +0000555 if (!fetch_object(walker, hash))
Nick Hengeveld1d389ab2005-10-10 23:22:01 -0700556 return 0;
Daniel Barkalowb3661562005-09-14 23:26:08 -0400557 while (altbase) {
brian m. carlson2bf1db72019-02-19 00:05:12 +0000558 if (!http_fetch_pack(walker, altbase, hash))
Daniel Barkalowb3661562005-09-14 23:26:08 -0400559 return 0;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400560 fetch_alternates(walker, data->alt->base);
Daniel Barkalowb3661562005-09-14 23:26:08 -0400561 altbase = altbase->next;
562 }
brian m. carlson2bf1db72019-02-19 00:05:12 +0000563 return error("Unable to find %s under %s", hash_to_hex(hash),
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400564 data->alt->base);
Daniel Barkalowb3661562005-09-14 23:26:08 -0400565}
566
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400567static int fetch_ref(struct walker *walker, struct ref *ref)
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400568{
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400569 struct walker_data *data = walker->data;
Daniel Barkalowc13b2632008-04-26 15:53:09 -0400570 return http_fetch_ref(data->alt->base, ref);
Daniel Barkalowcd541a62005-06-06 16:38:26 -0400571}
572
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400573static void cleanup(struct walker *walker)
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700574{
Tay Ray Chuan09ae9ac2010-03-02 18:49:28 +0800575 struct walker_data *data = walker->data;
576 struct alt_base *alt, *alt_next;
577
578 if (data) {
579 alt = data->alt;
580 while (alt) {
581 alt_next = alt->next;
582
583 free(alt->base);
584 free(alt);
585
586 alt = alt_next;
587 }
588 free(data);
589 walker->data = NULL;
590 }
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400591}
592
Tay Ray Chuan888692b2010-03-02 18:49:29 +0800593struct walker *get_http_walker(const char *url)
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400594{
Gerrit Pape9c880b32007-03-28 09:47:35 +0000595 char *s;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400596 struct walker_data *data = xmalloc(sizeof(struct walker_data));
597 struct walker *walker = xmalloc(sizeof(struct walker));
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700598
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400599 data->alt = xmalloc(sizeof(*data->alt));
Jeff King95244ae2014-06-19 17:19:43 -0400600 data->alt->base = xstrdup(url);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400601 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
Gerrit Pape9c880b32007-03-28 09:47:35 +0000602 *s = 0;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400603
604 data->alt->got_indices = 0;
605 data->alt->packs = NULL;
606 data->alt->next = NULL;
607 data->got_alternates = -1;
608
609 walker->corrupt_object_found = 0;
610 walker->fetch = fetch;
611 walker->fetch_ref = fetch_ref;
612 walker->prefetch = prefetch;
613 walker->cleanup = cleanup;
614 walker->data = data;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700615
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400616 add_fill_function(walker, (int (*)(void *)) fill_active_slot);
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400617
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400618 return walker;
Daniel Barkalow6eb7ed52005-04-23 18:47:23 -0700619}