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";
20 static int run_upload_archive(int argc, const char **argv, const char *prefix)
23 const char *sent_argv[MAX_ARGS];
24 const char *arg_cmd = "argument ";
31 usage(upload_archive_usage);
33 if (strlen(argv[1]) > sizeof(buf))
34 die("insanely long repository name");
36 strcpy(buf, argv[1]); /* enter-repo smudges its argument */
38 if (!enter_repo(buf, 0))
39 die("not a git archive");
41 /* put received options in sent_argv[] */
43 sent_argv[0] = "git-upload-archive";
45 /* This will die if not enough free space in buf */
46 len = packet_read_line(0, p, (buf + sizeof buf) - p);
48 break; /* got a flush */
49 if (sent_argc > MAX_ARGS - 2)
50 die("Too many options (>29)");
52 if (p[len-1] == '\n') {
55 if (len < strlen(arg_cmd) ||
56 strncmp(arg_cmd, p, strlen(arg_cmd)))
57 die("'argument' token or flush expected");
59 len -= strlen(arg_cmd);
60 memmove(p, p + strlen(arg_cmd), len);
61 sent_argv[sent_argc++] = p;
65 sent_argv[sent_argc] = NULL;
67 /* parse all options sent by the client */
68 treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar);
70 parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
71 parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
73 return ar.write_archive(&ar.args);
76 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
81 * Set up sideband subprocess.
83 * We (parent) monitor and read from child, sending its fd#1 and fd#2
84 * multiplexed out to our fd#1. If the child dies, we tell the other
85 * end over channel #3.
87 if (pipe(fd1) < 0 || pipe(fd2) < 0) {
89 packet_write(1, "NACK pipe failed on the remote side\n");
90 die("upload-archive: %s", strerror(err));
95 packet_write(1, "NACK fork failed on the remote side\n");
96 die("upload-archive: %s", strerror(err));
99 /* child - connect fd#1 and fd#2 to the pipe */
102 close(fd1[1]); close(fd2[1]);
103 close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
105 exit(run_upload_archive(argc, argv, prefix));
108 /* parent - read from child, multiplex and send out to fd#1 */
109 close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
110 packet_write(1, "ACK\n");
114 struct pollfd pfd[2];
121 pfd[0].events = POLLIN;
123 pfd[1].events = POLLIN;
124 if (poll(pfd, 2, -1) < 0) {
125 if (errno != EINTR) {
126 error("poll failed resuming: %s",
132 if (pfd[0].revents & (POLLIN|POLLHUP)) {
133 /* Data stream ready */
134 sz = read(pfd[0].fd, buf, sizeof(buf));
135 send_sideband(1, 1, buf, sz, LARGE_PACKET_MAX);
137 if (pfd[1].revents & (POLLIN|POLLHUP)) {
138 /* Status stream ready */
139 sz = read(pfd[1].fd, buf, sizeof(buf));
140 send_sideband(1, 2, buf, sz, LARGE_PACKET_MAX);
143 if (((pfd[0].revents | pfd[1].revents) & POLLHUP) == 0)
146 pid = waitpid(writer, &status, WNOHANG);
148 fprintf(stderr, "Hmph, HUP?\n");
151 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
152 send_sideband(1, 3, deadchild, strlen(deadchild),