upload-archive: convert sprintf to strbuf
[git/git.git] / builtin / upload-archive.c
CommitLineData
39345a21
FBH
1/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
39345a21
FBH
4#include "cache.h"
5#include "builtin.h"
6#include "archive.h"
7#include "pkt-line.h"
23d6d112 8#include "sideband.h"
1bc01efe 9#include "run-command.h"
090fd4fe 10#include "argv-array.h"
39345a21
FBH
11
12static const char upload_archive_usage[] =
1b1dd23f 13 "git upload-archive <repo>";
39345a21 14
23d6d112 15static const char deadchild[] =
1b1dd23f 16"git upload-archive: archiver died with error";
39345a21 17
7f4d0511 18#define MAX_ARGS (64)
23d6d112 19
1bc01efe 20int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
39345a21 21{
090fd4fe 22 struct argv_array sent_argv = ARGV_ARRAY_INIT;
39345a21 23 const char *arg_cmd = "argument ";
39345a21 24
f0c7fd49
JH
25 if (argc != 2)
26 usage(upload_archive_usage);
27
6379dd05
JK
28 if (!enter_repo(argv[1], 0))
29 die("'%s' does not appear to be a git repository", argv[1]);
f0c7fd49 30
39345a21 31 /* put received options in sent_argv[] */
090fd4fe
JK
32 argv_array_push(&sent_argv, "git-upload-archive");
33 for (;;) {
74543a04
JK
34 char *buf = packet_read_line(0, NULL);
35 if (!buf)
39345a21 36 break; /* got a flush */
090fd4fe
JK
37 if (sent_argv.argc > MAX_ARGS)
38 die("Too many options (>%d)", MAX_ARGS - 1);
39345a21 39
59556548 40 if (!starts_with(buf, arg_cmd))
090fd4fe
JK
41 die("'argument' token or flush expected");
42 argv_array_push(&sent_argv, buf + strlen(arg_cmd));
39345a21 43 }
f0c7fd49
JH
44
45 /* parse all options sent by the client */
090fd4fe 46 return write_archive(sent_argv.argc, sent_argv.argv, prefix, 0, NULL, 1);
23d6d112
JH
47}
48
28bea9e5 49__attribute__((format (printf, 1, 2)))
d3788e19
JH
50static void error_clnt(const char *fmt, ...)
51{
0cb9d6d6 52 struct strbuf buf = STRBUF_INIT;
d3788e19 53 va_list params;
d3788e19
JH
54
55 va_start(params, fmt);
0cb9d6d6 56 strbuf_vaddf(&buf, fmt, params);
d3788e19 57 va_end(params);
0cb9d6d6
JK
58 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
59 die("sent error to the client: %s", buf.buf);
d3788e19
JH
60}
61
1b19fa46 62static ssize_t process_input(int child_fd, int band)
d3788e19
JH
63{
64 char buf[16384];
65 ssize_t sz = read(child_fd, buf, sizeof(buf));
66 if (sz < 0) {
93d26e4c 67 if (errno != EAGAIN && errno != EINTR)
d3788e19 68 error_clnt("read error: %s\n", strerror(errno));
1b19fa46 69 return sz;
d3788e19
JH
70 }
71 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
1b19fa46 72 return sz;
d3788e19
JH
73}
74
23d6d112
JH
75int cmd_upload_archive(int argc, const char **argv, const char *prefix)
76{
1bc01efe
JK
77 struct child_process writer = { argv };
78
f0c7fd49
JH
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 */
1bc01efe
JK
86 argv[0] = "upload-archive--writer";
87 writer.out = writer.err = -1;
88 writer.git_cmd = 1;
89 if (start_command(&writer)) {
23d6d112 90 int err = errno;
1bc01efe 91 packet_write(1, "NACK unable to spawn subprocess\n");
23d6d112
JH
92 die("upload-archive: %s", strerror(err));
93 }
23d6d112 94
39345a21
FBH
95 packet_write(1, "ACK\n");
96 packet_flush(1);
97
23d6d112
JH
98 while (1) {
99 struct pollfd pfd[2];
23d6d112 100
1bc01efe 101 pfd[0].fd = writer.out;
23d6d112 102 pfd[0].events = POLLIN;
1bc01efe 103 pfd[1].fd = writer.err;
23d6d112
JH
104 pfd[1].events = POLLIN;
105 if (poll(pfd, 2, -1) < 0) {
106 if (errno != EINTR) {
107 error("poll failed resuming: %s",
108 strerror(errno));
109 sleep(1);
110 }
111 continue;
112 }
d3788e19 113 if (pfd[1].revents & POLLIN)
23d6d112 114 /* Status stream ready */
6b59f51b
NP
115 if (process_input(pfd[1].fd, 2))
116 continue;
117 if (pfd[0].revents & POLLIN)
118 /* Data stream ready */
119 if (process_input(pfd[0].fd, 1))
120 continue;
d3788e19 121
1bc01efe 122 if (finish_command(&writer))
d3788e19 123 error_clnt("%s", deadchild);
23d6d112
JH
124 packet_flush(1);
125 break;
126 }
127 return 0;
128}