1/*2* GIT - The information manager from hell3*4* Copyright (C) Linus Torvalds, 20055*/6#include "cache.h"78/*9* Default to not allowing changes to the list of files. The10* tool doesn't actually care, but this makes it harder to add11* files to the revision control by mistake by doing something12* like "update-cache *" and suddenly having all the object13* files be revision controlled.14*/15static int allow_add = 0, allow_remove = 0;1617static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)18{19z_stream stream;20unsigned long size = st->st_size;21int max_out_bytes = namelen + size + 200;22void *out = malloc(max_out_bytes);23void *metadata = malloc(namelen + 200);24void *in;25SHA_CTX c;2627in = "";28if (size)29in = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);30close(fd);31if (!out || (int)(long)in == -1)32return -1;3334memset(&stream, 0, sizeof(stream));35deflateInit(&stream, Z_BEST_COMPRESSION);3637/*38* ASCII size + nul byte39*/40stream.next_in = metadata;41stream.avail_in = 1+sprintf(metadata, "blob %lu", size);42stream.next_out = out;43stream.avail_out = max_out_bytes;44while (deflate(&stream, 0) == Z_OK)45/* nothing */;4647/*48* File content49*/50stream.next_in = in;51stream.avail_in = size;52while (deflate(&stream, Z_FINISH) == Z_OK)53/*nothing */;5455deflateEnd(&stream);5657SHA1_Init(&c);58SHA1_Update(&c, out, stream.total_out);59SHA1_Final(ce->sha1, &c);6061return write_sha1_buffer(ce->sha1, out, stream.total_out);62}6364/*65* This only updates the "non-critical" parts of the directory66* cache, ie the parts that aren't tracked by GIT, and only used67* to validate the cache.68*/69static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)70{71ce->ce_ctime.sec = htonl(st->st_ctime);72ce->ce_mtime.sec = htonl(st->st_mtime);73#ifdef NSEC74ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);75ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);76#endif77ce->ce_dev = htonl(st->st_dev);78ce->ce_ino = htonl(st->st_ino);79ce->ce_uid = htonl(st->st_uid);80ce->ce_gid = htonl(st->st_gid);81ce->ce_size = htonl(st->st_size);82}8384static int add_file_to_cache(char *path)85{86int size, namelen;87struct cache_entry *ce;88struct stat st;89int fd;9091fd = open(path, O_RDONLY);92if (fd < 0) {93if (errno == ENOENT) {94if (allow_remove)95return remove_file_from_cache(path);96}97return -1;98}99if (fstat(fd, &st) < 0) {100close(fd);101return -1;102}103namelen = strlen(path);104size = cache_entry_size(namelen);105ce = malloc(size);106memset(ce, 0, size);107memcpy(ce->name, path, namelen);108fill_stat_cache_info(ce, &st);109ce->ce_mode = htonl(st.st_mode);110ce->ce_namelen = htons(namelen);111112if (index_fd(path, namelen, ce, fd, &st) < 0)113return -1;114115return add_cache_entry(ce, allow_add);116}117118static int match_data(int fd, void *buffer, unsigned long size)119{120while (size) {121char compare[1024];122int ret = read(fd, compare, sizeof(compare));123124if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))125return -1;126size -= ret;127buffer += ret;128}129return 0;130}131132static int compare_data(struct cache_entry *ce, unsigned long expected_size)133{134int match = -1;135int fd = open(ce->name, O_RDONLY);136137if (fd >= 0) {138void *buffer;139unsigned long size;140char type[10];141142buffer = read_sha1_file(ce->sha1, type, &size);143if (buffer) {144if (size == expected_size && !strcmp(type, "blob"))145match = match_data(fd, buffer, size);146free(buffer);147}148close(fd);149}150return match;151}152153/*154* "refresh" does not calculate a new sha1 file or bring the155* cache up-to-date for mode/content changes. But what it156* _does_ do is to "re-match" the stat information of a file157* with the cache, so that you can refresh the cache for a158* file that hasn't been changed but where the stat entry is159* out of date.160*161* For example, you'd want to do this after doing a "read-tree",162* to link up the stat cache details with the proper files.163*/164static struct cache_entry *refresh_entry(struct cache_entry *ce)165{166struct stat st;167struct cache_entry *updated;168int changed, size;169170if (stat(ce->name, &st) < 0)171return NULL;172173changed = cache_match_stat(ce, &st);174if (!changed)175return ce;176177/*178* If the mode has changed, there's no point in trying179* to refresh the entry - it's not going to match180*/181if (changed & MODE_CHANGED)182return NULL;183184if (compare_data(ce, st.st_size))185return NULL;186187size = ce_size(ce);188updated = malloc(size);189memcpy(updated, ce, size);190fill_stat_cache_info(updated, &st);191return updated;192}193194static void refresh_cache(void)195{196int i;197198for (i = 0; i < active_nr; i++) {199struct cache_entry *ce = active_cache[i];200struct cache_entry *new = refresh_entry(ce);201202if (!new) {203printf("%s: needs update\n", ce->name);204continue;205}206active_cache[i] = new;207}208}209210/*211* We fundamentally don't like some paths: we don't want212* dot or dot-dot anywhere, and in fact, we don't even want213* any other dot-files (.git or anything else). They214* are hidden, for chist sake.215*216* Also, we don't want double slashes or slashes at the217* end that can make pathnames ambiguous.218*/219static int verify_path(char *path)220{221char c;222223goto inside;224for (;;) {225if (!c)226return 1;227if (c == '/') {228inside:229c = *path++;230if (c != '/' && c != '.' && c != '\0')231continue;232return 0;233}234c = *path++;235}236}237238static int remove_lock = 0;239240static void remove_lock_file(void)241{242if (remove_lock)243unlink(".git/index.lock");244}245246int main(int argc, char **argv)247{248int i, newfd, entries;249int allow_options = 1;250251newfd = open(".git/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);252if (newfd < 0)253die("unable to create new cachefile");254255atexit(remove_lock_file);256remove_lock = 1;257258entries = read_cache();259if (entries < 0)260die("cache corrupted");261262for (i = 1 ; i < argc; i++) {263char *path = argv[i];264265if (allow_options && *path == '-') {266if (!strcmp(path, "--")) {267allow_options = 0;268continue;269}270if (!strcmp(path, "--add")) {271allow_add = 1;272continue;273}274if (!strcmp(path, "--remove")) {275allow_remove = 1;276continue;277}278if (!strcmp(path, "--refresh")) {279refresh_cache();280continue;281}282die("unknown option %s", path);283}284if (!verify_path(path)) {285fprintf(stderr, "Ignoring path %s\n", argv[i]);286continue;287}288if (add_file_to_cache(path))289die("Unable to add %s to database", path);290}291if (write_cache(newfd, active_cache, active_nr) ||292rename(".git/index.lock", ".git/index"))293die("Unable to write new cachefile");294295remove_lock = 0;296return 0;297}