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