Make collapse_slashes() allocate memory for its result
[git] / builtin / upload-archive.c
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"
9
10 static const char upload_archive_usage[] =
11         "git upload-archive <repo>";
12
13 static const char deadchild[] =
14 "git upload-archive: archiver died with error";
15
16 static const char lostchild[] =
17 "git upload-archive: archiver process was lost";
18
19 #define MAX_ARGS (64)
20
21 static int run_upload_archive(int argc, const char **argv, const char *prefix)
22 {
23         const char *sent_argv[MAX_ARGS];
24         const char *arg_cmd = "argument ";
25         char *p, buf[4096];
26         int sent_argc;
27         int len;
28
29         if (argc != 2)
30                 usage(upload_archive_usage);
31
32         if (strlen(argv[1]) + 1 > sizeof(buf))
33                 die("insanely long repository name");
34
35         strcpy(buf, argv[1]); /* enter-repo smudges its argument */
36
37         if (!enter_repo(buf, 0))
38                 die("'%s' does not appear to be a git repository", buf);
39
40         /* put received options in sent_argv[] */
41         sent_argc = 1;
42         sent_argv[0] = "git-upload-archive";
43         for (p = buf;;) {
44                 /* This will die if not enough free space in buf */
45                 len = packet_read_line(0, p, (buf + sizeof buf) - p);
46                 if (len == 0)
47                         break;  /* got a flush */
48                 if (sent_argc > MAX_ARGS - 2)
49                         die("Too many options (>%d)", MAX_ARGS - 2);
50
51                 if (p[len-1] == '\n') {
52                         p[--len] = 0;
53                 }
54                 if (len < strlen(arg_cmd) ||
55                     strncmp(arg_cmd, p, strlen(arg_cmd)))
56                         die("'argument' token or flush expected");
57
58                 len -= strlen(arg_cmd);
59                 memmove(p, p + strlen(arg_cmd), len);
60                 sent_argv[sent_argc++] = p;
61                 p += len;
62                 *p++ = 0;
63         }
64         sent_argv[sent_argc] = NULL;
65
66         /* parse all options sent by the client */
67         return write_archive(sent_argc, sent_argv, prefix, 0, NULL, 1);
68 }
69
70 __attribute__((format (printf, 1, 2)))
71 static void error_clnt(const char *fmt, ...)
72 {
73         char buf[1024];
74         va_list params;
75         int len;
76
77         va_start(params, fmt);
78         len = vsprintf(buf, fmt, params);
79         va_end(params);
80         send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
81         die("sent error to the client: %s", buf);
82 }
83
84 static ssize_t process_input(int child_fd, int band)
85 {
86         char buf[16384];
87         ssize_t sz = read(child_fd, buf, sizeof(buf));
88         if (sz < 0) {
89                 if (errno != EAGAIN && errno != EINTR)
90                         error_clnt("read error: %s\n", strerror(errno));
91                 return sz;
92         }
93         send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
94         return sz;
95 }
96
97 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
98 {
99         pid_t writer;
100         int fd1[2], fd2[2];
101         /*
102          * Set up sideband subprocess.
103          *
104          * We (parent) monitor and read from child, sending its fd#1 and fd#2
105          * multiplexed out to our fd#1.  If the child dies, we tell the other
106          * end over channel #3.
107          */
108         if (pipe(fd1) < 0 || pipe(fd2) < 0) {
109                 int err = errno;
110                 packet_write(1, "NACK pipe failed on the remote side\n");
111                 die("upload-archive: %s", strerror(err));
112         }
113         writer = fork();
114         if (writer < 0) {
115                 int err = errno;
116                 packet_write(1, "NACK fork failed on the remote side\n");
117                 die("upload-archive: %s", strerror(err));
118         }
119         if (!writer) {
120                 /* child - connect fd#1 and fd#2 to the pipe */
121                 dup2(fd1[1], 1);
122                 dup2(fd2[1], 2);
123                 close(fd1[1]); close(fd2[1]);
124                 close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
125
126                 exit(run_upload_archive(argc, argv, prefix));
127         }
128
129         /* parent - read from child, multiplex and send out to fd#1 */
130         close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
131         packet_write(1, "ACK\n");
132         packet_flush(1);
133
134         while (1) {
135                 struct pollfd pfd[2];
136                 int status;
137
138                 pfd[0].fd = fd1[0];
139                 pfd[0].events = POLLIN;
140                 pfd[1].fd = fd2[0];
141                 pfd[1].events = POLLIN;
142                 if (poll(pfd, 2, -1) < 0) {
143                         if (errno != EINTR) {
144                                 error("poll failed resuming: %s",
145                                       strerror(errno));
146                                 sleep(1);
147                         }
148                         continue;
149                 }
150                 if (pfd[1].revents & POLLIN)
151                         /* Status stream ready */
152                         if (process_input(pfd[1].fd, 2))
153                                 continue;
154                 if (pfd[0].revents & POLLIN)
155                         /* Data stream ready */
156                         if (process_input(pfd[0].fd, 1))
157                                 continue;
158
159                 if (waitpid(writer, &status, 0) < 0)
160                         error_clnt("%s", lostchild);
161                 else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
162                         error_clnt("%s", deadchild);
163                 packet_flush(1);
164                 break;
165         }
166         return 0;
167 }