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;
183 static const uint8_t zero[8] = { 0 };
185 len = strlen(path);
186 pad = (len % 8);
188 SHA1Update(ctx, path, len);
189 n = fwrite(path, 1, len, outfile);
190 if (n != len)
191 return got_ferror(outfile, GOT_ERR_IO);
192 if (pad == 0)
193 return NULL;
194 SHA1Update(ctx, zero, pad);
195 n = fwrite(zero, 1, pad, outfile);
196 if (n != pad)
197 return got_ferror(outfile, GOT_ERR_IO);
198 return NULL;
201 static const struct got_error *
202 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
203 FILE *outfile)
205 const struct got_error *err;
206 size_t n;
208 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
209 if (err)
210 return err;
211 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
212 if (err)
213 return err;
214 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
215 if (err)
216 return err;
217 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
218 if (err)
219 return err;
221 err = write_fileindex_val32(ctx, entry->uid, outfile);
222 if (err)
223 return err;
224 err = write_fileindex_val32(ctx, entry->gid, outfile);
225 if (err)
226 return err;
227 err = write_fileindex_val32(ctx, entry->size, outfile);
228 if (err)
229 return err;
231 err = write_fileindex_val16(ctx, entry->mode, outfile);
232 if (err)
233 return err;
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);
246 if (err)
247 return err;
249 err = write_fileindex_path(ctx, entry->path, outfile);
250 return err;
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;
258 SHA1_CTX ctx;
259 uint8_t sha1[SHA1_DIGEST_LENGTH];
260 size_t n;
261 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
262 sizeof(hdr.nentries);
263 uint8_t buf[len];
265 SHA1Init(&ctx);
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);
274 if (n != len)
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);
280 if (err)
281 return err;
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);
289 return NULL;
292 static const struct got_error *
293 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
295 size_t n;
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);
302 return NULL;
305 static const struct got_error *
306 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
308 size_t n;
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);
315 return NULL;
318 static const struct got_error *
319 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
321 size_t n;
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);
328 return NULL;
331 static const struct got_error *
332 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
334 const struct got_error *err = NULL;
335 uint8_t buf[8];
336 size_t n, len = 0, totlen = sizeof(buf);
338 *path = malloc(totlen);
339 if (*path == NULL)
340 return got_error_from_errno();
342 do {
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);
348 if (p == NULL) {
349 err = got_error_from_errno();
350 break;
352 totlen += sizeof(buf);
353 *path = p;
355 SHA1Update(ctx, buf, sizeof(buf));
356 memcpy(*path + len, buf, sizeof(buf));
357 len += sizeof(buf);
358 } while (strchr(buf, '\0') == NULL);
360 if (err) {
361 free(*path);
362 *path = NULL;
364 return err;
367 static const struct got_error *
368 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
369 FILE *infile)
371 const struct got_error *err;
372 struct got_fileindex_entry *entry;
373 size_t n;
375 *entryp = NULL;
377 entry = calloc(1, sizeof(*entry));
378 if (entry == NULL)
379 return got_error_from_errno();
381 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
382 if (err)
383 goto done;
384 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
385 if (err)
386 goto done;
387 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
388 if (err)
389 goto done;
390 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
391 if (err)
392 goto done;
394 err = read_fileindex_val32(&entry->uid, ctx, infile);
395 if (err)
396 goto done;
397 err = read_fileindex_val32(&entry->gid, ctx, infile);
398 if (err)
399 goto done;
400 err = read_fileindex_val32(&entry->size, ctx, infile);
401 if (err)
402 goto done;
404 err = read_fileindex_val16(&entry->mode, ctx, infile);
405 if (err)
406 goto done;
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);
411 goto done;
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);
418 goto done;
420 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
422 err = read_fileindex_val32(&entry->flags, ctx, infile);
423 if (err)
424 goto done;
426 err = read_fileindex_path(&entry->path, ctx, infile);
427 done:
428 if (err)
429 free(entry);
430 else
431 *entryp = entry;
432 return err;
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;
440 SHA1_CTX ctx;
441 struct got_fileindex_entry *entry;
442 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
443 uint8_t sha1[SHA1_DIGEST_LENGTH];
444 size_t n;
445 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
446 sizeof(hdr.nentries);
447 uint8_t buf[len];
448 int i;
450 SHA1Init(&ctx);
452 n = fread(buf, 1, len, infile);
453 if (n != len) {
454 if (n == 0) /* EOF */
455 return NULL;
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);
473 if (err)
474 return err;
475 err = got_fileindex_entry_add(fileindex, entry);
476 if (err)
477 return err;
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);
487 return NULL;