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_update(struct got_fileindex_entry *entry,
32 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
33 {
34 struct stat sb;
36 if (lstat(ondisk_path, &sb) != 0)
37 return got_error_from_errno();
39 entry->ctime_sec = sb.st_ctime;
40 entry->ctime_nsec = sb.st_ctimensec;
41 entry->mtime_sec = sb.st_mtime;
42 entry->mtime_nsec = sb.st_mtimensec;
43 entry->uid = sb.st_uid;
44 entry->gid = sb.st_gid;
45 entry->size = (sb.st_size & 0xffffffff);
46 if (sb.st_mode & S_IFLNK)
47 entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
48 else
49 entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
50 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
51 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
52 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
53 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
55 return NULL;
56 }
58 const struct got_error *
59 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
60 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
61 uint8_t *commit_sha1)
62 {
63 size_t len;
65 *entry = calloc(1, sizeof(**entry));
66 if (*entry == NULL)
67 return got_error_from_errno();
69 (*entry)->path = strdup(relpath);
70 if ((*entry)->path == NULL) {
71 const struct got_error *err = got_error_from_errno();
72 free(*entry);
73 *entry = NULL;
74 return err;
75 }
77 len = strlen(relpath);
78 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
79 len = GOT_INDEX_ENTRY_F_PATH_LEN;
80 (*entry)->flags |= len;
82 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
83 commit_sha1);
84 }
86 void
87 got_fileindex_entry_free(struct got_fileindex_entry *entry)
88 {
89 free(entry->path);
90 free(entry);
91 }
93 const struct got_error *
94 got_fileindex_entry_add(struct got_fileindex *fileindex,
95 struct got_fileindex_entry *entry)
96 {
97 /* TODO keep entries sorted by name */
98 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
99 fileindex->nentries++;
100 return NULL;
103 void
104 got_fileindex_entry_remove(struct got_fileindex *fileindex,
105 struct got_fileindex_entry *entry)
107 TAILQ_REMOVE(&fileindex->entries, entry, entry);
108 fileindex->nentries--;
111 struct got_fileindex_entry *
112 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
114 struct got_fileindex_entry *entry;
115 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
116 if (strcmp(entry->path, path) == 0)
117 return entry;
120 return NULL;
123 const struct got_error *
124 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
125 const struct got_error *(cb)(void *, struct got_fileindex_entry *),
126 void *cb_arg)
128 const struct got_error *err = NULL;
129 struct got_fileindex_entry *entry, *tmp;
131 TAILQ_FOREACH_SAFE(entry, &fileindex->entries, entry, tmp) {
132 err = cb(cb_arg, entry);
133 if (err)
134 break;
137 return err;
140 struct got_fileindex *
141 got_fileindex_alloc(void)
143 struct got_fileindex *fileindex;
145 fileindex = calloc(1, sizeof(*fileindex));
146 if (fileindex)
147 TAILQ_INIT(&fileindex->entries);
148 return fileindex;
151 void
152 got_fileindex_free(struct got_fileindex *fileindex)
154 struct got_fileindex_entry *entry;
156 while (!TAILQ_EMPTY(&fileindex->entries)) {
157 entry = TAILQ_FIRST(&fileindex->entries);
158 TAILQ_REMOVE(&fileindex->entries, entry, entry);
159 got_fileindex_entry_free(entry);
160 fileindex->nentries--;
162 free(fileindex);
165 static const struct got_error *
166 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
168 size_t n;
170 val = htobe64(val);
171 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
172 n = fwrite(&val, 1, sizeof(val), outfile);
173 if (n != sizeof(val))
174 return got_ferror(outfile, GOT_ERR_IO);
175 return NULL;
178 static const struct got_error *
179 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
181 size_t n;
183 val = htobe32(val);
184 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
185 n = fwrite(&val, 1, sizeof(val), outfile);
186 if (n != sizeof(val))
187 return got_ferror(outfile, GOT_ERR_IO);
188 return NULL;
191 static const struct got_error *
192 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
194 size_t n;
196 val = htobe16(val);
197 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
198 n = fwrite(&val, 1, sizeof(val), outfile);
199 if (n != sizeof(val))
200 return got_ferror(outfile, GOT_ERR_IO);
201 return NULL;
204 static const struct got_error *
205 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
207 size_t n, len, pad = 0;
208 static const uint8_t zero[8] = { 0 };
210 len = strlen(path);
211 while ((len + pad) % 8 != 0)
212 pad++;
213 if (pad == 0)
214 pad = 8; /* NUL-terminate */
216 SHA1Update(ctx, path, len);
217 n = fwrite(path, 1, len, outfile);
218 if (n != len)
219 return got_ferror(outfile, GOT_ERR_IO);
220 SHA1Update(ctx, zero, pad);
221 n = fwrite(zero, 1, pad, outfile);
222 if (n != pad)
223 return got_ferror(outfile, GOT_ERR_IO);
224 return NULL;
227 static const struct got_error *
228 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
229 FILE *outfile)
231 const struct got_error *err;
232 size_t n;
234 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
235 if (err)
236 return err;
237 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
238 if (err)
239 return err;
240 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
241 if (err)
242 return err;
243 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
244 if (err)
245 return err;
247 err = write_fileindex_val32(ctx, entry->uid, outfile);
248 if (err)
249 return err;
250 err = write_fileindex_val32(ctx, entry->gid, outfile);
251 if (err)
252 return err;
253 err = write_fileindex_val32(ctx, entry->size, outfile);
254 if (err)
255 return err;
257 err = write_fileindex_val16(ctx, entry->mode, outfile);
258 if (err)
259 return err;
261 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
262 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
263 if (n != SHA1_DIGEST_LENGTH)
264 return got_ferror(outfile, GOT_ERR_IO);
266 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
267 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
268 if (n != SHA1_DIGEST_LENGTH)
269 return got_ferror(outfile, GOT_ERR_IO);
271 err = write_fileindex_val32(ctx, entry->flags, outfile);
272 if (err)
273 return err;
275 err = write_fileindex_path(ctx, entry->path, outfile);
276 return err;
279 const struct got_error *
280 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
282 struct got_fileindex_hdr hdr;
283 struct got_fileindex_entry *entry;
284 SHA1_CTX ctx;
285 uint8_t sha1[SHA1_DIGEST_LENGTH];
286 size_t n;
287 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
288 sizeof(hdr.nentries);
289 uint8_t buf[len];
291 SHA1Init(&ctx);
293 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
294 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
295 hdr.nentries = htobe32(fileindex->nentries);
297 memcpy(buf, &hdr, len);
298 SHA1Update(&ctx, buf, len);
299 n = fwrite(buf, 1, len, outfile);
300 if (n != len)
301 return got_ferror(outfile, GOT_ERR_IO);
303 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
304 const struct got_error *err;
305 err = write_fileindex_entry(&ctx, entry, outfile);
306 if (err)
307 return err;
310 SHA1Final(sha1, &ctx);
311 n = fwrite(sha1, 1, sizeof(sha1), outfile);
312 if (n != sizeof(sha1))
313 return got_ferror(outfile, GOT_ERR_IO);
315 return NULL;
318 static const struct got_error *
319 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
321 size_t n;
323 n = fread(val, 1, sizeof(*val), infile);
324 if (n != sizeof(*val))
325 return got_ferror(infile, GOT_ERR_IO);
326 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
327 *val = be64toh(*val);
328 return NULL;
331 static const struct got_error *
332 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
334 size_t n;
336 n = fread(val, 1, sizeof(*val), infile);
337 if (n != sizeof(*val))
338 return got_ferror(infile, GOT_ERR_IO);
339 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
340 *val = be32toh(*val);
341 return NULL;
344 static const struct got_error *
345 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
347 size_t n;
349 n = fread(val, 1, sizeof(*val), infile);
350 if (n != sizeof(*val))
351 return got_ferror(infile, GOT_ERR_IO);
352 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
353 *val = be16toh(*val);
354 return NULL;
357 static const struct got_error *
358 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
360 const struct got_error *err = NULL;
361 uint8_t buf[8];
362 size_t n, len = 0, totlen = sizeof(buf);
364 *path = malloc(totlen);
365 if (*path == NULL)
366 return got_error_from_errno();
368 do {
369 n = fread(buf, 1, sizeof(buf), infile);
370 if (n != sizeof(buf))
371 return got_ferror(infile, GOT_ERR_IO);
372 if (len + sizeof(buf) > totlen) {
373 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
374 if (p == NULL) {
375 err = got_error_from_errno();
376 break;
378 totlen += sizeof(buf);
379 *path = p;
381 SHA1Update(ctx, buf, sizeof(buf));
382 memcpy(*path + len, buf, sizeof(buf));
383 len += sizeof(buf);
384 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
386 if (err) {
387 free(*path);
388 *path = NULL;
390 return err;
393 static const struct got_error *
394 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
395 FILE *infile)
397 const struct got_error *err;
398 struct got_fileindex_entry *entry;
399 size_t n;
401 *entryp = NULL;
403 entry = calloc(1, sizeof(*entry));
404 if (entry == NULL)
405 return got_error_from_errno();
407 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
408 if (err)
409 goto done;
410 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
411 if (err)
412 goto done;
413 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
414 if (err)
415 goto done;
416 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
417 if (err)
418 goto done;
420 err = read_fileindex_val32(&entry->uid, ctx, infile);
421 if (err)
422 goto done;
423 err = read_fileindex_val32(&entry->gid, ctx, infile);
424 if (err)
425 goto done;
426 err = read_fileindex_val32(&entry->size, ctx, infile);
427 if (err)
428 goto done;
430 err = read_fileindex_val16(&entry->mode, ctx, infile);
431 if (err)
432 goto done;
434 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
435 if (n != SHA1_DIGEST_LENGTH) {
436 err = got_ferror(infile, GOT_ERR_IO);
437 goto done;
439 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
441 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
442 if (n != SHA1_DIGEST_LENGTH) {
443 err = got_ferror(infile, GOT_ERR_IO);
444 goto done;
446 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
448 err = read_fileindex_val32(&entry->flags, ctx, infile);
449 if (err)
450 goto done;
452 err = read_fileindex_path(&entry->path, ctx, infile);
453 done:
454 if (err)
455 free(entry);
456 else
457 *entryp = entry;
458 return err;
461 const struct got_error *
462 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
464 const struct got_error *err = NULL;
465 struct got_fileindex_hdr hdr;
466 SHA1_CTX ctx;
467 struct got_fileindex_entry *entry;
468 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
469 uint8_t sha1[SHA1_DIGEST_LENGTH];
470 size_t n;
471 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
472 sizeof(hdr.nentries);
473 uint8_t buf[len];
474 int i;
476 SHA1Init(&ctx);
478 n = fread(buf, 1, len, infile);
479 if (n != len) {
480 if (n == 0) /* EOF */
481 return NULL;
482 return got_ferror(infile, GOT_ERR_IO);
485 SHA1Update(&ctx, buf, len);
487 memcpy(&hdr, buf, len);
488 hdr.signature = be32toh(hdr.signature);
489 hdr.version = be32toh(hdr.version);
490 hdr.nentries = be32toh(hdr.nentries);
492 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
493 return got_error(GOT_ERR_FILEIDX_SIG);
494 if (hdr.version != GOT_FILE_INDEX_VERSION)
495 return got_error(GOT_ERR_FILEIDX_VER);
497 for (i = 0; i < hdr.nentries; i++) {
498 err = read_fileindex_entry(&entry, &ctx, infile);
499 if (err)
500 return err;
501 err = got_fileindex_entry_add(fileindex, entry);
502 if (err)
503 return err;
506 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
507 if (n != sizeof(sha1_expected))
508 return got_ferror(infile, GOT_ERR_IO);
509 SHA1Final(sha1, &ctx);
510 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
511 return got_error(GOT_ERR_FILEIDX_CSUM);
513 return NULL;