Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "bundle-uri.h" |
5 | | #include "bundle.h" |
6 | | #include "copy.h" |
7 | | #include "gettext.h" |
8 | | #include "refs.h" |
9 | | #include "run-command.h" |
10 | | #include "hashmap.h" |
11 | | #include "pkt-line.h" |
12 | | #include "config.h" |
13 | | #include "fetch-pack.h" |
14 | | #include "remote.h" |
15 | | #include "trace2.h" |
16 | | #include "odb.h" |
17 | | |
18 | | static struct { |
19 | | enum bundle_list_heuristic heuristic; |
20 | | const char *name; |
21 | | } heuristics[BUNDLE_HEURISTIC__COUNT] = { |
22 | | { BUNDLE_HEURISTIC_NONE, ""}, |
23 | | { BUNDLE_HEURISTIC_CREATIONTOKEN, "creationToken" }, |
24 | | }; |
25 | | |
26 | | static int compare_bundles(const void *hashmap_cmp_fn_data UNUSED, |
27 | | const struct hashmap_entry *he1, |
28 | | const struct hashmap_entry *he2, |
29 | | const void *id) |
30 | 0 | { |
31 | 0 | const struct remote_bundle_info *e1 = |
32 | 0 | container_of(he1, const struct remote_bundle_info, ent); |
33 | 0 | const struct remote_bundle_info *e2 = |
34 | 0 | container_of(he2, const struct remote_bundle_info, ent); |
35 | |
|
36 | 0 | return strcmp(e1->id, id ? (const char *)id : e2->id); |
37 | 0 | } |
38 | | |
39 | | void init_bundle_list(struct bundle_list *list) |
40 | 0 | { |
41 | 0 | memset(list, 0, sizeof(*list)); |
42 | | |
43 | | /* Implied defaults. */ |
44 | 0 | list->mode = BUNDLE_MODE_ALL; |
45 | 0 | list->version = 1; |
46 | |
|
47 | 0 | hashmap_init(&list->bundles, compare_bundles, NULL, 0); |
48 | 0 | } |
49 | | |
50 | | static int clear_remote_bundle_info(struct remote_bundle_info *bundle, |
51 | | void *data UNUSED) |
52 | 0 | { |
53 | 0 | FREE_AND_NULL(bundle->id); |
54 | 0 | FREE_AND_NULL(bundle->uri); |
55 | 0 | FREE_AND_NULL(bundle->file); |
56 | 0 | bundle->unbundled = 0; |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | | void clear_bundle_list(struct bundle_list *list) |
61 | 0 | { |
62 | 0 | if (!list) |
63 | 0 | return; |
64 | | |
65 | 0 | for_all_bundles_in_list(list, clear_remote_bundle_info, NULL); |
66 | 0 | hashmap_clear_and_free(&list->bundles, struct remote_bundle_info, ent); |
67 | 0 | free(list->baseURI); |
68 | 0 | } |
69 | | |
70 | | int for_all_bundles_in_list(struct bundle_list *list, |
71 | | bundle_iterator iter, |
72 | | void *data) |
73 | 0 | { |
74 | 0 | struct remote_bundle_info *info; |
75 | 0 | struct hashmap_iter i; |
76 | |
|
77 | 0 | hashmap_for_each_entry(&list->bundles, &i, info, ent) { |
78 | 0 | int result = iter(info, data); |
79 | |
|
80 | 0 | if (result) |
81 | 0 | return result; |
82 | 0 | } |
83 | | |
84 | 0 | return 0; |
85 | 0 | } |
86 | | |
87 | | static int summarize_bundle(struct remote_bundle_info *info, void *data) |
88 | 0 | { |
89 | 0 | FILE *fp = data; |
90 | 0 | fprintf(fp, "[bundle \"%s\"]\n", info->id); |
91 | 0 | if (info->uri) |
92 | 0 | fprintf(fp, "\turi = %s\n", info->uri); |
93 | 0 | else |
94 | 0 | fprintf(fp, "\t# uri = (missing)\n"); |
95 | |
|
96 | 0 | if (info->creationToken) |
97 | 0 | fprintf(fp, "\tcreationToken = %"PRIu64"\n", info->creationToken); |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | void print_bundle_list(FILE *fp, struct bundle_list *list) |
102 | 0 | { |
103 | 0 | const char *mode; |
104 | |
|
105 | 0 | switch (list->mode) { |
106 | 0 | case BUNDLE_MODE_ALL: |
107 | 0 | mode = "all"; |
108 | 0 | break; |
109 | | |
110 | 0 | case BUNDLE_MODE_ANY: |
111 | 0 | mode = "any"; |
112 | 0 | break; |
113 | | |
114 | 0 | case BUNDLE_MODE_NONE: |
115 | 0 | default: |
116 | 0 | mode = "<unknown>"; |
117 | 0 | } |
118 | | |
119 | 0 | fprintf(fp, "[bundle]\n"); |
120 | 0 | fprintf(fp, "\tversion = %d\n", list->version); |
121 | 0 | fprintf(fp, "\tmode = %s\n", mode); |
122 | |
|
123 | 0 | if (list->heuristic) { |
124 | 0 | int i; |
125 | 0 | for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) { |
126 | 0 | if (heuristics[i].heuristic == list->heuristic) { |
127 | 0 | fprintf(fp, "\theuristic = %s\n", |
128 | 0 | heuristics[list->heuristic].name); |
129 | 0 | break; |
130 | 0 | } |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | 0 | for_all_bundles_in_list(list, summarize_bundle, fp); |
135 | 0 | } |
136 | | |
137 | | /** |
138 | | * Given a key-value pair, update the state of the given bundle list. |
139 | | * Returns 0 if the key-value pair is understood. Returns -1 if the key |
140 | | * is not understood or the value is malformed. |
141 | | */ |
142 | | static int bundle_list_update(const char *key, const char *value, |
143 | | struct bundle_list *list) |
144 | 0 | { |
145 | 0 | struct strbuf id = STRBUF_INIT; |
146 | 0 | struct remote_bundle_info lookup = REMOTE_BUNDLE_INFO_INIT; |
147 | 0 | struct remote_bundle_info *bundle; |
148 | 0 | const char *subsection, *subkey; |
149 | 0 | size_t subsection_len; |
150 | |
|
151 | 0 | if (parse_config_key(key, "bundle", &subsection, &subsection_len, &subkey)) |
152 | 0 | return -1; |
153 | | |
154 | 0 | if (!subsection_len) { |
155 | 0 | if (!strcmp(subkey, "version")) { |
156 | 0 | int version; |
157 | 0 | if (!git_parse_int(value, &version)) |
158 | 0 | return -1; |
159 | 0 | if (version != 1) |
160 | 0 | return -1; |
161 | | |
162 | 0 | list->version = version; |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 0 | if (!strcmp(subkey, "mode")) { |
167 | 0 | if (!value) |
168 | 0 | return config_error_nonbool(key); |
169 | 0 | if (!strcmp(value, "all")) |
170 | 0 | list->mode = BUNDLE_MODE_ALL; |
171 | 0 | else if (!strcmp(value, "any")) |
172 | 0 | list->mode = BUNDLE_MODE_ANY; |
173 | 0 | else |
174 | 0 | return -1; |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | 0 | if (!strcmp(subkey, "heuristic")) { |
179 | 0 | int i; |
180 | |
|
181 | 0 | if (!value) |
182 | 0 | return config_error_nonbool(key); |
183 | 0 | for (i = 0; i < BUNDLE_HEURISTIC__COUNT; i++) { |
184 | 0 | if (heuristics[i].heuristic && |
185 | 0 | heuristics[i].name && |
186 | 0 | !strcmp(value, heuristics[i].name)) { |
187 | 0 | list->heuristic = heuristics[i].heuristic; |
188 | 0 | return 0; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | /* Ignore unknown heuristics. */ |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | | /* Ignore other unknown global keys. */ |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | 0 | strbuf_add(&id, subsection, subsection_len); |
201 | | |
202 | | /* |
203 | | * Check for an existing bundle with this <id>, or create one |
204 | | * if necessary. |
205 | | */ |
206 | 0 | lookup.id = id.buf; |
207 | 0 | hashmap_entry_init(&lookup.ent, strhash(lookup.id)); |
208 | 0 | if (!(bundle = hashmap_get_entry(&list->bundles, &lookup, ent, NULL))) { |
209 | 0 | CALLOC_ARRAY(bundle, 1); |
210 | 0 | bundle->id = strbuf_detach(&id, NULL); |
211 | 0 | hashmap_entry_init(&bundle->ent, strhash(bundle->id)); |
212 | 0 | hashmap_add(&list->bundles, &bundle->ent); |
213 | 0 | } |
214 | 0 | strbuf_release(&id); |
215 | |
|
216 | 0 | if (!strcmp(subkey, "uri")) { |
217 | 0 | if (!value) |
218 | 0 | return config_error_nonbool(key); |
219 | 0 | if (bundle->uri) |
220 | 0 | return -1; |
221 | 0 | bundle->uri = relative_url(list->baseURI, value, NULL); |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | 0 | if (!strcmp(subkey, "creationtoken")) { |
226 | 0 | if (!value) |
227 | 0 | return config_error_nonbool(key); |
228 | 0 | if (sscanf(value, "%"PRIu64, &bundle->creationToken) != 1) |
229 | 0 | warning(_("could not parse bundle list key %s with value '%s'"), |
230 | 0 | "creationToken", value); |
231 | 0 | return 0; |
232 | 0 | } |
233 | | |
234 | | /* |
235 | | * At this point, we ignore any information that we don't |
236 | | * understand, assuming it to be hints for a heuristic the client |
237 | | * does not currently understand. |
238 | | */ |
239 | 0 | return 0; |
240 | 0 | } |
241 | | |
242 | | static int config_to_bundle_list(const char *key, const char *value, |
243 | | const struct config_context *ctx UNUSED, |
244 | | void *data) |
245 | 0 | { |
246 | 0 | struct bundle_list *list = data; |
247 | 0 | return bundle_list_update(key, value, list); |
248 | 0 | } |
249 | | |
250 | | int bundle_uri_parse_config_format(const char *uri, |
251 | | const char *filename, |
252 | | struct bundle_list *list) |
253 | 0 | { |
254 | 0 | int result; |
255 | 0 | struct config_options opts = { |
256 | 0 | .error_action = CONFIG_ERROR_ERROR, |
257 | 0 | }; |
258 | |
|
259 | 0 | if (!list->baseURI) { |
260 | 0 | struct strbuf baseURI = STRBUF_INIT; |
261 | 0 | strbuf_addstr(&baseURI, uri); |
262 | | |
263 | | /* |
264 | | * If the URI does not end with a trailing slash, then |
265 | | * remove the filename portion of the path. This is |
266 | | * important for relative URIs. |
267 | | */ |
268 | 0 | strbuf_strip_file_from_path(&baseURI); |
269 | 0 | list->baseURI = strbuf_detach(&baseURI, NULL); |
270 | 0 | } |
271 | 0 | result = git_config_from_file_with_options(config_to_bundle_list, |
272 | 0 | filename, list, |
273 | 0 | CONFIG_SCOPE_UNKNOWN, |
274 | 0 | &opts); |
275 | |
|
276 | 0 | if (!result && list->mode == BUNDLE_MODE_NONE) { |
277 | 0 | warning(_("bundle list at '%s' has no mode"), uri); |
278 | 0 | result = 1; |
279 | 0 | } |
280 | |
|
281 | 0 | if (!result) { |
282 | 0 | struct hashmap_iter iter; |
283 | 0 | struct remote_bundle_info *bundle; |
284 | |
|
285 | 0 | hashmap_for_each_entry(&list->bundles, &iter, bundle, ent) { |
286 | 0 | if (!bundle->uri) { |
287 | 0 | error(_("bundle list at '%s': bundle '%s' has no uri"), |
288 | 0 | uri, bundle->id ? bundle->id : "<unknown>"); |
289 | 0 | result = 1; |
290 | 0 | } |
291 | 0 | } |
292 | 0 | } |
293 | |
|
294 | 0 | return result; |
295 | 0 | } |
296 | | |
297 | | static char *find_temp_filename(struct repository *r) |
298 | 0 | { |
299 | 0 | int fd; |
300 | 0 | struct strbuf name = STRBUF_INIT; |
301 | | /* |
302 | | * Find a temporary filename that is available. This is briefly |
303 | | * racy, but unlikely to collide. |
304 | | */ |
305 | 0 | fd = odb_mkstemp(r->objects, &name, |
306 | 0 | "bundles/tmp_uri_XXXXXX"); |
307 | 0 | if (fd < 0) { |
308 | 0 | warning(_("failed to create temporary file")); |
309 | 0 | return NULL; |
310 | 0 | } |
311 | | |
312 | 0 | close(fd); |
313 | 0 | unlink(name.buf); |
314 | 0 | return strbuf_detach(&name, NULL); |
315 | 0 | } |
316 | | |
317 | | static int download_https_uri_to_file(const char *file, const char *uri) |
318 | 0 | { |
319 | 0 | int result = 0; |
320 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
321 | 0 | FILE *child_in = NULL, *child_out = NULL; |
322 | 0 | struct strbuf line = STRBUF_INIT; |
323 | 0 | int found_get = 0; |
324 | | |
325 | | /* |
326 | | * The protocol we speak with git-remote-https(1) uses a space to |
327 | | * separate between URI and file, so the URI itself must not contain a |
328 | | * space. If it did, an adversary could change the location where the |
329 | | * downloaded file is being written to. |
330 | | * |
331 | | * Similarly, we use newlines to separate commands from one another. |
332 | | * Consequently, neither the URI nor the file must contain a newline or |
333 | | * otherwise an adversary could inject arbitrary commands. |
334 | | * |
335 | | * TODO: Restricting newlines in the target paths may break valid |
336 | | * usecases, even if those are a bit more on the esoteric side. |
337 | | * If this ever becomes a problem we should probably think about |
338 | | * alternatives. One alternative could be to use NUL-delimited |
339 | | * requests in git-remote-http(1). Another alternative could be |
340 | | * to use URL quoting. |
341 | | */ |
342 | 0 | if (strpbrk(uri, " \n")) |
343 | 0 | return error("bundle-uri: URI is malformed: '%s'", file); |
344 | 0 | if (strchr(file, '\n')) |
345 | 0 | return error("bundle-uri: filename is malformed: '%s'", file); |
346 | | |
347 | 0 | strvec_pushl(&cp.args, "git-remote-https", uri, NULL); |
348 | 0 | cp.err = -1; |
349 | 0 | cp.in = -1; |
350 | 0 | cp.out = -1; |
351 | |
|
352 | 0 | if (start_command(&cp)) |
353 | 0 | return 1; |
354 | | |
355 | 0 | child_in = fdopen(cp.in, "w"); |
356 | 0 | if (!child_in) { |
357 | 0 | result = 1; |
358 | 0 | goto cleanup; |
359 | 0 | } |
360 | | |
361 | 0 | child_out = fdopen(cp.out, "r"); |
362 | 0 | if (!child_out) { |
363 | 0 | result = 1; |
364 | 0 | goto cleanup; |
365 | 0 | } |
366 | | |
367 | 0 | fprintf(child_in, "capabilities\n"); |
368 | 0 | fflush(child_in); |
369 | |
|
370 | 0 | while (!strbuf_getline(&line, child_out)) { |
371 | 0 | if (!line.len) |
372 | 0 | break; |
373 | 0 | if (!strcmp(line.buf, "get")) |
374 | 0 | found_get = 1; |
375 | 0 | } |
376 | 0 | strbuf_release(&line); |
377 | |
|
378 | 0 | if (!found_get) { |
379 | 0 | result = error(_("insufficient capabilities")); |
380 | 0 | goto cleanup; |
381 | 0 | } |
382 | | |
383 | 0 | fprintf(child_in, "get %s %s\n\n", uri, file); |
384 | |
|
385 | 0 | cleanup: |
386 | 0 | if (child_in) |
387 | 0 | fclose(child_in); |
388 | 0 | if (finish_command(&cp)) |
389 | 0 | result = 1; |
390 | 0 | if (child_out) |
391 | 0 | fclose(child_out); |
392 | 0 | return result; |
393 | 0 | } |
394 | | |
395 | | static int copy_uri_to_file(struct repository *r, |
396 | | const char *filename, const char *uri) |
397 | 0 | { |
398 | 0 | const char *out; |
399 | |
|
400 | 0 | if (starts_with(uri, "https:") || |
401 | 0 | starts_with(uri, "http:")) |
402 | 0 | return download_https_uri_to_file(filename, uri); |
403 | | |
404 | 0 | if (skip_prefix(uri, "file://", &out)) |
405 | 0 | uri = out; |
406 | | |
407 | | /* Copy as a file */ |
408 | 0 | return copy_file(r, filename, uri, 0); |
409 | 0 | } |
410 | | |
411 | | static int unbundle_from_file(struct repository *r, const char *file) |
412 | 0 | { |
413 | 0 | int result = 0; |
414 | 0 | int bundle_fd; |
415 | 0 | struct bundle_header header = BUNDLE_HEADER_INIT; |
416 | 0 | struct string_list_item *refname; |
417 | 0 | struct strbuf bundle_ref = STRBUF_INIT; |
418 | 0 | size_t bundle_prefix_len; |
419 | 0 | struct unbundle_opts opts = { |
420 | 0 | .flags = VERIFY_BUNDLE_QUIET | |
421 | 0 | (fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK : 0), |
422 | 0 | }; |
423 | |
|
424 | 0 | bundle_fd = read_bundle_header(file, &header); |
425 | 0 | if (bundle_fd < 0) { |
426 | 0 | result = 1; |
427 | 0 | goto cleanup; |
428 | 0 | } |
429 | | |
430 | | /* |
431 | | * Skip the reachability walk here, since we will be adding |
432 | | * a reachable ref pointing to the new tips, which will reach |
433 | | * the prerequisite commits. |
434 | | */ |
435 | 0 | result = unbundle(r, &header, bundle_fd, NULL, &opts); |
436 | 0 | if (result) { |
437 | 0 | result = 1; |
438 | 0 | goto cleanup; |
439 | 0 | } |
440 | | |
441 | | /* |
442 | | * Convert all refs/heads/ from the bundle into refs/bundles/ |
443 | | * in the local repository. |
444 | | */ |
445 | 0 | strbuf_addstr(&bundle_ref, "refs/bundles/"); |
446 | 0 | bundle_prefix_len = bundle_ref.len; |
447 | |
|
448 | 0 | for_each_string_list_item(refname, &header.references) { |
449 | 0 | struct object_id *oid = refname->util; |
450 | 0 | struct object_id old_oid; |
451 | 0 | const char *branch_name; |
452 | 0 | int has_old; |
453 | |
|
454 | 0 | if (!skip_prefix(refname->string, "refs/", &branch_name)) |
455 | 0 | continue; |
456 | | |
457 | 0 | strbuf_setlen(&bundle_ref, bundle_prefix_len); |
458 | 0 | strbuf_addstr(&bundle_ref, branch_name); |
459 | |
|
460 | 0 | has_old = !refs_read_ref(get_main_ref_store(r), |
461 | 0 | bundle_ref.buf, &old_oid); |
462 | 0 | refs_update_ref(get_main_ref_store(r), |
463 | 0 | "fetched bundle", bundle_ref.buf, oid, |
464 | 0 | has_old ? &old_oid : NULL, |
465 | 0 | 0, UPDATE_REFS_MSG_ON_ERR); |
466 | 0 | } |
467 | |
|
468 | 0 | cleanup: |
469 | 0 | strbuf_release(&bundle_ref); |
470 | 0 | bundle_header_release(&header); |
471 | 0 | return result; |
472 | 0 | } |
473 | | |
474 | | struct bundle_list_context { |
475 | | struct repository *r; |
476 | | struct bundle_list *list; |
477 | | enum bundle_list_mode mode; |
478 | | int count; |
479 | | int depth; |
480 | | }; |
481 | | |
482 | | /* |
483 | | * This early definition is necessary because we use indirect recursion: |
484 | | * |
485 | | * While iterating through a bundle list that was downloaded as part |
486 | | * of fetch_bundle_uri_internal(), iterator methods eventually call it |
487 | | * again, but with depth + 1. |
488 | | */ |
489 | | static int fetch_bundle_uri_internal(struct repository *r, |
490 | | struct remote_bundle_info *bundle, |
491 | | int depth, |
492 | | struct bundle_list *list); |
493 | | |
494 | | static int download_bundle_to_file(struct remote_bundle_info *bundle, void *data) |
495 | 0 | { |
496 | 0 | int res; |
497 | 0 | struct bundle_list_context *ctx = data; |
498 | |
|
499 | 0 | if (ctx->mode == BUNDLE_MODE_ANY && ctx->count) |
500 | 0 | return 0; |
501 | | |
502 | 0 | res = fetch_bundle_uri_internal(ctx->r, bundle, ctx->depth + 1, ctx->list); |
503 | | |
504 | | /* |
505 | | * Only increment count if the download succeeded. If our mode is |
506 | | * BUNDLE_MODE_ANY, then we will want to try other URIs in the |
507 | | * list in case they work instead. |
508 | | */ |
509 | 0 | if (!res) |
510 | 0 | ctx->count++; |
511 | | |
512 | | /* |
513 | | * To be opportunistic as possible, we continue iterating and |
514 | | * download as many bundles as we can, so we can apply the ones |
515 | | * that work, even in BUNDLE_MODE_ALL mode. |
516 | | */ |
517 | 0 | return 0; |
518 | 0 | } |
519 | | |
520 | | struct bundles_for_sorting { |
521 | | struct remote_bundle_info **items; |
522 | | size_t alloc; |
523 | | size_t nr; |
524 | | }; |
525 | | |
526 | | static int append_bundle(struct remote_bundle_info *bundle, void *data) |
527 | 0 | { |
528 | 0 | struct bundles_for_sorting *list = data; |
529 | 0 | list->items[list->nr++] = bundle; |
530 | 0 | return 0; |
531 | 0 | } |
532 | | |
533 | | /** |
534 | | * For use in QSORT() to get a list sorted by creationToken |
535 | | * in decreasing order. |
536 | | */ |
537 | | static int compare_creation_token_decreasing(const void *va, const void *vb) |
538 | 0 | { |
539 | 0 | const struct remote_bundle_info * const *a = va; |
540 | 0 | const struct remote_bundle_info * const *b = vb; |
541 | |
|
542 | 0 | if ((*a)->creationToken > (*b)->creationToken) |
543 | 0 | return -1; |
544 | 0 | if ((*a)->creationToken < (*b)->creationToken) |
545 | 0 | return 1; |
546 | 0 | return 0; |
547 | 0 | } |
548 | | |
549 | | static int fetch_bundles_by_token(struct repository *r, |
550 | | struct bundle_list *list) |
551 | 0 | { |
552 | 0 | int cur; |
553 | 0 | int move_direction = 0; |
554 | 0 | const char *creationTokenStr; |
555 | 0 | uint64_t maxCreationToken = 0, newMaxCreationToken = 0; |
556 | 0 | struct bundle_list_context ctx = { |
557 | 0 | .r = r, |
558 | 0 | .list = list, |
559 | 0 | .mode = list->mode, |
560 | 0 | }; |
561 | 0 | struct bundles_for_sorting bundles = { |
562 | 0 | .alloc = hashmap_get_size(&list->bundles), |
563 | 0 | }; |
564 | |
|
565 | 0 | ALLOC_ARRAY(bundles.items, bundles.alloc); |
566 | |
|
567 | 0 | for_all_bundles_in_list(list, append_bundle, &bundles); |
568 | |
|
569 | 0 | if (!bundles.nr) { |
570 | 0 | free(bundles.items); |
571 | 0 | return 0; |
572 | 0 | } |
573 | | |
574 | 0 | QSORT(bundles.items, bundles.nr, compare_creation_token_decreasing); |
575 | | |
576 | | /* |
577 | | * If fetch.bundleCreationToken exists, parses to a uint64t, and |
578 | | * is not strictly smaller than the maximum creation token in the |
579 | | * bundle list, then do not download any bundles. |
580 | | */ |
581 | 0 | if (!repo_config_get_value(r, |
582 | 0 | "fetch.bundlecreationtoken", |
583 | 0 | &creationTokenStr)) { |
584 | 0 | if (!creationTokenStr || |
585 | 0 | sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) != 1) |
586 | 0 | maxCreationToken = 0; |
587 | 0 | if (bundles.items[0]->creationToken <= maxCreationToken) { |
588 | 0 | free(bundles.items); |
589 | 0 | return 0; |
590 | 0 | } |
591 | 0 | } |
592 | | |
593 | | /* |
594 | | * Attempt to download and unbundle the minimum number of bundles by |
595 | | * creationToken in decreasing order. If we fail to unbundle (after |
596 | | * a successful download) then move to the next non-downloaded bundle |
597 | | * and attempt downloading. Once we succeed in applying a bundle, |
598 | | * move to the previous unapplied bundle and attempt to unbundle it |
599 | | * again. |
600 | | * |
601 | | * In the case of a fresh clone, we will likely download all of the |
602 | | * bundles before successfully unbundling the oldest one, then the |
603 | | * rest of the bundles unbundle successfully in increasing order |
604 | | * of creationToken. |
605 | | * |
606 | | * If there are existing objects, then this process may terminate |
607 | | * early when all required commits from "new" bundles exist in the |
608 | | * repo's object store. |
609 | | */ |
610 | 0 | cur = 0; |
611 | 0 | while (cur >= 0 && cur < bundles.nr) { |
612 | 0 | struct remote_bundle_info *bundle = bundles.items[cur]; |
613 | | |
614 | | /* |
615 | | * If we need to dig into bundles below the previous |
616 | | * creation token value, then likely we are in an erroneous |
617 | | * state due to missing or invalid bundles. Halt the process |
618 | | * instead of continuing to download extra data. |
619 | | */ |
620 | 0 | if (bundle->creationToken <= maxCreationToken) |
621 | 0 | break; |
622 | | |
623 | 0 | if (!bundle->file) { |
624 | | /* |
625 | | * Not downloaded yet. Try downloading. |
626 | | * |
627 | | * Note that bundle->file is non-NULL if a download |
628 | | * was attempted, even if it failed to download. |
629 | | */ |
630 | 0 | if (fetch_bundle_uri_internal(ctx.r, bundle, ctx.depth + 1, ctx.list)) { |
631 | | /* Mark as unbundled so we do not retry. */ |
632 | 0 | bundle->unbundled = 1; |
633 | | |
634 | | /* Try looking deeper in the list. */ |
635 | 0 | move_direction = 1; |
636 | 0 | goto move; |
637 | 0 | } |
638 | | |
639 | | /* We expect bundles when using creationTokens. */ |
640 | 0 | if (!is_bundle(bundle->file, 1)) { |
641 | 0 | warning(_("file downloaded from '%s' is not a bundle"), |
642 | 0 | bundle->uri); |
643 | 0 | break; |
644 | 0 | } |
645 | 0 | } |
646 | | |
647 | 0 | if (bundle->file && !bundle->unbundled) { |
648 | | /* |
649 | | * This was downloaded, but not successfully |
650 | | * unbundled. Try unbundling again. |
651 | | */ |
652 | 0 | if (unbundle_from_file(ctx.r, bundle->file)) { |
653 | | /* Try looking deeper in the list. */ |
654 | 0 | move_direction = 1; |
655 | 0 | } else { |
656 | | /* |
657 | | * Succeeded in unbundle. Retry bundles |
658 | | * that previously failed to unbundle. |
659 | | */ |
660 | 0 | move_direction = -1; |
661 | 0 | bundle->unbundled = 1; |
662 | |
|
663 | 0 | if (bundle->creationToken > newMaxCreationToken) |
664 | 0 | newMaxCreationToken = bundle->creationToken; |
665 | 0 | } |
666 | 0 | } |
667 | | |
668 | | /* |
669 | | * Else case: downloaded and unbundled successfully. |
670 | | * Skip this by moving in the same direction as the |
671 | | * previous step. |
672 | | */ |
673 | |
|
674 | 0 | move: |
675 | | /* Move in the specified direction and repeat. */ |
676 | 0 | cur += move_direction; |
677 | 0 | } |
678 | | |
679 | | /* |
680 | | * We succeed if the loop terminates because 'cur' drops below |
681 | | * zero. The other case is that we terminate because 'cur' |
682 | | * reaches the end of the list, so we have a failure no matter |
683 | | * which bundles we apply from the list. |
684 | | */ |
685 | 0 | if (cur < 0) { |
686 | 0 | struct strbuf value = STRBUF_INIT; |
687 | 0 | strbuf_addf(&value, "%"PRIu64"", newMaxCreationToken); |
688 | 0 | if (repo_config_set_multivar_gently(ctx.r, |
689 | 0 | "fetch.bundleCreationToken", |
690 | 0 | value.buf, NULL, 0)) |
691 | 0 | warning(_("failed to store maximum creation token")); |
692 | |
|
693 | 0 | strbuf_release(&value); |
694 | 0 | } |
695 | |
|
696 | 0 | free(bundles.items); |
697 | 0 | return cur >= 0; |
698 | 0 | } |
699 | | |
700 | | static int download_bundle_list(struct repository *r, |
701 | | struct bundle_list *local_list, |
702 | | struct bundle_list *global_list, |
703 | | int depth) |
704 | 0 | { |
705 | 0 | struct bundle_list_context ctx = { |
706 | 0 | .r = r, |
707 | 0 | .list = global_list, |
708 | 0 | .depth = depth + 1, |
709 | 0 | .mode = local_list->mode, |
710 | 0 | }; |
711 | |
|
712 | 0 | return for_all_bundles_in_list(local_list, download_bundle_to_file, &ctx); |
713 | 0 | } |
714 | | |
715 | | static int fetch_bundle_list_in_config_format(struct repository *r, |
716 | | struct bundle_list *global_list, |
717 | | struct remote_bundle_info *bundle, |
718 | | int depth) |
719 | 0 | { |
720 | 0 | int result; |
721 | 0 | struct bundle_list list_from_bundle; |
722 | |
|
723 | 0 | init_bundle_list(&list_from_bundle); |
724 | |
|
725 | 0 | if ((result = bundle_uri_parse_config_format(bundle->uri, |
726 | 0 | bundle->file, |
727 | 0 | &list_from_bundle))) |
728 | 0 | goto cleanup; |
729 | | |
730 | 0 | if (list_from_bundle.mode == BUNDLE_MODE_NONE) { |
731 | 0 | warning(_("unrecognized bundle mode from URI '%s'"), |
732 | 0 | bundle->uri); |
733 | 0 | result = -1; |
734 | 0 | goto cleanup; |
735 | 0 | } |
736 | | |
737 | | /* |
738 | | * If this list uses the creationToken heuristic, then the URIs |
739 | | * it advertises are expected to be bundles, not nested lists. |
740 | | * We can drop 'global_list' and 'depth'. |
741 | | */ |
742 | 0 | if (list_from_bundle.heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) { |
743 | 0 | result = fetch_bundles_by_token(r, &list_from_bundle); |
744 | 0 | global_list->heuristic = BUNDLE_HEURISTIC_CREATIONTOKEN; |
745 | 0 | } else if ((result = download_bundle_list(r, &list_from_bundle, |
746 | 0 | global_list, depth))) |
747 | 0 | goto cleanup; |
748 | | |
749 | 0 | cleanup: |
750 | 0 | clear_bundle_list(&list_from_bundle); |
751 | 0 | return result; |
752 | 0 | } |
753 | | |
754 | | /** |
755 | | * This limits the recursion on fetch_bundle_uri_internal() when following |
756 | | * bundle lists. |
757 | | */ |
758 | | static int max_bundle_uri_depth = 4; |
759 | | |
760 | | /** |
761 | | * Recursively download all bundles advertised at the given URI |
762 | | * to files. If the file is a bundle, then add it to the given |
763 | | * 'list'. Otherwise, expect a bundle list and recurse on the |
764 | | * URIs in that list according to the list mode (ANY or ALL). |
765 | | */ |
766 | | static int fetch_bundle_uri_internal(struct repository *r, |
767 | | struct remote_bundle_info *bundle, |
768 | | int depth, |
769 | | struct bundle_list *list) |
770 | 0 | { |
771 | 0 | int result = 0; |
772 | 0 | struct remote_bundle_info *bcopy; |
773 | |
|
774 | 0 | if (depth >= max_bundle_uri_depth) { |
775 | 0 | warning(_("exceeded bundle URI recursion limit (%d)"), |
776 | 0 | max_bundle_uri_depth); |
777 | 0 | return -1; |
778 | 0 | } |
779 | | |
780 | 0 | if (!bundle->uri) { |
781 | 0 | error(_("bundle '%s' has no uri"), |
782 | 0 | bundle->id ? bundle->id : "<unknown>"); |
783 | 0 | return -1; |
784 | 0 | } |
785 | | |
786 | 0 | if (!bundle->file && |
787 | 0 | !(bundle->file = find_temp_filename(r))) { |
788 | 0 | result = -1; |
789 | 0 | goto cleanup; |
790 | 0 | } |
791 | | |
792 | 0 | if ((result = copy_uri_to_file(r, bundle->file, bundle->uri))) { |
793 | 0 | warning(_("failed to download bundle from URI '%s'"), bundle->uri); |
794 | 0 | goto cleanup; |
795 | 0 | } |
796 | | |
797 | 0 | if ((result = !is_bundle(bundle->file, 1))) { |
798 | 0 | result = fetch_bundle_list_in_config_format( |
799 | 0 | r, list, bundle, depth); |
800 | 0 | if (result) |
801 | 0 | warning(_("file at URI '%s' is not a bundle or bundle list"), |
802 | 0 | bundle->uri); |
803 | 0 | goto cleanup; |
804 | 0 | } |
805 | | |
806 | | /* Copy the bundle and insert it into the global list. */ |
807 | 0 | CALLOC_ARRAY(bcopy, 1); |
808 | 0 | bcopy->id = xstrdup(bundle->id); |
809 | 0 | bcopy->file = xstrdup(bundle->file); |
810 | 0 | hashmap_entry_init(&bcopy->ent, strhash(bcopy->id)); |
811 | 0 | hashmap_add(&list->bundles, &bcopy->ent); |
812 | |
|
813 | 0 | cleanup: |
814 | 0 | if (result && bundle->file) |
815 | 0 | unlink(bundle->file); |
816 | 0 | return result; |
817 | 0 | } |
818 | | |
819 | | /** |
820 | | * This loop iterator breaks the loop with nonzero return code on the |
821 | | * first successful unbundling of a bundle. |
822 | | */ |
823 | | static int attempt_unbundle(struct remote_bundle_info *info, void *data) |
824 | 0 | { |
825 | 0 | struct repository *r = data; |
826 | |
|
827 | 0 | if (!info->file || info->unbundled) |
828 | 0 | return 0; |
829 | | |
830 | 0 | if (!unbundle_from_file(r, info->file)) { |
831 | 0 | info->unbundled = 1; |
832 | 0 | return 1; |
833 | 0 | } |
834 | | |
835 | 0 | return 0; |
836 | 0 | } |
837 | | |
838 | | static int unbundle_all_bundles(struct repository *r, |
839 | | struct bundle_list *list) |
840 | 0 | { |
841 | | /* |
842 | | * Iterate through all bundles looking for ones that can |
843 | | * successfully unbundle. If any succeed, then perhaps another |
844 | | * will succeed in the next attempt. |
845 | | * |
846 | | * Keep in mind that a non-zero result for the loop here means |
847 | | * the loop terminated early on a successful unbundling, which |
848 | | * signals that we can try again. |
849 | | */ |
850 | 0 | while (for_all_bundles_in_list(list, attempt_unbundle, r)) ; |
851 | |
|
852 | 0 | return 0; |
853 | 0 | } |
854 | | |
855 | | static int unlink_bundle(struct remote_bundle_info *info, void *data UNUSED) |
856 | 0 | { |
857 | 0 | if (info->file) |
858 | 0 | unlink_or_warn(info->file); |
859 | 0 | return 0; |
860 | 0 | } |
861 | | |
862 | | int fetch_bundle_uri(struct repository *r, const char *uri, |
863 | | int *has_heuristic) |
864 | 0 | { |
865 | 0 | int result; |
866 | 0 | struct bundle_list list; |
867 | 0 | struct remote_bundle_info bundle = { |
868 | 0 | .uri = xstrdup(uri), |
869 | 0 | .id = xstrdup(""), |
870 | 0 | }; |
871 | |
|
872 | 0 | trace2_region_enter("fetch", "fetch-bundle-uri", r); |
873 | |
|
874 | 0 | init_bundle_list(&list); |
875 | | |
876 | | /* |
877 | | * Do not fetch an empty bundle URI. An empty bundle URI |
878 | | * could signal that a configured bundle URI has been disabled. |
879 | | */ |
880 | 0 | if (!*uri) { |
881 | 0 | result = 0; |
882 | 0 | goto cleanup; |
883 | 0 | } |
884 | | |
885 | | /* If a bundle is added to this global list, then it is required. */ |
886 | 0 | list.mode = BUNDLE_MODE_ALL; |
887 | |
|
888 | 0 | if ((result = fetch_bundle_uri_internal(r, &bundle, 0, &list))) |
889 | 0 | goto cleanup; |
890 | | |
891 | 0 | result = unbundle_all_bundles(r, &list); |
892 | |
|
893 | 0 | cleanup: |
894 | 0 | if (has_heuristic) |
895 | 0 | *has_heuristic = (list.heuristic != BUNDLE_HEURISTIC_NONE); |
896 | 0 | for_all_bundles_in_list(&list, unlink_bundle, NULL); |
897 | 0 | clear_bundle_list(&list); |
898 | 0 | clear_remote_bundle_info(&bundle, NULL); |
899 | 0 | trace2_region_leave("fetch", "fetch-bundle-uri", r); |
900 | 0 | return result; |
901 | 0 | } |
902 | | |
903 | | int fetch_bundle_list(struct repository *r, struct bundle_list *list) |
904 | 0 | { |
905 | 0 | int result; |
906 | 0 | struct bundle_list global_list; |
907 | | |
908 | | /* |
909 | | * If the creationToken heuristic is used, then the URIs |
910 | | * advertised by 'list' are not nested lists and instead |
911 | | * direct bundles. We do not need to use global_list. |
912 | | */ |
913 | 0 | if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) |
914 | 0 | return fetch_bundles_by_token(r, list); |
915 | | |
916 | 0 | init_bundle_list(&global_list); |
917 | | |
918 | | /* If a bundle is added to this global list, then it is required. */ |
919 | 0 | global_list.mode = BUNDLE_MODE_ALL; |
920 | |
|
921 | 0 | if ((result = download_bundle_list(r, list, &global_list, 0))) |
922 | 0 | goto cleanup; |
923 | | |
924 | 0 | if (list->heuristic == BUNDLE_HEURISTIC_CREATIONTOKEN) |
925 | 0 | result = fetch_bundles_by_token(r, list); |
926 | 0 | else |
927 | 0 | result = unbundle_all_bundles(r, &global_list); |
928 | |
|
929 | 0 | cleanup: |
930 | 0 | for_all_bundles_in_list(&global_list, unlink_bundle, NULL); |
931 | 0 | clear_bundle_list(&global_list); |
932 | 0 | return result; |
933 | 0 | } |
934 | | |
935 | | /** |
936 | | * API for serve.c. |
937 | | */ |
938 | | |
939 | | int bundle_uri_advertise(struct repository *r, struct strbuf *value UNUSED) |
940 | 0 | { |
941 | 0 | static int advertise_bundle_uri = -1; |
942 | |
|
943 | 0 | if (advertise_bundle_uri != -1) |
944 | 0 | goto cached; |
945 | | |
946 | 0 | advertise_bundle_uri = 0; |
947 | 0 | repo_config_get_maybe_bool(r, "uploadpack.advertisebundleuris", &advertise_bundle_uri); |
948 | |
|
949 | 0 | cached: |
950 | 0 | return advertise_bundle_uri; |
951 | 0 | } |
952 | | |
953 | | static int config_to_packet_line(const char *key, const char *value, |
954 | | const struct config_context *ctx UNUSED, |
955 | | void *data) |
956 | 0 | { |
957 | 0 | struct packet_reader *writer = data; |
958 | |
|
959 | 0 | if (starts_with(key, "bundle.")) { |
960 | 0 | if (value && *value) |
961 | 0 | packet_write_fmt(writer->fd, "%s=%s", key, value); |
962 | 0 | else |
963 | 0 | warning(_("config '%s' has no value"), key); |
964 | 0 | } |
965 | |
|
966 | 0 | return 0; |
967 | 0 | } |
968 | | |
969 | | int bundle_uri_command(struct repository *r, |
970 | | struct packet_reader *request) |
971 | 0 | { |
972 | 0 | struct packet_writer writer; |
973 | 0 | packet_writer_init(&writer, 1); |
974 | |
|
975 | 0 | while (packet_reader_read(request) == PACKET_READ_NORMAL) |
976 | 0 | die(_("bundle-uri: unexpected argument: '%s'"), request->line); |
977 | 0 | if (request->status != PACKET_READ_FLUSH) |
978 | 0 | die(_("bundle-uri: expected flush after arguments")); |
979 | | |
980 | | /* |
981 | | * Read all "bundle.*" config lines to the client as key=value |
982 | | * packet lines. |
983 | | */ |
984 | 0 | repo_config(r, config_to_packet_line, &writer); |
985 | |
|
986 | 0 | packet_writer_flush(&writer); |
987 | |
|
988 | 0 | return 0; |
989 | 0 | } |
990 | | |
991 | | /** |
992 | | * General API for {transport,connect}.c etc. |
993 | | */ |
994 | | int bundle_uri_parse_line(struct bundle_list *list, const char *line) |
995 | 0 | { |
996 | 0 | int result; |
997 | 0 | const char *equals; |
998 | 0 | struct strbuf key = STRBUF_INIT; |
999 | |
|
1000 | 0 | if (!strlen(line)) |
1001 | 0 | return error(_("bundle-uri: got an empty line")); |
1002 | | |
1003 | 0 | equals = strchr(line, '='); |
1004 | |
|
1005 | 0 | if (!equals) |
1006 | 0 | return error(_("bundle-uri: line is not of the form 'key=value'")); |
1007 | 0 | if (line == equals || !*(equals + 1)) |
1008 | 0 | return error(_("bundle-uri: line has empty key or value")); |
1009 | | |
1010 | 0 | strbuf_add(&key, line, equals - line); |
1011 | 0 | result = bundle_list_update(key.buf, equals + 1, list); |
1012 | 0 | strbuf_release(&key); |
1013 | |
|
1014 | 0 | return result; |
1015 | 0 | } |