]>
Commit | Line | Data |
---|---|---|
6eb996b5 DB |
1 | #include "cache.h" |
2 | #include "transport.h" | |
ae4efe19 | 3 | #include "quote.h" |
6eb996b5 DB |
4 | #include "run-command.h" |
5 | #include "commit.h" | |
6 | #include "diff.h" | |
7 | #include "revision.h" | |
ef08ef9e | 8 | #include "quote.h" |
72ff8943 | 9 | #include "remote.h" |
73b49a75 | 10 | #include "string-list.h" |
7851b1e6 | 11 | #include "thread-utils.h" |
c34fe630 | 12 | #include "sigchain.h" |
bfc366d9 | 13 | #include "argv-array.h" |
664059fb | 14 | #include "refs.h" |
7851b1e6 | 15 | |
bf3c523c IL |
16 | static int debug; |
17 | ||
9cba13ca | 18 | struct helper_data { |
6eb996b5 DB |
19 | const char *name; |
20 | struct child_process *helper; | |
292ce46b | 21 | FILE *out; |
ef08ef9e | 22 | unsigned fetch : 1, |
a24a32dd | 23 | import : 1, |
bfc366d9 | 24 | bidi_import : 1, |
73b49a75 | 25 | export : 1, |
ae4efe19 | 26 | option : 1, |
fa8c097c IL |
27 | push : 1, |
28 | connect : 1, | |
0d957a4d | 29 | signed_tags : 1, |
9ba38048 | 30 | check_connectivity : 1, |
597b831a MM |
31 | no_disconnect_req : 1, |
32 | no_private_update : 1; | |
a515ebe9 SR |
33 | char *export_marks; |
34 | char *import_marks; | |
72ff8943 DB |
35 | /* These go from remote name (as in "list") to private name */ |
36 | struct refspec *refspecs; | |
37 | int refspec_nr; | |
61b075bd IL |
38 | /* Transport options for fetch-pack/send-pack (should one of |
39 | * those be invoked). | |
40 | */ | |
41 | struct git_transport_options transport_options; | |
6eb996b5 DB |
42 | }; |
43 | ||
bf3c523c IL |
44 | static void sendline(struct helper_data *helper, struct strbuf *buffer) |
45 | { | |
46 | if (debug) | |
47 | fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf); | |
48 | if (write_in_full(helper->helper->in, buffer->buf, buffer->len) | |
49 | != buffer->len) | |
50 | die_errno("Full write to remote helper failed"); | |
51 | } | |
52 | ||
c096955c | 53 | static int recvline_fh(FILE *helper, struct strbuf *buffer, const char *name) |
bf3c523c IL |
54 | { |
55 | strbuf_reset(buffer); | |
56 | if (debug) | |
57 | fprintf(stderr, "Debug: Remote helper: Waiting...\n"); | |
fa8c097c | 58 | if (strbuf_getline(buffer, helper, '\n') == EOF) { |
bf3c523c IL |
59 | if (debug) |
60 | fprintf(stderr, "Debug: Remote helper quit.\n"); | |
266f1fdf | 61 | exit(128); |
bf3c523c IL |
62 | } |
63 | ||
64 | if (debug) | |
65 | fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf); | |
66 | return 0; | |
67 | } | |
68 | ||
fa8c097c IL |
69 | static int recvline(struct helper_data *helper, struct strbuf *buffer) |
70 | { | |
c096955c | 71 | return recvline_fh(helper->out, buffer, helper->name); |
fa8c097c IL |
72 | } |
73 | ||
bf3c523c IL |
74 | static void xchgline(struct helper_data *helper, struct strbuf *buffer) |
75 | { | |
76 | sendline(helper, buffer); | |
77 | recvline(helper, buffer); | |
78 | } | |
79 | ||
80 | static void write_constant(int fd, const char *str) | |
81 | { | |
82 | if (debug) | |
83 | fprintf(stderr, "Debug: Remote helper: -> %s", str); | |
84 | if (write_in_full(fd, str, strlen(str)) != strlen(str)) | |
85 | die_errno("Full write to remote helper failed"); | |
86 | } | |
87 | ||
c2e86add | 88 | static const char *remove_ext_force(const char *url) |
25d5cc48 IL |
89 | { |
90 | if (url) { | |
91 | const char *colon = strchr(url, ':'); | |
92 | if (colon && colon[1] == ':') | |
93 | return colon + 2; | |
94 | } | |
95 | return url; | |
96 | } | |
97 | ||
fa8c097c IL |
98 | static void do_take_over(struct transport *transport) |
99 | { | |
100 | struct helper_data *data; | |
101 | data = (struct helper_data *)transport->data; | |
102 | transport_take_over(transport, data->helper); | |
103 | fclose(data->out); | |
104 | free(data); | |
105 | } | |
106 | ||
6eb996b5 DB |
107 | static struct child_process *get_helper(struct transport *transport) |
108 | { | |
109 | struct helper_data *data = transport->data; | |
bfc366d9 | 110 | struct argv_array argv = ARGV_ARRAY_INIT; |
6eb996b5 DB |
111 | struct strbuf buf = STRBUF_INIT; |
112 | struct child_process *helper; | |
72ff8943 DB |
113 | const char **refspecs = NULL; |
114 | int refspec_nr = 0; | |
115 | int refspec_alloc = 0; | |
61b075bd | 116 | int duped; |
6b02de3b | 117 | int code; |
e1735872 DI |
118 | char git_dir_buf[sizeof(GIT_DIR_ENVIRONMENT) + PATH_MAX + 1]; |
119 | const char *helper_env[] = { | |
120 | git_dir_buf, | |
121 | NULL | |
122 | }; | |
123 | ||
6eb996b5 DB |
124 | |
125 | if (data->helper) | |
126 | return data->helper; | |
127 | ||
128 | helper = xcalloc(1, sizeof(*helper)); | |
129 | helper->in = -1; | |
130 | helper->out = -1; | |
131 | helper->err = 0; | |
bfc366d9 FA |
132 | argv_array_pushf(&argv, "git-remote-%s", data->name); |
133 | argv_array_push(&argv, transport->remote->name); | |
134 | argv_array_push(&argv, remove_ext_force(transport->url)); | |
135 | helper->argv = argv_array_detach(&argv, NULL); | |
6b02de3b IL |
136 | helper->git_cmd = 0; |
137 | helper->silent_exec_failure = 1; | |
e1735872 DI |
138 | |
139 | snprintf(git_dir_buf, sizeof(git_dir_buf), "%s=%s", GIT_DIR_ENVIRONMENT, get_git_dir()); | |
140 | helper->env = helper_env; | |
141 | ||
6b02de3b IL |
142 | code = start_command(helper); |
143 | if (code < 0 && errno == ENOENT) | |
144 | die("Unable to find remote helper for '%s'", data->name); | |
145 | else if (code != 0) | |
146 | exit(code); | |
147 | ||
6eb996b5 | 148 | data->helper = helper; |
fa8c097c | 149 | data->no_disconnect_req = 0; |
6eb996b5 | 150 | |
61b075bd IL |
151 | /* |
152 | * Open the output as FILE* so strbuf_getline() can be used. | |
153 | * Do this with duped fd because fclose() will close the fd, | |
154 | * and stuff like taking over will require the fd to remain. | |
61b075bd IL |
155 | */ |
156 | duped = dup(helper->out); | |
157 | if (duped < 0) | |
158 | die_errno("Can't dup helper output fd"); | |
159 | data->out = xfdopen(duped, "r"); | |
160 | ||
bf3c523c | 161 | write_constant(helper->in, "capabilities\n"); |
2d14d65c | 162 | |
6eb996b5 | 163 | while (1) { |
28ed5b35 IL |
164 | const char *capname; |
165 | int mandatory = 0; | |
bf3c523c | 166 | recvline(data, &buf); |
6eb996b5 DB |
167 | |
168 | if (!*buf.buf) | |
169 | break; | |
28ed5b35 IL |
170 | |
171 | if (*buf.buf == '*') { | |
172 | capname = buf.buf + 1; | |
173 | mandatory = 1; | |
174 | } else | |
175 | capname = buf.buf; | |
176 | ||
bf3c523c | 177 | if (debug) |
28ed5b35 IL |
178 | fprintf(stderr, "Debug: Got cap %s\n", capname); |
179 | if (!strcmp(capname, "fetch")) | |
6eb996b5 | 180 | data->fetch = 1; |
28ed5b35 | 181 | else if (!strcmp(capname, "option")) |
ef08ef9e | 182 | data->option = 1; |
28ed5b35 | 183 | else if (!strcmp(capname, "push")) |
ae4efe19 | 184 | data->push = 1; |
28ed5b35 | 185 | else if (!strcmp(capname, "import")) |
e65e91ed | 186 | data->import = 1; |
bfc366d9 FA |
187 | else if (!strcmp(capname, "bidi-import")) |
188 | data->bidi_import = 1; | |
73b49a75 SR |
189 | else if (!strcmp(capname, "export")) |
190 | data->export = 1; | |
9ba38048 NTND |
191 | else if (!strcmp(capname, "check-connectivity")) |
192 | data->check_connectivity = 1; | |
59556548 | 193 | else if (!data->refspecs && starts_with(capname, "refspec ")) { |
72ff8943 DB |
194 | ALLOC_GROW(refspecs, |
195 | refspec_nr + 1, | |
196 | refspec_alloc); | |
c28cce55 | 197 | refspecs[refspec_nr++] = xstrdup(capname + strlen("refspec ")); |
fa8c097c IL |
198 | } else if (!strcmp(capname, "connect")) { |
199 | data->connect = 1; | |
0d957a4d JK |
200 | } else if (!strcmp(capname, "signed-tags")) { |
201 | data->signed_tags = 1; | |
59556548 | 202 | } else if (starts_with(capname, "export-marks ")) { |
a515ebe9 SR |
203 | struct strbuf arg = STRBUF_INIT; |
204 | strbuf_addstr(&arg, "--export-marks="); | |
205 | strbuf_addstr(&arg, capname + strlen("export-marks ")); | |
206 | data->export_marks = strbuf_detach(&arg, NULL); | |
59556548 | 207 | } else if (starts_with(capname, "import-marks")) { |
a515ebe9 SR |
208 | struct strbuf arg = STRBUF_INIT; |
209 | strbuf_addstr(&arg, "--import-marks="); | |
210 | strbuf_addstr(&arg, capname + strlen("import-marks ")); | |
211 | data->import_marks = strbuf_detach(&arg, NULL); | |
59556548 | 212 | } else if (starts_with(capname, "no-private-update")) { |
597b831a | 213 | data->no_private_update = 1; |
28ed5b35 | 214 | } else if (mandatory) { |
9517e6b8 | 215 | die("Unknown mandatory capability %s. This remote " |
82247e9b | 216 | "helper probably needs newer version of Git.", |
28ed5b35 | 217 | capname); |
72ff8943 DB |
218 | } |
219 | } | |
220 | if (refspecs) { | |
221 | int i; | |
222 | data->refspec_nr = refspec_nr; | |
223 | data->refspecs = parse_fetch_refspec(refspec_nr, refspecs); | |
b120ef3e | 224 | for (i = 0; i < refspec_nr; i++) |
72ff8943 | 225 | free((char *)refspecs[i]); |
72ff8943 | 226 | free(refspecs); |
a93b4a09 FC |
227 | } else if (data->import || data->bidi_import || data->export) { |
228 | warning("This remote helper should implement refspec capability."); | |
6eb996b5 | 229 | } |
b962dbdc | 230 | strbuf_release(&buf); |
bf3c523c IL |
231 | if (debug) |
232 | fprintf(stderr, "Debug: Capabilities complete.\n"); | |
6eb996b5 DB |
233 | return data->helper; |
234 | } | |
235 | ||
236 | static int disconnect_helper(struct transport *transport) | |
237 | { | |
238 | struct helper_data *data = transport->data; | |
cc567322 | 239 | int res = 0; |
bf3c523c | 240 | |
6eb996b5 | 241 | if (data->helper) { |
bf3c523c IL |
242 | if (debug) |
243 | fprintf(stderr, "Debug: Disconnecting.\n"); | |
fa8c097c | 244 | if (!data->no_disconnect_req) { |
c34fe630 JK |
245 | /* |
246 | * Ignore write errors; there's nothing we can do, | |
247 | * since we're about to close the pipe anyway. And the | |
248 | * most likely error is EPIPE due to the helper dying | |
249 | * to report an error itself. | |
250 | */ | |
251 | sigchain_push(SIGPIPE, SIG_IGN); | |
252 | xwrite(data->helper->in, "\n", 1); | |
253 | sigchain_pop(SIGPIPE); | |
fa8c097c | 254 | } |
6eb996b5 | 255 | close(data->helper->in); |
61b075bd | 256 | close(data->helper->out); |
292ce46b | 257 | fclose(data->out); |
cc567322 | 258 | res = finish_command(data->helper); |
bfc366d9 | 259 | argv_array_free_detached(data->helper->argv); |
6eb996b5 DB |
260 | free(data->helper); |
261 | data->helper = NULL; | |
262 | } | |
cc567322 | 263 | return res; |
6eb996b5 DB |
264 | } |
265 | ||
ef08ef9e SP |
266 | static const char *unsupported_options[] = { |
267 | TRANS_OPT_UPLOADPACK, | |
268 | TRANS_OPT_RECEIVEPACK, | |
269 | TRANS_OPT_THIN, | |
270 | TRANS_OPT_KEEP | |
271 | }; | |
23cd01ec | 272 | |
ef08ef9e SP |
273 | static const char *boolean_options[] = { |
274 | TRANS_OPT_THIN, | |
275 | TRANS_OPT_KEEP, | |
276 | TRANS_OPT_FOLLOWTAGS | |
277 | }; | |
278 | ||
279 | static int set_helper_option(struct transport *transport, | |
280 | const char *name, const char *value) | |
281 | { | |
282 | struct helper_data *data = transport->data; | |
ef08ef9e SP |
283 | struct strbuf buf = STRBUF_INIT; |
284 | int i, ret, is_bool = 0; | |
285 | ||
bf3c523c IL |
286 | get_helper(transport); |
287 | ||
ef08ef9e SP |
288 | if (!data->option) |
289 | return 1; | |
290 | ||
291 | for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) { | |
292 | if (!strcmp(name, unsupported_options[i])) | |
293 | return 1; | |
294 | } | |
295 | ||
296 | for (i = 0; i < ARRAY_SIZE(boolean_options); i++) { | |
297 | if (!strcmp(name, boolean_options[i])) { | |
298 | is_bool = 1; | |
299 | break; | |
300 | } | |
301 | } | |
302 | ||
303 | strbuf_addf(&buf, "option %s ", name); | |
304 | if (is_bool) | |
305 | strbuf_addstr(&buf, value ? "true" : "false"); | |
306 | else | |
307 | quote_c_style(value, &buf, NULL, 0); | |
308 | strbuf_addch(&buf, '\n'); | |
309 | ||
bf3c523c | 310 | xchgline(data, &buf); |
ef08ef9e SP |
311 | |
312 | if (!strcmp(buf.buf, "ok")) | |
313 | ret = 0; | |
59556548 | 314 | else if (starts_with(buf.buf, "error")) { |
ef08ef9e SP |
315 | ret = -1; |
316 | } else if (!strcmp(buf.buf, "unsupported")) | |
317 | ret = 1; | |
318 | else { | |
319 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
320 | ret = 1; | |
321 | } | |
322 | strbuf_release(&buf); | |
323 | return ret; | |
324 | } | |
325 | ||
326 | static void standard_options(struct transport *t) | |
327 | { | |
328 | char buf[16]; | |
329 | int n; | |
330 | int v = t->verbose; | |
ef08ef9e | 331 | |
d01b3c02 | 332 | set_helper_option(t, "progress", t->progress ? "true" : "false"); |
ef08ef9e SP |
333 | |
334 | n = snprintf(buf, sizeof(buf), "%d", v + 1); | |
335 | if (n >= sizeof(buf)) | |
336 | die("impossibly large verbosity value"); | |
337 | set_helper_option(t, "verbosity", buf); | |
338 | } | |
339 | ||
f2a37151 DB |
340 | static int release_helper(struct transport *transport) |
341 | { | |
cc567322 | 342 | int res = 0; |
72ff8943 DB |
343 | struct helper_data *data = transport->data; |
344 | free_refspec(data->refspec_nr, data->refspecs); | |
345 | data->refspecs = NULL; | |
cc567322 | 346 | res = disconnect_helper(transport); |
f2a37151 | 347 | free(transport->data); |
cc567322 | 348 | return res; |
f2a37151 DB |
349 | } |
350 | ||
6eb996b5 | 351 | static int fetch_with_fetch(struct transport *transport, |
37148311 | 352 | int nr_heads, struct ref **to_fetch) |
6eb996b5 | 353 | { |
292ce46b | 354 | struct helper_data *data = transport->data; |
6eb996b5 DB |
355 | int i; |
356 | struct strbuf buf = STRBUF_INIT; | |
357 | ||
ef08ef9e | 358 | standard_options(transport); |
9ba38048 NTND |
359 | if (data->check_connectivity && |
360 | data->transport_options.check_self_contained_and_connected) | |
361 | set_helper_option(transport, "check-connectivity", "true"); | |
ef08ef9e | 362 | |
16094885 NTND |
363 | if (transport->cloning) |
364 | set_helper_option(transport, "cloning", "true"); | |
365 | ||
366 | if (data->transport_options.update_shallow) | |
367 | set_helper_option(transport, "update-shallow", "true"); | |
368 | ||
6eb996b5 | 369 | for (i = 0; i < nr_heads; i++) { |
1088261f | 370 | const struct ref *posn = to_fetch[i]; |
6eb996b5 DB |
371 | if (posn->status & REF_STATUS_UPTODATE) |
372 | continue; | |
2d14d65c DB |
373 | |
374 | strbuf_addf(&buf, "fetch %s %s\n", | |
375 | sha1_to_hex(posn->old_sha1), posn->name); | |
292ce46b | 376 | } |
2d14d65c | 377 | |
292ce46b | 378 | strbuf_addch(&buf, '\n'); |
bf3c523c | 379 | sendline(data, &buf); |
292ce46b SP |
380 | |
381 | while (1) { | |
bf3c523c | 382 | recvline(data, &buf); |
292ce46b | 383 | |
59556548 | 384 | if (starts_with(buf.buf, "lock ")) { |
292ce46b SP |
385 | const char *name = buf.buf + 5; |
386 | if (transport->pack_lockfile) | |
387 | warning("%s also locked %s", data->name, name); | |
388 | else | |
389 | transport->pack_lockfile = xstrdup(name); | |
390 | } | |
9ba38048 NTND |
391 | else if (data->check_connectivity && |
392 | data->transport_options.check_self_contained_and_connected && | |
393 | !strcmp(buf.buf, "connectivity-ok")) | |
394 | data->transport_options.self_contained_and_connected = 1; | |
292ce46b SP |
395 | else if (!buf.len) |
396 | break; | |
397 | else | |
398 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
6eb996b5 | 399 | } |
292ce46b | 400 | strbuf_release(&buf); |
6eb996b5 DB |
401 | return 0; |
402 | } | |
403 | ||
e65e91ed DB |
404 | static int get_importer(struct transport *transport, struct child_process *fastimport) |
405 | { | |
406 | struct child_process *helper = get_helper(transport); | |
bfc366d9 FA |
407 | struct helper_data *data = transport->data; |
408 | struct argv_array argv = ARGV_ARRAY_INIT; | |
409 | int cat_blob_fd, code; | |
e65e91ed DB |
410 | memset(fastimport, 0, sizeof(*fastimport)); |
411 | fastimport->in = helper->out; | |
bfc366d9 | 412 | argv_array_push(&argv, "fast-import"); |
19ba02af | 413 | argv_array_push(&argv, debug ? "--stats" : "--quiet"); |
e65e91ed | 414 | |
bfc366d9 FA |
415 | if (data->bidi_import) { |
416 | cat_blob_fd = xdup(helper->in); | |
417 | argv_array_pushf(&argv, "--cat-blob-fd=%d", cat_blob_fd); | |
418 | } | |
419 | fastimport->argv = argv.argv; | |
e65e91ed | 420 | fastimport->git_cmd = 1; |
bfc366d9 FA |
421 | |
422 | code = start_command(fastimport); | |
423 | return code; | |
e65e91ed DB |
424 | } |
425 | ||
73b49a75 SR |
426 | static int get_exporter(struct transport *transport, |
427 | struct child_process *fastexport, | |
73b49a75 SR |
428 | struct string_list *revlist_args) |
429 | { | |
a515ebe9 | 430 | struct helper_data *data = transport->data; |
73b49a75 SR |
431 | struct child_process *helper = get_helper(transport); |
432 | int argc = 0, i; | |
433 | memset(fastexport, 0, sizeof(*fastexport)); | |
434 | ||
435 | /* we need to duplicate helper->in because we want to use it after | |
436 | * fastexport is done with it. */ | |
437 | fastexport->out = dup(helper->in); | |
b8bd826f | 438 | fastexport->argv = xcalloc(6 + revlist_args->nr, sizeof(*fastexport->argv)); |
73b49a75 | 439 | fastexport->argv[argc++] = "fast-export"; |
1f25c504 | 440 | fastexport->argv[argc++] = "--use-done-feature"; |
0d957a4d JK |
441 | fastexport->argv[argc++] = data->signed_tags ? |
442 | "--signed-tags=verbatim" : "--signed-tags=warn-strip"; | |
a515ebe9 SR |
443 | if (data->export_marks) |
444 | fastexport->argv[argc++] = data->export_marks; | |
445 | if (data->import_marks) | |
446 | fastexport->argv[argc++] = data->import_marks; | |
73b49a75 SR |
447 | |
448 | for (i = 0; i < revlist_args->nr; i++) | |
449 | fastexport->argv[argc++] = revlist_args->items[i].string; | |
450 | ||
451 | fastexport->git_cmd = 1; | |
452 | return start_command(fastexport); | |
453 | } | |
454 | ||
e65e91ed DB |
455 | static int fetch_with_import(struct transport *transport, |
456 | int nr_heads, struct ref **to_fetch) | |
457 | { | |
458 | struct child_process fastimport; | |
72ff8943 | 459 | struct helper_data *data = transport->data; |
e65e91ed DB |
460 | int i; |
461 | struct ref *posn; | |
462 | struct strbuf buf = STRBUF_INIT; | |
463 | ||
bf3c523c IL |
464 | get_helper(transport); |
465 | ||
e65e91ed DB |
466 | if (get_importer(transport, &fastimport)) |
467 | die("Couldn't run fast-import"); | |
468 | ||
469 | for (i = 0; i < nr_heads; i++) { | |
470 | posn = to_fetch[i]; | |
471 | if (posn->status & REF_STATUS_UPTODATE) | |
472 | continue; | |
473 | ||
474 | strbuf_addf(&buf, "import %s\n", posn->name); | |
bf3c523c | 475 | sendline(data, &buf); |
e65e91ed DB |
476 | strbuf_reset(&buf); |
477 | } | |
9504bc9d SR |
478 | |
479 | write_constant(data->helper->in, "\n"); | |
bfc366d9 FA |
480 | /* |
481 | * remote-helpers that advertise the bidi-import capability are required to | |
482 | * buffer the complete batch of import commands until this newline before | |
483 | * sending data to fast-import. | |
484 | * These helpers read back data from fast-import on their stdin, which could | |
485 | * be mixed with import commands, otherwise. | |
486 | */ | |
9504bc9d | 487 | |
cc567322 SR |
488 | if (finish_command(&fastimport)) |
489 | die("Error while running fast-import"); | |
bfc366d9 | 490 | argv_array_free_detached(fastimport.argv); |
e65e91ed | 491 | |
dff9d65d FA |
492 | /* |
493 | * The fast-import stream of a remote helper that advertises | |
494 | * the "refspec" capability writes to the refs named after the | |
495 | * right hand side of the first refspec matching each ref we | |
496 | * were fetching. | |
497 | * | |
498 | * (If no "refspec" capability was specified, for historical | |
7a43c554 | 499 | * reasons we default to the equivalent of *:*.) |
dff9d65d FA |
500 | * |
501 | * Store the result in to_fetch[i].old_sha1. Callers such | |
502 | * as "git fetch" can use the value to write feedback to the | |
503 | * terminal, populate FETCH_HEAD, and determine what new value | |
504 | * should be written to peer_ref if the update is a | |
505 | * fast-forward or this is a forced update. | |
506 | */ | |
e65e91ed | 507 | for (i = 0; i < nr_heads; i++) { |
72ff8943 | 508 | char *private; |
e65e91ed DB |
509 | posn = to_fetch[i]; |
510 | if (posn->status & REF_STATUS_UPTODATE) | |
511 | continue; | |
72ff8943 DB |
512 | if (data->refspecs) |
513 | private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name); | |
514 | else | |
c28cce55 | 515 | private = xstrdup(posn->name); |
d51b720f MH |
516 | if (private) { |
517 | read_ref(private, posn->old_sha1); | |
518 | free(private); | |
519 | } | |
e65e91ed | 520 | } |
b962dbdc | 521 | strbuf_release(&buf); |
e65e91ed DB |
522 | return 0; |
523 | } | |
524 | ||
fa8c097c IL |
525 | static int process_connect_service(struct transport *transport, |
526 | const char *name, const char *exec) | |
527 | { | |
528 | struct helper_data *data = transport->data; | |
529 | struct strbuf cmdbuf = STRBUF_INIT; | |
530 | struct child_process *helper; | |
531 | int r, duped, ret = 0; | |
532 | FILE *input; | |
533 | ||
534 | helper = get_helper(transport); | |
535 | ||
536 | /* | |
537 | * Yes, dup the pipe another time, as we need unbuffered version | |
538 | * of input pipe as FILE*. fclose() closes the underlying fd and | |
539 | * stream buffering only can be changed before first I/O operation | |
540 | * on it. | |
541 | */ | |
542 | duped = dup(helper->out); | |
543 | if (duped < 0) | |
544 | die_errno("Can't dup helper output fd"); | |
545 | input = xfdopen(duped, "r"); | |
546 | setvbuf(input, NULL, _IONBF, 0); | |
547 | ||
548 | /* | |
549 | * Handle --upload-pack and friends. This is fire and forget... | |
550 | * just warn if it fails. | |
551 | */ | |
552 | if (strcmp(name, exec)) { | |
553 | r = set_helper_option(transport, "servpath", exec); | |
554 | if (r > 0) | |
555 | warning("Setting remote service path not supported by protocol."); | |
556 | else if (r < 0) | |
557 | warning("Invalid remote service path."); | |
558 | } | |
559 | ||
560 | if (data->connect) | |
561 | strbuf_addf(&cmdbuf, "connect %s\n", name); | |
562 | else | |
563 | goto exit; | |
564 | ||
565 | sendline(data, &cmdbuf); | |
c096955c | 566 | recvline_fh(input, &cmdbuf, name); |
fa8c097c IL |
567 | if (!strcmp(cmdbuf.buf, "")) { |
568 | data->no_disconnect_req = 1; | |
569 | if (debug) | |
570 | fprintf(stderr, "Debug: Smart transport connection " | |
571 | "ready.\n"); | |
572 | ret = 1; | |
573 | } else if (!strcmp(cmdbuf.buf, "fallback")) { | |
574 | if (debug) | |
575 | fprintf(stderr, "Debug: Falling back to dumb " | |
576 | "transport.\n"); | |
577 | } else | |
578 | die("Unknown response to connect: %s", | |
579 | cmdbuf.buf); | |
580 | ||
581 | exit: | |
582 | fclose(input); | |
583 | return ret; | |
584 | } | |
585 | ||
586 | static int process_connect(struct transport *transport, | |
587 | int for_push) | |
588 | { | |
589 | struct helper_data *data = transport->data; | |
590 | const char *name; | |
591 | const char *exec; | |
592 | ||
593 | name = for_push ? "git-receive-pack" : "git-upload-pack"; | |
594 | if (for_push) | |
595 | exec = data->transport_options.receivepack; | |
596 | else | |
597 | exec = data->transport_options.uploadpack; | |
598 | ||
599 | return process_connect_service(transport, name, exec); | |
600 | } | |
601 | ||
b236752a IL |
602 | static int connect_helper(struct transport *transport, const char *name, |
603 | const char *exec, int fd[2]) | |
604 | { | |
605 | struct helper_data *data = transport->data; | |
606 | ||
607 | /* Get_helper so connect is inited. */ | |
608 | get_helper(transport); | |
609 | if (!data->connect) | |
610 | die("Operation not supported by protocol."); | |
611 | ||
612 | if (!process_connect_service(transport, name, exec)) | |
613 | die("Can't connect to subservice %s.", name); | |
614 | ||
615 | fd[0] = data->helper->out; | |
616 | fd[1] = data->helper->in; | |
617 | return 0; | |
618 | } | |
619 | ||
6eb996b5 | 620 | static int fetch(struct transport *transport, |
37148311 | 621 | int nr_heads, struct ref **to_fetch) |
6eb996b5 DB |
622 | { |
623 | struct helper_data *data = transport->data; | |
624 | int i, count; | |
625 | ||
fa8c097c IL |
626 | if (process_connect(transport, 0)) { |
627 | do_take_over(transport); | |
628 | return transport->fetch(transport, nr_heads, to_fetch); | |
629 | } | |
630 | ||
6eb996b5 DB |
631 | count = 0; |
632 | for (i = 0; i < nr_heads; i++) | |
633 | if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) | |
634 | count++; | |
635 | ||
636 | if (!count) | |
637 | return 0; | |
638 | ||
639 | if (data->fetch) | |
640 | return fetch_with_fetch(transport, nr_heads, to_fetch); | |
641 | ||
e65e91ed DB |
642 | if (data->import) |
643 | return fetch_with_import(transport, nr_heads, to_fetch); | |
644 | ||
6eb996b5 DB |
645 | return -1; |
646 | } | |
647 | ||
664059fb | 648 | static int push_update_ref_status(struct strbuf *buf, |
d2e73c6f SR |
649 | struct ref **ref, |
650 | struct ref *remote_refs) | |
651 | { | |
652 | char *refname, *msg; | |
653 | int status; | |
654 | ||
59556548 | 655 | if (starts_with(buf->buf, "ok ")) { |
d2e73c6f SR |
656 | status = REF_STATUS_OK; |
657 | refname = buf->buf + 3; | |
59556548 | 658 | } else if (starts_with(buf->buf, "error ")) { |
d2e73c6f SR |
659 | status = REF_STATUS_REMOTE_REJECT; |
660 | refname = buf->buf + 6; | |
661 | } else | |
82247e9b | 662 | die("expected ok/error, helper said '%s'", buf->buf); |
d2e73c6f SR |
663 | |
664 | msg = strchr(refname, ' '); | |
665 | if (msg) { | |
666 | struct strbuf msg_buf = STRBUF_INIT; | |
667 | const char *end; | |
668 | ||
669 | *msg++ = '\0'; | |
670 | if (!unquote_c_style(&msg_buf, msg, &end)) | |
671 | msg = strbuf_detach(&msg_buf, NULL); | |
672 | else | |
673 | msg = xstrdup(msg); | |
674 | strbuf_release(&msg_buf); | |
675 | ||
676 | if (!strcmp(msg, "no match")) { | |
677 | status = REF_STATUS_NONE; | |
678 | free(msg); | |
679 | msg = NULL; | |
680 | } | |
681 | else if (!strcmp(msg, "up to date")) { | |
682 | status = REF_STATUS_UPTODATE; | |
683 | free(msg); | |
684 | msg = NULL; | |
685 | } | |
686 | else if (!strcmp(msg, "non-fast forward")) { | |
687 | status = REF_STATUS_REJECT_NONFASTFORWARD; | |
688 | free(msg); | |
689 | msg = NULL; | |
690 | } | |
dbfeddb1 CR |
691 | else if (!strcmp(msg, "already exists")) { |
692 | status = REF_STATUS_REJECT_ALREADY_EXISTS; | |
693 | free(msg); | |
694 | msg = NULL; | |
695 | } | |
75e5c0dc JH |
696 | else if (!strcmp(msg, "fetch first")) { |
697 | status = REF_STATUS_REJECT_FETCH_FIRST; | |
698 | free(msg); | |
699 | msg = NULL; | |
700 | } | |
701 | else if (!strcmp(msg, "needs force")) { | |
702 | status = REF_STATUS_REJECT_NEEDS_FORCE; | |
703 | free(msg); | |
704 | msg = NULL; | |
705 | } | |
631b5ef2 JH |
706 | else if (!strcmp(msg, "stale info")) { |
707 | status = REF_STATUS_REJECT_STALE; | |
708 | free(msg); | |
709 | msg = NULL; | |
710 | } | |
d2e73c6f SR |
711 | } |
712 | ||
713 | if (*ref) | |
714 | *ref = find_ref_by_name(*ref, refname); | |
715 | if (!*ref) | |
716 | *ref = find_ref_by_name(remote_refs, refname); | |
717 | if (!*ref) { | |
718 | warning("helper reported unexpected status of %s", refname); | |
664059fb | 719 | return 1; |
d2e73c6f SR |
720 | } |
721 | ||
722 | if ((*ref)->status != REF_STATUS_NONE) { | |
723 | /* | |
724 | * Earlier, the ref was marked not to be pushed, so ignore the ref | |
725 | * status reported by the remote helper if the latter is 'no match'. | |
726 | */ | |
727 | if (status == REF_STATUS_NONE) | |
664059fb | 728 | return 1; |
d2e73c6f SR |
729 | } |
730 | ||
731 | (*ref)->status = status; | |
732 | (*ref)->remote_status = msg; | |
126aac5c | 733 | return !(status == REF_STATUS_OK); |
d2e73c6f SR |
734 | } |
735 | ||
736 | static void push_update_refs_status(struct helper_data *data, | |
737 | struct ref *remote_refs) | |
738 | { | |
739 | struct strbuf buf = STRBUF_INIT; | |
740 | struct ref *ref = remote_refs; | |
741 | for (;;) { | |
664059fb FC |
742 | char *private; |
743 | ||
d2e73c6f SR |
744 | recvline(data, &buf); |
745 | if (!buf.len) | |
746 | break; | |
747 | ||
664059fb FC |
748 | if (push_update_ref_status(&buf, &ref, remote_refs)) |
749 | continue; | |
750 | ||
597b831a | 751 | if (!data->refspecs || data->no_private_update) |
664059fb FC |
752 | continue; |
753 | ||
754 | /* propagate back the update to the remote namespace */ | |
755 | private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name); | |
756 | if (!private) | |
757 | continue; | |
758 | update_ref("update by helper", private, ref->new_sha1, NULL, 0, 0); | |
759 | free(private); | |
d2e73c6f SR |
760 | } |
761 | strbuf_release(&buf); | |
762 | } | |
763 | ||
73b49a75 | 764 | static int push_refs_with_push(struct transport *transport, |
05c1eb10 | 765 | struct ref *remote_refs, int flags) |
ae4efe19 SP |
766 | { |
767 | int force_all = flags & TRANSPORT_PUSH_FORCE; | |
768 | int mirror = flags & TRANSPORT_PUSH_MIRROR; | |
769 | struct helper_data *data = transport->data; | |
770 | struct strbuf buf = STRBUF_INIT; | |
ae4efe19 | 771 | struct ref *ref; |
05c1eb10 JH |
772 | struct string_list cas_options = STRING_LIST_INIT_DUP; |
773 | struct string_list_item *cas_option; | |
ae4efe19 | 774 | |
c0aa335c | 775 | get_helper(transport); |
ae4efe19 SP |
776 | if (!data->push) |
777 | return 1; | |
778 | ||
779 | for (ref = remote_refs; ref; ref = ref->next) { | |
20e8b465 | 780 | if (!ref->peer_ref && !mirror) |
ae4efe19 SP |
781 | continue; |
782 | ||
20e8b465 TRC |
783 | /* Check for statuses set by set_ref_status_for_push() */ |
784 | switch (ref->status) { | |
785 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
631b5ef2 | 786 | case REF_STATUS_REJECT_STALE: |
dbfeddb1 | 787 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
20e8b465 | 788 | case REF_STATUS_UPTODATE: |
ae4efe19 | 789 | continue; |
20e8b465 TRC |
790 | default: |
791 | ; /* do nothing */ | |
ae4efe19 SP |
792 | } |
793 | ||
794 | if (force_all) | |
795 | ref->force = 1; | |
796 | ||
797 | strbuf_addstr(&buf, "push "); | |
798 | if (!ref->deletion) { | |
799 | if (ref->force) | |
800 | strbuf_addch(&buf, '+'); | |
801 | if (ref->peer_ref) | |
802 | strbuf_addstr(&buf, ref->peer_ref->name); | |
803 | else | |
804 | strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1)); | |
805 | } | |
806 | strbuf_addch(&buf, ':'); | |
807 | strbuf_addstr(&buf, ref->name); | |
808 | strbuf_addch(&buf, '\n'); | |
05c1eb10 JH |
809 | |
810 | /* | |
811 | * The "--force-with-lease" options without explicit | |
812 | * values to expect have already been expanded into | |
813 | * the ref->old_sha1_expect[] field; we can ignore | |
814 | * transport->smart_options->cas altogether and instead | |
815 | * can enumerate them from the refs. | |
816 | */ | |
817 | if (ref->expect_old_sha1) { | |
818 | struct strbuf cas = STRBUF_INIT; | |
819 | strbuf_addf(&cas, "%s:%s", | |
820 | ref->name, sha1_to_hex(ref->old_sha1_expect)); | |
821 | string_list_append(&cas_options, strbuf_detach(&cas, NULL)); | |
822 | } | |
ae4efe19 | 823 | } |
05c1eb10 JH |
824 | if (buf.len == 0) { |
825 | string_list_clear(&cas_options, 0); | |
d8f67d20 | 826 | return 0; |
05c1eb10 | 827 | } |
ae4efe19 | 828 | |
ae4efe19 | 829 | standard_options(transport); |
05c1eb10 JH |
830 | for_each_string_list_item(cas_option, &cas_options) |
831 | set_helper_option(transport, "cas", cas_option->string); | |
ae4efe19 SP |
832 | |
833 | if (flags & TRANSPORT_PUSH_DRY_RUN) { | |
834 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
835 | die("helper %s does not support dry-run", data->name); | |
836 | } | |
837 | ||
838 | strbuf_addch(&buf, '\n'); | |
bf3c523c | 839 | sendline(data, &buf); |
ae4efe19 | 840 | strbuf_release(&buf); |
d2e73c6f SR |
841 | |
842 | push_update_refs_status(data, remote_refs); | |
ae4efe19 SP |
843 | return 0; |
844 | } | |
845 | ||
73b49a75 SR |
846 | static int push_refs_with_export(struct transport *transport, |
847 | struct ref *remote_refs, int flags) | |
848 | { | |
849 | struct ref *ref; | |
850 | struct child_process *helper, exporter; | |
851 | struct helper_data *data = transport->data; | |
183113a5 | 852 | struct string_list revlist_args = STRING_LIST_INIT_NODUP; |
73b49a75 SR |
853 | struct strbuf buf = STRBUF_INIT; |
854 | ||
21610d82 FC |
855 | if (!data->refspecs) |
856 | die("remote-helper doesn't support push; refspec needed"); | |
857 | ||
21457f57 FC |
858 | if (flags & TRANSPORT_PUSH_DRY_RUN) { |
859 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
860 | die("helper %s does not support dry-run", data->name); | |
861 | } | |
862 | ||
73b49a75 SR |
863 | helper = get_helper(transport); |
864 | ||
865 | write_constant(helper->in, "export\n"); | |
866 | ||
73b49a75 SR |
867 | strbuf_reset(&buf); |
868 | ||
869 | for (ref = remote_refs; ref; ref = ref->next) { | |
870 | char *private; | |
871 | unsigned char sha1[20]; | |
872 | ||
9c51558c FC |
873 | if (ref->deletion) |
874 | die("remote-helpers do not support ref deletion"); | |
875 | ||
73b49a75 SR |
876 | private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name); |
877 | if (private && !get_sha1(private, sha1)) { | |
878 | strbuf_addf(&buf, "^%s", private); | |
1d2f80fa | 879 | string_list_append(&revlist_args, strbuf_detach(&buf, NULL)); |
b056620f | 880 | hashcpy(ref->old_sha1, sha1); |
73b49a75 | 881 | } |
2faa1527 | 882 | free(private); |
73b49a75 | 883 | |
99d9ec09 | 884 | if (ref->deletion) |
105fe3e4 | 885 | die("remote-helpers do not support ref deletion"); |
105fe3e4 | 886 | |
67c9c782 FC |
887 | if (ref->peer_ref) { |
888 | if (strcmp(ref->peer_ref->name, ref->name)) | |
889 | die("remote-helpers do not support old:new syntax"); | |
3ea7d094 | 890 | string_list_append(&revlist_args, ref->peer_ref->name); |
67c9c782 | 891 | } |
73b49a75 SR |
892 | } |
893 | ||
a515ebe9 | 894 | if (get_exporter(transport, &exporter, &revlist_args)) |
73b49a75 SR |
895 | die("Couldn't run fast-export"); |
896 | ||
cc567322 SR |
897 | if (finish_command(&exporter)) |
898 | die("Error while running fast-export"); | |
6c8151a3 | 899 | push_update_refs_status(data, remote_refs); |
73b49a75 SR |
900 | return 0; |
901 | } | |
902 | ||
903 | static int push_refs(struct transport *transport, | |
904 | struct ref *remote_refs, int flags) | |
905 | { | |
906 | struct helper_data *data = transport->data; | |
907 | ||
908 | if (process_connect(transport, 1)) { | |
909 | do_take_over(transport); | |
910 | return transport->push_refs(transport, remote_refs, flags); | |
911 | } | |
912 | ||
913 | if (!remote_refs) { | |
914 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" | |
915 | "Perhaps you should specify a branch such as 'master'.\n"); | |
916 | return 0; | |
917 | } | |
918 | ||
919 | if (data->push) | |
920 | return push_refs_with_push(transport, remote_refs, flags); | |
921 | ||
922 | if (data->export) | |
923 | return push_refs_with_export(transport, remote_refs, flags); | |
924 | ||
925 | return -1; | |
926 | } | |
927 | ||
928 | ||
f8ec9167 DB |
929 | static int has_attribute(const char *attrs, const char *attr) { |
930 | int len; | |
931 | if (!attrs) | |
932 | return 0; | |
933 | ||
934 | len = strlen(attr); | |
935 | for (;;) { | |
936 | const char *space = strchrnul(attrs, ' '); | |
937 | if (len == space - attrs && !strncmp(attrs, attr, len)) | |
938 | return 1; | |
939 | if (!*space) | |
940 | return 0; | |
941 | attrs = space + 1; | |
942 | } | |
943 | } | |
944 | ||
6eb996b5 DB |
945 | static struct ref *get_refs_list(struct transport *transport, int for_push) |
946 | { | |
292ce46b | 947 | struct helper_data *data = transport->data; |
6eb996b5 DB |
948 | struct child_process *helper; |
949 | struct ref *ret = NULL; | |
950 | struct ref **tail = &ret; | |
951 | struct ref *posn; | |
952 | struct strbuf buf = STRBUF_INIT; | |
6eb996b5 DB |
953 | |
954 | helper = get_helper(transport); | |
2d14d65c | 955 | |
fa8c097c IL |
956 | if (process_connect(transport, for_push)) { |
957 | do_take_over(transport); | |
958 | return transport->get_refs_list(transport, for_push); | |
959 | } | |
960 | ||
ae4efe19 SP |
961 | if (data->push && for_push) |
962 | write_str_in_full(helper->in, "list for-push\n"); | |
963 | else | |
964 | write_str_in_full(helper->in, "list\n"); | |
6eb996b5 | 965 | |
6eb996b5 DB |
966 | while (1) { |
967 | char *eov, *eon; | |
bf3c523c | 968 | recvline(data, &buf); |
6eb996b5 DB |
969 | |
970 | if (!*buf.buf) | |
971 | break; | |
972 | ||
973 | eov = strchr(buf.buf, ' '); | |
974 | if (!eov) | |
975 | die("Malformed response in ref list: %s", buf.buf); | |
976 | eon = strchr(eov + 1, ' '); | |
977 | *eov = '\0'; | |
978 | if (eon) | |
979 | *eon = '\0'; | |
980 | *tail = alloc_ref(eov + 1); | |
981 | if (buf.buf[0] == '@') | |
982 | (*tail)->symref = xstrdup(buf.buf + 1); | |
983 | else if (buf.buf[0] != '?') | |
984 | get_sha1_hex(buf.buf, (*tail)->old_sha1); | |
f8ec9167 DB |
985 | if (eon) { |
986 | if (has_attribute(eon + 1, "unchanged")) { | |
987 | (*tail)->status |= REF_STATUS_UPTODATE; | |
988 | read_ref((*tail)->name, (*tail)->old_sha1); | |
989 | } | |
990 | } | |
6eb996b5 DB |
991 | tail = &((*tail)->next); |
992 | } | |
bf3c523c IL |
993 | if (debug) |
994 | fprintf(stderr, "Debug: Read ref listing.\n"); | |
6eb996b5 DB |
995 | strbuf_release(&buf); |
996 | ||
997 | for (posn = ret; posn; posn = posn->next) | |
998 | resolve_remote_symref(posn, ret); | |
999 | ||
1000 | return ret; | |
1001 | } | |
1002 | ||
c9e388bb | 1003 | int transport_helper_init(struct transport *transport, const char *name) |
6eb996b5 DB |
1004 | { |
1005 | struct helper_data *data = xcalloc(sizeof(*data), 1); | |
c9e388bb | 1006 | data->name = name; |
6eb996b5 | 1007 | |
bf3c523c IL |
1008 | if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) |
1009 | debug = 1; | |
1010 | ||
6eb996b5 | 1011 | transport->data = data; |
ef08ef9e | 1012 | transport->set_option = set_helper_option; |
6eb996b5 DB |
1013 | transport->get_refs_list = get_refs_list; |
1014 | transport->fetch = fetch; | |
ae4efe19 | 1015 | transport->push_refs = push_refs; |
f2a37151 | 1016 | transport->disconnect = release_helper; |
b236752a | 1017 | transport->connect = connect_helper; |
61b075bd | 1018 | transport->smart_options = &(data->transport_options); |
6eb996b5 DB |
1019 | return 0; |
1020 | } | |
419f37db IL |
1021 | |
1022 | /* | |
1023 | * Linux pipes can buffer 65536 bytes at once (and most platforms can | |
1024 | * buffer less), so attempt reads and writes with up to that size. | |
1025 | */ | |
1026 | #define BUFFERSIZE 65536 | |
1027 | /* This should be enough to hold debugging message. */ | |
1028 | #define PBUFFERSIZE 8192 | |
1029 | ||
1030 | /* Print bidirectional transfer loop debug message. */ | |
4621085b | 1031 | __attribute__((format (printf, 1, 2))) |
419f37db IL |
1032 | static void transfer_debug(const char *fmt, ...) |
1033 | { | |
1034 | va_list args; | |
1035 | char msgbuf[PBUFFERSIZE]; | |
1036 | static int debug_enabled = -1; | |
1037 | ||
1038 | if (debug_enabled < 0) | |
1039 | debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0; | |
1040 | if (!debug_enabled) | |
1041 | return; | |
1042 | ||
1043 | va_start(args, fmt); | |
1044 | vsnprintf(msgbuf, PBUFFERSIZE, fmt, args); | |
1045 | va_end(args); | |
1046 | fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf); | |
1047 | } | |
1048 | ||
1049 | /* Stream state: More data may be coming in this direction. */ | |
1050 | #define SSTATE_TRANSFERING 0 | |
1051 | /* | |
1052 | * Stream state: No more data coming in this direction, flushing rest of | |
1053 | * data. | |
1054 | */ | |
1055 | #define SSTATE_FLUSHING 1 | |
1056 | /* Stream state: Transfer in this direction finished. */ | |
1057 | #define SSTATE_FINISHED 2 | |
1058 | ||
1059 | #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING) | |
1060 | #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING) | |
1061 | #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING) | |
1062 | ||
1063 | /* Unidirectional transfer. */ | |
1064 | struct unidirectional_transfer { | |
1065 | /* Source */ | |
1066 | int src; | |
1067 | /* Destination */ | |
1068 | int dest; | |
1069 | /* Is source socket? */ | |
1070 | int src_is_sock; | |
1071 | /* Is destination socket? */ | |
1072 | int dest_is_sock; | |
41ccfdd9 | 1073 | /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */ |
419f37db IL |
1074 | int state; |
1075 | /* Buffer. */ | |
1076 | char buf[BUFFERSIZE]; | |
1077 | /* Buffer used. */ | |
1078 | size_t bufuse; | |
1079 | /* Name of source. */ | |
1080 | const char *src_name; | |
1081 | /* Name of destination. */ | |
1082 | const char *dest_name; | |
1083 | }; | |
1084 | ||
1085 | /* Closes the target (for writing) if transfer has finished. */ | |
1086 | static void udt_close_if_finished(struct unidirectional_transfer *t) | |
1087 | { | |
1088 | if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) { | |
1089 | t->state = SSTATE_FINISHED; | |
1090 | if (t->dest_is_sock) | |
1091 | shutdown(t->dest, SHUT_WR); | |
1092 | else | |
1093 | close(t->dest); | |
1094 | transfer_debug("Closed %s.", t->dest_name); | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * Tries to read read data from source into buffer. If buffer is full, | |
1100 | * no data is read. Returns 0 on success, -1 on error. | |
1101 | */ | |
1102 | static int udt_do_read(struct unidirectional_transfer *t) | |
1103 | { | |
1104 | ssize_t bytes; | |
1105 | ||
1106 | if (t->bufuse == BUFFERSIZE) | |
1107 | return 0; /* No space for more. */ | |
1108 | ||
1109 | transfer_debug("%s is readable", t->src_name); | |
1110 | bytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse); | |
1111 | if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN && | |
1112 | errno != EINTR) { | |
1113 | error("read(%s) failed: %s", t->src_name, strerror(errno)); | |
1114 | return -1; | |
1115 | } else if (bytes == 0) { | |
1116 | transfer_debug("%s EOF (with %i bytes in buffer)", | |
4621085b | 1117 | t->src_name, (int)t->bufuse); |
419f37db IL |
1118 | t->state = SSTATE_FLUSHING; |
1119 | } else if (bytes > 0) { | |
1120 | t->bufuse += bytes; | |
1121 | transfer_debug("Read %i bytes from %s (buffer now at %i)", | |
1122 | (int)bytes, t->src_name, (int)t->bufuse); | |
1123 | } | |
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | /* Tries to write data from buffer into destination. If buffer is empty, | |
1128 | * no data is written. Returns 0 on success, -1 on error. | |
1129 | */ | |
1130 | static int udt_do_write(struct unidirectional_transfer *t) | |
1131 | { | |
803dbdb9 | 1132 | ssize_t bytes; |
419f37db IL |
1133 | |
1134 | if (t->bufuse == 0) | |
1135 | return 0; /* Nothing to write. */ | |
1136 | ||
1137 | transfer_debug("%s is writable", t->dest_name); | |
7edc02f4 EFL |
1138 | bytes = xwrite(t->dest, t->buf, t->bufuse); |
1139 | if (bytes < 0 && errno != EWOULDBLOCK) { | |
419f37db IL |
1140 | error("write(%s) failed: %s", t->dest_name, strerror(errno)); |
1141 | return -1; | |
1142 | } else if (bytes > 0) { | |
1143 | t->bufuse -= bytes; | |
1144 | if (t->bufuse) | |
1145 | memmove(t->buf, t->buf + bytes, t->bufuse); | |
1146 | transfer_debug("Wrote %i bytes to %s (buffer now at %i)", | |
1147 | (int)bytes, t->dest_name, (int)t->bufuse); | |
1148 | } | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | ||
1153 | /* State of bidirectional transfer loop. */ | |
1154 | struct bidirectional_transfer_state { | |
1155 | /* Direction from program to git. */ | |
1156 | struct unidirectional_transfer ptg; | |
1157 | /* Direction from git to program. */ | |
1158 | struct unidirectional_transfer gtp; | |
1159 | }; | |
1160 | ||
1161 | static void *udt_copy_task_routine(void *udt) | |
1162 | { | |
1163 | struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt; | |
1164 | while (t->state != SSTATE_FINISHED) { | |
1165 | if (STATE_NEEDS_READING(t->state)) | |
1166 | if (udt_do_read(t)) | |
1167 | return NULL; | |
1168 | if (STATE_NEEDS_WRITING(t->state)) | |
1169 | if (udt_do_write(t)) | |
1170 | return NULL; | |
1171 | if (STATE_NEEDS_CLOSING(t->state)) | |
1172 | udt_close_if_finished(t); | |
1173 | } | |
1174 | return udt; /* Just some non-NULL value. */ | |
1175 | } | |
1176 | ||
1177 | #ifndef NO_PTHREADS | |
1178 | ||
1179 | /* | |
98e023de | 1180 | * Join thread, with appropriate errors on failure. Name is name for the |
419f37db IL |
1181 | * thread (for error messages). Returns 0 on success, 1 on failure. |
1182 | */ | |
1183 | static int tloop_join(pthread_t thread, const char *name) | |
1184 | { | |
1185 | int err; | |
1186 | void *tret; | |
1187 | err = pthread_join(thread, &tret); | |
1188 | if (!tret) { | |
1189 | error("%s thread failed", name); | |
1190 | return 1; | |
1191 | } | |
1192 | if (err) { | |
1193 | error("%s thread failed to join: %s", name, strerror(err)); | |
1194 | return 1; | |
1195 | } | |
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1201 | * -1 on failure. | |
1202 | */ | |
1203 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1204 | { | |
1205 | pthread_t gtp_thread; | |
1206 | pthread_t ptg_thread; | |
1207 | int err; | |
1208 | int ret = 0; | |
1209 | err = pthread_create(>p_thread, NULL, udt_copy_task_routine, | |
1210 | &s->gtp); | |
1211 | if (err) | |
1212 | die("Can't start thread for copying data: %s", strerror(err)); | |
1213 | err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine, | |
1214 | &s->ptg); | |
1215 | if (err) | |
1216 | die("Can't start thread for copying data: %s", strerror(err)); | |
1217 | ||
1218 | ret |= tloop_join(gtp_thread, "Git to program copy"); | |
1219 | ret |= tloop_join(ptg_thread, "Program to git copy"); | |
1220 | return ret; | |
1221 | } | |
1222 | #else | |
1223 | ||
1224 | /* Close the source and target (for writing) for transfer. */ | |
1225 | static void udt_kill_transfer(struct unidirectional_transfer *t) | |
1226 | { | |
1227 | t->state = SSTATE_FINISHED; | |
1228 | /* | |
1229 | * Socket read end left open isn't a disaster if nobody | |
1230 | * attempts to read from it (mingw compat headers do not | |
1231 | * have SHUT_RD)... | |
1232 | * | |
1233 | * We can't fully close the socket since otherwise gtp | |
1234 | * task would first close the socket it sends data to | |
1235 | * while closing the ptg file descriptors. | |
1236 | */ | |
1237 | if (!t->src_is_sock) | |
1238 | close(t->src); | |
1239 | if (t->dest_is_sock) | |
1240 | shutdown(t->dest, SHUT_WR); | |
1241 | else | |
1242 | close(t->dest); | |
1243 | } | |
1244 | ||
1245 | /* | |
98e023de | 1246 | * Join process, with appropriate errors on failure. Name is name for the |
419f37db IL |
1247 | * process (for error messages). Returns 0 on success, 1 on failure. |
1248 | */ | |
1249 | static int tloop_join(pid_t pid, const char *name) | |
1250 | { | |
1251 | int tret; | |
1252 | if (waitpid(pid, &tret, 0) < 0) { | |
1253 | error("%s process failed to wait: %s", name, strerror(errno)); | |
1254 | return 1; | |
1255 | } | |
1256 | if (!WIFEXITED(tret) || WEXITSTATUS(tret)) { | |
1257 | error("%s process failed", name); | |
1258 | return 1; | |
1259 | } | |
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | /* | |
1264 | * Spawn the transfer tasks and then wait for them. Returns 0 on success, | |
1265 | * -1 on failure. | |
1266 | */ | |
1267 | static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) | |
1268 | { | |
1269 | pid_t pid1, pid2; | |
1270 | int ret = 0; | |
1271 | ||
1272 | /* Fork thread #1: git to program. */ | |
1273 | pid1 = fork(); | |
1274 | if (pid1 < 0) | |
1275 | die_errno("Can't start thread for copying data"); | |
1276 | else if (pid1 == 0) { | |
1277 | udt_kill_transfer(&s->ptg); | |
1278 | exit(udt_copy_task_routine(&s->gtp) ? 0 : 1); | |
1279 | } | |
1280 | ||
1281 | /* Fork thread #2: program to git. */ | |
1282 | pid2 = fork(); | |
1283 | if (pid2 < 0) | |
1284 | die_errno("Can't start thread for copying data"); | |
1285 | else if (pid2 == 0) { | |
1286 | udt_kill_transfer(&s->gtp); | |
1287 | exit(udt_copy_task_routine(&s->ptg) ? 0 : 1); | |
1288 | } | |
1289 | ||
1290 | /* | |
1291 | * Close both streams in parent as to not interfere with | |
1292 | * end of file detection and wait for both tasks to finish. | |
1293 | */ | |
1294 | udt_kill_transfer(&s->gtp); | |
1295 | udt_kill_transfer(&s->ptg); | |
1296 | ret |= tloop_join(pid1, "Git to program copy"); | |
1297 | ret |= tloop_join(pid2, "Program to git copy"); | |
1298 | return ret; | |
1299 | } | |
1300 | #endif | |
1301 | ||
1302 | /* | |
1303 | * Copies data from stdin to output and from input to stdout simultaneously. | |
1304 | * Additionally filtering through given filter. If filter is NULL, uses | |
1305 | * identity filter. | |
1306 | */ | |
1307 | int bidirectional_transfer_loop(int input, int output) | |
1308 | { | |
1309 | struct bidirectional_transfer_state state; | |
1310 | ||
1311 | /* Fill the state fields. */ | |
1312 | state.ptg.src = input; | |
1313 | state.ptg.dest = 1; | |
1314 | state.ptg.src_is_sock = (input == output); | |
1315 | state.ptg.dest_is_sock = 0; | |
1316 | state.ptg.state = SSTATE_TRANSFERING; | |
1317 | state.ptg.bufuse = 0; | |
1318 | state.ptg.src_name = "remote input"; | |
1319 | state.ptg.dest_name = "stdout"; | |
1320 | ||
1321 | state.gtp.src = 0; | |
1322 | state.gtp.dest = output; | |
1323 | state.gtp.src_is_sock = 0; | |
1324 | state.gtp.dest_is_sock = (input == output); | |
1325 | state.gtp.state = SSTATE_TRANSFERING; | |
1326 | state.gtp.bufuse = 0; | |
1327 | state.gtp.src_name = "stdin"; | |
1328 | state.gtp.dest_name = "remote output"; | |
1329 | ||
1330 | return tloop_spawnwait_tasks(&state); | |
1331 | } |