]>
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 LT |
9 | struct cache_entry **active_cache = NULL; |
10 | unsigned int active_nr = 0, active_alloc = 0; | |
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; | |
19 | break; | |
20 | case S_IFLNK: | |
21 | changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0; | |
22 | break; | |
23 | default: | |
24 | die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); | |
25 | } | |
ccc4feb5 | 26 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
734aab75 | 27 | changed |= MTIME_CHANGED; |
ccc4feb5 LT |
28 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
29 | changed |= CTIME_CHANGED; | |
30 | ||
bdd4da59 | 31 | #ifdef NSEC |
ccc4feb5 LT |
32 | /* |
33 | * nsec seems unreliable - not all filesystems support it, so | |
34 | * as long as it is in the inode cache you get right nsec | |
35 | * but after it gets flushed, you get zero nsec. | |
36 | */ | |
94dfb7f2 | 37 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)) |
ccc4feb5 | 38 | changed |= MTIME_CHANGED; |
94dfb7f2 | 39 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)) |
734aab75 | 40 | changed |= CTIME_CHANGED; |
ccc4feb5 LT |
41 | #endif |
42 | ||
43 | if (ce->ce_uid != htonl(st->st_uid) || | |
44 | ce->ce_gid != htonl(st->st_gid)) | |
734aab75 | 45 | changed |= OWNER_CHANGED; |
e4479470 | 46 | /* We consider only the owner x bit to be relevant for "mode changes" */ |
e8871e88 | 47 | if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)) |
734aab75 | 48 | changed |= MODE_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 LT |
101 | { |
102 | active_nr--; | |
103 | if (pos >= active_nr) | |
104 | return 0; | |
105 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
106 | return 1; | |
107 | } | |
108 | ||
197ee8c9 LT |
109 | int remove_file_from_cache(char *path) |
110 | { | |
111 | int pos = cache_name_pos(path, strlen(path)); | |
c4e3cca1 JH |
112 | if (pos < 0) |
113 | pos = -pos-1; | |
114 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) | |
7b937ca3 | 115 | remove_entry_at(pos); |
197ee8c9 LT |
116 | return 0; |
117 | } | |
118 | ||
b5af9107 | 119 | int same_name(struct cache_entry *a, struct cache_entry *b) |
7b937ca3 LT |
120 | { |
121 | int len = ce_namelen(a); | |
122 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
123 | } | |
124 | ||
121481ab | 125 | int add_cache_entry(struct cache_entry *ce, int ok_to_add) |
197ee8c9 LT |
126 | { |
127 | int pos; | |
128 | ||
95fd5bf8 | 129 | pos = cache_name_pos(ce->name, htons(ce->ce_flags)); |
197ee8c9 LT |
130 | |
131 | /* existing match? Just replace it */ | |
76e7f4ec LT |
132 | if (pos >= 0) { |
133 | active_cache[pos] = ce; | |
197ee8c9 LT |
134 | return 0; |
135 | } | |
76e7f4ec | 136 | pos = -pos-1; |
197ee8c9 | 137 | |
7b937ca3 LT |
138 | /* |
139 | * Inserting a merged entry ("stage 0") into the index | |
140 | * will always replace all non-merged entries.. | |
141 | */ | |
142 | if (pos < active_nr && ce_stage(ce) == 0) { | |
143 | while (same_name(active_cache[pos], ce)) { | |
144 | ok_to_add = 1; | |
7b937ca3 LT |
145 | if (!remove_entry_at(pos)) |
146 | break; | |
147 | } | |
148 | } | |
149 | ||
121481ab LT |
150 | if (!ok_to_add) |
151 | return -1; | |
152 | ||
197ee8c9 LT |
153 | /* Make sure the array is big enough .. */ |
154 | if (active_nr == active_alloc) { | |
155 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 156 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
157 | } |
158 | ||
159 | /* Add it in.. */ | |
160 | active_nr++; | |
161 | if (active_nr > pos) | |
162 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
163 | active_cache[pos] = ce; | |
164 | return 0; | |
165 | } | |
166 | ||
e83c5163 LT |
167 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
168 | { | |
169 | SHA_CTX c; | |
170 | unsigned char sha1[20]; | |
171 | ||
ccc4feb5 | 172 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 173 | return error("bad signature"); |
ca9be054 LT |
174 | if (hdr->hdr_version != htonl(2)) |
175 | return error("bad index version"); | |
e83c5163 | 176 | SHA1_Init(&c); |
ca9be054 | 177 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 178 | SHA1_Final(sha1, &c); |
ca9be054 LT |
179 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
180 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
181 | return 0; |
182 | } | |
183 | ||
184 | int read_cache(void) | |
185 | { | |
186 | int fd, i; | |
187 | struct stat st; | |
188 | unsigned long size, offset; | |
189 | void *map; | |
190 | struct cache_header *hdr; | |
191 | ||
192 | errno = EBUSY; | |
193 | if (active_cache) | |
194 | return error("more than one cachefile"); | |
195 | errno = ENOENT; | |
196 | sha1_file_directory = getenv(DB_ENVIRONMENT); | |
197 | if (!sha1_file_directory) | |
198 | sha1_file_directory = DEFAULT_DB_ENVIRONMENT; | |
199 | if (access(sha1_file_directory, X_OK) < 0) | |
200 | return error("no access to SHA1 file directory"); | |
bb233d69 | 201 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
202 | if (fd < 0) |
203 | return (errno == ENOENT) ? 0 : error("open failed"); | |
204 | ||
19b2860c | 205 | size = 0; // avoid gcc warning |
e83c5163 LT |
206 | map = (void *)-1; |
207 | if (!fstat(fd, &st)) { | |
e83c5163 LT |
208 | size = st.st_size; |
209 | errno = EINVAL; | |
ca9be054 | 210 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 211 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
212 | } |
213 | close(fd); | |
214 | if (-1 == (int)(long)map) | |
215 | return error("mmap failed"); | |
216 | ||
217 | hdr = map; | |
218 | if (verify_hdr(hdr, size) < 0) | |
219 | goto unmap; | |
220 | ||
ccc4feb5 | 221 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
222 | active_alloc = alloc_nr(active_nr); |
223 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
224 | ||
225 | offset = sizeof(*hdr); | |
ccc4feb5 | 226 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
227 | struct cache_entry *ce = map + offset; |
228 | offset = offset + ce_size(ce); | |
229 | active_cache[i] = ce; | |
230 | } | |
231 | return active_nr; | |
232 | ||
233 | unmap: | |
234 | munmap(map, size); | |
235 | errno = EINVAL; | |
236 | return error("verify header failed"); | |
237 | } | |
238 | ||
4990aadc LT |
239 | #define WRITE_BUFFER_SIZE 8192 |
240 | static char write_buffer[WRITE_BUFFER_SIZE]; | |
241 | static unsigned long write_buffer_len; | |
242 | ||
ca9be054 | 243 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
244 | { |
245 | while (len) { | |
246 | unsigned int buffered = write_buffer_len; | |
247 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
248 | if (partial > len) | |
249 | partial = len; | |
250 | memcpy(write_buffer + buffered, data, partial); | |
251 | buffered += partial; | |
252 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 253 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
254 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
255 | return -1; | |
256 | buffered = 0; | |
257 | } | |
258 | write_buffer_len = buffered; | |
259 | len -= partial; | |
260 | data += partial; | |
261 | } | |
262 | return 0; | |
263 | } | |
264 | ||
ca9be054 | 265 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
266 | { |
267 | unsigned int left = write_buffer_len; | |
ca9be054 | 268 | |
4990aadc LT |
269 | if (left) { |
270 | write_buffer_len = 0; | |
ca9be054 | 271 | SHA1_Update(context, write_buffer, left); |
4990aadc | 272 | } |
ca9be054 LT |
273 | |
274 | /* Append the SHA1 signature at the end */ | |
275 | SHA1_Final(write_buffer + left, context); | |
276 | left += 20; | |
277 | if (write(fd, write_buffer, left) != left) | |
278 | return -1; | |
4990aadc LT |
279 | return 0; |
280 | } | |
281 | ||
197ee8c9 LT |
282 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
283 | { | |
284 | SHA_CTX c; | |
285 | struct cache_header hdr; | |
286 | int i; | |
287 | ||
ccc4feb5 | 288 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 289 | hdr.hdr_version = htonl(2); |
ccc4feb5 | 290 | hdr.hdr_entries = htonl(entries); |
197ee8c9 LT |
291 | |
292 | SHA1_Init(&c); | |
ca9be054 | 293 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
294 | return -1; |
295 | ||
296 | for (i = 0; i < entries; i++) { | |
297 | struct cache_entry *ce = cache[i]; | |
ca9be054 | 298 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
299 | return -1; |
300 | } | |
ca9be054 | 301 | return ce_flush(&c, newfd); |
197ee8c9 | 302 | } |