[PATCH] ssh-push: Don't add '/' to pathname
[git.git] / update-cache.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
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 "git-update-cache *" and suddenly having all the object
13  * files be revision controlled.
14  */
15 static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0;
16 static int force_remove;
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 }
33
34 static int add_file_to_cache(char *path)
35 {
36         int size, namelen, option, status;
37         struct cache_entry *ce;
38         struct stat st;
39         int fd;
40         char *target;
41
42         status = lstat(path, &st);
43         if (status < 0 || S_ISDIR(st.st_mode)) {
44                 /* When we used to have "path" and now we want to add
45                  * "path/file", we need a way to remove "path" before
46                  * being able to add "path/file".  However,
47                  * "git-update-cache --remove path" would not work.
48                  * --force-remove can be used but this is more user
49                  * friendly, especially since we can do the opposite
50                  * case just fine without --force-remove.
51                  */
52                 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
53                         if (allow_remove)
54                                 return remove_file_from_cache(path);
55                 }
56                 return error("open(\"%s\"): %s", path, strerror(errno));
57         }
58         namelen = strlen(path);
59         size = cache_entry_size(namelen);
60         ce = xmalloc(size);
61         memset(ce, 0, size);
62         memcpy(ce->name, path, namelen);
63         fill_stat_cache_info(ce, &st);
64         ce->ce_mode = create_ce_mode(st.st_mode);
65         ce->ce_flags = htons(namelen);
66         switch (st.st_mode & S_IFMT) {
67         case S_IFREG:
68                 fd = open(path, O_RDONLY);
69                 if (fd < 0)
70                         return -1;
71                 if (index_fd(ce->sha1, fd, &st) < 0)
72                         return -1;
73                 break;
74         case S_IFLNK:
75                 target = xmalloc(st.st_size+1);
76                 if (readlink(path, target, st.st_size+1) != st.st_size) {
77                         free(target);
78                         return -1;
79                 }
80                 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
81                         return -1;
82                 free(target);
83                 break;
84         default:
85                 return -1;
86         }
87         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
88         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
89         return add_cache_entry(ce, option);
90 }
91
92 static int match_data(int fd, void *buffer, unsigned long size)
93 {
94         while (size) {
95                 char compare[1024];
96                 int ret = read(fd, compare, sizeof(compare));
97
98                 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
99                         return -1;
100                 size -= ret;
101                 buffer += ret;
102         }
103         return 0;
104 }
105
106 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
107 {
108         int match = -1;
109         int fd = open(ce->name, O_RDONLY);
110
111         if (fd >= 0) {
112                 void *buffer;
113                 unsigned long size;
114                 char type[20];
115
116                 buffer = read_sha1_file(ce->sha1, type, &size);
117                 if (buffer) {
118                         if (size == expected_size && !strcmp(type, "blob"))
119                                 match = match_data(fd, buffer, size);
120                         free(buffer);
121                 }
122                 close(fd);
123         }
124         return match;
125 }
126
127 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
128 {
129         int match = -1;
130         char *target;
131         void *buffer;
132         unsigned long size;
133         char type[10];
134         int len;
135
136         target = xmalloc(expected_size);
137         len = readlink(ce->name, target, expected_size);
138         if (len != expected_size) {
139                 free(target);
140                 return -1;
141         }
142         buffer = read_sha1_file(ce->sha1, type, &size);
143         if (!buffer) {
144                 free(target);
145                 return -1;
146         }
147         if (size == expected_size)
148                 match = memcmp(buffer, target, size);
149         free(buffer);
150         free(target);
151         return match;
152 }
153
154 /*
155  * "refresh" does not calculate a new sha1 file or bring the
156  * cache up-to-date for mode/content changes. But what it
157  * _does_ do is to "re-match" the stat information of a file
158  * with the cache, so that you can refresh the cache for a
159  * file that hasn't been changed but where the stat entry is
160  * out of date.
161  *
162  * For example, you'd want to do this after doing a "git-read-tree",
163  * to link up the stat cache details with the proper files.
164  */
165 static struct cache_entry *refresh_entry(struct cache_entry *ce)
166 {
167         struct stat st;
168         struct cache_entry *updated;
169         int changed, size;
170
171         if (lstat(ce->name, &st) < 0)
172                 return ERR_PTR(-errno);
173
174         changed = ce_match_stat(ce, &st);
175         if (!changed)
176                 return ce;
177
178         /*
179          * If the mode or type has changed, there's no point in trying
180          * to refresh the entry - it's not going to match
181          */
182         if (changed & (MODE_CHANGED | TYPE_CHANGED))
183                 return ERR_PTR(-EINVAL);
184
185         switch (st.st_mode & S_IFMT) {
186         case S_IFREG:
187                 if (compare_data(ce, st.st_size))
188                         return ERR_PTR(-EINVAL);
189                 break;
190         case S_IFLNK:
191                 if (compare_link(ce, st.st_size))
192                         return ERR_PTR(-EINVAL);
193                 break;
194         default:
195                 return ERR_PTR(-EINVAL);
196         }
197
198         size = ce_size(ce);
199         updated = xmalloc(size);
200         memcpy(updated, ce, size);
201         fill_stat_cache_info(updated, &st);
202         return updated;
203 }
204
205 static int refresh_cache(void)
206 {
207         int i;
208         int has_errors = 0;
209
210         for (i = 0; i < active_nr; i++) {
211                 struct cache_entry *ce, *new;
212                 ce = active_cache[i];
213                 if (ce_stage(ce)) {
214                         printf("%s: needs merge\n", ce->name);
215                         has_errors = 1;
216                         while ((i < active_nr) &&
217                                ! strcmp(active_cache[i]->name, ce->name))
218                                 i++;
219                         i--;
220                         continue;
221                 }
222
223                 new = refresh_entry(ce);
224                 if (IS_ERR(new)) {
225                         if (!(not_new && PTR_ERR(new) == -ENOENT)) {
226                                 printf("%s: needs update\n", ce->name);
227                                 has_errors = 1;
228                         }
229                         continue;
230                 }
231                 active_cache_changed = 1;
232                 /* You can NOT just free active_cache[i] here, since it
233                  * might not be necessarily malloc()ed but can also come
234                  * from mmap(). */
235                 active_cache[i] = new;
236         }
237         return has_errors;
238 }
239
240 /*
241  * We fundamentally don't like some paths: we don't want
242  * dot or dot-dot anywhere, and for obvious reasons don't
243  * want to recurse into ".git" either.
244  *
245  * Also, we don't want double slashes or slashes at the
246  * end that can make pathnames ambiguous.
247  */
248 static int verify_dotfile(const char *rest)
249 {
250         /*
251          * The first character was '.', but that
252          * has already been discarded, we now test
253          * the rest.
254          */
255         switch (*rest) {
256         /* "." is not allowed */
257         case '\0': case '/':
258                 return 0;
259
260         /*
261          * ".git" followed by  NUL or slash is bad. This
262          * shares the path end test with the ".." case.
263          */
264         case 'g':
265                 if (rest[1] != 'i')
266                         break;
267                 if (rest[2] != 't')
268                         break;
269                 rest += 2;
270         /* fallthrough */
271         case '.':
272                 if (rest[1] == '\0' || rest[1] == '/')
273                         return 0;
274         }
275         return 1;
276 }
277
278 static int verify_path(char *path)
279 {
280         char c;
281
282         goto inside;
283         for (;;) {
284                 if (!c)
285                         return 1;
286                 if (c == '/') {
287 inside:
288                         c = *path++;
289                         switch (c) {
290                         default:
291                                 continue;
292                         case '/': case '\0':
293                                 break;
294                         case '.':
295                                 if (verify_dotfile(path))
296                                         continue;
297                         }
298                         return 0;
299                 }
300                 c = *path++;
301         }
302 }
303
304 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
305 {
306         int size, len, option;
307         unsigned int mode;
308         unsigned char sha1[20];
309         struct cache_entry *ce;
310
311         if (sscanf(arg1, "%o", &mode) != 1)
312                 return -1;
313         if (get_sha1_hex(arg2, sha1))
314                 return -1;
315         if (!verify_path(arg3))
316                 return -1;
317
318         len = strlen(arg3);
319         size = cache_entry_size(len);
320         ce = xmalloc(size);
321         memset(ce, 0, size);
322
323         memcpy(ce->sha1, sha1, 20);
324         memcpy(ce->name, arg3, len);
325         ce->ce_flags = htons(len);
326         ce->ce_mode = create_ce_mode(mode);
327         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
328         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
329         return add_cache_entry(ce, option);
330 }
331
332 static struct cache_file cache_file;
333
334 int main(int argc, char **argv)
335 {
336         int i, newfd, entries, has_errors = 0;
337         int allow_options = 1;
338
339         newfd = hold_index_file_for_update(&cache_file, get_index_file());
340         if (newfd < 0)
341                 die("unable to create new cachefile");
342
343         entries = read_cache();
344         if (entries < 0)
345                 die("cache corrupted");
346
347         for (i = 1 ; i < argc; i++) {
348                 char *path = argv[i];
349
350                 if (allow_options && *path == '-') {
351                         if (!strcmp(path, "--")) {
352                                 allow_options = 0;
353                                 continue;
354                         }
355                         if (!strcmp(path, "--add")) {
356                                 allow_add = 1;
357                                 continue;
358                         }
359                         if (!strcmp(path, "--replace")) {
360                                 allow_replace = 1;
361                                 continue;
362                         }
363                         if (!strcmp(path, "--remove")) {
364                                 allow_remove = 1;
365                                 continue;
366                         }
367                         if (!strcmp(path, "--refresh")) {
368                                 has_errors |= refresh_cache();
369                                 continue;
370                         }
371                         if (!strcmp(path, "--cacheinfo")) {
372                                 if (i+3 >= argc)
373                                         die("git-update-cache: --cacheinfo <mode> <sha1> <path>");
374                                 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
375                                         die("git-update-cache: --cacheinfo cannot add %s", argv[i+3]);
376                                 i += 3;
377                                 continue;
378                         }
379                         if (!strcmp(path, "--force-remove")) {
380                                 force_remove = 1;
381                                 continue;
382                         }
383
384                         if (!strcmp(path, "--ignore-missing")) {
385                                 not_new = 1;
386                                 continue;
387                         }
388                         die("unknown option %s", path);
389                 }
390                 if (!verify_path(path)) {
391                         fprintf(stderr, "Ignoring path %s\n", argv[i]);
392                         continue;
393                 }
394                 if (force_remove) {
395                         if (remove_file_from_cache(path))
396                                 die("git-update-cache: --force-remove cannot remove %s", path);
397                         continue;
398                 }
399                 if (add_file_to_cache(path))
400                         die("Unable to add %s to database", path);
401         }
402         if (write_cache(newfd, active_cache, active_nr) ||
403             commit_index_file(&cache_file))
404                 die("Unable to write new cachefile");
405
406         return has_errors ? 1 : 0;
407 }