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