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_open(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_close(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_open(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_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--;
113 free(fileindex);
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)];
120 size_t n;
122 val = htobe64(val);
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);
128 return NULL;
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)];
135 size_t n;
137 val = htobe32(val);
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);
143 return NULL;
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)];
150 size_t n;
152 val = htobe16(val);
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);
158 return NULL;
161 static const struct got_error *
162 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
164 size_t n, len, pad;
165 static const uint8_t zero[8] = { 0 };
167 len = strlen(path);
168 pad = (len % 8);
170 SHA1Update(ctx, path, len);
171 n = fwrite(path, 1, len, outfile);
172 if (n != len)
173 return got_ferror(outfile, GOT_ERR_IO);
174 if (pad == 0)
175 return NULL;
176 SHA1Update(ctx, zero, pad);
177 n = fwrite(zero, 1, pad, outfile);
178 if (n != pad)
179 return got_ferror(outfile, GOT_ERR_IO);
180 return NULL;
183 static const struct got_error *
184 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
185 FILE *outfile)
187 const struct got_error *err;
188 size_t n;
190 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
191 if (err)
192 return err;
193 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
194 if (err)
195 return err;
196 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
197 if (err)
198 return err;
199 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
200 if (err)
201 return err;
203 err = write_fileindex_val32(ctx, entry->uid, outfile);
204 if (err)
205 return err;
206 err = write_fileindex_val32(ctx, entry->gid, outfile);
207 if (err)
208 return err;
209 err = write_fileindex_val32(ctx, entry->size, outfile);
210 if (err)
211 return err;
213 err = write_fileindex_val16(ctx, entry->mode, outfile);
214 if (err)
215 return err;
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);
223 if (err)
224 return err;
226 err = write_fileindex_path(ctx, entry->path, outfile);
227 return err;
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;
235 SHA1_CTX ctx;
236 uint8_t sha1[SHA1_DIGEST_LENGTH];
237 size_t n;
238 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
239 sizeof(hdr.nentries);
240 uint8_t buf[len];
242 SHA1Init(&ctx);
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);
251 if (n != len)
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);
257 if (err)
258 return err;
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);
266 return NULL;
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)];
273 size_t n;
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);
281 return NULL;
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)];
288 size_t n;
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);
296 return NULL;
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)];
303 size_t n;
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);
311 return NULL;
314 static const struct got_error *
315 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
317 const struct got_error *err = NULL;
318 uint8_t buf[8];
319 size_t n, len = 0, totlen = sizeof(buf);
321 *path = malloc(totlen);
322 if (*path == NULL)
323 return got_error_from_errno();
325 do {
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);
331 if (p == NULL) {
332 err = got_error_from_errno();
333 break;
335 totlen += sizeof(buf);
336 *path = p;
338 SHA1Update(ctx, buf, sizeof(buf));
339 memcpy(*path + len, buf, sizeof(buf));
340 len += sizeof(buf);
341 } while (strchr(buf, '\0') == NULL);
343 if (err) {
344 free(*path);
345 *path = NULL;
347 return err;
350 static const struct got_error *
351 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
352 FILE *infile)
354 const struct got_error *err;
355 struct got_fileindex_entry *entry;
356 size_t n;
358 *entryp = NULL;
360 entry = calloc(1, sizeof(*entry));
361 if (entry == NULL)
362 return got_error_from_errno();
364 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
365 if (err)
366 goto done;
367 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
368 if (err)
369 goto done;
370 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
371 if (err)
372 goto done;
373 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
374 if (err)
375 goto done;
377 err = read_fileindex_val32(&entry->uid, ctx, infile);
378 if (err)
379 goto done;
380 err = read_fileindex_val32(&entry->gid, ctx, infile);
381 if (err)
382 goto done;
383 err = read_fileindex_val32(&entry->size, ctx, infile);
384 if (err)
385 goto done;
387 err = read_fileindex_val16(&entry->mode, ctx, infile);
388 if (err)
389 goto done;
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);
394 goto done;
396 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
398 err = read_fileindex_val32(&entry->flags, ctx, infile);
399 if (err)
400 goto done;
402 err = read_fileindex_path(&entry->path, ctx, infile);
403 done:
404 if (err)
405 free(entry);
406 else
407 *entryp = entry;
408 return err;
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;
416 SHA1_CTX ctx;
417 struct got_fileindex_entry *entry;
418 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
419 uint8_t sha1[SHA1_DIGEST_LENGTH];
420 size_t n;
421 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
422 sizeof(hdr.nentries);
423 uint8_t buf[len];
424 int i;
426 SHA1Init(&ctx);
428 n = fread(buf, 1, len, infile);
429 if (n != len)
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);
446 if (err)
447 return err;
448 err = got_fileindex_entry_add(fileindex, entry);
449 if (err)
450 return err;
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);
460 return NULL;