Blame


1 c48c4a9c 2018-03-11 stsp /*
2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 c48c4a9c 2018-03-11 stsp *
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 c48c4a9c 2018-03-11 stsp *
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c48c4a9c 2018-03-11 stsp */
16 c48c4a9c 2018-03-11 stsp
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
20 c48c4a9c 2018-03-11 stsp
21 c48c4a9c 2018-03-11 stsp #include <stdio.h>
22 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
23 c48c4a9c 2018-03-11 stsp #include <string.h>
24 c48c4a9c 2018-03-11 stsp #include <sha1.h>
25 c34b20a2 2018-03-12 stsp #include <endian.h>
26 133d2798 2019-01-08 stsp #include <limits.h>
27 c48c4a9c 2018-03-11 stsp
28 c48c4a9c 2018-03-11 stsp #include "got_error.h"
29 c48c4a9c 2018-03-11 stsp
30 133d2798 2019-01-08 stsp #include "got_lib_path.h"
31 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
32 c48c4a9c 2018-03-11 stsp
33 133d2798 2019-01-08 stsp struct got_fileindex {
34 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
35 133d2798 2019-01-08 stsp int nentries;
36 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
37 133d2798 2019-01-08 stsp };
38 133d2798 2019-01-08 stsp
39 c48c4a9c 2018-03-11 stsp const struct got_error *
40 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
41 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
42 c48c4a9c 2018-03-11 stsp {
43 c48c4a9c 2018-03-11 stsp struct stat sb;
44 c48c4a9c 2018-03-11 stsp
45 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
46 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
47 c48c4a9c 2018-03-11 stsp
48 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
49 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
50 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
51 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
52 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
53 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
54 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
55 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
56 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
57 51514078 2018-12-25 stsp else
58 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
59 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
60 51514078 2018-12-25 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
61 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
62 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
63 51514078 2018-12-25 stsp
64 51514078 2018-12-25 stsp return NULL;
65 51514078 2018-12-25 stsp }
66 51514078 2018-12-25 stsp
67 51514078 2018-12-25 stsp const struct got_error *
68 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
69 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
70 51514078 2018-12-25 stsp uint8_t *commit_sha1)
71 51514078 2018-12-25 stsp {
72 51514078 2018-12-25 stsp size_t len;
73 51514078 2018-12-25 stsp
74 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
75 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
76 0a585a0d 2018-03-17 stsp return got_error_from_errno();
77 c48c4a9c 2018-03-11 stsp
78 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
79 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
80 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
81 c48c4a9c 2018-03-11 stsp free(*entry);
82 c48c4a9c 2018-03-11 stsp *entry = NULL;
83 0a585a0d 2018-03-17 stsp return err;
84 c48c4a9c 2018-03-11 stsp }
85 51514078 2018-12-25 stsp
86 c34b20a2 2018-03-12 stsp len = strlen(relpath);
87 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
88 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
89 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
90 c48c4a9c 2018-03-11 stsp
91 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
92 51514078 2018-12-25 stsp commit_sha1);
93 c48c4a9c 2018-03-11 stsp }
94 c48c4a9c 2018-03-11 stsp
95 c48c4a9c 2018-03-11 stsp void
96 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
97 c48c4a9c 2018-03-11 stsp {
98 c48c4a9c 2018-03-11 stsp free(entry->path);
99 c48c4a9c 2018-03-11 stsp free(entry);
100 c48c4a9c 2018-03-11 stsp }
101 9d31a1d8 2018-03-11 stsp
102 9d31a1d8 2018-03-11 stsp const struct got_error *
103 9d31a1d8 2018-03-11 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
104 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry)
105 9d31a1d8 2018-03-11 stsp {
106 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
108 133d2798 2019-01-08 stsp
109 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 133d2798 2019-01-08 stsp fileindex->nentries++;
111 133d2798 2019-01-08 stsp return NULL;
112 9d31a1d8 2018-03-11 stsp }
113 9d31a1d8 2018-03-11 stsp
114 133d2798 2019-01-08 stsp void
115 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
116 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
117 512f0d0e 2019-01-02 stsp {
118 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
119 133d2798 2019-01-08 stsp fileindex->nentries--;
120 512f0d0e 2019-01-02 stsp }
121 512f0d0e 2019-01-02 stsp
122 51514078 2018-12-25 stsp struct got_fileindex_entry *
123 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
124 51514078 2018-12-25 stsp {
125 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
126 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
127 133d2798 2019-01-08 stsp key.path = (char *)path;
128 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
129 b504a804 2019-01-08 stsp }
130 51514078 2018-12-25 stsp
131 512f0d0e 2019-01-02 stsp const struct got_error *
132 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
133 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
134 512f0d0e 2019-01-02 stsp {
135 133d2798 2019-01-08 stsp const struct got_error *err;
136 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
137 9d31a1d8 2018-03-11 stsp
138 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
139 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
140 133d2798 2019-01-08 stsp if (err)
141 133d2798 2019-01-08 stsp return err;
142 b504a804 2019-01-08 stsp }
143 b504a804 2019-01-08 stsp return NULL;
144 9d31a1d8 2018-03-11 stsp }
145 9d31a1d8 2018-03-11 stsp
146 133d2798 2019-01-08 stsp struct got_fileindex *
147 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
148 6b798c3c 2019-01-08 stsp {
149 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
150 133d2798 2019-01-08 stsp
151 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
152 133d2798 2019-01-08 stsp if (fileindex == NULL)
153 133d2798 2019-01-08 stsp return NULL;
154 133d2798 2019-01-08 stsp
155 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
156 133d2798 2019-01-08 stsp return fileindex;
157 6b798c3c 2019-01-08 stsp }
158 6b798c3c 2019-01-08 stsp
159 9d31a1d8 2018-03-11 stsp void
160 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
161 9d31a1d8 2018-03-11 stsp {
162 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
163 133d2798 2019-01-08 stsp
164 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
165 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
167 133d2798 2019-01-08 stsp }
168 9d31a1d8 2018-03-11 stsp free(fileindex);
169 9d31a1d8 2018-03-11 stsp }
170 9d31a1d8 2018-03-11 stsp
171 c34b20a2 2018-03-12 stsp static const struct got_error *
172 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
173 c34b20a2 2018-03-12 stsp {
174 c34b20a2 2018-03-12 stsp size_t n;
175 c34b20a2 2018-03-12 stsp
176 c34b20a2 2018-03-12 stsp val = htobe64(val);
177 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
178 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
179 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
180 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
181 c34b20a2 2018-03-12 stsp return NULL;
182 c34b20a2 2018-03-12 stsp }
183 c34b20a2 2018-03-12 stsp
184 c34b20a2 2018-03-12 stsp static const struct got_error *
185 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
186 c34b20a2 2018-03-12 stsp {
187 c34b20a2 2018-03-12 stsp size_t n;
188 c34b20a2 2018-03-12 stsp
189 c34b20a2 2018-03-12 stsp val = htobe32(val);
190 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
191 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
192 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
193 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
194 c34b20a2 2018-03-12 stsp return NULL;
195 c34b20a2 2018-03-12 stsp }
196 c34b20a2 2018-03-12 stsp
197 c34b20a2 2018-03-12 stsp static const struct got_error *
198 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
199 c34b20a2 2018-03-12 stsp {
200 c34b20a2 2018-03-12 stsp size_t n;
201 c34b20a2 2018-03-12 stsp
202 c34b20a2 2018-03-12 stsp val = htobe16(val);
203 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
204 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
205 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
206 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
207 c34b20a2 2018-03-12 stsp return NULL;
208 c34b20a2 2018-03-12 stsp }
209 c34b20a2 2018-03-12 stsp
210 c34b20a2 2018-03-12 stsp static const struct got_error *
211 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
212 c34b20a2 2018-03-12 stsp {
213 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
214 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
215 c34b20a2 2018-03-12 stsp
216 c34b20a2 2018-03-12 stsp len = strlen(path);
217 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
218 3c5b70f2 2018-12-27 stsp pad++;
219 3c5b70f2 2018-12-27 stsp if (pad == 0)
220 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
221 c34b20a2 2018-03-12 stsp
222 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
223 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
224 c34b20a2 2018-03-12 stsp if (n != len)
225 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
226 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
227 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
228 c34b20a2 2018-03-12 stsp if (n != pad)
229 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
230 c34b20a2 2018-03-12 stsp return NULL;
231 c34b20a2 2018-03-12 stsp }
232 c34b20a2 2018-03-12 stsp
233 c34b20a2 2018-03-12 stsp static const struct got_error *
234 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
235 c34b20a2 2018-03-12 stsp FILE *outfile)
236 c34b20a2 2018-03-12 stsp {
237 c34b20a2 2018-03-12 stsp const struct got_error *err;
238 23b19d00 2018-03-12 stsp size_t n;
239 c34b20a2 2018-03-12 stsp
240 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
241 c34b20a2 2018-03-12 stsp if (err)
242 c34b20a2 2018-03-12 stsp return err;
243 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
244 c34b20a2 2018-03-12 stsp if (err)
245 c34b20a2 2018-03-12 stsp return err;
246 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
247 c34b20a2 2018-03-12 stsp if (err)
248 c34b20a2 2018-03-12 stsp return err;
249 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
250 c34b20a2 2018-03-12 stsp if (err)
251 c34b20a2 2018-03-12 stsp return err;
252 c34b20a2 2018-03-12 stsp
253 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
254 c34b20a2 2018-03-12 stsp if (err)
255 c34b20a2 2018-03-12 stsp return err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
257 c34b20a2 2018-03-12 stsp if (err)
258 c34b20a2 2018-03-12 stsp return err;
259 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
260 c34b20a2 2018-03-12 stsp if (err)
261 c34b20a2 2018-03-12 stsp return err;
262 c34b20a2 2018-03-12 stsp
263 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
264 c34b20a2 2018-03-12 stsp if (err)
265 c34b20a2 2018-03-12 stsp return err;
266 c34b20a2 2018-03-12 stsp
267 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
268 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
269 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
270 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
271 fc76cabb 2018-12-25 stsp
272 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
273 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
274 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
275 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
276 c34b20a2 2018-03-12 stsp
277 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
278 c34b20a2 2018-03-12 stsp if (err)
279 c34b20a2 2018-03-12 stsp return err;
280 c34b20a2 2018-03-12 stsp
281 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
282 c34b20a2 2018-03-12 stsp return err;
283 c34b20a2 2018-03-12 stsp }
284 c34b20a2 2018-03-12 stsp
285 9d31a1d8 2018-03-11 stsp const struct got_error *
286 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
287 9d31a1d8 2018-03-11 stsp {
288 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
289 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
290 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
291 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
292 c34b20a2 2018-03-12 stsp size_t n;
293 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
294 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
295 c34b20a2 2018-03-12 stsp uint8_t buf[len];
296 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
297 c34b20a2 2018-03-12 stsp
298 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
299 c34b20a2 2018-03-12 stsp
300 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
301 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
302 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
303 c34b20a2 2018-03-12 stsp
304 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
305 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
306 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
307 c34b20a2 2018-03-12 stsp if (n != len)
308 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
309 c34b20a2 2018-03-12 stsp
310 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
311 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
312 133d2798 2019-01-08 stsp if (err)
313 133d2798 2019-01-08 stsp return err;
314 133d2798 2019-01-08 stsp }
315 c34b20a2 2018-03-12 stsp
316 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
317 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
318 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
319 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
320 52a74475 2018-12-24 stsp
321 52a74475 2018-12-24 stsp return NULL;
322 52a74475 2018-12-24 stsp }
323 52a74475 2018-12-24 stsp
324 52a74475 2018-12-24 stsp static const struct got_error *
325 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
326 52a74475 2018-12-24 stsp {
327 52a74475 2018-12-24 stsp size_t n;
328 52a74475 2018-12-24 stsp
329 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
330 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
331 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
332 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
333 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
334 52a74475 2018-12-24 stsp return NULL;
335 52a74475 2018-12-24 stsp }
336 52a74475 2018-12-24 stsp
337 52a74475 2018-12-24 stsp static const struct got_error *
338 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
339 52a74475 2018-12-24 stsp {
340 52a74475 2018-12-24 stsp size_t n;
341 52a74475 2018-12-24 stsp
342 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
343 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
344 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
345 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
346 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
347 52a74475 2018-12-24 stsp return NULL;
348 52a74475 2018-12-24 stsp }
349 52a74475 2018-12-24 stsp
350 52a74475 2018-12-24 stsp static const struct got_error *
351 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
352 52a74475 2018-12-24 stsp {
353 52a74475 2018-12-24 stsp size_t n;
354 52a74475 2018-12-24 stsp
355 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
356 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
357 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
358 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
359 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
360 52a74475 2018-12-24 stsp return NULL;
361 52a74475 2018-12-24 stsp }
362 52a74475 2018-12-24 stsp
363 52a74475 2018-12-24 stsp static const struct got_error *
364 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
365 52a74475 2018-12-24 stsp {
366 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
367 52a74475 2018-12-24 stsp uint8_t buf[8];
368 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
369 52a74475 2018-12-24 stsp
370 52a74475 2018-12-24 stsp *path = malloc(totlen);
371 52a74475 2018-12-24 stsp if (*path == NULL)
372 52a74475 2018-12-24 stsp return got_error_from_errno();
373 52a74475 2018-12-24 stsp
374 52a74475 2018-12-24 stsp do {
375 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
376 52a74475 2018-12-24 stsp if (n != sizeof(buf))
377 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
378 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
379 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
380 52a74475 2018-12-24 stsp if (p == NULL) {
381 52a74475 2018-12-24 stsp err = got_error_from_errno();
382 52a74475 2018-12-24 stsp break;
383 52a74475 2018-12-24 stsp }
384 52a74475 2018-12-24 stsp totlen += sizeof(buf);
385 52a74475 2018-12-24 stsp *path = p;
386 52a74475 2018-12-24 stsp }
387 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
388 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
389 52a74475 2018-12-24 stsp len += sizeof(buf);
390 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
391 52a74475 2018-12-24 stsp
392 52a74475 2018-12-24 stsp if (err) {
393 52a74475 2018-12-24 stsp free(*path);
394 52a74475 2018-12-24 stsp *path = NULL;
395 52a74475 2018-12-24 stsp }
396 52a74475 2018-12-24 stsp return err;
397 52a74475 2018-12-24 stsp }
398 52a74475 2018-12-24 stsp
399 52a74475 2018-12-24 stsp static const struct got_error *
400 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
401 52a74475 2018-12-24 stsp FILE *infile)
402 52a74475 2018-12-24 stsp {
403 52a74475 2018-12-24 stsp const struct got_error *err;
404 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
405 52a74475 2018-12-24 stsp size_t n;
406 52a74475 2018-12-24 stsp
407 52a74475 2018-12-24 stsp *entryp = NULL;
408 52a74475 2018-12-24 stsp
409 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
410 52a74475 2018-12-24 stsp if (entry == NULL)
411 52a74475 2018-12-24 stsp return got_error_from_errno();
412 c34b20a2 2018-03-12 stsp
413 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
414 52a74475 2018-12-24 stsp if (err)
415 52a74475 2018-12-24 stsp goto done;
416 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
417 52a74475 2018-12-24 stsp if (err)
418 52a74475 2018-12-24 stsp goto done;
419 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
420 52a74475 2018-12-24 stsp if (err)
421 52a74475 2018-12-24 stsp goto done;
422 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
423 52a74475 2018-12-24 stsp if (err)
424 52a74475 2018-12-24 stsp goto done;
425 52a74475 2018-12-24 stsp
426 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
427 52a74475 2018-12-24 stsp if (err)
428 52a74475 2018-12-24 stsp goto done;
429 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
430 52a74475 2018-12-24 stsp if (err)
431 52a74475 2018-12-24 stsp goto done;
432 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
433 52a74475 2018-12-24 stsp if (err)
434 52a74475 2018-12-24 stsp goto done;
435 52a74475 2018-12-24 stsp
436 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
437 52a74475 2018-12-24 stsp if (err)
438 52a74475 2018-12-24 stsp goto done;
439 52a74475 2018-12-24 stsp
440 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
441 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
442 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
443 52a74475 2018-12-24 stsp goto done;
444 52a74475 2018-12-24 stsp }
445 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
446 52a74475 2018-12-24 stsp
447 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
448 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
449 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
450 fc76cabb 2018-12-25 stsp goto done;
451 fc76cabb 2018-12-25 stsp }
452 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
453 fc76cabb 2018-12-25 stsp
454 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
455 52a74475 2018-12-24 stsp if (err)
456 52a74475 2018-12-24 stsp goto done;
457 52a74475 2018-12-24 stsp
458 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
459 52a74475 2018-12-24 stsp done:
460 52a74475 2018-12-24 stsp if (err)
461 52a74475 2018-12-24 stsp free(entry);
462 52a74475 2018-12-24 stsp else
463 52a74475 2018-12-24 stsp *entryp = entry;
464 52a74475 2018-12-24 stsp return err;
465 52a74475 2018-12-24 stsp }
466 52a74475 2018-12-24 stsp
467 52a74475 2018-12-24 stsp const struct got_error *
468 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
469 52a74475 2018-12-24 stsp {
470 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
471 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
472 52a74475 2018-12-24 stsp SHA1_CTX ctx;
473 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
474 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
475 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
476 52a74475 2018-12-24 stsp size_t n;
477 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
478 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
479 52a74475 2018-12-24 stsp uint8_t buf[len];
480 52a74475 2018-12-24 stsp int i;
481 52a74475 2018-12-24 stsp
482 52a74475 2018-12-24 stsp SHA1Init(&ctx);
483 52a74475 2018-12-24 stsp
484 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
485 51514078 2018-12-25 stsp if (n != len) {
486 51514078 2018-12-25 stsp if (n == 0) /* EOF */
487 51514078 2018-12-25 stsp return NULL;
488 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
489 51514078 2018-12-25 stsp }
490 52a74475 2018-12-24 stsp
491 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
492 52a74475 2018-12-24 stsp
493 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
494 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
495 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
496 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
497 52a74475 2018-12-24 stsp
498 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
499 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
500 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
501 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
502 52a74475 2018-12-24 stsp
503 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
504 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
505 52a74475 2018-12-24 stsp if (err)
506 52a74475 2018-12-24 stsp return err;
507 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
508 52a74475 2018-12-24 stsp if (err)
509 52a74475 2018-12-24 stsp return err;
510 52a74475 2018-12-24 stsp }
511 52a74475 2018-12-24 stsp
512 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
513 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
514 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
515 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
516 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
517 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
518 52a74475 2018-12-24 stsp
519 9d31a1d8 2018-03-11 stsp return NULL;
520 9d31a1d8 2018-03-11 stsp }
521 133d2798 2019-01-08 stsp
522 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);