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