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/tree.h>
19 #include <sys/stat.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sha1.h>
25 #include <endian.h>
26 #include <limits.h>
28 #include "got_error.h"
30 #include "got_lib_path.h"
31 #include "got_lib_fileindex.h"
33 struct got_fileindex {
34 struct got_fileindex_tree entries;
35 int nentries;
36 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
37 };
39 const struct got_error *
40 got_fileindex_entry_update(struct got_fileindex_entry *entry,
41 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
42 {
43 struct stat sb;
45 if (lstat(ondisk_path, &sb) != 0)
46 return got_error_from_errno();
48 entry->ctime_sec = sb.st_ctime;
49 entry->ctime_nsec = sb.st_ctimensec;
50 entry->mtime_sec = sb.st_mtime;
51 entry->mtime_nsec = sb.st_mtimensec;
52 entry->uid = sb.st_uid;
53 entry->gid = sb.st_gid;
54 entry->size = (sb.st_size & 0xffffffff);
55 if (sb.st_mode & S_IFLNK)
56 entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
57 else
58 entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
59 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
60 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
61 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
62 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
64 return NULL;
65 }
67 const struct got_error *
68 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
69 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
70 uint8_t *commit_sha1)
71 {
72 size_t len;
74 *entry = calloc(1, sizeof(**entry));
75 if (*entry == NULL)
76 return got_error_from_errno();
78 (*entry)->path = strdup(relpath);
79 if ((*entry)->path == NULL) {
80 const struct got_error *err = got_error_from_errno();
81 free(*entry);
82 *entry = NULL;
83 return err;
84 }
86 len = strlen(relpath);
87 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
88 len = GOT_INDEX_ENTRY_F_PATH_LEN;
89 (*entry)->flags |= len;
91 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
92 commit_sha1);
93 }
95 void
96 got_fileindex_entry_free(struct got_fileindex_entry *entry)
97 {
98 free(entry->path);
99 free(entry);
102 const struct got_error *
103 got_fileindex_entry_add(struct got_fileindex *fileindex,
104 struct got_fileindex_entry *entry)
106 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 return got_error(GOT_ERR_NO_SPACE);
109 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 fileindex->nentries++;
111 return NULL;
114 void
115 got_fileindex_entry_remove(struct got_fileindex *fileindex,
116 struct got_fileindex_entry *entry)
118 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
119 fileindex->nentries--;
122 struct got_fileindex_entry *
123 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
125 struct got_fileindex_entry key;
126 memset(&key, 0, sizeof(key));
127 key.path = (char *)path;
128 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
131 const struct got_error *
132 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
133 got_fileindex_cb cb, void *cb_arg)
135 const struct got_error *err;
136 struct got_fileindex_entry *entry, *tmp;
138 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
139 err = (*cb)(cb_arg, entry);
140 if (err)
141 return err;
143 return NULL;
146 struct got_fileindex *
147 got_fileindex_alloc(void)
149 struct got_fileindex *fileindex;
151 fileindex = calloc(1, sizeof(*fileindex));
152 if (fileindex == NULL)
153 return NULL;
155 RB_INIT(&fileindex->entries);
156 return fileindex;
159 void
160 got_fileindex_free(struct got_fileindex *fileindex)
162 struct got_fileindex_entry *entry;
164 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
165 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 got_fileindex_entry_free(entry);
168 free(fileindex);
171 static const struct got_error *
172 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
174 size_t n;
176 val = htobe64(val);
177 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
178 n = fwrite(&val, 1, sizeof(val), outfile);
179 if (n != sizeof(val))
180 return got_ferror(outfile, GOT_ERR_IO);
181 return NULL;
184 static const struct got_error *
185 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
187 size_t n;
189 val = htobe32(val);
190 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
191 n = fwrite(&val, 1, sizeof(val), outfile);
192 if (n != sizeof(val))
193 return got_ferror(outfile, GOT_ERR_IO);
194 return NULL;
197 static const struct got_error *
198 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
200 size_t n;
202 val = htobe16(val);
203 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
204 n = fwrite(&val, 1, sizeof(val), outfile);
205 if (n != sizeof(val))
206 return got_ferror(outfile, GOT_ERR_IO);
207 return NULL;
210 static const struct got_error *
211 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
213 size_t n, len, pad = 0;
214 static const uint8_t zero[8] = { 0 };
216 len = strlen(path);
217 while ((len + pad) % 8 != 0)
218 pad++;
219 if (pad == 0)
220 pad = 8; /* NUL-terminate */
222 SHA1Update(ctx, path, len);
223 n = fwrite(path, 1, len, outfile);
224 if (n != len)
225 return got_ferror(outfile, GOT_ERR_IO);
226 SHA1Update(ctx, zero, pad);
227 n = fwrite(zero, 1, pad, outfile);
228 if (n != pad)
229 return got_ferror(outfile, GOT_ERR_IO);
230 return NULL;
233 static const struct got_error *
234 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
235 FILE *outfile)
237 const struct got_error *err;
238 size_t n;
240 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
241 if (err)
242 return err;
243 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
244 if (err)
245 return err;
246 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
247 if (err)
248 return err;
249 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
250 if (err)
251 return err;
253 err = write_fileindex_val32(ctx, entry->uid, outfile);
254 if (err)
255 return err;
256 err = write_fileindex_val32(ctx, entry->gid, outfile);
257 if (err)
258 return err;
259 err = write_fileindex_val32(ctx, entry->size, outfile);
260 if (err)
261 return err;
263 err = write_fileindex_val16(ctx, entry->mode, outfile);
264 if (err)
265 return err;
267 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
268 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
269 if (n != SHA1_DIGEST_LENGTH)
270 return got_ferror(outfile, GOT_ERR_IO);
272 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
273 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
274 if (n != SHA1_DIGEST_LENGTH)
275 return got_ferror(outfile, GOT_ERR_IO);
277 err = write_fileindex_val32(ctx, entry->flags, outfile);
278 if (err)
279 return err;
281 err = write_fileindex_path(ctx, entry->path, outfile);
282 return err;
285 const struct got_error *
286 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
288 const struct got_error *err = NULL;
289 struct got_fileindex_hdr hdr;
290 SHA1_CTX ctx;
291 uint8_t sha1[SHA1_DIGEST_LENGTH];
292 size_t n;
293 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
294 sizeof(hdr.nentries);
295 uint8_t buf[len];
296 struct got_fileindex_entry *entry;
298 SHA1Init(&ctx);
300 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
301 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
302 hdr.nentries = htobe32(fileindex->nentries);
304 memcpy(buf, &hdr, len);
305 SHA1Update(&ctx, buf, len);
306 n = fwrite(buf, 1, len, outfile);
307 if (n != len)
308 return got_ferror(outfile, GOT_ERR_IO);
310 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
311 err = write_fileindex_entry(&ctx, entry, outfile);
312 if (err)
313 return err;
316 SHA1Final(sha1, &ctx);
317 n = fwrite(sha1, 1, sizeof(sha1), outfile);
318 if (n != sizeof(sha1))
319 return got_ferror(outfile, GOT_ERR_IO);
321 return NULL;
324 static const struct got_error *
325 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
327 size_t n;
329 n = fread(val, 1, sizeof(*val), infile);
330 if (n != sizeof(*val))
331 return got_ferror(infile, GOT_ERR_IO);
332 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
333 *val = be64toh(*val);
334 return NULL;
337 static const struct got_error *
338 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
340 size_t n;
342 n = fread(val, 1, sizeof(*val), infile);
343 if (n != sizeof(*val))
344 return got_ferror(infile, GOT_ERR_IO);
345 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
346 *val = be32toh(*val);
347 return NULL;
350 static const struct got_error *
351 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
353 size_t n;
355 n = fread(val, 1, sizeof(*val), infile);
356 if (n != sizeof(*val))
357 return got_ferror(infile, GOT_ERR_IO);
358 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
359 *val = be16toh(*val);
360 return NULL;
363 static const struct got_error *
364 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
366 const struct got_error *err = NULL;
367 uint8_t buf[8];
368 size_t n, len = 0, totlen = sizeof(buf);
370 *path = malloc(totlen);
371 if (*path == NULL)
372 return got_error_from_errno();
374 do {
375 n = fread(buf, 1, sizeof(buf), infile);
376 if (n != sizeof(buf))
377 return got_ferror(infile, GOT_ERR_IO);
378 if (len + sizeof(buf) > totlen) {
379 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
380 if (p == NULL) {
381 err = got_error_from_errno();
382 break;
384 totlen += sizeof(buf);
385 *path = p;
387 SHA1Update(ctx, buf, sizeof(buf));
388 memcpy(*path + len, buf, sizeof(buf));
389 len += sizeof(buf);
390 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
392 if (err) {
393 free(*path);
394 *path = NULL;
396 return err;
399 static const struct got_error *
400 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
401 FILE *infile)
403 const struct got_error *err;
404 struct got_fileindex_entry *entry;
405 size_t n;
407 *entryp = NULL;
409 entry = calloc(1, sizeof(*entry));
410 if (entry == NULL)
411 return got_error_from_errno();
413 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
414 if (err)
415 goto done;
416 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
417 if (err)
418 goto done;
419 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
420 if (err)
421 goto done;
422 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
423 if (err)
424 goto done;
426 err = read_fileindex_val32(&entry->uid, ctx, infile);
427 if (err)
428 goto done;
429 err = read_fileindex_val32(&entry->gid, ctx, infile);
430 if (err)
431 goto done;
432 err = read_fileindex_val32(&entry->size, ctx, infile);
433 if (err)
434 goto done;
436 err = read_fileindex_val16(&entry->mode, ctx, infile);
437 if (err)
438 goto done;
440 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
441 if (n != SHA1_DIGEST_LENGTH) {
442 err = got_ferror(infile, GOT_ERR_IO);
443 goto done;
445 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
447 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
448 if (n != SHA1_DIGEST_LENGTH) {
449 err = got_ferror(infile, GOT_ERR_IO);
450 goto done;
452 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
454 err = read_fileindex_val32(&entry->flags, ctx, infile);
455 if (err)
456 goto done;
458 err = read_fileindex_path(&entry->path, ctx, infile);
459 done:
460 if (err)
461 free(entry);
462 else
463 *entryp = entry;
464 return err;
467 const struct got_error *
468 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
470 const struct got_error *err = NULL;
471 struct got_fileindex_hdr hdr;
472 SHA1_CTX ctx;
473 struct got_fileindex_entry *entry;
474 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
475 uint8_t sha1[SHA1_DIGEST_LENGTH];
476 size_t n;
477 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
478 sizeof(hdr.nentries);
479 uint8_t buf[len];
480 int i;
482 SHA1Init(&ctx);
484 n = fread(buf, 1, len, infile);
485 if (n != len) {
486 if (n == 0) /* EOF */
487 return NULL;
488 return got_ferror(infile, GOT_ERR_IO);
491 SHA1Update(&ctx, buf, len);
493 memcpy(&hdr, buf, len);
494 hdr.signature = be32toh(hdr.signature);
495 hdr.version = be32toh(hdr.version);
496 hdr.nentries = be32toh(hdr.nentries);
498 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
499 return got_error(GOT_ERR_FILEIDX_SIG);
500 if (hdr.version != GOT_FILE_INDEX_VERSION)
501 return got_error(GOT_ERR_FILEIDX_VER);
503 for (i = 0; i < hdr.nentries; i++) {
504 err = read_fileindex_entry(&entry, &ctx, infile);
505 if (err)
506 return err;
507 err = got_fileindex_entry_add(fileindex, entry);
508 if (err)
509 return err;
512 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
513 if (n != sizeof(sha1_expected))
514 return got_ferror(infile, GOT_ERR_IO);
515 SHA1Final(sha1, &ctx);
516 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
517 return got_error(GOT_ERR_FILEIDX_CSUM);
519 return NULL;
522 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);