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