builtin / upload-archive.con commit Merge branch 'jk/name-hash-dirent' (b1b7ced)
   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
  11static const char upload_archive_usage[] =
  12        "git upload-archive <repo>";
  13
  14static const char deadchild[] =
  15"git upload-archive: archiver died with error";
  16
  17static const char lostchild[] =
  18"git upload-archive: archiver process was lost";
  19
  20#define MAX_ARGS (64)
  21
  22static void prepare_argv(const char **sent_argv, const char **argv)
  23{
  24        const char *arg_cmd = "argument ";
  25        char *p, buf[4096];
  26        int sent_argc;
  27        int len;
  28
  29        /* put received options in sent_argv[] */
  30        sent_argc = 2;
  31        sent_argv[0] = "archive";
  32        sent_argv[1] = "--remote-request";
  33        for (p = buf;;) {
  34                /* This will die if not enough free space in buf */
  35                len = packet_read_line(0, p, (buf + sizeof buf) - p);
  36                if (len == 0)
  37                        break;  /* got a flush */
  38                if (sent_argc > MAX_ARGS - 2)
  39                        die("Too many options (>%d)", MAX_ARGS - 2);
  40
  41                if (p[len-1] == '\n') {
  42                        p[--len] = 0;
  43                }
  44                if (len < strlen(arg_cmd) ||
  45                    strncmp(arg_cmd, p, strlen(arg_cmd)))
  46                        die("'argument' token or flush expected");
  47
  48                len -= strlen(arg_cmd);
  49                memmove(p, p + strlen(arg_cmd), len);
  50                sent_argv[sent_argc++] = p;
  51                p += len;
  52                *p++ = 0;
  53        }
  54        sent_argv[sent_argc] = NULL;
  55}
  56
  57__attribute__((format (printf, 1, 2)))
  58static void error_clnt(const char *fmt, ...)
  59{
  60        char buf[1024];
  61        va_list params;
  62        int len;
  63
  64        va_start(params, fmt);
  65        len = vsprintf(buf, fmt, params);
  66        va_end(params);
  67        send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
  68        die("sent error to the client: %s", buf);
  69}
  70
  71static ssize_t process_input(int child_fd, int band)
  72{
  73        char buf[16384];
  74        ssize_t sz = read(child_fd, buf, sizeof(buf));
  75        if (sz < 0) {
  76                if (errno != EAGAIN && errno != EINTR)
  77                        error_clnt("read error: %s\n", strerror(errno));
  78                return sz;
  79        }
  80        send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
  81        return sz;
  82}
  83
  84int cmd_upload_archive(int argc, const char **argv, const char *prefix)
  85{
  86        const char *sent_argv[MAX_ARGS];
  87        struct child_process cld = { sent_argv };
  88        cld.out = cld.err = -1;
  89        cld.git_cmd = 1;
  90
  91        if (argc != 2)
  92                usage(upload_archive_usage);
  93
  94        if (!enter_repo(argv[1], 0))
  95                die("'%s' does not appear to be a git repository", argv[1]);
  96
  97        prepare_argv(sent_argv, argv);
  98        if (start_command(&cld)) {
  99                int err = errno;
 100                packet_write(1, "NACK fork failed on the remote side\n");
 101                die("upload-archive: %s", strerror(err));
 102        }
 103
 104        /* parent - read from child, multiplex and send out to fd#1 */
 105        packet_write(1, "ACK\n");
 106        packet_flush(1);
 107
 108        while (1) {
 109                struct pollfd pfd[2];
 110                int status;
 111
 112                pfd[0].fd = cld.out;
 113                pfd[0].events = POLLIN;
 114                pfd[1].fd = cld.err;
 115                pfd[1].events = POLLIN;
 116                if (poll(pfd, 2, -1) < 0) {
 117                        if (errno != EINTR) {
 118                                error("poll failed resuming: %s",
 119                                      strerror(errno));
 120                                sleep(1);
 121                        }
 122                        continue;
 123                }
 124                if (pfd[1].revents & POLLIN)
 125                        /* Status stream ready */
 126                        if (process_input(pfd[1].fd, 2))
 127                                continue;
 128                if (pfd[0].revents & POLLIN)
 129                        /* Data stream ready */
 130                        if (process_input(pfd[0].fd, 1))
 131                                continue;
 132
 133                if (waitpid(cld.pid, &status, 0) < 0)
 134                        error_clnt("%s", lostchild);
 135                else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
 136                        error_clnt("%s", deadchild);
 137                packet_flush(1);
 138                break;
 139        }
 140        return 0;
 141}