builtin / upload-archive.con commit test-lib: introduce test_commit_bulk (b1c36cb)
   1/*
   2 * Copyright (c) 2006 Franck Bui-Huu
   3 */
   4#include "cache.h"
   5#include "builtin.h"
   6#include "archive.h"
   7#include "pkt-line.h"
   8#include "sideband.h"
   9#include "run-command.h"
  10#include "argv-array.h"
  11
  12static const char upload_archive_usage[] =
  13        "git upload-archive <repo>";
  14
  15static const char deadchild[] =
  16"git upload-archive: archiver died with error";
  17
  18#define MAX_ARGS (64)
  19
  20int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
  21{
  22        struct argv_array sent_argv = ARGV_ARRAY_INIT;
  23        const char *arg_cmd = "argument ";
  24
  25        if (argc != 2 || !strcmp(argv[1], "-h"))
  26                usage(upload_archive_usage);
  27
  28        if (!enter_repo(argv[1], 0))
  29                die("'%s' does not appear to be a git repository", argv[1]);
  30
  31        init_archivers();
  32
  33        /* put received options in sent_argv[] */
  34        argv_array_push(&sent_argv, "git-upload-archive");
  35        for (;;) {
  36                char *buf = packet_read_line(0, NULL);
  37                if (!buf)
  38                        break;  /* got a flush */
  39                if (sent_argv.argc > MAX_ARGS)
  40                        die("Too many options (>%d)", MAX_ARGS - 1);
  41
  42                if (!starts_with(buf, arg_cmd))
  43                        die("'argument' token or flush expected");
  44                argv_array_push(&sent_argv, buf + strlen(arg_cmd));
  45        }
  46
  47        /* parse all options sent by the client */
  48        return write_archive(sent_argv.argc, sent_argv.argv, prefix,
  49                             the_repository, NULL, 1);
  50}
  51
  52__attribute__((format (printf, 1, 2)))
  53static void error_clnt(const char *fmt, ...)
  54{
  55        struct strbuf buf = STRBUF_INIT;
  56        va_list params;
  57
  58        va_start(params, fmt);
  59        strbuf_vaddf(&buf, fmt, params);
  60        va_end(params);
  61        send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
  62        die("sent error to the client: %s", buf.buf);
  63}
  64
  65static ssize_t process_input(int child_fd, int band)
  66{
  67        char buf[16384];
  68        ssize_t sz = read(child_fd, buf, sizeof(buf));
  69        if (sz < 0) {
  70                if (errno != EAGAIN && errno != EINTR)
  71                        error_clnt("read error: %s\n", strerror(errno));
  72                return sz;
  73        }
  74        send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
  75        return sz;
  76}
  77
  78int cmd_upload_archive(int argc, const char **argv, const char *prefix)
  79{
  80        struct child_process writer = { argv };
  81
  82        if (argc == 2 && !strcmp(argv[1], "-h"))
  83                usage(upload_archive_usage);
  84
  85        /*
  86         * Set up sideband subprocess.
  87         *
  88         * We (parent) monitor and read from child, sending its fd#1 and fd#2
  89         * multiplexed out to our fd#1.  If the child dies, we tell the other
  90         * end over channel #3.
  91         */
  92        argv[0] = "upload-archive--writer";
  93        writer.out = writer.err = -1;
  94        writer.git_cmd = 1;
  95        if (start_command(&writer)) {
  96                int err = errno;
  97                packet_write_fmt(1, "NACK unable to spawn subprocess\n");
  98                die("upload-archive: %s", strerror(err));
  99        }
 100
 101        packet_write_fmt(1, "ACK\n");
 102        packet_flush(1);
 103
 104        while (1) {
 105                struct pollfd pfd[2];
 106
 107                pfd[0].fd = writer.out;
 108                pfd[0].events = POLLIN;
 109                pfd[1].fd = writer.err;
 110                pfd[1].events = POLLIN;
 111                if (poll(pfd, 2, -1) < 0) {
 112                        if (errno != EINTR) {
 113                                error_errno("poll failed resuming");
 114                                sleep(1);
 115                        }
 116                        continue;
 117                }
 118                if (pfd[1].revents & POLLIN)
 119                        /* Status stream ready */
 120                        if (process_input(pfd[1].fd, 2))
 121                                continue;
 122                if (pfd[0].revents & POLLIN)
 123                        /* Data stream ready */
 124                        if (process_input(pfd[0].fd, 1))
 125                                continue;
 126
 127                if (finish_command(&writer))
 128                        error_clnt("%s", deadchild);
 129                packet_flush(1);
 130                break;
 131        }
 132        return 0;
 133}