Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <endian.h>
26 #include "got_error.h"
28 #include "got_lib_fileindex.h"
30 const struct got_error *
31 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
32 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
33 uint8_t *commit_sha1)
34 {
35 struct stat sb;
36 size_t len;
38 if (lstat(ondisk_path, &sb) != 0)
39 return got_error_from_errno();
41 *entry = calloc(1, sizeof(**entry));
42 if (*entry == NULL)
43 return got_error_from_errno();
45 (*entry)->path = strdup(relpath);
46 if ((*entry)->path == NULL) {
47 const struct got_error *err = got_error_from_errno();
48 free(*entry);
49 *entry = NULL;
50 return err;
51 }
53 (*entry)->ctime_sec = sb.st_ctime;
54 (*entry)->ctime_nsec = sb.st_ctimensec;
55 (*entry)->mtime_sec = sb.st_mtime;
56 (*entry)->mtime_nsec = sb.st_mtimensec;
57 (*entry)->uid = sb.st_uid;
58 (*entry)->gid = sb.st_gid;
59 (*entry)->size = (sb.st_size & 0xffffffff);
60 if (sb.st_mode & S_IFLNK)
61 (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
62 else
63 (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
64 (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
65 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
66 memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
67 memcpy((*entry)->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
68 len = strlen(relpath);
69 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
70 len = GOT_INDEX_ENTRY_F_PATH_LEN;
71 (*entry)->flags |= len;
73 return NULL;
74 }
76 void
77 got_fileindex_entry_free(struct got_fileindex_entry *entry)
78 {
79 free(entry->path);
80 free(entry);
81 }
83 const struct got_error *
84 got_fileindex_entry_add(struct got_fileindex *fileindex,
85 struct got_fileindex_entry *entry)
86 {
87 /* TODO keep entries sorted by name */
88 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
89 fileindex->nentries++;
90 return NULL;
91 }
93 struct got_fileindex *
94 got_fileindex_alloc(void)
95 {
96 struct got_fileindex *fileindex;
98 fileindex = calloc(1, sizeof(*fileindex));
99 if (fileindex)
100 TAILQ_INIT(&fileindex->entries);
101 return fileindex;
104 void
105 got_fileindex_free(struct got_fileindex *fileindex)
107 struct got_fileindex_entry *entry;
109 while (!TAILQ_EMPTY(&fileindex->entries)) {
110 entry = TAILQ_FIRST(&fileindex->entries);
111 TAILQ_REMOVE(&fileindex->entries, entry, entry);
112 got_fileindex_entry_free(entry);
113 fileindex->nentries--;
115 free(fileindex);
118 static const struct got_error *
119 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
121 size_t n;
123 val = htobe64(val);
124 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
125 n = fwrite(&val, 1, sizeof(val), outfile);
126 if (n != sizeof(val))
127 return got_ferror(outfile, GOT_ERR_IO);
128 return NULL;
131 static const struct got_error *
132 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
134 size_t n;
136 val = htobe32(val);
137 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
138 n = fwrite(&val, 1, sizeof(val), outfile);
139 if (n != sizeof(val))
140 return got_ferror(outfile, GOT_ERR_IO);
141 return NULL;
144 static const struct got_error *
145 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
147 size_t n;
149 val = htobe16(val);
150 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
151 n = fwrite(&val, 1, sizeof(val), outfile);
152 if (n != sizeof(val))
153 return got_ferror(outfile, GOT_ERR_IO);
154 return NULL;
157 static const struct got_error *
158 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
160 size_t n, len, pad;
161 static const uint8_t zero[8] = { 0 };
163 len = strlen(path);
164 pad = (len % 8);
166 SHA1Update(ctx, path, len);
167 n = fwrite(path, 1, len, outfile);
168 if (n != len)
169 return got_ferror(outfile, GOT_ERR_IO);
170 if (pad == 0)
171 return NULL;
172 SHA1Update(ctx, zero, pad);
173 n = fwrite(zero, 1, pad, outfile);
174 if (n != pad)
175 return got_ferror(outfile, GOT_ERR_IO);
176 return NULL;
179 static const struct got_error *
180 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
181 FILE *outfile)
183 const struct got_error *err;
184 size_t n;
186 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
187 if (err)
188 return err;
189 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
190 if (err)
191 return err;
192 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
193 if (err)
194 return err;
195 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
196 if (err)
197 return err;
199 err = write_fileindex_val32(ctx, entry->uid, outfile);
200 if (err)
201 return err;
202 err = write_fileindex_val32(ctx, entry->gid, outfile);
203 if (err)
204 return err;
205 err = write_fileindex_val32(ctx, entry->size, outfile);
206 if (err)
207 return err;
209 err = write_fileindex_val16(ctx, entry->mode, outfile);
210 if (err)
211 return err;
213 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
214 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
215 if (n != SHA1_DIGEST_LENGTH)
216 return got_ferror(outfile, GOT_ERR_IO);
218 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
219 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
220 if (n != SHA1_DIGEST_LENGTH)
221 return got_ferror(outfile, GOT_ERR_IO);
223 err = write_fileindex_val32(ctx, entry->flags, outfile);
224 if (err)
225 return err;
227 err = write_fileindex_path(ctx, entry->path, outfile);
228 return err;
231 const struct got_error *
232 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
234 struct got_fileindex_hdr hdr;
235 struct got_fileindex_entry *entry;
236 SHA1_CTX ctx;
237 uint8_t sha1[SHA1_DIGEST_LENGTH];
238 size_t n;
239 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
240 sizeof(hdr.nentries);
241 uint8_t buf[len];
243 SHA1Init(&ctx);
245 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
246 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
247 hdr.nentries = htobe32(fileindex->nentries);
249 memcpy(buf, &hdr, len);
250 SHA1Update(&ctx, buf, len);
251 n = fwrite(buf, 1, len, outfile);
252 if (n != len)
253 return got_ferror(outfile, GOT_ERR_IO);
255 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
256 const struct got_error *err;
257 err = write_fileindex_entry(&ctx, entry, outfile);
258 if (err)
259 return err;
262 SHA1Final(sha1, &ctx);
263 n = fwrite(sha1, 1, sizeof(sha1), outfile);
264 if (n != sizeof(sha1))
265 return got_ferror(outfile, GOT_ERR_IO);
267 return NULL;
270 static const struct got_error *
271 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
273 size_t n;
275 n = fread(val, 1, sizeof(*val), infile);
276 if (n != sizeof(*val))
277 return got_ferror(infile, GOT_ERR_IO);
278 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
279 *val = be64toh(*val);
280 return NULL;
283 static const struct got_error *
284 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
286 size_t n;
288 n = fread(val, 1, sizeof(*val), infile);
289 if (n != sizeof(*val))
290 return got_ferror(infile, GOT_ERR_IO);
291 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
292 *val = be32toh(*val);
293 return NULL;
296 static const struct got_error *
297 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
299 size_t n;
301 n = fread(val, 1, sizeof(*val), infile);
302 if (n != sizeof(*val))
303 return got_ferror(infile, GOT_ERR_IO);
304 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
305 *val = be16toh(*val);
306 return NULL;
309 static const struct got_error *
310 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
312 const struct got_error *err = NULL;
313 uint8_t buf[8];
314 size_t n, len = 0, totlen = sizeof(buf);
316 *path = malloc(totlen);
317 if (*path == NULL)
318 return got_error_from_errno();
320 do {
321 n = fread(buf, 1, sizeof(buf), infile);
322 if (n != sizeof(buf))
323 return got_ferror(infile, GOT_ERR_IO);
324 if (len + sizeof(buf) > totlen) {
325 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
326 if (p == NULL) {
327 err = got_error_from_errno();
328 break;
330 totlen += sizeof(buf);
331 *path = p;
333 SHA1Update(ctx, buf, sizeof(buf));
334 memcpy(*path + len, buf, sizeof(buf));
335 len += sizeof(buf);
336 } while (strchr(buf, '\0') == NULL);
338 if (err) {
339 free(*path);
340 *path = NULL;
342 return err;
345 static const struct got_error *
346 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
347 FILE *infile)
349 const struct got_error *err;
350 struct got_fileindex_entry *entry;
351 size_t n;
353 *entryp = NULL;
355 entry = calloc(1, sizeof(*entry));
356 if (entry == NULL)
357 return got_error_from_errno();
359 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
360 if (err)
361 goto done;
362 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
363 if (err)
364 goto done;
365 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
366 if (err)
367 goto done;
368 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
369 if (err)
370 goto done;
372 err = read_fileindex_val32(&entry->uid, ctx, infile);
373 if (err)
374 goto done;
375 err = read_fileindex_val32(&entry->gid, ctx, infile);
376 if (err)
377 goto done;
378 err = read_fileindex_val32(&entry->size, ctx, infile);
379 if (err)
380 goto done;
382 err = read_fileindex_val16(&entry->mode, ctx, infile);
383 if (err)
384 goto done;
386 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
387 if (n != SHA1_DIGEST_LENGTH) {
388 err = got_ferror(infile, GOT_ERR_IO);
389 goto done;
391 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
393 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
394 if (n != SHA1_DIGEST_LENGTH) {
395 err = got_ferror(infile, GOT_ERR_IO);
396 goto done;
398 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
400 err = read_fileindex_val32(&entry->flags, ctx, infile);
401 if (err)
402 goto done;
404 err = read_fileindex_path(&entry->path, ctx, infile);
405 done:
406 if (err)
407 free(entry);
408 else
409 *entryp = entry;
410 return err;
413 const struct got_error *
414 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
416 const struct got_error *err = NULL;
417 struct got_fileindex_hdr hdr;
418 SHA1_CTX ctx;
419 struct got_fileindex_entry *entry;
420 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
421 uint8_t sha1[SHA1_DIGEST_LENGTH];
422 size_t n;
423 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
424 sizeof(hdr.nentries);
425 uint8_t buf[len];
426 int i;
428 SHA1Init(&ctx);
430 n = fread(buf, 1, len, infile);
431 if (n != len)
432 return got_ferror(infile, GOT_ERR_IO);
434 SHA1Update(&ctx, buf, len);
436 memcpy(&hdr, buf, len);
437 hdr.signature = be32toh(hdr.signature);
438 hdr.version = be32toh(hdr.version);
439 hdr.nentries = be32toh(hdr.nentries);
441 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
442 return got_error(GOT_ERR_FILEIDX_SIG);
443 if (hdr.version != GOT_FILE_INDEX_VERSION)
444 return got_error(GOT_ERR_FILEIDX_VER);
446 for (i = 0; i < hdr.nentries; i++) {
447 err = read_fileindex_entry(&entry, &ctx, infile);
448 if (err)
449 return err;
450 err = got_fileindex_entry_add(fileindex, entry);
451 if (err)
452 return err;
455 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
456 if (n != sizeof(sha1_expected))
457 return got_ferror(infile, GOT_ERR_IO);
458 SHA1Final(sha1, &ctx);
459 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
460 return got_error(GOT_ERR_FILEIDX_CSUM);
462 return NULL;