Fix "update-cache" not fixing up the size field as appropriate.
[git/dscho.git] / update-cache.c
blobb39185a1fecb18626fb3c3cc8bd5054096f3edfe
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
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.
15 static int allow_add = 0, allow_remove = 0;
17 static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
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;
26 close(fd);
27 if (!out || (int)(long)in == -1)
28 return -1;
30 memset(&stream, 0, sizeof(stream));
31 deflateInit(&stream, Z_BEST_COMPRESSION);
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 */;
44 * File content
46 stream.next_in = in;
47 stream.avail_in = st->st_size;
48 while (deflate(&stream, Z_FINISH) == Z_OK)
49 /*nothing */;
51 deflateEnd(&stream);
53 SHA1_Init(&c);
54 SHA1_Update(&c, out, stream.total_out);
55 SHA1_Final(ce->sha1, &c);
57 return write_sha1_buffer(ce->sha1, out, stream.total_out);
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.
65 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
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;
77 static int add_file_to_cache(char *path)
79 int size, namelen;
80 struct cache_entry *ce;
81 struct stat st;
82 int fd;
84 fd = open(path, O_RDONLY);
85 if (fd < 0) {
86 if (errno == ENOENT) {
87 if (allow_remove)
88 return remove_file_from_cache(path);
90 return -1;
92 if (fstat(fd, &st) < 0) {
93 close(fd);
94 return -1;
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);
101 fill_stat_cache_info(ce, &st);
102 ce->st_mode = st.st_mode;
103 ce->st_size = st.st_size;
104 ce->namelen = namelen;
106 if (index_fd(path, namelen, ce, fd, &st) < 0)
107 return -1;
109 return add_cache_entry(ce, allow_add);
112 static int match_data(int fd, void *buffer, unsigned long size)
114 while (size) {
115 char compare[1024];
116 int ret = read(fd, compare, sizeof(compare));
118 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
119 return -1;
120 size -= ret;
121 buffer += ret;
123 return 0;
126 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
128 int match = -1;
129 int fd = open(ce->name, O_RDONLY);
131 if (fd >= 0) {
132 void *buffer;
133 unsigned long size;
134 char type[10];
136 buffer = read_sha1_file(ce->sha1, type, &size);
137 if (buffer) {
138 if (size == expected_size && !strcmp(type, "blob"))
139 match = match_data(fd, buffer, size);
140 free(buffer);
142 close(fd);
144 return match;
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.
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.
158 static struct cache_entry *refresh_entry(struct cache_entry *ce)
160 struct stat st;
161 struct cache_entry *updated;
162 int changed, size;
164 if (stat(ce->name, &st) < 0)
165 return NULL;
167 changed = cache_match_stat(ce, &st);
168 if (!changed)
169 return ce;
172 * If the mode has changed, there's no point in trying
173 * to refresh the entry - it's not going to match
175 if (changed & MODE_CHANGED)
176 return NULL;
178 if (compare_data(ce, st.st_size))
179 return NULL;
181 size = ce_size(ce);
182 updated = malloc(size);
183 memcpy(updated, ce, size);
184 fill_stat_cache_info(updated, &st);
185 updated->st_size = st.st_size;
186 return updated;
189 static void refresh_cache(void)
191 int i;
193 for (i = 0; i < active_nr; i++) {
194 struct cache_entry *ce = active_cache[i];
195 struct cache_entry *new = refresh_entry(ce);
197 if (!new) {
198 printf("%s: needs update\n", ce->name);
199 continue;
201 active_cache[i] = new;
206 * We fundamentally don't like some paths: we don't want
207 * dot or dot-dot anywhere, and in fact, we don't even want
208 * any other dot-files (.dircache or anything else). They
209 * are hidden, for chist sake.
211 * Also, we don't want double slashes or slashes at the
212 * end that can make pathnames ambiguous.
214 static int verify_path(char *path)
216 char c;
218 goto inside;
219 for (;;) {
220 if (!c)
221 return 1;
222 if (c == '/') {
223 inside:
224 c = *path++;
225 if (c != '/' && c != '.' && c != '\0')
226 continue;
227 return 0;
229 c = *path++;
233 int main(int argc, char **argv)
235 int i, newfd, entries;
236 int allow_options = 1;
238 entries = read_cache();
239 if (entries < 0) {
240 perror("cache corrupted");
241 return -1;
244 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
245 if (newfd < 0) {
246 perror("unable to create new cachefile");
247 return -1;
249 for (i = 1 ; i < argc; i++) {
250 char *path = argv[i];
252 if (allow_options && *path == '-') {
253 if (!strcmp(path, "--")) {
254 allow_options = 0;
255 continue;
257 if (!strcmp(path, "--add")) {
258 allow_add = 1;
259 continue;
261 if (!strcmp(path, "--remove")) {
262 allow_remove = 1;
263 continue;
265 if (!strcmp(path, "--refresh")) {
266 refresh_cache();
267 continue;
269 usage("unknown option %s", path);
271 if (!verify_path(path)) {
272 fprintf(stderr, "Ignoring path %s\n", argv[i]);
273 continue;
275 if (add_file_to_cache(path)) {
276 fprintf(stderr, "Unable to add %s to database\n", path);
277 goto out;
280 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
281 return 0;
282 out:
283 unlink(".dircache/index.lock");
284 return 0;