]>
Commit | Line | Data |
---|---|---|
6eb7ed54 DB |
1 | #include "cache.h" |
2 | #include "commit.h" | |
30ae764b | 3 | #include "walker.h" |
29508e1e | 4 | #include "http.h" |
7baa3e86 | 5 | |
49a0f240 | 6 | #define PREV_BUF_SIZE 4096 |
49a0f240 | 7 | |
b3661567 DB |
8 | struct alt_base |
9 | { | |
2afea3bc | 10 | char *base; |
b3661567 DB |
11 | int got_indices; |
12 | struct packed_git *packs; | |
13 | struct alt_base *next; | |
14 | }; | |
15 | ||
e388ab74 | 16 | enum object_request_state { |
1d389ab6 NH |
17 | WAITING, |
18 | ABORTED, | |
19 | ACTIVE, | |
20 | COMPLETE, | |
21 | }; | |
6eb7ed54 | 22 | |
e388ab74 | 23 | struct object_request |
1d389ab6 | 24 | { |
30ae764b | 25 | struct walker *walker; |
1d389ab6 NH |
26 | unsigned char sha1[20]; |
27 | struct alt_base *repo; | |
28 | char *url; | |
29 | char filename[PATH_MAX]; | |
30 | char tmpfile[PATH_MAX]; | |
31 | int local; | |
e388ab74 | 32 | enum object_request_state state; |
1d389ab6 NH |
33 | CURLcode curl_result; |
34 | char errorstr[CURL_ERROR_SIZE]; | |
35 | long http_code; | |
36 | unsigned char real_sha1[20]; | |
9126f009 | 37 | git_SHA_CTX c; |
1d389ab6 NH |
38 | z_stream stream; |
39 | int zret; | |
40 | int rename; | |
41 | struct active_request_slot *slot; | |
e388ab74 | 42 | struct object_request *next; |
1d389ab6 NH |
43 | }; |
44 | ||
e388ab74 | 45 | struct alternates_request { |
30ae764b | 46 | struct walker *walker; |
8e29f6a0 | 47 | const char *base; |
acc075a8 | 48 | char *url; |
028c2976 | 49 | struct strbuf *buffer; |
acc075a8 NH |
50 | struct active_request_slot *slot; |
51 | int http_specific; | |
52 | }; | |
53 | ||
30ae764b DB |
54 | struct walker_data { |
55 | const char *url; | |
56 | int got_alternates; | |
57 | struct alt_base *alt; | |
30ae764b DB |
58 | }; |
59 | ||
96f1e58f | 60 | static struct object_request *object_queue_head; |
bc8f2652 | 61 | |
182005b9 DB |
62 | static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, |
63 | void *data) | |
64 | { | |
bf0f910d | 65 | unsigned char expn[4096]; |
6eb7ed54 DB |
66 | size_t size = eltsize * nmemb; |
67 | int posn = 0; | |
e388ab74 | 68 | struct object_request *obj_req = (struct object_request *)data; |
6eb7ed54 | 69 | do { |
93822c22 | 70 | ssize_t retval = xwrite(obj_req->local, |
4c42aa1a | 71 | (char *) ptr + posn, size - posn); |
6eb7ed54 DB |
72 | if (retval < 0) |
73 | return posn; | |
74 | posn += retval; | |
75 | } while (posn < size); | |
76 | ||
e388ab74 NH |
77 | obj_req->stream.avail_in = size; |
78 | obj_req->stream.next_in = ptr; | |
6eb7ed54 | 79 | do { |
e388ab74 NH |
80 | obj_req->stream.next_out = expn; |
81 | obj_req->stream.avail_out = sizeof(expn); | |
39c68542 | 82 | obj_req->zret = git_inflate(&obj_req->stream, Z_SYNC_FLUSH); |
9126f009 | 83 | git_SHA1_Update(&obj_req->c, expn, |
4c42aa1a | 84 | sizeof(expn) - obj_req->stream.avail_out); |
e388ab74 | 85 | } while (obj_req->stream.avail_in && obj_req->zret == Z_OK); |
1d389ab6 | 86 | data_received++; |
6eb7ed54 DB |
87 | return size; |
88 | } | |
89 | ||
30ae764b | 90 | static void fetch_alternates(struct walker *walker, const char *base); |
1d389ab6 | 91 | |
29508e1e | 92 | static void process_object_response(void *callback_data); |
1d389ab6 | 93 | |
30ae764b DB |
94 | static void start_object_request(struct walker *walker, |
95 | struct object_request *obj_req) | |
1d389ab6 | 96 | { |
e388ab74 | 97 | char *hex = sha1_to_hex(obj_req->sha1); |
1d389ab6 NH |
98 | char prevfile[PATH_MAX]; |
99 | char *url; | |
100 | char *posn; | |
101 | int prevlocal; | |
102 | unsigned char prev_buf[PREV_BUF_SIZE]; | |
103 | ssize_t prev_read = 0; | |
104 | long prev_posn = 0; | |
105 | char range[RANGE_HEADER_SIZE]; | |
106 | struct curl_slist *range_header = NULL; | |
107 | struct active_request_slot *slot; | |
108 | ||
e388ab74 | 109 | snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename); |
691f1a28 | 110 | unlink_or_warn(prevfile); |
e388ab74 | 111 | rename(obj_req->tmpfile, prevfile); |
691f1a28 | 112 | unlink_or_warn(obj_req->tmpfile); |
1d389ab6 | 113 | |
e388ab74 NH |
114 | if (obj_req->local != -1) |
115 | error("fd leakage in start: %d", obj_req->local); | |
116 | obj_req->local = open(obj_req->tmpfile, | |
1d389ab6 | 117 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
4c42aa1a TRC |
118 | /* |
119 | * This could have failed due to the "lazy directory creation"; | |
b721e01f JH |
120 | * try to mkdir the last path component. |
121 | */ | |
e388ab74 NH |
122 | if (obj_req->local < 0 && errno == ENOENT) { |
123 | char *dir = strrchr(obj_req->tmpfile, '/'); | |
b721e01f JH |
124 | if (dir) { |
125 | *dir = 0; | |
e388ab74 | 126 | mkdir(obj_req->tmpfile, 0777); |
b721e01f JH |
127 | *dir = '/'; |
128 | } | |
e388ab74 | 129 | obj_req->local = open(obj_req->tmpfile, |
b721e01f JH |
130 | O_WRONLY | O_CREAT | O_EXCL, 0666); |
131 | } | |
132 | ||
e388ab74 NH |
133 | if (obj_req->local < 0) { |
134 | obj_req->state = ABORTED; | |
bd2afde8 | 135 | error("Couldn't create temporary file %s for %s: %s", |
e388ab74 | 136 | obj_req->tmpfile, obj_req->filename, strerror(errno)); |
1d389ab6 NH |
137 | return; |
138 | } | |
139 | ||
e388ab74 | 140 | memset(&obj_req->stream, 0, sizeof(obj_req->stream)); |
1d389ab6 | 141 | |
39c68542 | 142 | git_inflate_init(&obj_req->stream); |
1d389ab6 | 143 | |
9126f009 | 144 | git_SHA1_Init(&obj_req->c); |
1d389ab6 | 145 | |
2afea3bc GP |
146 | url = xmalloc(strlen(obj_req->repo->base) + 51); |
147 | obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51); | |
e388ab74 NH |
148 | strcpy(url, obj_req->repo->base); |
149 | posn = url + strlen(obj_req->repo->base); | |
2afea3bc GP |
150 | strcpy(posn, "/objects/"); |
151 | posn += 9; | |
1d389ab6 NH |
152 | memcpy(posn, hex, 2); |
153 | posn += 2; | |
154 | *(posn++) = '/'; | |
155 | strcpy(posn, hex + 2); | |
e388ab74 | 156 | strcpy(obj_req->url, url); |
1d389ab6 | 157 | |
4c42aa1a TRC |
158 | /* |
159 | * If a previous temp file is present, process what was already | |
160 | * fetched. | |
161 | */ | |
1d389ab6 NH |
162 | prevlocal = open(prevfile, O_RDONLY); |
163 | if (prevlocal != -1) { | |
164 | do { | |
93d26e4c | 165 | prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE); |
1d389ab6 NH |
166 | if (prev_read>0) { |
167 | if (fwrite_sha1_file(prev_buf, | |
168 | 1, | |
169 | prev_read, | |
4c42aa1a | 170 | obj_req) == prev_read) |
1d389ab6 | 171 | prev_posn += prev_read; |
4c42aa1a | 172 | else |
1d389ab6 | 173 | prev_read = -1; |
1d389ab6 NH |
174 | } |
175 | } while (prev_read > 0); | |
176 | close(prevlocal); | |
177 | } | |
691f1a28 | 178 | unlink_or_warn(prevfile); |
1d389ab6 | 179 | |
4c42aa1a TRC |
180 | /* |
181 | * Reset inflate/SHA1 if there was an error reading the previous temp | |
182 | * file; also rewind to the beginning of the local file. | |
183 | */ | |
1d389ab6 | 184 | if (prev_read == -1) { |
e388ab74 | 185 | memset(&obj_req->stream, 0, sizeof(obj_req->stream)); |
39c68542 | 186 | git_inflate_init(&obj_req->stream); |
9126f009 | 187 | git_SHA1_Init(&obj_req->c); |
1d389ab6 NH |
188 | if (prev_posn>0) { |
189 | prev_posn = 0; | |
b5da2467 | 190 | lseek(obj_req->local, 0, SEEK_SET); |
e388ab74 | 191 | ftruncate(obj_req->local, 0); |
1d389ab6 NH |
192 | } |
193 | } | |
194 | ||
195 | slot = get_active_slot(); | |
29508e1e | 196 | slot->callback_func = process_object_response; |
e388ab74 NH |
197 | slot->callback_data = obj_req; |
198 | obj_req->slot = slot; | |
29508e1e | 199 | |
e388ab74 | 200 | curl_easy_setopt(slot->curl, CURLOPT_FILE, obj_req); |
1d389ab6 | 201 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file); |
e388ab74 | 202 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr); |
1d389ab6 | 203 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
e9176745 | 204 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header); |
1d389ab6 | 205 | |
4c42aa1a TRC |
206 | /* |
207 | * If we have successfully processed data from a previous fetch | |
208 | * attempt, only fetch the data we don't already have. | |
209 | */ | |
1d389ab6 | 210 | if (prev_posn>0) { |
30ae764b | 211 | if (walker->get_verbosely) |
1d389ab6 NH |
212 | fprintf(stderr, |
213 | "Resuming fetch of object %s at byte %ld\n", | |
214 | hex, prev_posn); | |
215 | sprintf(range, "Range: bytes=%ld-", prev_posn); | |
216 | range_header = curl_slist_append(range_header, range); | |
217 | curl_easy_setopt(slot->curl, | |
218 | CURLOPT_HTTPHEADER, range_header); | |
219 | } | |
220 | ||
a7a8d378 | 221 | /* Try to get the request started, abort the request on error */ |
e388ab74 | 222 | obj_req->state = ACTIVE; |
1d389ab6 | 223 | if (!start_active_slot(slot)) { |
e388ab74 NH |
224 | obj_req->state = ABORTED; |
225 | obj_req->slot = NULL; | |
4c42aa1a TRC |
226 | close(obj_req->local); |
227 | obj_req->local = -1; | |
e388ab74 NH |
228 | free(obj_req->url); |
229 | return; | |
1d389ab6 | 230 | } |
1d389ab6 NH |
231 | } |
232 | ||
e388ab74 | 233 | static void finish_object_request(struct object_request *obj_req) |
1d389ab6 | 234 | { |
50496b21 NH |
235 | struct stat st; |
236 | ||
4c42aa1a TRC |
237 | close(obj_req->local); |
238 | obj_req->local = -1; | |
1d389ab6 | 239 | |
e388ab74 | 240 | if (obj_req->http_code == 416) { |
1d389ab6 | 241 | fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n"); |
e388ab74 NH |
242 | } else if (obj_req->curl_result != CURLE_OK) { |
243 | if (stat(obj_req->tmpfile, &st) == 0) | |
50496b21 | 244 | if (st.st_size == 0) |
691f1a28 | 245 | unlink_or_warn(obj_req->tmpfile); |
1d389ab6 NH |
246 | return; |
247 | } | |
248 | ||
39c68542 | 249 | git_inflate_end(&obj_req->stream); |
9126f009 | 250 | git_SHA1_Final(obj_req->real_sha1, &obj_req->c); |
e388ab74 | 251 | if (obj_req->zret != Z_STREAM_END) { |
691f1a28 | 252 | unlink_or_warn(obj_req->tmpfile); |
1d389ab6 NH |
253 | return; |
254 | } | |
a89fccd2 | 255 | if (hashcmp(obj_req->sha1, obj_req->real_sha1)) { |
691f1a28 | 256 | unlink_or_warn(obj_req->tmpfile); |
1d389ab6 NH |
257 | return; |
258 | } | |
e388ab74 NH |
259 | obj_req->rename = |
260 | move_temp_to_file(obj_req->tmpfile, obj_req->filename); | |
1d389ab6 | 261 | |
e388ab74 | 262 | if (obj_req->rename == 0) |
30ae764b | 263 | walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1)); |
1d389ab6 NH |
264 | } |
265 | ||
29508e1e NH |
266 | static void process_object_response(void *callback_data) |
267 | { | |
e388ab74 NH |
268 | struct object_request *obj_req = |
269 | (struct object_request *)callback_data; | |
30ae764b DB |
270 | struct walker *walker = obj_req->walker; |
271 | struct walker_data *data = walker->data; | |
272 | struct alt_base *alt = data->alt; | |
29508e1e | 273 | |
e388ab74 NH |
274 | obj_req->curl_result = obj_req->slot->curl_result; |
275 | obj_req->http_code = obj_req->slot->http_code; | |
276 | obj_req->slot = NULL; | |
277 | obj_req->state = COMPLETE; | |
29508e1e NH |
278 | |
279 | /* Use alternates if necessary */ | |
be4a015b | 280 | if (missing_target(obj_req)) { |
30ae764b | 281 | fetch_alternates(walker, alt->base); |
e388ab74 NH |
282 | if (obj_req->repo->next != NULL) { |
283 | obj_req->repo = | |
284 | obj_req->repo->next; | |
285 | close(obj_req->local); | |
286 | obj_req->local = -1; | |
30ae764b | 287 | start_object_request(walker, obj_req); |
29508e1e NH |
288 | return; |
289 | } | |
290 | } | |
291 | ||
e388ab74 | 292 | finish_object_request(obj_req); |
29508e1e NH |
293 | } |
294 | ||
e388ab74 | 295 | static void release_object_request(struct object_request *obj_req) |
1d389ab6 | 296 | { |
e388ab74 | 297 | struct object_request *entry = object_queue_head; |
1d389ab6 | 298 | |
e388ab74 NH |
299 | if (obj_req->local != -1) |
300 | error("fd leakage in release: %d", obj_req->local); | |
301 | if (obj_req == object_queue_head) { | |
302 | object_queue_head = obj_req->next; | |
1d389ab6 | 303 | } else { |
e388ab74 | 304 | while (entry->next != NULL && entry->next != obj_req) |
1d389ab6 | 305 | entry = entry->next; |
e388ab74 | 306 | if (entry->next == obj_req) |
1d389ab6 NH |
307 | entry->next = entry->next->next; |
308 | } | |
309 | ||
e388ab74 NH |
310 | free(obj_req->url); |
311 | free(obj_req); | |
1d389ab6 NH |
312 | } |
313 | ||
a7a8d378 | 314 | #ifdef USE_CURL_MULTI |
30ae764b | 315 | static int fill_active_slot(struct walker *walker) |
1d389ab6 | 316 | { |
45c17412 | 317 | struct object_request *obj_req; |
1d389ab6 | 318 | |
45c17412 | 319 | for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) { |
e388ab74 NH |
320 | if (obj_req->state == WAITING) { |
321 | if (has_sha1_file(obj_req->sha1)) | |
09db444f | 322 | obj_req->state = COMPLETE; |
45c17412 | 323 | else { |
30ae764b | 324 | start_object_request(walker, obj_req); |
45c17412 DB |
325 | return 1; |
326 | } | |
f1a906a3 | 327 | } |
8fcf7f9a | 328 | } |
45c17412 | 329 | return 0; |
1d389ab6 | 330 | } |
a7a8d378 | 331 | #endif |
1d389ab6 | 332 | |
30ae764b | 333 | static void prefetch(struct walker *walker, unsigned char *sha1) |
1d389ab6 | 334 | { |
e388ab74 NH |
335 | struct object_request *newreq; |
336 | struct object_request *tail; | |
30ae764b | 337 | struct walker_data *data = walker->data; |
1d389ab6 NH |
338 | char *filename = sha1_file_name(sha1); |
339 | ||
340 | newreq = xmalloc(sizeof(*newreq)); | |
30ae764b | 341 | newreq->walker = walker; |
e702496e | 342 | hashcpy(newreq->sha1, sha1); |
30ae764b | 343 | newreq->repo = data->alt; |
1d389ab6 NH |
344 | newreq->url = NULL; |
345 | newreq->local = -1; | |
346 | newreq->state = WAITING; | |
347 | snprintf(newreq->filename, sizeof(newreq->filename), "%s", filename); | |
348 | snprintf(newreq->tmpfile, sizeof(newreq->tmpfile), | |
349 | "%s.temp", filename); | |
e8dff6ba | 350 | newreq->slot = NULL; |
1d389ab6 NH |
351 | newreq->next = NULL; |
352 | ||
e9176745 TRC |
353 | http_is_verbose = walker->get_verbosely; |
354 | ||
e388ab74 NH |
355 | if (object_queue_head == NULL) { |
356 | object_queue_head = newreq; | |
1d389ab6 | 357 | } else { |
e388ab74 | 358 | tail = object_queue_head; |
4c42aa1a | 359 | while (tail->next != NULL) |
1d389ab6 | 360 | tail = tail->next; |
1d389ab6 NH |
361 | tail->next = newreq; |
362 | } | |
29508e1e | 363 | |
a7a8d378 | 364 | #ifdef USE_CURL_MULTI |
29508e1e NH |
365 | fill_active_slots(); |
366 | step_active_slots(); | |
a7a8d378 | 367 | #endif |
1d389ab6 NH |
368 | } |
369 | ||
e388ab74 | 370 | static void process_alternates_response(void *callback_data) |
b3661567 | 371 | { |
e388ab74 NH |
372 | struct alternates_request *alt_req = |
373 | (struct alternates_request *)callback_data; | |
30ae764b DB |
374 | struct walker *walker = alt_req->walker; |
375 | struct walker_data *cdata = walker->data; | |
acc075a8 | 376 | struct active_request_slot *slot = alt_req->slot; |
30ae764b | 377 | struct alt_base *tail = cdata->alt; |
8e29f6a0 | 378 | const char *base = alt_req->base; |
bc8f2652 | 379 | static const char null_byte = '\0'; |
acc075a8 NH |
380 | char *data; |
381 | int i = 0; | |
1d389ab6 | 382 | |
acc075a8 NH |
383 | if (alt_req->http_specific) { |
384 | if (slot->curl_result != CURLE_OK || | |
028c2976 | 385 | !alt_req->buffer->len) { |
acc075a8 NH |
386 | |
387 | /* Try reusing the slot to get non-http alternates */ | |
388 | alt_req->http_specific = 0; | |
389 | sprintf(alt_req->url, "%s/objects/info/alternates", | |
390 | base); | |
391 | curl_easy_setopt(slot->curl, CURLOPT_URL, | |
392 | alt_req->url); | |
393 | active_requests++; | |
394 | slot->in_use = 1; | |
c9826473 NH |
395 | if (slot->finished != NULL) |
396 | (*slot->finished) = 0; | |
a3f583cb | 397 | if (!start_active_slot(slot)) { |
30ae764b | 398 | cdata->got_alternates = -1; |
29508e1e | 399 | slot->in_use = 0; |
c9826473 NH |
400 | if (slot->finished != NULL) |
401 | (*slot->finished) = 1; | |
1d389ab6 | 402 | } |
a3f583cb | 403 | return; |
b3661567 | 404 | } |
acc075a8 | 405 | } else if (slot->curl_result != CURLE_OK) { |
be4a015b | 406 | if (!missing_target(slot)) { |
30ae764b | 407 | cdata->got_alternates = -1; |
acc075a8 NH |
408 | return; |
409 | } | |
b3661567 DB |
410 | } |
411 | ||
29508e1e | 412 | fwrite_buffer(&null_byte, 1, 1, alt_req->buffer); |
028c2976 MH |
413 | alt_req->buffer->len--; |
414 | data = alt_req->buffer->buf; | |
1b0c1e67 | 415 | |
028c2976 | 416 | while (i < alt_req->buffer->len) { |
b3661567 | 417 | int posn = i; |
028c2976 | 418 | while (posn < alt_req->buffer->len && data[posn] != '\n') |
b3661567 DB |
419 | posn++; |
420 | if (data[posn] == '\n') { | |
1b0c1e67 DB |
421 | int okay = 0; |
422 | int serverlen = 0; | |
423 | struct alt_base *newalt; | |
424 | char *target = NULL; | |
b3661567 | 425 | if (data[i] == '/') { |
4c42aa1a TRC |
426 | /* |
427 | * This counts | |
5df1e0d0 JH |
428 | * http://git.host/pub/scm/linux.git/ |
429 | * -----------here^ | |
430 | * so memcpy(dst, base, serverlen) will | |
431 | * copy up to "...git.host". | |
432 | */ | |
433 | const char *colon_ss = strstr(base,"://"); | |
434 | if (colon_ss) { | |
435 | serverlen = (strchr(colon_ss + 3, '/') | |
436 | - base); | |
437 | okay = 1; | |
438 | } | |
1b0c1e67 | 439 | } else if (!memcmp(data + i, "../", 3)) { |
4c42aa1a TRC |
440 | /* |
441 | * Relative URL; chop the corresponding | |
5df1e0d0 JH |
442 | * number of subpath from base (and ../ |
443 | * from data), and concatenate the result. | |
444 | * | |
445 | * The code first drops ../ from data, and | |
446 | * then drops one ../ from data and one path | |
447 | * from base. IOW, one extra ../ is dropped | |
448 | * from data than path is dropped from base. | |
449 | * | |
450 | * This is not wrong. The alternate in | |
451 | * http://git.host/pub/scm/linux.git/ | |
452 | * to borrow from | |
453 | * http://git.host/pub/scm/linus.git/ | |
454 | * is ../../linus.git/objects/. You need | |
455 | * two ../../ to borrow from your direct | |
456 | * neighbour. | |
457 | */ | |
1b0c1e67 DB |
458 | i += 3; |
459 | serverlen = strlen(base); | |
8fcf7f9a | 460 | while (i + 2 < posn && |
1b0c1e67 DB |
461 | !memcmp(data + i, "../", 3)) { |
462 | do { | |
463 | serverlen--; | |
464 | } while (serverlen && | |
465 | base[serverlen - 1] != '/'); | |
466 | i += 3; | |
467 | } | |
a9486b02 | 468 | /* If the server got removed, give up. */ |
8fcf7f9a | 469 | okay = strchr(base, ':') - base + 3 < |
4c42aa1a | 470 | serverlen; |
acc075a8 | 471 | } else if (alt_req->http_specific) { |
1b0c1e67 DB |
472 | char *colon = strchr(data + i, ':'); |
473 | char *slash = strchr(data + i, '/'); | |
474 | if (colon && slash && colon < data + posn && | |
475 | slash < data + posn && colon < slash) { | |
476 | okay = 1; | |
477 | } | |
478 | } | |
5df1e0d0 | 479 | /* skip "objects\n" at end */ |
1b0c1e67 DB |
480 | if (okay) { |
481 | target = xmalloc(serverlen + posn - i - 6); | |
5df1e0d0 JH |
482 | memcpy(target, base, serverlen); |
483 | memcpy(target + serverlen, data + i, | |
484 | posn - i - 7); | |
485 | target[serverlen + posn - i - 7] = 0; | |
30ae764b | 486 | if (walker->get_verbosely) |
8fcf7f9a | 487 | fprintf(stderr, |
b3661567 DB |
488 | "Also look at %s\n", target); |
489 | newalt = xmalloc(sizeof(*newalt)); | |
1d389ab6 | 490 | newalt->next = NULL; |
b3661567 DB |
491 | newalt->base = target; |
492 | newalt->got_indices = 0; | |
493 | newalt->packs = NULL; | |
8d9fbe57 | 494 | |
1d389ab6 NH |
495 | while (tail->next != NULL) |
496 | tail = tail->next; | |
497 | tail->next = newalt; | |
b3661567 DB |
498 | } |
499 | } | |
500 | i = posn + 1; | |
501 | } | |
bc8f2652 | 502 | |
30ae764b | 503 | cdata->got_alternates = 1; |
acc075a8 NH |
504 | } |
505 | ||
30ae764b | 506 | static void fetch_alternates(struct walker *walker, const char *base) |
acc075a8 | 507 | { |
028c2976 | 508 | struct strbuf buffer = STRBUF_INIT; |
acc075a8 | 509 | char *url; |
acc075a8 | 510 | struct active_request_slot *slot; |
cb754fdf | 511 | struct alternates_request alt_req; |
30ae764b | 512 | struct walker_data *cdata = walker->data; |
acc075a8 | 513 | |
4c42aa1a TRC |
514 | /* |
515 | * If another request has already started fetching alternates, | |
516 | * wait for them to arrive and return to processing this request's | |
517 | * curl message | |
518 | */ | |
29508e1e | 519 | #ifdef USE_CURL_MULTI |
30ae764b | 520 | while (cdata->got_alternates == 0) { |
29508e1e | 521 | step_active_slots(); |
acc075a8 | 522 | } |
29508e1e | 523 | #endif |
acc075a8 NH |
524 | |
525 | /* Nothing to do if they've already been fetched */ | |
30ae764b | 526 | if (cdata->got_alternates == 1) |
acc075a8 NH |
527 | return; |
528 | ||
529 | /* Start the fetch */ | |
30ae764b | 530 | cdata->got_alternates = 0; |
acc075a8 | 531 | |
30ae764b | 532 | if (walker->get_verbosely) |
acc075a8 | 533 | fprintf(stderr, "Getting alternates list for %s\n", base); |
8fcf7f9a | 534 | |
acc075a8 NH |
535 | url = xmalloc(strlen(base) + 31); |
536 | sprintf(url, "%s/objects/info/http-alternates", base); | |
537 | ||
4c42aa1a TRC |
538 | /* |
539 | * Use a callback to process the result, since another request | |
540 | * may fail and need to have alternates loaded before continuing | |
541 | */ | |
acc075a8 | 542 | slot = get_active_slot(); |
e388ab74 | 543 | slot->callback_func = process_alternates_response; |
30ae764b | 544 | alt_req.walker = walker; |
acc075a8 NH |
545 | slot->callback_data = &alt_req; |
546 | ||
547 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); | |
29508e1e | 548 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
acc075a8 NH |
549 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
550 | ||
551 | alt_req.base = base; | |
552 | alt_req.url = url; | |
553 | alt_req.buffer = &buffer; | |
554 | alt_req.http_specific = 1; | |
555 | alt_req.slot = slot; | |
556 | ||
557 | if (start_active_slot(slot)) | |
558 | run_active_slot(slot); | |
559 | else | |
30ae764b | 560 | cdata->got_alternates = -1; |
acc075a8 | 561 | |
028c2976 | 562 | strbuf_release(&buffer); |
acc075a8 | 563 | free(url); |
b3661567 DB |
564 | } |
565 | ||
30ae764b | 566 | static int fetch_indices(struct walker *walker, struct alt_base *repo) |
182005b9 | 567 | { |
b8caac2b | 568 | int ret; |
1d389ab6 | 569 | |
b3661567 | 570 | if (repo->got_indices) |
182005b9 DB |
571 | return 0; |
572 | ||
30ae764b | 573 | if (walker->get_verbosely) |
6fd72e39 | 574 | fprintf(stderr, "Getting pack list for %s\n", repo->base); |
8fcf7f9a | 575 | |
b8caac2b TRC |
576 | switch (http_get_info_packs(repo->base, &repo->packs)) { |
577 | case HTTP_OK: | |
578 | case HTTP_MISSING_TARGET: | |
579 | repo->got_indices = 1; | |
580 | ret = 0; | |
581 | break; | |
582 | default: | |
5e3a7691 | 583 | repo->got_indices = 0; |
b8caac2b | 584 | ret = -1; |
b3661567 | 585 | } |
182005b9 | 586 | |
3a462bc9 | 587 | return ret; |
182005b9 DB |
588 | } |
589 | ||
30ae764b | 590 | static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1) |
182005b9 | 591 | { |
182005b9 | 592 | struct packed_git *target; |
49a0f240 | 593 | int ret; |
cb754fdf | 594 | struct slot_results results; |
2264dfa5 | 595 | struct http_pack_request *preq; |
182005b9 | 596 | |
30ae764b | 597 | if (fetch_indices(walker, repo)) |
182005b9 | 598 | return -1; |
b3661567 | 599 | target = find_sha1_pack(sha1, repo->packs); |
182005b9 | 600 | if (!target) |
b3661567 | 601 | return -1; |
182005b9 | 602 | |
30ae764b | 603 | if (walker->get_verbosely) { |
182005b9 DB |
604 | fprintf(stderr, "Getting pack %s\n", |
605 | sha1_to_hex(target->sha1)); | |
606 | fprintf(stderr, " which contains %s\n", | |
607 | sha1_to_hex(sha1)); | |
608 | } | |
609 | ||
2264dfa5 TRC |
610 | preq = new_http_pack_request(target, repo->base); |
611 | if (preq == NULL) | |
612 | goto abort; | |
613 | preq->lst = &repo->packs; | |
614 | preq->slot->results = &results; | |
182005b9 | 615 | |
2264dfa5 TRC |
616 | if (start_active_slot(preq->slot)) { |
617 | run_active_slot(preq->slot); | |
c8568e13 | 618 | if (results.curl_result != CURLE_OK) { |
2264dfa5 TRC |
619 | error("Unable to get pack file %s\n%s", preq->url, |
620 | curl_errorstr); | |
621 | goto abort; | |
1d389ab6 NH |
622 | } |
623 | } else { | |
2264dfa5 TRC |
624 | error("Unable to start request"); |
625 | goto abort; | |
182005b9 DB |
626 | } |
627 | ||
2264dfa5 TRC |
628 | ret = finish_http_pack_request(preq); |
629 | release_http_pack_request(preq); | |
49a0f240 | 630 | if (ret) |
b721e01f | 631 | return ret; |
49a0f240 | 632 | |
182005b9 | 633 | return 0; |
2264dfa5 TRC |
634 | |
635 | abort: | |
636 | return -1; | |
182005b9 DB |
637 | } |
638 | ||
53f31389 MW |
639 | static void abort_object_request(struct object_request *obj_req) |
640 | { | |
641 | if (obj_req->local >= 0) { | |
642 | close(obj_req->local); | |
643 | obj_req->local = -1; | |
644 | } | |
691f1a28 | 645 | unlink_or_warn(obj_req->tmpfile); |
53f31389 | 646 | if (obj_req->slot) { |
a6080a0a | 647 | release_active_slot(obj_req->slot); |
53f31389 MW |
648 | obj_req->slot = NULL; |
649 | } | |
650 | release_object_request(obj_req); | |
651 | } | |
652 | ||
30ae764b | 653 | static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1) |
6eb7ed54 DB |
654 | { |
655 | char *hex = sha1_to_hex(sha1); | |
29508e1e | 656 | int ret = 0; |
e388ab74 | 657 | struct object_request *obj_req = object_queue_head; |
1d389ab6 | 658 | |
a89fccd2 | 659 | while (obj_req != NULL && hashcmp(obj_req->sha1, sha1)) |
e388ab74 NH |
660 | obj_req = obj_req->next; |
661 | if (obj_req == NULL) | |
1d389ab6 NH |
662 | return error("Couldn't find request for %s in the queue", hex); |
663 | ||
e388ab74 | 664 | if (has_sha1_file(obj_req->sha1)) { |
53f31389 | 665 | abort_object_request(obj_req); |
11f0dafe NH |
666 | return 0; |
667 | } | |
668 | ||
a7a8d378 | 669 | #ifdef USE_CURL_MULTI |
4c42aa1a | 670 | while (obj_req->state == WAITING) |
29508e1e | 671 | step_active_slots(); |
a7a8d378 | 672 | #else |
30ae764b | 673 | start_object_request(walker, obj_req); |
a7a8d378 | 674 | #endif |
6eb7ed54 | 675 | |
4c42aa1a | 676 | while (obj_req->state == ACTIVE) |
e388ab74 | 677 | run_active_slot(obj_req->slot); |
4c42aa1a | 678 | |
e388ab74 | 679 | if (obj_req->local != -1) { |
4c42aa1a TRC |
680 | close(obj_req->local); |
681 | obj_req->local = -1; | |
313c4714 | 682 | } |
6eb7ed54 | 683 | |
e388ab74 | 684 | if (obj_req->state == ABORTED) { |
29508e1e | 685 | ret = error("Request for %s aborted", hex); |
e388ab74 NH |
686 | } else if (obj_req->curl_result != CURLE_OK && |
687 | obj_req->http_code != 416) { | |
be4a015b | 688 | if (missing_target(obj_req)) |
e2029eb9 PB |
689 | ret = -1; /* Be silent, it is probably in a pack. */ |
690 | else | |
691 | ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)", | |
e388ab74 NH |
692 | obj_req->errorstr, obj_req->curl_result, |
693 | obj_req->http_code, hex); | |
694 | } else if (obj_req->zret != Z_STREAM_END) { | |
30ae764b | 695 | walker->corrupt_object_found++; |
bd2afde8 | 696 | ret = error("File %s (%s) corrupt", hex, obj_req->url); |
a89fccd2 | 697 | } else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) { |
bd2afde8 | 698 | ret = error("File %s has bad hash", hex); |
e388ab74 | 699 | } else if (obj_req->rename < 0) { |
7b934ec0 MW |
700 | ret = error("unable to write sha1 filename %s", |
701 | obj_req->filename); | |
6eb7ed54 | 702 | } |
49a0f240 | 703 | |
e388ab74 | 704 | release_object_request(obj_req); |
29508e1e | 705 | return ret; |
6eb7ed54 DB |
706 | } |
707 | ||
30ae764b | 708 | static int fetch(struct walker *walker, unsigned char *sha1) |
b3661567 | 709 | { |
30ae764b DB |
710 | struct walker_data *data = walker->data; |
711 | struct alt_base *altbase = data->alt; | |
1d389ab6 | 712 | |
30ae764b | 713 | if (!fetch_object(walker, altbase, sha1)) |
1d389ab6 | 714 | return 0; |
b3661567 | 715 | while (altbase) { |
30ae764b | 716 | if (!fetch_pack(walker, altbase, sha1)) |
b3661567 | 717 | return 0; |
30ae764b | 718 | fetch_alternates(walker, data->alt->base); |
b3661567 DB |
719 | altbase = altbase->next; |
720 | } | |
bd2afde8 | 721 | return error("Unable to find %s under %s", sha1_to_hex(sha1), |
30ae764b | 722 | data->alt->base); |
b3661567 DB |
723 | } |
724 | ||
c13b2633 | 725 | static int fetch_ref(struct walker *walker, struct ref *ref) |
cd541a68 | 726 | { |
30ae764b | 727 | struct walker_data *data = walker->data; |
c13b2633 | 728 | return http_fetch_ref(data->alt->base, ref); |
cd541a68 DB |
729 | } |
730 | ||
30ae764b DB |
731 | static void cleanup(struct walker *walker) |
732 | { | |
30ae764b | 733 | http_cleanup(); |
30ae764b DB |
734 | } |
735 | ||
9fc6440d | 736 | struct walker *get_http_walker(const char *url, struct remote *remote) |
6eb7ed54 | 737 | { |
9c880b3e | 738 | char *s; |
30ae764b DB |
739 | struct walker_data *data = xmalloc(sizeof(struct walker_data)); |
740 | struct walker *walker = xmalloc(sizeof(struct walker)); | |
6eb7ed54 | 741 | |
9fc6440d | 742 | http_init(remote); |
d402d556 | 743 | |
30ae764b DB |
744 | data->alt = xmalloc(sizeof(*data->alt)); |
745 | data->alt->base = xmalloc(strlen(url) + 1); | |
746 | strcpy(data->alt->base, url); | |
747 | for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s) | |
9c880b3e | 748 | *s = 0; |
6eb7ed54 | 749 | |
30ae764b DB |
750 | data->alt->got_indices = 0; |
751 | data->alt->packs = NULL; | |
752 | data->alt->next = NULL; | |
753 | data->got_alternates = -1; | |
fc57b6aa | 754 | |
30ae764b DB |
755 | walker->corrupt_object_found = 0; |
756 | walker->fetch = fetch; | |
757 | walker->fetch_ref = fetch_ref; | |
758 | walker->prefetch = prefetch; | |
759 | walker->cleanup = cleanup; | |
760 | walker->data = data; | |
6eb7ed54 | 761 | |
30ae764b DB |
762 | #ifdef USE_CURL_MULTI |
763 | add_fill_function(walker, (int (*)(void *)) fill_active_slot); | |
764 | #endif | |
8e29f6a0 | 765 | |
30ae764b | 766 | return walker; |
6eb7ed54 | 767 | } |