6 static int finish_pack(const char *pack_tmp_name
, const char *me
)
13 unsigned char sha1
[20];
17 if (pipe(pipe_fd
) < 0)
18 die("%s: unable to set up pipe", me
);
20 strcpy(idx
, pack_tmp_name
); /* ".git/objects/pack-XXXXXX" */
21 cp
= strrchr(idx
, '/');
22 memcpy(cp
, "/pidx", 5);
26 die("git-clone-pack: unable to fork off git-index-pack");
32 execl_git_cmd("index-pack", "-o", idx
, pack_tmp_name
, NULL
);
33 error("cannot exec git-index-pack <%s> <%s>",
38 if (read(pipe_fd
[0], hash
, 40) != 40) {
39 error("%s: unable to read from git-index-pack", me
);
46 int retval
= waitpid(pid
, &status
, 0);
51 error("waitpid failed (%s)", strerror(errno
));
54 if (WIFSIGNALED(status
)) {
55 int sig
= WTERMSIG(status
);
56 error("git-index-pack died of signal %d", sig
);
59 if (!WIFEXITED(status
)) {
60 error("git-index-pack died of unnatural causes %d",
64 code
= WEXITSTATUS(status
);
66 error("git-index-pack died with error code %d", code
);
74 if (get_sha1_hex(hash
, sha1
)) {
75 error("git-index-pack reported nonsense '%s'", hash
);
78 /* Now we have pack in pack_tmp_name[], and
79 * idx in idx[]; rename them to their final names.
81 snprintf(final
, sizeof(final
),
82 "%s/pack/pack-%s.pack", get_object_directory(), hash
);
83 move_temp_to_file(pack_tmp_name
, final
);
85 snprintf(final
, sizeof(final
),
86 "%s/pack/pack-%s.idx", get_object_directory(), hash
);
87 move_temp_to_file(idx
, final
);
93 unlink(pack_tmp_name
);
97 int receive_unpack_pack(int fd
[2], const char *me
, int quiet
)
104 die("%s: unable to fork off git-unpack-objects", me
);
109 execl_git_cmd("unpack-objects", quiet
? "-q" : NULL
, NULL
);
110 die("git-unpack-objects exec failed");
114 while (waitpid(pid
, &status
, 0) < 0) {
116 die("waiting for git-unpack-objects: %s",
119 if (WIFEXITED(status
)) {
120 int code
= WEXITSTATUS(status
);
122 die("git-unpack-objects died with error code %d",
126 if (WIFSIGNALED(status
)) {
127 int sig
= WTERMSIG(status
);
128 die("git-unpack-objects died of signal %d", sig
);
130 die("git-unpack-objects died of unnatural causes %d", status
);
134 * We average out the download speed over this many "events", where
135 * an event is a minimum of about half a second. That way, we get
136 * a reasonably stable number.
138 #define NR_AVERAGE (4)
141 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
142 * Keeing the time in that format means that "bytes / msecs" means
143 * is the same as kB/s (modulo rounding).
145 * 1000512 is a magic number (usecs in a second, rounded up by half
146 * of 1024, to make "rounding" come out right ;)
148 #define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
150 int receive_keep_pack(int fd
[2], const char *me
, int quiet
)
152 char tmpfile
[PATH_MAX
];
155 static struct timeval prev_tv
;
159 } download
[NR_AVERAGE
] = { {0, 0}, };
160 unsigned long avg_bytes
, avg_time
;
164 snprintf(tmpfile
, sizeof(tmpfile
),
165 "%s/pack/tmp-XXXXXX", get_object_directory());
166 ofd
= mkstemp(tmpfile
);
168 return error("unable to create temporary file %s", tmpfile
);
170 gettimeofday(&prev_tv
, NULL
);
176 ssize_t sz
, wsz
, pos
;
177 sz
= read(ifd
, buf
, sizeof(buf
));
181 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
182 error("error reading pack (%s)", strerror(errno
));
191 wsz
= write(ofd
, buf
+ pos
, sz
- pos
);
193 error("error writing pack (%s)",
203 static unsigned long last
;
205 unsigned long diff
= total
- last
;
206 /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
209 gettimeofday(&tv
, NULL
);
210 msecs
= tv
.tv_sec
- prev_tv
.tv_sec
;
212 msecs
+= usec_to_binarymsec(tv
.tv_usec
- prev_tv
.tv_usec
);
218 /* Update averages ..*/
221 avg_bytes
-= download
[idx
].bytes
;
222 avg_time
-= download
[idx
].time
;
223 download
[idx
].bytes
= diff
;
224 download
[idx
].time
= msecs
;
226 if (idx
>= NR_AVERAGE
)
229 fprintf(stderr
, "%4lu.%03luMB (%lu kB/s) \r",
231 1000*((total
>> 10) & 1023)>>10,
232 avg_bytes
/ avg_time
);
237 return finish_pack(tmpfile
, me
);