[PATCH] fix compare symlink against readlink not data
[git/git.git] / update-cache.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
f2a19340 6#include <signal.h>
e83c5163
LT
7#include "cache.h"
8
121481ab
LT
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.
15 */
c6e007b0
JB
16static int allow_add = 0, allow_remove = 0, not_new = 0;
17
c4b83e61
JH
18/*
19 * update-cache --refresh may not touch anything at all, in which case
20 * writing 1.6MB of the same thing is a waste.
21 */
22static int cache_changed = 0;
23
c6e007b0
JB
24/* Three functions to allow overloaded pointer return; see linux/err.h */
25static inline void *ERR_PTR(long error)
26{
27 return (void *) error;
28}
29
30static inline long PTR_ERR(const void *ptr)
31{
32 return (long) ptr;
33}
34
35static inline long IS_ERR(const void *ptr)
36{
37 return (unsigned long)ptr > (unsigned long)-1000L;
38}
121481ab 39
711cf3a0
LT
40/*
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.
44 */
45static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
46{
ccc4feb5
LT
47 ce->ce_ctime.sec = htonl(st->st_ctime);
48 ce->ce_mtime.sec = htonl(st->st_mtime);
5ade8628 49#ifdef NSEC
ccc4feb5
LT
50 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
51 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
5ade8628 52#endif
ccc4feb5
LT
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);
711cf3a0
LT
58}
59
c4b83e61 60static int add_file_to_cache_1(char *path)
e83c5163
LT
61{
62 int size, namelen;
63 struct cache_entry *ce;
64 struct stat st;
65 int fd;
8ae0a8c5 66 unsigned int len;
ffbe1add 67 char *target;
e83c5163 68
8ae0a8c5 69 if (lstat(path, &st) < 0) {
e2a669bb 70 if (errno == ENOENT || errno == ENOTDIR) {
121481ab
LT
71 if (allow_remove)
72 return remove_file_from_cache(path);
73 }
e83c5163
LT
74 return -1;
75 }
e83c5163
LT
76 namelen = strlen(path);
77 size = cache_entry_size(namelen);
812666c8 78 ce = xmalloc(size);
e83c5163
LT
79 memset(ce, 0, size);
80 memcpy(ce->name, path, namelen);
711cf3a0 81 fill_stat_cache_info(ce, &st);
e4479470 82 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 83 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
84 switch (st.st_mode & S_IFMT) {
85 case S_IFREG:
86 fd = open(path, O_RDONLY);
87 if (fd < 0)
88 return -1;
89 if (index_fd(ce->sha1, fd, &st) < 0)
90 return -1;
91 break;
92 case S_IFLNK:
ffbe1add
KS
93 target = xmalloc(st.st_size+1);
94 if (readlink(path, target, st.st_size+1) != st.st_size) {
95 free(target);
8ae0a8c5 96 return -1;
ffbe1add
KS
97 }
98 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
8ae0a8c5 99 return -1;
ffbe1add 100 free(target);
8ae0a8c5
KS
101 break;
102 default:
e83c5163 103 return -1;
8ae0a8c5 104 }
c4b83e61
JH
105 if (!cache_changed) {
106 /* If we have not smudged the cache, be careful
107 * to keep it clean. Find out if we have a matching
108 * cache entry that add_cache_entry would replace with,
109 * and if it matches then do not bother calling it.
110 */
111 int pos = cache_name_pos(ce->name, namelen);
112 if ((0 <= pos) &&
113 !memcmp(active_cache[pos], ce, sizeof(*ce))) {
114 free(ce);
115 /* magic to tell add_file_to_cache that
116 * we have not updated anything.
117 */
118 return 999;
119 }
120 }
121481ab
LT
121 return add_cache_entry(ce, allow_add);
122}
123
c4b83e61
JH
124static int add_file_to_cache(char *path)
125{
126 int ret = add_file_to_cache_1(path);
127 if (ret == 0)
128 cache_changed = 1;
129 else if (ret == 999)
130 ret = 0;
131 return ret;
132}
133
711cf3a0
LT
134static int match_data(int fd, void *buffer, unsigned long size)
135{
136 while (size) {
137 char compare[1024];
138 int ret = read(fd, compare, sizeof(compare));
139
140 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
141 return -1;
142 size -= ret;
143 buffer += ret;
144 }
145 return 0;
146}
147
32d197f1 148static int compare_data(struct cache_entry *ce, unsigned long expected_size)
121481ab 149{
711cf3a0
LT
150 int match = -1;
151 int fd = open(ce->name, O_RDONLY);
152
153 if (fd >= 0) {
154 void *buffer;
155 unsigned long size;
156 char type[10];
157
158 buffer = read_sha1_file(ce->sha1, type, &size);
159 if (buffer) {
32d197f1 160 if (size == expected_size && !strcmp(type, "blob"))
711cf3a0
LT
161 match = match_data(fd, buffer, size);
162 free(buffer);
163 }
164 close(fd);
165 }
166 return match;
167}
168
ffbe1add
KS
169static int compare_link(struct cache_entry *ce, unsigned long expected_size)
170{
171 int match = -1;
172 char *target;
173 void *buffer;
174 unsigned long size;
175 char type[10];
176 int len;
177
178 target = xmalloc(expected_size);
179 len = readlink(ce->name, target, expected_size);
180 if (len != expected_size) {
181 free(target);
182 return -1;
183 }
184 buffer = read_sha1_file(ce->sha1, type, &size);
185 if (!buffer) {
186 free(target);
187 return -1;
188 }
189 if (size == expected_size)
190 match = memcmp(buffer, target, size);
191 free(buffer);
192 free(target);
193 return match;
194}
195
711cf3a0
LT
196/*
197 * "refresh" does not calculate a new sha1 file or bring the
198 * cache up-to-date for mode/content changes. But what it
199 * _does_ do is to "re-match" the stat information of a file
200 * with the cache, so that you can refresh the cache for a
201 * file that hasn't been changed but where the stat entry is
202 * out of date.
203 *
204 * For example, you'd want to do this after doing a "read-tree",
205 * to link up the stat cache details with the proper files.
206 */
207static struct cache_entry *refresh_entry(struct cache_entry *ce)
208{
209 struct stat st;
210 struct cache_entry *updated;
211 int changed, size;
212
8ae0a8c5 213 if (lstat(ce->name, &st) < 0)
c6e007b0 214 return ERR_PTR(-errno);
711cf3a0
LT
215
216 changed = cache_match_stat(ce, &st);
217 if (!changed)
218 return ce;
219
121481ab 220 /*
8ae0a8c5 221 * If the mode or type has changed, there's no point in trying
711cf3a0 222 * to refresh the entry - it's not going to match
121481ab 223 */
8ae0a8c5 224 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 225 return ERR_PTR(-EINVAL);
711cf3a0 226
ffbe1add
KS
227 switch (st.st_mode & S_IFMT) {
228 case S_IFREG:
229 if (compare_data(ce, st.st_size))
230 return ERR_PTR(-EINVAL);
231 break;
232 case S_IFLNK:
233 if (compare_link(ce, st.st_size))
234 return ERR_PTR(-EINVAL);
235 break;
236 default:
c6e007b0 237 return ERR_PTR(-EINVAL);
ffbe1add 238 }
711cf3a0 239
c4b83e61 240 cache_changed = 1;
711cf3a0 241 size = ce_size(ce);
812666c8 242 updated = xmalloc(size);
711cf3a0
LT
243 memcpy(updated, ce, size);
244 fill_stat_cache_info(updated, &st);
245 return updated;
121481ab
LT
246}
247
90535218 248static int refresh_cache(void)
121481ab
LT
249{
250 int i;
90535218 251 int has_errors = 0;
121481ab 252
711cf3a0 253 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
254 struct cache_entry *ce, *new;
255 ce = active_cache[i];
256 if (ce_stage(ce)) {
257 printf("%s: needs merge\n", ce->name);
90535218 258 has_errors = 1;
1bc992ac
JH
259 while ((i < active_nr) &&
260 ! strcmp(active_cache[i]->name, ce->name))
261 i++;
262 i--;
263 continue;
264 }
711cf3a0 265
1bc992ac 266 new = refresh_entry(ce);
c6e007b0 267 if (IS_ERR(new)) {
90535218 268 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
c6e007b0 269 printf("%s: needs update\n", ce->name);
90535218
JH
270 has_errors = 1;
271 }
711cf3a0
LT
272 continue;
273 }
274 active_cache[i] = new;
275 }
90535218 276 return has_errors;
e83c5163
LT
277}
278
e83c5163
LT
279/*
280 * We fundamentally don't like some paths: we don't want
281 * dot or dot-dot anywhere, and in fact, we don't even want
4bb04f21 282 * any other dot-files (.git or anything else). They
e83c5163
LT
283 * are hidden, for chist sake.
284 *
285 * Also, we don't want double slashes or slashes at the
aebb2679 286 * end that can make pathnames ambiguous.
e83c5163
LT
287 */
288static int verify_path(char *path)
289{
290 char c;
291
292 goto inside;
293 for (;;) {
294 if (!c)
295 return 1;
296 if (c == '/') {
297inside:
298 c = *path++;
299 if (c != '/' && c != '.' && c != '\0')
300 continue;
301 return 0;
302 }
303 c = *path++;
304 }
305}
306
9945d980
LT
307static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
308{
309 int size, len;
310 unsigned int mode;
311 unsigned char sha1[20];
312 struct cache_entry *ce;
313
314 if (sscanf(arg1, "%o", &mode) != 1)
315 return -1;
9945d980
LT
316 if (get_sha1_hex(arg2, sha1))
317 return -1;
9945d980
LT
318 if (!verify_path(arg3))
319 return -1;
9945d980 320
c4b83e61 321 cache_changed = 1;
9945d980
LT
322 len = strlen(arg3);
323 size = cache_entry_size(len);
812666c8 324 ce = xmalloc(size);
9945d980
LT
325 memset(ce, 0, size);
326
327 memcpy(ce->sha1, sha1, 20);
328 memcpy(ce->name, arg3, len);
f5cabd13 329 ce->ce_flags = htons(len);
e4479470 330 ce->ce_mode = create_ce_mode(mode);
9945d980
LT
331 return add_cache_entry(ce, allow_add);
332}
333
bb233d69 334static const char *lockfile_name = NULL;
9614b8dc
LT
335
336static void remove_lock_file(void)
337{
bb233d69
LT
338 if (lockfile_name)
339 unlink(lockfile_name);
9614b8dc
LT
340}
341
f2a19340
LT
342static void remove_lock_file_on_signal(int signo)
343{
344 remove_lock_file();
345}
346
e83c5163
LT
347int main(int argc, char **argv)
348{
90535218 349 int i, newfd, entries, has_errors = 0;
121481ab 350 int allow_options = 1;
bb233d69
LT
351 static char lockfile[MAXPATHLEN+1];
352 const char *indexfile = get_index_file();
e83c5163 353
bb233d69
LT
354 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
355
356 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
9614b8dc 357 if (newfd < 0)
2de381f9 358 die("unable to create new cachefile");
9614b8dc 359
f2a19340 360 signal(SIGINT, remove_lock_file_on_signal);
9614b8dc 361 atexit(remove_lock_file);
bb233d69 362 lockfile_name = lockfile;
9614b8dc 363
e83c5163 364 entries = read_cache();
9614b8dc 365 if (entries < 0)
2de381f9 366 die("cache corrupted");
e83c5163 367
e83c5163
LT
368 for (i = 1 ; i < argc; i++) {
369 char *path = argv[i];
121481ab
LT
370
371 if (allow_options && *path == '-') {
372 if (!strcmp(path, "--")) {
373 allow_options = 0;
374 continue;
375 }
376 if (!strcmp(path, "--add")) {
377 allow_add = 1;
378 continue;
379 }
380 if (!strcmp(path, "--remove")) {
381 allow_remove = 1;
382 continue;
383 }
384 if (!strcmp(path, "--refresh")) {
90535218 385 has_errors |= refresh_cache();
121481ab
LT
386 continue;
387 }
9945d980
LT
388 if (!strcmp(path, "--cacheinfo")) {
389 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
390 die("update-cache: --cacheinfo <mode> <sha1> <path>");
391 i += 3;
392 continue;
393 }
0ff5bf7c
JH
394 if (!strcmp(path, "--force-remove")) {
395 if (argc <= i + 1)
396 die("update-cache: --force-remove <path>");
397 if (remove_file_from_cache(argv[i+1]))
398 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
399 i++;
400 continue;
401 }
402
c6e007b0
JB
403 if (!strcmp(path, "--ignore-missing")) {
404 not_new = 1;
405 continue;
406 }
2de381f9 407 die("unknown option %s", path);
121481ab 408 }
e83c5163
LT
409 if (!verify_path(path)) {
410 fprintf(stderr, "Ignoring path %s\n", argv[i]);
411 continue;
412 }
9614b8dc 413 if (add_file_to_cache(path))
2de381f9 414 die("Unable to add %s to database", path);
e83c5163 415 }
c4b83e61
JH
416
417 if (!cache_changed)
418 unlink(lockfile);
419 else if (write_cache(newfd, active_cache, active_nr) ||
420 rename(lockfile, indexfile))
2de381f9 421 die("Unable to write new cachefile");
9614b8dc 422
bb233d69 423 lockfile_name = NULL;
c4b83e61 424 return has_errors ? 1 : 0;
e83c5163 425}