builtin / upload-archive.con commit pkt-line: teach packet_read_line to chomp newlines (819b929)
   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        char buf[4096];
  25        int len;
  26
  27        if (argc != 2)
  28                usage(upload_archive_usage);
  29
  30        if (!enter_repo(argv[1], 0))
  31                die("'%s' does not appear to be a git repository", argv[1]);
  32
  33        /* put received options in sent_argv[] */
  34        argv_array_push(&sent_argv, "git-upload-archive");
  35        for (;;) {
  36                /* This will die if not enough free space in buf */
  37                len = packet_read_line(0, buf, sizeof(buf));
  38                if (len == 0)
  39                        break;  /* got a flush */
  40                if (sent_argv.argc > MAX_ARGS)
  41                        die("Too many options (>%d)", MAX_ARGS - 1);
  42
  43                if (prefixcmp(buf, arg_cmd))
  44                        die("'argument' token or flush expected");
  45                argv_array_push(&sent_argv, buf + strlen(arg_cmd));
  46        }
  47
  48        /* parse all options sent by the client */
  49        return write_archive(sent_argv.argc, sent_argv.argv, prefix, 0, NULL, 1);
  50}
  51
  52__attribute__((format (printf, 1, 2)))
  53static void error_clnt(const char *fmt, ...)
  54{
  55        char buf[1024];
  56        va_list params;
  57        int len;
  58
  59        va_start(params, fmt);
  60        len = vsprintf(buf, fmt, params);
  61        va_end(params);
  62        send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
  63        die("sent error to the client: %s", buf);
  64}
  65
  66static ssize_t process_input(int child_fd, int band)
  67{
  68        char buf[16384];
  69        ssize_t sz = read(child_fd, buf, sizeof(buf));
  70        if (sz < 0) {
  71                if (errno != EAGAIN && errno != EINTR)
  72                        error_clnt("read error: %s\n", strerror(errno));
  73                return sz;
  74        }
  75        send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
  76        return sz;
  77}
  78
  79int cmd_upload_archive(int argc, const char **argv, const char *prefix)
  80{
  81        struct child_process writer = { argv };
  82
  83        /*
  84         * Set up sideband subprocess.
  85         *
  86         * We (parent) monitor and read from child, sending its fd#1 and fd#2
  87         * multiplexed out to our fd#1.  If the child dies, we tell the other
  88         * end over channel #3.
  89         */
  90        argv[0] = "upload-archive--writer";
  91        writer.out = writer.err = -1;
  92        writer.git_cmd = 1;
  93        if (start_command(&writer)) {
  94                int err = errno;
  95                packet_write(1, "NACK unable to spawn subprocess\n");
  96                die("upload-archive: %s", strerror(err));
  97        }
  98
  99        packet_write(1, "ACK\n");
 100        packet_flush(1);
 101
 102        while (1) {
 103                struct pollfd pfd[2];
 104
 105                pfd[0].fd = writer.out;
 106                pfd[0].events = POLLIN;
 107                pfd[1].fd = writer.err;
 108                pfd[1].events = POLLIN;
 109                if (poll(pfd, 2, -1) < 0) {
 110                        if (errno != EINTR) {
 111                                error("poll failed resuming: %s",
 112                                      strerror(errno));
 113                                sleep(1);
 114                        }
 115                        continue;
 116                }
 117                if (pfd[1].revents & POLLIN)
 118                        /* Status stream ready */
 119                        if (process_input(pfd[1].fd, 2))
 120                                continue;
 121                if (pfd[0].revents & POLLIN)
 122                        /* Data stream ready */
 123                        if (process_input(pfd[0].fd, 1))
 124                                continue;
 125
 126                if (finish_command(&writer))
 127                        error_clnt("%s", deadchild);
 128                packet_flush(1);
 129                break;
 130        }
 131        return 0;
 132}