remote-curl: implement stateless-connect command
[git] / transport-helper.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "quote.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "remote.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
11 #include "sigchain.h"
12 #include "argv-array.h"
13 #include "refs.h"
14 #include "transport-internal.h"
15 #include "protocol.h"
16
17 static int debug;
18
19 struct helper_data {
20         const char *name;
21         struct child_process *helper;
22         FILE *out;
23         unsigned fetch : 1,
24                 import : 1,
25                 bidi_import : 1,
26                 export : 1,
27                 option : 1,
28                 push : 1,
29                 connect : 1,
30                 stateless_connect : 1,
31                 signed_tags : 1,
32                 check_connectivity : 1,
33                 no_disconnect_req : 1,
34                 no_private_update : 1;
35         char *export_marks;
36         char *import_marks;
37         /* These go from remote name (as in "list") to private name */
38         struct refspec *refspecs;
39         int refspec_nr;
40         /* Transport options for fetch-pack/send-pack (should one of
41          * those be invoked).
42          */
43         struct git_transport_options transport_options;
44 };
45
46 static void sendline(struct helper_data *helper, struct strbuf *buffer)
47 {
48         if (debug)
49                 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
50         if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
51                 die_errno("Full write to remote helper failed");
52 }
53
54 static int recvline_fh(FILE *helper, struct strbuf *buffer)
55 {
56         strbuf_reset(buffer);
57         if (debug)
58                 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
59         if (strbuf_getline(buffer, helper) == EOF) {
60                 if (debug)
61                         fprintf(stderr, "Debug: Remote helper quit.\n");
62                 return 1;
63         }
64
65         if (debug)
66                 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
67         return 0;
68 }
69
70 static int recvline(struct helper_data *helper, struct strbuf *buffer)
71 {
72         return recvline_fh(helper->out, buffer);
73 }
74
75 static void write_constant(int fd, const char *str)
76 {
77         if (debug)
78                 fprintf(stderr, "Debug: Remote helper: -> %s", str);
79         if (write_in_full(fd, str, strlen(str)) < 0)
80                 die_errno("Full write to remote helper failed");
81 }
82
83 static const char *remove_ext_force(const char *url)
84 {
85         if (url) {
86                 const char *colon = strchr(url, ':');
87                 if (colon && colon[1] == ':')
88                         return colon + 2;
89         }
90         return url;
91 }
92
93 static void do_take_over(struct transport *transport)
94 {
95         struct helper_data *data;
96         data = (struct helper_data *)transport->data;
97         transport_take_over(transport, data->helper);
98         fclose(data->out);
99         free(data);
100 }
101
102 static void standard_options(struct transport *t);
103
104 static struct child_process *get_helper(struct transport *transport)
105 {
106         struct helper_data *data = transport->data;
107         struct strbuf buf = STRBUF_INIT;
108         struct child_process *helper;
109         const char **refspecs = NULL;
110         int refspec_nr = 0;
111         int refspec_alloc = 0;
112         int duped;
113         int code;
114
115         if (data->helper)
116                 return data->helper;
117
118         helper = xmalloc(sizeof(*helper));
119         child_process_init(helper);
120         helper->in = -1;
121         helper->out = -1;
122         helper->err = 0;
123         argv_array_pushf(&helper->args, "git-remote-%s", data->name);
124         argv_array_push(&helper->args, transport->remote->name);
125         argv_array_push(&helper->args, remove_ext_force(transport->url));
126         helper->git_cmd = 0;
127         helper->silent_exec_failure = 1;
128
129         if (have_git_dir())
130                 argv_array_pushf(&helper->env_array, "%s=%s",
131                                  GIT_DIR_ENVIRONMENT, get_git_dir());
132
133         code = start_command(helper);
134         if (code < 0 && errno == ENOENT)
135                 die("Unable to find remote helper for '%s'", data->name);
136         else if (code != 0)
137                 exit(code);
138
139         data->helper = helper;
140         data->no_disconnect_req = 0;
141
142         /*
143          * Open the output as FILE* so strbuf_getline_*() family of
144          * functions can be used.
145          * Do this with duped fd because fclose() will close the fd,
146          * and stuff like taking over will require the fd to remain.
147          */
148         duped = dup(helper->out);
149         if (duped < 0)
150                 die_errno("Can't dup helper output fd");
151         data->out = xfdopen(duped, "r");
152
153         write_constant(helper->in, "capabilities\n");
154
155         while (1) {
156                 const char *capname, *arg;
157                 int mandatory = 0;
158                 if (recvline(data, &buf))
159                         exit(128);
160
161                 if (!*buf.buf)
162                         break;
163
164                 if (*buf.buf == '*') {
165                         capname = buf.buf + 1;
166                         mandatory = 1;
167                 } else
168                         capname = buf.buf;
169
170                 if (debug)
171                         fprintf(stderr, "Debug: Got cap %s\n", capname);
172                 if (!strcmp(capname, "fetch"))
173                         data->fetch = 1;
174                 else if (!strcmp(capname, "option"))
175                         data->option = 1;
176                 else if (!strcmp(capname, "push"))
177                         data->push = 1;
178                 else if (!strcmp(capname, "import"))
179                         data->import = 1;
180                 else if (!strcmp(capname, "bidi-import"))
181                         data->bidi_import = 1;
182                 else if (!strcmp(capname, "export"))
183                         data->export = 1;
184                 else if (!strcmp(capname, "check-connectivity"))
185                         data->check_connectivity = 1;
186                 else if (!data->refspecs && skip_prefix(capname, "refspec ", &arg)) {
187                         ALLOC_GROW(refspecs,
188                                    refspec_nr + 1,
189                                    refspec_alloc);
190                         refspecs[refspec_nr++] = xstrdup(arg);
191                 } else if (!strcmp(capname, "connect")) {
192                         data->connect = 1;
193                 } else if (!strcmp(capname, "stateless-connect")) {
194                         data->stateless_connect = 1;
195                 } else if (!strcmp(capname, "signed-tags")) {
196                         data->signed_tags = 1;
197                 } else if (skip_prefix(capname, "export-marks ", &arg)) {
198                         data->export_marks = xstrdup(arg);
199                 } else if (skip_prefix(capname, "import-marks ", &arg)) {
200                         data->import_marks = xstrdup(arg);
201                 } else if (starts_with(capname, "no-private-update")) {
202                         data->no_private_update = 1;
203                 } else if (mandatory) {
204                         die("Unknown mandatory capability %s. This remote "
205                             "helper probably needs newer version of Git.",
206                             capname);
207                 }
208         }
209         if (refspecs) {
210                 int i;
211                 data->refspec_nr = refspec_nr;
212                 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
213                 for (i = 0; i < refspec_nr; i++)
214                         free((char *)refspecs[i]);
215                 free(refspecs);
216         } else if (data->import || data->bidi_import || data->export) {
217                 warning("This remote helper should implement refspec capability.");
218         }
219         strbuf_release(&buf);
220         if (debug)
221                 fprintf(stderr, "Debug: Capabilities complete.\n");
222         standard_options(transport);
223         return data->helper;
224 }
225
226 static int disconnect_helper(struct transport *transport)
227 {
228         struct helper_data *data = transport->data;
229         int res = 0;
230
231         if (data->helper) {
232                 if (debug)
233                         fprintf(stderr, "Debug: Disconnecting.\n");
234                 if (!data->no_disconnect_req) {
235                         /*
236                          * Ignore write errors; there's nothing we can do,
237                          * since we're about to close the pipe anyway. And the
238                          * most likely error is EPIPE due to the helper dying
239                          * to report an error itself.
240                          */
241                         sigchain_push(SIGPIPE, SIG_IGN);
242                         xwrite(data->helper->in, "\n", 1);
243                         sigchain_pop(SIGPIPE);
244                 }
245                 close(data->helper->in);
246                 close(data->helper->out);
247                 fclose(data->out);
248                 res = finish_command(data->helper);
249                 FREE_AND_NULL(data->helper);
250         }
251         return res;
252 }
253
254 static const char *unsupported_options[] = {
255         TRANS_OPT_UPLOADPACK,
256         TRANS_OPT_RECEIVEPACK,
257         TRANS_OPT_THIN,
258         TRANS_OPT_KEEP
259         };
260
261 static const char *boolean_options[] = {
262         TRANS_OPT_THIN,
263         TRANS_OPT_KEEP,
264         TRANS_OPT_FOLLOWTAGS,
265         TRANS_OPT_DEEPEN_RELATIVE
266         };
267
268 static int strbuf_set_helper_option(struct helper_data *data,
269                                     struct strbuf *buf)
270 {
271         int ret;
272
273         sendline(data, buf);
274         if (recvline(data, buf))
275                 exit(128);
276
277         if (!strcmp(buf->buf, "ok"))
278                 ret = 0;
279         else if (starts_with(buf->buf, "error"))
280                 ret = -1;
281         else if (!strcmp(buf->buf, "unsupported"))
282                 ret = 1;
283         else {
284                 warning("%s unexpectedly said: '%s'", data->name, buf->buf);
285                 ret = 1;
286         }
287         return ret;
288 }
289
290 static int string_list_set_helper_option(struct helper_data *data,
291                                          const char *name,
292                                          struct string_list *list)
293 {
294         struct strbuf buf = STRBUF_INIT;
295         int i, ret = 0;
296
297         for (i = 0; i < list->nr; i++) {
298                 strbuf_addf(&buf, "option %s ", name);
299                 quote_c_style(list->items[i].string, &buf, NULL, 0);
300                 strbuf_addch(&buf, '\n');
301
302                 if ((ret = strbuf_set_helper_option(data, &buf)))
303                         break;
304                 strbuf_reset(&buf);
305         }
306         strbuf_release(&buf);
307         return ret;
308 }
309
310 static int set_helper_option(struct transport *transport,
311                           const char *name, const char *value)
312 {
313         struct helper_data *data = transport->data;
314         struct strbuf buf = STRBUF_INIT;
315         int i, ret, is_bool = 0;
316
317         get_helper(transport);
318
319         if (!data->option)
320                 return 1;
321
322         if (!strcmp(name, "deepen-not"))
323                 return string_list_set_helper_option(data, name,
324                                                      (struct string_list *)value);
325
326         for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
327                 if (!strcmp(name, unsupported_options[i]))
328                         return 1;
329         }
330
331         for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
332                 if (!strcmp(name, boolean_options[i])) {
333                         is_bool = 1;
334                         break;
335                 }
336         }
337
338         strbuf_addf(&buf, "option %s ", name);
339         if (is_bool)
340                 strbuf_addstr(&buf, value ? "true" : "false");
341         else
342                 quote_c_style(value, &buf, NULL, 0);
343         strbuf_addch(&buf, '\n');
344
345         ret = strbuf_set_helper_option(data, &buf);
346         strbuf_release(&buf);
347         return ret;
348 }
349
350 static void standard_options(struct transport *t)
351 {
352         char buf[16];
353         int v = t->verbose;
354
355         set_helper_option(t, "progress", t->progress ? "true" : "false");
356
357         xsnprintf(buf, sizeof(buf), "%d", v + 1);
358         set_helper_option(t, "verbosity", buf);
359
360         switch (t->family) {
361         case TRANSPORT_FAMILY_ALL:
362                 /*
363                  * this is already the default,
364                  * do not break old remote helpers by setting "all" here
365                  */
366                 break;
367         case TRANSPORT_FAMILY_IPV4:
368                 set_helper_option(t, "family", "ipv4");
369                 break;
370         case TRANSPORT_FAMILY_IPV6:
371                 set_helper_option(t, "family", "ipv6");
372                 break;
373         }
374 }
375
376 static int release_helper(struct transport *transport)
377 {
378         int res = 0;
379         struct helper_data *data = transport->data;
380         free_refspec(data->refspec_nr, data->refspecs);
381         data->refspecs = NULL;
382         res = disconnect_helper(transport);
383         free(transport->data);
384         return res;
385 }
386
387 static int fetch_with_fetch(struct transport *transport,
388                             int nr_heads, struct ref **to_fetch)
389 {
390         struct helper_data *data = transport->data;
391         int i;
392         struct strbuf buf = STRBUF_INIT;
393
394         for (i = 0; i < nr_heads; i++) {
395                 const struct ref *posn = to_fetch[i];
396                 if (posn->status & REF_STATUS_UPTODATE)
397                         continue;
398
399                 strbuf_addf(&buf, "fetch %s %s\n",
400                             oid_to_hex(&posn->old_oid),
401                             posn->symref ? posn->symref : posn->name);
402         }
403
404         strbuf_addch(&buf, '\n');
405         sendline(data, &buf);
406
407         while (1) {
408                 if (recvline(data, &buf))
409                         exit(128);
410
411                 if (starts_with(buf.buf, "lock ")) {
412                         const char *name = buf.buf + 5;
413                         if (transport->pack_lockfile)
414                                 warning("%s also locked %s", data->name, name);
415                         else
416                                 transport->pack_lockfile = xstrdup(name);
417                 }
418                 else if (data->check_connectivity &&
419                          data->transport_options.check_self_contained_and_connected &&
420                          !strcmp(buf.buf, "connectivity-ok"))
421                         data->transport_options.self_contained_and_connected = 1;
422                 else if (!buf.len)
423                         break;
424                 else
425                         warning("%s unexpectedly said: '%s'", data->name, buf.buf);
426         }
427         strbuf_release(&buf);
428         return 0;
429 }
430
431 static int get_importer(struct transport *transport, struct child_process *fastimport)
432 {
433         struct child_process *helper = get_helper(transport);
434         struct helper_data *data = transport->data;
435         int cat_blob_fd, code;
436         child_process_init(fastimport);
437         fastimport->in = helper->out;
438         argv_array_push(&fastimport->args, "fast-import");
439         argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
440
441         if (data->bidi_import) {
442                 cat_blob_fd = xdup(helper->in);
443                 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
444         }
445         fastimport->git_cmd = 1;
446
447         code = start_command(fastimport);
448         return code;
449 }
450
451 static int get_exporter(struct transport *transport,
452                         struct child_process *fastexport,
453                         struct string_list *revlist_args)
454 {
455         struct helper_data *data = transport->data;
456         struct child_process *helper = get_helper(transport);
457         int i;
458
459         child_process_init(fastexport);
460
461         /* we need to duplicate helper->in because we want to use it after
462          * fastexport is done with it. */
463         fastexport->out = dup(helper->in);
464         argv_array_push(&fastexport->args, "fast-export");
465         argv_array_push(&fastexport->args, "--use-done-feature");
466         argv_array_push(&fastexport->args, data->signed_tags ?
467                 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
468         if (data->export_marks)
469                 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
470         if (data->import_marks)
471                 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
472
473         for (i = 0; i < revlist_args->nr; i++)
474                 argv_array_push(&fastexport->args, revlist_args->items[i].string);
475
476         fastexport->git_cmd = 1;
477         return start_command(fastexport);
478 }
479
480 static int fetch_with_import(struct transport *transport,
481                              int nr_heads, struct ref **to_fetch)
482 {
483         struct child_process fastimport;
484         struct helper_data *data = transport->data;
485         int i;
486         struct ref *posn;
487         struct strbuf buf = STRBUF_INIT;
488
489         get_helper(transport);
490
491         if (get_importer(transport, &fastimport))
492                 die("Couldn't run fast-import");
493
494         for (i = 0; i < nr_heads; i++) {
495                 posn = to_fetch[i];
496                 if (posn->status & REF_STATUS_UPTODATE)
497                         continue;
498
499                 strbuf_addf(&buf, "import %s\n",
500                             posn->symref ? posn->symref : posn->name);
501                 sendline(data, &buf);
502                 strbuf_reset(&buf);
503         }
504
505         write_constant(data->helper->in, "\n");
506         /*
507          * remote-helpers that advertise the bidi-import capability are required to
508          * buffer the complete batch of import commands until this newline before
509          * sending data to fast-import.
510          * These helpers read back data from fast-import on their stdin, which could
511          * be mixed with import commands, otherwise.
512          */
513
514         if (finish_command(&fastimport))
515                 die("Error while running fast-import");
516
517         /*
518          * The fast-import stream of a remote helper that advertises
519          * the "refspec" capability writes to the refs named after the
520          * right hand side of the first refspec matching each ref we
521          * were fetching.
522          *
523          * (If no "refspec" capability was specified, for historical
524          * reasons we default to the equivalent of *:*.)
525          *
526          * Store the result in to_fetch[i].old_sha1.  Callers such
527          * as "git fetch" can use the value to write feedback to the
528          * terminal, populate FETCH_HEAD, and determine what new value
529          * should be written to peer_ref if the update is a
530          * fast-forward or this is a forced update.
531          */
532         for (i = 0; i < nr_heads; i++) {
533                 char *private, *name;
534                 posn = to_fetch[i];
535                 if (posn->status & REF_STATUS_UPTODATE)
536                         continue;
537                 name = posn->symref ? posn->symref : posn->name;
538                 if (data->refspecs)
539                         private = apply_refspecs(data->refspecs, data->refspec_nr, name);
540                 else
541                         private = xstrdup(name);
542                 if (private) {
543                         if (read_ref(private, &posn->old_oid) < 0)
544                                 die("Could not read ref %s", private);
545                         free(private);
546                 }
547         }
548         strbuf_release(&buf);
549         return 0;
550 }
551
552 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
553 {
554         struct helper_data *data = transport->data;
555         int ret = 0;
556         int duped;
557         FILE *input;
558         struct child_process *helper;
559
560         helper = get_helper(transport);
561
562         /*
563          * Yes, dup the pipe another time, as we need unbuffered version
564          * of input pipe as FILE*. fclose() closes the underlying fd and
565          * stream buffering only can be changed before first I/O operation
566          * on it.
567          */
568         duped = dup(helper->out);
569         if (duped < 0)
570                 die_errno("Can't dup helper output fd");
571         input = xfdopen(duped, "r");
572         setvbuf(input, NULL, _IONBF, 0);
573
574         sendline(data, cmdbuf);
575         if (recvline_fh(input, cmdbuf))
576                 exit(128);
577
578         if (!strcmp(cmdbuf->buf, "")) {
579                 data->no_disconnect_req = 1;
580                 if (debug)
581                         fprintf(stderr, "Debug: Smart transport connection "
582                                 "ready.\n");
583                 ret = 1;
584         } else if (!strcmp(cmdbuf->buf, "fallback")) {
585                 if (debug)
586                         fprintf(stderr, "Debug: Falling back to dumb "
587                                 "transport.\n");
588         } else {
589                 die("Unknown response to connect: %s",
590                         cmdbuf->buf);
591         }
592
593         fclose(input);
594         return ret;
595 }
596
597 static int process_connect_service(struct transport *transport,
598                                    const char *name, const char *exec)
599 {
600         struct helper_data *data = transport->data;
601         struct strbuf cmdbuf = STRBUF_INIT;
602         int ret = 0;
603
604         /*
605          * Handle --upload-pack and friends. This is fire and forget...
606          * just warn if it fails.
607          */
608         if (strcmp(name, exec)) {
609                 int r = set_helper_option(transport, "servpath", exec);
610                 if (r > 0)
611                         warning("Setting remote service path not supported by protocol.");
612                 else if (r < 0)
613                         warning("Invalid remote service path.");
614         }
615
616         if (data->connect) {
617                 strbuf_addf(&cmdbuf, "connect %s\n", name);
618                 ret = run_connect(transport, &cmdbuf);
619         } else if (data->stateless_connect &&
620                    (get_protocol_version_config() == protocol_v2) &&
621                    !strcmp("git-upload-pack", name)) {
622                 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
623                 ret = run_connect(transport, &cmdbuf);
624                 if (ret)
625                         transport->stateless_rpc = 1;
626         }
627
628         strbuf_release(&cmdbuf);
629         return ret;
630 }
631
632 static int process_connect(struct transport *transport,
633                                      int for_push)
634 {
635         struct helper_data *data = transport->data;
636         const char *name;
637         const char *exec;
638
639         name = for_push ? "git-receive-pack" : "git-upload-pack";
640         if (for_push)
641                 exec = data->transport_options.receivepack;
642         else
643                 exec = data->transport_options.uploadpack;
644
645         return process_connect_service(transport, name, exec);
646 }
647
648 static int connect_helper(struct transport *transport, const char *name,
649                    const char *exec, int fd[2])
650 {
651         struct helper_data *data = transport->data;
652
653         /* Get_helper so connect is inited. */
654         get_helper(transport);
655         if (!data->connect)
656                 die("Operation not supported by protocol.");
657
658         if (!process_connect_service(transport, name, exec))
659                 die("Can't connect to subservice %s.", name);
660
661         fd[0] = data->helper->out;
662         fd[1] = data->helper->in;
663         return 0;
664 }
665
666 static int fetch(struct transport *transport,
667                  int nr_heads, struct ref **to_fetch)
668 {
669         struct helper_data *data = transport->data;
670         int i, count;
671
672         if (process_connect(transport, 0)) {
673                 do_take_over(transport);
674                 return transport->vtable->fetch(transport, nr_heads, to_fetch);
675         }
676
677         count = 0;
678         for (i = 0; i < nr_heads; i++)
679                 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
680                         count++;
681
682         if (!count)
683                 return 0;
684
685         if (data->check_connectivity &&
686             data->transport_options.check_self_contained_and_connected)
687                 set_helper_option(transport, "check-connectivity", "true");
688
689         if (transport->cloning)
690                 set_helper_option(transport, "cloning", "true");
691
692         if (data->transport_options.update_shallow)
693                 set_helper_option(transport, "update-shallow", "true");
694
695         if (data->fetch)
696                 return fetch_with_fetch(transport, nr_heads, to_fetch);
697
698         if (data->import)
699                 return fetch_with_import(transport, nr_heads, to_fetch);
700
701         return -1;
702 }
703
704 static int push_update_ref_status(struct strbuf *buf,
705                                    struct ref **ref,
706                                    struct ref *remote_refs)
707 {
708         char *refname, *msg;
709         int status, forced = 0;
710
711         if (starts_with(buf->buf, "ok ")) {
712                 status = REF_STATUS_OK;
713                 refname = buf->buf + 3;
714         } else if (starts_with(buf->buf, "error ")) {
715                 status = REF_STATUS_REMOTE_REJECT;
716                 refname = buf->buf + 6;
717         } else
718                 die("expected ok/error, helper said '%s'", buf->buf);
719
720         msg = strchr(refname, ' ');
721         if (msg) {
722                 struct strbuf msg_buf = STRBUF_INIT;
723                 const char *end;
724
725                 *msg++ = '\0';
726                 if (!unquote_c_style(&msg_buf, msg, &end))
727                         msg = strbuf_detach(&msg_buf, NULL);
728                 else
729                         msg = xstrdup(msg);
730                 strbuf_release(&msg_buf);
731
732                 if (!strcmp(msg, "no match")) {
733                         status = REF_STATUS_NONE;
734                         FREE_AND_NULL(msg);
735                 }
736                 else if (!strcmp(msg, "up to date")) {
737                         status = REF_STATUS_UPTODATE;
738                         FREE_AND_NULL(msg);
739                 }
740                 else if (!strcmp(msg, "non-fast forward")) {
741                         status = REF_STATUS_REJECT_NONFASTFORWARD;
742                         FREE_AND_NULL(msg);
743                 }
744                 else if (!strcmp(msg, "already exists")) {
745                         status = REF_STATUS_REJECT_ALREADY_EXISTS;
746                         FREE_AND_NULL(msg);
747                 }
748                 else if (!strcmp(msg, "fetch first")) {
749                         status = REF_STATUS_REJECT_FETCH_FIRST;
750                         FREE_AND_NULL(msg);
751                 }
752                 else if (!strcmp(msg, "needs force")) {
753                         status = REF_STATUS_REJECT_NEEDS_FORCE;
754                         FREE_AND_NULL(msg);
755                 }
756                 else if (!strcmp(msg, "stale info")) {
757                         status = REF_STATUS_REJECT_STALE;
758                         FREE_AND_NULL(msg);
759                 }
760                 else if (!strcmp(msg, "forced update")) {
761                         forced = 1;
762                         FREE_AND_NULL(msg);
763                 }
764         }
765
766         if (*ref)
767                 *ref = find_ref_by_name(*ref, refname);
768         if (!*ref)
769                 *ref = find_ref_by_name(remote_refs, refname);
770         if (!*ref) {
771                 warning("helper reported unexpected status of %s", refname);
772                 return 1;
773         }
774
775         if ((*ref)->status != REF_STATUS_NONE) {
776                 /*
777                  * Earlier, the ref was marked not to be pushed, so ignore the ref
778                  * status reported by the remote helper if the latter is 'no match'.
779                  */
780                 if (status == REF_STATUS_NONE)
781                         return 1;
782         }
783
784         (*ref)->status = status;
785         (*ref)->forced_update |= forced;
786         (*ref)->remote_status = msg;
787         return !(status == REF_STATUS_OK);
788 }
789
790 static int push_update_refs_status(struct helper_data *data,
791                                     struct ref *remote_refs,
792                                     int flags)
793 {
794         struct strbuf buf = STRBUF_INIT;
795         struct ref *ref = remote_refs;
796         int ret = 0;
797
798         for (;;) {
799                 char *private;
800
801                 if (recvline(data, &buf)) {
802                         ret = 1;
803                         break;
804                 }
805
806                 if (!buf.len)
807                         break;
808
809                 if (push_update_ref_status(&buf, &ref, remote_refs))
810                         continue;
811
812                 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->refspecs || data->no_private_update)
813                         continue;
814
815                 /* propagate back the update to the remote namespace */
816                 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
817                 if (!private)
818                         continue;
819                 update_ref("update by helper", private, &ref->new_oid, NULL,
820                            0, 0);
821                 free(private);
822         }
823         strbuf_release(&buf);
824         return ret;
825 }
826
827 static void set_common_push_options(struct transport *transport,
828                                    const char *name, int flags)
829 {
830         if (flags & TRANSPORT_PUSH_DRY_RUN) {
831                 if (set_helper_option(transport, "dry-run", "true") != 0)
832                         die("helper %s does not support dry-run", name);
833         } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
834                 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
835                         die("helper %s does not support --signed", name);
836         } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
837                 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
838                         die("helper %s does not support --signed=if-asked", name);
839         }
840
841         if (flags & TRANSPORT_PUSH_OPTIONS) {
842                 struct string_list_item *item;
843                 for_each_string_list_item(item, transport->push_options)
844                         if (set_helper_option(transport, "push-option", item->string) != 0)
845                                 die("helper %s does not support 'push-option'", name);
846         }
847 }
848
849 static int push_refs_with_push(struct transport *transport,
850                                struct ref *remote_refs, int flags)
851 {
852         int force_all = flags & TRANSPORT_PUSH_FORCE;
853         int mirror = flags & TRANSPORT_PUSH_MIRROR;
854         struct helper_data *data = transport->data;
855         struct strbuf buf = STRBUF_INIT;
856         struct ref *ref;
857         struct string_list cas_options = STRING_LIST_INIT_DUP;
858         struct string_list_item *cas_option;
859
860         get_helper(transport);
861         if (!data->push)
862                 return 1;
863
864         for (ref = remote_refs; ref; ref = ref->next) {
865                 if (!ref->peer_ref && !mirror)
866                         continue;
867
868                 /* Check for statuses set by set_ref_status_for_push() */
869                 switch (ref->status) {
870                 case REF_STATUS_REJECT_NONFASTFORWARD:
871                 case REF_STATUS_REJECT_STALE:
872                 case REF_STATUS_REJECT_ALREADY_EXISTS:
873                 case REF_STATUS_UPTODATE:
874                         continue;
875                 default:
876                         ; /* do nothing */
877                 }
878
879                 if (force_all)
880                         ref->force = 1;
881
882                 strbuf_addstr(&buf, "push ");
883                 if (!ref->deletion) {
884                         if (ref->force)
885                                 strbuf_addch(&buf, '+');
886                         if (ref->peer_ref)
887                                 strbuf_addstr(&buf, ref->peer_ref->name);
888                         else
889                                 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
890                 }
891                 strbuf_addch(&buf, ':');
892                 strbuf_addstr(&buf, ref->name);
893                 strbuf_addch(&buf, '\n');
894
895                 /*
896                  * The "--force-with-lease" options without explicit
897                  * values to expect have already been expanded into
898                  * the ref->old_oid_expect[] field; we can ignore
899                  * transport->smart_options->cas altogether and instead
900                  * can enumerate them from the refs.
901                  */
902                 if (ref->expect_old_sha1) {
903                         struct strbuf cas = STRBUF_INIT;
904                         strbuf_addf(&cas, "%s:%s",
905                                     ref->name, oid_to_hex(&ref->old_oid_expect));
906                         string_list_append_nodup(&cas_options,
907                                                  strbuf_detach(&cas, NULL));
908                 }
909         }
910         if (buf.len == 0) {
911                 string_list_clear(&cas_options, 0);
912                 return 0;
913         }
914
915         for_each_string_list_item(cas_option, &cas_options)
916                 set_helper_option(transport, "cas", cas_option->string);
917         set_common_push_options(transport, data->name, flags);
918
919         strbuf_addch(&buf, '\n');
920         sendline(data, &buf);
921         strbuf_release(&buf);
922         string_list_clear(&cas_options, 0);
923
924         return push_update_refs_status(data, remote_refs, flags);
925 }
926
927 static int push_refs_with_export(struct transport *transport,
928                 struct ref *remote_refs, int flags)
929 {
930         struct ref *ref;
931         struct child_process *helper, exporter;
932         struct helper_data *data = transport->data;
933         struct string_list revlist_args = STRING_LIST_INIT_DUP;
934         struct strbuf buf = STRBUF_INIT;
935
936         if (!data->refspecs)
937                 die("remote-helper doesn't support push; refspec needed");
938
939         set_common_push_options(transport, data->name, flags);
940         if (flags & TRANSPORT_PUSH_FORCE) {
941                 if (set_helper_option(transport, "force", "true") != 0)
942                         warning("helper %s does not support 'force'", data->name);
943         }
944
945         helper = get_helper(transport);
946
947         write_constant(helper->in, "export\n");
948
949         for (ref = remote_refs; ref; ref = ref->next) {
950                 char *private;
951                 struct object_id oid;
952
953                 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
954                 if (private && !get_oid(private, &oid)) {
955                         strbuf_addf(&buf, "^%s", private);
956                         string_list_append_nodup(&revlist_args,
957                                                  strbuf_detach(&buf, NULL));
958                         oidcpy(&ref->old_oid, &oid);
959                 }
960                 free(private);
961
962                 if (ref->peer_ref) {
963                         if (strcmp(ref->name, ref->peer_ref->name)) {
964                                 if (!ref->deletion) {
965                                         const char *name;
966                                         int flag;
967
968                                         /* Follow symbolic refs (mainly for HEAD). */
969                                         name = resolve_ref_unsafe(ref->peer_ref->name,
970                                                                   RESOLVE_REF_READING,
971                                                                   &oid, &flag);
972                                         if (!name || !(flag & REF_ISSYMREF))
973                                                 name = ref->peer_ref->name;
974
975                                         strbuf_addf(&buf, "%s:%s", name, ref->name);
976                                 } else
977                                         strbuf_addf(&buf, ":%s", ref->name);
978
979                                 string_list_append(&revlist_args, "--refspec");
980                                 string_list_append(&revlist_args, buf.buf);
981                                 strbuf_release(&buf);
982                         }
983                         if (!ref->deletion)
984                                 string_list_append(&revlist_args, ref->peer_ref->name);
985                 }
986         }
987
988         if (get_exporter(transport, &exporter, &revlist_args))
989                 die("Couldn't run fast-export");
990
991         string_list_clear(&revlist_args, 1);
992
993         if (finish_command(&exporter))
994                 die("Error while running fast-export");
995         if (push_update_refs_status(data, remote_refs, flags))
996                 return 1;
997
998         if (data->export_marks) {
999                 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1000                 rename(buf.buf, data->export_marks);
1001                 strbuf_release(&buf);
1002         }
1003
1004         return 0;
1005 }
1006
1007 static int push_refs(struct transport *transport,
1008                 struct ref *remote_refs, int flags)
1009 {
1010         struct helper_data *data = transport->data;
1011
1012         if (process_connect(transport, 1)) {
1013                 do_take_over(transport);
1014                 return transport->vtable->push_refs(transport, remote_refs, flags);
1015         }
1016
1017         if (!remote_refs) {
1018                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
1019                         "Perhaps you should specify a branch such as 'master'.\n");
1020                 return 0;
1021         }
1022
1023         if (data->push)
1024                 return push_refs_with_push(transport, remote_refs, flags);
1025
1026         if (data->export)
1027                 return push_refs_with_export(transport, remote_refs, flags);
1028
1029         return -1;
1030 }
1031
1032
1033 static int has_attribute(const char *attrs, const char *attr) {
1034         int len;
1035         if (!attrs)
1036                 return 0;
1037
1038         len = strlen(attr);
1039         for (;;) {
1040                 const char *space = strchrnul(attrs, ' ');
1041                 if (len == space - attrs && !strncmp(attrs, attr, len))
1042                         return 1;
1043                 if (!*space)
1044                         return 0;
1045                 attrs = space + 1;
1046         }
1047 }
1048
1049 static struct ref *get_refs_list(struct transport *transport, int for_push,
1050                                  const struct argv_array *ref_prefixes)
1051 {
1052         struct helper_data *data = transport->data;
1053         struct child_process *helper;
1054         struct ref *ret = NULL;
1055         struct ref **tail = &ret;
1056         struct ref *posn;
1057         struct strbuf buf = STRBUF_INIT;
1058
1059         helper = get_helper(transport);
1060
1061         if (process_connect(transport, for_push)) {
1062                 do_take_over(transport);
1063                 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1064         }
1065
1066         if (data->push && for_push)
1067                 write_str_in_full(helper->in, "list for-push\n");
1068         else
1069                 write_str_in_full(helper->in, "list\n");
1070
1071         while (1) {
1072                 char *eov, *eon;
1073                 if (recvline(data, &buf))
1074                         exit(128);
1075
1076                 if (!*buf.buf)
1077                         break;
1078
1079                 eov = strchr(buf.buf, ' ');
1080                 if (!eov)
1081                         die("Malformed response in ref list: %s", buf.buf);
1082                 eon = strchr(eov + 1, ' ');
1083                 *eov = '\0';
1084                 if (eon)
1085                         *eon = '\0';
1086                 *tail = alloc_ref(eov + 1);
1087                 if (buf.buf[0] == '@')
1088                         (*tail)->symref = xstrdup(buf.buf + 1);
1089                 else if (buf.buf[0] != '?')
1090                         get_oid_hex(buf.buf, &(*tail)->old_oid);
1091                 if (eon) {
1092                         if (has_attribute(eon + 1, "unchanged")) {
1093                                 (*tail)->status |= REF_STATUS_UPTODATE;
1094                                 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1095                                         die(_("Could not read ref %s"),
1096                                             (*tail)->name);
1097                         }
1098                 }
1099                 tail = &((*tail)->next);
1100         }
1101         if (debug)
1102                 fprintf(stderr, "Debug: Read ref listing.\n");
1103         strbuf_release(&buf);
1104
1105         for (posn = ret; posn; posn = posn->next)
1106                 resolve_remote_symref(posn, ret);
1107
1108         return ret;
1109 }
1110
1111 static struct transport_vtable vtable = {
1112         set_helper_option,
1113         get_refs_list,
1114         fetch,
1115         push_refs,
1116         connect_helper,
1117         release_helper
1118 };
1119
1120 int transport_helper_init(struct transport *transport, const char *name)
1121 {
1122         struct helper_data *data = xcalloc(1, sizeof(*data));
1123         data->name = name;
1124
1125         transport_check_allowed(name);
1126
1127         if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1128                 debug = 1;
1129
1130         transport->data = data;
1131         transport->vtable = &vtable;
1132         transport->smart_options = &(data->transport_options);
1133         return 0;
1134 }
1135
1136 /*
1137  * Linux pipes can buffer 65536 bytes at once (and most platforms can
1138  * buffer less), so attempt reads and writes with up to that size.
1139  */
1140 #define BUFFERSIZE 65536
1141 /* This should be enough to hold debugging message. */
1142 #define PBUFFERSIZE 8192
1143
1144 /* Print bidirectional transfer loop debug message. */
1145 __attribute__((format (printf, 1, 2)))
1146 static void transfer_debug(const char *fmt, ...)
1147 {
1148         /*
1149          * NEEDSWORK: This function is sometimes used from multiple threads, and
1150          * we end up using debug_enabled racily. That "should not matter" since
1151          * we always write the same value, but it's still wrong. This function
1152          * is listed in .tsan-suppressions for the time being.
1153          */
1154
1155         va_list args;
1156         char msgbuf[PBUFFERSIZE];
1157         static int debug_enabled = -1;
1158
1159         if (debug_enabled < 0)
1160                 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1161         if (!debug_enabled)
1162                 return;
1163
1164         va_start(args, fmt);
1165         vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1166         va_end(args);
1167         fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1168 }
1169
1170 /* Stream state: More data may be coming in this direction. */
1171 #define SSTATE_TRANSFERRING 0
1172 /*
1173  * Stream state: No more data coming in this direction, flushing rest of
1174  * data.
1175  */
1176 #define SSTATE_FLUSHING 1
1177 /* Stream state: Transfer in this direction finished. */
1178 #define SSTATE_FINISHED 2
1179
1180 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1181 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1182 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1183
1184 /* Unidirectional transfer. */
1185 struct unidirectional_transfer {
1186         /* Source */
1187         int src;
1188         /* Destination */
1189         int dest;
1190         /* Is source socket? */
1191         int src_is_sock;
1192         /* Is destination socket? */
1193         int dest_is_sock;
1194         /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1195         int state;
1196         /* Buffer. */
1197         char buf[BUFFERSIZE];
1198         /* Buffer used. */
1199         size_t bufuse;
1200         /* Name of source. */
1201         const char *src_name;
1202         /* Name of destination. */
1203         const char *dest_name;
1204 };
1205
1206 /* Closes the target (for writing) if transfer has finished. */
1207 static void udt_close_if_finished(struct unidirectional_transfer *t)
1208 {
1209         if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1210                 t->state = SSTATE_FINISHED;
1211                 if (t->dest_is_sock)
1212                         shutdown(t->dest, SHUT_WR);
1213                 else
1214                         close(t->dest);
1215                 transfer_debug("Closed %s.", t->dest_name);
1216         }
1217 }
1218
1219 /*
1220  * Tries to read data from source into buffer. If buffer is full,
1221  * no data is read. Returns 0 on success, -1 on error.
1222  */
1223 static int udt_do_read(struct unidirectional_transfer *t)
1224 {
1225         ssize_t bytes;
1226
1227         if (t->bufuse == BUFFERSIZE)
1228                 return 0;       /* No space for more. */
1229
1230         transfer_debug("%s is readable", t->src_name);
1231         bytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1232         if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
1233                 errno != EINTR) {
1234                 error_errno("read(%s) failed", t->src_name);
1235                 return -1;
1236         } else if (bytes == 0) {
1237                 transfer_debug("%s EOF (with %i bytes in buffer)",
1238                         t->src_name, (int)t->bufuse);
1239                 t->state = SSTATE_FLUSHING;
1240         } else if (bytes > 0) {
1241                 t->bufuse += bytes;
1242                 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1243                         (int)bytes, t->src_name, (int)t->bufuse);
1244         }
1245         return 0;
1246 }
1247
1248 /* Tries to write data from buffer into destination. If buffer is empty,
1249  * no data is written. Returns 0 on success, -1 on error.
1250  */
1251 static int udt_do_write(struct unidirectional_transfer *t)
1252 {
1253         ssize_t bytes;
1254
1255         if (t->bufuse == 0)
1256                 return 0;       /* Nothing to write. */
1257
1258         transfer_debug("%s is writable", t->dest_name);
1259         bytes = xwrite(t->dest, t->buf, t->bufuse);
1260         if (bytes < 0 && errno != EWOULDBLOCK) {
1261                 error_errno("write(%s) failed", t->dest_name);
1262                 return -1;
1263         } else if (bytes > 0) {
1264                 t->bufuse -= bytes;
1265                 if (t->bufuse)
1266                         memmove(t->buf, t->buf + bytes, t->bufuse);
1267                 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1268                         (int)bytes, t->dest_name, (int)t->bufuse);
1269         }
1270         return 0;
1271 }
1272
1273
1274 /* State of bidirectional transfer loop. */
1275 struct bidirectional_transfer_state {
1276         /* Direction from program to git. */
1277         struct unidirectional_transfer ptg;
1278         /* Direction from git to program. */
1279         struct unidirectional_transfer gtp;
1280 };
1281
1282 static void *udt_copy_task_routine(void *udt)
1283 {
1284         struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1285         while (t->state != SSTATE_FINISHED) {
1286                 if (STATE_NEEDS_READING(t->state))
1287                         if (udt_do_read(t))
1288                                 return NULL;
1289                 if (STATE_NEEDS_WRITING(t->state))
1290                         if (udt_do_write(t))
1291                                 return NULL;
1292                 if (STATE_NEEDS_CLOSING(t->state))
1293                         udt_close_if_finished(t);
1294         }
1295         return udt;     /* Just some non-NULL value. */
1296 }
1297
1298 #ifndef NO_PTHREADS
1299
1300 /*
1301  * Join thread, with appropriate errors on failure. Name is name for the
1302  * thread (for error messages). Returns 0 on success, 1 on failure.
1303  */
1304 static int tloop_join(pthread_t thread, const char *name)
1305 {
1306         int err;
1307         void *tret;
1308         err = pthread_join(thread, &tret);
1309         if (!tret) {
1310                 error("%s thread failed", name);
1311                 return 1;
1312         }
1313         if (err) {
1314                 error("%s thread failed to join: %s", name, strerror(err));
1315                 return 1;
1316         }
1317         return 0;
1318 }
1319
1320 /*
1321  * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1322  * -1 on failure.
1323  */
1324 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1325 {
1326         pthread_t gtp_thread;
1327         pthread_t ptg_thread;
1328         int err;
1329         int ret = 0;
1330         err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1331                 &s->gtp);
1332         if (err)
1333                 die("Can't start thread for copying data: %s", strerror(err));
1334         err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1335                 &s->ptg);
1336         if (err)
1337                 die("Can't start thread for copying data: %s", strerror(err));
1338
1339         ret |= tloop_join(gtp_thread, "Git to program copy");
1340         ret |= tloop_join(ptg_thread, "Program to git copy");
1341         return ret;
1342 }
1343 #else
1344
1345 /* Close the source and target (for writing) for transfer. */
1346 static void udt_kill_transfer(struct unidirectional_transfer *t)
1347 {
1348         t->state = SSTATE_FINISHED;
1349         /*
1350          * Socket read end left open isn't a disaster if nobody
1351          * attempts to read from it (mingw compat headers do not
1352          * have SHUT_RD)...
1353          *
1354          * We can't fully close the socket since otherwise gtp
1355          * task would first close the socket it sends data to
1356          * while closing the ptg file descriptors.
1357          */
1358         if (!t->src_is_sock)
1359                 close(t->src);
1360         if (t->dest_is_sock)
1361                 shutdown(t->dest, SHUT_WR);
1362         else
1363                 close(t->dest);
1364 }
1365
1366 /*
1367  * Join process, with appropriate errors on failure. Name is name for the
1368  * process (for error messages). Returns 0 on success, 1 on failure.
1369  */
1370 static int tloop_join(pid_t pid, const char *name)
1371 {
1372         int tret;
1373         if (waitpid(pid, &tret, 0) < 0) {
1374                 error_errno("%s process failed to wait", name);
1375                 return 1;
1376         }
1377         if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1378                 error("%s process failed", name);
1379                 return 1;
1380         }
1381         return 0;
1382 }
1383
1384 /*
1385  * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1386  * -1 on failure.
1387  */
1388 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1389 {
1390         pid_t pid1, pid2;
1391         int ret = 0;
1392
1393         /* Fork thread #1: git to program. */
1394         pid1 = fork();
1395         if (pid1 < 0)
1396                 die_errno("Can't start thread for copying data");
1397         else if (pid1 == 0) {
1398                 udt_kill_transfer(&s->ptg);
1399                 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1400         }
1401
1402         /* Fork thread #2: program to git. */
1403         pid2 = fork();
1404         if (pid2 < 0)
1405                 die_errno("Can't start thread for copying data");
1406         else if (pid2 == 0) {
1407                 udt_kill_transfer(&s->gtp);
1408                 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1409         }
1410
1411         /*
1412          * Close both streams in parent as to not interfere with
1413          * end of file detection and wait for both tasks to finish.
1414          */
1415         udt_kill_transfer(&s->gtp);
1416         udt_kill_transfer(&s->ptg);
1417         ret |= tloop_join(pid1, "Git to program copy");
1418         ret |= tloop_join(pid2, "Program to git copy");
1419         return ret;
1420 }
1421 #endif
1422
1423 /*
1424  * Copies data from stdin to output and from input to stdout simultaneously.
1425  * Additionally filtering through given filter. If filter is NULL, uses
1426  * identity filter.
1427  */
1428 int bidirectional_transfer_loop(int input, int output)
1429 {
1430         struct bidirectional_transfer_state state;
1431
1432         /* Fill the state fields. */
1433         state.ptg.src = input;
1434         state.ptg.dest = 1;
1435         state.ptg.src_is_sock = (input == output);
1436         state.ptg.dest_is_sock = 0;
1437         state.ptg.state = SSTATE_TRANSFERRING;
1438         state.ptg.bufuse = 0;
1439         state.ptg.src_name = "remote input";
1440         state.ptg.dest_name = "stdout";
1441
1442         state.gtp.src = 0;
1443         state.gtp.dest = output;
1444         state.gtp.src_is_sock = 0;
1445         state.gtp.dest_is_sock = (input == output);
1446         state.gtp.state = SSTATE_TRANSFERRING;
1447         state.gtp.bufuse = 0;
1448         state.gtp.src_name = "stdin";
1449         state.gtp.dest_name = "remote output";
1450
1451         return tloop_spawnwait_tasks(&state);
1452 }