3 static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
5 int len = len1 < len2 ? len1 : len2;
8 cmp = memcmp(name1, name2, len);
18 static int cache_name_pos(const char *name, int namelen)
24 while (last > first) {
25 int next = (last + first) >> 1;
26 struct cache_entry *ce = active_cache[next];
27 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
39 static int remove_file_from_cache(char *path)
41 int pos = cache_name_pos(path, strlen(path));
46 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
50 static int add_cache_entry(struct cache_entry *ce)
54 pos = cache_name_pos(ce->name, ce->namelen);
56 /* existing match? Just replace it */
58 active_cache[-pos-1] = ce;
62 /* Make sure the array is big enough .. */
63 if (active_nr == active_alloc) {
64 active_alloc = alloc_nr(active_alloc);
65 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
71 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
72 active_cache[pos] = ce;
76 static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
79 int max_out_bytes = namelen + st->st_size + 200;
80 void *out = malloc(max_out_bytes);
81 void *metadata = malloc(namelen + 200);
82 void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
86 if (!out || (int)(long)in == -1)
89 memset(&stream, 0, sizeof(stream));
90 deflateInit(&stream, Z_BEST_COMPRESSION);
93 * ASCII size + nul byte
95 stream.next_in = metadata;
96 stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
97 stream.next_out = out;
98 stream.avail_out = max_out_bytes;
99 while (deflate(&stream, 0) == Z_OK)
106 stream.avail_in = st->st_size;
107 while (deflate(&stream, Z_FINISH) == Z_OK)
113 SHA1_Update(&c, out, stream.total_out);
114 SHA1_Final(ce->sha1, &c);
116 return write_sha1_buffer(ce->sha1, out, stream.total_out);
119 static int add_file_to_cache(char *path)
122 struct cache_entry *ce;
126 fd = open(path, O_RDONLY);
129 return remove_file_from_cache(path);
132 if (fstat(fd, &st) < 0) {
136 namelen = strlen(path);
137 size = cache_entry_size(namelen);
140 memcpy(ce->name, path, namelen);
141 ce->ctime.sec = st.st_ctime;
142 ce->ctime.nsec = st.st_ctim.tv_nsec;
143 ce->mtime.sec = st.st_mtime;
144 ce->mtime.nsec = st.st_mtim.tv_nsec;
145 ce->st_dev = st.st_dev;
146 ce->st_ino = st.st_ino;
147 ce->st_mode = st.st_mode;
148 ce->st_uid = st.st_uid;
149 ce->st_gid = st.st_gid;
150 ce->st_size = st.st_size;
151 ce->namelen = namelen;
153 if (index_fd(path, namelen, ce, fd, &st) < 0)
156 return add_cache_entry(ce);
159 static int write_cache(int newfd, struct cache_entry **cache, int entries)
162 struct cache_header hdr;
165 hdr.signature = CACHE_SIGNATURE;
167 hdr.entries = entries;
170 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
171 for (i = 0; i < entries; i++) {
172 struct cache_entry *ce = cache[i];
173 int size = ce_size(ce);
174 SHA1_Update(&c, ce, size);
176 SHA1_Final(hdr.sha1, &c);
178 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
181 for (i = 0; i < entries; i++) {
182 struct cache_entry *ce = cache[i];
183 int size = ce_size(ce);
184 if (write(newfd, ce, size) != size)
191 * We fundamentally don't like some paths: we don't want
192 * dot or dot-dot anywhere, and in fact, we don't even want
193 * any other dot-files (.dircache or anything else). They
194 * are hidden, for chist sake.
196 * Also, we don't want double slashes or slashes at the
197 * end that can make pathnames ambiguous.
199 static int verify_path(char *path)
210 if (c != '/' && c != '.' && c != '\0')
218 int main(int argc, char **argv)
220 int i, newfd, entries;
222 entries = read_cache();
224 perror("cache corrupted");
228 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
230 perror("unable to create new cachefile");
233 for (i = 1 ; i < argc; i++) {
234 char *path = argv[i];
235 if (!verify_path(path)) {
236 fprintf(stderr, "Ignoring path %s\n", argv[i]);
239 if (add_file_to_cache(path)) {
240 fprintf(stderr, "Unable to add %s to database\n", path);
244 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
247 unlink(".dircache/index.lock");