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