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