cache.h: remove this no-longer-used header
[alt-git.git] / builtin / upload-archive.c
blobd4c7f162ff0bc241e64c8888d31b4ce3a4f8ceff
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
4 #include "builtin.h"
5 #include "archive.h"
6 #include "pkt-line.h"
7 #include "sideband.h"
8 #include "repository.h"
9 #include "run-command.h"
10 #include "strvec.h"
12 static const char upload_archive_usage[] =
13 "git upload-archive <repository>";
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 strvec sent_argv = STRVEC_INIT;
23 const char *arg_cmd = "argument ";
25 if (argc != 2 || !strcmp(argv[1], "-h"))
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 init_archivers();
33 /* put received options in sent_argv[] */
34 strvec_push(&sent_argv, "git-upload-archive");
35 for (;;) {
36 char *buf = packet_read_line(0, NULL);
37 if (!buf)
38 break; /* got a flush */
39 if (sent_argv.nr > MAX_ARGS)
40 die("Too many options (>%d)", MAX_ARGS - 1);
42 if (!starts_with(buf, arg_cmd))
43 die("'argument' token or flush expected");
44 strvec_push(&sent_argv, buf + strlen(arg_cmd));
47 /* parse all options sent by the client */
48 return write_archive(sent_argv.nr, sent_argv.v, prefix,
49 the_repository, NULL, 1);
52 __attribute__((format (printf, 1, 2)))
53 static void error_clnt(const char *fmt, ...)
55 struct strbuf buf = STRBUF_INIT;
56 va_list params;
58 va_start(params, fmt);
59 strbuf_vaddf(&buf, fmt, params);
60 va_end(params);
61 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
62 die("sent error to the client: %s", buf.buf);
65 static ssize_t process_input(int child_fd, int band)
67 char buf[16384];
68 ssize_t sz = read(child_fd, buf, sizeof(buf));
69 if (sz < 0) {
70 if (errno != EAGAIN && errno != EINTR)
71 error_clnt("read error: %s\n", strerror(errno));
72 return sz;
74 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
75 return sz;
78 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
80 struct child_process writer = CHILD_PROCESS_INIT;
82 BUG_ON_NON_EMPTY_PREFIX(prefix);
84 if (argc == 2 && !strcmp(argv[1], "-h"))
85 usage(upload_archive_usage);
88 * Set up sideband subprocess.
90 * We (parent) monitor and read from child, sending its fd#1 and fd#2
91 * multiplexed out to our fd#1. If the child dies, we tell the other
92 * end over channel #3.
94 writer.out = writer.err = -1;
95 writer.git_cmd = 1;
96 strvec_push(&writer.args, "upload-archive--writer");
97 strvec_pushv(&writer.args, argv + 1);
98 if (start_command(&writer)) {
99 int err = errno;
100 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
101 die("upload-archive: %s", strerror(err));
104 packet_write_fmt(1, "ACK\n");
105 packet_flush(1);
107 while (1) {
108 struct pollfd pfd[2];
110 pfd[0].fd = writer.out;
111 pfd[0].events = POLLIN;
112 pfd[1].fd = writer.err;
113 pfd[1].events = POLLIN;
114 if (poll(pfd, 2, -1) < 0) {
115 if (errno != EINTR) {
116 error_errno("poll failed resuming");
117 sleep(1);
119 continue;
121 if (pfd[1].revents & POLLIN)
122 /* Status stream ready */
123 if (process_input(pfd[1].fd, 2))
124 continue;
125 if (pfd[0].revents & POLLIN)
126 /* Data stream ready */
127 if (process_input(pfd[0].fd, 1))
128 continue;
130 if (finish_command(&writer))
131 error_clnt("%s", deadchild);
132 packet_flush(1);
133 break;
135 return 0;