t-reftable-block: use reftable_record_key() instead of strbuf_addstr()
[git/gitster.git] / builtin / upload-archive.c
blob1b09e5e1aa3f08e91bf6de731b111864ef405573
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
4 #include "builtin.h"
5 #include "archive.h"
6 #include "path.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "repository.h"
10 #include "run-command.h"
11 #include "strvec.h"
13 static const char upload_archive_usage[] =
14 "git upload-archive <repository>";
16 static const char deadchild[] =
17 "git upload-archive: archiver died with error";
19 #define MAX_ARGS (64)
21 int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
23 struct strvec sent_argv = STRVEC_INIT;
24 const char *arg_cmd = "argument ";
26 if (argc != 2 || !strcmp(argv[1], "-h"))
27 usage(upload_archive_usage);
29 if (!enter_repo(argv[1], 0))
30 die("'%s' does not appear to be a git repository", argv[1]);
32 init_archivers();
34 /* put received options in sent_argv[] */
35 strvec_push(&sent_argv, "git-upload-archive");
36 for (;;) {
37 char *buf = packet_read_line(0, NULL);
38 if (!buf)
39 break; /* got a flush */
40 if (sent_argv.nr > MAX_ARGS)
41 die("Too many options (>%d)", MAX_ARGS - 1);
43 if (!starts_with(buf, arg_cmd))
44 die("'argument' token or flush expected");
45 strvec_push(&sent_argv, buf + strlen(arg_cmd));
48 /* parse all options sent by the client */
49 return write_archive(sent_argv.nr, sent_argv.v, prefix,
50 the_repository, NULL, 1);
53 __attribute__((format (printf, 1, 2)))
54 static void error_clnt(const char *fmt, ...)
56 struct strbuf buf = STRBUF_INIT;
57 va_list params;
59 va_start(params, fmt);
60 strbuf_vaddf(&buf, fmt, params);
61 va_end(params);
62 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
63 die("sent error to the client: %s", buf.buf);
66 static ssize_t process_input(int child_fd, int band)
68 char buf[16384];
69 ssize_t sz = read(child_fd, buf, sizeof(buf));
70 if (sz < 0) {
71 if (errno != EAGAIN && errno != EINTR)
72 error_clnt("read error: %s\n", strerror(errno));
73 return sz;
75 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
76 return sz;
79 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
81 struct child_process writer = CHILD_PROCESS_INIT;
83 BUG_ON_NON_EMPTY_PREFIX(prefix);
85 if (argc == 2 && !strcmp(argv[1], "-h"))
86 usage(upload_archive_usage);
89 * Set up sideband subprocess.
91 * We (parent) monitor and read from child, sending its fd#1 and fd#2
92 * multiplexed out to our fd#1. If the child dies, we tell the other
93 * end over channel #3.
95 writer.out = writer.err = -1;
96 writer.git_cmd = 1;
97 strvec_push(&writer.args, "upload-archive--writer");
98 strvec_pushv(&writer.args, argv + 1);
99 if (start_command(&writer)) {
100 int err = errno;
101 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
102 die("upload-archive: %s", strerror(err));
105 packet_write_fmt(1, "ACK\n");
106 packet_flush(1);
108 while (1) {
109 struct pollfd pfd[2];
111 pfd[0].fd = writer.out;
112 pfd[0].events = POLLIN;
113 pfd[1].fd = writer.err;
114 pfd[1].events = POLLIN;
115 if (poll(pfd, 2, -1) < 0) {
116 if (errno != EINTR) {
117 error_errno("poll failed resuming");
118 sleep(1);
120 continue;
122 if (pfd[1].revents & POLLIN)
123 /* Status stream ready */
124 if (process_input(pfd[1].fd, 2))
125 continue;
126 if (pfd[0].revents & POLLIN)
127 /* Data stream ready */
128 if (process_input(pfd[0].fd, 1))
129 continue;
131 if (finish_command(&writer))
132 error_clnt("%s", deadchild);
133 packet_flush(1);
134 break;
136 return 0;