builtin-apply: remove unused increment
[git/debian.git] / fetch-clone.c
blob5e84c4620ff96f5c7ab93e5f3bfd10609edd5fd5
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
5 #include <sys/time.h>
7 static int finish_pack(const char *pack_tmp_name, const char *me)
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;
18 if (pipe(pipe_fd) < 0)
19 die("%s: unable to set up pipe", me);
21 strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
22 cp = strrchr(idx, '/');
23 memcpy(cp, "/pidx", 5);
25 pid = fork();
26 if (pid < 0)
27 die("%s: unable to fork off git-index-pack", me);
28 if (!pid) {
29 close(0);
30 dup2(pipe_fd[1], 1);
31 close(pipe_fd[0]);
32 close(pipe_fd[1]);
33 execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
34 error("cannot exec git-index-pack <%s> <%s>",
35 idx, pack_tmp_name);
36 exit(1);
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;
43 close(pipe_fd[0]);
45 for (;;) {
46 int status, code;
47 int retval = waitpid(pid, &status, 0);
49 if (retval < 0) {
50 if (errno == EINTR)
51 continue;
52 error("waitpid failed (%s)", strerror(errno));
53 goto error_die;
55 if (WIFSIGNALED(status)) {
56 int sig = WTERMSIG(status);
57 error("git-index-pack died of signal %d", sig);
58 goto error_die;
60 if (!WIFEXITED(status)) {
61 error("git-index-pack died of unnatural causes %d",
62 status);
63 goto error_die;
65 code = WEXITSTATUS(status);
66 if (code) {
67 error("git-index-pack died with error code %d", code);
68 goto error_die;
70 if (err)
71 goto error_die;
72 break;
74 hash[40] = 0;
75 if (get_sha1_hex(hash, sha1)) {
76 error("git-index-pack reported nonsense '%s'", hash);
77 goto error_die;
79 /* Now we have pack in pack_tmp_name[], and
80 * idx in idx[]; rename them to their final names.
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;
92 error_die:
93 unlink(idx);
94 unlink(pack_tmp_name);
95 exit(1);
98 static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
100 pid_t side_pid;
102 if (!sideband) {
103 fd[0] = xd[0];
104 fd[1] = xd[1];
105 return 0;
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].
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, "remote: ", 8);
133 safe_write(2, buf+1, len);
134 safe_write(2, "\n", 1);
135 exit(1);
136 case 2:
137 safe_write(2, "remote: ", 8);
138 safe_write(2, buf+1, len);
139 continue;
140 case 1:
141 safe_write(fd[1], buf+1, len);
142 continue;
143 default:
144 die("%s: protocol error: bad band #%d",
145 me, (buf[0] & 0xFF));
148 exit(0);
150 close(xd[0]);
151 close(fd[1]);
152 fd[1] = xd[1];
153 return side_pid;
156 int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
158 int status;
159 pid_t pid, side_pid;
160 int fd[2];
162 side_pid = setup_sideband(sideband, me, fd, xd);
163 pid = fork();
164 if (pid < 0)
165 die("%s: unable to fork off git-unpack-objects", me);
166 if (!pid) {
167 dup2(fd[0], 0);
168 close(fd[0]);
169 close(fd[1]);
170 execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
171 die("git-unpack-objects exec failed");
173 close(fd[0]);
174 close(fd[1]);
175 while (waitpid(pid, &status, 0) < 0) {
176 if (errno != EINTR)
177 die("waiting for git-unpack-objects: %s",
178 strerror(errno));
180 if (WIFEXITED(status)) {
181 int code = WEXITSTATUS(status);
182 if (code)
183 die("git-unpack-objects died with error code %d",
184 code);
185 return 0;
187 if (WIFSIGNALED(status)) {
188 int sig = WTERMSIG(status);
189 die("git-unpack-objects died of signal %d", sig);
191 die("git-unpack-objects died of unnatural causes %d", status);
195 * We average out the download speed over this many "events", where
196 * an event is a minimum of about half a second. That way, we get
197 * a reasonably stable number.
199 #define NR_AVERAGE (4)
202 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
203 * Keeping the time in that format means that "bytes / msecs" means
204 * the same as kB/s (modulo rounding).
206 * 1000512 is a magic number (usecs in a second, rounded up by half
207 * of 1024, to make "rounding" come out right ;)
209 #define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
211 int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
213 char tmpfile[PATH_MAX];
214 int ofd, ifd, fd[2];
215 unsigned long total;
216 static struct timeval prev_tv;
217 struct average {
218 unsigned long bytes;
219 unsigned long time;
220 } download[NR_AVERAGE] = { {0, 0}, };
221 unsigned long avg_bytes, avg_time;
222 int idx = 0;
224 setup_sideband(sideband, me, fd, xd);
226 ifd = fd[0];
227 snprintf(tmpfile, sizeof(tmpfile),
228 "%s/pack/tmp-XXXXXX", get_object_directory());
229 ofd = mkstemp(tmpfile);
230 if (ofd < 0)
231 return error("unable to create temporary file %s", tmpfile);
233 gettimeofday(&prev_tv, NULL);
234 total = 0;
235 avg_bytes = 0;
236 avg_time = 0;
237 while (1) {
238 char buf[8192];
239 ssize_t sz, wsz, pos;
240 sz = read(ifd, buf, sizeof(buf));
241 if (sz == 0)
242 break;
243 if (sz < 0) {
244 if (errno != EINTR && errno != EAGAIN) {
245 error("error reading pack (%s)", strerror(errno));
246 close(ofd);
247 unlink(tmpfile);
248 return -1;
250 sz = 0;
252 pos = 0;
253 while (pos < sz) {
254 wsz = write(ofd, buf + pos, sz - pos);
255 if (wsz < 0) {
256 error("error writing pack (%s)",
257 strerror(errno));
258 close(ofd);
259 unlink(tmpfile);
260 return -1;
262 pos += wsz;
264 total += sz;
265 if (!quiet) {
266 static unsigned long last;
267 struct timeval tv;
268 unsigned long diff = total - last;
269 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
270 unsigned long msecs;
272 gettimeofday(&tv, NULL);
273 msecs = tv.tv_sec - prev_tv.tv_sec;
274 msecs <<= 10;
275 msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
277 if (msecs > 500) {
278 prev_tv = tv;
279 last = total;
281 /* Update averages ..*/
282 avg_bytes += diff;
283 avg_time += msecs;
284 avg_bytes -= download[idx].bytes;
285 avg_time -= download[idx].time;
286 download[idx].bytes = diff;
287 download[idx].time = msecs;
288 idx++;
289 if (idx >= NR_AVERAGE)
290 idx = 0;
292 fprintf(stderr, "%4lu.%03luMB (%lu kB/s) \r",
293 total >> 20,
294 1000*((total >> 10) & 1023)>>10,
295 avg_bytes / avg_time );
299 close(ofd);
300 return finish_pack(tmpfile, me);