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