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_alloc(struct got_fileindex_entry **entry,
32 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1)
33 {
34 struct stat sb;
35 size_t len;
37 if (lstat(ondisk_path, &sb) != 0)
38 return got_error_from_errno();
40 *entry = calloc(1, sizeof(**entry));
41 if (*entry == NULL)
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();
47 free(*entry);
48 *entry = NULL;
49 return err;
50 }
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;
61 else
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;
71 return NULL;
72 }
74 void
75 got_fileindex_entry_free(struct got_fileindex_entry *entry)
76 {
77 free(entry->path);
78 free(entry);
79 }
81 const struct got_error *
82 got_fileindex_entry_add(struct got_fileindex *fileindex,
83 struct got_fileindex_entry *entry)
84 {
85 /* TODO keep entries sorted by name */
86 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
87 fileindex->nentries++;
88 return NULL;
89 }
91 struct got_fileindex *
92 got_fileindex_alloc(void)
93 {
94 struct got_fileindex *fileindex;
96 fileindex = calloc(1, sizeof(*fileindex));
97 if (fileindex)
98 TAILQ_INIT(&fileindex->entries);
99 return fileindex;
102 void
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--;
113 free(fileindex);
116 static const struct got_error *
117 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
119 size_t n;
121 val = htobe64(val);
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);
126 return NULL;
129 static const struct got_error *
130 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
132 size_t n;
134 val = htobe32(val);
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);
139 return NULL;
142 static const struct got_error *
143 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
145 size_t n;
147 val = htobe16(val);
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);
152 return NULL;
155 static const struct got_error *
156 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
158 size_t n, len, pad;
159 static const uint8_t zero[8] = { 0 };
161 len = strlen(path);
162 pad = (len % 8);
164 SHA1Update(ctx, path, len);
165 n = fwrite(path, 1, len, outfile);
166 if (n != len)
167 return got_ferror(outfile, GOT_ERR_IO);
168 if (pad == 0)
169 return NULL;
170 SHA1Update(ctx, zero, pad);
171 n = fwrite(zero, 1, pad, outfile);
172 if (n != pad)
173 return got_ferror(outfile, GOT_ERR_IO);
174 return NULL;
177 static const struct got_error *
178 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
179 FILE *outfile)
181 const struct got_error *err;
182 size_t n;
184 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
185 if (err)
186 return err;
187 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
188 if (err)
189 return err;
190 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
191 if (err)
192 return err;
193 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
194 if (err)
195 return err;
197 err = write_fileindex_val32(ctx, entry->uid, outfile);
198 if (err)
199 return err;
200 err = write_fileindex_val32(ctx, entry->gid, outfile);
201 if (err)
202 return err;
203 err = write_fileindex_val32(ctx, entry->size, outfile);
204 if (err)
205 return err;
207 err = write_fileindex_val16(ctx, entry->mode, outfile);
208 if (err)
209 return err;
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);
217 if (err)
218 return err;
220 err = write_fileindex_path(ctx, entry->path, outfile);
221 return err;
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;
229 SHA1_CTX ctx;
230 uint8_t sha1[SHA1_DIGEST_LENGTH];
231 size_t n;
232 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
233 sizeof(hdr.nentries);
234 uint8_t buf[len];
236 SHA1Init(&ctx);
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);
245 if (n != len)
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);
251 if (err)
252 return err;
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);
260 return NULL;
263 static const struct got_error *
264 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
266 size_t n;
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);
273 return NULL;
276 static const struct got_error *
277 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
279 size_t n;
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);
286 return NULL;
289 static const struct got_error *
290 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
292 size_t n;
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);
299 return NULL;
302 static const struct got_error *
303 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
305 const struct got_error *err = NULL;
306 uint8_t buf[8];
307 size_t n, len = 0, totlen = sizeof(buf);
309 *path = malloc(totlen);
310 if (*path == NULL)
311 return got_error_from_errno();
313 do {
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);
319 if (p == NULL) {
320 err = got_error_from_errno();
321 break;
323 totlen += sizeof(buf);
324 *path = p;
326 SHA1Update(ctx, buf, sizeof(buf));
327 memcpy(*path + len, buf, sizeof(buf));
328 len += sizeof(buf);
329 } while (strchr(buf, '\0') == NULL);
331 if (err) {
332 free(*path);
333 *path = NULL;
335 return err;
338 static const struct got_error *
339 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
340 FILE *infile)
342 const struct got_error *err;
343 struct got_fileindex_entry *entry;
344 size_t n;
346 *entryp = NULL;
348 entry = calloc(1, sizeof(*entry));
349 if (entry == NULL)
350 return got_error_from_errno();
352 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
353 if (err)
354 goto done;
355 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
356 if (err)
357 goto done;
358 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
359 if (err)
360 goto done;
361 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
362 if (err)
363 goto done;
365 err = read_fileindex_val32(&entry->uid, ctx, infile);
366 if (err)
367 goto done;
368 err = read_fileindex_val32(&entry->gid, ctx, infile);
369 if (err)
370 goto done;
371 err = read_fileindex_val32(&entry->size, ctx, infile);
372 if (err)
373 goto done;
375 err = read_fileindex_val16(&entry->mode, ctx, infile);
376 if (err)
377 goto done;
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);
382 goto done;
384 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
386 err = read_fileindex_val32(&entry->flags, ctx, infile);
387 if (err)
388 goto done;
390 err = read_fileindex_path(&entry->path, ctx, infile);
391 done:
392 if (err)
393 free(entry);
394 else
395 *entryp = entry;
396 return err;
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;
404 SHA1_CTX ctx;
405 struct got_fileindex_entry *entry;
406 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
407 uint8_t sha1[SHA1_DIGEST_LENGTH];
408 size_t n;
409 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
410 sizeof(hdr.nentries);
411 uint8_t buf[len];
412 int i;
414 SHA1Init(&ctx);
416 n = fread(buf, 1, len, infile);
417 if (n != len)
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);
434 if (err)
435 return err;
436 err = got_fileindex_entry_add(fileindex, entry);
437 if (err)
438 return err;
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);
448 return NULL;