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