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