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