fetch-clone.con commit gitweb: Make blame and snapshot a feature. (ddb8d90)
   1#include "cache.h"
   2#include "exec_cmd.h"
   3#include "pkt-line.h"
   4#include <sys/wait.h>
   5#include <sys/time.h>
   6
   7static int finish_pack(const char *pack_tmp_name, const char *me)
   8{
   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;
  17
  18        if (pipe(pipe_fd) < 0)
  19                die("%s: unable to set up pipe", me);
  20
  21        strcpy(idx, pack_tmp_name); /* ".git/objects/pack-XXXXXX" */
  22        cp = strrchr(idx, '/');
  23        memcpy(cp, "/pidx", 5);
  24
  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);
  37        }
  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;
  42        }
  43        close(pipe_fd[0]);
  44
  45        for (;;) {
  46                int status, code;
  47
  48                if (waitpid(pid, &status, 0) < 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
  97static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2])
  98{
  99        pid_t side_pid;
 100
 101        if (!sideband) {
 102                fd[0] = xd[0];
 103                fd[1] = xd[1];
 104                return 0;
 105        }
 106        /* xd[] is talking with upload-pack; subprocess reads from
 107         * xd[0], spits out band#2 to stderr, and feeds us band#1
 108         * through our fd[0].
 109         */
 110        if (pipe(fd) < 0)
 111                die("%s: unable to set up pipe", me);
 112        side_pid = fork();
 113        if (side_pid < 0)
 114                die("%s: unable to fork off sideband demultiplexer", me);
 115        if (!side_pid) {
 116                /* subprocess */
 117                close(fd[0]);
 118                if (xd[0] != xd[1])
 119                        close(xd[1]);
 120                while (1) {
 121                        char buf[1024];
 122                        int len = packet_read_line(xd[0], buf, sizeof(buf));
 123                        if (len == 0)
 124                                break;
 125                        if (len < 1)
 126                                die("%s: protocol error: no band designator",
 127                                    me);
 128                        len--;
 129                        switch (buf[0] & 0xFF) {
 130                        case 3:
 131                                safe_write(2, "remote: ", 8);
 132                                safe_write(2, buf+1, len);
 133                                safe_write(2, "\n", 1);
 134                                exit(1);
 135                        case 2:
 136                                safe_write(2, "remote: ", 8);
 137                                safe_write(2, buf+1, len);
 138                                continue;
 139                        case 1:
 140                                safe_write(fd[1], buf+1, len);
 141                                continue;
 142                        default:
 143                                die("%s: protocol error: bad band #%d",
 144                                    me, (buf[0] & 0xFF));
 145                        }
 146                }
 147                exit(0);
 148        }
 149        close(xd[0]);
 150        close(fd[1]);
 151        fd[1] = xd[1];
 152        return side_pid;
 153}
 154
 155int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband)
 156{
 157        int status;
 158        pid_t pid, side_pid;
 159        int fd[2];
 160
 161        side_pid = setup_sideband(sideband, me, fd, xd);
 162        pid = fork();
 163        if (pid < 0)
 164                die("%s: unable to fork off git-unpack-objects", me);
 165        if (!pid) {
 166                dup2(fd[0], 0);
 167                close(fd[0]);
 168                close(fd[1]);
 169                execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
 170                die("git-unpack-objects exec failed");
 171        }
 172        close(fd[0]);
 173        close(fd[1]);
 174        while (waitpid(pid, &status, 0) < 0) {
 175                if (errno != EINTR)
 176                        die("waiting for git-unpack-objects: %s",
 177                            strerror(errno));
 178        }
 179        if (WIFEXITED(status)) {
 180                int code = WEXITSTATUS(status);
 181                if (code)
 182                        die("git-unpack-objects died with error code %d",
 183                            code);
 184                return 0;
 185        }
 186        if (WIFSIGNALED(status)) {
 187                int sig = WTERMSIG(status);
 188                die("git-unpack-objects died of signal %d", sig);
 189        }
 190        die("git-unpack-objects died of unnatural causes %d", status);
 191}
 192
 193/*
 194 * We average out the download speed over this many "events", where
 195 * an event is a minimum of about half a second. That way, we get
 196 * a reasonably stable number.
 197 */
 198#define NR_AVERAGE (4)
 199
 200/*
 201 * A "binary msec" is a power-of-two-msec, aka 1/1024th of a second.
 202 * Keeping the time in that format means that "bytes / msecs" means
 203 * the same as kB/s (modulo rounding).
 204 *
 205 * 1000512 is a magic number (usecs in a second, rounded up by half
 206 * of 1024, to make "rounding" come out right ;)
 207 */
 208#define usec_to_binarymsec(x) ((int)(x) / (1000512 >> 10))
 209
 210int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband)
 211{
 212        char tmpfile[PATH_MAX];
 213        int ofd, ifd, fd[2];
 214        unsigned long total;
 215        static struct timeval prev_tv;
 216        struct average {
 217                unsigned long bytes;
 218                unsigned long time;
 219        } download[NR_AVERAGE] = { {0, 0}, };
 220        unsigned long avg_bytes, avg_time;
 221        int idx = 0;
 222
 223        setup_sideband(sideband, me, fd, xd);
 224
 225        ifd = fd[0];
 226        snprintf(tmpfile, sizeof(tmpfile),
 227                 "%s/pack/tmp-XXXXXX", get_object_directory());
 228        ofd = mkstemp(tmpfile);
 229        if (ofd < 0)
 230                return error("unable to create temporary file %s", tmpfile);
 231
 232        gettimeofday(&prev_tv, NULL);
 233        total = 0;
 234        avg_bytes = 0;
 235        avg_time = 0;
 236        while (1) {
 237                char buf[8192];
 238                ssize_t sz, wsz, pos;
 239                sz = read(ifd, buf, sizeof(buf));
 240                if (sz == 0)
 241                        break;
 242                if (sz < 0) {
 243                        if (errno != EINTR && errno != EAGAIN) {
 244                                error("error reading pack (%s)", strerror(errno));
 245                                close(ofd);
 246                                unlink(tmpfile);
 247                                return -1;
 248                        }
 249                        sz = 0;
 250                }
 251                pos = 0;
 252                while (pos < sz) {
 253                        wsz = write(ofd, buf + pos, sz - pos);
 254                        if (wsz < 0) {
 255                                error("error writing pack (%s)",
 256                                      strerror(errno));
 257                                close(ofd);
 258                                unlink(tmpfile);
 259                                return -1;
 260                        }
 261                        pos += wsz;
 262                }
 263                total += sz;
 264                if (!quiet) {
 265                        static unsigned long last;
 266                        struct timeval tv;
 267                        unsigned long diff = total - last;
 268                        /* not really "msecs", but a power-of-two millisec (1/1024th of a sec) */
 269                        unsigned long msecs;
 270
 271                        gettimeofday(&tv, NULL);
 272                        msecs = tv.tv_sec - prev_tv.tv_sec;
 273                        msecs <<= 10;
 274                        msecs += usec_to_binarymsec(tv.tv_usec - prev_tv.tv_usec);
 275
 276                        if (msecs > 500) {
 277                                prev_tv = tv;
 278                                last = total;
 279
 280                                /* Update averages ..*/
 281                                avg_bytes += diff;
 282                                avg_time += msecs;
 283                                avg_bytes -= download[idx].bytes;
 284                                avg_time -= download[idx].time;
 285                                download[idx].bytes = diff;
 286                                download[idx].time = msecs;
 287                                idx++;
 288                                if (idx >= NR_AVERAGE)
 289                                        idx = 0;
 290
 291                                fprintf(stderr, "%4lu.%03luMB  (%lu kB/s)      \r",
 292                                        total >> 20,
 293                                        1000*((total >> 10) & 1023)>>10,
 294                                        avg_bytes / avg_time );
 295                        }
 296                }
 297        }
 298        close(ofd);
 299        return finish_pack(tmpfile, me);
 300}