0bcd5d408191333e70f54fbf28676a7e3696ced5
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
8static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
9{
10 int len = len1 < len2 ? len1 : len2;
11 int cmp;
12
13 cmp = memcmp(name1, name2, len);
14 if (cmp)
15 return cmp;
16 if (len1 < len2)
17 return -1;
18 if (len1 > len2)
19 return 1;
20 return 0;
21}
22
23static int cache_name_pos(const char *name, int namelen)
24{
25 int first, last;
26
27 first = 0;
28 last = active_nr;
29 while (last > first) {
30 int next = (last + first) >> 1;
31 struct cache_entry *ce = active_cache[next];
32 int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
33 if (!cmp)
34 return -next-1;
35 if (cmp < 0) {
36 last = next;
37 continue;
38 }
39 first = next+1;
40 }
41 return first;
42}
43
44static int remove_file_from_cache(char *path)
45{
46 int pos = cache_name_pos(path, strlen(path));
47 if (pos < 0) {
48 pos = -pos-1;
49 active_nr--;
50 if (pos < active_nr)
51 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
52 }
53}
54
55static int add_cache_entry(struct cache_entry *ce)
56{
57 int pos;
58
59 pos = cache_name_pos(ce->name, ce->namelen);
60
61 /* existing match? Just replace it */
62 if (pos < 0) {
63 active_cache[-pos-1] = ce;
64 return 0;
65 }
66
67 /* Make sure the array is big enough .. */
68 if (active_nr == active_alloc) {
69 active_alloc = alloc_nr(active_alloc);
70 active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
71 }
72
73 /* Add it in.. */
74 active_nr++;
75 if (active_nr > pos)
76 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
77 active_cache[pos] = ce;
78 return 0;
79}
80
81static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
82{
83 z_stream stream;
84 int max_out_bytes = namelen + st->st_size + 200;
85 void *out = malloc(max_out_bytes);
86 void *metadata = malloc(namelen + 200);
87 void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
88 SHA_CTX c;
89
90 close(fd);
91 if (!out || (int)(long)in == -1)
92 return -1;
93
94 memset(&stream, 0, sizeof(stream));
95 deflateInit(&stream, Z_BEST_COMPRESSION);
96
97 /*
98 * ASCII size + nul byte
99 */
100 stream.next_in = metadata;
101 stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
102 stream.next_out = out;
103 stream.avail_out = max_out_bytes;
104 while (deflate(&stream, 0) == Z_OK)
105 /* nothing */;
106
107 /*
108 * File content
109 */
110 stream.next_in = in;
111 stream.avail_in = st->st_size;
112 while (deflate(&stream, Z_FINISH) == Z_OK)
113 /*nothing */;
114
115 deflateEnd(&stream);
116
117 SHA1_Init(&c);
118 SHA1_Update(&c, out, stream.total_out);
119 SHA1_Final(ce->sha1, &c);
120
121 return write_sha1_buffer(ce->sha1, out, stream.total_out);
122}
123
124static int add_file_to_cache(char *path)
125{
126 int size, namelen;
127 struct cache_entry *ce;
128 struct stat st;
129 int fd;
130
131 fd = open(path, O_RDONLY);
132 if (fd < 0) {
133 if (errno == ENOENT)
134 return remove_file_from_cache(path);
135 return -1;
136 }
137 if (fstat(fd, &st) < 0) {
138 close(fd);
139 return -1;
140 }
141 namelen = strlen(path);
142 size = cache_entry_size(namelen);
143 ce = malloc(size);
144 memset(ce, 0, size);
145 memcpy(ce->name, path, namelen);
146 ce->ctime.sec = st.st_ctime;
147 ce->ctime.nsec = st.st_ctim.tv_nsec;
148 ce->mtime.sec = st.st_mtime;
149 ce->mtime.nsec = st.st_mtim.tv_nsec;
150 ce->st_dev = st.st_dev;
151 ce->st_ino = st.st_ino;
152 ce->st_mode = st.st_mode;
153 ce->st_uid = st.st_uid;
154 ce->st_gid = st.st_gid;
155 ce->st_size = st.st_size;
156 ce->namelen = namelen;
157
158 if (index_fd(path, namelen, ce, fd, &st) < 0)
159 return -1;
160
161 return add_cache_entry(ce);
162}
163
164static int write_cache(int newfd, struct cache_entry **cache, int entries)
165{
166 SHA_CTX c;
167 struct cache_header hdr;
168 int i;
169
170 hdr.signature = CACHE_SIGNATURE;
171 hdr.version = 1;
172 hdr.entries = entries;
173
174 SHA1_Init(&c);
175 SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
176 for (i = 0; i < entries; i++) {
177 struct cache_entry *ce = cache[i];
178 int size = ce_size(ce);
179 SHA1_Update(&c, ce, size);
180 }
181 SHA1_Final(hdr.sha1, &c);
182
183 if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
184 return -1;
185
186 for (i = 0; i < entries; i++) {
187 struct cache_entry *ce = cache[i];
188 int size = ce_size(ce);
189 if (write(newfd, ce, size) != size)
190 return -1;
191 }
192 return 0;
193}
194
195/*
196 * We fundamentally don't like some paths: we don't want
197 * dot or dot-dot anywhere, and in fact, we don't even want
198 * any other dot-files (.dircache or anything else). They
199 * are hidden, for chist sake.
200 *
201 * Also, we don't want double slashes or slashes at the
202 * end that can make pathnames ambiguous.
203 */
204static int verify_path(char *path)
205{
206 char c;
207
208 goto inside;
209 for (;;) {
210 if (!c)
211 return 1;
212 if (c == '/') {
213inside:
214 c = *path++;
215 if (c != '/' && c != '.' && c != '\0')
216 continue;
217 return 0;
218 }
219 c = *path++;
220 }
221}
222
223int main(int argc, char **argv)
224{
225 int i, newfd, entries;
226
227 entries = read_cache();
228 if (entries < 0) {
229 perror("cache corrupted");
230 return -1;
231 }
232
233 newfd = open(".dircache/index.lock", O_RDWR | O_CREAT | O_EXCL, 0600);
234 if (newfd < 0) {
235 perror("unable to create new cachefile");
236 return -1;
237 }
238 for (i = 1 ; i < argc; i++) {
239 char *path = argv[i];
240 if (!verify_path(path)) {
241 fprintf(stderr, "Ignoring path %s\n", argv[i]);
242 continue;
243 }
244 if (add_file_to_cache(path)) {
245 fprintf(stderr, "Unable to add %s to database\n", path);
246 goto out;
247 }
248 }
249 if (!write_cache(newfd, active_cache, active_nr) && !rename(".dircache/index.lock", ".dircache/index"))
250 return 0;
251out:
252 unlink(".dircache/index.lock");
253}