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 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;
112 helper
= xcalloc(1, sizeof(*helper
));
116 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
117 strbuf_addf(&buf
, "git-remote-%s", data
->name
);
118 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
119 helper
->argv
[1] = transport
->remote
->name
;
120 helper
->argv
[2] = remove_ext_force(transport
->url
);
122 helper
->silent_exec_failure
= 1;
123 code
= start_command(helper
);
124 if (code
< 0 && errno
== ENOENT
)
125 die("Unable to find remote helper for '%s'", data
->name
);
129 data
->helper
= helper
;
130 data
->no_disconnect_req
= 0;
133 * Open the output as FILE* so strbuf_getline() can be used.
134 * Do this with duped fd because fclose() will close the fd,
135 * and stuff like taking over will require the fd to remain.
137 duped
= dup(helper
->out
);
139 die_errno("Can't dup helper output fd");
140 data
->out
= xfdopen(duped
, "r");
142 write_constant(helper
->in
, "capabilities\n");
147 recvline(data
, &buf
);
152 if (*buf
.buf
== '*') {
153 capname
= buf
.buf
+ 1;
159 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
160 if (!strcmp(capname
, "fetch"))
162 else if (!strcmp(capname
, "option"))
164 else if (!strcmp(capname
, "push"))
166 else if (!strcmp(capname
, "import"))
168 else if (!strcmp(capname
, "export"))
170 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
174 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
175 } else if (!strcmp(capname
, "connect")) {
177 } else if (!strcmp(buf
.buf
, "gitdir")) {
178 struct strbuf gitdir
= STRBUF_INIT
;
179 strbuf_addf(&gitdir
, "gitdir %s\n", get_git_dir());
180 sendline(data
, &gitdir
);
181 strbuf_release(&gitdir
);
182 } else if (mandatory
) {
183 die("Unknown mandatory capability %s. This remote "
184 "helper probably needs newer version of Git.\n",
190 data
->refspec_nr
= refspec_nr
;
191 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
192 for (i
= 0; i
< refspec_nr
; i
++) {
193 free((char *)refspecs
[i
]);
197 strbuf_release(&buf
);
199 fprintf(stderr
, "Debug: Capabilities complete.\n");
203 static int disconnect_helper(struct transport
*transport
)
205 struct helper_data
*data
= transport
->data
;
206 struct strbuf buf
= STRBUF_INIT
;
210 fprintf(stderr
, "Debug: Disconnecting.\n");
211 if (!data
->no_disconnect_req
) {
212 strbuf_addf(&buf
, "\n");
213 sendline(data
, &buf
);
215 close(data
->helper
->in
);
216 close(data
->helper
->out
);
218 finish_command(data
->helper
);
219 free((char *)data
->helper
->argv
[0]);
220 free(data
->helper
->argv
);
227 static const char *unsupported_options
[] = {
228 TRANS_OPT_UPLOADPACK
,
229 TRANS_OPT_RECEIVEPACK
,
233 static const char *boolean_options
[] = {
239 static int set_helper_option(struct transport
*transport
,
240 const char *name
, const char *value
)
242 struct helper_data
*data
= transport
->data
;
243 struct strbuf buf
= STRBUF_INIT
;
244 int i
, ret
, is_bool
= 0;
246 get_helper(transport
);
251 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
252 if (!strcmp(name
, unsupported_options
[i
]))
256 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
257 if (!strcmp(name
, boolean_options
[i
])) {
263 strbuf_addf(&buf
, "option %s ", name
);
265 strbuf_addstr(&buf
, value
? "true" : "false");
267 quote_c_style(value
, &buf
, NULL
, 0);
268 strbuf_addch(&buf
, '\n');
270 xchgline(data
, &buf
);
272 if (!strcmp(buf
.buf
, "ok"))
274 else if (!prefixcmp(buf
.buf
, "error")) {
276 } else if (!strcmp(buf
.buf
, "unsupported"))
279 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
282 strbuf_release(&buf
);
286 static void standard_options(struct transport
*t
)
292 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
294 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
295 if (n
>= sizeof(buf
))
296 die("impossibly large verbosity value");
297 set_helper_option(t
, "verbosity", buf
);
300 static int release_helper(struct transport
*transport
)
302 struct helper_data
*data
= transport
->data
;
303 free_refspec(data
->refspec_nr
, data
->refspecs
);
304 data
->refspecs
= NULL
;
305 disconnect_helper(transport
);
306 free(transport
->data
);
310 static int fetch_with_fetch(struct transport
*transport
,
311 int nr_heads
, struct ref
**to_fetch
)
313 struct helper_data
*data
= transport
->data
;
315 struct strbuf buf
= STRBUF_INIT
;
317 standard_options(transport
);
319 for (i
= 0; i
< nr_heads
; i
++) {
320 const struct ref
*posn
= to_fetch
[i
];
321 if (posn
->status
& REF_STATUS_UPTODATE
)
324 strbuf_addf(&buf
, "fetch %s %s\n",
325 sha1_to_hex(posn
->old_sha1
), posn
->name
);
328 strbuf_addch(&buf
, '\n');
329 sendline(data
, &buf
);
332 recvline(data
, &buf
);
334 if (!prefixcmp(buf
.buf
, "lock ")) {
335 const char *name
= buf
.buf
+ 5;
336 if (transport
->pack_lockfile
)
337 warning("%s also locked %s", data
->name
, name
);
339 transport
->pack_lockfile
= xstrdup(name
);
344 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
346 strbuf_release(&buf
);
350 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
352 struct child_process
*helper
= get_helper(transport
);
353 memset(fastimport
, 0, sizeof(*fastimport
));
354 fastimport
->in
= helper
->out
;
355 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
356 fastimport
->argv
[0] = "fast-import";
357 fastimport
->argv
[1] = "--quiet";
359 fastimport
->git_cmd
= 1;
360 return start_command(fastimport
);
363 static int get_exporter(struct transport
*transport
,
364 struct child_process
*fastexport
,
365 const char *export_marks
,
366 const char *import_marks
,
367 struct string_list
*revlist_args
)
369 struct child_process
*helper
= get_helper(transport
);
371 memset(fastexport
, 0, sizeof(*fastexport
));
373 /* we need to duplicate helper->in because we want to use it after
374 * fastexport is done with it. */
375 fastexport
->out
= dup(helper
->in
);
376 fastexport
->argv
= xcalloc(4 + revlist_args
->nr
, sizeof(*fastexport
->argv
));
377 fastexport
->argv
[argc
++] = "fast-export";
379 fastexport
->argv
[argc
++] = export_marks
;
381 fastexport
->argv
[argc
++] = import_marks
;
383 for (i
= 0; i
< revlist_args
->nr
; i
++)
384 fastexport
->argv
[argc
++] = revlist_args
->items
[i
].string
;
386 fastexport
->git_cmd
= 1;
387 return start_command(fastexport
);
390 static int fetch_with_import(struct transport
*transport
,
391 int nr_heads
, struct ref
**to_fetch
)
393 struct child_process fastimport
;
394 struct helper_data
*data
= transport
->data
;
397 struct strbuf buf
= STRBUF_INIT
;
399 get_helper(transport
);
401 if (get_importer(transport
, &fastimport
))
402 die("Couldn't run fast-import");
404 for (i
= 0; i
< nr_heads
; i
++) {
406 if (posn
->status
& REF_STATUS_UPTODATE
)
409 strbuf_addf(&buf
, "import %s\n", posn
->name
);
410 sendline(data
, &buf
);
413 disconnect_helper(transport
);
414 finish_command(&fastimport
);
415 free(fastimport
.argv
);
416 fastimport
.argv
= NULL
;
418 for (i
= 0; i
< nr_heads
; i
++) {
421 if (posn
->status
& REF_STATUS_UPTODATE
)
424 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
426 private = strdup(posn
->name
);
427 read_ref(private, posn
->old_sha1
);
430 strbuf_release(&buf
);
434 static int process_connect_service(struct transport
*transport
,
435 const char *name
, const char *exec
)
437 struct helper_data
*data
= transport
->data
;
438 struct strbuf cmdbuf
= STRBUF_INIT
;
439 struct child_process
*helper
;
440 int r
, duped
, ret
= 0;
443 helper
= get_helper(transport
);
446 * Yes, dup the pipe another time, as we need unbuffered version
447 * of input pipe as FILE*. fclose() closes the underlying fd and
448 * stream buffering only can be changed before first I/O operation
451 duped
= dup(helper
->out
);
453 die_errno("Can't dup helper output fd");
454 input
= xfdopen(duped
, "r");
455 setvbuf(input
, NULL
, _IONBF
, 0);
458 * Handle --upload-pack and friends. This is fire and forget...
459 * just warn if it fails.
461 if (strcmp(name
, exec
)) {
462 r
= set_helper_option(transport
, "servpath", exec
);
464 warning("Setting remote service path not supported by protocol.");
466 warning("Invalid remote service path.");
470 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
474 sendline(data
, &cmdbuf
);
475 recvline_fh(input
, &cmdbuf
);
476 if (!strcmp(cmdbuf
.buf
, "")) {
477 data
->no_disconnect_req
= 1;
479 fprintf(stderr
, "Debug: Smart transport connection "
482 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
484 fprintf(stderr
, "Debug: Falling back to dumb "
487 die("Unknown response to connect: %s",
495 static int process_connect(struct transport
*transport
,
498 struct helper_data
*data
= transport
->data
;
502 name
= for_push
? "git-receive-pack" : "git-upload-pack";
504 exec
= data
->transport_options
.receivepack
;
506 exec
= data
->transport_options
.uploadpack
;
508 return process_connect_service(transport
, name
, exec
);
511 static int connect_helper(struct transport
*transport
, const char *name
,
512 const char *exec
, int fd
[2])
514 struct helper_data
*data
= transport
->data
;
516 /* Get_helper so connect is inited. */
517 get_helper(transport
);
519 die("Operation not supported by protocol.");
521 if (!process_connect_service(transport
, name
, exec
))
522 die("Can't connect to subservice %s.", name
);
524 fd
[0] = data
->helper
->out
;
525 fd
[1] = data
->helper
->in
;
529 static int fetch(struct transport
*transport
,
530 int nr_heads
, struct ref
**to_fetch
)
532 struct helper_data
*data
= transport
->data
;
535 if (process_connect(transport
, 0)) {
536 do_take_over(transport
);
537 return transport
->fetch(transport
, nr_heads
, to_fetch
);
541 for (i
= 0; i
< nr_heads
; i
++)
542 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
549 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
552 return fetch_with_import(transport
, nr_heads
, to_fetch
);
557 static int push_refs_with_push(struct transport
*transport
,
558 struct ref
*remote_refs
, int flags
)
560 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
561 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
562 struct helper_data
*data
= transport
->data
;
563 struct strbuf buf
= STRBUF_INIT
;
564 struct child_process
*helper
;
567 helper
= get_helper(transport
);
571 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
572 if (!ref
->peer_ref
&& !mirror
)
575 /* Check for statuses set by set_ref_status_for_push() */
576 switch (ref
->status
) {
577 case REF_STATUS_REJECT_NONFASTFORWARD
:
578 case REF_STATUS_UPTODATE
:
587 strbuf_addstr(&buf
, "push ");
588 if (!ref
->deletion
) {
590 strbuf_addch(&buf
, '+');
592 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
594 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
596 strbuf_addch(&buf
, ':');
597 strbuf_addstr(&buf
, ref
->name
);
598 strbuf_addch(&buf
, '\n');
603 standard_options(transport
);
605 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
606 if (set_helper_option(transport
, "dry-run", "true") != 0)
607 die("helper %s does not support dry-run", data
->name
);
610 strbuf_addch(&buf
, '\n');
611 sendline(data
, &buf
);
618 recvline(data
, &buf
);
622 if (!prefixcmp(buf
.buf
, "ok ")) {
623 status
= REF_STATUS_OK
;
624 refname
= buf
.buf
+ 3;
625 } else if (!prefixcmp(buf
.buf
, "error ")) {
626 status
= REF_STATUS_REMOTE_REJECT
;
627 refname
= buf
.buf
+ 6;
629 die("expected ok/error, helper said '%s'\n", buf
.buf
);
631 msg
= strchr(refname
, ' ');
633 struct strbuf msg_buf
= STRBUF_INIT
;
637 if (!unquote_c_style(&msg_buf
, msg
, &end
))
638 msg
= strbuf_detach(&msg_buf
, NULL
);
641 strbuf_release(&msg_buf
);
643 if (!strcmp(msg
, "no match")) {
644 status
= REF_STATUS_NONE
;
648 else if (!strcmp(msg
, "up to date")) {
649 status
= REF_STATUS_UPTODATE
;
653 else if (!strcmp(msg
, "non-fast forward")) {
654 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
661 ref
= find_ref_by_name(ref
, refname
);
663 ref
= find_ref_by_name(remote_refs
, refname
);
665 warning("helper reported unexpected status of %s", refname
);
669 if (ref
->status
!= REF_STATUS_NONE
) {
671 * Earlier, the ref was marked not to be pushed, so ignore the ref
672 * status reported by the remote helper if the latter is 'no match'.
674 if (status
== REF_STATUS_NONE
)
678 ref
->status
= status
;
679 ref
->remote_status
= msg
;
681 strbuf_release(&buf
);
685 static int push_refs_with_export(struct transport
*transport
,
686 struct ref
*remote_refs
, int flags
)
689 struct child_process
*helper
, exporter
;
690 struct helper_data
*data
= transport
->data
;
691 char *export_marks
= NULL
, *import_marks
= NULL
;
692 struct string_list revlist_args
= STRING_LIST_INIT_NODUP
;
693 struct strbuf buf
= STRBUF_INIT
;
695 helper
= get_helper(transport
);
697 write_constant(helper
->in
, "export\n");
699 recvline(data
, &buf
);
701 fprintf(stderr
, "Debug: Got export_marks '%s'\n", buf
.buf
);
703 struct strbuf arg
= STRBUF_INIT
;
704 strbuf_addstr(&arg
, "--export-marks=");
705 strbuf_addbuf(&arg
, &buf
);
706 export_marks
= strbuf_detach(&arg
, NULL
);
709 recvline(data
, &buf
);
711 fprintf(stderr
, "Debug: Got import_marks '%s'\n", buf
.buf
);
713 struct strbuf arg
= STRBUF_INIT
;
714 strbuf_addstr(&arg
, "--import-marks=");
715 strbuf_addbuf(&arg
, &buf
);
716 import_marks
= strbuf_detach(&arg
, NULL
);
721 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
723 unsigned char sha1
[20];
727 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, ref
->name
);
728 if (private && !get_sha1(private, sha1
)) {
729 strbuf_addf(&buf
, "^%s", private);
730 string_list_append(&revlist_args
, strbuf_detach(&buf
, NULL
));
733 string_list_append(&revlist_args
, ref
->name
);
737 if (get_exporter(transport
, &exporter
,
738 export_marks
, import_marks
, &revlist_args
))
739 die("Couldn't run fast-export");
741 data
->no_disconnect_req
= 1;
742 finish_command(&exporter
);
743 disconnect_helper(transport
);
747 static int push_refs(struct transport
*transport
,
748 struct ref
*remote_refs
, int flags
)
750 struct helper_data
*data
= transport
->data
;
752 if (process_connect(transport
, 1)) {
753 do_take_over(transport
);
754 return transport
->push_refs(transport
, remote_refs
, flags
);
758 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
759 "Perhaps you should specify a branch such as 'master'.\n");
764 return push_refs_with_push(transport
, remote_refs
, flags
);
767 return push_refs_with_export(transport
, remote_refs
, flags
);
773 static int has_attribute(const char *attrs
, const char *attr
) {
780 const char *space
= strchrnul(attrs
, ' ');
781 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
789 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
791 struct helper_data
*data
= transport
->data
;
792 struct child_process
*helper
;
793 struct ref
*ret
= NULL
;
794 struct ref
**tail
= &ret
;
796 struct strbuf buf
= STRBUF_INIT
;
798 helper
= get_helper(transport
);
800 if (process_connect(transport
, for_push
)) {
801 do_take_over(transport
);
802 return transport
->get_refs_list(transport
, for_push
);
805 if (data
->push
&& for_push
)
806 write_str_in_full(helper
->in
, "list for-push\n");
808 write_str_in_full(helper
->in
, "list\n");
812 recvline(data
, &buf
);
817 eov
= strchr(buf
.buf
, ' ');
819 die("Malformed response in ref list: %s", buf
.buf
);
820 eon
= strchr(eov
+ 1, ' ');
824 *tail
= alloc_ref(eov
+ 1);
825 if (buf
.buf
[0] == '@')
826 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
827 else if (buf
.buf
[0] != '?')
828 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
830 if (has_attribute(eon
+ 1, "unchanged")) {
831 (*tail
)->status
|= REF_STATUS_UPTODATE
;
832 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
835 tail
= &((*tail
)->next
);
838 fprintf(stderr
, "Debug: Read ref listing.\n");
839 strbuf_release(&buf
);
841 for (posn
= ret
; posn
; posn
= posn
->next
)
842 resolve_remote_symref(posn
, ret
);
847 int transport_helper_init(struct transport
*transport
, const char *name
)
849 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
852 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
855 transport
->data
= data
;
856 transport
->set_option
= set_helper_option
;
857 transport
->get_refs_list
= get_refs_list
;
858 transport
->fetch
= fetch
;
859 transport
->push_refs
= push_refs
;
860 transport
->disconnect
= release_helper
;
861 transport
->connect
= connect_helper
;
862 transport
->smart_options
= &(data
->transport_options
);
867 * Linux pipes can buffer 65536 bytes at once (and most platforms can
868 * buffer less), so attempt reads and writes with up to that size.
870 #define BUFFERSIZE 65536
871 /* This should be enough to hold debugging message. */
872 #define PBUFFERSIZE 8192
874 /* Print bidirectional transfer loop debug message. */
875 static void transfer_debug(const char *fmt
, ...)
878 char msgbuf
[PBUFFERSIZE
];
879 static int debug_enabled
= -1;
881 if (debug_enabled
< 0)
882 debug_enabled
= getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
887 vsnprintf(msgbuf
, PBUFFERSIZE
, fmt
, args
);
889 fprintf(stderr
, "Transfer loop debugging: %s\n", msgbuf
);
892 /* Stream state: More data may be coming in this direction. */
893 #define SSTATE_TRANSFERING 0
895 * Stream state: No more data coming in this direction, flushing rest of
898 #define SSTATE_FLUSHING 1
899 /* Stream state: Transfer in this direction finished. */
900 #define SSTATE_FINISHED 2
902 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
903 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
904 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
906 /* Unidirectional transfer. */
907 struct unidirectional_transfer
{
912 /* Is source socket? */
914 /* Is destination socket? */
916 /* Transfer state (TRANSFERING/FLUSHING/FINISHED) */
919 char buf
[BUFFERSIZE
];
922 /* Name of source. */
923 const char *src_name
;
924 /* Name of destination. */
925 const char *dest_name
;
928 /* Closes the target (for writing) if transfer has finished. */
929 static void udt_close_if_finished(struct unidirectional_transfer
*t
)
931 if (STATE_NEEDS_CLOSING(t
->state
) && !t
->bufuse
) {
932 t
->state
= SSTATE_FINISHED
;
934 shutdown(t
->dest
, SHUT_WR
);
937 transfer_debug("Closed %s.", t
->dest_name
);
942 * Tries to read read data from source into buffer. If buffer is full,
943 * no data is read. Returns 0 on success, -1 on error.
945 static int udt_do_read(struct unidirectional_transfer
*t
)
949 if (t
->bufuse
== BUFFERSIZE
)
950 return 0; /* No space for more. */
952 transfer_debug("%s is readable", t
->src_name
);
953 bytes
= read(t
->src
, t
->buf
+ t
->bufuse
, BUFFERSIZE
- t
->bufuse
);
954 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
956 error("read(%s) failed: %s", t
->src_name
, strerror(errno
));
958 } else if (bytes
== 0) {
959 transfer_debug("%s EOF (with %i bytes in buffer)",
960 t
->src_name
, t
->bufuse
);
961 t
->state
= SSTATE_FLUSHING
;
962 } else if (bytes
> 0) {
964 transfer_debug("Read %i bytes from %s (buffer now at %i)",
965 (int)bytes
, t
->src_name
, (int)t
->bufuse
);
970 /* Tries to write data from buffer into destination. If buffer is empty,
971 * no data is written. Returns 0 on success, -1 on error.
973 static int udt_do_write(struct unidirectional_transfer
*t
)
978 return 0; /* Nothing to write. */
980 transfer_debug("%s is writable", t
->dest_name
);
981 bytes
= write(t
->dest
, t
->buf
, t
->bufuse
);
982 if (bytes
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
&&
984 error("write(%s) failed: %s", t
->dest_name
, strerror(errno
));
986 } else if (bytes
> 0) {
989 memmove(t
->buf
, t
->buf
+ bytes
, t
->bufuse
);
990 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
991 (int)bytes
, t
->dest_name
, (int)t
->bufuse
);
997 /* State of bidirectional transfer loop. */
998 struct bidirectional_transfer_state
{
999 /* Direction from program to git. */
1000 struct unidirectional_transfer ptg
;
1001 /* Direction from git to program. */
1002 struct unidirectional_transfer gtp
;
1005 static void *udt_copy_task_routine(void *udt
)
1007 struct unidirectional_transfer
*t
= (struct unidirectional_transfer
*)udt
;
1008 while (t
->state
!= SSTATE_FINISHED
) {
1009 if (STATE_NEEDS_READING(t
->state
))
1012 if (STATE_NEEDS_WRITING(t
->state
))
1013 if (udt_do_write(t
))
1015 if (STATE_NEEDS_CLOSING(t
->state
))
1016 udt_close_if_finished(t
);
1018 return udt
; /* Just some non-NULL value. */
1024 * Join thread, with apporiate errors on failure. Name is name for the
1025 * thread (for error messages). Returns 0 on success, 1 on failure.
1027 static int tloop_join(pthread_t thread
, const char *name
)
1031 err
= pthread_join(thread
, &tret
);
1033 error("%s thread failed", name
);
1037 error("%s thread failed to join: %s", name
, strerror(err
));
1044 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1047 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1049 pthread_t gtp_thread
;
1050 pthread_t ptg_thread
;
1053 err
= pthread_create(>p_thread
, NULL
, udt_copy_task_routine
,
1056 die("Can't start thread for copying data: %s", strerror(err
));
1057 err
= pthread_create(&ptg_thread
, NULL
, udt_copy_task_routine
,
1060 die("Can't start thread for copying data: %s", strerror(err
));
1062 ret
|= tloop_join(gtp_thread
, "Git to program copy");
1063 ret
|= tloop_join(ptg_thread
, "Program to git copy");
1068 /* Close the source and target (for writing) for transfer. */
1069 static void udt_kill_transfer(struct unidirectional_transfer
*t
)
1071 t
->state
= SSTATE_FINISHED
;
1073 * Socket read end left open isn't a disaster if nobody
1074 * attempts to read from it (mingw compat headers do not
1077 * We can't fully close the socket since otherwise gtp
1078 * task would first close the socket it sends data to
1079 * while closing the ptg file descriptors.
1081 if (!t
->src_is_sock
)
1083 if (t
->dest_is_sock
)
1084 shutdown(t
->dest
, SHUT_WR
);
1090 * Join process, with apporiate errors on failure. Name is name for the
1091 * process (for error messages). Returns 0 on success, 1 on failure.
1093 static int tloop_join(pid_t pid
, const char *name
)
1096 if (waitpid(pid
, &tret
, 0) < 0) {
1097 error("%s process failed to wait: %s", name
, strerror(errno
));
1100 if (!WIFEXITED(tret
) || WEXITSTATUS(tret
)) {
1101 error("%s process failed", name
);
1108 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1111 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state
*s
)
1116 /* Fork thread #1: git to program. */
1119 die_errno("Can't start thread for copying data");
1120 else if (pid1
== 0) {
1121 udt_kill_transfer(&s
->ptg
);
1122 exit(udt_copy_task_routine(&s
->gtp
) ? 0 : 1);
1125 /* Fork thread #2: program to git. */
1128 die_errno("Can't start thread for copying data");
1129 else if (pid2
== 0) {
1130 udt_kill_transfer(&s
->gtp
);
1131 exit(udt_copy_task_routine(&s
->ptg
) ? 0 : 1);
1135 * Close both streams in parent as to not interfere with
1136 * end of file detection and wait for both tasks to finish.
1138 udt_kill_transfer(&s
->gtp
);
1139 udt_kill_transfer(&s
->ptg
);
1140 ret
|= tloop_join(pid1
, "Git to program copy");
1141 ret
|= tloop_join(pid2
, "Program to git copy");
1147 * Copies data from stdin to output and from input to stdout simultaneously.
1148 * Additionally filtering through given filter. If filter is NULL, uses
1151 int bidirectional_transfer_loop(int input
, int output
)
1153 struct bidirectional_transfer_state state
;
1155 /* Fill the state fields. */
1156 state
.ptg
.src
= input
;
1158 state
.ptg
.src_is_sock
= (input
== output
);
1159 state
.ptg
.dest_is_sock
= 0;
1160 state
.ptg
.state
= SSTATE_TRANSFERING
;
1161 state
.ptg
.bufuse
= 0;
1162 state
.ptg
.src_name
= "remote input";
1163 state
.ptg
.dest_name
= "stdout";
1166 state
.gtp
.dest
= output
;
1167 state
.gtp
.src_is_sock
= 0;
1168 state
.gtp
.dest_is_sock
= (input
== output
);
1169 state
.gtp
.state
= SSTATE_TRANSFERING
;
1170 state
.gtp
.bufuse
= 0;
1171 state
.gtp
.src_name
= "stdin";
1172 state
.gtp
.dest_name
= "remote output";
1174 return tloop_spawnwait_tasks(&state
);