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_alloc(struct got_fileindex_entry **entry,
32 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
38 if (lstat(ondisk_path, &sb) != 0)
39 return got_error_from_errno();
41 *entry = calloc(1, sizeof(**entry));
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();
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;
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;
77 got_fileindex_entry_free(struct got_fileindex_entry *entry)
83 const struct got_error *
84 got_fileindex_entry_add(struct got_fileindex *fileindex,
85 struct got_fileindex_entry *entry)
87 /* TODO keep entries sorted by name */
88 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
89 fileindex->nentries++;
93 struct got_fileindex *
94 got_fileindex_alloc(void)
96 struct got_fileindex *fileindex;
98 fileindex = calloc(1, sizeof(*fileindex));
100 TAILQ_INIT(&fileindex->entries);
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--;
118 static const struct got_error *
119 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
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);
131 static const struct got_error *
132 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
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);
144 static const struct got_error *
145 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
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);
157 static const struct got_error *
158 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
161 static const uint8_t zero[8] = { 0 };
166 SHA1Update(ctx, path, len);
167 n = fwrite(path, 1, len, outfile);
169 return got_ferror(outfile, GOT_ERR_IO);
172 SHA1Update(ctx, zero, pad);
173 n = fwrite(zero, 1, pad, outfile);
175 return got_ferror(outfile, GOT_ERR_IO);
179 static const struct got_error *
180 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
183 const struct got_error *err;
186 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
189 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
192 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
195 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
199 err = write_fileindex_val32(ctx, entry->uid, outfile);
202 err = write_fileindex_val32(ctx, entry->gid, outfile);
205 err = write_fileindex_val32(ctx, entry->size, outfile);
209 err = write_fileindex_val16(ctx, entry->mode, outfile);
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);
227 err = write_fileindex_path(ctx, entry->path, outfile);
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;
237 uint8_t sha1[SHA1_DIGEST_LENGTH];
239 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
240 sizeof(hdr.nentries);
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);
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);
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);
270 static const struct got_error *
271 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
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);
283 static const struct got_error *
284 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
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);
296 static const struct got_error *
297 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
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);
309 static const struct got_error *
310 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
312 const struct got_error *err = NULL;
314 size_t n, len = 0, totlen = sizeof(buf);
316 *path = malloc(totlen);
318 return got_error_from_errno();
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);
327 err = got_error_from_errno();
330 totlen += sizeof(buf);
333 SHA1Update(ctx, buf, sizeof(buf));
334 memcpy(*path + len, buf, sizeof(buf));
336 } while (strchr(buf, '\0') == NULL);
345 static const struct got_error *
346 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
349 const struct got_error *err;
350 struct got_fileindex_entry *entry;
355 entry = calloc(1, sizeof(*entry));
357 return got_error_from_errno();
359 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
362 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
365 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
368 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
372 err = read_fileindex_val32(&entry->uid, ctx, infile);
375 err = read_fileindex_val32(&entry->gid, ctx, infile);
378 err = read_fileindex_val32(&entry->size, ctx, infile);
382 err = read_fileindex_val16(&entry->mode, ctx, infile);
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);
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);
398 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
400 err = read_fileindex_val32(&entry->flags, ctx, infile);
404 err = read_fileindex_path(&entry->path, ctx, infile);
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;
419 struct got_fileindex_entry *entry;
420 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
421 uint8_t sha1[SHA1_DIGEST_LENGTH];
423 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
424 sizeof(hdr.nentries);
430 n = fread(buf, 1, len, infile);
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);
450 err = got_fileindex_entry_add(fileindex, entry);
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);