]>
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 | */ | |
c6e007b0 JB |
16 | static int allow_add = 0, allow_remove = 0, not_new = 0; |
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 | ||
e83c5163 LT |
54 | static int add_file_to_cache(char *path) |
55 | { | |
56 | int size, namelen; | |
57 | struct cache_entry *ce; | |
58 | struct stat st; | |
59 | int fd; | |
8ae0a8c5 KS |
60 | unsigned int len; |
61 | char target[1024]; | |
e83c5163 | 62 | |
8ae0a8c5 | 63 | if (lstat(path, &st) < 0) { |
e2a669bb | 64 | if (errno == ENOENT || errno == ENOTDIR) { |
121481ab LT |
65 | if (allow_remove) |
66 | return remove_file_from_cache(path); | |
67 | } | |
e83c5163 LT |
68 | return -1; |
69 | } | |
e83c5163 LT |
70 | namelen = strlen(path); |
71 | size = cache_entry_size(namelen); | |
812666c8 | 72 | ce = xmalloc(size); |
e83c5163 LT |
73 | memset(ce, 0, size); |
74 | memcpy(ce->name, path, namelen); | |
711cf3a0 | 75 | fill_stat_cache_info(ce, &st); |
e4479470 | 76 | ce->ce_mode = create_ce_mode(st.st_mode); |
f5cabd13 | 77 | ce->ce_flags = htons(namelen); |
8ae0a8c5 KS |
78 | switch (st.st_mode & S_IFMT) { |
79 | case S_IFREG: | |
80 | fd = open(path, O_RDONLY); | |
81 | if (fd < 0) | |
82 | return -1; | |
83 | if (index_fd(ce->sha1, fd, &st) < 0) | |
84 | return -1; | |
85 | break; | |
86 | case S_IFLNK: | |
87 | len = readlink(path, target, sizeof(target)); | |
88 | if (len == -1 || len+1 > sizeof(target)) | |
89 | return -1; | |
90 | if (write_sha1_file(target, len, "blob", ce->sha1)) | |
91 | return -1; | |
92 | break; | |
93 | default: | |
e83c5163 | 94 | return -1; |
8ae0a8c5 | 95 | } |
121481ab LT |
96 | return add_cache_entry(ce, allow_add); |
97 | } | |
98 | ||
711cf3a0 LT |
99 | static int match_data(int fd, void *buffer, unsigned long size) |
100 | { | |
101 | while (size) { | |
102 | char compare[1024]; | |
103 | int ret = read(fd, compare, sizeof(compare)); | |
104 | ||
105 | if (ret <= 0 || ret > size || memcmp(buffer, compare, ret)) | |
106 | return -1; | |
107 | size -= ret; | |
108 | buffer += ret; | |
109 | } | |
110 | return 0; | |
111 | } | |
112 | ||
32d197f1 | 113 | static int compare_data(struct cache_entry *ce, unsigned long expected_size) |
121481ab | 114 | { |
711cf3a0 LT |
115 | int match = -1; |
116 | int fd = open(ce->name, O_RDONLY); | |
117 | ||
118 | if (fd >= 0) { | |
119 | void *buffer; | |
120 | unsigned long size; | |
121 | char type[10]; | |
122 | ||
123 | buffer = read_sha1_file(ce->sha1, type, &size); | |
124 | if (buffer) { | |
32d197f1 | 125 | if (size == expected_size && !strcmp(type, "blob")) |
711cf3a0 LT |
126 | match = match_data(fd, buffer, size); |
127 | free(buffer); | |
128 | } | |
129 | close(fd); | |
130 | } | |
131 | return match; | |
132 | } | |
133 | ||
134 | /* | |
135 | * "refresh" does not calculate a new sha1 file or bring the | |
136 | * cache up-to-date for mode/content changes. But what it | |
137 | * _does_ do is to "re-match" the stat information of a file | |
138 | * with the cache, so that you can refresh the cache for a | |
139 | * file that hasn't been changed but where the stat entry is | |
140 | * out of date. | |
141 | * | |
142 | * For example, you'd want to do this after doing a "read-tree", | |
143 | * to link up the stat cache details with the proper files. | |
144 | */ | |
145 | static struct cache_entry *refresh_entry(struct cache_entry *ce) | |
146 | { | |
147 | struct stat st; | |
148 | struct cache_entry *updated; | |
149 | int changed, size; | |
150 | ||
8ae0a8c5 | 151 | if (lstat(ce->name, &st) < 0) |
c6e007b0 | 152 | return ERR_PTR(-errno); |
711cf3a0 LT |
153 | |
154 | changed = cache_match_stat(ce, &st); | |
155 | if (!changed) | |
156 | return ce; | |
157 | ||
121481ab | 158 | /* |
8ae0a8c5 | 159 | * If the mode or type has changed, there's no point in trying |
711cf3a0 | 160 | * to refresh the entry - it's not going to match |
121481ab | 161 | */ |
8ae0a8c5 | 162 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) |
c6e007b0 | 163 | return ERR_PTR(-EINVAL); |
711cf3a0 | 164 | |
32d197f1 | 165 | if (compare_data(ce, st.st_size)) |
c6e007b0 | 166 | return ERR_PTR(-EINVAL); |
711cf3a0 LT |
167 | |
168 | size = ce_size(ce); | |
812666c8 | 169 | updated = xmalloc(size); |
711cf3a0 LT |
170 | memcpy(updated, ce, size); |
171 | fill_stat_cache_info(updated, &st); | |
172 | return updated; | |
121481ab LT |
173 | } |
174 | ||
90535218 | 175 | static int refresh_cache(void) |
121481ab LT |
176 | { |
177 | int i; | |
90535218 | 178 | int has_errors = 0; |
121481ab | 179 | |
711cf3a0 | 180 | for (i = 0; i < active_nr; i++) { |
1bc992ac JH |
181 | struct cache_entry *ce, *new; |
182 | ce = active_cache[i]; | |
183 | if (ce_stage(ce)) { | |
184 | printf("%s: needs merge\n", ce->name); | |
90535218 | 185 | has_errors = 1; |
1bc992ac JH |
186 | while ((i < active_nr) && |
187 | ! strcmp(active_cache[i]->name, ce->name)) | |
188 | i++; | |
189 | i--; | |
190 | continue; | |
191 | } | |
711cf3a0 | 192 | |
1bc992ac | 193 | new = refresh_entry(ce); |
c6e007b0 | 194 | if (IS_ERR(new)) { |
90535218 | 195 | if (!(not_new && PTR_ERR(new) == -ENOENT)) { |
c6e007b0 | 196 | printf("%s: needs update\n", ce->name); |
90535218 JH |
197 | has_errors = 1; |
198 | } | |
711cf3a0 LT |
199 | continue; |
200 | } | |
201 | active_cache[i] = new; | |
202 | } | |
90535218 | 203 | return has_errors; |
e83c5163 LT |
204 | } |
205 | ||
e83c5163 LT |
206 | /* |
207 | * We fundamentally don't like some paths: we don't want | |
208 | * dot or dot-dot anywhere, and in fact, we don't even want | |
4bb04f21 | 209 | * any other dot-files (.git or anything else). They |
e83c5163 LT |
210 | * are hidden, for chist sake. |
211 | * | |
212 | * Also, we don't want double slashes or slashes at the | |
aebb2679 | 213 | * end that can make pathnames ambiguous. |
e83c5163 LT |
214 | */ |
215 | static int verify_path(char *path) | |
216 | { | |
217 | char c; | |
218 | ||
219 | goto inside; | |
220 | for (;;) { | |
221 | if (!c) | |
222 | return 1; | |
223 | if (c == '/') { | |
224 | inside: | |
225 | c = *path++; | |
226 | if (c != '/' && c != '.' && c != '\0') | |
227 | continue; | |
228 | return 0; | |
229 | } | |
230 | c = *path++; | |
231 | } | |
232 | } | |
233 | ||
9945d980 LT |
234 | static int add_cacheinfo(char *arg1, char *arg2, char *arg3) |
235 | { | |
236 | int size, len; | |
237 | unsigned int mode; | |
238 | unsigned char sha1[20]; | |
239 | struct cache_entry *ce; | |
240 | ||
241 | if (sscanf(arg1, "%o", &mode) != 1) | |
242 | return -1; | |
9945d980 LT |
243 | if (get_sha1_hex(arg2, sha1)) |
244 | return -1; | |
9945d980 LT |
245 | if (!verify_path(arg3)) |
246 | return -1; | |
9945d980 LT |
247 | |
248 | len = strlen(arg3); | |
249 | size = cache_entry_size(len); | |
812666c8 | 250 | ce = xmalloc(size); |
9945d980 LT |
251 | memset(ce, 0, size); |
252 | ||
253 | memcpy(ce->sha1, sha1, 20); | |
254 | memcpy(ce->name, arg3, len); | |
f5cabd13 | 255 | ce->ce_flags = htons(len); |
e4479470 | 256 | ce->ce_mode = create_ce_mode(mode); |
9945d980 LT |
257 | return add_cache_entry(ce, allow_add); |
258 | } | |
259 | ||
bb233d69 | 260 | static const char *lockfile_name = NULL; |
9614b8dc LT |
261 | |
262 | static void remove_lock_file(void) | |
263 | { | |
bb233d69 LT |
264 | if (lockfile_name) |
265 | unlink(lockfile_name); | |
9614b8dc LT |
266 | } |
267 | ||
f2a19340 LT |
268 | static void remove_lock_file_on_signal(int signo) |
269 | { | |
270 | remove_lock_file(); | |
271 | } | |
272 | ||
e83c5163 LT |
273 | int main(int argc, char **argv) |
274 | { | |
90535218 | 275 | int i, newfd, entries, has_errors = 0; |
121481ab | 276 | int allow_options = 1; |
bb233d69 LT |
277 | static char lockfile[MAXPATHLEN+1]; |
278 | const char *indexfile = get_index_file(); | |
e83c5163 | 279 | |
bb233d69 LT |
280 | snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile); |
281 | ||
282 | newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600); | |
9614b8dc | 283 | if (newfd < 0) |
2de381f9 | 284 | die("unable to create new cachefile"); |
9614b8dc | 285 | |
f2a19340 | 286 | signal(SIGINT, remove_lock_file_on_signal); |
9614b8dc | 287 | atexit(remove_lock_file); |
bb233d69 | 288 | lockfile_name = lockfile; |
9614b8dc | 289 | |
e83c5163 | 290 | entries = read_cache(); |
9614b8dc | 291 | if (entries < 0) |
2de381f9 | 292 | die("cache corrupted"); |
e83c5163 | 293 | |
e83c5163 LT |
294 | for (i = 1 ; i < argc; i++) { |
295 | char *path = argv[i]; | |
121481ab LT |
296 | |
297 | if (allow_options && *path == '-') { | |
298 | if (!strcmp(path, "--")) { | |
299 | allow_options = 0; | |
300 | continue; | |
301 | } | |
302 | if (!strcmp(path, "--add")) { | |
303 | allow_add = 1; | |
304 | continue; | |
305 | } | |
306 | if (!strcmp(path, "--remove")) { | |
307 | allow_remove = 1; | |
308 | continue; | |
309 | } | |
310 | if (!strcmp(path, "--refresh")) { | |
90535218 | 311 | has_errors |= refresh_cache(); |
121481ab LT |
312 | continue; |
313 | } | |
9945d980 LT |
314 | if (!strcmp(path, "--cacheinfo")) { |
315 | if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3])) | |
316 | die("update-cache: --cacheinfo <mode> <sha1> <path>"); | |
317 | i += 3; | |
318 | continue; | |
319 | } | |
0ff5bf7c JH |
320 | if (!strcmp(path, "--force-remove")) { |
321 | if (argc <= i + 1) | |
322 | die("update-cache: --force-remove <path>"); | |
323 | if (remove_file_from_cache(argv[i+1])) | |
324 | die("update-cache: --force-remove cannot remove %s", argv[i+1]); | |
325 | i++; | |
326 | continue; | |
327 | } | |
328 | ||
c6e007b0 JB |
329 | if (!strcmp(path, "--ignore-missing")) { |
330 | not_new = 1; | |
331 | continue; | |
332 | } | |
2de381f9 | 333 | die("unknown option %s", path); |
121481ab | 334 | } |
e83c5163 LT |
335 | if (!verify_path(path)) { |
336 | fprintf(stderr, "Ignoring path %s\n", argv[i]); | |
337 | continue; | |
338 | } | |
9614b8dc | 339 | if (add_file_to_cache(path)) |
2de381f9 | 340 | die("Unable to add %s to database", path); |
e83c5163 | 341 | } |
bb233d69 | 342 | if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile)) |
2de381f9 | 343 | die("Unable to write new cachefile"); |
9614b8dc | 344 | |
bb233d69 | 345 | lockfile_name = NULL; |
90535218 | 346 | return has_errors; |
e83c5163 | 347 | } |