]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - builtin/upload-archive.c
The eighth batch
[thirdparty/git.git] / builtin / upload-archive.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
4#define USE_THE_REPOSITORY_VARIABLE
5#include "builtin.h"
6#include "archive.h"
7#include "path.h"
8#include "pkt-line.h"
9#include "sideband.h"
10#include "run-command.h"
11#include "strvec.h"
12
13static const char upload_archive_usage[] =
14 "git upload-archive <repository>";
15
16static const char deadchild[] =
17"git upload-archive: archiver died with error";
18
19#define MAX_ARGS (64)
20
21int cmd_upload_archive_writer(int argc,
22 const char **argv,
23 const char *prefix,
24 struct repository *repo UNUSED)
25{
26 struct strvec sent_argv = STRVEC_INIT;
27 const char *arg_cmd = "argument ";
28 int ret;
29
30 show_usage_if_asked(argc, argv, upload_archive_usage);
31 if (argc != 2)
32 usage(upload_archive_usage);
33
34 if (!enter_repo(argv[1], 0))
35 die("'%s' does not appear to be a git repository", argv[1]);
36
37 init_archivers();
38
39 /* put received options in sent_argv[] */
40 strvec_push(&sent_argv, "git-upload-archive");
41 for (;;) {
42 char *buf = packet_read_line(0, NULL);
43 if (!buf)
44 break; /* got a flush */
45 if (sent_argv.nr > MAX_ARGS)
46 die("Too many options (>%d)", MAX_ARGS - 1);
47
48 if (!starts_with(buf, arg_cmd))
49 die("'argument' token or flush expected");
50 strvec_push(&sent_argv, buf + strlen(arg_cmd));
51 }
52
53 /* parse all options sent by the client */
54 ret = write_archive(sent_argv.nr, sent_argv.v, prefix,
55 the_repository, NULL, 1);
56
57 strvec_clear(&sent_argv);
58 return ret;
59}
60
61__attribute__((format (printf, 1, 2)))
62static void error_clnt(const char *fmt, ...)
63{
64 struct strbuf buf = STRBUF_INIT;
65 va_list params;
66
67 va_start(params, fmt);
68 strbuf_vaddf(&buf, fmt, params);
69 va_end(params);
70 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
71 die("sent error to the client: %s", buf.buf);
72}
73
74static ssize_t process_input(int child_fd, int band)
75{
76 char buf[16384];
77 ssize_t sz = read(child_fd, buf, sizeof(buf));
78 if (sz < 0) {
79 if (errno != EAGAIN && errno != EINTR)
80 error_clnt("read error: %s\n", strerror(errno));
81 return sz;
82 }
83 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
84 return sz;
85}
86
87int cmd_upload_archive(int argc,
88const char **argv,
89const char *prefix,
90struct repository *repo UNUSED)
91{
92 struct child_process writer = CHILD_PROCESS_INIT;
93
94 BUG_ON_NON_EMPTY_PREFIX(prefix);
95
96 show_usage_if_asked(argc, argv, upload_archive_usage);
97
98 /*
99 * Set up sideband subprocess.
100 *
101 * We (parent) monitor and read from child, sending its fd#1 and fd#2
102 * multiplexed out to our fd#1. If the child dies, we tell the other
103 * end over channel #3.
104 */
105 writer.out = writer.err = -1;
106 writer.git_cmd = 1;
107 strvec_push(&writer.args, "upload-archive--writer");
108 strvec_pushv(&writer.args, argv + 1);
109 if (start_command(&writer)) {
110 int err = errno;
111 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
112 die("upload-archive: %s", strerror(err));
113 }
114
115 packet_write_fmt(1, "ACK\n");
116 packet_flush(1);
117
118 while (1) {
119 struct pollfd pfd[2];
120
121 pfd[0].fd = writer.out;
122 pfd[0].events = POLLIN;
123 pfd[1].fd = writer.err;
124 pfd[1].events = POLLIN;
125 if (poll(pfd, 2, -1) < 0) {
126 if (errno != EINTR) {
127 error_errno("poll failed resuming");
128 sleep(1);
129 }
130 continue;
131 }
132 if (pfd[1].revents & POLLIN)
133 /* Status stream ready */
134 if (process_input(pfd[1].fd, 2))
135 continue;
136 if (pfd[0].revents & POLLIN)
137 /* Data stream ready */
138 if (process_input(pfd[0].fd, 1))
139 continue;
140
141 if (finish_command(&writer))
142 error_clnt("%s", deadchild);
143 packet_flush(1);
144 break;
145 }
146 return 0;
147}