Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Stefan Beller | cf78ae4 | 2018-03-23 18:21:10 +0100 | [diff] [blame] | 2 | #include "repository.h" |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 3 | #include "commit.h" |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 4 | #include "walker.h" |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 5 | #include "http.h" |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 6 | #include "list.h" |
Jeff King | abcbdc0 | 2016-12-14 14:39:55 -0800 | [diff] [blame] | 7 | #include "transport.h" |
Jonathan Tan | d6fe003 | 2017-08-18 15:20:34 -0700 | [diff] [blame] | 8 | #include "packfile.h" |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 9 | #include "object-store.h" |
Johannes Schindelin | 7baa3e8 | 2005-10-15 11:10:46 -0700 | [diff] [blame] | 10 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 11 | struct alt_base { |
Gerrit Pape | 2afea3b | 2007-03-28 09:46:15 +0000 | [diff] [blame] | 12 | char *base; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 13 | int got_indices; |
| 14 | struct packed_git *packs; |
| 15 | struct alt_base *next; |
| 16 | }; |
| 17 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 18 | enum object_request_state { |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 19 | WAITING, |
| 20 | ABORTED, |
| 21 | ACTIVE, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 22 | COMPLETE |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 23 | }; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 24 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 25 | struct object_request { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 26 | struct walker *walker; |
brian m. carlson | 16f0705 | 2018-03-12 02:27:31 +0000 | [diff] [blame] | 27 | struct object_id oid; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 28 | struct alt_base *repo; |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 29 | enum object_request_state state; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 30 | struct http_object_request *req; |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 31 | struct list_head node; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 34 | struct alternates_request { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 35 | struct walker *walker; |
Petr Baudis | 8e29f6a | 2006-07-27 23:56:22 +0200 | [diff] [blame] | 36 | const char *base; |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 37 | struct strbuf *url; |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 38 | struct strbuf *buffer; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 39 | struct active_request_slot *slot; |
| 40 | int http_specific; |
| 41 | }; |
| 42 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 43 | struct walker_data { |
| 44 | const char *url; |
| 45 | int got_alternates; |
| 46 | struct alt_base *alt; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 47 | }; |
| 48 | |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 49 | static LIST_HEAD(object_queue_head); |
Sergey Vlasov | bc8f265 | 2005-10-13 10:49:53 -0700 | [diff] [blame] | 50 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 51 | static void fetch_alternates(struct walker *walker, const char *base); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 52 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 53 | static void process_object_response(void *callback_data); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 54 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 55 | static void start_object_request(struct walker *walker, |
| 56 | struct object_request *obj_req) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 57 | { |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 58 | struct active_request_slot *slot; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 59 | struct http_object_request *req; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 60 | |
Jeff King | f0be0db | 2019-01-07 03:34:40 -0500 | [diff] [blame] | 61 | req = new_http_object_request(obj_req->repo->base, &obj_req->oid); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 62 | if (req == NULL) { |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 63 | obj_req->state = ABORTED; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 66 | obj_req->req = req; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 67 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 68 | slot = req->slot; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 69 | slot->callback_func = process_object_response; |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 70 | slot->callback_data = obj_req; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 71 | |
Nick Hengeveld | a7a8d37 | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 72 | /* Try to get the request started, abort the request on error */ |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 73 | obj_req->state = ACTIVE; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 74 | if (!start_active_slot(slot)) { |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 75 | obj_req->state = ABORTED; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 76 | release_http_object_request(req); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 77 | return; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 78 | } |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 81 | static void finish_object_request(struct object_request *obj_req) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 82 | { |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 83 | if (finish_http_object_request(obj_req->req)) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 84 | return; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 85 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 86 | if (obj_req->req->rename == 0) |
brian m. carlson | 16f0705 | 2018-03-12 02:27:31 +0000 | [diff] [blame] | 87 | walker_say(obj_req->walker, "got %s\n", oid_to_hex(&obj_req->oid)); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 90 | static void process_object_response(void *callback_data) |
| 91 | { |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 92 | struct object_request *obj_req = |
| 93 | (struct object_request *)callback_data; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 94 | struct walker *walker = obj_req->walker; |
| 95 | struct walker_data *data = walker->data; |
| 96 | struct alt_base *alt = data->alt; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 97 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 98 | process_http_object_request(obj_req->req); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 99 | obj_req->state = COMPLETE; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 100 | |
Jeff King | ccbbd8b | 2019-03-24 08:09:46 -0400 | [diff] [blame] | 101 | 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 Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 106 | /* Use alternates if necessary */ |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 107 | if (missing_target(obj_req->req)) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 108 | fetch_alternates(walker, alt->base); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 109 | if (obj_req->repo->next != NULL) { |
| 110 | obj_req->repo = |
| 111 | obj_req->repo->next; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 112 | release_http_object_request(obj_req->req); |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 113 | start_object_request(walker, obj_req); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 118 | finish_object_request(obj_req); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 121 | static void release_object_request(struct object_request *obj_req) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 122 | { |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 123 | if (obj_req->req !=NULL && obj_req->req->localfile != -1) |
| 124 | error("fd leakage in release: %d", obj_req->req->localfile); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 125 | |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 126 | list_del(&obj_req->node); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 127 | free(obj_req); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 130 | static int fill_active_slot(struct walker *walker) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 131 | { |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 132 | struct object_request *obj_req; |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 133 | struct list_head *pos, *tmp, *head = &object_queue_head; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 134 | |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 135 | list_for_each_safe(pos, tmp, head) { |
| 136 | obj_req = list_entry(pos, struct object_request, node); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 137 | if (obj_req->state == WAITING) { |
Jeff King | 98374a0 | 2019-01-07 03:37:54 -0500 | [diff] [blame] | 138 | if (has_object_file(&obj_req->oid)) |
Mark Wooding | 09db444 | 2006-02-01 11:44:28 +0000 | [diff] [blame] | 139 | obj_req->state = COMPLETE; |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 140 | else { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 141 | start_object_request(walker, obj_req); |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 142 | return 1; |
| 143 | } |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 144 | } |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 145 | } |
Daniel Barkalow | 45c1741 | 2007-09-10 23:02:28 -0400 | [diff] [blame] | 146 | return 0; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 149 | static void prefetch(struct walker *walker, unsigned char *sha1) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 150 | { |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 151 | struct object_request *newreq; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 152 | struct walker_data *data = walker->data; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 153 | |
| 154 | newreq = xmalloc(sizeof(*newreq)); |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 155 | newreq->walker = walker; |
brian m. carlson | 92e2cab | 2021-04-26 01:02:50 +0000 | [diff] [blame] | 156 | oidread(&newreq->oid, sha1); |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 157 | newreq->repo = data->alt; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 158 | newreq->state = WAITING; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 159 | newreq->req = NULL; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 160 | |
Tay Ray Chuan | e917674 | 2009-06-06 16:43:41 +0800 | [diff] [blame] | 161 | http_is_verbose = walker->get_verbosely; |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 162 | list_add_tail(&newreq->node, &object_queue_head); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 163 | |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 164 | fill_active_slots(); |
| 165 | step_active_slots(); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jeff King | abcbdc0 | 2016-12-14 14:39:55 -0800 | [diff] [blame] | 168 | static int is_alternate_allowed(const char *url) |
| 169 | { |
| 170 | const char *protocols[] = { |
| 171 | "http", "https", "ftp", "ftps" |
| 172 | }; |
| 173 | int i; |
| 174 | |
Eric Wong | de46138 | 2017-03-04 08:36:45 +0000 | [diff] [blame] | 175 | if (http_follow_config != HTTP_FOLLOW_ALWAYS) { |
| 176 | warning("alternate disabled by http.followRedirects: %s", url); |
| 177 | return 0; |
| 178 | } |
| 179 | |
Jeff King | abcbdc0 | 2016-12-14 14:39:55 -0800 | [diff] [blame] | 180 | 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 Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 199 | static void process_alternates_response(void *callback_data) |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 200 | { |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 201 | struct alternates_request *alt_req = |
| 202 | (struct alternates_request *)callback_data; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 203 | struct walker *walker = alt_req->walker; |
| 204 | struct walker_data *cdata = walker->data; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 205 | struct active_request_slot *slot = alt_req->slot; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 206 | struct alt_base *tail = cdata->alt; |
Petr Baudis | 8e29f6a | 2006-07-27 23:56:22 +0200 | [diff] [blame] | 207 | const char *base = alt_req->base; |
Dan McGee | a04ff3e | 2011-05-03 23:47:27 +0800 | [diff] [blame] | 208 | const char null_byte = '\0'; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 209 | char *data; |
| 210 | int i = 0; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 211 | |
Jeff King | ccbbd8b | 2019-03-24 08:09:46 -0400 | [diff] [blame] | 212 | normalize_curl_result(&slot->curl_result, slot->http_code, |
| 213 | curl_errorstr, sizeof(curl_errorstr)); |
| 214 | |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 215 | if (alt_req->http_specific) { |
| 216 | if (slot->curl_result != CURLE_OK || |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 217 | !alt_req->buffer->len) { |
Junio C Hamano | dc1b5ea | 2005-10-10 23:22:02 -0700 | [diff] [blame] | 218 | |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 219 | /* Try reusing the slot to get non-http alternates */ |
| 220 | alt_req->http_specific = 0; |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 221 | strbuf_reset(alt_req->url); |
| 222 | strbuf_addf(alt_req->url, "%s/objects/info/alternates", |
| 223 | base); |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 224 | curl_easy_setopt(slot->curl, CURLOPT_URL, |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 225 | alt_req->url->buf); |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 226 | active_requests++; |
| 227 | slot->in_use = 1; |
Nick Hengeveld | c982647 | 2006-03-15 08:59:52 -0800 | [diff] [blame] | 228 | if (slot->finished != NULL) |
| 229 | (*slot->finished) = 0; |
Mark Wooding | a3f583c | 2006-02-01 11:44:39 +0000 | [diff] [blame] | 230 | if (!start_active_slot(slot)) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 231 | cdata->got_alternates = -1; |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 232 | slot->in_use = 0; |
Nick Hengeveld | c982647 | 2006-03-15 08:59:52 -0800 | [diff] [blame] | 233 | if (slot->finished != NULL) |
| 234 | (*slot->finished) = 1; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 235 | } |
Mark Wooding | a3f583c | 2006-02-01 11:44:39 +0000 | [diff] [blame] | 236 | return; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 237 | } |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 238 | } else if (slot->curl_result != CURLE_OK) { |
Junio C Hamano | be4a015 | 2006-09-16 10:58:20 -0700 | [diff] [blame] | 239 | if (!missing_target(slot)) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 240 | cdata->got_alternates = -1; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 241 | return; |
| 242 | } |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Dan McGee | a04ff3e | 2011-05-03 23:47:27 +0800 | [diff] [blame] | 245 | fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer); |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 246 | alt_req->buffer->len--; |
| 247 | data = alt_req->buffer->buf; |
Daniel Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 248 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 249 | while (i < alt_req->buffer->len) { |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 250 | int posn = i; |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 251 | while (posn < alt_req->buffer->len && data[posn] != '\n') |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 252 | posn++; |
| 253 | if (data[posn] == '\n') { |
Daniel Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 254 | int okay = 0; |
| 255 | int serverlen = 0; |
| 256 | struct alt_base *newalt; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 257 | if (data[i] == '/') { |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 258 | /* |
| 259 | * This counts |
Junio C Hamano | 5df1e0d | 2006-09-12 23:53:27 -0700 | [diff] [blame] | 260 | * 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 Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 271 | } else if (!memcmp(data + i, "../", 3)) { |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 272 | /* |
| 273 | * Relative URL; chop the corresponding |
Junio C Hamano | 5df1e0d | 2006-09-12 23:53:27 -0700 | [diff] [blame] | 274 | * 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 Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 290 | i += 3; |
| 291 | serverlen = strlen(base); |
Junio C Hamano | 8fcf7f9 | 2006-01-31 18:15:51 -0800 | [diff] [blame] | 292 | while (i + 2 < posn && |
Daniel Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 293 | !memcmp(data + i, "../", 3)) { |
| 294 | do { |
| 295 | serverlen--; |
| 296 | } while (serverlen && |
| 297 | base[serverlen - 1] != '/'); |
| 298 | i += 3; |
| 299 | } |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 300 | /* If the server got removed, give up. */ |
Junio C Hamano | 8fcf7f9 | 2006-01-31 18:15:51 -0800 | [diff] [blame] | 301 | okay = strchr(base, ':') - base + 3 < |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 302 | serverlen; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 303 | } else if (alt_req->http_specific) { |
Daniel Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 304 | 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 Barkalow | 1b0c1e6 | 2005-09-18 14:14:19 -0400 | [diff] [blame] | 311 | if (okay) { |
René Scharfe | 59b8263 | 2014-08-30 17:55:45 +0200 | [diff] [blame] | 312 | struct strbuf target = STRBUF_INIT; |
| 313 | strbuf_add(&target, base, serverlen); |
Jeff King | d61434a | 2017-03-13 10:04:45 -0400 | [diff] [blame] | 314 | 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 King | abcbdc0 | 2016-12-14 14:39:55 -0800 | [diff] [blame] | 321 | 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 Wong | 5cae73d | 2017-03-04 01:50:16 +0000 | [diff] [blame] | 332 | } else { |
| 333 | strbuf_release(&target); |
Jeff King | abcbdc0 | 2016-12-14 14:39:55 -0800 | [diff] [blame] | 334 | } |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | i = posn + 1; |
| 338 | } |
Sergey Vlasov | bc8f265 | 2005-10-13 10:49:53 -0700 | [diff] [blame] | 339 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 340 | cdata->got_alternates = 1; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 343 | static void fetch_alternates(struct walker *walker, const char *base) |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 344 | { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 345 | struct strbuf buffer = STRBUF_INIT; |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 346 | struct strbuf url = STRBUF_INIT; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 347 | struct active_request_slot *slot; |
Nick Hengeveld | cb754fd | 2006-01-31 18:00:37 -0800 | [diff] [blame] | 348 | struct alternates_request alt_req; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 349 | struct walker_data *cdata = walker->data; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 350 | |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 351 | /* |
| 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 Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 356 | while (cdata->got_alternates == 0) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 357 | step_active_slots(); |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* Nothing to do if they've already been fetched */ |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 361 | if (cdata->got_alternates == 1) |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 362 | return; |
| 363 | |
| 364 | /* Start the fetch */ |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 365 | cdata->got_alternates = 0; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 366 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 367 | if (walker->get_verbosely) |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 368 | fprintf(stderr, "Getting alternates list for %s\n", base); |
Junio C Hamano | 8fcf7f9 | 2006-01-31 18:15:51 -0800 | [diff] [blame] | 369 | |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 370 | strbuf_addf(&url, "%s/objects/info/http-alternates", base); |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 371 | |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 372 | /* |
| 373 | * Use a callback to process the result, since another request |
| 374 | * may fail and need to have alternates loaded before continuing |
| 375 | */ |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 376 | slot = get_active_slot(); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 377 | slot->callback_func = process_alternates_response; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 378 | alt_req.walker = walker; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 379 | slot->callback_data = &alt_req; |
| 380 | |
Ævar Arnfjörð Bjarmason | 8dda4cb | 2021-07-30 19:59:46 +0200 | [diff] [blame] | 381 | curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buffer); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 382 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 383 | curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf); |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 384 | |
| 385 | alt_req.base = base; |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 386 | alt_req.url = &url; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 387 | 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 Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 394 | cdata->got_alternates = -1; |
Nick Hengeveld | acc075a | 2005-11-12 09:11:32 -0800 | [diff] [blame] | 395 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 396 | strbuf_release(&buffer); |
Jeff King | 54ba4c5 | 2015-09-24 17:07:31 -0400 | [diff] [blame] | 397 | strbuf_release(&url); |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 400 | static int fetch_indices(struct walker *walker, struct alt_base *repo) |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 401 | { |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 402 | int ret; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 403 | |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 404 | if (repo->got_indices) |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 405 | return 0; |
| 406 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 407 | if (walker->get_verbosely) |
Petr Baudis | 6fd72e3 | 2005-11-12 01:49:59 +0100 | [diff] [blame] | 408 | fprintf(stderr, "Getting pack list for %s\n", repo->base); |
Junio C Hamano | 8fcf7f9 | 2006-01-31 18:15:51 -0800 | [diff] [blame] | 409 | |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 410 | 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 Hengeveld | 5e3a769 | 2005-11-18 11:03:11 -0800 | [diff] [blame] | 417 | repo->got_indices = 0; |
Tay Ray Chuan | b8caac2 | 2009-06-06 16:43:59 +0800 | [diff] [blame] | 418 | ret = -1; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 419 | } |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 420 | |
Mike Hommey | 3a462bc | 2007-12-10 22:36:11 +0100 | [diff] [blame] | 421 | return ret; |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Michael Haggerty | 07c19e7 | 2012-09-09 08:19:38 +0200 | [diff] [blame] | 424 | static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1) |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 425 | { |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 426 | struct packed_git *target; |
Nick Hengeveld | 49a0f24 | 2005-09-28 10:14:04 -0700 | [diff] [blame] | 427 | int ret; |
Nick Hengeveld | cb754fd | 2006-01-31 18:00:37 -0800 | [diff] [blame] | 428 | struct slot_results results; |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 429 | struct http_pack_request *preq; |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 430 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 431 | if (fetch_indices(walker, repo)) |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 432 | return -1; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 433 | target = find_sha1_pack(sha1, repo->packs); |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 434 | if (!target) |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 435 | return -1; |
Jonathan Tan | eb05349 | 2020-06-10 13:57:16 -0700 | [diff] [blame] | 436 | close_pack_index(target); |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 437 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 438 | if (walker->get_verbosely) { |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 439 | fprintf(stderr, "Getting pack %s\n", |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 440 | hash_to_hex(target->hash)); |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 441 | fprintf(stderr, " which contains %s\n", |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 442 | hash_to_hex(sha1)); |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Jonathan Tan | eb05349 | 2020-06-10 13:57:16 -0700 | [diff] [blame] | 445 | preq = new_http_pack_request(target->hash, repo->base); |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 446 | if (preq == NULL) |
| 447 | goto abort; |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 448 | preq->slot->results = &results; |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 449 | |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 450 | if (start_active_slot(preq->slot)) { |
| 451 | run_active_slot(preq->slot); |
Nick Hengeveld | c8568e1 | 2006-01-31 11:06:55 -0800 | [diff] [blame] | 452 | if (results.curl_result != CURLE_OK) { |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 453 | error("Unable to get pack file %s\n%s", preq->url, |
| 454 | curl_errorstr); |
| 455 | goto abort; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 456 | } |
| 457 | } else { |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 458 | error("Unable to start request"); |
| 459 | goto abort; |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 462 | ret = finish_http_pack_request(preq); |
| 463 | release_http_pack_request(preq); |
Nick Hengeveld | 49a0f24 | 2005-09-28 10:14:04 -0700 | [diff] [blame] | 464 | if (ret) |
Junio C Hamano | b721e01 | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 465 | return ret; |
Jonathan Tan | eb05349 | 2020-06-10 13:57:16 -0700 | [diff] [blame] | 466 | http_install_packfile(target, &repo->packs); |
Nick Hengeveld | 49a0f24 | 2005-09-28 10:14:04 -0700 | [diff] [blame] | 467 | |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 468 | return 0; |
Tay Ray Chuan | 2264dfa | 2009-06-06 16:44:01 +0800 | [diff] [blame] | 469 | |
| 470 | abort: |
| 471 | return -1; |
barkalow@iabervon.org | 182005b | 2005-07-31 20:54:17 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 474 | static void abort_object_request(struct object_request *obj_req) |
| 475 | { |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 476 | release_object_request(obj_req); |
| 477 | } |
| 478 | |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 479 | static int fetch_object(struct walker *walker, unsigned char *hash) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 480 | { |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 481 | char *hex = hash_to_hex(hash); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 482 | int ret = 0; |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 483 | struct object_request *obj_req = NULL; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 484 | struct http_object_request *req; |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 485 | struct list_head *pos, *head = &object_queue_head; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 486 | |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 487 | list_for_each(pos, head) { |
| 488 | obj_req = list_entry(pos, struct object_request, node); |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 489 | if (hasheq(obj_req->oid.hash, hash)) |
Eric Wong | 94e9901 | 2016-07-11 20:51:31 +0000 | [diff] [blame] | 490 | break; |
| 491 | } |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 492 | if (obj_req == NULL) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 493 | return error("Couldn't find request for %s in the queue", hex); |
Junio C Hamano | 271421c | 2005-09-30 00:07:39 -0700 | [diff] [blame] | 494 | |
Jeff King | 98374a0 | 2019-01-07 03:37:54 -0500 | [diff] [blame] | 495 | if (has_object_file(&obj_req->oid)) { |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 496 | if (obj_req->req != NULL) |
| 497 | abort_http_object_request(obj_req->req); |
Mark Wooding | 53f3138 | 2006-02-07 10:07:39 +0000 | [diff] [blame] | 498 | abort_object_request(obj_req); |
Nick Hengeveld | 11f0daf | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 502 | while (obj_req->state == WAITING) |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 503 | step_active_slots(); |
Nick Hengeveld | 49a0f24 | 2005-09-28 10:14:04 -0700 | [diff] [blame] | 504 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 505 | /* |
| 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 Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 510 | while (obj_req->state == ACTIVE) |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 511 | run_active_slot(obj_req->req->slot); |
Tay Ray Chuan | 4c42aa1 | 2009-06-06 16:43:33 +0800 | [diff] [blame] | 512 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 513 | req = obj_req->req; |
| 514 | |
| 515 | if (req->localfile != -1) { |
| 516 | close(req->localfile); |
| 517 | req->localfile = -1; |
Petr Baudis | 313c471 | 2005-11-12 00:55:16 +0100 | [diff] [blame] | 518 | } |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 519 | |
Jeff King | 3d10f72 | 2019-03-24 08:13:16 -0400 | [diff] [blame] | 520 | normalize_curl_result(&req->curl_result, req->http_code, |
| 521 | req->errorstr, sizeof(req->errorstr)); |
Eric Wong | 17966c0 | 2016-07-11 20:51:30 +0000 | [diff] [blame] | 522 | |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 523 | if (obj_req->state == ABORTED) { |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 524 | ret = error("Request for %s aborted", hex); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 525 | } else if (req->curl_result != CURLE_OK && |
| 526 | req->http_code != 416) { |
| 527 | if (missing_target(req)) |
Petr Baudis | e2029eb | 2005-10-21 18:18:46 +0200 | [diff] [blame] | 528 | 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 Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 531 | req->errorstr, req->curl_result, |
| 532 | req->http_code, hex); |
| 533 | } else if (req->zret != Z_STREAM_END) { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 534 | walker->corrupt_object_found++; |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 535 | ret = error("File %s (%s) corrupt", hex, req->url); |
Jeff King | f0be0db | 2019-01-07 03:34:40 -0500 | [diff] [blame] | 536 | } else if (!oideq(&obj_req->oid, &req->real_oid)) { |
Junio C Hamano | bd2afde | 2006-02-22 17:47:10 -0800 | [diff] [blame] | 537 | ret = error("File %s has bad hash", hex); |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 538 | } else if (req->rename < 0) { |
Christian Couder | ea65773 | 2018-01-17 18:54:54 +0100 | [diff] [blame] | 539 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 514c5fd | 2019-01-07 03:35:42 -0500 | [diff] [blame] | 540 | loose_object_path(the_repository, &buf, &req->oid); |
Christian Couder | ea65773 | 2018-01-17 18:54:54 +0100 | [diff] [blame] | 541 | ret = error("unable to write sha1 filename %s", buf.buf); |
| 542 | strbuf_release(&buf); |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Tay Ray Chuan | 5424bc5 | 2009-06-06 16:44:02 +0800 | [diff] [blame] | 545 | release_http_object_request(req); |
Nick Hengeveld | e388ab7 | 2005-11-18 11:03:04 -0800 | [diff] [blame] | 546 | release_object_request(obj_req); |
Nick Hengeveld | 29508e1 | 2005-11-18 11:02:58 -0800 | [diff] [blame] | 547 | return ret; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 548 | } |
| 549 | |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 550 | static int fetch(struct walker *walker, unsigned char *hash) |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 551 | { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 552 | struct walker_data *data = walker->data; |
| 553 | struct alt_base *altbase = data->alt; |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 554 | |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 555 | if (!fetch_object(walker, hash)) |
Nick Hengeveld | 1d389ab | 2005-10-10 23:22:01 -0700 | [diff] [blame] | 556 | return 0; |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 557 | while (altbase) { |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 558 | if (!http_fetch_pack(walker, altbase, hash)) |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 559 | return 0; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 560 | fetch_alternates(walker, data->alt->base); |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 561 | altbase = altbase->next; |
| 562 | } |
brian m. carlson | 2bf1db7 | 2019-02-19 00:05:12 +0000 | [diff] [blame] | 563 | return error("Unable to find %s under %s", hash_to_hex(hash), |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 564 | data->alt->base); |
Daniel Barkalow | b366156 | 2005-09-14 23:26:08 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 567 | static int fetch_ref(struct walker *walker, struct ref *ref) |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 568 | { |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 569 | struct walker_data *data = walker->data; |
Daniel Barkalow | c13b263 | 2008-04-26 15:53:09 -0400 | [diff] [blame] | 570 | return http_fetch_ref(data->alt->base, ref); |
Daniel Barkalow | cd541a6 | 2005-06-06 16:38:26 -0400 | [diff] [blame] | 571 | } |
| 572 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 573 | static void cleanup(struct walker *walker) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 574 | { |
Tay Ray Chuan | 09ae9ac | 2010-03-02 18:49:28 +0800 | [diff] [blame] | 575 | 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 Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 591 | } |
| 592 | |
Tay Ray Chuan | 888692b | 2010-03-02 18:49:29 +0800 | [diff] [blame] | 593 | struct walker *get_http_walker(const char *url) |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 594 | { |
Gerrit Pape | 9c880b3 | 2007-03-28 09:47:35 +0000 | [diff] [blame] | 595 | char *s; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 596 | struct walker_data *data = xmalloc(sizeof(struct walker_data)); |
| 597 | struct walker *walker = xmalloc(sizeof(struct walker)); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 598 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 599 | data->alt = xmalloc(sizeof(*data->alt)); |
Jeff King | 95244ae | 2014-06-19 17:19:43 -0400 | [diff] [blame] | 600 | data->alt->base = xstrdup(url); |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 601 | for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s) |
Gerrit Pape | 9c880b3 | 2007-03-28 09:47:35 +0000 | [diff] [blame] | 602 | *s = 0; |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 603 | |
| 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 Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 615 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 616 | add_fill_function(walker, (int (*)(void *)) fill_active_slot); |
Daniel Barkalow | fc57b6a | 2007-09-10 23:02:34 -0400 | [diff] [blame] | 617 | |
Daniel Barkalow | 30ae764 | 2007-09-10 23:02:45 -0400 | [diff] [blame] | 618 | return walker; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 619 | } |