4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
18 struct child_process
*helper
;
26 no_disconnect_req
: 1;
29 /* These go from remote name (as in "list") to private name */
30 struct refspec
*refspecs
;
32 /* Transport options for fetch-pack/send-pack (should one of
35 struct git_transport_options transport_options
;
38 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
41 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
42 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
44 die_errno("Full write to remote helper failed");
47 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
51 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
52 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
54 fprintf(stderr
, "Debug: Remote helper quit.\n");
59 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
63 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
65 return recvline_fh(helper
->out
, buffer
);
68 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
70 sendline(helper
, buffer
);
71 recvline(helper
, buffer
);
74 static void write_constant(int fd
, const char *str
)
77 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
78 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
79 die_errno("Full write to remote helper failed");
82 static const char *remove_ext_force(const char *url
)
85 const char *colon
= strchr(url
, ':');
86 if (colon
&& colon
[1] == ':')
92 static void do_take_over(struct transport
*transport
)
94 struct helper_data
*data
;
95 data
= (struct helper_data
*)transport
->data
;
96 transport_take_over(transport
, data
->helper
);
101 static struct child_process
*get_helper(struct transport
*transport
)
103 struct helper_data
*data
= transport
->data
;
104 struct strbuf buf
= STRBUF_INIT
;
105 struct child_process
*helper
;
106 const char **refspecs
= NULL
;
108 int refspec_alloc
= 0;
111 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
112 const char *helper_env
[] = {
121 helper
= xcalloc(1, sizeof(*helper
));
125 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
126 strbuf_addf(&buf
, "git-remote-%s", data
->name
);
127 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
128 helper
->argv
[1] = transport
->remote
->name
;
129 helper
->argv
[2] = remove_ext_force(transport
->url
);
131 helper
->silent_exec_failure
= 1;
133 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
134 helper
->env
= helper_env
;
136 code
= start_command(helper
);
137 if (code
< 0 && errno
== ENOENT
)
138 die("Unable to find remote helper for '%s'", data
->name
);
142 data
->helper
= helper
;
143 data
->no_disconnect_req
= 0;
146 * Open the output as FILE* so strbuf_getline() can be used.
147 * Do this with duped fd because fclose() will close the fd,
148 * and stuff like taking over will require the fd to remain.
150 duped
= dup(helper
->out
);
152 die_errno("Can't dup helper output fd");
153 data
->out
= xfdopen(duped
, "r");
155 write_constant(helper
->in
, "capabilities\n");
160 recvline(data
, &buf
);
165 if (*buf
.buf
== '*') {
166 capname
= buf
.buf
+ 1;
172 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
173 if (!strcmp(capname
, "fetch"))
175 else if (!strcmp(capname
, "option"))
177 else if (!strcmp(capname
, "push"))
179 else if (!strcmp(capname
, "import"))
181 else if (!strcmp(capname
, "export"))
183 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
187 refspecs
[refspec_nr
++] = xstrdup(capname
+ strlen("refspec "));
188 } else if (!strcmp(capname
, "connect")) {
190 } else if (!prefixcmp(capname
, "export-marks ")) {
191 struct strbuf arg
= STRBUF_INIT
;
192 strbuf_addstr(&arg
, "--export-marks=");
193 strbuf_addstr(&arg
, capname
+ strlen("export-marks "));
194 data
->export_marks
= strbuf_detach(&arg
, NULL
);
195 } else if (!prefixcmp(capname
, "import-marks")) {
196 struct strbuf arg
= STRBUF_INIT
;
197 strbuf_addstr(&arg
, "--import-marks=");
198 strbuf_addstr(&arg
, capname
+ strlen("import-marks "));
199 data
->import_marks
= strbuf_detach(&arg
, NULL
);
200 } else if (mandatory
) {
201 die("Unknown mandatory capability %s. This remote "
202 "helper probably needs newer version of Git.",
208 data
->refspec_nr
= refspec_nr
;
209 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
210 for (i
= 0; i
< refspec_nr
; i
++) {
211 free((char *)refspecs
[i
]);
215 strbuf_release(&buf
);
217 fprintf(stderr
, "Debug: Capabilities complete.\n");
221 static int disconnect_helper(struct transport
*transport
)
223 struct helper_data
*data
= transport
->data
;
228 fprintf(stderr
, "Debug: Disconnecting.\n");
229 if (!data
->no_disconnect_req
) {
231 * Ignore write errors; there's nothing we can do,
232 * since we're about to close the pipe anyway. And the
233 * most likely error is EPIPE due to the helper dying
234 * to report an error itself.
236 sigchain_push(SIGPIPE
, SIG_IGN
);
237 xwrite(data
->helper
->in
, "\n", 1);
238 sigchain_pop(SIGPIPE
);
240 close(data
->helper
->in
);
241 close(data
->helper
->out
);
243 res
= finish_command(data
->helper
);
244 free((char *)data
->helper
->argv
[0]);
245 free(data
->helper
->argv
);
252 static const char *unsupported_options
[] = {
253 TRANS_OPT_UPLOADPACK
,
254 TRANS_OPT_RECEIVEPACK
,
258 static const char *boolean_options
[] = {
264 static int set_helper_option(struct transport
*transport
,
265 const char *name
, const char *value
)
267 struct helper_data
*data
= transport
->data
;
268 struct strbuf buf
= STRBUF_INIT
;
269 int i
, ret
, is_bool
= 0;
271 get_helper(transport
);
276 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
277 if (!strcmp(name
, unsupported_options
[i
]))
281 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
282 if (!strcmp(name
, boolean_options
[i
])) {
288 strbuf_addf(&buf
, "option %s ", name
);
290 strbuf_addstr(&buf
, value
? "true" : "false");
292 quote_c_style(value
, &buf
, NULL
, 0);
293 strbuf_addch(&buf
, '\n');
295 xchgline(data
, &buf
);
297 if (!strcmp(buf
.buf
, "ok"))
299 else if (!prefixcmp(buf
.buf
, "error")) {
301 } else if (!strcmp(buf
.buf
, "unsupported"))
304 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
307 strbuf_release(&buf
);
311 static void standard_options(struct transport
*t
)
317 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
319 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
320 if (n
>= sizeof(buf
))
321 die("impossibly large verbosity value");
322 set_helper_option(t
, "verbosity", buf
);
325 static int release_helper(struct transport
*transport
)
328 struct helper_data
*data
= transport
->data
;
329 free_refspec(data
->refspec_nr
, data
->refspecs
);
330 data
->refspecs
= NULL
;
331 res
= disconnect_helper(transport
);
332 free(transport
->data
);
336 static int fetch_with_fetch(struct transport
*transport
,
337 int nr_heads
, struct ref
**to_fetch
)
339 struct helper_data
*data
= transport
->data
;
341 struct strbuf buf
= STRBUF_INIT
;
343 standard_options(transport
);
345 for (i
= 0; i
< nr_heads
; i
++) {
346 const struct ref
*posn
= to_fetch
[i
];
347 if (posn
->status
& REF_STATUS_UPTODATE
)
350 strbuf_addf(&buf
, "fetch %s %s\n",
351 sha1_to_hex(posn
->old_sha1
), posn
->name
);
354 strbuf_addch(&buf
, '\n');
355 sendline(data
, &buf
);
358 recvline(data
, &buf
);
360 if (!prefixcmp(buf
.buf
, "lock ")) {
361 const char *name
= buf
.buf
+ 5;
362 if (transport
->pack_lockfile
)
363 warning("%s also locked %s", data
->name
, name
);
365 transport
->pack_lockfile
= xstrdup(name
);
370 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
372 strbuf_release(&buf
);
376 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
378 struct child_process
*helper
= get_helper(transport
);
379 memset(fastimport
, 0, sizeof(*fastimport
));
380 fastimport
->in
= helper
->out
;
381 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
382 fastimport
->argv
[0] = "fast-import";
383 fastimport
->argv
[1] = "--quiet";
385 fastimport
->git_cmd
= 1;
386 return start_command(fastimport
);
389 static int get_exporter(struct transport
*transport
,
390 struct child_process
*fastexport
,
391 struct string_list
*revlist_args
)
393 struct helper_data
*data
= transport
->data
;
394 struct child_process
*helper
= get_helper(transport
);
396 memset(fastexport
, 0, sizeof(*fastexport
));
398 /* we need to duplicate helper->in because we want to use it after
399 * fastexport is done with it. */
400 fastexport
->out
= dup(helper
->in
);
401 fastexport
->argv
= xcalloc(6 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
402 fastexport
->argv
[argc
++] = "fast-export";
403 fastexport
->argv
[argc
++] = "--use-done-feature";
404 if (data
->export_marks
)
405 fastexport
->argv
[argc
++] = data
->export_marks
;
406 if (data
->import_marks
)
407 fastexport
->argv
[argc
++] = data
->import_marks
;
409 for (i
= 0; i
< revlist_args
->nr
; i
++)
410 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
412 fastexport
->argv
[argc
++] = "--";
414 fastexport
->git_cmd
= 1;
415 return start_command(fastexport
);
418 static void check_helper_status(struct helper_data
*data
)
422 pid
= waitpid(data
->helper
->pid
, &status
, WNOHANG
);
424 die("Could not retrieve status of remote helper '%s'",
426 if (pid
> 0 && WIFEXITED(status
))
427 die("Remote helper '%s' died with %d",
428 data
->name
, WEXITSTATUS(status
));
431 static int fetch_with_import(struct transport
*transport
,
432 int nr_heads
, struct ref
**to_fetch
)
434 struct child_process fastimport
;
435 struct helper_data
*data
= transport
->data
;
438 struct strbuf buf
= STRBUF_INIT
;
440 get_helper(transport
);
442 if (get_importer(transport
, &fastimport
))
443 die("Couldn't run fast-import");
445 for (i
= 0; i
< nr_heads
; i
++) {
447 if (posn
->status
& REF_STATUS_UPTODATE
)
450 strbuf_addf(&buf
, "import %s\n", posn
->name
);
451 sendline(data
, &buf
);
455 write_constant(data
->helper
->in
, "\n");
457 if (finish_command(&fastimport
))
458 die("Error while running fast-import");
459 check_helper_status(data
);
461 free(fastimport
.argv
);
462 fastimport
.argv
= NULL
;
465 * The fast-import stream of a remote helper that advertises
466 * the "refspec" capability writes to the refs named after the
467 * right hand side of the first refspec matching each ref we
470 * (If no "refspec" capability was specified, for historical
471 * reasons we default to *:*.)
473 * Store the result in to_fetch[i].old_sha1. Callers such
474 * as "git fetch" can use the value to write feedback to the
475 * terminal, populate FETCH_HEAD, and determine what new value
476 * should be written to peer_ref if the update is a
477 * fast-forward or this is a forced update.
479 for (i
= 0; i
< nr_heads
; i
++) {
482 if (posn
->status
& REF_STATUS_UPTODATE
)
485 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
487 private = xstrdup(posn
->name
);
489 read_ref(private, posn
->old_sha1
);
493 strbuf_release(&buf
);
497 static int process_connect_service(struct transport
*transport
,
498 const char *name
, const char *exec
)
500 struct helper_data
*data
= transport
->data
;
501 struct strbuf cmdbuf
= STRBUF_INIT
;
502 struct child_process
*helper
;
503 int r
, duped
, ret
= 0;
506 helper
= get_helper(transport
);
509 * Yes, dup the pipe another time, as we need unbuffered version
510 * of input pipe as FILE*. fclose() closes the underlying fd and
511 * stream buffering only can be changed before first I/O operation
514 duped
= dup(helper
->out
);
516 die_errno("Can't dup helper output fd");
517 input
= xfdopen(duped
, "r");
518 setvbuf(input
, NULL
, _IONBF
, 0);
521 * Handle --upload-pack and friends. This is fire and forget...
522 * just warn if it fails.
524 if (strcmp(name
, exec
)) {
525 r
= set_helper_option(transport
, "servpath", exec
);
527 warning("Setting remote service path not supported by protocol.");
529 warning("Invalid remote service path.");
533 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
537 sendline(data
, &cmdbuf
);
538 recvline_fh(input
, &cmdbuf
);
539 if (!strcmp(cmdbuf
.buf
, "")) {
540 data
->no_disconnect_req
= 1;
542 fprintf(stderr
, "Debug: Smart transport connection "
545 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
547 fprintf(stderr
, "Debug: Falling back to dumb "
550 die("Unknown response to connect: %s",
558 static int process_connect(struct transport
*transport
,
561 struct helper_data
*data
= transport
->data
;
565 name
= for_push
? "git-receive-pack" : "git-upload-pack";
567 exec
= data
->transport_options
.receivepack
;
569 exec
= data
->transport_options
.uploadpack
;
571 return process_connect_service(transport
, name
, exec
);
574 static int connect_helper(struct transport
*transport
, const char *name
,
575 const char *exec
, int fd
[2])
577 struct helper_data
*data
= transport
->data
;
579 /* Get_helper so connect is inited. */
580 get_helper(transport
);
582 die("Operation not supported by protocol.");
584 if (!process_connect_service(transport
, name
, exec
))
585 die("Can't connect to subservice %s.", name
);
587 fd
[0] = data
->helper
->out
;
588 fd
[1] = data
->helper
->in
;
592 static int fetch(struct transport
*transport
,
593 int nr_heads
, struct ref
**to_fetch
)
595 struct helper_data
*data
= transport
->data
;
598 if (process_connect(transport
, 0)) {
599 do_take_over(transport
);
600 return transport
->fetch(transport
, nr_heads
, to_fetch
);
604 for (i
= 0; i
< nr_heads
; i
++)
605 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
612 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
615 return fetch_with_import(transport
, nr_heads
, to_fetch
);
620 static void push_update_ref_status(struct strbuf
*buf
,
622 struct ref
*remote_refs
)
627 if (!prefixcmp(buf
->buf
, "ok ")) {
628 status
= REF_STATUS_OK
;
629 refname
= buf
->buf
+ 3;
630 } else if (!prefixcmp(buf
->buf
, "error ")) {
631 status
= REF_STATUS_REMOTE_REJECT
;
632 refname
= buf
->buf
+ 6;
634 die("expected ok/error, helper said '%s'", buf
->buf
);
636 msg
= strchr(refname
, ' ');
638 struct strbuf msg_buf
= STRBUF_INIT
;
642 if (!unquote_c_style(&msg_buf
, msg
, &end
))
643 msg
= strbuf_detach(&msg_buf
, NULL
);
646 strbuf_release(&msg_buf
);
648 if (!strcmp(msg
, "no match")) {
649 status
= REF_STATUS_NONE
;
653 else if (!strcmp(msg
, "up to date")) {
654 status
= REF_STATUS_UPTODATE
;
658 else if (!strcmp(msg
, "non-fast forward")) {
659 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
666 *ref
= find_ref_by_name(*ref
, refname
);
668 *ref
= find_ref_by_name(remote_refs
, refname
);
670 warning("helper reported unexpected status of %s", refname
);
674 if ((*ref
)->status
!= REF_STATUS_NONE
) {
676 * Earlier, the ref was marked not to be pushed, so ignore the ref
677 * status reported by the remote helper if the latter is 'no match'.
679 if (status
== REF_STATUS_NONE
)
683 (*ref
)->status
= status
;
684 (*ref
)->remote_status
= msg
;
687 static void push_update_refs_status(struct helper_data
*data
,
688 struct ref
*remote_refs
)
690 struct strbuf buf
= STRBUF_INIT
;
691 struct ref
*ref
= remote_refs
;
693 recvline(data
, &buf
);
697 push_update_ref_status(&buf
, &ref
, remote_refs
);
699 strbuf_release(&buf
);
702 static int push_refs_with_push(struct transport
*transport
,
703 struct ref
*remote_refs
, int flags
)
705 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
706 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
707 struct helper_data
*data
= transport
->data
;
708 struct strbuf buf
= STRBUF_INIT
;
711 get_helper(transport
);
715 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
716 if (!ref
->peer_ref
&& !mirror
)
719 /* Check for statuses set by set_ref_status_for_push() */
720 switch (ref
->status
) {
721 case REF_STATUS_REJECT_NONFASTFORWARD
:
722 case REF_STATUS_UPTODATE
:
731 strbuf_addstr(&buf
, "push ");
732 if (!ref
->deletion
) {
734 strbuf_addch(&buf
, '+');
736 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
738 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
740 strbuf_addch(&buf
, ':');
741 strbuf_addstr(&buf
, ref
->name
);
742 strbuf_addch(&buf
, '\n');
747 standard_options(transport
);
749 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
750 if (set_helper_option(transport
, "dry-run", "true") != 0)
751 die("helper %s does not support dry-run", data
->name
);
754 strbuf_addch(&buf
, '\n');
755 sendline(data
, &buf
);
756 strbuf_release(&buf
);
758 push_update_refs_status(data
, remote_refs
);
762 static int push_refs_with_export(struct transport
*transport
,
763 struct ref
*remote_refs
, int flags
)
766 struct child_process
*helper
, exporter
;
767 struct helper_data
*data
= transport
->data
;
768 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
769 struct strbuf buf
= STRBUF_INIT
;
771 helper
= get_helper(transport
);
773 write_constant(helper
->in
, "export\n");
777 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
779 unsigned char sha1
[20];
783 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
784 if (private && !get_sha1(private, sha1
)) {
785 strbuf_addf(&buf
, "^%s", private);
786 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
791 die("remote-helpers do not support ref deletion");
795 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
799 if (get_exporter(transport
, &exporter
, &revlist_args
))
800 die("Couldn't run fast-export");
802 if (finish_command(&exporter
))
803 die("Error while running fast-export");
804 check_helper_status(data
);
805 push_update_refs_status(data
, remote_refs
);
809 static int push_refs(struct transport
*transport
,
810 struct ref
*remote_refs
, int flags
)
812 struct helper_data
*data
= transport
->data
;
814 if (process_connect(transport
, 1)) {
815 do_take_over(transport
);
816 return transport
->push_refs(transport
, remote_refs
, flags
);
820 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
821 "Perhaps you should specify a branch such as 'master'.\n");
826 return push_refs_with_push(transport
, remote_refs
, flags
);
829 return push_refs_with_export(transport
, remote_refs
, flags
);
835 static int has_attribute(const char *attrs
, const char *attr
) {
842 const char *space
= strchrnul(attrs
, ' ');
843 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
851 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
853 struct helper_data
*data
= transport
->data
;
854 struct child_process
*helper
;
855 struct ref
*ret
= NULL
;
856 struct ref
**tail
= &ret
;
858 struct strbuf buf
= STRBUF_INIT
;
860 helper
= get_helper(transport
);
862 if (process_connect(transport
, for_push
)) {
863 do_take_over(transport
);
864 return transport
->get_refs_list(transport
, for_push
);
867 if (data
->push
&& for_push
)
868 write_str_in_full(helper
->in
, "list for-push\n");
870 write_str_in_full(helper
->in
, "list\n");
874 recvline(data
, &buf
);
879 eov
= strchr(buf
.buf
, ' ');
881 die("Malformed response in ref list: %s", buf
.buf
);
882 eon
= strchr(eov
+ 1, ' ');
886 *tail
= alloc_ref(eov
+ 1);
887 if (buf
.buf
[0] == '@')
888 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
889 else if (buf
.buf
[0] != '?')
890 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
892 if (has_attribute(eon
+ 1, "unchanged")) {
893 (*tail
)->status
|= REF_STATUS_UPTODATE
;
894 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
897 tail
= &((*tail
)->next
);
900 fprintf(stderr
, "Debug: Read ref listing.\n");
901 strbuf_release(&buf
);
903 for (posn
= ret
; posn
; posn
= posn
->next
)
904 resolve_remote_symref(posn
, ret
);
909 int transport_helper_init(struct transport
*transport
, const char *name
)
911 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
914 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
917 transport
->data
= data
;
918 transport
->set_option
= set_helper_option
;
919 transport
->get_refs_list
= get_refs_list
;
920 transport
->fetch
= fetch
;
921 transport
->push_refs
= push_refs
;
922 transport
->disconnect
= release_helper
;
923 transport
->connect
= connect_helper
;
924 transport
->smart_options
= &(data
->transport_options
);
929 * Linux pipes can buffer 65536 bytes at once (and most platforms can
930 * buffer less), so attempt reads and writes with up to that size.
932 #define BUFFERSIZE 65536
933 /* This should be enough to hold debugging message. */
934 #define PBUFFERSIZE 8192
936 /* Print bidirectional transfer loop debug message. */
937 static void transfer_debug(const char *fmt
, ...)
940 char msgbuf
[PBUFFERSIZE
];
941 static int debug_enabled
= -1;
943 if (debug_enabled
< 0)
944 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
949 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
951 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
954 /* Stream state: More data may be coming in this direction. */
955 #define SSTATE_TRANSFERING 0
957 * Stream state: No more data coming in this direction, flushing rest of
960 #define SSTATE_FLUSHING 1
961 /* Stream state: Transfer in this direction finished. */
962 #define SSTATE_FINISHED 2
964 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
965 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
966 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
968 /* Unidirectional transfer. */
969 struct unidirectional_transfer
{
974 /* Is source socket? */
976 /* Is destination socket? */
978 /* Transfer state (TRANSFERING/FLUSHING/FINISHED) */
981 char buf
[BUFFERSIZE
];
984 /* Name of source. */
985 const char *src_name
;
986 /* Name of destination. */
987 const char *dest_name
;
990 /* Closes the target (for writing) if transfer has finished. */
991 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
993 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
994 t
->state
= SSTATE_FINISHED
;
996 shutdown(t
->dest
, SHUT_WR
);
999 transfer_debug("Closed %s.", t
->dest_name
);
1004 * Tries to read read data from source into buffer. If buffer is full,
1005 * no data is read. Returns 0 on success, -1 on error.
1007 static int udt_do_read(struct unidirectional_transfer
*t
)
1011 if (t
->bufuse
== BUFFERSIZE
)
1012 return 0; /* No space for more. */
1014 transfer_debug("%s is readable", t
->src_name
);
1015 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
1016 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1018 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
1020 } else if (bytes
== 0) {
1021 transfer_debug("%s EOF (with %i bytes in buffer)",
1022 t
->src_name
, t
->bufuse
);
1023 t
->state
= SSTATE_FLUSHING
;
1024 } else if (bytes
> 0) {
1026 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1027 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
1032 /* Tries to write data from buffer into destination. If buffer is empty,
1033 * no data is written. Returns 0 on success, -1 on error.
1035 static int udt_do_write(struct unidirectional_transfer
*t
)
1040 return 0; /* Nothing to write. */
1042 transfer_debug("%s is writable", t
->dest_name
);
1043 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1044 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1046 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1048 } else if (bytes
> 0) {
1051 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1052 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1053 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1059 /* State of bidirectional transfer loop. */
1060 struct bidirectional_transfer_state
{
1061 /* Direction from program to git. */
1062 struct unidirectional_transfer ptg
;
1063 /* Direction from git to program. */
1064 struct unidirectional_transfer gtp
;
1067 static void *udt_copy_task_routine(void *udt
)
1069 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1070 while (t
->state
!= SSTATE_FINISHED
) {
1071 if (STATE_NEEDS_READING(t
->state
))
1074 if (STATE_NEEDS_WRITING(t
->state
))
1075 if (udt_do_write(t
))
1077 if (STATE_NEEDS_CLOSING(t
->state
))
1078 udt_close_if_finished(t
);
1080 return udt
; /* Just some non-NULL value. */
1086 * Join thread, with apporiate errors on failure. Name is name for the
1087 * thread (for error messages). Returns 0 on success, 1 on failure.
1089 static int tloop_join(pthread_t thread
, const char *name
)
1093 err
= pthread_join(thread
, &tret
);
1095 error("%s thread failed", name
);
1099 error("%s thread failed to join: %s", name
, strerror(err
));
1106 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1109 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1111 pthread_t gtp_thread
;
1112 pthread_t ptg_thread
;
1115 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1118 die("Can't start thread for copying data: %s", strerror(err
));
1119 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1122 die("Can't start thread for copying data: %s", strerror(err
));
1124 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1125 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1130 /* Close the source and target (for writing) for transfer. */
1131 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1133 t
->state
= SSTATE_FINISHED
;
1135 * Socket read end left open isn't a disaster if nobody
1136 * attempts to read from it (mingw compat headers do not
1139 * We can't fully close the socket since otherwise gtp
1140 * task would first close the socket it sends data to
1141 * while closing the ptg file descriptors.
1143 if (!t
->src_is_sock
)
1145 if (t
->dest_is_sock
)
1146 shutdown(t
->dest
, SHUT_WR
);
1152 * Join process, with apporiate errors on failure. Name is name for the
1153 * process (for error messages). Returns 0 on success, 1 on failure.
1155 static int tloop_join(pid_t pid
, const char *name
)
1158 if (waitpid(pid
, &tret
, 0) < 0) {
1159 error("%s process failed to wait: %s", name
, strerror(errno
));
1162 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1163 error("%s process failed", name
);
1170 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1173 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1178 /* Fork thread #1: git to program. */
1181 die_errno("Can't start thread for copying data");
1182 else if (pid1
== 0) {
1183 udt_kill_transfer(&s
->ptg
);
1184 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1187 /* Fork thread #2: program to git. */
1190 die_errno("Can't start thread for copying data");
1191 else if (pid2
== 0) {
1192 udt_kill_transfer(&s
->gtp
);
1193 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1197 * Close both streams in parent as to not interfere with
1198 * end of file detection and wait for both tasks to finish.
1200 udt_kill_transfer(&s
->gtp
);
1201 udt_kill_transfer(&s
->ptg
);
1202 ret
|= tloop_join(pid1
, "Git to program copy");
1203 ret
|= tloop_join(pid2
, "Program to git copy");
1209 * Copies data from stdin to output and from input to stdout simultaneously.
1210 * Additionally filtering through given filter. If filter is NULL, uses
1213 int bidirectional_transfer_loop(int input
, int output
)
1215 struct bidirectional_transfer_state state
;
1217 /* Fill the state fields. */
1218 state
.ptg
.src
= input
;
1220 state
.ptg
.src_is_sock
= (input
== output
);
1221 state
.ptg
.dest_is_sock
= 0;
1222 state
.ptg
.state
= SSTATE_TRANSFERING
;
1223 state
.ptg
.bufuse
= 0;
1224 state
.ptg
.src_name
= "remote input";
1225 state
.ptg
.dest_name
= "stdout";
1228 state
.gtp
.dest
= output
;
1229 state
.gtp
.src_is_sock
= 0;
1230 state
.gtp
.dest_is_sock
= (input
== output
);
1231 state
.gtp
.state
= SSTATE_TRANSFERING
;
1232 state
.gtp
.bufuse
= 0;
1233 state
.gtp
.src_name
= "stdin";
1234 state
.gtp
.dest_name
= "remote output";
1236 return tloop_spawnwait_tasks(&state
);