2 * Copyright (c) 2006 Franck Bui-Huu
9 #include "run-command.h"
10 #include "argv-array.h"
12 static const char upload_archive_usage[] =
13 "git upload-archive <repo>";
15 static const char deadchild[] =
16 "git upload-archive: archiver died with error";
20 int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
22 struct argv_array sent_argv = ARGV_ARRAY_INIT;
23 const char *arg_cmd = "argument ";
28 usage(upload_archive_usage);
30 if (!enter_repo(argv[1], 0))
31 die("'%s' does not appear to be a git repository", argv[1]);
33 /* put received options in sent_argv[] */
34 argv_array_push(&sent_argv, "git-upload-archive");
36 /* This will die if not enough free space in buf */
37 len = packet_read_line(0, buf, sizeof(buf));
39 break; /* got a flush */
40 if (sent_argv.argc > MAX_ARGS)
41 die("Too many options (>%d)", MAX_ARGS - 1);
43 if (buf[len-1] == '\n') {
47 if (prefixcmp(buf, arg_cmd))
48 die("'argument' token or flush expected");
49 argv_array_push(&sent_argv, buf + strlen(arg_cmd));
52 /* parse all options sent by the client */
53 return write_archive(sent_argv.argc, sent_argv.argv, prefix, 0, NULL, 1);
56 __attribute__((format (printf, 1, 2)))
57 static void error_clnt(const char *fmt, ...)
63 va_start(params, fmt);
64 len = vsprintf(buf, fmt, params);
66 send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
67 die("sent error to the client: %s", buf);
70 static ssize_t process_input(int child_fd, int band)
73 ssize_t sz = read(child_fd, buf, sizeof(buf));
75 if (errno != EAGAIN && errno != EINTR)
76 error_clnt("read error: %s\n", strerror(errno));
79 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
83 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
85 struct child_process writer = { argv };
88 * Set up sideband subprocess.
90 * We (parent) monitor and read from child, sending its fd#1 and fd#2
91 * multiplexed out to our fd#1. If the child dies, we tell the other
92 * end over channel #3.
94 argv[0] = "upload-archive--writer";
95 writer.out = writer.err = -1;
97 if (start_command(&writer)) {
99 packet_write(1, "NACK unable to spawn subprocess\n");
100 die("upload-archive: %s", strerror(err));
103 packet_write(1, "ACK\n");
107 struct pollfd pfd[2];
109 pfd[0].fd = writer.out;
110 pfd[0].events = POLLIN;
111 pfd[1].fd = writer.err;
112 pfd[1].events = POLLIN;
113 if (poll(pfd, 2, -1) < 0) {
114 if (errno != EINTR) {
115 error("poll failed resuming: %s",
121 if (pfd[1].revents & POLLIN)
122 /* Status stream ready */
123 if (process_input(pfd[1].fd, 2))
125 if (pfd[0].revents & POLLIN)
126 /* Data stream ready */
127 if (process_input(pfd[0].fd, 1))
130 if (finish_command(&writer))
131 error_clnt("%s", deadchild);