git-filter-branch.sh: Allow running in bare repositories
[alt-git.git] / builtin-upload-archive.c
blob13a6c6203eff7d8dc96a56fa5709dc9eb69da0a6
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
4 #include "cache.h"
5 #include "builtin.h"
6 #include "archive.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
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 ";
26 char *p, buf[4096];
27 int treeish_idx;
28 int sent_argc;
29 int len;
31 if (argc != 2)
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[] */
43 sent_argc = 1;
44 sent_argv[0] = "git-upload-archive";
45 for (p = buf;;) {
46 /* This will die if not enough free space in buf */
47 len = packet_read_line(0, p, (buf + sizeof buf) - p);
48 if (len == 0)
49 break; /* got a flush */
50 if (sent_argc > MAX_ARGS - 2)
51 die("Too many options (>29)");
53 if (p[len-1] == '\n') {
54 p[--len] = 0;
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;
63 p += len;
64 *p++ = 0;
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, ...)
79 char buf[1024];
80 va_list params;
81 int len;
83 va_start(params, fmt);
84 len = vsprintf(buf, fmt, params);
85 va_end(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)
92 char buf[16384];
93 ssize_t sz = read(child_fd, buf, sizeof(buf));
94 if (sz < 0) {
95 if (errno != EAGAIN && errno != EINTR)
96 error_clnt("read error: %s\n", strerror(errno));
97 return;
99 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
102 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
104 pid_t writer;
105 int fd1[2], fd2[2];
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) {
114 int err = errno;
115 packet_write(1, "NACK pipe failed on the remote side\n");
116 die("upload-archive: %s", strerror(err));
118 writer = fork();
119 if (writer < 0) {
120 int err = errno;
121 packet_write(1, "NACK fork failed on the remote side\n");
122 die("upload-archive: %s", strerror(err));
124 if (!writer) {
125 /* child - connect fd#1 and fd#2 to the pipe */
126 dup2(fd1[1], 1);
127 dup2(fd2[1], 2);
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");
137 packet_flush(1);
139 while (1) {
140 struct pollfd pfd[2];
141 int status;
143 pfd[0].fd = fd1[0];
144 pfd[0].events = POLLIN;
145 pfd[1].fd = fd2[0];
146 pfd[1].events = POLLIN;
147 if (poll(pfd, 2, -1) < 0) {
148 if (errno != EINTR) {
149 error("poll failed resuming: %s",
150 strerror(errno));
151 sleep(1);
153 continue;
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)
163 continue;
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);
169 packet_flush(1);
170 break;
172 return 0;