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