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)
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_free(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_alloc(void)
94 struct got_fileindex *fileindex;
96 fileindex = calloc(1, sizeof(*fileindex));
98 TAILQ_INIT(&fileindex->entries);
103 got_fileindex_free(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_free(entry);
111 fileindex->nentries--;
116 static const struct got_error *
117 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
122 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
123 n = fwrite(&val, 1, sizeof(val), outfile);
124 if (n != sizeof(val))
125 return got_ferror(outfile, GOT_ERR_IO);
129 static const struct got_error *
130 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
135 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
136 n = fwrite(&val, 1, sizeof(val), outfile);
137 if (n != sizeof(val))
138 return got_ferror(outfile, GOT_ERR_IO);
142 static const struct got_error *
143 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
148 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
149 n = fwrite(&val, 1, sizeof(val), outfile);
150 if (n != sizeof(val))
151 return got_ferror(outfile, GOT_ERR_IO);
155 static const struct got_error *
156 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
159 static const uint8_t zero[8] = { 0 };
164 SHA1Update(ctx, path, len);
165 n = fwrite(path, 1, len, outfile);
167 return got_ferror(outfile, GOT_ERR_IO);
170 SHA1Update(ctx, zero, pad);
171 n = fwrite(zero, 1, pad, outfile);
173 return got_ferror(outfile, GOT_ERR_IO);
177 static const struct got_error *
178 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
181 const struct got_error *err;
184 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
187 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
190 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
193 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
197 err = write_fileindex_val32(ctx, entry->uid, outfile);
200 err = write_fileindex_val32(ctx, entry->gid, outfile);
203 err = write_fileindex_val32(ctx, entry->size, outfile);
207 err = write_fileindex_val16(ctx, entry->mode, outfile);
211 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
212 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
213 if (n != SHA1_DIGEST_LENGTH)
214 return got_ferror(outfile, GOT_ERR_IO);
216 err = write_fileindex_val32(ctx, entry->flags, outfile);
220 err = write_fileindex_path(ctx, entry->path, outfile);
224 const struct got_error *
225 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
227 struct got_fileindex_hdr hdr;
228 struct got_fileindex_entry *entry;
230 uint8_t sha1[SHA1_DIGEST_LENGTH];
232 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
233 sizeof(hdr.nentries);
238 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
239 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
240 hdr.nentries = htobe32(fileindex->nentries);
242 memcpy(buf, &hdr, len);
243 SHA1Update(&ctx, buf, len);
244 n = fwrite(buf, 1, len, outfile);
246 return got_ferror(outfile, GOT_ERR_IO);
248 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
249 const struct got_error *err;
250 err = write_fileindex_entry(&ctx, entry, outfile);
255 SHA1Final(sha1, &ctx);
256 n = fwrite(sha1, 1, sizeof(sha1), outfile);
257 if (n != sizeof(sha1))
258 return got_ferror(outfile, GOT_ERR_IO);
263 static const struct got_error *
264 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
268 n = fread(val, 1, sizeof(*val), infile);
269 if (n != sizeof(*val))
270 return got_ferror(infile, GOT_ERR_IO);
271 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
272 *val = be64toh(*val);
276 static const struct got_error *
277 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
281 n = fread(val, 1, sizeof(*val), infile);
282 if (n != sizeof(*val))
283 return got_ferror(infile, GOT_ERR_IO);
284 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
285 *val = be32toh(*val);
289 static const struct got_error *
290 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
294 n = fread(val, 1, sizeof(*val), infile);
295 if (n != sizeof(*val))
296 return got_ferror(infile, GOT_ERR_IO);
297 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
298 *val = be16toh(*val);
302 static const struct got_error *
303 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
305 const struct got_error *err = NULL;
307 size_t n, len = 0, totlen = sizeof(buf);
309 *path = malloc(totlen);
311 return got_error_from_errno();
314 n = fread(buf, 1, sizeof(buf), infile);
315 if (n != sizeof(buf))
316 return got_ferror(infile, GOT_ERR_IO);
317 if (len + sizeof(buf) > totlen) {
318 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
320 err = got_error_from_errno();
323 totlen += sizeof(buf);
326 SHA1Update(ctx, buf, sizeof(buf));
327 memcpy(*path + len, buf, sizeof(buf));
329 } while (strchr(buf, '\0') == NULL);
338 static const struct got_error *
339 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
342 const struct got_error *err;
343 struct got_fileindex_entry *entry;
348 entry = calloc(1, sizeof(*entry));
350 return got_error_from_errno();
352 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
355 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
358 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
361 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
365 err = read_fileindex_val32(&entry->uid, ctx, infile);
368 err = read_fileindex_val32(&entry->gid, ctx, infile);
371 err = read_fileindex_val32(&entry->size, ctx, infile);
375 err = read_fileindex_val16(&entry->mode, ctx, infile);
379 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
380 if (n != SHA1_DIGEST_LENGTH) {
381 err = got_ferror(infile, GOT_ERR_IO);
384 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
386 err = read_fileindex_val32(&entry->flags, ctx, infile);
390 err = read_fileindex_path(&entry->path, ctx, infile);
399 const struct got_error *
400 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
402 const struct got_error *err = NULL;
403 struct got_fileindex_hdr hdr;
405 struct got_fileindex_entry *entry;
406 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
407 uint8_t sha1[SHA1_DIGEST_LENGTH];
409 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
410 sizeof(hdr.nentries);
416 n = fread(buf, 1, len, infile);
418 return got_ferror(infile, GOT_ERR_IO);
420 SHA1Update(&ctx, buf, len);
422 memcpy(&hdr, buf, len);
423 hdr.signature = be32toh(hdr.signature);
424 hdr.version = be32toh(hdr.version);
425 hdr.nentries = be32toh(hdr.nentries);
427 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
428 return got_error(GOT_ERR_FILEIDX_SIG);
429 if (hdr.version != GOT_FILE_INDEX_VERSION)
430 return got_error(GOT_ERR_FILEIDX_VER);
432 for (i = 0; i < hdr.nentries; i++) {
433 err = read_fileindex_entry(&entry, &ctx, infile);
436 err = got_fileindex_entry_add(fileindex, entry);
441 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
442 if (n != sizeof(sha1_expected))
443 return got_ferror(infile, GOT_ERR_IO);
444 SHA1Final(sha1, &ctx);
445 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
446 return got_error(GOT_ERR_FILEIDX_CSUM);