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)
183 static const uint8_t zero[8] = { 0 };
188 SHA1Update(ctx, path, len);
189 n = fwrite(path, 1, len, outfile);
191 return got_ferror(outfile, GOT_ERR_IO);
194 SHA1Update(ctx, zero, pad);
195 n = fwrite(zero, 1, pad, outfile);
197 return got_ferror(outfile, GOT_ERR_IO);
201 static const struct got_error *
202 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
205 const struct got_error *err;
208 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
211 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
214 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
217 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
221 err = write_fileindex_val32(ctx, entry->uid, outfile);
224 err = write_fileindex_val32(ctx, entry->gid, outfile);
227 err = write_fileindex_val32(ctx, entry->size, outfile);
231 err = write_fileindex_val16(ctx, entry->mode, outfile);
235 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
236 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
237 if (n != SHA1_DIGEST_LENGTH)
238 return got_ferror(outfile, GOT_ERR_IO);
240 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
241 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
242 if (n != SHA1_DIGEST_LENGTH)
243 return got_ferror(outfile, GOT_ERR_IO);
245 err = write_fileindex_val32(ctx, entry->flags, outfile);
249 err = write_fileindex_path(ctx, entry->path, outfile);
253 const struct got_error *
254 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
256 struct got_fileindex_hdr hdr;
257 struct got_fileindex_entry *entry;
259 uint8_t sha1[SHA1_DIGEST_LENGTH];
261 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
262 sizeof(hdr.nentries);
267 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
268 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
269 hdr.nentries = htobe32(fileindex->nentries);
271 memcpy(buf, &hdr, len);
272 SHA1Update(&ctx, buf, len);
273 n = fwrite(buf, 1, len, outfile);
275 return got_ferror(outfile, GOT_ERR_IO);
277 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
278 const struct got_error *err;
279 err = write_fileindex_entry(&ctx, entry, outfile);
284 SHA1Final(sha1, &ctx);
285 n = fwrite(sha1, 1, sizeof(sha1), outfile);
286 if (n != sizeof(sha1))
287 return got_ferror(outfile, GOT_ERR_IO);
292 static const struct got_error *
293 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
297 n = fread(val, 1, sizeof(*val), infile);
298 if (n != sizeof(*val))
299 return got_ferror(infile, GOT_ERR_IO);
300 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
301 *val = be64toh(*val);
305 static const struct got_error *
306 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
310 n = fread(val, 1, sizeof(*val), infile);
311 if (n != sizeof(*val))
312 return got_ferror(infile, GOT_ERR_IO);
313 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
314 *val = be32toh(*val);
318 static const struct got_error *
319 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
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 = be16toh(*val);
331 static const struct got_error *
332 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
334 const struct got_error *err = NULL;
336 size_t n, len = 0, totlen = sizeof(buf);
338 *path = malloc(totlen);
340 return got_error_from_errno();
343 n = fread(buf, 1, sizeof(buf), infile);
344 if (n != sizeof(buf))
345 return got_ferror(infile, GOT_ERR_IO);
346 if (len + sizeof(buf) > totlen) {
347 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
349 err = got_error_from_errno();
352 totlen += sizeof(buf);
355 SHA1Update(ctx, buf, sizeof(buf));
356 memcpy(*path + len, buf, sizeof(buf));
358 } while (strchr(buf, '\0') == NULL);
367 static const struct got_error *
368 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
371 const struct got_error *err;
372 struct got_fileindex_entry *entry;
377 entry = calloc(1, sizeof(*entry));
379 return got_error_from_errno();
381 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
384 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
387 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
390 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
394 err = read_fileindex_val32(&entry->uid, ctx, infile);
397 err = read_fileindex_val32(&entry->gid, ctx, infile);
400 err = read_fileindex_val32(&entry->size, ctx, infile);
404 err = read_fileindex_val16(&entry->mode, ctx, infile);
408 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
409 if (n != SHA1_DIGEST_LENGTH) {
410 err = got_ferror(infile, GOT_ERR_IO);
413 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
415 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
416 if (n != SHA1_DIGEST_LENGTH) {
417 err = got_ferror(infile, GOT_ERR_IO);
420 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
422 err = read_fileindex_val32(&entry->flags, ctx, infile);
426 err = read_fileindex_path(&entry->path, ctx, infile);
435 const struct got_error *
436 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
438 const struct got_error *err = NULL;
439 struct got_fileindex_hdr hdr;
441 struct got_fileindex_entry *entry;
442 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
443 uint8_t sha1[SHA1_DIGEST_LENGTH];
445 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
446 sizeof(hdr.nentries);
452 n = fread(buf, 1, len, infile);
454 if (n == 0) /* EOF */
456 return got_ferror(infile, GOT_ERR_IO);
459 SHA1Update(&ctx, buf, len);
461 memcpy(&hdr, buf, len);
462 hdr.signature = be32toh(hdr.signature);
463 hdr.version = be32toh(hdr.version);
464 hdr.nentries = be32toh(hdr.nentries);
466 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
467 return got_error(GOT_ERR_FILEIDX_SIG);
468 if (hdr.version != GOT_FILE_INDEX_VERSION)
469 return got_error(GOT_ERR_FILEIDX_VER);
471 for (i = 0; i < hdr.nentries; i++) {
472 err = read_fileindex_entry(&entry, &ctx, infile);
475 err = got_fileindex_entry_add(fileindex, entry);
480 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
481 if (n != sizeof(sha1_expected))
482 return got_ferror(infile, GOT_ERR_IO);
483 SHA1Final(sha1, &ctx);
484 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
485 return got_error(GOT_ERR_FILEIDX_CSUM);