4 #include "run-command.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
17 struct child_process
*helper
;
25 no_disconnect_req
: 1;
26 /* These go from remote name (as in "list") to private name */
27 struct refspec
*refspecs
;
29 /* Transport options for fetch-pack/send-pack (should one of
32 struct git_transport_options transport_options
;
35 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
38 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
39 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
41 die_errno("Full write to remote helper failed");
44 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
48 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
49 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
51 fprintf(stderr
, "Debug: Remote helper quit.\n");
56 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
60 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
62 return recvline_fh(helper
->out
, buffer
);
65 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
67 sendline(helper
, buffer
);
68 recvline(helper
, buffer
);
71 static void write_constant(int fd
, const char *str
)
74 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
75 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
76 die_errno("Full write to remote helper failed");
79 static const char *remove_ext_force(const char *url
)
82 const char *colon
= strchr(url
, ':');
83 if (colon
&& colon
[1] == ':')
89 static void do_take_over(struct transport
*transport
)
91 struct helper_data
*data
;
92 data
= (struct helper_data
*)transport
->data
;
93 transport_take_over(transport
, data
->helper
);
98 static struct child_process
*get_helper(struct transport
*transport
)
100 struct helper_data
*data
= transport
->data
;
101 struct strbuf buf
= STRBUF_INIT
;
102 struct child_process
*helper
;
103 const char **refspecs
= NULL
;
105 int refspec_alloc
= 0;
108 char git_dir_buf
[sizeof(GIT_DIR_ENVIRONMENT
) + PATH_MAX
+ 1];
109 const char *helper_env
[] = {
118 helper
= xcalloc(1, sizeof(*helper
));
122 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
123 strbuf_addf(&buf
, "git-remote-%s", data
->name
);
124 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
125 helper
->argv
[1] = transport
->remote
->name
;
126 helper
->argv
[2] = remove_ext_force(transport
->url
);
128 helper
->silent_exec_failure
= 1;
130 snprintf(git_dir_buf
, sizeof(git_dir_buf
), "%s=%s", GIT_DIR_ENVIRONMENT
, get_git_dir());
131 helper
->env
= helper_env
;
133 code
= start_command(helper
);
134 if (code
< 0 && errno
== ENOENT
)
135 die("Unable to find remote helper for '%s'", data
->name
);
139 data
->helper
= helper
;
140 data
->no_disconnect_req
= 0;
143 * Open the output as FILE* so strbuf_getline() can be used.
144 * Do this with duped fd because fclose() will close the fd,
145 * and stuff like taking over will require the fd to remain.
147 duped
= dup(helper
->out
);
149 die_errno("Can't dup helper output fd");
150 data
->out
= xfdopen(duped
, "r");
152 write_constant(helper
->in
, "capabilities\n");
157 recvline(data
, &buf
);
162 if (*buf
.buf
== '*') {
163 capname
= buf
.buf
+ 1;
169 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
170 if (!strcmp(capname
, "fetch"))
172 else if (!strcmp(capname
, "option"))
174 else if (!strcmp(capname
, "push"))
176 else if (!strcmp(capname
, "import"))
178 else if (!strcmp(capname
, "export"))
180 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
184 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
185 } else if (!strcmp(capname
, "connect")) {
187 } else if (mandatory
) {
188 die("Unknown mandatory capability %s. This remote "
189 "helper probably needs newer version of Git.\n",
195 data
->refspec_nr
= refspec_nr
;
196 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
197 for (i
= 0; i
< refspec_nr
; i
++) {
198 free((char *)refspecs
[i
]);
202 strbuf_release(&buf
);
204 fprintf(stderr
, "Debug: Capabilities complete.\n");
208 static int disconnect_helper(struct transport
*transport
)
210 struct helper_data
*data
= transport
->data
;
211 struct strbuf buf
= STRBUF_INIT
;
216 fprintf(stderr
, "Debug: Disconnecting.\n");
217 if (!data
->no_disconnect_req
) {
218 strbuf_addf(&buf
, "\n");
219 sendline(data
, &buf
);
221 close(data
->helper
->in
);
222 close(data
->helper
->out
);
224 res
= finish_command(data
->helper
);
225 free((char *)data
->helper
->argv
[0]);
226 free(data
->helper
->argv
);
233 static const char *unsupported_options
[] = {
234 TRANS_OPT_UPLOADPACK
,
235 TRANS_OPT_RECEIVEPACK
,
239 static const char *boolean_options
[] = {
245 static int set_helper_option(struct transport
*transport
,
246 const char *name
, const char *value
)
248 struct helper_data
*data
= transport
->data
;
249 struct strbuf buf
= STRBUF_INIT
;
250 int i
, ret
, is_bool
= 0;
252 get_helper(transport
);
257 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
258 if (!strcmp(name
, unsupported_options
[i
]))
262 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
263 if (!strcmp(name
, boolean_options
[i
])) {
269 strbuf_addf(&buf
, "option %s ", name
);
271 strbuf_addstr(&buf
, value
? "true" : "false");
273 quote_c_style(value
, &buf
, NULL
, 0);
274 strbuf_addch(&buf
, '\n');
276 xchgline(data
, &buf
);
278 if (!strcmp(buf
.buf
, "ok"))
280 else if (!prefixcmp(buf
.buf
, "error")) {
282 } else if (!strcmp(buf
.buf
, "unsupported"))
285 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
288 strbuf_release(&buf
);
292 static void standard_options(struct transport
*t
)
298 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
300 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
301 if (n
>= sizeof(buf
))
302 die("impossibly large verbosity value");
303 set_helper_option(t
, "verbosity", buf
);
306 static int release_helper(struct transport
*transport
)
309 struct helper_data
*data
= transport
->data
;
310 free_refspec(data
->refspec_nr
, data
->refspecs
);
311 data
->refspecs
= NULL
;
312 res
= disconnect_helper(transport
);
313 free(transport
->data
);
317 static int fetch_with_fetch(struct transport
*transport
,
318 int nr_heads
, struct ref
**to_fetch
)
320 struct helper_data
*data
= transport
->data
;
322 struct strbuf buf
= STRBUF_INIT
;
324 standard_options(transport
);
326 for (i
= 0; i
< nr_heads
; i
++) {
327 const struct ref
*posn
= to_fetch
[i
];
328 if (posn
->status
& REF_STATUS_UPTODATE
)
331 strbuf_addf(&buf
, "fetch %s %s\n",
332 sha1_to_hex(posn
->old_sha1
), posn
->name
);
335 strbuf_addch(&buf
, '\n');
336 sendline(data
, &buf
);
339 recvline(data
, &buf
);
341 if (!prefixcmp(buf
.buf
, "lock ")) {
342 const char *name
= buf
.buf
+ 5;
343 if (transport
->pack_lockfile
)
344 warning("%s also locked %s", data
->name
, name
);
346 transport
->pack_lockfile
= xstrdup(name
);
351 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
353 strbuf_release(&buf
);
357 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
359 struct child_process
*helper
= get_helper(transport
);
360 memset(fastimport
, 0, sizeof(*fastimport
));
361 fastimport
->in
= helper
->out
;
362 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
363 fastimport
->argv
[0] = "fast-import";
364 fastimport
->argv
[1] = "--quiet";
366 fastimport
->git_cmd
= 1;
367 return start_command(fastimport
);
370 static int get_exporter(struct transport
*transport
,
371 struct child_process
*fastexport
,
372 const char *export_marks
,
373 const char *import_marks
,
374 struct string_list
*revlist_args
)
376 struct child_process
*helper
= get_helper(transport
);
378 memset(fastexport
, 0, sizeof(*fastexport
));
380 /* we need to duplicate helper->in because we want to use it after
381 * fastexport is done with it. */
382 fastexport
->out
= dup(helper
->in
);
383 fastexport
->argv
= xcalloc(5 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
384 fastexport
->argv
[argc
++] = "fast-export";
385 fastexport
->argv
[argc
++] = "--use-done-feature";
387 fastexport
->argv
[argc
++] = export_marks
;
389 fastexport
->argv
[argc
++] = import_marks
;
391 for (i
= 0; i
< revlist_args
->nr
; i
++)
392 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
394 fastexport
->git_cmd
= 1;
395 return start_command(fastexport
);
398 static int fetch_with_import(struct transport
*transport
,
399 int nr_heads
, struct ref
**to_fetch
)
401 struct child_process fastimport
;
402 struct helper_data
*data
= transport
->data
;
405 struct strbuf buf
= STRBUF_INIT
;
407 get_helper(transport
);
409 if (get_importer(transport
, &fastimport
))
410 die("Couldn't run fast-import");
412 for (i
= 0; i
< nr_heads
; i
++) {
414 if (posn
->status
& REF_STATUS_UPTODATE
)
417 strbuf_addf(&buf
, "import %s\n", posn
->name
);
418 sendline(data
, &buf
);
422 write_constant(data
->helper
->in
, "\n");
424 if (finish_command(&fastimport
))
425 die("Error while running fast-import");
426 free(fastimport
.argv
);
427 fastimport
.argv
= NULL
;
429 for (i
= 0; i
< nr_heads
; i
++) {
432 if (posn
->status
& REF_STATUS_UPTODATE
)
435 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
437 private = strdup(posn
->name
);
438 read_ref(private, posn
->old_sha1
);
441 strbuf_release(&buf
);
445 static int process_connect_service(struct transport
*transport
,
446 const char *name
, const char *exec
)
448 struct helper_data
*data
= transport
->data
;
449 struct strbuf cmdbuf
= STRBUF_INIT
;
450 struct child_process
*helper
;
451 int r
, duped
, ret
= 0;
454 helper
= get_helper(transport
);
457 * Yes, dup the pipe another time, as we need unbuffered version
458 * of input pipe as FILE*. fclose() closes the underlying fd and
459 * stream buffering only can be changed before first I/O operation
462 duped
= dup(helper
->out
);
464 die_errno("Can't dup helper output fd");
465 input
= xfdopen(duped
, "r");
466 setvbuf(input
, NULL
, _IONBF
, 0);
469 * Handle --upload-pack and friends. This is fire and forget...
470 * just warn if it fails.
472 if (strcmp(name
, exec
)) {
473 r
= set_helper_option(transport
, "servpath", exec
);
475 warning("Setting remote service path not supported by protocol.");
477 warning("Invalid remote service path.");
481 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
485 sendline(data
, &cmdbuf
);
486 recvline_fh(input
, &cmdbuf
);
487 if (!strcmp(cmdbuf
.buf
, "")) {
488 data
->no_disconnect_req
= 1;
490 fprintf(stderr
, "Debug: Smart transport connection "
493 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
495 fprintf(stderr
, "Debug: Falling back to dumb "
498 die("Unknown response to connect: %s",
506 static int process_connect(struct transport
*transport
,
509 struct helper_data
*data
= transport
->data
;
513 name
= for_push
? "git-receive-pack" : "git-upload-pack";
515 exec
= data
->transport_options
.receivepack
;
517 exec
= data
->transport_options
.uploadpack
;
519 return process_connect_service(transport
, name
, exec
);
522 static int connect_helper(struct transport
*transport
, const char *name
,
523 const char *exec
, int fd
[2])
525 struct helper_data
*data
= transport
->data
;
527 /* Get_helper so connect is inited. */
528 get_helper(transport
);
530 die("Operation not supported by protocol.");
532 if (!process_connect_service(transport
, name
, exec
))
533 die("Can't connect to subservice %s.", name
);
535 fd
[0] = data
->helper
->out
;
536 fd
[1] = data
->helper
->in
;
540 static int fetch(struct transport
*transport
,
541 int nr_heads
, struct ref
**to_fetch
)
543 struct helper_data
*data
= transport
->data
;
546 if (process_connect(transport
, 0)) {
547 do_take_over(transport
);
548 return transport
->fetch(transport
, nr_heads
, to_fetch
);
552 for (i
= 0; i
< nr_heads
; i
++)
553 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
560 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
563 return fetch_with_import(transport
, nr_heads
, to_fetch
);
568 static void push_update_ref_status(struct strbuf
*buf
,
570 struct ref
*remote_refs
)
575 if (!prefixcmp(buf
->buf
, "ok ")) {
576 status
= REF_STATUS_OK
;
577 refname
= buf
->buf
+ 3;
578 } else if (!prefixcmp(buf
->buf
, "error ")) {
579 status
= REF_STATUS_REMOTE_REJECT
;
580 refname
= buf
->buf
+ 6;
582 die("expected ok/error, helper said '%s'\n", buf
->buf
);
584 msg
= strchr(refname
, ' ');
586 struct strbuf msg_buf
= STRBUF_INIT
;
590 if (!unquote_c_style(&msg_buf
, msg
, &end
))
591 msg
= strbuf_detach(&msg_buf
, NULL
);
594 strbuf_release(&msg_buf
);
596 if (!strcmp(msg
, "no match")) {
597 status
= REF_STATUS_NONE
;
601 else if (!strcmp(msg
, "up to date")) {
602 status
= REF_STATUS_UPTODATE
;
606 else if (!strcmp(msg
, "non-fast forward")) {
607 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
614 *ref
= find_ref_by_name(*ref
, refname
);
616 *ref
= find_ref_by_name(remote_refs
, refname
);
618 warning("helper reported unexpected status of %s", refname
);
622 if ((*ref
)->status
!= REF_STATUS_NONE
) {
624 * Earlier, the ref was marked not to be pushed, so ignore the ref
625 * status reported by the remote helper if the latter is 'no match'.
627 if (status
== REF_STATUS_NONE
)
631 (*ref
)->status
= status
;
632 (*ref
)->remote_status
= msg
;
635 static void push_update_refs_status(struct helper_data
*data
,
636 struct ref
*remote_refs
)
638 struct strbuf buf
= STRBUF_INIT
;
639 struct ref
*ref
= remote_refs
;
641 recvline(data
, &buf
);
645 push_update_ref_status(&buf
, &ref
, remote_refs
);
647 strbuf_release(&buf
);
650 static int push_refs_with_push(struct transport
*transport
,
651 struct ref
*remote_refs
, int flags
)
653 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
654 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
655 struct helper_data
*data
= transport
->data
;
656 struct strbuf buf
= STRBUF_INIT
;
659 get_helper(transport
);
663 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
664 if (!ref
->peer_ref
&& !mirror
)
667 /* Check for statuses set by set_ref_status_for_push() */
668 switch (ref
->status
) {
669 case REF_STATUS_REJECT_NONFASTFORWARD
:
670 case REF_STATUS_UPTODATE
:
679 strbuf_addstr(&buf
, "push ");
680 if (!ref
->deletion
) {
682 strbuf_addch(&buf
, '+');
684 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
686 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
688 strbuf_addch(&buf
, ':');
689 strbuf_addstr(&buf
, ref
->name
);
690 strbuf_addch(&buf
, '\n');
695 standard_options(transport
);
697 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
698 if (set_helper_option(transport
, "dry-run", "true") != 0)
699 die("helper %s does not support dry-run", data
->name
);
702 strbuf_addch(&buf
, '\n');
703 sendline(data
, &buf
);
704 strbuf_release(&buf
);
706 push_update_refs_status(data
, remote_refs
);
710 static int push_refs_with_export(struct transport
*transport
,
711 struct ref
*remote_refs
, int flags
)
714 struct child_process
*helper
, exporter
;
715 struct helper_data
*data
= transport
->data
;
716 char *export_marks
= NULL
, *import_marks
= NULL
;
717 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
718 struct strbuf buf
= STRBUF_INIT
;
720 helper
= get_helper(transport
);
722 write_constant(helper
->in
, "export\n");
724 recvline(data
, &buf
);
726 fprintf(stderr
, "Debug: Got export_marks '%s'\n", buf
.buf
);
728 struct strbuf arg
= STRBUF_INIT
;
729 strbuf_addstr(&arg
, "--export-marks=");
730 strbuf_addbuf(&arg
, &buf
);
731 export_marks
= strbuf_detach(&arg
, NULL
);
734 recvline(data
, &buf
);
736 fprintf(stderr
, "Debug: Got import_marks '%s'\n", buf
.buf
);
738 struct strbuf arg
= STRBUF_INIT
;
739 strbuf_addstr(&arg
, "--import-marks=");
740 strbuf_addbuf(&arg
, &buf
);
741 import_marks
= strbuf_detach(&arg
, NULL
);
746 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
748 unsigned char sha1
[20];
752 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
753 if (private && !get_sha1(private, sha1
)) {
754 strbuf_addf(&buf
, "^%s", private);
755 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
760 string_list_append(&revlist_args
, ref
->peer_ref
->name
);
764 if (get_exporter(transport
, &exporter
,
765 export_marks
, import_marks
, &revlist_args
))
766 die("Couldn't run fast-export");
768 if (finish_command(&exporter
))
769 die("Error while running fast-export");
770 push_update_refs_status(data
, remote_refs
);
774 static int push_refs(struct transport
*transport
,
775 struct ref
*remote_refs
, int flags
)
777 struct helper_data
*data
= transport
->data
;
779 if (process_connect(transport
, 1)) {
780 do_take_over(transport
);
781 return transport
->push_refs(transport
, remote_refs
, flags
);
785 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
786 "Perhaps you should specify a branch such as 'master'.\n");
791 return push_refs_with_push(transport
, remote_refs
, flags
);
794 return push_refs_with_export(transport
, remote_refs
, flags
);
800 static int has_attribute(const char *attrs
, const char *attr
) {
807 const char *space
= strchrnul(attrs
, ' ');
808 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
816 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
818 struct helper_data
*data
= transport
->data
;
819 struct child_process
*helper
;
820 struct ref
*ret
= NULL
;
821 struct ref
**tail
= &ret
;
823 struct strbuf buf
= STRBUF_INIT
;
825 helper
= get_helper(transport
);
827 if (process_connect(transport
, for_push
)) {
828 do_take_over(transport
);
829 return transport
->get_refs_list(transport
, for_push
);
832 if (data
->push
&& for_push
)
833 write_str_in_full(helper
->in
, "list for-push\n");
835 write_str_in_full(helper
->in
, "list\n");
839 recvline(data
, &buf
);
844 eov
= strchr(buf
.buf
, ' ');
846 die("Malformed response in ref list: %s", buf
.buf
);
847 eon
= strchr(eov
+ 1, ' ');
851 *tail
= alloc_ref(eov
+ 1);
852 if (buf
.buf
[0] == '@')
853 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
854 else if (buf
.buf
[0] != '?')
855 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
857 if (has_attribute(eon
+ 1, "unchanged")) {
858 (*tail
)->status
|= REF_STATUS_UPTODATE
;
859 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
862 tail
= &((*tail
)->next
);
865 fprintf(stderr
, "Debug: Read ref listing.\n");
866 strbuf_release(&buf
);
868 for (posn
= ret
; posn
; posn
= posn
->next
)
869 resolve_remote_symref(posn
, ret
);
874 int transport_helper_init(struct transport
*transport
, const char *name
)
876 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
879 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
882 transport
->data
= data
;
883 transport
->set_option
= set_helper_option
;
884 transport
->get_refs_list
= get_refs_list
;
885 transport
->fetch
= fetch
;
886 transport
->push_refs
= push_refs
;
887 transport
->disconnect
= release_helper
;
888 transport
->connect
= connect_helper
;
889 transport
->smart_options
= &(data
->transport_options
);
894 * Linux pipes can buffer 65536 bytes at once (and most platforms can
895 * buffer less), so attempt reads and writes with up to that size.
897 #define BUFFERSIZE 65536
898 /* This should be enough to hold debugging message. */
899 #define PBUFFERSIZE 8192
901 /* Print bidirectional transfer loop debug message. */
902 static void transfer_debug(const char *fmt
, ...)
905 char msgbuf
[PBUFFERSIZE
];
906 static int debug_enabled
= -1;
908 if (debug_enabled
< 0)
909 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
914 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
916 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
919 /* Stream state: More data may be coming in this direction. */
920 #define SSTATE_TRANSFERING 0
922 * Stream state: No more data coming in this direction, flushing rest of
925 #define SSTATE_FLUSHING 1
926 /* Stream state: Transfer in this direction finished. */
927 #define SSTATE_FINISHED 2
929 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
930 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
931 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
933 /* Unidirectional transfer. */
934 struct unidirectional_transfer
{
939 /* Is source socket? */
941 /* Is destination socket? */
943 /* Transfer state (TRANSFERING/FLUSHING/FINISHED) */
946 char buf
[BUFFERSIZE
];
949 /* Name of source. */
950 const char *src_name
;
951 /* Name of destination. */
952 const char *dest_name
;
955 /* Closes the target (for writing) if transfer has finished. */
956 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
958 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
959 t
->state
= SSTATE_FINISHED
;
961 shutdown(t
->dest
, SHUT_WR
);
964 transfer_debug("Closed %s.", t
->dest_name
);
969 * Tries to read read data from source into buffer. If buffer is full,
970 * no data is read. Returns 0 on success, -1 on error.
972 static int udt_do_read(struct unidirectional_transfer
*t
)
976 if (t
->bufuse
== BUFFERSIZE
)
977 return 0; /* No space for more. */
979 transfer_debug("%s is readable", t
->src_name
);
980 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
981 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
983 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
985 } else if (bytes
== 0) {
986 transfer_debug("%s EOF (with %i bytes in buffer)",
987 t
->src_name
, t
->bufuse
);
988 t
->state
= SSTATE_FLUSHING
;
989 } else if (bytes
> 0) {
991 transfer_debug("Read %i bytes from %s (buffer now at %i)",
992 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
997 /* Tries to write data from buffer into destination. If buffer is empty,
998 * no data is written. Returns 0 on success, -1 on error.
1000 static int udt_do_write(struct unidirectional_transfer
*t
)
1005 return 0; /* Nothing to write. */
1007 transfer_debug("%s is writable", t
->dest_name
);
1008 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
1009 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
1011 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
1013 } else if (bytes
> 0) {
1016 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
1017 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1018 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
1024 /* State of bidirectional transfer loop. */
1025 struct bidirectional_transfer_state
{
1026 /* Direction from program to git. */
1027 struct unidirectional_transfer ptg
;
1028 /* Direction from git to program. */
1029 struct unidirectional_transfer gtp
;
1032 static void *udt_copy_task_routine(void *udt
)
1034 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1035 while (t
->state
!= SSTATE_FINISHED
) {
1036 if (STATE_NEEDS_READING(t
->state
))
1039 if (STATE_NEEDS_WRITING(t
->state
))
1040 if (udt_do_write(t
))
1042 if (STATE_NEEDS_CLOSING(t
->state
))
1043 udt_close_if_finished(t
);
1045 return udt
; /* Just some non-NULL value. */
1051 * Join thread, with apporiate errors on failure. Name is name for the
1052 * thread (for error messages). Returns 0 on success, 1 on failure.
1054 static int tloop_join(pthread_t thread
, const char *name
)
1058 err
= pthread_join(thread
, &tret
);
1060 error("%s thread failed", name
);
1064 error("%s thread failed to join: %s", name
, strerror(err
));
1071 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1074 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1076 pthread_t gtp_thread
;
1077 pthread_t ptg_thread
;
1080 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1083 die("Can't start thread for copying data: %s", strerror(err
));
1084 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1087 die("Can't start thread for copying data: %s", strerror(err
));
1089 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1090 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1095 /* Close the source and target (for writing) for transfer. */
1096 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1098 t
->state
= SSTATE_FINISHED
;
1100 * Socket read end left open isn't a disaster if nobody
1101 * attempts to read from it (mingw compat headers do not
1104 * We can't fully close the socket since otherwise gtp
1105 * task would first close the socket it sends data to
1106 * while closing the ptg file descriptors.
1108 if (!t
->src_is_sock
)
1110 if (t
->dest_is_sock
)
1111 shutdown(t
->dest
, SHUT_WR
);
1117 * Join process, with apporiate errors on failure. Name is name for the
1118 * process (for error messages). Returns 0 on success, 1 on failure.
1120 static int tloop_join(pid_t pid
, const char *name
)
1123 if (waitpid(pid
, &tret
, 0) < 0) {
1124 error("%s process failed to wait: %s", name
, strerror(errno
));
1127 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1128 error("%s process failed", name
);
1135 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1138 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1143 /* Fork thread #1: git to program. */
1146 die_errno("Can't start thread for copying data");
1147 else if (pid1
== 0) {
1148 udt_kill_transfer(&s
->ptg
);
1149 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1152 /* Fork thread #2: program to git. */
1155 die_errno("Can't start thread for copying data");
1156 else if (pid2
== 0) {
1157 udt_kill_transfer(&s
->gtp
);
1158 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1162 * Close both streams in parent as to not interfere with
1163 * end of file detection and wait for both tasks to finish.
1165 udt_kill_transfer(&s
->gtp
);
1166 udt_kill_transfer(&s
->ptg
);
1167 ret
|= tloop_join(pid1
, "Git to program copy");
1168 ret
|= tloop_join(pid2
, "Program to git copy");
1174 * Copies data from stdin to output and from input to stdout simultaneously.
1175 * Additionally filtering through given filter. If filter is NULL, uses
1178 int bidirectional_transfer_loop(int input
, int output
)
1180 struct bidirectional_transfer_state state
;
1182 /* Fill the state fields. */
1183 state
.ptg
.src
= input
;
1185 state
.ptg
.src_is_sock
= (input
== output
);
1186 state
.ptg
.dest_is_sock
= 0;
1187 state
.ptg
.state
= SSTATE_TRANSFERING
;
1188 state
.ptg
.bufuse
= 0;
1189 state
.ptg
.src_name
= "remote input";
1190 state
.ptg
.dest_name
= "stdout";
1193 state
.gtp
.dest
= output
;
1194 state
.gtp
.src_is_sock
= 0;
1195 state
.gtp
.dest_is_sock
= (input
== output
);
1196 state
.gtp
.state
= SSTATE_TRANSFERING
;
1197 state
.gtp
.bufuse
= 0;
1198 state
.gtp
.src_name
= "stdin";
1199 state
.gtp
.dest_name
= "remote output";
1201 return tloop_spawnwait_tasks(&state
);