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