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 c34b20a2 2018-03-12 stsp size_t n, len, pad;
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 c34b20a2 2018-03-12 stsp pad = (len % 8);
187 c34b20a2 2018-03-12 stsp
188 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
189 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
190 c34b20a2 2018-03-12 stsp if (n != len)
191 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
192 c34b20a2 2018-03-12 stsp if (pad == 0)
193 c34b20a2 2018-03-12 stsp return NULL;
194 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
195 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
196 c34b20a2 2018-03-12 stsp if (n != pad)
197 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
198 c34b20a2 2018-03-12 stsp return NULL;
199 c34b20a2 2018-03-12 stsp }
200 c34b20a2 2018-03-12 stsp
201 c34b20a2 2018-03-12 stsp static const struct got_error *
202 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
203 c34b20a2 2018-03-12 stsp FILE *outfile)
204 c34b20a2 2018-03-12 stsp {
205 c34b20a2 2018-03-12 stsp const struct got_error *err;
206 23b19d00 2018-03-12 stsp size_t n;
207 c34b20a2 2018-03-12 stsp
208 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
209 c34b20a2 2018-03-12 stsp if (err)
210 c34b20a2 2018-03-12 stsp return err;
211 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
212 c34b20a2 2018-03-12 stsp if (err)
213 c34b20a2 2018-03-12 stsp return err;
214 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
215 c34b20a2 2018-03-12 stsp if (err)
216 c34b20a2 2018-03-12 stsp return err;
217 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
218 c34b20a2 2018-03-12 stsp if (err)
219 c34b20a2 2018-03-12 stsp return err;
220 c34b20a2 2018-03-12 stsp
221 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
222 c34b20a2 2018-03-12 stsp if (err)
223 c34b20a2 2018-03-12 stsp return err;
224 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
225 c34b20a2 2018-03-12 stsp if (err)
226 c34b20a2 2018-03-12 stsp return err;
227 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
228 c34b20a2 2018-03-12 stsp if (err)
229 c34b20a2 2018-03-12 stsp return err;
230 c34b20a2 2018-03-12 stsp
231 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
232 c34b20a2 2018-03-12 stsp if (err)
233 c34b20a2 2018-03-12 stsp return err;
234 c34b20a2 2018-03-12 stsp
235 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
236 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
237 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
238 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
239 fc76cabb 2018-12-25 stsp
240 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
241 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
242 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
243 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
244 c34b20a2 2018-03-12 stsp
245 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
246 c34b20a2 2018-03-12 stsp if (err)
247 c34b20a2 2018-03-12 stsp return err;
248 c34b20a2 2018-03-12 stsp
249 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
250 c34b20a2 2018-03-12 stsp return err;
251 c34b20a2 2018-03-12 stsp }
252 c34b20a2 2018-03-12 stsp
253 9d31a1d8 2018-03-11 stsp const struct got_error *
254 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
255 9d31a1d8 2018-03-11 stsp {
256 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
257 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
258 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
259 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
260 c34b20a2 2018-03-12 stsp size_t n;
261 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
262 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
263 c34b20a2 2018-03-12 stsp uint8_t buf[len];
264 c34b20a2 2018-03-12 stsp
265 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
266 c34b20a2 2018-03-12 stsp
267 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
268 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
269 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
270 c34b20a2 2018-03-12 stsp
271 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
272 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
273 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
274 c34b20a2 2018-03-12 stsp if (n != len)
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 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
278 c34b20a2 2018-03-12 stsp const struct got_error *err;
279 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
280 c34b20a2 2018-03-12 stsp if (err)
281 c34b20a2 2018-03-12 stsp return err;
282 c34b20a2 2018-03-12 stsp }
283 c34b20a2 2018-03-12 stsp
284 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
285 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
286 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
287 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
288 52a74475 2018-12-24 stsp
289 52a74475 2018-12-24 stsp return NULL;
290 52a74475 2018-12-24 stsp }
291 52a74475 2018-12-24 stsp
292 52a74475 2018-12-24 stsp static const struct got_error *
293 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
294 52a74475 2018-12-24 stsp {
295 52a74475 2018-12-24 stsp size_t n;
296 52a74475 2018-12-24 stsp
297 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
298 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
299 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
300 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
301 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
302 52a74475 2018-12-24 stsp return NULL;
303 52a74475 2018-12-24 stsp }
304 52a74475 2018-12-24 stsp
305 52a74475 2018-12-24 stsp static const struct got_error *
306 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
307 52a74475 2018-12-24 stsp {
308 52a74475 2018-12-24 stsp size_t n;
309 52a74475 2018-12-24 stsp
310 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
311 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
312 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
313 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
314 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
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_val16(uint16_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 = be16toh(*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_path(char **path, SHA1_CTX *ctx, FILE *infile)
333 52a74475 2018-12-24 stsp {
334 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
335 52a74475 2018-12-24 stsp uint8_t buf[8];
336 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
337 52a74475 2018-12-24 stsp
338 52a74475 2018-12-24 stsp *path = malloc(totlen);
339 52a74475 2018-12-24 stsp if (*path == NULL)
340 52a74475 2018-12-24 stsp return got_error_from_errno();
341 52a74475 2018-12-24 stsp
342 52a74475 2018-12-24 stsp do {
343 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
344 52a74475 2018-12-24 stsp if (n != sizeof(buf))
345 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
346 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
347 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
348 52a74475 2018-12-24 stsp if (p == NULL) {
349 52a74475 2018-12-24 stsp err = got_error_from_errno();
350 52a74475 2018-12-24 stsp break;
351 52a74475 2018-12-24 stsp }
352 52a74475 2018-12-24 stsp totlen += sizeof(buf);
353 52a74475 2018-12-24 stsp *path = p;
354 52a74475 2018-12-24 stsp }
355 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
356 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
357 52a74475 2018-12-24 stsp len += sizeof(buf);
358 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
359 52a74475 2018-12-24 stsp
360 52a74475 2018-12-24 stsp if (err) {
361 52a74475 2018-12-24 stsp free(*path);
362 52a74475 2018-12-24 stsp *path = NULL;
363 52a74475 2018-12-24 stsp }
364 52a74475 2018-12-24 stsp return err;
365 52a74475 2018-12-24 stsp }
366 52a74475 2018-12-24 stsp
367 52a74475 2018-12-24 stsp static const struct got_error *
368 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
369 52a74475 2018-12-24 stsp FILE *infile)
370 52a74475 2018-12-24 stsp {
371 52a74475 2018-12-24 stsp const struct got_error *err;
372 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
373 52a74475 2018-12-24 stsp size_t n;
374 52a74475 2018-12-24 stsp
375 52a74475 2018-12-24 stsp *entryp = NULL;
376 52a74475 2018-12-24 stsp
377 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
378 52a74475 2018-12-24 stsp if (entry == NULL)
379 52a74475 2018-12-24 stsp return got_error_from_errno();
380 c34b20a2 2018-03-12 stsp
381 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
382 52a74475 2018-12-24 stsp if (err)
383 52a74475 2018-12-24 stsp goto done;
384 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
385 52a74475 2018-12-24 stsp if (err)
386 52a74475 2018-12-24 stsp goto done;
387 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
388 52a74475 2018-12-24 stsp if (err)
389 52a74475 2018-12-24 stsp goto done;
390 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
391 52a74475 2018-12-24 stsp if (err)
392 52a74475 2018-12-24 stsp goto done;
393 52a74475 2018-12-24 stsp
394 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
395 52a74475 2018-12-24 stsp if (err)
396 52a74475 2018-12-24 stsp goto done;
397 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
398 52a74475 2018-12-24 stsp if (err)
399 52a74475 2018-12-24 stsp goto done;
400 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
401 52a74475 2018-12-24 stsp if (err)
402 52a74475 2018-12-24 stsp goto done;
403 52a74475 2018-12-24 stsp
404 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
405 52a74475 2018-12-24 stsp if (err)
406 52a74475 2018-12-24 stsp goto done;
407 52a74475 2018-12-24 stsp
408 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
409 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
410 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
411 52a74475 2018-12-24 stsp goto done;
412 52a74475 2018-12-24 stsp }
413 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
414 52a74475 2018-12-24 stsp
415 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
416 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
417 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
418 fc76cabb 2018-12-25 stsp goto done;
419 fc76cabb 2018-12-25 stsp }
420 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
421 fc76cabb 2018-12-25 stsp
422 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, 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_path(&entry->path, ctx, infile);
427 52a74475 2018-12-24 stsp done:
428 52a74475 2018-12-24 stsp if (err)
429 52a74475 2018-12-24 stsp free(entry);
430 52a74475 2018-12-24 stsp else
431 52a74475 2018-12-24 stsp *entryp = entry;
432 52a74475 2018-12-24 stsp return err;
433 52a74475 2018-12-24 stsp }
434 52a74475 2018-12-24 stsp
435 52a74475 2018-12-24 stsp const struct got_error *
436 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
437 52a74475 2018-12-24 stsp {
438 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
439 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
440 52a74475 2018-12-24 stsp SHA1_CTX ctx;
441 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
442 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
443 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
444 52a74475 2018-12-24 stsp size_t n;
445 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
446 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
447 52a74475 2018-12-24 stsp uint8_t buf[len];
448 52a74475 2018-12-24 stsp int i;
449 52a74475 2018-12-24 stsp
450 52a74475 2018-12-24 stsp SHA1Init(&ctx);
451 52a74475 2018-12-24 stsp
452 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
453 51514078 2018-12-25 stsp if (n != len) {
454 51514078 2018-12-25 stsp if (n == 0) /* EOF */
455 51514078 2018-12-25 stsp return NULL;
456 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
457 51514078 2018-12-25 stsp }
458 52a74475 2018-12-24 stsp
459 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
460 52a74475 2018-12-24 stsp
461 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
462 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
463 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
464 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
465 52a74475 2018-12-24 stsp
466 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
467 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
468 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
469 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
470 52a74475 2018-12-24 stsp
471 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
472 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
473 52a74475 2018-12-24 stsp if (err)
474 52a74475 2018-12-24 stsp return err;
475 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
476 52a74475 2018-12-24 stsp if (err)
477 52a74475 2018-12-24 stsp return err;
478 52a74475 2018-12-24 stsp }
479 52a74475 2018-12-24 stsp
480 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
481 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
482 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
483 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
484 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
485 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
486 52a74475 2018-12-24 stsp
487 9d31a1d8 2018-03-11 stsp return NULL;
488 9d31a1d8 2018-03-11 stsp }