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