]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
2de381f9 | 6 | #include <stdarg.h> |
e83c5163 LT |
7 | #include "cache.h" |
8 | ||
e83c5163 | 9 | struct cache_entry **active_cache = NULL; |
ee267527 | 10 | unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0; |
e83c5163 | 11 | |
734aab75 LT |
12 | int cache_match_stat(struct cache_entry *ce, struct stat *st) |
13 | { | |
14 | unsigned int changed = 0; | |
15 | ||
8ae0a8c5 KS |
16 | switch (ntohl(ce->ce_mode) & S_IFMT) { |
17 | case S_IFREG: | |
18 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; | |
ffbe1add KS |
19 | /* We consider only the owner x bit to be relevant for "mode changes" */ |
20 | if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)) | |
21 | changed |= MODE_CHANGED; | |
8ae0a8c5 KS |
22 | break; |
23 | case S_IFLNK: | |
24 | changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0; | |
25 | break; | |
26 | default: | |
27 | die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); | |
28 | } | |
ccc4feb5 | 29 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
734aab75 | 30 | changed |= MTIME_CHANGED; |
ccc4feb5 LT |
31 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
32 | changed |= CTIME_CHANGED; | |
33 | ||
bdd4da59 | 34 | #ifdef NSEC |
ccc4feb5 LT |
35 | /* |
36 | * nsec seems unreliable - not all filesystems support it, so | |
37 | * as long as it is in the inode cache you get right nsec | |
38 | * but after it gets flushed, you get zero nsec. | |
39 | */ | |
94dfb7f2 | 40 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)) |
ccc4feb5 | 41 | changed |= MTIME_CHANGED; |
94dfb7f2 | 42 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)) |
734aab75 | 43 | changed |= CTIME_CHANGED; |
ccc4feb5 LT |
44 | #endif |
45 | ||
46 | if (ce->ce_uid != htonl(st->st_uid) || | |
47 | ce->ce_gid != htonl(st->st_gid)) | |
734aab75 | 48 | changed |= OWNER_CHANGED; |
ccc4feb5 LT |
49 | if (ce->ce_dev != htonl(st->st_dev) || |
50 | ce->ce_ino != htonl(st->st_ino)) | |
734aab75 | 51 | changed |= INODE_CHANGED; |
ccc4feb5 | 52 | if (ce->ce_size != htonl(st->st_size)) |
734aab75 LT |
53 | changed |= DATA_CHANGED; |
54 | return changed; | |
55 | } | |
56 | ||
95fd5bf8 | 57 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
eb38c22f | 58 | { |
95fd5bf8 LT |
59 | int len1 = flags1 & CE_NAMEMASK; |
60 | int len2 = flags2 & CE_NAMEMASK; | |
eb38c22f LT |
61 | int len = len1 < len2 ? len1 : len2; |
62 | int cmp; | |
63 | ||
64 | cmp = memcmp(name1, name2, len); | |
65 | if (cmp) | |
66 | return cmp; | |
67 | if (len1 < len2) | |
68 | return -1; | |
69 | if (len1 > len2) | |
70 | return 1; | |
95fd5bf8 LT |
71 | if (flags1 < flags2) |
72 | return -1; | |
73 | if (flags1 > flags2) | |
74 | return 1; | |
eb38c22f LT |
75 | return 0; |
76 | } | |
77 | ||
78 | int cache_name_pos(const char *name, int namelen) | |
79 | { | |
80 | int first, last; | |
81 | ||
82 | first = 0; | |
83 | last = active_nr; | |
84 | while (last > first) { | |
85 | int next = (last + first) >> 1; | |
86 | struct cache_entry *ce = active_cache[next]; | |
95fd5bf8 | 87 | int cmp = cache_name_compare(name, namelen, ce->name, htons(ce->ce_flags)); |
eb38c22f | 88 | if (!cmp) |
76e7f4ec | 89 | return next; |
eb38c22f LT |
90 | if (cmp < 0) { |
91 | last = next; | |
92 | continue; | |
93 | } | |
94 | first = next+1; | |
95 | } | |
76e7f4ec | 96 | return -first-1; |
eb38c22f LT |
97 | } |
98 | ||
7b937ca3 | 99 | /* Remove entry, return true if there are more entries to go.. */ |
b5af9107 | 100 | int remove_entry_at(int pos) |
7b937ca3 | 101 | { |
ee267527 | 102 | active_cache_changed = 1; |
7b937ca3 LT |
103 | active_nr--; |
104 | if (pos >= active_nr) | |
105 | return 0; | |
106 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
107 | return 1; | |
108 | } | |
109 | ||
197ee8c9 LT |
110 | int remove_file_from_cache(char *path) |
111 | { | |
112 | int pos = cache_name_pos(path, strlen(path)); | |
c4e3cca1 JH |
113 | if (pos < 0) |
114 | pos = -pos-1; | |
115 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) | |
7b937ca3 | 116 | remove_entry_at(pos); |
197ee8c9 LT |
117 | return 0; |
118 | } | |
119 | ||
b5af9107 | 120 | int same_name(struct cache_entry *a, struct cache_entry *b) |
7b937ca3 LT |
121 | { |
122 | int len = ce_namelen(a); | |
123 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
124 | } | |
125 | ||
121481ab | 126 | int add_cache_entry(struct cache_entry *ce, int ok_to_add) |
197ee8c9 LT |
127 | { |
128 | int pos; | |
129 | ||
95fd5bf8 | 130 | pos = cache_name_pos(ce->name, htons(ce->ce_flags)); |
197ee8c9 LT |
131 | |
132 | /* existing match? Just replace it */ | |
76e7f4ec | 133 | if (pos >= 0) { |
ee267527 | 134 | active_cache_changed = 1; |
76e7f4ec | 135 | active_cache[pos] = ce; |
197ee8c9 LT |
136 | return 0; |
137 | } | |
76e7f4ec | 138 | pos = -pos-1; |
197ee8c9 | 139 | |
7b937ca3 LT |
140 | /* |
141 | * Inserting a merged entry ("stage 0") into the index | |
142 | * will always replace all non-merged entries.. | |
143 | */ | |
144 | if (pos < active_nr && ce_stage(ce) == 0) { | |
145 | while (same_name(active_cache[pos], ce)) { | |
146 | ok_to_add = 1; | |
7b937ca3 LT |
147 | if (!remove_entry_at(pos)) |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
121481ab LT |
152 | if (!ok_to_add) |
153 | return -1; | |
154 | ||
197ee8c9 LT |
155 | /* Make sure the array is big enough .. */ |
156 | if (active_nr == active_alloc) { | |
157 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 158 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
159 | } |
160 | ||
161 | /* Add it in.. */ | |
162 | active_nr++; | |
163 | if (active_nr > pos) | |
164 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
165 | active_cache[pos] = ce; | |
ee267527 | 166 | active_cache_changed = 1; |
197ee8c9 LT |
167 | return 0; |
168 | } | |
169 | ||
e83c5163 LT |
170 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
171 | { | |
172 | SHA_CTX c; | |
173 | unsigned char sha1[20]; | |
174 | ||
ccc4feb5 | 175 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 176 | return error("bad signature"); |
ca9be054 LT |
177 | if (hdr->hdr_version != htonl(2)) |
178 | return error("bad index version"); | |
e83c5163 | 179 | SHA1_Init(&c); |
ca9be054 | 180 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 181 | SHA1_Final(sha1, &c); |
ca9be054 LT |
182 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
183 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
184 | return 0; |
185 | } | |
186 | ||
187 | int read_cache(void) | |
188 | { | |
189 | int fd, i; | |
190 | struct stat st; | |
191 | unsigned long size, offset; | |
192 | void *map; | |
193 | struct cache_header *hdr; | |
194 | ||
195 | errno = EBUSY; | |
196 | if (active_cache) | |
197 | return error("more than one cachefile"); | |
198 | errno = ENOENT; | |
bb233d69 | 199 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
200 | if (fd < 0) |
201 | return (errno == ENOENT) ? 0 : error("open failed"); | |
202 | ||
19b2860c | 203 | size = 0; // avoid gcc warning |
e83c5163 LT |
204 | map = (void *)-1; |
205 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
206 | size = st.st_size; |
207 | errno = EINVAL; | |
ca9be054 | 208 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 209 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
210 | } |
211 | close(fd); | |
212 | if (-1 == (int)(long)map) | |
213 | return error("mmap failed"); | |
214 | ||
215 | hdr = map; | |
216 | if (verify_hdr(hdr, size) < 0) | |
217 | goto unmap; | |
218 | ||
ccc4feb5 | 219 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
220 | active_alloc = alloc_nr(active_nr); |
221 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
222 | ||
223 | offset = sizeof(*hdr); | |
ccc4feb5 | 224 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
225 | struct cache_entry *ce = map + offset; |
226 | offset = offset + ce_size(ce); | |
227 | active_cache[i] = ce; | |
228 | } | |
229 | return active_nr; | |
230 | ||
231 | unmap: | |
232 | munmap(map, size); | |
233 | errno = EINVAL; | |
234 | return error("verify header failed"); | |
235 | } | |
236 | ||
4990aadc LT |
237 | #define WRITE_BUFFER_SIZE 8192 |
238 | static char write_buffer[WRITE_BUFFER_SIZE]; | |
239 | static unsigned long write_buffer_len; | |
240 | ||
ca9be054 | 241 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
242 | { |
243 | while (len) { | |
244 | unsigned int buffered = write_buffer_len; | |
245 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
246 | if (partial > len) | |
247 | partial = len; | |
248 | memcpy(write_buffer + buffered, data, partial); | |
249 | buffered += partial; | |
250 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 251 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
252 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
253 | return -1; | |
254 | buffered = 0; | |
255 | } | |
256 | write_buffer_len = buffered; | |
257 | len -= partial; | |
258 | data += partial; | |
259 | } | |
260 | return 0; | |
261 | } | |
262 | ||
ca9be054 | 263 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
264 | { |
265 | unsigned int left = write_buffer_len; | |
ca9be054 | 266 | |
4990aadc LT |
267 | if (left) { |
268 | write_buffer_len = 0; | |
ca9be054 | 269 | SHA1_Update(context, write_buffer, left); |
4990aadc | 270 | } |
ca9be054 LT |
271 | |
272 | /* Append the SHA1 signature at the end */ | |
273 | SHA1_Final(write_buffer + left, context); | |
274 | left += 20; | |
275 | if (write(fd, write_buffer, left) != left) | |
276 | return -1; | |
4990aadc LT |
277 | return 0; |
278 | } | |
279 | ||
197ee8c9 LT |
280 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
281 | { | |
282 | SHA_CTX c; | |
283 | struct cache_header hdr; | |
284 | int i; | |
285 | ||
ccc4feb5 | 286 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 287 | hdr.hdr_version = htonl(2); |
ccc4feb5 | 288 | hdr.hdr_entries = htonl(entries); |
197ee8c9 LT |
289 | |
290 | SHA1_Init(&c); | |
ca9be054 | 291 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
292 | return -1; |
293 | ||
294 | for (i = 0; i < entries; i++) { | |
295 | struct cache_entry *ce = cache[i]; | |
ca9be054 | 296 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
297 | return -1; |
298 | } | |
ca9be054 | 299 | return ce_flush(&c, newfd); |
197ee8c9 | 300 | } |