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