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