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 51514078 2018-12-25 stsp struct got_fileindex_entry *
104 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
105 51514078 2018-12-25 stsp {
106 51514078 2018-12-25 stsp struct got_fileindex_entry *entry;
107 51514078 2018-12-25 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
108 51514078 2018-12-25 stsp if (strcmp(entry->path, path) == 0)
109 51514078 2018-12-25 stsp return entry;
110 51514078 2018-12-25 stsp }
111 51514078 2018-12-25 stsp
112 51514078 2018-12-25 stsp return NULL;
113 51514078 2018-12-25 stsp }
114 51514078 2018-12-25 stsp
115 9d31a1d8 2018-03-11 stsp struct got_fileindex *
116 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
117 9d31a1d8 2018-03-11 stsp {
118 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
119 9d31a1d8 2018-03-11 stsp
120 9d31a1d8 2018-03-11 stsp fileindex = calloc(1, sizeof(*fileindex));
121 9d31a1d8 2018-03-11 stsp if (fileindex)
122 9d31a1d8 2018-03-11 stsp TAILQ_INIT(&fileindex->entries);
123 9d31a1d8 2018-03-11 stsp return fileindex;
124 9d31a1d8 2018-03-11 stsp }
125 9d31a1d8 2018-03-11 stsp
126 9d31a1d8 2018-03-11 stsp void
127 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
128 9d31a1d8 2018-03-11 stsp {
129 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
130 9d31a1d8 2018-03-11 stsp
131 9d31a1d8 2018-03-11 stsp while (!TAILQ_EMPTY(&fileindex->entries)) {
132 9d31a1d8 2018-03-11 stsp entry = TAILQ_FIRST(&fileindex->entries);
133 9d31a1d8 2018-03-11 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
134 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(entry);
135 9d31a1d8 2018-03-11 stsp fileindex->nentries--;
136 9d31a1d8 2018-03-11 stsp }
137 9d31a1d8 2018-03-11 stsp free(fileindex);
138 9d31a1d8 2018-03-11 stsp }
139 9d31a1d8 2018-03-11 stsp
140 c34b20a2 2018-03-12 stsp static const struct got_error *
141 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
142 c34b20a2 2018-03-12 stsp {
143 c34b20a2 2018-03-12 stsp size_t n;
144 c34b20a2 2018-03-12 stsp
145 c34b20a2 2018-03-12 stsp val = htobe64(val);
146 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
147 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
148 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
149 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
150 c34b20a2 2018-03-12 stsp return NULL;
151 c34b20a2 2018-03-12 stsp }
152 c34b20a2 2018-03-12 stsp
153 c34b20a2 2018-03-12 stsp static const struct got_error *
154 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
155 c34b20a2 2018-03-12 stsp {
156 c34b20a2 2018-03-12 stsp size_t n;
157 c34b20a2 2018-03-12 stsp
158 c34b20a2 2018-03-12 stsp val = htobe32(val);
159 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
160 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
161 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
162 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
163 c34b20a2 2018-03-12 stsp return NULL;
164 c34b20a2 2018-03-12 stsp }
165 c34b20a2 2018-03-12 stsp
166 c34b20a2 2018-03-12 stsp static const struct got_error *
167 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
168 c34b20a2 2018-03-12 stsp {
169 c34b20a2 2018-03-12 stsp size_t n;
170 c34b20a2 2018-03-12 stsp
171 c34b20a2 2018-03-12 stsp val = htobe16(val);
172 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
173 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
174 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
175 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
176 c34b20a2 2018-03-12 stsp return NULL;
177 c34b20a2 2018-03-12 stsp }
178 c34b20a2 2018-03-12 stsp
179 c34b20a2 2018-03-12 stsp static const struct got_error *
180 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
181 c34b20a2 2018-03-12 stsp {
182 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
183 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
184 c34b20a2 2018-03-12 stsp
185 c34b20a2 2018-03-12 stsp len = strlen(path);
186 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
187 3c5b70f2 2018-12-27 stsp pad++;
188 3c5b70f2 2018-12-27 stsp if (pad == 0)
189 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
190 c34b20a2 2018-03-12 stsp
191 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
192 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
193 c34b20a2 2018-03-12 stsp if (n != len)
194 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
195 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
196 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
197 c34b20a2 2018-03-12 stsp if (n != pad)
198 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
199 c34b20a2 2018-03-12 stsp return NULL;
200 c34b20a2 2018-03-12 stsp }
201 c34b20a2 2018-03-12 stsp
202 c34b20a2 2018-03-12 stsp static const struct got_error *
203 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
204 c34b20a2 2018-03-12 stsp FILE *outfile)
205 c34b20a2 2018-03-12 stsp {
206 c34b20a2 2018-03-12 stsp const struct got_error *err;
207 23b19d00 2018-03-12 stsp size_t n;
208 c34b20a2 2018-03-12 stsp
209 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
210 c34b20a2 2018-03-12 stsp if (err)
211 c34b20a2 2018-03-12 stsp return err;
212 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
213 c34b20a2 2018-03-12 stsp if (err)
214 c34b20a2 2018-03-12 stsp return err;
215 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
216 c34b20a2 2018-03-12 stsp if (err)
217 c34b20a2 2018-03-12 stsp return err;
218 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
219 c34b20a2 2018-03-12 stsp if (err)
220 c34b20a2 2018-03-12 stsp return err;
221 c34b20a2 2018-03-12 stsp
222 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
223 c34b20a2 2018-03-12 stsp if (err)
224 c34b20a2 2018-03-12 stsp return err;
225 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
226 c34b20a2 2018-03-12 stsp if (err)
227 c34b20a2 2018-03-12 stsp return err;
228 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
229 c34b20a2 2018-03-12 stsp if (err)
230 c34b20a2 2018-03-12 stsp return err;
231 c34b20a2 2018-03-12 stsp
232 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
233 c34b20a2 2018-03-12 stsp if (err)
234 c34b20a2 2018-03-12 stsp return err;
235 c34b20a2 2018-03-12 stsp
236 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
237 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
238 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
239 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
240 fc76cabb 2018-12-25 stsp
241 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
242 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
243 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
244 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
245 c34b20a2 2018-03-12 stsp
246 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
247 c34b20a2 2018-03-12 stsp if (err)
248 c34b20a2 2018-03-12 stsp return err;
249 c34b20a2 2018-03-12 stsp
250 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
251 c34b20a2 2018-03-12 stsp return err;
252 c34b20a2 2018-03-12 stsp }
253 c34b20a2 2018-03-12 stsp
254 9d31a1d8 2018-03-11 stsp const struct got_error *
255 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
256 9d31a1d8 2018-03-11 stsp {
257 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
258 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
259 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
260 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
261 c34b20a2 2018-03-12 stsp size_t n;
262 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
263 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
264 c34b20a2 2018-03-12 stsp uint8_t buf[len];
265 c34b20a2 2018-03-12 stsp
266 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
267 c34b20a2 2018-03-12 stsp
268 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
269 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
270 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
271 c34b20a2 2018-03-12 stsp
272 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
273 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
274 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
275 c34b20a2 2018-03-12 stsp if (n != len)
276 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp
278 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
279 c34b20a2 2018-03-12 stsp const struct got_error *err;
280 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, 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
285 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
286 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
287 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
288 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
289 52a74475 2018-12-24 stsp
290 52a74475 2018-12-24 stsp return NULL;
291 52a74475 2018-12-24 stsp }
292 52a74475 2018-12-24 stsp
293 52a74475 2018-12-24 stsp static const struct got_error *
294 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
295 52a74475 2018-12-24 stsp {
296 52a74475 2018-12-24 stsp size_t n;
297 52a74475 2018-12-24 stsp
298 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
299 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
300 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
301 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
302 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
303 52a74475 2018-12-24 stsp return NULL;
304 52a74475 2018-12-24 stsp }
305 52a74475 2018-12-24 stsp
306 52a74475 2018-12-24 stsp static const struct got_error *
307 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
308 52a74475 2018-12-24 stsp {
309 52a74475 2018-12-24 stsp size_t n;
310 52a74475 2018-12-24 stsp
311 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
312 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
313 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
314 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
315 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
316 52a74475 2018-12-24 stsp return NULL;
317 52a74475 2018-12-24 stsp }
318 52a74475 2018-12-24 stsp
319 52a74475 2018-12-24 stsp static const struct got_error *
320 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
321 52a74475 2018-12-24 stsp {
322 52a74475 2018-12-24 stsp size_t n;
323 52a74475 2018-12-24 stsp
324 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
325 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
326 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
327 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
328 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
329 52a74475 2018-12-24 stsp return NULL;
330 52a74475 2018-12-24 stsp }
331 52a74475 2018-12-24 stsp
332 52a74475 2018-12-24 stsp static const struct got_error *
333 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
334 52a74475 2018-12-24 stsp {
335 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
336 52a74475 2018-12-24 stsp uint8_t buf[8];
337 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
338 52a74475 2018-12-24 stsp
339 52a74475 2018-12-24 stsp *path = malloc(totlen);
340 52a74475 2018-12-24 stsp if (*path == NULL)
341 52a74475 2018-12-24 stsp return got_error_from_errno();
342 52a74475 2018-12-24 stsp
343 52a74475 2018-12-24 stsp do {
344 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
345 52a74475 2018-12-24 stsp if (n != sizeof(buf))
346 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
347 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
348 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
349 52a74475 2018-12-24 stsp if (p == NULL) {
350 52a74475 2018-12-24 stsp err = got_error_from_errno();
351 52a74475 2018-12-24 stsp break;
352 52a74475 2018-12-24 stsp }
353 52a74475 2018-12-24 stsp totlen += sizeof(buf);
354 52a74475 2018-12-24 stsp *path = p;
355 52a74475 2018-12-24 stsp }
356 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
357 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
358 52a74475 2018-12-24 stsp len += sizeof(buf);
359 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
360 52a74475 2018-12-24 stsp
361 52a74475 2018-12-24 stsp if (err) {
362 52a74475 2018-12-24 stsp free(*path);
363 52a74475 2018-12-24 stsp *path = NULL;
364 52a74475 2018-12-24 stsp }
365 52a74475 2018-12-24 stsp return err;
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_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
370 52a74475 2018-12-24 stsp FILE *infile)
371 52a74475 2018-12-24 stsp {
372 52a74475 2018-12-24 stsp const struct got_error *err;
373 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
374 52a74475 2018-12-24 stsp size_t n;
375 52a74475 2018-12-24 stsp
376 52a74475 2018-12-24 stsp *entryp = NULL;
377 52a74475 2018-12-24 stsp
378 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
379 52a74475 2018-12-24 stsp if (entry == NULL)
380 52a74475 2018-12-24 stsp return got_error_from_errno();
381 c34b20a2 2018-03-12 stsp
382 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
383 52a74475 2018-12-24 stsp if (err)
384 52a74475 2018-12-24 stsp goto done;
385 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
386 52a74475 2018-12-24 stsp if (err)
387 52a74475 2018-12-24 stsp goto done;
388 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
389 52a74475 2018-12-24 stsp if (err)
390 52a74475 2018-12-24 stsp goto done;
391 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
392 52a74475 2018-12-24 stsp if (err)
393 52a74475 2018-12-24 stsp goto done;
394 52a74475 2018-12-24 stsp
395 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
396 52a74475 2018-12-24 stsp if (err)
397 52a74475 2018-12-24 stsp goto done;
398 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
399 52a74475 2018-12-24 stsp if (err)
400 52a74475 2018-12-24 stsp goto done;
401 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
402 52a74475 2018-12-24 stsp if (err)
403 52a74475 2018-12-24 stsp goto done;
404 52a74475 2018-12-24 stsp
405 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
406 52a74475 2018-12-24 stsp if (err)
407 52a74475 2018-12-24 stsp goto done;
408 52a74475 2018-12-24 stsp
409 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
410 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
411 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
412 52a74475 2018-12-24 stsp goto done;
413 52a74475 2018-12-24 stsp }
414 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
415 52a74475 2018-12-24 stsp
416 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
417 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
418 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
419 fc76cabb 2018-12-25 stsp goto done;
420 fc76cabb 2018-12-25 stsp }
421 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
422 fc76cabb 2018-12-25 stsp
423 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
424 52a74475 2018-12-24 stsp if (err)
425 52a74475 2018-12-24 stsp goto done;
426 52a74475 2018-12-24 stsp
427 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
428 52a74475 2018-12-24 stsp done:
429 52a74475 2018-12-24 stsp if (err)
430 52a74475 2018-12-24 stsp free(entry);
431 52a74475 2018-12-24 stsp else
432 52a74475 2018-12-24 stsp *entryp = entry;
433 52a74475 2018-12-24 stsp return err;
434 52a74475 2018-12-24 stsp }
435 52a74475 2018-12-24 stsp
436 52a74475 2018-12-24 stsp const struct got_error *
437 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
438 52a74475 2018-12-24 stsp {
439 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
440 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
441 52a74475 2018-12-24 stsp SHA1_CTX ctx;
442 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
443 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
444 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
445 52a74475 2018-12-24 stsp size_t n;
446 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
447 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
448 52a74475 2018-12-24 stsp uint8_t buf[len];
449 52a74475 2018-12-24 stsp int i;
450 52a74475 2018-12-24 stsp
451 52a74475 2018-12-24 stsp SHA1Init(&ctx);
452 52a74475 2018-12-24 stsp
453 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
454 51514078 2018-12-25 stsp if (n != len) {
455 51514078 2018-12-25 stsp if (n == 0) /* EOF */
456 51514078 2018-12-25 stsp return NULL;
457 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
458 51514078 2018-12-25 stsp }
459 52a74475 2018-12-24 stsp
460 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
461 52a74475 2018-12-24 stsp
462 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
463 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
464 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
465 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
466 52a74475 2018-12-24 stsp
467 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
468 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
469 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
470 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
471 52a74475 2018-12-24 stsp
472 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
473 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
474 52a74475 2018-12-24 stsp if (err)
475 52a74475 2018-12-24 stsp return err;
476 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
477 52a74475 2018-12-24 stsp if (err)
478 52a74475 2018-12-24 stsp return err;
479 52a74475 2018-12-24 stsp }
480 52a74475 2018-12-24 stsp
481 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
482 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
483 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
484 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
485 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
486 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
487 52a74475 2018-12-24 stsp
488 9d31a1d8 2018-03-11 stsp return NULL;
489 9d31a1d8 2018-03-11 stsp }