Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <endian.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)
33 {
34 struct stat sb;
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;
48 else
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);
55 return NULL;
56 }
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,
61 uint8_t *commit_sha1)
62 {
63 size_t len;
65 *entry = calloc(1, sizeof(**entry));
66 if (*entry == NULL)
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();
72 free(*entry);
73 *entry = NULL;
74 return err;
75 }
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,
83 commit_sha1);
84 }
86 void
87 got_fileindex_entry_free(struct got_fileindex_entry *entry)
88 {
89 free(entry->path);
90 free(entry);
91 }
93 const struct got_error *
94 got_fileindex_entry_add(struct got_fileindex *fileindex,
95 struct got_fileindex_entry *entry)
96 {
97 /* TODO keep entries sorted by name */
98 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
99 fileindex->nentries++;
100 return NULL;
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)
109 return entry;
112 return NULL;
115 struct got_fileindex *
116 got_fileindex_alloc(void)
118 struct got_fileindex *fileindex;
120 fileindex = calloc(1, sizeof(*fileindex));
121 if (fileindex)
122 TAILQ_INIT(&fileindex->entries);
123 return fileindex;
126 void
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--;
137 free(fileindex);
140 static const struct got_error *
141 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
143 size_t n;
145 val = htobe64(val);
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);
150 return NULL;
153 static const struct got_error *
154 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
156 size_t n;
158 val = htobe32(val);
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);
163 return NULL;
166 static const struct got_error *
167 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
169 size_t n;
171 val = htobe16(val);
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);
176 return NULL;
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 };
185 len = strlen(path);
186 while ((len + pad) % 8 != 0)
187 pad++;
188 if (pad == 0)
189 pad = 8; /* NUL-terminate */
191 SHA1Update(ctx, path, len);
192 n = fwrite(path, 1, len, outfile);
193 if (n != len)
194 return got_ferror(outfile, GOT_ERR_IO);
195 SHA1Update(ctx, zero, pad);
196 n = fwrite(zero, 1, pad, outfile);
197 if (n != pad)
198 return got_ferror(outfile, GOT_ERR_IO);
199 return NULL;
202 static const struct got_error *
203 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
204 FILE *outfile)
206 const struct got_error *err;
207 size_t n;
209 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
210 if (err)
211 return err;
212 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
213 if (err)
214 return err;
215 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
216 if (err)
217 return err;
218 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
219 if (err)
220 return err;
222 err = write_fileindex_val32(ctx, entry->uid, outfile);
223 if (err)
224 return err;
225 err = write_fileindex_val32(ctx, entry->gid, outfile);
226 if (err)
227 return err;
228 err = write_fileindex_val32(ctx, entry->size, outfile);
229 if (err)
230 return err;
232 err = write_fileindex_val16(ctx, entry->mode, outfile);
233 if (err)
234 return err;
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);
247 if (err)
248 return err;
250 err = write_fileindex_path(ctx, entry->path, outfile);
251 return err;
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;
259 SHA1_CTX ctx;
260 uint8_t sha1[SHA1_DIGEST_LENGTH];
261 size_t n;
262 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
263 sizeof(hdr.nentries);
264 uint8_t buf[len];
266 SHA1Init(&ctx);
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);
275 if (n != len)
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);
281 if (err)
282 return err;
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);
290 return NULL;
293 static const struct got_error *
294 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
296 size_t n;
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);
303 return NULL;
306 static const struct got_error *
307 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
309 size_t n;
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);
316 return NULL;
319 static const struct got_error *
320 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
322 size_t n;
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);
329 return NULL;
332 static const struct got_error *
333 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
335 const struct got_error *err = NULL;
336 uint8_t buf[8];
337 size_t n, len = 0, totlen = sizeof(buf);
339 *path = malloc(totlen);
340 if (*path == NULL)
341 return got_error_from_errno();
343 do {
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);
349 if (p == NULL) {
350 err = got_error_from_errno();
351 break;
353 totlen += sizeof(buf);
354 *path = p;
356 SHA1Update(ctx, buf, sizeof(buf));
357 memcpy(*path + len, buf, sizeof(buf));
358 len += sizeof(buf);
359 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
361 if (err) {
362 free(*path);
363 *path = NULL;
365 return err;
368 static const struct got_error *
369 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
370 FILE *infile)
372 const struct got_error *err;
373 struct got_fileindex_entry *entry;
374 size_t n;
376 *entryp = NULL;
378 entry = calloc(1, sizeof(*entry));
379 if (entry == NULL)
380 return got_error_from_errno();
382 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
383 if (err)
384 goto done;
385 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
386 if (err)
387 goto done;
388 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
389 if (err)
390 goto done;
391 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
392 if (err)
393 goto done;
395 err = read_fileindex_val32(&entry->uid, ctx, infile);
396 if (err)
397 goto done;
398 err = read_fileindex_val32(&entry->gid, ctx, infile);
399 if (err)
400 goto done;
401 err = read_fileindex_val32(&entry->size, ctx, infile);
402 if (err)
403 goto done;
405 err = read_fileindex_val16(&entry->mode, ctx, infile);
406 if (err)
407 goto done;
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);
412 goto done;
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);
419 goto done;
421 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
423 err = read_fileindex_val32(&entry->flags, ctx, infile);
424 if (err)
425 goto done;
427 err = read_fileindex_path(&entry->path, ctx, infile);
428 done:
429 if (err)
430 free(entry);
431 else
432 *entryp = entry;
433 return err;
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;
441 SHA1_CTX ctx;
442 struct got_fileindex_entry *entry;
443 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
444 uint8_t sha1[SHA1_DIGEST_LENGTH];
445 size_t n;
446 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
447 sizeof(hdr.nentries);
448 uint8_t buf[len];
449 int i;
451 SHA1Init(&ctx);
453 n = fread(buf, 1, len, infile);
454 if (n != len) {
455 if (n == 0) /* EOF */
456 return NULL;
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);
474 if (err)
475 return err;
476 err = got_fileindex_entry_add(fileindex, entry);
477 if (err)
478 return err;
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);
488 return NULL;