1#include "cache.h"
2#include "exec_cmd.h"
3#include "pkt-line.h"
4#include "sideband.h"
5#include <sys/wait.h>
6#include <sys/time.h>
7
8static int finish_pack(const char *pack_tmp_name, const char *me)
9{
10 int pipe_fd[2];
11 pid_t pid;
12 char idx[PATH_MAX];
13 char final[PATH_MAX];
14 char hash[41];
15 unsigned char sha1[20];
16 char *cp;
17 int err = 0;
18
19 if (pipe(pipe_fd) < 0)
20 die("%s: unable to set up pipe", me);
21
22 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
23 cp = strrchr(idx, '/');
24 memcpy(cp, "/pidx", 5);
25
26 pid = fork();
27 if (pid < 0)
28 die("%s: unable to fork off git-index-pack", me);
29 if (!pid) {
30 close(0);
31 dup2(pipe_fd[1], 1);
32 close(pipe_fd[0]);
33 close(pipe_fd[1]);
34 execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
35 error("cannot exec git-index-pack <%s> <%s>",
36 idx, pack_tmp_name);
37 exit(1);
38 }
39 close(pipe_fd[1]);
40 if (read(pipe_fd[0], hash, 40) != 40) {
41 error("%s: unable to read from git-index-pack", me);
42 err = 1;
43 }
44 close(pipe_fd[0]);
45
46 for (;;) {
47 int status, code;
48
49 if (waitpid(pid, &status, 0) < 0) {
50 if (errno == EINTR)
51 continue;
52 error("waitpid failed (%s)", strerror(errno));
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
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 char buf[LARGE_PACKET_MAX];
119
120 close(fd[0]);
121 if (xd[0] != xd[1])
122 close(xd[1]);
123 if (recv_sideband(me, xd[0], fd[1], 2, buf, sizeof(buf)))
124 exit(1);
125 exit(0);
126 }
127 close(xd[0]);
128 close(fd[1]);
129 fd[1] = xd[1];
130 return side_pid;
131}
132
133int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
134{
135 int status;
136 pid_t pid, side_pid;
137 int fd[2];
138
139 side_pid = setup_sideband(sideband, me, fd, xd);
140 pid = fork();
141 if (pid < 0)
142 die("%s: unable to fork off git-unpack-objects", me);
143 if (!pid) {
144 dup2(fd[0], 0);
145 close(fd[0]);
146 close(fd[1]);
147 execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
148 die("git-unpack-objects exec failed");
149 }
150 close(fd[0]);
151 close(fd[1]);
152 while (waitpid(pid, &status, 0) < 0) {
153 if (errno != EINTR)
154 die("waiting for git-unpack-objects: %s",
155 strerror(errno));
156 }
157 if (WIFEXITED(status)) {
158 int code = WEXITSTATUS(status);
159 if (code)
160 die("git-unpack-objects died with error code %d",
161 code);
162 return 0;
163 }
164 if (WIFSIGNALED(status)) {
165 int sig = WTERMSIG(status);
166 die("git-unpack-objects died of signal %d", sig);
167 }
168 die("git-unpack-objects died of unnatural causes %d", status);
169}
170
171/*
172 * We average out the download speed over this many "events", where
173 * an event is a minimum of about half a second. That way, we get
174 * a reasonably stable number.
175 */
176#define NR_AVERAGE (4)
177
178/*
179 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
180 * Keeping the time in that format means that "bytes / msecs" means
181 * the same as kB/s (modulo rounding).
182 *
183 * 1000512 is a magic number (usecs in a second, rounded up by half
184 * of 1024, to make "rounding" come out right ;)
185 */
186#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
187
188int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
189{
190 char tmpfile[PATH_MAX];
191 int ofd, ifd, fd[2];
192 unsigned long total;
193 static struct timeval prev_tv;
194 struct average {
195 unsigned long bytes;
196 unsigned long time;
197 } download[NR_AVERAGE] = { {0, 0}, };
198 unsigned long avg_bytes, avg_time;
199 int idx = 0;
200
201 setup_sideband(sideband, me, fd, xd);
202
203 ifd = fd[0];
204 snprintf(tmpfile, sizeof(tmpfile),
205 "%s/pack/tmp-XXXXXX", get_object_directory());
206 ofd = mkstemp(tmpfile);
207 if (ofd < 0)
208 return error("unable to create temporary file %s", tmpfile);
209
210 gettimeofday(&prev_tv, NULL);
211 total = 0;
212 avg_bytes = 0;
213 avg_time = 0;
214 while (1) {
215 char buf[8192];
216 ssize_t sz, wsz, pos;
217 sz = read(ifd, buf, sizeof(buf));
218 if (sz == 0)
219 break;
220 if (sz < 0) {
221 if (errno != EINTR && errno != EAGAIN) {
222 error("error reading pack (%s)", strerror(errno));
223 close(ofd);
224 unlink(tmpfile);
225 return -1;
226 }
227 sz = 0;
228 }
229 pos = 0;
230 while (pos < sz) {
231 wsz = write(ofd, buf + pos, sz - pos);
232 if (wsz < 0) {
233 error("error writing pack (%s)",
234 strerror(errno));
235 close(ofd);
236 unlink(tmpfile);
237 return -1;
238 }
239 pos += wsz;
240 }
241 total += sz;
242 if (!quiet) {
243 static unsigned long last;
244 struct timeval tv;
245 unsigned long diff = total - last;
246 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
247 unsigned long msecs;
248
249 gettimeofday(&tv, NULL);
250 msecs = tv.tv_sec - prev_tv.tv_sec;
251 msecs <<= 10;
252 msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
253
254 if (msecs > 500) {
255 prev_tv = tv;
256 last = total;
257
258 /* Update averages ..*/
259 avg_bytes += diff;
260 avg_time += msecs;
261 avg_bytes -= download[idx].bytes;
262 avg_time -= download[idx].time;
263 download[idx].bytes = diff;
264 download[idx].time = msecs;
265 idx++;
266 if (idx >= NR_AVERAGE)
267 idx = 0;
268
269 fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
270 total >> 20,
271 1000*((total >> 10) & 1023)>>10,
272 avg_bytes / avg_time );
273 }
274 }
275 }
276 close(ofd);
277 return finish_pack(tmpfile, me);
278}