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