2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
21 c48c4a9c 2018-03-11 stsp #include <stdio.h>
22 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
23 c48c4a9c 2018-03-11 stsp #include <string.h>
24 c48c4a9c 2018-03-11 stsp #include <sha1.h>
25 c34b20a2 2018-03-12 stsp #include <endian.h>
26 133d2798 2019-01-08 stsp #include <limits.h>
28 c48c4a9c 2018-03-11 stsp #include "got_error.h"
29 8da9e5f4 2019-01-12 stsp #include "got_object.h"
31 133d2798 2019-01-08 stsp #include "got_lib_path.h"
32 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
34 133d2798 2019-01-08 stsp struct got_fileindex {
35 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
36 133d2798 2019-01-08 stsp int nentries;
37 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
40 c48c4a9c 2018-03-11 stsp const struct got_error *
41 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
42 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
44 c48c4a9c 2018-03-11 stsp struct stat sb;
46 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
47 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
49 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
50 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
51 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
52 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
53 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
54 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
55 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
56 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
57 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
59 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
60 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
61 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
62 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
63 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
65 51514078 2018-12-25 stsp return NULL;
68 51514078 2018-12-25 stsp const struct got_error *
69 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
70 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
71 51514078 2018-12-25 stsp uint8_t *commit_sha1)
75 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
76 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
77 0a585a0d 2018-03-17 stsp return got_error_from_errno();
79 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
80 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
81 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
82 c48c4a9c 2018-03-11 stsp free(*entry);
83 c48c4a9c 2018-03-11 stsp *entry = NULL;
87 c34b20a2 2018-03-12 stsp len = strlen(relpath);
88 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
89 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
90 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
92 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
93 51514078 2018-12-25 stsp commit_sha1);
97 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
99 c48c4a9c 2018-03-11 stsp free(entry->path);
100 c48c4a9c 2018-03-11 stsp free(entry);
103 50952927 2019-01-12 stsp static const struct got_error *
104 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
106 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
109 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 133d2798 2019-01-08 stsp fileindex->nentries++;
111 133d2798 2019-01-08 stsp return NULL;
114 50952927 2019-01-12 stsp const struct got_error *
115 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
116 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
118 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
119 a7f9d64d 2019-01-13 stsp entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
121 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
125 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
126 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
128 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
129 133d2798 2019-01-08 stsp fileindex->nentries--;
132 51514078 2018-12-25 stsp struct got_fileindex_entry *
133 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
135 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
136 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
137 133d2798 2019-01-08 stsp key.path = (char *)path;
138 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
141 512f0d0e 2019-01-02 stsp const struct got_error *
142 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
143 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
145 133d2798 2019-01-08 stsp const struct got_error *err;
146 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
148 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
149 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
151 133d2798 2019-01-08 stsp return err;
153 b504a804 2019-01-08 stsp return NULL;
156 133d2798 2019-01-08 stsp struct got_fileindex *
157 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
159 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
161 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
162 133d2798 2019-01-08 stsp if (fileindex == NULL)
163 133d2798 2019-01-08 stsp return NULL;
165 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
166 133d2798 2019-01-08 stsp return fileindex;
170 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
172 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
174 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
175 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
176 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
178 9d31a1d8 2018-03-11 stsp free(fileindex);
181 c34b20a2 2018-03-12 stsp static const struct got_error *
182 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
186 c34b20a2 2018-03-12 stsp val = htobe64(val);
187 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
188 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
189 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
190 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
191 c34b20a2 2018-03-12 stsp return NULL;
194 c34b20a2 2018-03-12 stsp static const struct got_error *
195 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
199 c34b20a2 2018-03-12 stsp val = htobe32(val);
200 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
201 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
202 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
203 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
204 c34b20a2 2018-03-12 stsp return NULL;
207 c34b20a2 2018-03-12 stsp static const struct got_error *
208 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
212 c34b20a2 2018-03-12 stsp val = htobe16(val);
213 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
214 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
215 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
216 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
217 c34b20a2 2018-03-12 stsp return NULL;
220 c34b20a2 2018-03-12 stsp static const struct got_error *
221 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
223 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
224 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
226 c34b20a2 2018-03-12 stsp len = strlen(path);
227 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
229 3c5b70f2 2018-12-27 stsp if (pad == 0)
230 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
232 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
233 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
234 c34b20a2 2018-03-12 stsp if (n != len)
235 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
236 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
237 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
238 c34b20a2 2018-03-12 stsp if (n != pad)
239 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
240 c34b20a2 2018-03-12 stsp return NULL;
243 c34b20a2 2018-03-12 stsp static const struct got_error *
244 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
245 c34b20a2 2018-03-12 stsp FILE *outfile)
247 c34b20a2 2018-03-12 stsp const struct got_error *err;
250 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
252 c34b20a2 2018-03-12 stsp return err;
253 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
255 c34b20a2 2018-03-12 stsp return err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
258 c34b20a2 2018-03-12 stsp return err;
259 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
261 c34b20a2 2018-03-12 stsp return err;
263 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
265 c34b20a2 2018-03-12 stsp return err;
266 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
268 c34b20a2 2018-03-12 stsp return err;
269 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
271 c34b20a2 2018-03-12 stsp return err;
273 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
275 c34b20a2 2018-03-12 stsp return err;
277 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
278 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
279 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
280 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
282 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
283 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
284 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
285 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
287 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
289 c34b20a2 2018-03-12 stsp return err;
291 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
292 c34b20a2 2018-03-12 stsp return err;
295 9d31a1d8 2018-03-11 stsp const struct got_error *
296 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
298 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
299 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
300 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
301 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
303 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
305 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
307 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
308 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
309 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
311 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
312 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
313 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
314 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
315 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
316 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
317 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
318 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
319 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
320 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
321 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
322 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
324 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
325 a7f9d64d 2019-01-13 stsp entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
326 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
328 133d2798 2019-01-08 stsp return err;
331 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
332 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
333 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
334 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
336 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
337 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
339 52a74475 2018-12-24 stsp return NULL;
342 52a74475 2018-12-24 stsp static const struct got_error *
343 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
347 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
348 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
349 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
350 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
351 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
352 52a74475 2018-12-24 stsp return NULL;
355 52a74475 2018-12-24 stsp static const struct got_error *
356 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
360 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
361 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
362 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
363 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
364 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
365 52a74475 2018-12-24 stsp return NULL;
368 52a74475 2018-12-24 stsp static const struct got_error *
369 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
373 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
374 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
375 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
376 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
377 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
378 52a74475 2018-12-24 stsp return NULL;
381 52a74475 2018-12-24 stsp static const struct got_error *
382 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
384 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
385 52a74475 2018-12-24 stsp uint8_t buf[8];
386 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
388 52a74475 2018-12-24 stsp *path = malloc(totlen);
389 52a74475 2018-12-24 stsp if (*path == NULL)
390 52a74475 2018-12-24 stsp return got_error_from_errno();
393 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
394 52a74475 2018-12-24 stsp if (n != sizeof(buf))
395 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
396 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
397 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
398 52a74475 2018-12-24 stsp if (p == NULL) {
399 52a74475 2018-12-24 stsp err = got_error_from_errno();
402 52a74475 2018-12-24 stsp totlen += sizeof(buf);
405 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
406 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
407 52a74475 2018-12-24 stsp len += sizeof(buf);
408 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
411 52a74475 2018-12-24 stsp free(*path);
412 52a74475 2018-12-24 stsp *path = NULL;
414 52a74475 2018-12-24 stsp return err;
417 52a74475 2018-12-24 stsp static const struct got_error *
418 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
419 52a74475 2018-12-24 stsp FILE *infile)
421 52a74475 2018-12-24 stsp const struct got_error *err;
422 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
425 52a74475 2018-12-24 stsp *entryp = NULL;
427 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
428 52a74475 2018-12-24 stsp if (entry == NULL)
429 52a74475 2018-12-24 stsp return got_error_from_errno();
431 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
434 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
437 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
440 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
444 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
447 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
450 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
454 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
458 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
459 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
460 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
463 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
465 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
466 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
467 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
470 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
472 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
476 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
479 52a74475 2018-12-24 stsp free(entry);
481 52a74475 2018-12-24 stsp *entryp = entry;
482 52a74475 2018-12-24 stsp return err;
485 52a74475 2018-12-24 stsp const struct got_error *
486 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
488 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
489 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
490 52a74475 2018-12-24 stsp SHA1_CTX ctx;
491 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
492 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
493 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
497 52a74475 2018-12-24 stsp SHA1Init(&ctx);
499 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
500 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
501 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
502 b6d05318 2019-01-13 stsp return NULL;
503 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
505 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
506 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
507 51514078 2018-12-25 stsp if (n == 0) /* EOF */
508 51514078 2018-12-25 stsp return NULL;
509 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
511 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
512 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
513 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
514 b6d05318 2019-01-13 stsp return NULL;
515 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
518 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
519 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
520 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
522 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
523 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
524 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
526 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
527 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
528 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
529 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
531 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
532 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
534 52a74475 2018-12-24 stsp return err;
535 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
537 52a74475 2018-12-24 stsp return err;
540 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
541 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
542 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
544 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
545 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
547 9d31a1d8 2018-03-11 stsp return NULL;
551 8da9e5f4 2019-01-12 stsp in_same_subdir(struct got_fileindex_entry *ie, const char *parent_path,
552 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te)
554 8da9e5f4 2019-01-12 stsp size_t parent_len = strlen(parent_path);
555 8da9e5f4 2019-01-12 stsp char *ie_name;
557 8da9e5f4 2019-01-12 stsp if (!got_path_is_child(ie->path, parent_path, parent_len))
560 8da9e5f4 2019-01-12 stsp ie_name = ie->path + parent_len;
561 8da9e5f4 2019-01-12 stsp while (ie_name[0] == '/')
564 50952927 2019-01-12 stsp return strchr(ie_name, '/') == NULL;
568 8da9e5f4 2019-01-12 stsp cmp_entries(struct got_fileindex_entry *ie, const char *parent_path,
569 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te)
571 8da9e5f4 2019-01-12 stsp size_t parent_len = strlen(parent_path);
572 8da9e5f4 2019-01-12 stsp char *ie_name;
574 8da9e5f4 2019-01-12 stsp if (!in_same_subdir(ie, parent_path, te)) {
575 8da9e5f4 2019-01-12 stsp if (parent_path[0])
576 50952927 2019-01-12 stsp return strcmp(ie->path, parent_path);
577 50952927 2019-01-12 stsp return strcmp(ie->path, te->name);
580 8da9e5f4 2019-01-12 stsp ie_name = ie->path + parent_len;
581 8da9e5f4 2019-01-12 stsp while (ie_name[0] == '/')
584 50952927 2019-01-12 stsp return strcmp(ie_name, te->name);
587 8da9e5f4 2019-01-12 stsp static const struct got_error *
588 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
589 8da9e5f4 2019-01-12 stsp struct got_tree_object *, const char *, struct got_repository *,
590 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *, void *);
592 50952927 2019-01-12 stsp struct got_fileindex_entry *
593 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
595 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
597 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
599 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
600 a7f9d64d 2019-01-13 stsp while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
601 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
603 50952927 2019-01-12 stsp return next;
606 8da9e5f4 2019-01-12 stsp static const struct got_error *
607 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
608 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
609 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
610 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
612 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
614 8da9e5f4 2019-01-12 stsp if (te && S_ISREG(te->mode)) {
615 8da9e5f4 2019-01-12 stsp *next = SIMPLEQ_NEXT(te, entry);
616 8da9e5f4 2019-01-12 stsp return NULL;
619 8da9e5f4 2019-01-12 stsp while (te && S_ISDIR(te->mode)) {
620 8da9e5f4 2019-01-12 stsp char *subpath;
621 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
623 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
624 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
625 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
627 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
629 8da9e5f4 2019-01-12 stsp free(subpath);
630 8da9e5f4 2019-01-12 stsp return err;
633 8da9e5f4 2019-01-12 stsp if (*ie == NULL || !in_same_subdir(*ie, path, te)) {
634 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
636 8da9e5f4 2019-01-12 stsp return err;
639 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
640 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
641 8da9e5f4 2019-01-12 stsp free(subpath);
642 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
644 8da9e5f4 2019-01-12 stsp return err;
645 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_NEXT(te, entry);
648 8da9e5f4 2019-01-12 stsp *next = te;
649 8da9e5f4 2019-01-12 stsp return NULL;
652 8da9e5f4 2019-01-12 stsp static const struct got_error *
653 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
654 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
655 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
656 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
658 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
659 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
660 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
661 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
662 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
664 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
665 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
667 8da9e5f4 2019-01-12 stsp if (te && *ie) {
668 8da9e5f4 2019-01-12 stsp int cmp = cmp_entries(*ie, path, te);
669 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
670 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
674 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
675 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
676 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
677 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
678 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
679 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
682 8da9e5f4 2019-01-12 stsp *ie = next;
684 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
687 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
688 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
692 8da9e5f4 2019-01-12 stsp } else if (*ie) {
693 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
694 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
697 8da9e5f4 2019-01-12 stsp *ie = next;
698 8da9e5f4 2019-01-12 stsp } else if (te) {
699 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
702 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
707 8da9e5f4 2019-01-12 stsp } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
709 8da9e5f4 2019-01-12 stsp return err;
712 8da9e5f4 2019-01-12 stsp const struct got_error *
713 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
714 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
715 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
717 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
718 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
719 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
722 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);