2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.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)
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;
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);
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,
65 *entry = calloc(1, sizeof(**entry));
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();
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,
87 got_fileindex_entry_free(struct got_fileindex_entry *entry)
93 const struct got_error *
94 got_fileindex_entry_add(struct got_fileindex *fileindex,
95 struct got_fileindex_entry *entry)
97 /* TODO keep entries sorted by name */
98 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
99 fileindex->nentries++;
103 struct got_fileindex_entry *
104 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
106 struct got_fileindex_entry *entry;
107 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
108 if (strcmp(entry->path, path) == 0)
115 struct got_fileindex *
116 got_fileindex_alloc(void)
118 struct got_fileindex *fileindex;
120 fileindex = calloc(1, sizeof(*fileindex));
122 TAILQ_INIT(&fileindex->entries);
127 got_fileindex_free(struct got_fileindex *fileindex)
129 struct got_fileindex_entry *entry;
131 while (!TAILQ_EMPTY(&fileindex->entries)) {
132 entry = TAILQ_FIRST(&fileindex->entries);
133 TAILQ_REMOVE(&fileindex->entries, entry, entry);
134 got_fileindex_entry_free(entry);
135 fileindex->nentries--;
140 static const struct got_error *
141 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
146 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
147 n = fwrite(&val, 1, sizeof(val), outfile);
148 if (n != sizeof(val))
149 return got_ferror(outfile, GOT_ERR_IO);
153 static const struct got_error *
154 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
159 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
160 n = fwrite(&val, 1, sizeof(val), outfile);
161 if (n != sizeof(val))
162 return got_ferror(outfile, GOT_ERR_IO);
166 static const struct got_error *
167 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
172 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
173 n = fwrite(&val, 1, sizeof(val), outfile);
174 if (n != sizeof(val))
175 return got_ferror(outfile, GOT_ERR_IO);
179 static const struct got_error *
180 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
182 size_t n, len, pad = 0;
183 static const uint8_t zero[8] = { 0 };
186 while ((len + pad) % 8 != 0)
189 pad = 8; /* NUL-terminate */
191 SHA1Update(ctx, path, len);
192 n = fwrite(path, 1, len, outfile);
194 return got_ferror(outfile, GOT_ERR_IO);
195 SHA1Update(ctx, zero, pad);
196 n = fwrite(zero, 1, pad, outfile);
198 return got_ferror(outfile, GOT_ERR_IO);
202 static const struct got_error *
203 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
206 const struct got_error *err;
209 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
212 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
215 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
218 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
222 err = write_fileindex_val32(ctx, entry->uid, outfile);
225 err = write_fileindex_val32(ctx, entry->gid, outfile);
228 err = write_fileindex_val32(ctx, entry->size, outfile);
232 err = write_fileindex_val16(ctx, entry->mode, outfile);
236 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
237 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
238 if (n != SHA1_DIGEST_LENGTH)
239 return got_ferror(outfile, GOT_ERR_IO);
241 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
242 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
243 if (n != SHA1_DIGEST_LENGTH)
244 return got_ferror(outfile, GOT_ERR_IO);
246 err = write_fileindex_val32(ctx, entry->flags, outfile);
250 err = write_fileindex_path(ctx, entry->path, outfile);
254 const struct got_error *
255 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
257 struct got_fileindex_hdr hdr;
258 struct got_fileindex_entry *entry;
260 uint8_t sha1[SHA1_DIGEST_LENGTH];
262 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
263 sizeof(hdr.nentries);
268 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
269 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
270 hdr.nentries = htobe32(fileindex->nentries);
272 memcpy(buf, &hdr, len);
273 SHA1Update(&ctx, buf, len);
274 n = fwrite(buf, 1, len, outfile);
276 return got_ferror(outfile, GOT_ERR_IO);
278 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
279 const struct got_error *err;
280 err = write_fileindex_entry(&ctx, entry, outfile);
285 SHA1Final(sha1, &ctx);
286 n = fwrite(sha1, 1, sizeof(sha1), outfile);
287 if (n != sizeof(sha1))
288 return got_ferror(outfile, GOT_ERR_IO);
293 static const struct got_error *
294 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
298 n = fread(val, 1, sizeof(*val), infile);
299 if (n != sizeof(*val))
300 return got_ferror(infile, GOT_ERR_IO);
301 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
302 *val = be64toh(*val);
306 static const struct got_error *
307 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
311 n = fread(val, 1, sizeof(*val), infile);
312 if (n != sizeof(*val))
313 return got_ferror(infile, GOT_ERR_IO);
314 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
315 *val = be32toh(*val);
319 static const struct got_error *
320 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
324 n = fread(val, 1, sizeof(*val), infile);
325 if (n != sizeof(*val))
326 return got_ferror(infile, GOT_ERR_IO);
327 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
328 *val = be16toh(*val);
332 static const struct got_error *
333 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
335 const struct got_error *err = NULL;
337 size_t n, len = 0, totlen = sizeof(buf);
339 *path = malloc(totlen);
341 return got_error_from_errno();
344 n = fread(buf, 1, sizeof(buf), infile);
345 if (n != sizeof(buf))
346 return got_ferror(infile, GOT_ERR_IO);
347 if (len + sizeof(buf) > totlen) {
348 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
350 err = got_error_from_errno();
353 totlen += sizeof(buf);
356 SHA1Update(ctx, buf, sizeof(buf));
357 memcpy(*path + len, buf, sizeof(buf));
359 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
368 static const struct got_error *
369 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
372 const struct got_error *err;
373 struct got_fileindex_entry *entry;
378 entry = calloc(1, sizeof(*entry));
380 return got_error_from_errno();
382 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
385 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
388 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
391 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
395 err = read_fileindex_val32(&entry->uid, ctx, infile);
398 err = read_fileindex_val32(&entry->gid, ctx, infile);
401 err = read_fileindex_val32(&entry->size, ctx, infile);
405 err = read_fileindex_val16(&entry->mode, ctx, infile);
409 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
410 if (n != SHA1_DIGEST_LENGTH) {
411 err = got_ferror(infile, GOT_ERR_IO);
414 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
416 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
417 if (n != SHA1_DIGEST_LENGTH) {
418 err = got_ferror(infile, GOT_ERR_IO);
421 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
423 err = read_fileindex_val32(&entry->flags, ctx, infile);
427 err = read_fileindex_path(&entry->path, ctx, infile);
436 const struct got_error *
437 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
439 const struct got_error *err = NULL;
440 struct got_fileindex_hdr hdr;
442 struct got_fileindex_entry *entry;
443 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
444 uint8_t sha1[SHA1_DIGEST_LENGTH];
446 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
447 sizeof(hdr.nentries);
453 n = fread(buf, 1, len, infile);
455 if (n == 0) /* EOF */
457 return got_ferror(infile, GOT_ERR_IO);
460 SHA1Update(&ctx, buf, len);
462 memcpy(&hdr, buf, len);
463 hdr.signature = be32toh(hdr.signature);
464 hdr.version = be32toh(hdr.version);
465 hdr.nentries = be32toh(hdr.nentries);
467 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
468 return got_error(GOT_ERR_FILEIDX_SIG);
469 if (hdr.version != GOT_FILE_INDEX_VERSION)
470 return got_error(GOT_ERR_FILEIDX_VER);
472 for (i = 0; i < hdr.nentries; i++) {
473 err = read_fileindex_entry(&entry, &ctx, infile);
476 err = got_fileindex_entry_add(fileindex, entry);
481 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
482 if (n != sizeof(sha1_expected))
483 return got_ferror(infile, GOT_ERR_IO);
484 SHA1Final(sha1, &ctx);
485 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
486 return got_error(GOT_ERR_FILEIDX_CSUM);