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