1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 /*
9 * Default to not allowing changes to the list of files. The
10 * tool doesn't actually care, but this makes it harder to add
11 * files to the revision control by mistake by doing something
12 * like "update-cache *" and suddenly having all the object
13 * files be revision controlled.
14 */
15 static int allow_add = 0, allow_remove = 0, not_new = 0;
17 /* Three functions to allow overloaded pointer return; see linux/err.h */
18 static inline void *ERR_PTR(long error)
19 {
20 return (void *) error;
21 }
23 static inline long PTR_ERR(const void *ptr)
24 {
25 return (long) ptr;
26 }
28 static inline long IS_ERR(const void *ptr)
29 {
30 return (unsigned long)ptr > (unsigned long)-1000L;
31 }
33 static int index_fd(unsigned char *sha1, int fd, struct stat *st)
34 {
35 z_stream stream;
36 unsigned long size = st->st_size;
37 int max_out_bytes = size + 200;
38 void *out = malloc(max_out_bytes);
39 void *metadata = malloc(200);
40 int metadata_size;
41 void *in;
42 SHA_CTX c;
44 in = "";
45 if (size)
46 in = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
47 close(fd);
48 if (!out || (int)(long)in == -1)
49 return -1;
51 metadata_size = 1+sprintf(metadata, "blob %lu", size);
53 SHA1_Init(&c);
54 SHA1_Update(&c, metadata, metadata_size);
55 SHA1_Update(&c, in, size);
56 SHA1_Final(sha1, &c);
58 memset(&stream, 0, sizeof(stream));
59 deflateInit(&stream, Z_BEST_COMPRESSION);
61 /*
62 * ASCII size + nul byte
63 */
64 stream.next_in = metadata;
65 stream.avail_in = metadata_size;
66 stream.next_out = out;
67 stream.avail_out = max_out_bytes;
68 while (deflate(&stream, 0) == Z_OK)
69 /* nothing */;
71 /*
72 * File content
73 */
74 stream.next_in = in;
75 stream.avail_in = size;
76 while (deflate(&stream, Z_FINISH) == Z_OK)
77 /*nothing */;
79 deflateEnd(&stream);
81 return write_sha1_buffer(sha1, out, stream.total_out);
82 }
84 /*
85 * This only updates the "non-critical" parts of the directory
86 * cache, ie the parts that aren't tracked by GIT, and only used
87 * to validate the cache.
88 */
89 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
90 {
91 ce->ce_ctime.sec = htonl(st->st_ctime);
92 ce->ce_mtime.sec = htonl(st->st_mtime);
93 #ifdef NSEC
94 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
95 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
96 #endif
97 ce->ce_dev = htonl(st->st_dev);
98 ce->ce_ino = htonl(st->st_ino);
99 ce->ce_uid = htonl(st->st_uid);
100 ce->ce_gid = htonl(st->st_gid);
101 ce->ce_size = htonl(st->st_size);
102 }
104 static int add_file_to_cache(char *path)
105 {
106 int size, namelen;
107 struct cache_entry *ce;
108 struct stat st;
109 int fd;
111 fd = open(path, O_RDONLY);
112 if (fd < 0) {
113 if (errno == ENOENT) {
114 if (allow_remove)
115 return remove_file_from_cache(path);
116 }
117 return -1;
118 }
119 if (fstat(fd, &st) < 0) {
120 close(fd);
121 return -1;
122 }
123 namelen = strlen(path);
124 size = cache_entry_size(namelen);
125 ce = malloc(size);
126 memset(ce, 0, size);
127 memcpy(ce->name, path, namelen);
128 fill_stat_cache_info(ce, &st);
129 ce->ce_mode = create_ce_mode(st.st_mode);
130 ce->ce_flags = htons(namelen);
132 if (index_fd(ce->sha1, fd, &st) < 0)
133 return -1;
135 return add_cache_entry(ce, allow_add);
136 }
138 static int match_data(int fd, void *buffer, unsigned long size)
139 {
140 while (size) {
141 char compare[1024];
142 int ret = read(fd, compare, sizeof(compare));
144 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
145 return -1;
146 size -= ret;
147 buffer += ret;
148 }
149 return 0;
150 }
152 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
153 {
154 int match = -1;
155 int fd = open(ce->name, O_RDONLY);
157 if (fd >= 0) {
158 void *buffer;
159 unsigned long size;
160 char type[10];
162 buffer = read_sha1_file(ce->sha1, type, &size);
163 if (buffer) {
164 if (size == expected_size && !strcmp(type, "blob"))
165 match = match_data(fd, buffer, size);
166 free(buffer);
167 }
168 close(fd);
169 }
170 return match;
171 }
173 /*
174 * "refresh" does not calculate a new sha1 file or bring the
175 * cache up-to-date for mode/content changes. But what it
176 * _does_ do is to "re-match" the stat information of a file
177 * with the cache, so that you can refresh the cache for a
178 * file that hasn't been changed but where the stat entry is
179 * out of date.
180 *
181 * For example, you'd want to do this after doing a "read-tree",
182 * to link up the stat cache details with the proper files.
183 */
184 static struct cache_entry *refresh_entry(struct cache_entry *ce)
185 {
186 struct stat st;
187 struct cache_entry *updated;
188 int changed, size;
190 if (stat(ce->name, &st) < 0)
191 return ERR_PTR(-errno);
193 changed = cache_match_stat(ce, &st);
194 if (!changed)
195 return ce;
197 /*
198 * If the mode has changed, there's no point in trying
199 * to refresh the entry - it's not going to match
200 */
201 if (changed & MODE_CHANGED)
202 return ERR_PTR(-EINVAL);
204 if (compare_data(ce, st.st_size))
205 return ERR_PTR(-EINVAL);
207 size = ce_size(ce);
208 updated = malloc(size);
209 memcpy(updated, ce, size);
210 fill_stat_cache_info(updated, &st);
211 return updated;
212 }
214 static void refresh_cache(void)
215 {
216 int i;
218 for (i = 0; i < active_nr; i++) {
219 struct cache_entry *ce, *new;
220 ce = active_cache[i];
221 if (ce_stage(ce)) {
222 printf("%s: needs merge\n", ce->name);
223 while ((i < active_nr) &&
224 ! strcmp(active_cache[i]->name, ce->name))
225 i++;
226 i--;
227 continue;
228 }
230 new = refresh_entry(ce);
231 if (IS_ERR(new)) {
232 if (!(not_new && PTR_ERR(new) == -ENOENT))
233 printf("%s: needs update\n", ce->name);
234 continue;
235 }
236 active_cache[i] = new;
237 }
238 }
240 /*
241 * We fundamentally don't like some paths: we don't want
242 * dot or dot-dot anywhere, and in fact, we don't even want
243 * any other dot-files (.git or anything else). They
244 * are hidden, for chist sake.
245 *
246 * Also, we don't want double slashes or slashes at the
247 * end that can make pathnames ambiguous.
248 */
249 static int verify_path(char *path)
250 {
251 char c;
253 goto inside;
254 for (;;) {
255 if (!c)
256 return 1;
257 if (c == '/') {
258 inside:
259 c = *path++;
260 if (c != '/' && c != '.' && c != '\0')
261 continue;
262 return 0;
263 }
264 c = *path++;
265 }
266 }
268 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
269 {
270 int size, len;
271 unsigned int mode;
272 unsigned char sha1[20];
273 struct cache_entry *ce;
275 if (sscanf(arg1, "%o", &mode) != 1)
276 return -1;
277 if (get_sha1_hex(arg2, sha1))
278 return -1;
279 if (!verify_path(arg3))
280 return -1;
282 len = strlen(arg3);
283 size = cache_entry_size(len);
284 ce = malloc(size);
285 memset(ce, 0, size);
287 memcpy(ce->sha1, sha1, 20);
288 memcpy(ce->name, arg3, len);
289 ce->ce_flags = htons(len);
290 ce->ce_mode = create_ce_mode(mode);
291 return add_cache_entry(ce, allow_add);
292 }
294 static const char *lockfile_name = NULL;
296 static void remove_lock_file(void)
297 {
298 if (lockfile_name)
299 unlink(lockfile_name);
300 }
302 int main(int argc, char **argv)
303 {
304 int i, newfd, entries;
305 int allow_options = 1;
306 static char lockfile[MAXPATHLEN+1];
307 const char *indexfile = get_index_file();
309 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
311 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
312 if (newfd < 0)
313 die("unable to create new cachefile");
315 atexit(remove_lock_file);
316 lockfile_name = lockfile;
318 entries = read_cache();
319 if (entries < 0)
320 die("cache corrupted");
322 for (i = 1 ; i < argc; i++) {
323 char *path = argv[i];
325 if (allow_options && *path == '-') {
326 if (!strcmp(path, "--")) {
327 allow_options = 0;
328 continue;
329 }
330 if (!strcmp(path, "--add")) {
331 allow_add = 1;
332 continue;
333 }
334 if (!strcmp(path, "--remove")) {
335 allow_remove = 1;
336 continue;
337 }
338 if (!strcmp(path, "--refresh")) {
339 refresh_cache();
340 continue;
341 }
342 if (!strcmp(path, "--cacheinfo")) {
343 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
344 die("update-cache: --cacheinfo <mode> <sha1> <path>");
345 i += 3;
346 continue;
347 }
348 if (!strcmp(path, "--ignore-missing")) {
349 not_new = 1;
350 continue;
351 }
352 die("unknown option %s", path);
353 }
354 if (!verify_path(path)) {
355 fprintf(stderr, "Ignoring path %s\n", argv[i]);
356 continue;
357 }
358 if (add_file_to_cache(path))
359 die("Unable to add %s to database", path);
360 }
361 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
362 die("Unable to write new cachefile");
364 lockfile_name = NULL;
365 return 0;
366 }