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_open(struct got_fileindex_entry **entry,
32 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1)
37 if (lstat(ondisk_path, &sb) != 0)
38 return got_error_from_errno();
40 *entry = calloc(1, sizeof(**entry));
42 return got_error_from_errno();
44 (*entry)->path = strdup(relpath);
45 if ((*entry)->path == NULL) {
46 const struct got_error *err = got_error_from_errno();
52 (*entry)->ctime_sec = sb.st_ctime;
53 (*entry)->ctime_nsec = sb.st_ctimensec;
54 (*entry)->mtime_sec = sb.st_mtime;
55 (*entry)->mtime_nsec = sb.st_mtimensec;
56 (*entry)->uid = sb.st_uid;
57 (*entry)->gid = sb.st_gid;
58 (*entry)->size = (sb.st_size & 0xffffffff);
59 if (sb.st_mode & S_IFLNK)
60 (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
62 (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
63 (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
64 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
65 memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
66 len = strlen(relpath);
67 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
68 len = GOT_INDEX_ENTRY_F_PATH_LEN;
69 (*entry)->flags |= len;
75 got_fileindex_entry_close(struct got_fileindex_entry *entry)
81 const struct got_error *
82 got_fileindex_entry_add(struct got_fileindex *fileindex,
83 struct got_fileindex_entry *entry)
85 /* TODO keep entries sorted by name */
86 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
87 fileindex->nentries++;
91 struct got_fileindex *
92 got_fileindex_open(void)
94 struct got_fileindex *fileindex;
96 fileindex = calloc(1, sizeof(*fileindex));
98 TAILQ_INIT(&fileindex->entries);
103 got_fileindex_close(struct got_fileindex *fileindex)
105 struct got_fileindex_entry *entry;
107 while (!TAILQ_EMPTY(&fileindex->entries)) {
108 entry = TAILQ_FIRST(&fileindex->entries);
109 TAILQ_REMOVE(&fileindex->entries, entry, entry);
110 got_fileindex_entry_close(entry);
111 fileindex->nentries--;
116 static const struct got_error *
117 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
119 uint8_t buf[sizeof(uint64_t)];
123 memcpy(buf, &val, sizeof(val));
124 SHA1Update(ctx, buf, sizeof(val));
125 n = fwrite(buf, 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)
134 uint8_t buf[sizeof(uint32_t)];
138 memcpy(buf, &val, sizeof(val));
139 SHA1Update(ctx, buf, sizeof(val));
140 n = fwrite(buf, 1, sizeof(val), outfile);
141 if (n != sizeof(val))
142 return got_ferror(outfile, GOT_ERR_IO);
146 static const struct got_error *
147 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
149 uint8_t buf[sizeof(uint16_t)];
153 memcpy(buf, &val, sizeof(val));
154 SHA1Update(ctx, buf, sizeof(val));
155 n = fwrite(buf, 1, sizeof(val), outfile);
156 if (n != sizeof(val))
157 return got_ferror(outfile, GOT_ERR_IO);
161 static const struct got_error *
162 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
165 static const uint8_t zero[8] = { 0 };
170 SHA1Update(ctx, path, len);
171 n = fwrite(path, 1, len, outfile);
173 return got_ferror(outfile, GOT_ERR_IO);
176 SHA1Update(ctx, zero, pad);
177 n = fwrite(zero, 1, pad, outfile);
179 return got_ferror(outfile, GOT_ERR_IO);
183 static const struct got_error *
184 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
187 const struct got_error *err;
190 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
193 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
196 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
199 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
203 err = write_fileindex_val32(ctx, entry->uid, outfile);
206 err = write_fileindex_val32(ctx, entry->gid, outfile);
209 err = write_fileindex_val32(ctx, entry->size, outfile);
213 err = write_fileindex_val16(ctx, entry->mode, outfile);
217 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
218 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
219 if (n != SHA1_DIGEST_LENGTH)
220 return got_ferror(outfile, GOT_ERR_IO);
222 err = write_fileindex_val32(ctx, entry->flags, outfile);
226 err = write_fileindex_path(ctx, entry->path, outfile);
230 const struct got_error *
231 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
233 struct got_fileindex_hdr hdr;
234 struct got_fileindex_entry *entry;
236 uint8_t sha1[SHA1_DIGEST_LENGTH];
238 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
239 sizeof(hdr.nentries);
244 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
245 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
246 hdr.nentries = htobe32(fileindex->nentries);
248 memcpy(buf, &hdr, len);
249 SHA1Update(&ctx, buf, len);
250 n = fwrite(buf, 1, len, outfile);
252 return got_ferror(outfile, GOT_ERR_IO);
254 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
255 const struct got_error *err;
256 err = write_fileindex_entry(&ctx, entry, outfile);
261 SHA1Final(sha1, &ctx);
262 n = fwrite(sha1, 1, sizeof(sha1), outfile);
263 if (n != sizeof(sha1))
264 return got_ferror(outfile, GOT_ERR_IO);
269 static const struct got_error *
270 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
272 uint8_t buf[sizeof(uint64_t)];
275 n = fread(buf, 1, sizeof(buf), infile);
276 if (n != sizeof(buf))
277 return got_ferror(infile, GOT_ERR_IO);
278 SHA1Update(ctx, buf, sizeof(buf));
279 memcpy(val, buf, sizeof(*val));
280 *val = be64toh(*val);
284 static const struct got_error *
285 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
287 uint8_t buf[sizeof(uint32_t)];
290 n = fread(buf, 1, sizeof(buf), infile);
291 if (n != sizeof(buf))
292 return got_ferror(infile, GOT_ERR_IO);
293 SHA1Update(ctx, buf, sizeof(buf));
294 memcpy(val, buf, sizeof(*val));
295 *val = be32toh(*val);
299 static const struct got_error *
300 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
302 uint8_t buf[sizeof(uint16_t)];
305 n = fread(buf, 1, sizeof(buf), infile);
306 if (n != sizeof(buf))
307 return got_ferror(infile, GOT_ERR_IO);
308 SHA1Update(ctx, buf, sizeof(buf));
309 memcpy(val, buf, sizeof(*val));
310 *val = be16toh(*val);
314 static const struct got_error *
315 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
317 const struct got_error *err = NULL;
319 size_t n, len = 0, totlen = sizeof(buf);
321 *path = malloc(totlen);
323 return got_error_from_errno();
326 n = fread(buf, 1, sizeof(buf), infile);
327 if (n != sizeof(buf))
328 return got_ferror(infile, GOT_ERR_IO);
329 if (len + sizeof(buf) > totlen) {
330 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
332 err = got_error_from_errno();
335 totlen += sizeof(buf);
338 SHA1Update(ctx, buf, sizeof(buf));
339 memcpy(*path + len, buf, sizeof(buf));
341 } while (strchr(buf, '\0') == NULL);
350 static const struct got_error *
351 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
354 const struct got_error *err;
355 struct got_fileindex_entry *entry;
360 entry = calloc(1, sizeof(*entry));
362 return got_error_from_errno();
364 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
367 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
370 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
373 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
377 err = read_fileindex_val32(&entry->uid, ctx, infile);
380 err = read_fileindex_val32(&entry->gid, ctx, infile);
383 err = read_fileindex_val32(&entry->size, ctx, infile);
387 err = read_fileindex_val16(&entry->mode, ctx, infile);
391 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
392 if (n != SHA1_DIGEST_LENGTH) {
393 err = got_ferror(infile, GOT_ERR_IO);
396 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
398 err = read_fileindex_val32(&entry->flags, ctx, infile);
402 err = read_fileindex_path(&entry->path, ctx, infile);
411 const struct got_error *
412 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
414 const struct got_error *err = NULL;
415 struct got_fileindex_hdr hdr;
417 struct got_fileindex_entry *entry;
418 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
419 uint8_t sha1[SHA1_DIGEST_LENGTH];
421 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
422 sizeof(hdr.nentries);
428 n = fread(buf, 1, len, infile);
430 return got_ferror(infile, GOT_ERR_IO);
432 SHA1Update(&ctx, buf, len);
434 memcpy(&hdr, buf, len);
435 hdr.signature = be32toh(hdr.signature);
436 hdr.version = be32toh(hdr.version);
437 hdr.nentries = be32toh(hdr.nentries);
439 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
440 return got_error(GOT_ERR_FILEIDX_SIG);
441 if (hdr.version != GOT_FILE_INDEX_VERSION)
442 return got_error(GOT_ERR_FILEIDX_VER);
444 for (i = 0; i < hdr.nentries; i++) {
445 err = read_fileindex_entry(&entry, &ctx, infile);
448 err = got_fileindex_entry_add(fileindex, entry);
453 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
454 if (n != sizeof(sha1_expected))
455 return got_ferror(infile, GOT_ERR_IO);
456 SHA1Final(sha1, &ctx);
457 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
458 return got_error(GOT_ERR_FILEIDX_CSUM);