2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 c48c4a9c 2018-03-11 stsp #include <stdio.h>
23 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
24 c48c4a9c 2018-03-11 stsp #include <string.h>
25 c48c4a9c 2018-03-11 stsp #include <sha1.h>
26 c34b20a2 2018-03-12 stsp #include <endian.h>
27 133d2798 2019-01-08 stsp #include <limits.h>
29 c48c4a9c 2018-03-11 stsp #include "got_error.h"
30 8da9e5f4 2019-01-12 stsp #include "got_object.h"
32 133d2798 2019-01-08 stsp #include "got_lib_path.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
35 133d2798 2019-01-08 stsp struct got_fileindex {
36 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
37 133d2798 2019-01-08 stsp int nentries;
38 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
41 c48c4a9c 2018-03-11 stsp const struct got_error *
42 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
43 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
45 c48c4a9c 2018-03-11 stsp struct stat sb;
47 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
48 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
50 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
51 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
52 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
53 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
54 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
55 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
56 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
57 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
58 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
60 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
61 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
62 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
63 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
64 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
66 51514078 2018-12-25 stsp return NULL;
69 51514078 2018-12-25 stsp const struct got_error *
70 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
71 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
72 51514078 2018-12-25 stsp uint8_t *commit_sha1)
76 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
77 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
78 0a585a0d 2018-03-17 stsp return got_error_from_errno();
80 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
81 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
82 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
83 c48c4a9c 2018-03-11 stsp free(*entry);
84 c48c4a9c 2018-03-11 stsp *entry = NULL;
88 c34b20a2 2018-03-12 stsp len = strlen(relpath);
89 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
90 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
91 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
93 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
94 51514078 2018-12-25 stsp commit_sha1);
98 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
100 c48c4a9c 2018-03-11 stsp free(entry->path);
101 c48c4a9c 2018-03-11 stsp free(entry);
104 50952927 2019-01-12 stsp static const struct got_error *
105 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
107 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
108 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
110 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
111 133d2798 2019-01-08 stsp fileindex->nentries++;
112 133d2798 2019-01-08 stsp return NULL;
115 50952927 2019-01-12 stsp const struct got_error *
116 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
117 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
119 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
120 a7f9d64d 2019-01-13 stsp entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
122 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
126 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
127 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
129 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
130 133d2798 2019-01-08 stsp fileindex->nentries--;
133 51514078 2018-12-25 stsp struct got_fileindex_entry *
134 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
136 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
137 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
138 133d2798 2019-01-08 stsp key.path = (char *)path;
139 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
142 512f0d0e 2019-01-02 stsp const struct got_error *
143 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
144 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
146 133d2798 2019-01-08 stsp const struct got_error *err;
147 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
149 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
150 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
152 133d2798 2019-01-08 stsp return err;
154 b504a804 2019-01-08 stsp return NULL;
157 133d2798 2019-01-08 stsp struct got_fileindex *
158 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
160 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
162 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
163 133d2798 2019-01-08 stsp if (fileindex == NULL)
164 133d2798 2019-01-08 stsp return NULL;
166 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
167 133d2798 2019-01-08 stsp return fileindex;
171 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
173 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
175 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
176 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
177 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
179 9d31a1d8 2018-03-11 stsp free(fileindex);
182 c34b20a2 2018-03-12 stsp static const struct got_error *
183 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
187 c34b20a2 2018-03-12 stsp val = htobe64(val);
188 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
189 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
190 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
191 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
192 c34b20a2 2018-03-12 stsp return NULL;
195 c34b20a2 2018-03-12 stsp static const struct got_error *
196 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
200 c34b20a2 2018-03-12 stsp val = htobe32(val);
201 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
202 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
203 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
204 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
205 c34b20a2 2018-03-12 stsp return NULL;
208 c34b20a2 2018-03-12 stsp static const struct got_error *
209 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
213 c34b20a2 2018-03-12 stsp val = htobe16(val);
214 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
215 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
216 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
217 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
218 c34b20a2 2018-03-12 stsp return NULL;
221 c34b20a2 2018-03-12 stsp static const struct got_error *
222 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
224 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
225 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
227 c34b20a2 2018-03-12 stsp len = strlen(path);
228 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
230 3c5b70f2 2018-12-27 stsp if (pad == 0)
231 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
233 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
234 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
235 c34b20a2 2018-03-12 stsp if (n != len)
236 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
237 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
238 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
239 c34b20a2 2018-03-12 stsp if (n != pad)
240 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
241 c34b20a2 2018-03-12 stsp return NULL;
244 c34b20a2 2018-03-12 stsp static const struct got_error *
245 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
246 c34b20a2 2018-03-12 stsp FILE *outfile)
248 c34b20a2 2018-03-12 stsp const struct got_error *err;
251 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
253 c34b20a2 2018-03-12 stsp return err;
254 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
256 c34b20a2 2018-03-12 stsp return err;
257 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
259 c34b20a2 2018-03-12 stsp return err;
260 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
262 c34b20a2 2018-03-12 stsp return err;
264 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
266 c34b20a2 2018-03-12 stsp return err;
267 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
269 c34b20a2 2018-03-12 stsp return err;
270 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
272 c34b20a2 2018-03-12 stsp return err;
274 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
276 c34b20a2 2018-03-12 stsp return err;
278 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
279 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
280 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
281 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
283 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
284 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
285 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
286 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
288 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
290 c34b20a2 2018-03-12 stsp return err;
292 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
293 c34b20a2 2018-03-12 stsp return err;
296 9d31a1d8 2018-03-11 stsp const struct got_error *
297 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
299 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
300 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
301 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
302 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
304 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
306 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
308 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
309 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
310 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
312 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
313 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
314 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
315 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
316 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
317 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
318 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
319 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
320 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
321 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
322 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
323 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
325 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
326 a7f9d64d 2019-01-13 stsp entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
327 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
329 133d2798 2019-01-08 stsp return err;
332 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
333 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
334 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
335 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
337 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
338 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
340 52a74475 2018-12-24 stsp return NULL;
343 52a74475 2018-12-24 stsp static const struct got_error *
344 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
348 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
349 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
350 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
351 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
352 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
353 52a74475 2018-12-24 stsp return NULL;
356 52a74475 2018-12-24 stsp static const struct got_error *
357 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
361 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
362 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
363 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
364 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
365 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
366 52a74475 2018-12-24 stsp return NULL;
369 52a74475 2018-12-24 stsp static const struct got_error *
370 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
374 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
375 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
376 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
377 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
378 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
379 52a74475 2018-12-24 stsp return NULL;
382 52a74475 2018-12-24 stsp static const struct got_error *
383 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
385 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
386 52a74475 2018-12-24 stsp uint8_t buf[8];
387 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
389 52a74475 2018-12-24 stsp *path = malloc(totlen);
390 52a74475 2018-12-24 stsp if (*path == NULL)
391 52a74475 2018-12-24 stsp return got_error_from_errno();
394 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
395 52a74475 2018-12-24 stsp if (n != sizeof(buf))
396 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
397 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
398 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
399 52a74475 2018-12-24 stsp if (p == NULL) {
400 52a74475 2018-12-24 stsp err = got_error_from_errno();
403 52a74475 2018-12-24 stsp totlen += sizeof(buf);
406 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
407 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
408 52a74475 2018-12-24 stsp len += sizeof(buf);
409 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
412 52a74475 2018-12-24 stsp free(*path);
413 52a74475 2018-12-24 stsp *path = NULL;
415 52a74475 2018-12-24 stsp return err;
418 52a74475 2018-12-24 stsp static const struct got_error *
419 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
420 52a74475 2018-12-24 stsp FILE *infile)
422 52a74475 2018-12-24 stsp const struct got_error *err;
423 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
426 52a74475 2018-12-24 stsp *entryp = NULL;
428 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
429 52a74475 2018-12-24 stsp if (entry == NULL)
430 52a74475 2018-12-24 stsp return got_error_from_errno();
432 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
435 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
438 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
441 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
445 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
448 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
451 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
455 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
459 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
460 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
461 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
464 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
466 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
467 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
468 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
471 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
473 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
477 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
480 52a74475 2018-12-24 stsp free(entry);
482 52a74475 2018-12-24 stsp *entryp = entry;
483 52a74475 2018-12-24 stsp return err;
486 52a74475 2018-12-24 stsp const struct got_error *
487 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
489 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
490 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
491 52a74475 2018-12-24 stsp SHA1_CTX ctx;
492 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
493 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
494 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
498 52a74475 2018-12-24 stsp SHA1Init(&ctx);
500 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
501 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
502 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
503 b6d05318 2019-01-13 stsp return NULL;
504 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
506 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
507 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
508 51514078 2018-12-25 stsp if (n == 0) /* EOF */
509 51514078 2018-12-25 stsp return NULL;
510 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
512 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
513 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
514 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
515 b6d05318 2019-01-13 stsp return NULL;
516 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
519 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
520 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
521 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
523 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
524 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
525 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
527 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
528 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
529 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
530 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
532 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
533 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
535 52a74475 2018-12-24 stsp return err;
536 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
538 52a74475 2018-12-24 stsp return err;
541 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
542 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
543 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
544 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
545 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
546 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
548 9d31a1d8 2018-03-11 stsp return NULL;
551 50952927 2019-01-12 stsp struct got_fileindex_entry *
552 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
554 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
556 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
558 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
559 a7f9d64d 2019-01-13 stsp while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
560 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
562 50952927 2019-01-12 stsp return next;
565 8da9e5f4 2019-01-12 stsp static const struct got_error *
566 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
567 9d2a8e53 2019-01-28 stsp struct got_tree_object *, const char *, struct got_repository *,
568 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *, void *);
570 9d2a8e53 2019-01-28 stsp static const struct got_error *
571 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
572 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
573 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
574 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
576 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
578 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
579 8da9e5f4 2019-01-12 stsp char *subpath;
580 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
582 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
583 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
584 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
586 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
588 8da9e5f4 2019-01-12 stsp free(subpath);
589 8da9e5f4 2019-01-12 stsp return err;
592 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
593 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
594 8da9e5f4 2019-01-12 stsp free(subpath);
595 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
597 8da9e5f4 2019-01-12 stsp return err;
600 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
601 8da9e5f4 2019-01-12 stsp return NULL;
605 bad322f2 2019-01-28 stsp * Decide whether a fileindex entry path is equivalent to a tree entry path,
606 bad322f2 2019-01-28 stsp * and if it is not, then decide which of the two should be processed first.
609 bad322f2 2019-01-28 stsp cmp_entries(const char *ie_path, const char *parent_path,
610 bad322f2 2019-01-28 stsp size_t parent_len, const char *te_name)
612 bad322f2 2019-01-28 stsp int cmp = strncmp(ie_path, parent_path, parent_len);
613 62d20534 2019-01-28 stsp if (cmp == 0) {
614 bad322f2 2019-01-28 stsp const char *ie_name = ie_path + parent_len;
615 70e2377c 2019-01-28 stsp while (ie_name[0] == '/')
617 bad322f2 2019-01-28 stsp cmp = strcmp(ie_name, te_name);
619 70e2377c 2019-01-28 stsp return cmp;
623 8da9e5f4 2019-01-12 stsp static const struct got_error *
624 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
625 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
626 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
627 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
629 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
630 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
631 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
632 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
633 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
635 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
636 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
638 8da9e5f4 2019-01-12 stsp if (te && *ie) {
639 bad322f2 2019-01-28 stsp int cmp = cmp_entries((*ie)->path, path, path_len,
641 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
642 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
646 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
647 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
648 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
649 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
650 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
651 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
654 8da9e5f4 2019-01-12 stsp *ie = next;
656 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
659 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
660 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
664 8da9e5f4 2019-01-12 stsp } else if (*ie) {
665 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
666 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
669 8da9e5f4 2019-01-12 stsp *ie = next;
670 8da9e5f4 2019-01-12 stsp } else if (te) {
671 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
674 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
679 8da9e5f4 2019-01-12 stsp } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
681 8da9e5f4 2019-01-12 stsp return err;
684 8da9e5f4 2019-01-12 stsp const struct got_error *
685 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
686 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
687 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
689 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
690 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
691 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
694 d1f6d47b 2019-02-04 stsp static const struct got_error *
695 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
696 d1f6d47b 2019-02-04 stsp const char *, struct got_repository *, struct got_fileindex_diff_dir_cb *,
699 d1f6d47b 2019-02-04 stsp static const struct got_error *
700 d1f6d47b 2019-02-04 stsp walk_dir(struct dirent **next, struct got_fileindex *fileindex,
701 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry **ie, struct dirent *de, const char *path,
702 d1f6d47b 2019-02-04 stsp DIR *dir, struct got_repository *repo,
703 d1f6d47b 2019-02-04 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
705 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
707 d1f6d47b 2019-02-04 stsp if (de->d_type == DT_DIR) {
708 d1f6d47b 2019-02-04 stsp char *subpath;
709 d1f6d47b 2019-02-04 stsp DIR *subdir;
711 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
712 d1f6d47b 2019-02-04 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
713 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
715 d1f6d47b 2019-02-04 stsp subdir = opendir(subpath);
716 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
717 d1f6d47b 2019-02-04 stsp free(subpath);
718 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
721 d1f6d47b 2019-02-04 stsp err = diff_fileindex_dir(fileindex, ie, subdir, subpath, repo,
722 d1f6d47b 2019-02-04 stsp cb, cb_arg);
723 d1f6d47b 2019-02-04 stsp free(subpath);
724 d1f6d47b 2019-02-04 stsp closedir(subdir);
726 d1f6d47b 2019-02-04 stsp return err;
729 d1f6d47b 2019-02-04 stsp *next = readdir(dir);
730 d1f6d47b 2019-02-04 stsp return NULL;
733 d1f6d47b 2019-02-04 stsp static const struct got_error *
734 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
735 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry **ie, DIR *dir, const char *path,
736 d1f6d47b 2019-02-04 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
737 d1f6d47b 2019-02-04 stsp void *cb_arg)
739 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
740 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
741 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
742 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
744 d1f6d47b 2019-02-04 stsp de = readdir(dir);
746 d1f6d47b 2019-02-04 stsp if (de && *ie) {
747 d1f6d47b 2019-02-04 stsp int cmp = cmp_entries((*ie)->path, path, path_len,
748 d1f6d47b 2019-02-04 stsp de->d_name);
749 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
750 d1f6d47b 2019-02-04 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
753 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
754 d1f6d47b 2019-02-04 stsp err = walk_dir(&de, fileindex, ie, de, path,
755 d1f6d47b 2019-02-04 stsp dir, repo, cb, cb_arg);
756 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
757 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
758 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
761 d1f6d47b 2019-02-04 stsp *ie = next;
763 d1f6d47b 2019-02-04 stsp err = cb->diff_new(cb_arg, de, path);
766 d1f6d47b 2019-02-04 stsp err = walk_dir(&de, fileindex, ie, de, path,
767 d1f6d47b 2019-02-04 stsp dir, repo, cb, cb_arg);
772 d1f6d47b 2019-02-04 stsp } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || de);
774 d1f6d47b 2019-02-04 stsp return err;
777 d1f6d47b 2019-02-04 stsp const struct got_error *
778 d1f6d47b 2019-02-04 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *dir,
779 d1f6d47b 2019-02-04 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
780 d1f6d47b 2019-02-04 stsp void *cb_arg)
782 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
783 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
784 d1f6d47b 2019-02-04 stsp return diff_fileindex_dir(fileindex, &min, dir, "", repo, cb, cb_arg);
787 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);