[PATCH] show-diff.c: make diff options customizable.
[git/debian.git] / read-cache.c
blobbfa2de767a9be1dfb4c53ebc21784741c1f49b77
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include <stdarg.h>
7 #include "cache.h"
9 struct cache_entry **active_cache = NULL;
10 unsigned int active_nr = 0, active_alloc = 0;
12 int cache_match_stat(struct cache_entry *ce, struct stat *st)
14 unsigned int changed = 0;
16 if (ce->ce_mtime.sec != htonl(st->st_mtime))
17 changed |= MTIME_CHANGED;
18 if (ce->ce_ctime.sec != htonl(st->st_ctime))
19 changed |= CTIME_CHANGED;
21 #ifdef NSEC
23 * nsec seems unreliable - not all filesystems support it, so
24 * as long as it is in the inode cache you get right nsec
25 * but after it gets flushed, you get zero nsec.
27 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)
28 changed |= MTIME_CHANGED;
29 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)
30 changed |= CTIME_CHANGED;
31 #endif
33 if (ce->ce_uid != htonl(st->st_uid) ||
34 ce->ce_gid != htonl(st->st_gid))
35 changed |= OWNER_CHANGED;
36 /* We consider only the owner x bit to be relevant for "mode changes" */
37 if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
38 changed |= MODE_CHANGED;
39 if (ce->ce_dev != htonl(st->st_dev) ||
40 ce->ce_ino != htonl(st->st_ino))
41 changed |= INODE_CHANGED;
42 if (ce->ce_size != htonl(st->st_size))
43 changed |= DATA_CHANGED;
44 return changed;
47 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
49 int len1 = flags1 & CE_NAMEMASK;
50 int len2 = flags2 & CE_NAMEMASK;
51 int len = len1 < len2 ? len1 : len2;
52 int cmp;
54 cmp = memcmp(name1, name2, len);
55 if (cmp)
56 return cmp;
57 if (len1 < len2)
58 return -1;
59 if (len1 > len2)
60 return 1;
61 if (flags1 < flags2)
62 return -1;
63 if (flags1 > flags2)
64 return 1;
65 return 0;
68 int cache_name_pos(const char *name, int namelen)
70 int first, last;
72 first = 0;
73 last = active_nr;
74 while (last > first) {
75 int next = (last + first) >> 1;
76 struct cache_entry *ce = active_cache[next];
77 int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags));
78 if (!cmp)
79 return next;
80 if (cmp < 0) {
81 last = next;
82 continue;
84 first = next+1;
86 return -first-1;
89 /* Remove entry, return true if there are more entries to go.. */
90 static int remove_entry_at(int pos)
92 active_nr--;
93 if (pos >= active_nr)
94 return 0;
95 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
96 return 1;
99 int remove_file_from_cache(char *path)
101 int pos = cache_name_pos(path, strlen(path));
102 if (pos < 0)
103 pos = -pos-1;
104 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
105 remove_entry_at(pos);
106 return 0;
109 static int same_name(struct cache_entry *a, struct cache_entry *b)
111 int len = ce_namelen(a);
112 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
115 int add_cache_entry(struct cache_entry *ce, int ok_to_add)
117 int pos;
119 pos = cache_name_pos(ce->name, htons(ce->ce_flags));
121 /* existing match? Just replace it */
122 if (pos >= 0) {
123 active_cache[pos] = ce;
124 return 0;
126 pos = -pos-1;
129 * Inserting a merged entry ("stage 0") into the index
130 * will always replace all non-merged entries..
132 if (pos < active_nr && ce_stage(ce) == 0) {
133 while (same_name(active_cache[pos], ce)) {
134 ok_to_add = 1;
135 if (!remove_entry_at(pos))
136 break;
140 if (!ok_to_add)
141 return -1;
143 /* Make sure the array is big enough .. */
144 if (active_nr == active_alloc) {
145 active_alloc = alloc_nr(active_alloc);
146 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
149 /* Add it in.. */
150 active_nr++;
151 if (active_nr > pos)
152 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
153 active_cache[pos] = ce;
154 return 0;
157 static int verify_hdr(struct cache_header *hdr, unsigned long size)
159 SHA_CTX c;
160 unsigned char sha1[20];
162 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
163 return error("bad signature");
164 if (hdr->hdr_version != htonl(1))
165 return error("bad version");
166 SHA1_Init(&c);
167 SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
168 SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
169 SHA1_Final(sha1, &c);
170 if (memcmp(sha1, hdr->sha1, 20))
171 return error("bad header sha1");
172 return 0;
175 int read_cache(void)
177 int fd, i;
178 struct stat st;
179 unsigned long size, offset;
180 void *map;
181 struct cache_header *hdr;
183 errno = EBUSY;
184 if (active_cache)
185 return error("more than one cachefile");
186 errno = ENOENT;
187 sha1_file_directory = getenv(DB_ENVIRONMENT);
188 if (!sha1_file_directory)
189 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
190 if (access(sha1_file_directory, X_OK) < 0)
191 return error("no access to SHA1 file directory");
192 fd = open(".git/index", O_RDONLY);
193 if (fd < 0)
194 return (errno == ENOENT) ? 0 : error("open failed");
196 size = 0; // avoid gcc warning
197 map = (void *)-1;
198 if (!fstat(fd, &st)) {
199 size = st.st_size;
200 errno = EINVAL;
201 if (size >= sizeof(struct cache_header))
202 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
204 close(fd);
205 if (-1 == (int)(long)map)
206 return error("mmap failed");
208 hdr = map;
209 if (verify_hdr(hdr, size) < 0)
210 goto unmap;
212 active_nr = ntohl(hdr->hdr_entries);
213 active_alloc = alloc_nr(active_nr);
214 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
216 offset = sizeof(*hdr);
217 for (i = 0; i < active_nr; i++) {
218 struct cache_entry *ce = map + offset;
219 offset = offset + ce_size(ce);
220 active_cache[i] = ce;
222 return active_nr;
224 unmap:
225 munmap(map, size);
226 errno = EINVAL;
227 return error("verify header failed");
230 int write_cache(int newfd, struct cache_entry **cache, int entries)
232 SHA_CTX c;
233 struct cache_header hdr;
234 int i;
236 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
237 hdr.hdr_version = htonl(1);
238 hdr.hdr_entries = htonl(entries);
240 SHA1_Init(&c);
241 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
242 for (i = 0; i < entries; i++) {
243 struct cache_entry *ce = cache[i];
244 int size = ce_size(ce);
245 SHA1_Update(&c, ce, size);
247 SHA1_Final(hdr.sha1, &c);
249 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
250 return -1;
252 for (i = 0; i < entries; i++) {
253 struct cache_entry *ce = cache[i];
254 int size = ce_size(ce);
255 if (write(newfd, ce, size) != size)
256 return -1;
258 return 0;