fetch/clone: mark messages from remote side stand out.
[git/git.git] / fetch-clone.c
CommitLineData
ad897215 1#include "cache.h"
77cb17e9 2#include "exec_cmd.h"
583b7ea3 3#include "pkt-line.h"
ad897215 4#include <sys/wait.h>
5ee2ad65 5#include <sys/time.h>
ad897215
JH
6
7static int finish_pack(const char *pack_tmp_name, const char *me)
8{
9 int pipe_fd[2];
10 pid_t pid;
11 char idx[PATH_MAX];
12 char final[PATH_MAX];
13 char hash[41];
14 unsigned char sha1[20];
15 char *cp;
16 int err = 0;
17
18 if (pipe(pipe_fd) < 0)
19 die("%s: unable to set up pipe", me);
20
21 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
22 cp = strrchr(idx, '/');
23 memcpy(cp, "/pidx", 5);
24
25 pid = fork();
26 if (pid < 0)
583b7ea3 27 die("%s: unable to fork off git-index-pack", me);
ad897215
JH
28 if (!pid) {
29 close(0);
30 dup2(pipe_fd[1], 1);
31 close(pipe_fd[0]);
32 close(pipe_fd[1]);
77cb17e9 33 execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
ad897215
JH
34 error("cannot exec git-index-pack <%s> <%s>",
35 idx, pack_tmp_name);
36 exit(1);
37 }
38 close(pipe_fd[1]);
39 if (read(pipe_fd[0], hash, 40) != 40) {
40 error("%s: unable to read from git-index-pack", me);
41 err = 1;
42 }
43 close(pipe_fd[0]);
44
45 for (;;) {
46 int status, code;
47 int retval = waitpid(pid, &status, 0);
48
49 if (retval < 0) {
50 if (errno == EINTR)
51 continue;
7f272ca8 52 error("waitpid failed (%s)", strerror(errno));
ad897215
JH
53 goto error_die;
54 }
55 if (WIFSIGNALED(status)) {
56 int sig = WTERMSIG(status);
57 error("git-index-pack died of signal %d", sig);
58 goto error_die;
59 }
60 if (!WIFEXITED(status)) {
61 error("git-index-pack died of unnatural causes %d",
62 status);
63 goto error_die;
64 }
65 code = WEXITSTATUS(status);
66 if (code) {
67 error("git-index-pack died with error code %d", code);
68 goto error_die;
69 }
70 if (err)
71 goto error_die;
72 break;
73 }
74 hash[40] = 0;
75 if (get_sha1_hex(hash, sha1)) {
76 error("git-index-pack reported nonsense '%s'", hash);
77 goto error_die;
78 }
79 /* Now we have pack in pack_tmp_name[], and
80 * idx in idx[]; rename them to their final names.
81 */
82 snprintf(final, sizeof(final),
83 "%s/pack/pack-%s.pack", get_object_directory(), hash);
84 move_temp_to_file(pack_tmp_name, final);
85 chmod(final, 0444);
86 snprintf(final, sizeof(final),
87 "%s/pack/pack-%s.idx", get_object_directory(), hash);
88 move_temp_to_file(idx, final);
89 chmod(final, 0444);
90 return 0;
91
92 error_die:
93 unlink(idx);
94 unlink(pack_tmp_name);
95 exit(1);
96}
97
583b7ea3
JH
98static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
99{
100 pid_t side_pid;
101
102 if (!sideband) {
103 fd[0] = xd[0];
104 fd[1] = xd[1];
105 return 0;
106 }
107 /* xd[] is talking with upload-pack; subprocess reads from
108 * xd[0], spits out band#2 to stderr, and feeds us band#1
109 * through our fd[0].
110 */
111 if (pipe(fd) < 0)
112 die("%s: unable to set up pipe", me);
113 side_pid = fork();
114 if (side_pid < 0)
115 die("%s: unable to fork off sideband demultiplexer", me);
116 if (!side_pid) {
117 /* subprocess */
118 close(fd[0]);
119 if (xd[0] != xd[1])
120 close(xd[1]);
121 while (1) {
122 char buf[1024];
123 int len = packet_read_line(xd[0], buf, sizeof(buf));
124 if (len == 0)
125 break;
126 if (len < 1)
127 die("%s: protocol error: no band designator",
128 me);
129 len--;
130 switch (buf[0] & 0xFF) {
131 case 3:
132 safe_write(2, buf+1, len);
133 fprintf(stderr, "\n");
134 exit(1);
135 case 2:
dfa46478
JH
136 /* color sideband */
137 safe_write(2, "\033[44;37;1m", 10);
583b7ea3 138 safe_write(2, buf+1, len);
dfa46478 139 safe_write(2, "\033[m", 3);
583b7ea3
JH
140 continue;
141 case 1:
142 safe_write(fd[1], buf+1, len);
143 continue;
144 default:
145 die("%s: protocol error: bad band #%d",
146 me, (buf[0] & 0xFF));
147 }
148 }
149 exit(0);
150 }
151 close(xd[0]);
152 close(fd[1]);
153 fd[1] = xd[1];
154 return side_pid;
155}
156
157int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
ad897215
JH
158{
159 int status;
583b7ea3
JH
160 pid_t pid, side_pid;
161 int fd[2];
ad897215 162
583b7ea3 163 side_pid = setup_sideband(sideband, me, fd, xd);
ad897215
JH
164 pid = fork();
165 if (pid < 0)
166 die("%s: unable to fork off git-unpack-objects", me);
167 if (!pid) {
168 dup2(fd[0], 0);
169 close(fd[0]);
170 close(fd[1]);
77cb17e9 171 execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
ad897215
JH
172 die("git-unpack-objects exec failed");
173 }
174 close(fd[0]);
175 close(fd[1]);
176 while (waitpid(pid, &status, 0) < 0) {
177 if (errno != EINTR)
178 die("waiting for git-unpack-objects: %s",
179 strerror(errno));
180 }
181 if (WIFEXITED(status)) {
182 int code = WEXITSTATUS(status);
183 if (code)
184 die("git-unpack-objects died with error code %d",
185 code);
186 return 0;
187 }
188 if (WIFSIGNALED(status)) {
189 int sig = WTERMSIG(status);
190 die("git-unpack-objects died of signal %d", sig);
191 }
192 die("git-unpack-objects died of unnatural causes %d", status);
193}
194
c548cf4e
LT
195/*
196 * We average out the download speed over this many "events", where
197 * an event is a minimum of about half a second. That way, we get
198 * a reasonably stable number.
199 */
200#define NR_AVERAGE (4)
201
202/*
203 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
82e5a82f
PR
204 * Keeping the time in that format means that "bytes / msecs" means
205 * the same as kB/s (modulo rounding).
c548cf4e
LT
206 *
207 * 1000512 is a magic number (usecs in a second, rounded up by half
208 * of 1024, to make "rounding" come out right ;)
209 */
210#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
211
583b7ea3 212int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
ad897215
JH
213{
214 char tmpfile[PATH_MAX];
583b7ea3 215 int ofd, ifd, fd[2];
5ee2ad65
LT
216 unsigned long total;
217 static struct timeval prev_tv;
c548cf4e
LT
218 struct average {
219 unsigned long bytes;
220 unsigned long time;
221 } download[NR_AVERAGE] = { {0, 0}, };
222 unsigned long avg_bytes, avg_time;
223 int idx = 0;
ad897215 224
583b7ea3
JH
225 setup_sideband(sideband, me, fd, xd);
226
ad897215
JH
227 ifd = fd[0];
228 snprintf(tmpfile, sizeof(tmpfile),
229 "%s/pack/tmp-XXXXXX", get_object_directory());
230 ofd = mkstemp(tmpfile);
231 if (ofd < 0)
232 return error("unable to create temporary file %s", tmpfile);
233
5ee2ad65
LT
234 gettimeofday(&prev_tv, NULL);
235 total = 0;
c548cf4e
LT
236 avg_bytes = 0;
237 avg_time = 0;
ad897215
JH
238 while (1) {
239 char buf[8192];
240 ssize_t sz, wsz, pos;
241 sz = read(ifd, buf, sizeof(buf));
242 if (sz == 0)
243 break;
244 if (sz < 0) {
98deeaa8
LT
245 if (errno != EINTR && errno != EAGAIN) {
246 error("error reading pack (%s)", strerror(errno));
247 close(ofd);
248 unlink(tmpfile);
249 return -1;
250 }
251 sz = 0;
ad897215
JH
252 }
253 pos = 0;
254 while (pos < sz) {
255 wsz = write(ofd, buf + pos, sz - pos);
256 if (wsz < 0) {
257 error("error writing pack (%s)",
258 strerror(errno));
259 close(ofd);
260 unlink(tmpfile);
261 return -1;
262 }
263 pos += wsz;
264 }
5ee2ad65
LT
265 total += sz;
266 if (!quiet) {
267 static unsigned long last;
268 struct timeval tv;
269 unsigned long diff = total - last;
270 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
271 unsigned long msecs;
272
273 gettimeofday(&tv, NULL);
274 msecs = tv.tv_sec - prev_tv.tv_sec;
275 msecs <<= 10;
c548cf4e
LT
276 msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
277
5ee2ad65
LT
278 if (msecs > 500) {
279 prev_tv = tv;
280 last = total;
c548cf4e
LT
281
282 /* Update averages ..*/
283 avg_bytes += diff;
284 avg_time += msecs;
285 avg_bytes -= download[idx].bytes;
286 avg_time -= download[idx].time;
287 download[idx].bytes = diff;
288 download[idx].time = msecs;
289 idx++;
290 if (idx >= NR_AVERAGE)
291 idx = 0;
292
293 fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
5ee2ad65
LT
294 total >> 20,
295 1000*((total >> 10) & 1023)>>10,
c548cf4e 296 avg_bytes / avg_time );
5ee2ad65
LT
297 }
298 }
ad897215
JH
299 }
300 close(ofd);
301 return finish_pack(tmpfile, me);
302}