[PATCH] git-tar-tree: make file contents accessible to write_header()
[git/jrn.git] / update-cache.c
blob97d5e8b6b0695b82a235026430050a4140a1586a
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include <signal.h>
7 #include "cache.h"
9 /*
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "update-cache *" and suddenly having all the object
14 * files be revision controlled.
16 static int allow_add = 0, allow_remove = 0, not_new = 0;
19 * update-cache --refresh may not touch anything at all, in which case
20 * writing 1.6MB of the same thing is a waste.
22 static int cache_changed = 0;
24 /* Three functions to allow overloaded pointer return; see linux/err.h */
25 static inline void *ERR_PTR(long error)
27 return (void *) error;
30 static inline long PTR_ERR(const void *ptr)
32 return (long) ptr;
35 static inline long IS_ERR(const void *ptr)
37 return (unsigned long)ptr > (unsigned long)-1000L;
41 * This only updates the "non-critical" parts of the directory
42 * cache, ie the parts that aren't tracked by GIT, and only used
43 * to validate the cache.
45 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
47 ce->ce_ctime.sec = htonl(st->st_ctime);
48 ce->ce_mtime.sec = htonl(st->st_mtime);
49 #ifdef NSEC
50 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
51 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
52 #endif
53 ce->ce_dev = htonl(st->st_dev);
54 ce->ce_ino = htonl(st->st_ino);
55 ce->ce_uid = htonl(st->st_uid);
56 ce->ce_gid = htonl(st->st_gid);
57 ce->ce_size = htonl(st->st_size);
60 static int add_file_to_cache_1(char *path)
62 int size, namelen;
63 struct cache_entry *ce;
64 struct stat st;
65 int fd;
66 char *target;
68 if (lstat(path, &st) < 0) {
69 if (errno == ENOENT || errno == ENOTDIR) {
70 if (allow_remove)
71 return remove_file_from_cache(path);
73 return -1;
75 namelen = strlen(path);
76 size = cache_entry_size(namelen);
77 ce = xmalloc(size);
78 memset(ce, 0, size);
79 memcpy(ce->name, path, namelen);
80 fill_stat_cache_info(ce, &st);
81 ce->ce_mode = create_ce_mode(st.st_mode);
82 ce->ce_flags = htons(namelen);
83 switch (st.st_mode & S_IFMT) {
84 case S_IFREG:
85 fd = open(path, O_RDONLY);
86 if (fd < 0)
87 return -1;
88 if (index_fd(ce->sha1, fd, &st) < 0)
89 return -1;
90 break;
91 case S_IFLNK:
92 target = xmalloc(st.st_size+1);
93 if (readlink(path, target, st.st_size+1) != st.st_size) {
94 free(target);
95 return -1;
97 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
98 return -1;
99 free(target);
100 break;
101 default:
102 return -1;
104 if (!cache_changed) {
105 /* If we have not smudged the cache, be careful
106 * to keep it clean. Find out if we have a matching
107 * cache entry that add_cache_entry would replace with,
108 * and if it matches then do not bother calling it.
110 int pos = cache_name_pos(ce->name, namelen);
111 if ((0 <= pos) &&
112 !memcmp(active_cache[pos], ce, sizeof(*ce))) {
113 free(ce);
114 /* magic to tell add_file_to_cache that
115 * we have not updated anything.
117 return 999;
120 return add_cache_entry(ce, allow_add);
123 static int add_file_to_cache(char *path)
125 int ret = add_file_to_cache_1(path);
126 if (ret == 0)
127 cache_changed = 1;
128 else if (ret == 999)
129 ret = 0;
130 return ret;
133 static int match_data(int fd, void *buffer, unsigned long size)
135 while (size) {
136 char compare[1024];
137 int ret = read(fd, compare, sizeof(compare));
139 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
140 return -1;
141 size -= ret;
142 buffer += ret;
144 return 0;
147 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
149 int match = -1;
150 int fd = open(ce->name, O_RDONLY);
152 if (fd >= 0) {
153 void *buffer;
154 unsigned long size;
155 char type[10];
157 buffer = read_sha1_file(ce->sha1, type, &size);
158 if (buffer) {
159 if (size == expected_size && !strcmp(type, "blob"))
160 match = match_data(fd, buffer, size);
161 free(buffer);
163 close(fd);
165 return match;
168 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
170 int match = -1;
171 char *target;
172 void *buffer;
173 unsigned long size;
174 char type[10];
175 int len;
177 target = xmalloc(expected_size);
178 len = readlink(ce->name, target, expected_size);
179 if (len != expected_size) {
180 free(target);
181 return -1;
183 buffer = read_sha1_file(ce->sha1, type, &size);
184 if (!buffer) {
185 free(target);
186 return -1;
188 if (size == expected_size)
189 match = memcmp(buffer, target, size);
190 free(buffer);
191 free(target);
192 return match;
196 * "refresh" does not calculate a new sha1 file or bring the
197 * cache up-to-date for mode/content changes. But what it
198 * _does_ do is to "re-match" the stat information of a file
199 * with the cache, so that you can refresh the cache for a
200 * file that hasn't been changed but where the stat entry is
201 * out of date.
203 * For example, you'd want to do this after doing a "read-tree",
204 * to link up the stat cache details with the proper files.
206 static struct cache_entry *refresh_entry(struct cache_entry *ce)
208 struct stat st;
209 struct cache_entry *updated;
210 int changed, size;
212 if (lstat(ce->name, &st) < 0)
213 return ERR_PTR(-errno);
215 changed = cache_match_stat(ce, &st);
216 if (!changed)
217 return ce;
220 * If the mode or type has changed, there's no point in trying
221 * to refresh the entry - it's not going to match
223 if (changed & (MODE_CHANGED | TYPE_CHANGED))
224 return ERR_PTR(-EINVAL);
226 switch (st.st_mode & S_IFMT) {
227 case S_IFREG:
228 if (compare_data(ce, st.st_size))
229 return ERR_PTR(-EINVAL);
230 break;
231 case S_IFLNK:
232 if (compare_link(ce, st.st_size))
233 return ERR_PTR(-EINVAL);
234 break;
235 default:
236 return ERR_PTR(-EINVAL);
239 cache_changed = 1;
240 size = ce_size(ce);
241 updated = xmalloc(size);
242 memcpy(updated, ce, size);
243 fill_stat_cache_info(updated, &st);
244 return updated;
247 static int refresh_cache(void)
249 int i;
250 int has_errors = 0;
252 for (i = 0; i < active_nr; i++) {
253 struct cache_entry *ce, *new;
254 ce = active_cache[i];
255 if (ce_stage(ce)) {
256 printf("%s: needs merge\n", ce->name);
257 has_errors = 1;
258 while ((i < active_nr) &&
259 ! strcmp(active_cache[i]->name, ce->name))
260 i++;
261 i--;
262 continue;
265 new = refresh_entry(ce);
266 if (IS_ERR(new)) {
267 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
268 printf("%s: needs update\n", ce->name);
269 has_errors = 1;
271 continue;
273 active_cache[i] = new;
275 return has_errors;
279 * We fundamentally don't like some paths: we don't want
280 * dot or dot-dot anywhere, and in fact, we don't even want
281 * any other dot-files (.git or anything else). They
282 * are hidden, for chist sake.
284 * Also, we don't want double slashes or slashes at the
285 * end that can make pathnames ambiguous.
287 static int verify_path(char *path)
289 char c;
291 goto inside;
292 for (;;) {
293 if (!c)
294 return 1;
295 if (c == '/') {
296 inside:
297 c = *path++;
298 if (c != '/' && c != '.' && c != '\0')
299 continue;
300 return 0;
302 c = *path++;
306 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
308 int size, len;
309 unsigned int mode;
310 unsigned char sha1[20];
311 struct cache_entry *ce;
313 if (sscanf(arg1, "%o", &mode) != 1)
314 return -1;
315 if (get_sha1_hex(arg2, sha1))
316 return -1;
317 if (!verify_path(arg3))
318 return -1;
320 cache_changed = 1;
321 len = strlen(arg3);
322 size = cache_entry_size(len);
323 ce = xmalloc(size);
324 memset(ce, 0, size);
326 memcpy(ce->sha1, sha1, 20);
327 memcpy(ce->name, arg3, len);
328 ce->ce_flags = htons(len);
329 ce->ce_mode = create_ce_mode(mode);
330 return add_cache_entry(ce, allow_add);
333 static const char *lockfile_name = NULL;
335 static void remove_lock_file(void)
337 if (lockfile_name)
338 unlink(lockfile_name);
341 static void remove_lock_file_on_signal(int signo)
343 remove_lock_file();
346 int main(int argc, char **argv)
348 int i, newfd, entries, has_errors = 0;
349 int allow_options = 1;
350 static char lockfile[MAXPATHLEN+1];
351 const char *indexfile = get_index_file();
353 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
355 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
356 if (newfd < 0)
357 die("unable to create new cachefile");
359 signal(SIGINT, remove_lock_file_on_signal);
360 atexit(remove_lock_file);
361 lockfile_name = lockfile;
363 entries = read_cache();
364 if (entries < 0)
365 die("cache corrupted");
367 for (i = 1 ; i < argc; i++) {
368 char *path = argv[i];
370 if (allow_options && *path == '-') {
371 if (!strcmp(path, "--")) {
372 allow_options = 0;
373 continue;
375 if (!strcmp(path, "--add")) {
376 allow_add = 1;
377 continue;
379 if (!strcmp(path, "--remove")) {
380 allow_remove = 1;
381 continue;
383 if (!strcmp(path, "--refresh")) {
384 has_errors |= refresh_cache();
385 continue;
387 if (!strcmp(path, "--cacheinfo")) {
388 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
389 die("update-cache: --cacheinfo <mode> <sha1> <path>");
390 i += 3;
391 continue;
393 if (!strcmp(path, "--force-remove")) {
394 if (argc <= i + 1)
395 die("update-cache: --force-remove <path>");
396 if (remove_file_from_cache(argv[i+1]))
397 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
398 i++;
399 continue;
402 if (!strcmp(path, "--ignore-missing")) {
403 not_new = 1;
404 continue;
406 die("unknown option %s", path);
408 if (!verify_path(path)) {
409 fprintf(stderr, "Ignoring path %s\n", argv[i]);
410 continue;
412 if (add_file_to_cache(path))
413 die("Unable to add %s to database", path);
416 if (!cache_changed)
417 unlink(lockfile);
418 else if (write_cache(newfd, active_cache, active_nr) ||
419 rename(lockfile, indexfile))
420 die("Unable to write new cachefile");
422 lockfile_name = NULL;
423 return has_errors ? 1 : 0;