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