2 * Copyright (c) 2006 Franck Bui-Huu
10 static const char upload_archive_usage[] =
11 "git upload-archive <repo>";
13 static const char deadchild[] =
14 "git upload-archive: archiver died with error";
16 static const char lostchild[] =
17 "git upload-archive: archiver process was lost";
20 static int run_upload_archive(int argc, const char **argv, const char *prefix)
22 const struct archiver *ar;
23 struct archiver_args args;
24 const char *sent_argv[MAX_ARGS];
25 const char *arg_cmd = "argument ";
32 usage(upload_archive_usage);
34 if (strlen(argv[1]) + 1 > sizeof(buf))
35 die("insanely long repository name");
37 strcpy(buf, argv[1]); /* enter-repo smudges its argument */
39 if (!enter_repo(buf, 0))
40 die("not a git archive");
42 /* put received options in sent_argv[] */
44 sent_argv[0] = "git-upload-archive";
46 /* This will die if not enough free space in buf */
47 len = packet_read_line(0, p, (buf + sizeof buf) - p);
49 break; /* got a flush */
50 if (sent_argc > MAX_ARGS - 2)
51 die("Too many options (>29)");
53 if (p[len-1] == '\n') {
56 if (len < strlen(arg_cmd) ||
57 strncmp(arg_cmd, p, strlen(arg_cmd)))
58 die("'argument' token or flush expected");
60 len -= strlen(arg_cmd);
61 memmove(p, p + strlen(arg_cmd), len);
62 sent_argv[sent_argc++] = p;
66 sent_argv[sent_argc] = NULL;
68 /* parse all options sent by the client */
69 treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar, &args);
71 parse_treeish_arg(sent_argv + treeish_idx, &args, prefix);
72 parse_pathspec_arg(sent_argv + treeish_idx + 1, &args);
74 return ar->write_archive(&args);
77 static void error_clnt(const char *fmt, ...)
83 va_start(params, fmt);
84 len = vsprintf(buf, fmt, params);
86 send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
87 die("sent error to the client: %s", buf);
90 static void process_input(int child_fd, int band)
93 ssize_t sz = read(child_fd, buf, sizeof(buf));
95 if (errno != EAGAIN && errno != EINTR)
96 error_clnt("read error: %s\n", strerror(errno));
99 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
102 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
107 * Set up sideband subprocess.
109 * We (parent) monitor and read from child, sending its fd#1 and fd#2
110 * multiplexed out to our fd#1. If the child dies, we tell the other
111 * end over channel #3.
113 if (pipe(fd1) < 0 || pipe(fd2) < 0) {
115 packet_write(1, "NACK pipe failed on the remote side\n");
116 die("upload-archive: %s", strerror(err));
121 packet_write(1, "NACK fork failed on the remote side\n");
122 die("upload-archive: %s", strerror(err));
125 /* child - connect fd#1 and fd#2 to the pipe */
128 close(fd1[1]); close(fd2[1]);
129 close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
131 exit(run_upload_archive(argc, argv, prefix));
134 /* parent - read from child, multiplex and send out to fd#1 */
135 close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
136 packet_write(1, "ACK\n");
140 struct pollfd pfd[2];
144 pfd[0].events = POLLIN;
146 pfd[1].events = POLLIN;
147 if (poll(pfd, 2, -1) < 0) {
148 if (errno != EINTR) {
149 error("poll failed resuming: %s",
155 if (pfd[0].revents & POLLIN)
156 /* Data stream ready */
157 process_input(pfd[0].fd, 1);
158 if (pfd[1].revents & POLLIN)
159 /* Status stream ready */
160 process_input(pfd[1].fd, 2);
161 /* Always finish to read data when available */
162 if ((pfd[0].revents | pfd[1].revents) & POLLIN)
165 if (waitpid(writer, &status, 0) < 0)
166 error_clnt("%s", lostchild);
167 else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
168 error_clnt("%s", deadchild);