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