fetch-clone.con commit Merge branch 'master' into next (d55e0ff)
   1#include "cache.h"
   2#include "exec_cmd.h"
   3#include <sys/wait.h>
   4#include <sys/time.h>
   5
   6static int finish_pack(const char *pack_tmp_name, const char *me)
   7{
   8        int pipe_fd[2];
   9        pid_t pid;
  10        char idx[PATH_MAX];
  11        char final[PATH_MAX];
  12        char hash[41];
  13        unsigned char sha1[20];
  14        char *cp;
  15        int err = 0;
  16
  17        if (pipe(pipe_fd) < 0)
  18                die("%s: unable to set up pipe", me);
  19
  20        strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
  21        cp = strrchr(idx, '/');
  22        memcpy(cp, "/pidx", 5);
  23
  24        pid = fork();
  25        if (pid < 0)
  26                die("git-clone-pack: unable to fork off git-index-pack");
  27        if (!pid) {
  28                close(0);
  29                dup2(pipe_fd[1], 1);
  30                close(pipe_fd[0]);
  31                close(pipe_fd[1]);
  32                execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
  33                error("cannot exec git-index-pack <%s> <%s>",
  34                      idx, pack_tmp_name);
  35                exit(1);
  36        }
  37        close(pipe_fd[1]);
  38        if (read(pipe_fd[0], hash, 40) != 40) {
  39                error("%s: unable to read from git-index-pack", me);
  40                err = 1;
  41        }
  42        close(pipe_fd[0]);
  43
  44        for (;;) {
  45                int status, code;
  46                int retval = waitpid(pid, &status, 0);
  47
  48                if (retval < 0) {
  49                        if (errno == EINTR)
  50                                continue;
  51                        error("waitpid failed (%s)", strerror(errno));
  52                        goto error_die;
  53                }
  54                if (WIFSIGNALED(status)) {
  55                        int sig = WTERMSIG(status);
  56                        error("git-index-pack died of signal %d", sig);
  57                        goto error_die;
  58                }
  59                if (!WIFEXITED(status)) {
  60                        error("git-index-pack died of unnatural causes %d",
  61                              status);
  62                        goto error_die;
  63                }
  64                code = WEXITSTATUS(status);
  65                if (code) {
  66                        error("git-index-pack died with error code %d", code);
  67                        goto error_die;
  68                }
  69                if (err)
  70                        goto error_die;
  71                break;
  72        }
  73        hash[40] = 0;
  74        if (get_sha1_hex(hash, sha1)) {
  75                error("git-index-pack reported nonsense '%s'", hash);
  76                goto error_die;
  77        }
  78        /* Now we have pack in pack_tmp_name[], and
  79         * idx in idx[]; rename them to their final names.
  80         */
  81        snprintf(final, sizeof(final),
  82                 "%s/pack/pack-%s.pack", get_object_directory(), hash);
  83        move_temp_to_file(pack_tmp_name, final);
  84        chmod(final, 0444);
  85        snprintf(final, sizeof(final),
  86                 "%s/pack/pack-%s.idx", get_object_directory(), hash);
  87        move_temp_to_file(idx, final);
  88        chmod(final, 0444);
  89        return 0;
  90
  91 error_die:
  92        unlink(idx);
  93        unlink(pack_tmp_name);
  94        exit(1);
  95}
  96
  97int receive_unpack_pack(int fd[2], const char *me, int quiet)
  98{
  99        int status;
 100        pid_t pid;
 101
 102        pid = fork();
 103        if (pid < 0)
 104                die("%s: unable to fork off git-unpack-objects", me);
 105        if (!pid) {
 106                dup2(fd[0], 0);
 107                close(fd[0]);
 108                close(fd[1]);
 109                execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
 110                die("git-unpack-objects exec failed");
 111        }
 112        close(fd[0]);
 113        close(fd[1]);
 114        while (waitpid(pid, &status, 0) < 0) {
 115                if (errno != EINTR)
 116                        die("waiting for git-unpack-objects: %s",
 117                            strerror(errno));
 118        }
 119        if (WIFEXITED(status)) {
 120                int code = WEXITSTATUS(status);
 121                if (code)
 122                        die("git-unpack-objects died with error code %d",
 123                            code);
 124                return 0;
 125        }
 126        if (WIFSIGNALED(status)) {
 127                int sig = WTERMSIG(status);
 128                die("git-unpack-objects died of signal %d", sig);
 129        }
 130        die("git-unpack-objects died of unnatural causes %d", status);
 131}
 132
 133/*
 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.
 137 */
 138#define NR_AVERAGE (4)
 139
 140/*
 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).
 144 *
 145 * 1000512 is a magic number (usecs in a second, rounded up by half
 146 * of 1024, to make "rounding" come out right ;)
 147 */
 148#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
 149
 150int receive_keep_pack(int fd[2], const char *me, int quiet)
 151{
 152        char tmpfile[PATH_MAX];
 153        int ofd, ifd;
 154        unsigned long total;
 155        static struct timeval prev_tv;
 156        struct average {
 157                unsigned long bytes;
 158                unsigned long time;
 159        } download[NR_AVERAGE] = { {0, 0}, };
 160        unsigned long avg_bytes, avg_time;
 161        int idx = 0;
 162
 163        ifd = fd[0];
 164        snprintf(tmpfile, sizeof(tmpfile),
 165                 "%s/pack/tmp-XXXXXX", get_object_directory());
 166        ofd = mkstemp(tmpfile);
 167        if (ofd < 0)
 168                return error("unable to create temporary file %s", tmpfile);
 169
 170        gettimeofday(&prev_tv, NULL);
 171        total = 0;
 172        avg_bytes = 0;
 173        avg_time = 0;
 174        while (1) {
 175                char buf[8192];
 176                ssize_t sz, wsz, pos;
 177                sz = read(ifd, buf, sizeof(buf));
 178                if (sz == 0)
 179                        break;
 180                if (sz < 0) {
 181                        if (errno != EINTR && errno != EAGAIN) {
 182                                error("error reading pack (%s)", strerror(errno));
 183                                close(ofd);
 184                                unlink(tmpfile);
 185                                return -1;
 186                        }
 187                        sz = 0;
 188                }
 189                pos = 0;
 190                while (pos < sz) {
 191                        wsz = write(ofd, buf + pos, sz - pos);
 192                        if (wsz < 0) {
 193                                error("error writing pack (%s)",
 194                                      strerror(errno));
 195                                close(ofd);
 196                                unlink(tmpfile);
 197                                return -1;
 198                        }
 199                        pos += wsz;
 200                }
 201                total += sz;
 202                if (!quiet) {
 203                        static unsigned long last;
 204                        struct timeval tv;
 205                        unsigned long diff = total - last;
 206                        /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
 207                        unsigned long msecs;
 208
 209                        gettimeofday(&tv, NULL);
 210                        msecs = tv.tv_sec - prev_tv.tv_sec;
 211                        msecs <<= 10;
 212                        msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
 213
 214                        if (msecs > 500) {
 215                                prev_tv = tv;
 216                                last = total;
 217
 218                                /* Update averages ..*/
 219                                avg_bytes += diff;
 220                                avg_time += msecs;
 221                                avg_bytes -= download[idx].bytes;
 222                                avg_time -= download[idx].time;
 223                                download[idx].bytes = diff;
 224                                download[idx].time = msecs;
 225                                idx++;
 226                                if (idx >= NR_AVERAGE)
 227                                        idx = 0;
 228
 229                                fprintf(stderr, "%4lu.%03luMB  (%lu kB/s)      \r",
 230                                        total >> 20,
 231                                        1000*((total >> 10) & 1023)>>10,
 232                                        avg_bytes / avg_time );
 233                        }
 234                }
 235        }
 236        close(ofd);
 237        return finish_pack(tmpfile, me);
 238}