update-cache.con commit [PATCH] Do not initialize sha1_file_directory by hand. (3d0291c)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include <signal.h>
   7#include "cache.h"
   8
   9/*
  10 * Default to not allowing changes to the list of files. The
  11 * tool doesn't actually care, but this makes it harder to add
  12 * files to the revision control by mistake by doing something
  13 * like "update-cache *" and suddenly having all the object
  14 * files be revision controlled.
  15 */
  16static int allow_add = 0, allow_remove = 0, not_new = 0;
  17
  18/* Three functions to allow overloaded pointer return; see linux/err.h */
  19static inline void *ERR_PTR(long error)
  20{
  21        return (void *) error;
  22}
  23
  24static inline long PTR_ERR(const void *ptr)
  25{
  26        return (long) ptr;
  27}
  28
  29static inline long IS_ERR(const void *ptr)
  30{
  31        return (unsigned long)ptr > (unsigned long)-1000L;
  32}
  33
  34/*
  35 * This only updates the "non-critical" parts of the directory
  36 * cache, ie the parts that aren't tracked by GIT, and only used
  37 * to validate the cache.
  38 */
  39static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
  40{
  41        ce->ce_ctime.sec = htonl(st->st_ctime);
  42        ce->ce_mtime.sec = htonl(st->st_mtime);
  43#ifdef NSEC
  44        ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
  45        ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
  46#endif
  47        ce->ce_dev = htonl(st->st_dev);
  48        ce->ce_ino = htonl(st->st_ino);
  49        ce->ce_uid = htonl(st->st_uid);
  50        ce->ce_gid = htonl(st->st_gid);
  51        ce->ce_size = htonl(st->st_size);
  52}
  53
  54static int add_file_to_cache(char *path)
  55{
  56        int size, namelen;
  57        struct cache_entry *ce;
  58        struct stat st;
  59        int fd;
  60        char *target;
  61
  62        if (lstat(path, &st) < 0) {
  63                if (errno == ENOENT || errno == ENOTDIR) {
  64                        if (allow_remove)
  65                                return remove_file_from_cache(path);
  66                }
  67                return -1;
  68        }
  69        namelen = strlen(path);
  70        size = cache_entry_size(namelen);
  71        ce = xmalloc(size);
  72        memset(ce, 0, size);
  73        memcpy(ce->name, path, namelen);
  74        fill_stat_cache_info(ce, &st);
  75        ce->ce_mode = create_ce_mode(st.st_mode);
  76        ce->ce_flags = htons(namelen);
  77        switch (st.st_mode & S_IFMT) {
  78        case S_IFREG:
  79                fd = open(path, O_RDONLY);
  80                if (fd < 0)
  81                        return -1;
  82                if (index_fd(ce->sha1, fd, &st) < 0)
  83                        return -1;
  84                break;
  85        case S_IFLNK:
  86                target = xmalloc(st.st_size+1);
  87                if (readlink(path, target, st.st_size+1) != st.st_size) {
  88                        free(target);
  89                        return -1;
  90                }
  91                if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
  92                        return -1;
  93                free(target);
  94                break;
  95        default:
  96                return -1;
  97        }
  98        return add_cache_entry(ce, allow_add);
  99}
 100
 101static int match_data(int fd, void *buffer, unsigned long size)
 102{
 103        while (size) {
 104                char compare[1024];
 105                int ret = read(fd, compare, sizeof(compare));
 106
 107                if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
 108                        return -1;
 109                size -= ret;
 110                buffer += ret;
 111        }
 112        return 0;
 113}
 114
 115static int compare_data(struct cache_entry *ce, unsigned long expected_size)
 116{
 117        int match = -1;
 118        int fd = open(ce->name, O_RDONLY);
 119
 120        if (fd >= 0) {
 121                void *buffer;
 122                unsigned long size;
 123                char type[10];
 124
 125                buffer = read_sha1_file(ce->sha1, type, &size);
 126                if (buffer) {
 127                        if (size == expected_size && !strcmp(type, "blob"))
 128                                match = match_data(fd, buffer, size);
 129                        free(buffer);
 130                }
 131                close(fd);
 132        }
 133        return match;
 134}
 135
 136static int compare_link(struct cache_entry *ce, unsigned long expected_size)
 137{
 138        int match = -1;
 139        char *target;
 140        void *buffer;
 141        unsigned long size;
 142        char type[10];
 143        int len;
 144
 145        target = xmalloc(expected_size);
 146        len = readlink(ce->name, target, expected_size);
 147        if (len != expected_size) {
 148                free(target);
 149                return -1;
 150        }
 151        buffer = read_sha1_file(ce->sha1, type, &size);
 152        if (!buffer) {
 153                free(target);
 154                return -1;
 155        }
 156        if (size == expected_size)
 157                match = memcmp(buffer, target, size);
 158        free(buffer);
 159        free(target);
 160        return match;
 161}
 162
 163/*
 164 * "refresh" does not calculate a new sha1 file or bring the
 165 * cache up-to-date for mode/content changes. But what it
 166 * _does_ do is to "re-match" the stat information of a file
 167 * with the cache, so that you can refresh the cache for a
 168 * file that hasn't been changed but where the stat entry is
 169 * out of date.
 170 *
 171 * For example, you'd want to do this after doing a "read-tree",
 172 * to link up the stat cache details with the proper files.
 173 */
 174static struct cache_entry *refresh_entry(struct cache_entry *ce)
 175{
 176        struct stat st;
 177        struct cache_entry *updated;
 178        int changed, size;
 179
 180        if (lstat(ce->name, &st) < 0)
 181                return ERR_PTR(-errno);
 182
 183        changed = cache_match_stat(ce, &st);
 184        if (!changed)
 185                return ce;
 186
 187        /*
 188         * If the mode or type has changed, there's no point in trying
 189         * to refresh the entry - it's not going to match
 190         */
 191        if (changed & (MODE_CHANGED | TYPE_CHANGED))
 192                return ERR_PTR(-EINVAL);
 193
 194        switch (st.st_mode & S_IFMT) {
 195        case S_IFREG:
 196                if (compare_data(ce, st.st_size))
 197                        return ERR_PTR(-EINVAL);
 198                break;
 199        case S_IFLNK:
 200                if (compare_link(ce, st.st_size))
 201                        return ERR_PTR(-EINVAL);
 202                break;
 203        default:
 204                return ERR_PTR(-EINVAL);
 205        }
 206
 207        size = ce_size(ce);
 208        updated = xmalloc(size);
 209        memcpy(updated, ce, size);
 210        fill_stat_cache_info(updated, &st);
 211        return updated;
 212}
 213
 214static int refresh_cache(void)
 215{
 216        int i;
 217        int has_errors = 0;
 218
 219        for (i = 0; i < active_nr; i++) {
 220                struct cache_entry *ce, *new;
 221                ce = active_cache[i];
 222                if (ce_stage(ce)) {
 223                        printf("%s: needs merge\n", ce->name);
 224                        has_errors = 1;
 225                        while ((i < active_nr) &&
 226                               ! strcmp(active_cache[i]->name, ce->name))
 227                                i++;
 228                        i--;
 229                        continue;
 230                }
 231
 232                new = refresh_entry(ce);
 233                if (IS_ERR(new)) {
 234                        if (!(not_new && PTR_ERR(new) == -ENOENT)) {
 235                                printf("%s: needs update\n", ce->name);
 236                                has_errors = 1;
 237                        }
 238                        continue;
 239                }
 240                active_cache_changed = 1;
 241                active_cache[i] = new;
 242        }
 243        return has_errors;
 244}
 245
 246/*
 247 * We fundamentally don't like some paths: we don't want
 248 * dot or dot-dot anywhere, and in fact, we don't even want
 249 * any other dot-files (.git or anything else). They
 250 * are hidden, for chist sake.
 251 *
 252 * Also, we don't want double slashes or slashes at the
 253 * end that can make pathnames ambiguous.
 254 */
 255static int verify_path(char *path)
 256{
 257        char c;
 258
 259        goto inside;
 260        for (;;) {
 261                if (!c)
 262                        return 1;
 263                if (c == '/') {
 264inside:
 265                        c = *path++;
 266                        if (c != '/' && c != '.' && c != '\0')
 267                                continue;
 268                        return 0;
 269                }
 270                c = *path++;
 271        }
 272}
 273
 274static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
 275{
 276        int size, len;
 277        unsigned int mode;
 278        unsigned char sha1[20];
 279        struct cache_entry *ce;
 280
 281        if (sscanf(arg1, "%o", &mode) != 1)
 282                return -1;
 283        if (get_sha1_hex(arg2, sha1))
 284                return -1;
 285        if (!verify_path(arg3))
 286                return -1;
 287
 288        len = strlen(arg3);
 289        size = cache_entry_size(len);
 290        ce = xmalloc(size);
 291        memset(ce, 0, size);
 292
 293        memcpy(ce->sha1, sha1, 20);
 294        memcpy(ce->name, arg3, len);
 295        ce->ce_flags = htons(len);
 296        ce->ce_mode = create_ce_mode(mode);
 297        return add_cache_entry(ce, allow_add);
 298}
 299
 300static const char *lockfile_name = NULL;
 301
 302static void remove_lock_file(void)
 303{
 304        if (lockfile_name)
 305                unlink(lockfile_name);
 306}
 307
 308static void remove_lock_file_on_signal(int signo)
 309{
 310        remove_lock_file();
 311}
 312
 313int main(int argc, char **argv)
 314{
 315        int i, newfd, entries, has_errors = 0;
 316        int allow_options = 1;
 317        static char lockfile[MAXPATHLEN+1];
 318        const char *indexfile = get_index_file();
 319
 320        snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
 321
 322        newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
 323        if (newfd < 0)
 324                die("unable to create new cachefile");
 325
 326        signal(SIGINT, remove_lock_file_on_signal);
 327        atexit(remove_lock_file);
 328        lockfile_name = lockfile;
 329
 330        entries = read_cache();
 331        if (entries < 0)
 332                die("cache corrupted");
 333
 334        for (i = 1 ; i < argc; i++) {
 335                char *path = argv[i];
 336
 337                if (allow_options && *path == '-') {
 338                        if (!strcmp(path, "--")) {
 339                                allow_options = 0;
 340                                continue;
 341                        }
 342                        if (!strcmp(path, "--add")) {
 343                                allow_add = 1;
 344                                continue;
 345                        }
 346                        if (!strcmp(path, "--remove")) {
 347                                allow_remove = 1;
 348                                continue;
 349                        }
 350                        if (!strcmp(path, "--refresh")) {
 351                                has_errors |= refresh_cache();
 352                                continue;
 353                        }
 354                        if (!strcmp(path, "--cacheinfo")) {
 355                                if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
 356                                        die("update-cache: --cacheinfo <mode> <sha1> <path>");
 357                                i += 3;
 358                                continue;
 359                        }
 360                        if (!strcmp(path, "--force-remove")) {
 361                                if (argc <= i + 1)
 362                                        die("update-cache: --force-remove <path>");
 363                                if (remove_file_from_cache(argv[i+1]))
 364                                        die("update-cache: --force-remove cannot remove %s", argv[i+1]);
 365                                i++;
 366                                continue;
 367                        }
 368
 369                        if (!strcmp(path, "--ignore-missing")) {
 370                                not_new = 1;
 371                                continue;
 372                        }
 373                        die("unknown option %s", path);
 374                }
 375                if (!verify_path(path)) {
 376                        fprintf(stderr, "Ignoring path %s\n", argv[i]);
 377                        continue;
 378                }
 379                if (add_file_to_cache(path))
 380                        die("Unable to add %s to database", path);
 381        }
 382        if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
 383                die("Unable to write new cachefile");
 384
 385        lockfile_name = NULL;
 386        return has_errors ? 1 : 0;
 387}