Merge branch 'jc/nobody-sets-src-peer-ref'
[git/jnareb-git.git] / builtin / upload-archive.c
blobaf2da35e7d05f4de2d7a77a708ee3cf6f490266e
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"
12 static const char upload_archive_usage[] =
13 "git upload-archive <repo>";
15 static const char deadchild[] =
16 "git upload-archive: archiver died with error";
18 #define MAX_ARGS (64)
20 int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
22 struct argv_array sent_argv = ARGV_ARRAY_INIT;
23 const char *arg_cmd = "argument ";
25 if (argc != 2)
26 usage(upload_archive_usage);
28 if (!enter_repo(argv[1], 0))
29 die("'%s' does not appear to be a git repository", argv[1]);
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);
40 if (prefixcmp(buf, arg_cmd))
41 die("'argument' token or flush expected");
42 argv_array_push(&sent_argv, buf + strlen(arg_cmd));
45 /* parse all options sent by the client */
46 return write_archive(sent_argv.argc, sent_argv.argv, prefix, 0, NULL, 1);
49 __attribute__((format (printf, 1, 2)))
50 static void error_clnt(const char *fmt, ...)
52 char buf[1024];
53 va_list params;
54 int len;
56 va_start(params, fmt);
57 len = vsprintf(buf, fmt, params);
58 va_end(params);
59 send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
60 die("sent error to the client: %s", buf);
63 static ssize_t process_input(int child_fd, int band)
65 char buf[16384];
66 ssize_t sz = read(child_fd, buf, sizeof(buf));
67 if (sz < 0) {
68 if (errno != EAGAIN && errno != EINTR)
69 error_clnt("read error: %s\n", strerror(errno));
70 return sz;
72 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
73 return sz;
76 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
78 struct child_process writer = { argv };
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 argv[0] = "upload-archive--writer";
88 writer.out = writer.err = -1;
89 writer.git_cmd = 1;
90 if (start_command(&writer)) {
91 int err = errno;
92 packet_write(1, "NACK unable to spawn subprocess\n");
93 die("upload-archive: %s", strerror(err));
96 packet_write(1, "ACK\n");
97 packet_flush(1);
99 while (1) {
100 struct pollfd pfd[2];
102 pfd[0].fd = writer.out;
103 pfd[0].events = POLLIN;
104 pfd[1].fd = writer.err;
105 pfd[1].events = POLLIN;
106 if (poll(pfd, 2, -1) < 0) {
107 if (errno != EINTR) {
108 error("poll failed resuming: %s",
109 strerror(errno));
110 sleep(1);
112 continue;
114 if (pfd[1].revents & POLLIN)
115 /* Status stream ready */
116 if (process_input(pfd[1].fd, 2))
117 continue;
118 if (pfd[0].revents & POLLIN)
119 /* Data stream ready */
120 if (process_input(pfd[0].fd, 1))
121 continue;
123 if (finish_command(&writer))
124 error_clnt("%s", deadchild);
125 packet_flush(1);
126 break;
128 return 0;