Initial revision of "git", the information manager from hell
[git.git] / read-cache.c
1 #include "cache.h"
2
3 const char *sha1_file_directory = NULL;
4 struct cache_entry **active_cache = NULL;
5 unsigned int active_nr = 0, active_alloc = 0;
6
7 void usage(const char *err)
8 {
9         fprintf(stderr, "read-tree: %s\n", err);
10         exit(1);
11 }
12
13 static unsigned hexval(char c)
14 {
15         if (c >= '0' && c <= '9')
16                 return c - '0';
17         if (c >= 'a' && c <= 'f')
18                 return c - 'a' + 10;
19         if (c >= 'A' && c <= 'F')
20                 return c - 'A' + 10;
21         return ~0;
22 }
23
24 int get_sha1_hex(char *hex, unsigned char *sha1)
25 {
26         int i;
27         for (i = 0; i < 20; i++) {
28                 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
29                 if (val & ~0xff)
30                         return -1;
31                 *sha1++ = val;
32                 hex += 2;
33         }
34         return 0;
35 }
36
37 char * sha1_to_hex(unsigned char *sha1)
38 {
39         static char buffer[50];
40         static const char hex[] = "0123456789abcdef";
41         char *buf = buffer;
42         int i;
43
44         for (i = 0; i < 20; i++) {
45                 unsigned int val = *sha1++;
46                 *buf++ = hex[val >> 4];
47                 *buf++ = hex[val & 0xf];
48         }
49         return buffer;
50 }
51
52 /*
53  * NOTE! This returns a statically allocated buffer, so you have to be
54  * careful about using it. Do a "strdup()" if you need to save the
55  * filename.
56  */
57 char *sha1_file_name(unsigned char *sha1)
58 {
59         int i;
60         static char *name, *base;
61
62         if (!base) {
63                 char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
64                 int len = strlen(sha1_file_directory);
65                 base = malloc(len + 60);
66                 memcpy(base, sha1_file_directory, len);
67                 memset(base+len, 0, 60);
68                 base[len] = '/';
69                 base[len+3] = '/';
70                 name = base + len + 1;
71         }
72         for (i = 0; i < 20; i++) {
73                 static char hex[] = "0123456789abcdef";
74                 unsigned int val = sha1[i];
75                 char *pos = name + i*2 + (i > 0);
76                 *pos++ = hex[val >> 4];
77                 *pos = hex[val & 0xf];
78         }
79         return base;
80 }
81
82 void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
83 {
84         z_stream stream;
85         char buffer[8192];
86         struct stat st;
87         int i, fd, ret, bytes;
88         void *map, *buf;
89         char *filename = sha1_file_name(sha1);
90
91         fd = open(filename, O_RDONLY);
92         if (fd < 0) {
93                 perror(filename);
94                 return NULL;
95         }
96         if (fstat(fd, &st) < 0) {
97                 close(fd);
98                 return NULL;
99         }
100         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
101         close(fd);
102         if (-1 == (int)(long)map)
103                 return NULL;
104
105         /* Get the data stream */
106         memset(&stream, 0, sizeof(stream));
107         stream.next_in = map;
108         stream.avail_in = st.st_size;
109         stream.next_out = buffer;
110         stream.avail_out = sizeof(buffer);
111
112         inflateInit(&stream);
113         ret = inflate(&stream, 0);
114         if (sscanf(buffer, "%10s %lu", type, size) != 2)
115                 return NULL;
116         bytes = strlen(buffer) + 1;
117         buf = malloc(*size);
118         if (!buf)
119                 return NULL;
120
121         memcpy(buf, buffer + bytes, stream.total_out - bytes);
122         bytes = stream.total_out - bytes;
123         if (bytes < *size && ret == Z_OK) {
124                 stream.next_out = buf + bytes;
125                 stream.avail_out = *size - bytes;
126                 while (inflate(&stream, Z_FINISH) == Z_OK)
127                         /* nothing */;
128         }
129         inflateEnd(&stream);
130         return buf;
131 }
132
133 int write_sha1_file(char *buf, unsigned len)
134 {
135         int size;
136         char *compressed;
137         z_stream stream;
138         unsigned char sha1[20];
139         SHA_CTX c;
140
141         /* Set it up */
142         memset(&stream, 0, sizeof(stream));
143         deflateInit(&stream, Z_BEST_COMPRESSION);
144         size = deflateBound(&stream, len);
145         compressed = malloc(size);
146
147         /* Compress it */
148         stream.next_in = buf;
149         stream.avail_in = len;
150         stream.next_out = compressed;
151         stream.avail_out = size;
152         while (deflate(&stream, Z_FINISH) == Z_OK)
153                 /* nothing */;
154         deflateEnd(&stream);
155         size = stream.total_out;
156
157         /* Sha1.. */
158         SHA1_Init(&c);
159         SHA1_Update(&c, compressed, size);
160         SHA1_Final(sha1, &c);
161
162         if (write_sha1_buffer(sha1, compressed, size) < 0)
163                 return -1;
164         printf("%s\n", sha1_to_hex(sha1));
165         return 0;
166 }
167
168 int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
169 {
170         char *filename = sha1_file_name(sha1);
171         int i, fd;
172
173         fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
174         if (fd < 0)
175                 return (errno == EEXIST) ? 0 : -1;
176         write(fd, buf, size);
177         close(fd);
178         return 0;
179 }
180
181 static int error(const char * string)
182 {
183         fprintf(stderr, "error: %s\n", string);
184         return -1;
185 }
186
187 static int verify_hdr(struct cache_header *hdr, unsigned long size)
188 {
189         SHA_CTX c;
190         unsigned char sha1[20];
191
192         if (hdr->signature != CACHE_SIGNATURE)
193                 return error("bad signature");
194         if (hdr->version != 1)
195                 return error("bad version");
196         SHA1_Init(&c);
197         SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
198         SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
199         SHA1_Final(sha1, &c);
200         if (memcmp(sha1, hdr->sha1, 20))
201                 return error("bad header sha1");
202         return 0;
203 }
204
205 int read_cache(void)
206 {
207         int fd, i;
208         struct stat st;
209         unsigned long size, offset;
210         void *map;
211         struct cache_header *hdr;
212
213         errno = EBUSY;
214         if (active_cache)
215                 return error("more than one cachefile");
216         errno = ENOENT;
217         sha1_file_directory = getenv(DB_ENVIRONMENT);
218         if (!sha1_file_directory)
219                 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
220         if (access(sha1_file_directory, X_OK) < 0)
221                 return error("no access to SHA1 file directory");
222         fd = open(".dircache/index", O_RDONLY);
223         if (fd < 0)
224                 return (errno == ENOENT) ? 0 : error("open failed");
225
226         map = (void *)-1;
227         if (!fstat(fd, &st)) {
228                 map = NULL;
229                 size = st.st_size;
230                 errno = EINVAL;
231                 if (size > sizeof(struct cache_header))
232                         map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
233         }
234         close(fd);
235         if (-1 == (int)(long)map)
236                 return error("mmap failed");
237
238         hdr = map;
239         if (verify_hdr(hdr, size) < 0)
240                 goto unmap;
241
242         active_nr = hdr->entries;
243         active_alloc = alloc_nr(active_nr);
244         active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
245
246         offset = sizeof(*hdr);
247         for (i = 0; i < hdr->entries; i++) {
248                 struct cache_entry *ce = map + offset;
249                 offset = offset + ce_size(ce);
250                 active_cache[i] = ce;
251         }
252         return active_nr;
253
254 unmap:
255         munmap(map, size);
256         errno = EINVAL;
257         return error("verify header failed");
258 }
259