local-fetch.con commit [PATCH] git-local-fetch: Fix error checking and leak in setup_indices() (8be707d)
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "fetch.h"
   7
   8static int use_link = 0;
   9static int use_symlink = 0;
  10static int use_filecopy = 1;
  11
  12static char *path; /* "Remote" git repository */
  13
  14void prefetch(unsigned char *sha1)
  15{
  16}
  17
  18static struct packed_git *packs = NULL;
  19
  20static void setup_index(unsigned char *sha1)
  21{
  22        struct packed_git *new_pack;
  23        char filename[PATH_MAX];
  24        strcpy(filename, path);
  25        strcat(filename, "/objects/pack/pack-");
  26        strcat(filename, sha1_to_hex(sha1));
  27        strcat(filename, ".idx");
  28        new_pack = parse_pack_index_file(sha1, filename);
  29        new_pack->next = packs;
  30        packs = new_pack;
  31}
  32
  33static int setup_indices(void)
  34{
  35        DIR *dir;
  36        struct dirent *de;
  37        char filename[PATH_MAX];
  38        unsigned char sha1[20];
  39        sprintf(filename, "%s/objects/pack/", path);
  40        dir = opendir(filename);
  41        if (!dir)
  42                return -1;
  43        while ((de = readdir(dir)) != NULL) {
  44                int namelen = strlen(de->d_name);
  45                if (namelen != 50 || 
  46                    strcmp(de->d_name + namelen - 5, ".pack"))
  47                        continue;
  48                get_sha1_hex(de->d_name + 5, sha1);
  49                setup_index(sha1);
  50        }
  51        closedir(dir);
  52        return 0;
  53}
  54
  55static int copy_file(const char *source, const char *dest, const char *hex)
  56{
  57        if (use_link) {
  58                if (!link(source, dest)) {
  59                        pull_say("link %s\n", hex);
  60                        return 0;
  61                }
  62                /* If we got ENOENT there is no point continuing. */
  63                if (errno == ENOENT) {
  64                        fprintf(stderr, "does not exist %s\n", source);
  65                        return -1;
  66                }
  67        }
  68        if (use_symlink && !symlink(source, dest)) {
  69                pull_say("symlink %s\n", hex);
  70                return 0;
  71        }
  72        if (use_filecopy) {
  73                int ifd, ofd, status;
  74                struct stat st;
  75                void *map;
  76                ifd = open(source, O_RDONLY);
  77                if (ifd < 0 || fstat(ifd, &st) < 0) {
  78                        close(ifd);
  79                        fprintf(stderr, "cannot open %s\n", source);
  80                        return -1;
  81                }
  82                map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0);
  83                close(ifd);
  84                if (map == MAP_FAILED) {
  85                        fprintf(stderr, "cannot mmap %s\n", source);
  86                        return -1;
  87                }
  88                ofd = open(dest, O_WRONLY | O_CREAT | O_EXCL, 0666);
  89                status = ((ofd < 0) ||
  90                          (write(ofd, map, st.st_size) != st.st_size));
  91                munmap(map, st.st_size);
  92                close(ofd);
  93                if (status)
  94                        fprintf(stderr, "cannot write %s\n", dest);
  95                else
  96                        pull_say("copy %s\n", hex);
  97                return status;
  98        }
  99        fprintf(stderr, "failed to copy %s with given copy methods.\n", hex);
 100        return -1;
 101}
 102
 103static int fetch_pack(const unsigned char *sha1)
 104{
 105        struct packed_git *target;
 106        char filename[PATH_MAX];
 107        if (setup_indices())
 108                return -1;
 109        target = find_sha1_pack(sha1, packs);
 110        if (!target)
 111                return error("Couldn't find %s: not separate or in any pack", 
 112                             sha1_to_hex(sha1));
 113        if (get_verbosely) {
 114                fprintf(stderr, "Getting pack %s\n",
 115                        sha1_to_hex(target->sha1));
 116                fprintf(stderr, " which contains %s\n",
 117                        sha1_to_hex(sha1));
 118        }
 119        sprintf(filename, "%s/objects/pack/pack-%s.pack", 
 120                path, sha1_to_hex(target->sha1));
 121        copy_file(filename, sha1_pack_name(target->sha1),
 122                  sha1_to_hex(target->sha1));
 123        sprintf(filename, "%s/objects/pack/pack-%s.idx", 
 124                path, sha1_to_hex(target->sha1));
 125        copy_file(filename, sha1_pack_index_name(target->sha1),
 126                  sha1_to_hex(target->sha1));
 127        install_packed_git(target);
 128        return 0;
 129}
 130
 131static int fetch_file(const unsigned char *sha1)
 132{
 133        static int object_name_start = -1;
 134        static char filename[PATH_MAX];
 135        char *hex = sha1_to_hex(sha1);
 136        const char *dest_filename = sha1_file_name(sha1);
 137
 138        if (object_name_start < 0) {
 139                strcpy(filename, path); /* e.g. git.git */
 140                strcat(filename, "/objects/");
 141                object_name_start = strlen(filename);
 142        }
 143        filename[object_name_start+0] = hex[0];
 144        filename[object_name_start+1] = hex[1];
 145        filename[object_name_start+2] = '/';
 146        strcpy(filename + object_name_start + 3, hex + 2);
 147        return copy_file(filename, dest_filename, hex);
 148}
 149
 150int fetch(unsigned char *sha1)
 151{
 152        return fetch_file(sha1) && fetch_pack(sha1);
 153}
 154
 155int fetch_ref(char *ref, unsigned char *sha1)
 156{
 157        static int ref_name_start = -1;
 158        static char filename[PATH_MAX];
 159        static char hex[41];
 160        int ifd;
 161
 162        if (ref_name_start < 0) {
 163                sprintf(filename, "%s/refs/", path);
 164                ref_name_start = strlen(filename);
 165        }
 166        strcpy(filename + ref_name_start, ref);
 167        ifd = open(filename, O_RDONLY);
 168        if (ifd < 0) {
 169                close(ifd);
 170                fprintf(stderr, "cannot open %s\n", filename);
 171                return -1;
 172        }
 173        if (read(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
 174                close(ifd);
 175                fprintf(stderr, "cannot read from %s\n", filename);
 176                return -1;
 177        }
 178        close(ifd);
 179        pull_say("ref %s\n", sha1_to_hex(sha1));
 180        return 0;
 181}
 182
 183static const char local_pull_usage[] =
 184"git-local-fetch [-c] [-t] [-a] [-d] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
 185
 186/* 
 187 * By default we only use file copy.
 188 * If -l is specified, a hard link is attempted.
 189 * If -s is specified, then a symlink is attempted.
 190 * If -n is _not_ specified, then a regular file-to-file copy is done.
 191 */
 192int main(int argc, char **argv)
 193{
 194        char *commit_id;
 195        int arg = 1;
 196
 197        while (arg < argc && argv[arg][0] == '-') {
 198                if (argv[arg][1] == 't')
 199                        get_tree = 1;
 200                else if (argv[arg][1] == 'c')
 201                        get_history = 1;
 202                else if (argv[arg][1] == 'a') {
 203                        get_all = 1;
 204                        get_tree = 1;
 205                        get_history = 1;
 206                }
 207                else if (argv[arg][1] == 'l')
 208                        use_link = 1;
 209                else if (argv[arg][1] == 's')
 210                        use_symlink = 1;
 211                else if (argv[arg][1] == 'n')
 212                        use_filecopy = 0;
 213                else if (argv[arg][1] == 'v')
 214                        get_verbosely = 1;
 215                else if (argv[arg][1] == 'w')
 216                        write_ref = argv[++arg];
 217                else
 218                        usage(local_pull_usage);
 219                arg++;
 220        }
 221        if (argc < arg + 2)
 222                usage(local_pull_usage);
 223        commit_id = argv[arg];
 224        path = argv[arg + 1];
 225
 226        if (pull(commit_id))
 227                return 1;
 228
 229        return 0;
 230}