Make the default directory permissions more lax.
[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 */
e83c5163
LT
6#include "cache.h"
7
121481ab
LT
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 "update-cache *" and suddenly having all the object
13 * files be revision controlled.
14 */
15static int allow_add = 0, allow_remove = 0;
16
e83c5163
LT
17static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
18{
19 z_stream stream;
20 int max_out_bytes = namelen + st->st_size + 200;
21 void *out = malloc(max_out_bytes);
22 void *metadata = malloc(namelen + 200);
23 void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
24 SHA_CTX c;
25
26 close(fd);
27 if (!out || (int)(long)in == -1)
28 return -1;
29
30 memset(&stream, 0, sizeof(stream));
31 deflateInit(&stream, Z_BEST_COMPRESSION);
32
33 /*
34 * ASCII size + nul byte
35 */
36 stream.next_in = metadata;
37 stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
38 stream.next_out = out;
39 stream.avail_out = max_out_bytes;
40 while (deflate(&stream, 0) == Z_OK)
41 /* nothing */;
42
43 /*
44 * File content
45 */
46 stream.next_in = in;
47 stream.avail_in = st->st_size;
48 while (deflate(&stream, Z_FINISH) == Z_OK)
49 /*nothing */;
50
51 deflateEnd(&stream);
52
53 SHA1_Init(&c);
54 SHA1_Update(&c, out, stream.total_out);
55 SHA1_Final(ce->sha1, &c);
56
57 return write_sha1_buffer(ce->sha1, out, stream.total_out);
58}
59
711cf3a0
LT
60/*
61 * This only updates the "non-critical" parts of the directory
62 * cache, ie the parts that aren't tracked by GIT, and only used
63 * to validate the cache.
64 */
65static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
66{
67 ce->ctime.sec = st->st_ctime;
68 ce->ctime.nsec = st->st_ctim.tv_nsec;
69 ce->mtime.sec = st->st_mtime;
70 ce->mtime.nsec = st->st_mtim.tv_nsec;
71 ce->st_dev = st->st_dev;
72 ce->st_ino = st->st_ino;
73 ce->st_uid = st->st_uid;
74 ce->st_gid = st->st_gid;
75}
76
e83c5163
LT
77static int add_file_to_cache(char *path)
78{
79 int size, namelen;
80 struct cache_entry *ce;
81 struct stat st;
82 int fd;
83
84 fd = open(path, O_RDONLY);
85 if (fd < 0) {
121481ab
LT
86 if (errno == ENOENT) {
87 if (allow_remove)
88 return remove_file_from_cache(path);
89 }
e83c5163
LT
90 return -1;
91 }
92 if (fstat(fd, &st) < 0) {
93 close(fd);
94 return -1;
95 }
96 namelen = strlen(path);
97 size = cache_entry_size(namelen);
98 ce = malloc(size);
99 memset(ce, 0, size);
100 memcpy(ce->name, path, namelen);
711cf3a0 101 fill_stat_cache_info(ce, &st);
e83c5163 102 ce->st_mode = st.st_mode;
e83c5163
LT
103 ce->st_size = st.st_size;
104 ce->namelen = namelen;
105
106 if (index_fd(path, namelen, ce, fd, &st) < 0)
107 return -1;
108
121481ab
LT
109 return add_cache_entry(ce, allow_add);
110}
111
711cf3a0
LT
112static 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
126static int compare_data(struct cache_entry *ce)
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;
134 char type[10];
135
136 buffer = read_sha1_file(ce->sha1, type, &size);
137 if (buffer) {
138 if (size == ce->st_size && !strcmp(type, "blob"))
139 match = match_data(fd, buffer, size);
140 free(buffer);
141 }
142 close(fd);
143 }
144 return match;
145}
146
147/*
148 * "refresh" does not calculate a new sha1 file or bring the
149 * cache up-to-date for mode/content changes. But what it
150 * _does_ do is to "re-match" the stat information of a file
151 * with the cache, so that you can refresh the cache for a
152 * file that hasn't been changed but where the stat entry is
153 * out of date.
154 *
155 * For example, you'd want to do this after doing a "read-tree",
156 * to link up the stat cache details with the proper files.
157 */
158static struct cache_entry *refresh_entry(struct cache_entry *ce)
159{
160 struct stat st;
161 struct cache_entry *updated;
162 int changed, size;
163
164 if (stat(ce->name, &st) < 0)
165 return NULL;
166
167 changed = cache_match_stat(ce, &st);
168 if (!changed)
169 return ce;
170
121481ab 171 /*
711cf3a0
LT
172 * If the length has changed, there's no point in trying
173 * to refresh the entry - it's not going to match
121481ab 174 */
711cf3a0
LT
175 if (changed & (DATA_CHANGED | MODE_CHANGED))
176 return NULL;
177
178 if (compare_data(ce))
179 return NULL;
180
181 size = ce_size(ce);
182 updated = malloc(size);
183 memcpy(updated, ce, size);
184 fill_stat_cache_info(updated, &st);
185 return updated;
121481ab
LT
186}
187
188static void refresh_cache(void)
189{
190 int i;
191
711cf3a0
LT
192 for (i = 0; i < active_nr; i++) {
193 struct cache_entry *ce = active_cache[i];
194 struct cache_entry *new = refresh_entry(ce);
195
196 if (!new) {
197 printf("%s: needs update\n", ce->name);
198 continue;
199 }
200 active_cache[i] = new;
201 }
e83c5163
LT
202}
203
e83c5163
LT
204/*
205 * We fundamentally don't like some paths: we don't want
206 * dot or dot-dot anywhere, and in fact, we don't even want
207 * any other dot-files (.dircache or anything else). They
208 * are hidden, for chist sake.
209 *
210 * Also, we don't want double slashes or slashes at the
211 * end that can make pathnames ambiguous.
212 */
213static int verify_path(char *path)
214{
215 char c;
216
217 goto inside;
218 for (;;) {
219 if (!c)
220 return 1;
221 if (c == '/') {
222inside:
223 c = *path++;
224 if (c != '/' && c != '.' && c != '\0')
225 continue;
226 return 0;
227 }
228 c = *path++;
229 }
230}
231
232int main(int argc, char **argv)
233{
234 int i, newfd, entries;
121481ab 235 int allow_options = 1;
e83c5163
LT
236
237 entries = read_cache();
238 if (entries < 0) {
239 perror("cache corrupted");
240 return -1;
241 }
242
243 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
244 if (newfd < 0) {
245 perror("unable to create new cachefile");
246 return -1;
247 }
248 for (i = 1 ; i < argc; i++) {
249 char *path = argv[i];
121481ab
LT
250
251 if (allow_options && *path == '-') {
252 if (!strcmp(path, "--")) {
253 allow_options = 0;
254 continue;
255 }
256 if (!strcmp(path, "--add")) {
257 allow_add = 1;
258 continue;
259 }
260 if (!strcmp(path, "--remove")) {
261 allow_remove = 1;
262 continue;
263 }
264 if (!strcmp(path, "--refresh")) {
265 refresh_cache();
266 continue;
267 }
268 usage("unknown option %s", path);
269 }
e83c5163
LT
270 if (!verify_path(path)) {
271 fprintf(stderr, "Ignoring path %s\n", argv[i]);
272 continue;
273 }
274 if (add_file_to_cache(path)) {
275 fprintf(stderr, "Unable to add %s to database\n", path);
276 goto out;
277 }
278 }
279 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
280 return 0;
281out:
282 unlink(".dircache/index.lock");
19b2860c 283 return 0;
e83c5163 284}