2 * Copyright (c) 2006 Franck Bui-Huu
9 #include "run-command.h"
11 static const char upload_archive_usage[] =
12 "git upload-archive <repo>";
14 static const char deadchild[] =
15 "git upload-archive: archiver died with error";
19 int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
21 const char *sent_argv[MAX_ARGS];
22 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[] */
35 sent_argv[0] = "git-upload-archive";
37 /* This will die if not enough free space in buf */
38 len = packet_read_line(0, p, (buf + sizeof buf) - p);
40 break; /* got a flush */
41 if (sent_argc > MAX_ARGS - 2)
42 die("Too many options (>%d)", MAX_ARGS - 2);
44 if (p[len-1] == '\n') {
47 if (len < strlen(arg_cmd) ||
48 strncmp(arg_cmd, p, strlen(arg_cmd)))
49 die("'argument' token or flush expected");
51 len -= strlen(arg_cmd);
52 memmove(p, p + strlen(arg_cmd), len);
53 sent_argv[sent_argc++] = p;
57 sent_argv[sent_argc] = NULL;
59 /* parse all options sent by the client */
60 return write_archive(sent_argc, sent_argv, prefix, 0, NULL, 1);
63 __attribute__((format (printf, 1, 2)))
64 static void error_clnt(const char *fmt, ...)
70 va_start(params, fmt);
71 len = vsprintf(buf, fmt, params);
73 send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
74 die("sent error to the client: %s", buf);
77 static ssize_t process_input(int child_fd, int band)
80 ssize_t sz = read(child_fd, buf, sizeof(buf));
82 if (errno != EAGAIN && errno != EINTR)
83 error_clnt("read error: %s\n", strerror(errno));
86 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
90 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
92 struct child_process writer = { argv };
95 * Set up sideband subprocess.
97 * We (parent) monitor and read from child, sending its fd#1 and fd#2
98 * multiplexed out to our fd#1. If the child dies, we tell the other
99 * end over channel #3.
101 argv[0] = "upload-archive--writer";
102 writer.out = writer.err = -1;
104 if (start_command(&writer)) {
106 packet_write(1, "NACK unable to spawn subprocess\n");
107 die("upload-archive: %s", strerror(err));
110 packet_write(1, "ACK\n");
114 struct pollfd pfd[2];
116 pfd[0].fd = writer.out;
117 pfd[0].events = POLLIN;
118 pfd[1].fd = writer.err;
119 pfd[1].events = POLLIN;
120 if (poll(pfd, 2, -1) < 0) {
121 if (errno != EINTR) {
122 error("poll failed resuming: %s",
128 if (pfd[1].revents & POLLIN)
129 /* Status stream ready */
130 if (process_input(pfd[1].fd, 2))
132 if (pfd[0].revents & POLLIN)
133 /* Data stream ready */
134 if (process_input(pfd[0].fd, 1))
137 if (finish_command(&writer))
138 error_clnt("%s", deadchild);