2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
10 int len = len1 < len2 ? len1 : len2;
13 cmp = memcmp(name1, name2, len);
23 static int cache_name_pos(const char *name, int namelen)
29 while (last > first) {
30 int next = (last + first) >> 1;
31 struct cache_entry *ce = active_cache[next];
32 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
44 static int remove_file_from_cache(char *path)
46 int pos = cache_name_pos(path, strlen(path));
51 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
55 static int add_cache_entry(struct cache_entry *ce)
59 pos = cache_name_pos(ce->name, ce->namelen);
61 /* existing match? Just replace it */
63 active_cache[-pos-1] = ce;
67 /* Make sure the array is big enough .. */
68 if (active_nr == active_alloc) {
69 active_alloc = alloc_nr(active_alloc);
70 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
76 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
77 active_cache[pos] = ce;
81 static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
84 int max_out_bytes = namelen + st->st_size + 200;
85 void *out = malloc(max_out_bytes);
86 void *metadata = malloc(namelen + 200);
87 void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
91 if (!out || (int)(long)in == -1)
94 memset(&stream, 0, sizeof(stream));
95 deflateInit(&stream, Z_BEST_COMPRESSION);
98 * ASCII size + nul byte
100 stream.next_in = metadata;
101 stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
102 stream.next_out = out;
103 stream.avail_out = max_out_bytes;
104 while (deflate(&stream, 0) == Z_OK)
111 stream.avail_in = st->st_size;
112 while (deflate(&stream, Z_FINISH) == Z_OK)
118 SHA1_Update(&c, out, stream.total_out);
119 SHA1_Final(ce->sha1, &c);
121 return write_sha1_buffer(ce->sha1, out, stream.total_out);
124 static int add_file_to_cache(char *path)
127 struct cache_entry *ce;
131 fd = open(path, O_RDONLY);
134 return remove_file_from_cache(path);
137 if (fstat(fd, &st) < 0) {
141 namelen = strlen(path);
142 size = cache_entry_size(namelen);
145 memcpy(ce->name, path, namelen);
146 ce->ctime.sec = st.st_ctime;
147 ce->ctime.nsec = st.st_ctim.tv_nsec;
148 ce->mtime.sec = st.st_mtime;
149 ce->mtime.nsec = st.st_mtim.tv_nsec;
150 ce->st_dev = st.st_dev;
151 ce->st_ino = st.st_ino;
152 ce->st_mode = st.st_mode;
153 ce->st_uid = st.st_uid;
154 ce->st_gid = st.st_gid;
155 ce->st_size = st.st_size;
156 ce->namelen = namelen;
158 if (index_fd(path, namelen, ce, fd, &st) < 0)
161 return add_cache_entry(ce);
164 static int write_cache(int newfd, struct cache_entry **cache, int entries)
167 struct cache_header hdr;
170 hdr.signature = CACHE_SIGNATURE;
172 hdr.entries = entries;
175 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
176 for (i = 0; i < entries; i++) {
177 struct cache_entry *ce = cache[i];
178 int size = ce_size(ce);
179 SHA1_Update(&c, ce, size);
181 SHA1_Final(hdr.sha1, &c);
183 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
186 for (i = 0; i < entries; i++) {
187 struct cache_entry *ce = cache[i];
188 int size = ce_size(ce);
189 if (write(newfd, ce, size) != size)
196 * We fundamentally don't like some paths: we don't want
197 * dot or dot-dot anywhere, and in fact, we don't even want
198 * any other dot-files (.dircache or anything else). They
199 * are hidden, for chist sake.
201 * Also, we don't want double slashes or slashes at the
202 * end that can make pathnames ambiguous.
204 static int verify_path(char *path)
215 if (c != '/' && c != '.' && c != '\0')
223 int main(int argc, char **argv)
225 int i, newfd, entries;
227 entries = read_cache();
229 perror("cache corrupted");
233 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
235 perror("unable to create new cachefile");
238 for (i = 1 ; i < argc; i++) {
239 char *path = argv[i];
240 if (!verify_path(path)) {
241 fprintf(stderr, "Ignoring path %s\n", argv[i]);
244 if (add_file_to_cache(path)) {
245 fprintf(stderr, "Unable to add %s to database\n", path);
249 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
252 unlink(".dircache/index.lock");