2 * Copyright (c) 2006 Franck Bui-Huu
13 static const char upload_archive_usage[] =
14 "git-upload-archive <repo>";
16 static const char deadchild[] =
17 "git-upload-archive: archiver died with error";
19 static const char lostchild[] =
20 "git-upload-archive: archiver process was lost";
23 static int run_upload_archive(int argc, const char **argv, const char *prefix)
26 const char *sent_argv[MAX_ARGS];
27 const char *arg_cmd = "argument ";
34 usage(upload_archive_usage);
36 if (strlen(argv[1]) > sizeof(buf))
37 die("insanely long repository name");
39 strcpy(buf, argv[1]); /* enter-repo smudges its argument */
41 if (!enter_repo(buf, 0))
42 die("not a git archive");
44 /* put received options in sent_argv[] */
46 sent_argv[0] = "git-upload-archive";
48 /* This will die if not enough free space in buf */
49 len = packet_read_line(0, p, (buf + sizeof buf) - p);
51 break; /* got a flush */
52 if (sent_argc > MAX_ARGS - 2)
53 die("Too many options (>29)");
55 if (p[len-1] == '\n') {
58 if (len < strlen(arg_cmd) ||
59 strncmp(arg_cmd, p, strlen(arg_cmd)))
60 die("'argument' token or flush expected");
62 len -= strlen(arg_cmd);
63 memmove(p, p + strlen(arg_cmd), len);
64 sent_argv[sent_argc++] = p;
68 sent_argv[sent_argc] = NULL;
70 /* parse all options sent by the client */
71 treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar);
73 parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
74 parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
76 return ar.write_archive(&ar.args);
79 static void error_clnt(const char *fmt, ...)
85 va_start(params, fmt);
86 len = vsprintf(buf, fmt, params);
88 send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
89 die("sent error to the client: %s", buf);
92 static void process_input(int child_fd, int band)
95 ssize_t sz = read(child_fd, buf, sizeof(buf));
98 error_clnt("read error: %s\n", strerror(errno));
101 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
104 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
109 * Set up sideband subprocess.
111 * We (parent) monitor and read from child, sending its fd#1 and fd#2
112 * multiplexed out to our fd#1. If the child dies, we tell the other
113 * end over channel #3.
115 if (pipe(fd1) < 0 || pipe(fd2) < 0) {
117 packet_write(1, "NACK pipe failed on the remote side\n");
118 die("upload-archive: %s", strerror(err));
123 packet_write(1, "NACK fork failed on the remote side\n");
124 die("upload-archive: %s", strerror(err));
127 /* child - connect fd#1 and fd#2 to the pipe */
130 close(fd1[1]); close(fd2[1]);
131 close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
133 exit(run_upload_archive(argc, argv, prefix));
136 /* parent - read from child, multiplex and send out to fd#1 */
137 close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
138 packet_write(1, "ACK\n");
142 struct pollfd pfd[2];
146 pfd[0].events = POLLIN;
148 pfd[1].events = POLLIN;
149 if (poll(pfd, 2, -1) < 0) {
150 if (errno != EINTR) {
151 error("poll failed resuming: %s",
157 if (pfd[0].revents & POLLIN)
158 /* Data stream ready */
159 process_input(pfd[0].fd, 1);
160 if (pfd[1].revents & POLLIN)
161 /* Status stream ready */
162 process_input(pfd[1].fd, 2);
163 /* Always finish to read data when available */
164 if ((pfd[0].revents | pfd[1].revents) & POLLIN)
167 if (waitpid(writer, &status, 0) < 0)
168 error_clnt("%s", lostchild);
169 else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
170 error_clnt("%s", deadchild);