builtin-upload-archive.con commit archive: add write_archive() (6e94e68)
   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
  10static const char upload_archive_usage[] =
  11        "git upload-archive <repo>";
  12
  13static const char deadchild[] =
  14"git upload-archive: archiver died with error";
  15
  16static const char lostchild[] =
  17"git upload-archive: archiver process was lost";
  18
  19
  20static int run_upload_archive(int argc, const char **argv, const char *prefix)
  21{
  22        const char *sent_argv[MAX_ARGS];
  23        const char *arg_cmd = "argument ";
  24        char *p, buf[4096];
  25        int sent_argc;
  26        int len;
  27
  28        if (argc != 2)
  29                usage(upload_archive_usage);
  30
  31        if (strlen(argv[1]) + 1 > sizeof(buf))
  32                die("insanely long repository name");
  33
  34        strcpy(buf, argv[1]); /* enter-repo smudges its argument */
  35
  36        if (!enter_repo(buf, 0))
  37                die("not a git archive");
  38
  39        /* put received options in sent_argv[] */
  40        sent_argc = 1;
  41        sent_argv[0] = "git-upload-archive";
  42        for (p = buf;;) {
  43                /* This will die if not enough free space in buf */
  44                len = packet_read_line(0, p, (buf + sizeof buf) - p);
  45                if (len == 0)
  46                        break;  /* got a flush */
  47                if (sent_argc > MAX_ARGS - 2)
  48                        die("Too many options (>29)");
  49
  50                if (p[len-1] == '\n') {
  51                        p[--len] = 0;
  52                }
  53                if (len < strlen(arg_cmd) ||
  54                    strncmp(arg_cmd, p, strlen(arg_cmd)))
  55                        die("'argument' token or flush expected");
  56
  57                len -= strlen(arg_cmd);
  58                memmove(p, p + strlen(arg_cmd), len);
  59                sent_argv[sent_argc++] = p;
  60                p += len;
  61                *p++ = 0;
  62        }
  63        sent_argv[sent_argc] = NULL;
  64
  65        /* parse all options sent by the client */
  66        return write_archive(sent_argc, sent_argv, prefix, 0);
  67}
  68
  69static void error_clnt(const char *fmt, ...)
  70{
  71        char buf[1024];
  72        va_list params;
  73        int len;
  74
  75        va_start(params, fmt);
  76        len = vsprintf(buf, fmt, params);
  77        va_end(params);
  78        send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
  79        die("sent error to the client: %s", buf);
  80}
  81
  82static void process_input(int child_fd, int band)
  83{
  84        char buf[16384];
  85        ssize_t sz = read(child_fd, buf, sizeof(buf));
  86        if (sz < 0) {
  87                if (errno != EAGAIN && errno != EINTR)
  88                        error_clnt("read error: %s\n", strerror(errno));
  89                return;
  90        }
  91        send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
  92}
  93
  94int cmd_upload_archive(int argc, const char **argv, const char *prefix)
  95{
  96        pid_t writer;
  97        int fd1[2], fd2[2];
  98        /*
  99         * Set up sideband subprocess.
 100         *
 101         * We (parent) monitor and read from child, sending its fd#1 and fd#2
 102         * multiplexed out to our fd#1.  If the child dies, we tell the other
 103         * end over channel #3.
 104         */
 105        if (pipe(fd1) < 0 || pipe(fd2) < 0) {
 106                int err = errno;
 107                packet_write(1, "NACK pipe failed on the remote side\n");
 108                die("upload-archive: %s", strerror(err));
 109        }
 110        writer = fork();
 111        if (writer < 0) {
 112                int err = errno;
 113                packet_write(1, "NACK fork failed on the remote side\n");
 114                die("upload-archive: %s", strerror(err));
 115        }
 116        if (!writer) {
 117                /* child - connect fd#1 and fd#2 to the pipe */
 118                dup2(fd1[1], 1);
 119                dup2(fd2[1], 2);
 120                close(fd1[1]); close(fd2[1]);
 121                close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
 122
 123                exit(run_upload_archive(argc, argv, prefix));
 124        }
 125
 126        /* parent - read from child, multiplex and send out to fd#1 */
 127        close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
 128        packet_write(1, "ACK\n");
 129        packet_flush(1);
 130
 131        while (1) {
 132                struct pollfd pfd[2];
 133                int status;
 134
 135                pfd[0].fd = fd1[0];
 136                pfd[0].events = POLLIN;
 137                pfd[1].fd = fd2[0];
 138                pfd[1].events = POLLIN;
 139                if (poll(pfd, 2, -1) < 0) {
 140                        if (errno != EINTR) {
 141                                error("poll failed resuming: %s",
 142                                      strerror(errno));
 143                                sleep(1);
 144                        }
 145                        continue;
 146                }
 147                if (pfd[0].revents & POLLIN)
 148                        /* Data stream ready */
 149                        process_input(pfd[0].fd, 1);
 150                if (pfd[1].revents & POLLIN)
 151                        /* Status stream ready */
 152                        process_input(pfd[1].fd, 2);
 153                /* Always finish to read data when available */
 154                if ((pfd[0].revents | pfd[1].revents) & POLLIN)
 155                        continue;
 156
 157                if (waitpid(writer, &status, 0) < 0)
 158                        error_clnt("%s", lostchild);
 159                else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
 160                        error_clnt("%s", deadchild);
 161                packet_flush(1);
 162                break;
 163        }
 164        return 0;
 165}