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>
28 c442a90d 2019-03-10 stsp #include <uuid.h>
30 c48c4a9c 2018-03-11 stsp #include "got_error.h"
31 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 133d2798 2019-01-08 stsp #include "got_lib_path.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
35 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
37 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
38 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_STAGE 0x00003000
40 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_EXTENDED 0x00004000
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
42 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 133d2798 2019-01-08 stsp struct got_fileindex {
47 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
48 133d2798 2019-01-08 stsp int nentries;
49 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
52 c48c4a9c 2018-03-11 stsp const struct got_error *
53 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
54 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
55 02c07007 2019-02-10 stsp int update_timestamps)
57 c48c4a9c 2018-03-11 stsp struct stat sb;
59 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
60 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
62 02c07007 2019-02-10 stsp if (update_timestamps) {
63 02c07007 2019-02-10 stsp entry->ctime_sec = sb.st_ctime;
64 02c07007 2019-02-10 stsp entry->ctime_nsec = sb.st_ctimensec;
65 02c07007 2019-02-10 stsp entry->mtime_sec = sb.st_mtime;
66 02c07007 2019-02-10 stsp entry->mtime_nsec = sb.st_mtimensec;
68 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
69 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
70 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
71 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
72 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
74 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
75 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
76 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
78 ddce0520 2019-03-26 stsp if (blob_sha1) {
79 ddce0520 2019-03-26 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
80 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
82 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_BLOB;
84 ddce0520 2019-03-26 stsp if (commit_sha1) {
85 ddce0520 2019-03-26 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
86 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
88 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
90 51514078 2018-12-25 stsp return NULL;
93 51514078 2018-12-25 stsp const struct got_error *
94 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
95 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
96 51514078 2018-12-25 stsp uint8_t *commit_sha1)
100 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
101 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
102 0a585a0d 2018-03-17 stsp return got_error_from_errno();
104 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
105 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
106 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
107 c48c4a9c 2018-03-11 stsp free(*entry);
108 c48c4a9c 2018-03-11 stsp *entry = NULL;
109 0a585a0d 2018-03-17 stsp return err;
112 c34b20a2 2018-03-12 stsp len = strlen(relpath);
113 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
114 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
115 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
117 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
118 02c07007 2019-02-10 stsp commit_sha1, 1);
122 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
124 c48c4a9c 2018-03-11 stsp free(entry->path);
125 c48c4a9c 2018-03-11 stsp free(entry);
129 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
131 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
135 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
137 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
140 50952927 2019-01-12 stsp static const struct got_error *
141 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
143 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
144 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
146 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
147 133d2798 2019-01-08 stsp fileindex->nentries++;
148 133d2798 2019-01-08 stsp return NULL;
151 50952927 2019-01-12 stsp const struct got_error *
152 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
153 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
155 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
156 e288864f 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
158 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
162 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
163 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
165 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 133d2798 2019-01-08 stsp fileindex->nentries--;
169 51514078 2018-12-25 stsp struct got_fileindex_entry *
170 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
172 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
173 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
174 133d2798 2019-01-08 stsp key.path = (char *)path;
175 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
178 512f0d0e 2019-01-02 stsp const struct got_error *
179 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
180 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
182 133d2798 2019-01-08 stsp const struct got_error *err;
183 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
185 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
186 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
188 133d2798 2019-01-08 stsp return err;
190 b504a804 2019-01-08 stsp return NULL;
193 133d2798 2019-01-08 stsp struct got_fileindex *
194 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
196 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
198 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
199 133d2798 2019-01-08 stsp if (fileindex == NULL)
200 133d2798 2019-01-08 stsp return NULL;
202 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
203 133d2798 2019-01-08 stsp return fileindex;
207 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
209 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
211 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
212 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
213 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
215 9d31a1d8 2018-03-11 stsp free(fileindex);
218 c34b20a2 2018-03-12 stsp static const struct got_error *
219 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
223 c34b20a2 2018-03-12 stsp val = htobe64(val);
224 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
225 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
226 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
227 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
228 c34b20a2 2018-03-12 stsp return NULL;
231 c34b20a2 2018-03-12 stsp static const struct got_error *
232 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
236 c34b20a2 2018-03-12 stsp val = htobe32(val);
237 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
238 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
239 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
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_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
249 c34b20a2 2018-03-12 stsp val = htobe16(val);
250 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
251 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
252 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
253 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
254 c34b20a2 2018-03-12 stsp return NULL;
257 c34b20a2 2018-03-12 stsp static const struct got_error *
258 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
260 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
261 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
263 c34b20a2 2018-03-12 stsp len = strlen(path);
264 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
266 3c5b70f2 2018-12-27 stsp if (pad == 0)
267 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
269 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
270 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
271 c34b20a2 2018-03-12 stsp if (n != len)
272 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
273 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
274 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
275 c34b20a2 2018-03-12 stsp if (n != pad)
276 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp return NULL;
280 c34b20a2 2018-03-12 stsp static const struct got_error *
281 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
282 c34b20a2 2018-03-12 stsp FILE *outfile)
284 c34b20a2 2018-03-12 stsp const struct got_error *err;
287 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
289 c34b20a2 2018-03-12 stsp return err;
290 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
292 c34b20a2 2018-03-12 stsp return err;
293 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
295 c34b20a2 2018-03-12 stsp return err;
296 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
298 c34b20a2 2018-03-12 stsp return err;
300 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
302 c34b20a2 2018-03-12 stsp return err;
303 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
305 c34b20a2 2018-03-12 stsp return err;
306 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
308 c34b20a2 2018-03-12 stsp return err;
310 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
312 c34b20a2 2018-03-12 stsp return err;
314 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
315 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
316 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
317 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
319 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
320 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
321 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
322 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
324 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
326 c34b20a2 2018-03-12 stsp return err;
328 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
329 c34b20a2 2018-03-12 stsp return err;
332 9d31a1d8 2018-03-11 stsp const struct got_error *
333 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
335 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
336 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
337 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
338 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
340 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
342 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
344 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
345 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
346 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
348 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
349 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
350 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
351 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
352 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
353 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
354 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
355 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
356 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
357 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
358 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
359 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
361 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
362 e288864f 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
363 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
365 133d2798 2019-01-08 stsp return err;
368 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
369 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
370 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
371 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
373 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
374 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
376 52a74475 2018-12-24 stsp return NULL;
379 52a74475 2018-12-24 stsp static const struct got_error *
380 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
384 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
385 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
386 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
387 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
388 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
389 52a74475 2018-12-24 stsp return NULL;
392 52a74475 2018-12-24 stsp static const struct got_error *
393 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
397 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
398 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
399 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
400 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
401 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
402 52a74475 2018-12-24 stsp return NULL;
405 52a74475 2018-12-24 stsp static const struct got_error *
406 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
410 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
411 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
412 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
413 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
414 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
415 52a74475 2018-12-24 stsp return NULL;
418 52a74475 2018-12-24 stsp static const struct got_error *
419 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
421 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
422 52a74475 2018-12-24 stsp uint8_t buf[8];
423 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
425 52a74475 2018-12-24 stsp *path = malloc(totlen);
426 52a74475 2018-12-24 stsp if (*path == NULL)
427 52a74475 2018-12-24 stsp return got_error_from_errno();
430 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
431 52a74475 2018-12-24 stsp if (n != sizeof(buf))
432 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
433 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
434 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
435 52a74475 2018-12-24 stsp if (p == NULL) {
436 52a74475 2018-12-24 stsp err = got_error_from_errno();
439 52a74475 2018-12-24 stsp totlen += sizeof(buf);
442 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
443 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
444 52a74475 2018-12-24 stsp len += sizeof(buf);
445 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
448 52a74475 2018-12-24 stsp free(*path);
449 52a74475 2018-12-24 stsp *path = NULL;
451 52a74475 2018-12-24 stsp return err;
454 52a74475 2018-12-24 stsp static const struct got_error *
455 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
456 52a74475 2018-12-24 stsp FILE *infile)
458 52a74475 2018-12-24 stsp const struct got_error *err;
459 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
462 52a74475 2018-12-24 stsp *entryp = NULL;
464 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
465 52a74475 2018-12-24 stsp if (entry == NULL)
466 52a74475 2018-12-24 stsp return got_error_from_errno();
468 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
471 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
474 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
477 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
481 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
484 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
487 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
491 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
495 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
496 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
497 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
500 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
502 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
503 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
504 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
509 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
513 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
516 52a74475 2018-12-24 stsp free(entry);
518 52a74475 2018-12-24 stsp *entryp = entry;
519 52a74475 2018-12-24 stsp return err;
522 52a74475 2018-12-24 stsp const struct got_error *
523 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
525 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
526 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
527 52a74475 2018-12-24 stsp SHA1_CTX ctx;
528 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
529 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
530 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
534 52a74475 2018-12-24 stsp SHA1Init(&ctx);
536 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
537 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
538 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
539 b6d05318 2019-01-13 stsp return NULL;
540 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
543 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
544 51514078 2018-12-25 stsp if (n == 0) /* EOF */
545 51514078 2018-12-25 stsp return NULL;
546 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
549 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
550 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
551 b6d05318 2019-01-13 stsp return NULL;
552 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
556 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
557 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
559 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
560 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
561 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
563 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
564 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
565 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
566 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
568 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
569 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
571 52a74475 2018-12-24 stsp return err;
572 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
574 52a74475 2018-12-24 stsp return err;
577 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
578 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
579 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
581 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
582 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
584 9d31a1d8 2018-03-11 stsp return NULL;
587 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
588 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
590 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
592 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
594 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
595 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
596 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
598 50952927 2019-01-12 stsp return next;
601 8da9e5f4 2019-01-12 stsp static const struct got_error *
602 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
603 9d2a8e53 2019-01-28 stsp struct got_tree_object *, const char *, struct got_repository *,
604 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *, void *);
606 9d2a8e53 2019-01-28 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 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
612 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
614 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
615 8da9e5f4 2019-01-12 stsp char *subpath;
616 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
618 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
619 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
620 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
622 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
624 8da9e5f4 2019-01-12 stsp free(subpath);
625 8da9e5f4 2019-01-12 stsp return err;
628 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
629 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
630 8da9e5f4 2019-01-12 stsp free(subpath);
631 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
633 8da9e5f4 2019-01-12 stsp return err;
636 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
637 8da9e5f4 2019-01-12 stsp return NULL;
640 8da9e5f4 2019-01-12 stsp static const struct got_error *
641 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
642 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
643 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
644 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
646 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
647 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
648 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
649 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
650 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
652 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
653 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
654 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
655 8da9e5f4 2019-01-12 stsp if (te && *ie) {
656 18831e78 2019-02-10 stsp char *te_path;
658 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
659 18831e78 2019-02-10 stsp err = got_error_from_errno();
662 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
663 18831e78 2019-02-10 stsp free(te_path);
664 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
665 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
669 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
670 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
671 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
672 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
673 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
674 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
677 8da9e5f4 2019-01-12 stsp *ie = next;
679 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
682 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
683 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
687 8da9e5f4 2019-01-12 stsp } else if (*ie) {
688 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
689 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
692 8da9e5f4 2019-01-12 stsp *ie = next;
693 8da9e5f4 2019-01-12 stsp } else if (te) {
694 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
697 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
704 8da9e5f4 2019-01-12 stsp return err;
707 8da9e5f4 2019-01-12 stsp const struct got_error *
708 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
709 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
710 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
712 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
713 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
714 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
717 d1f6d47b 2019-02-04 stsp static const struct got_error *
718 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
719 c7f4312f 2019-02-05 stsp const char *, const char *, struct got_repository *,
720 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *, void *);
722 d1f6d47b 2019-02-04 stsp static const struct got_error *
723 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
724 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
725 c7f4312f 2019-02-05 stsp const char *path, DIR *dir, const char *rootpath,
726 c7f4312f 2019-02-05 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
727 c7f4312f 2019-02-05 stsp void *cb_arg)
729 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
730 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
732 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
733 d1f6d47b 2019-02-04 stsp char *subpath;
734 c7f4312f 2019-02-05 stsp char *subdirpath;
735 d1f6d47b 2019-02-04 stsp DIR *subdir;
737 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
738 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
739 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
741 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
742 c7f4312f 2019-02-05 stsp free(subpath);
743 c7f4312f 2019-02-05 stsp return got_error_from_errno();
746 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
747 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
748 d1f6d47b 2019-02-04 stsp free(subpath);
749 c7f4312f 2019-02-05 stsp free(subdirpath);
750 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
753 c7f4312f 2019-02-05 stsp err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
754 c7f4312f 2019-02-05 stsp subpath, repo, cb, cb_arg);
755 d1f6d47b 2019-02-04 stsp free(subpath);
756 c7f4312f 2019-02-05 stsp free(subdirpath);
757 d1f6d47b 2019-02-04 stsp closedir(subdir);
759 d1f6d47b 2019-02-04 stsp return err;
762 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
763 d1f6d47b 2019-02-04 stsp return NULL;
766 d1f6d47b 2019-02-04 stsp static const struct got_error *
767 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
768 c7f4312f 2019-02-05 stsp struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
769 c7f4312f 2019-02-05 stsp const char *path, struct got_repository *repo,
770 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
772 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
773 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
774 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
775 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
776 f5d3d7af 2019-02-05 stsp struct got_pathlist_head dirlist;
777 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
779 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&dirlist);
781 500cd40f 2019-02-04 stsp while (1) {
782 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
783 ed83bff7 2019-02-05 stsp struct dirent *dep = NULL;
785 ed83bff7 2019-02-05 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
786 ed83bff7 2019-02-05 stsp if (de == NULL) {
787 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
791 ed83bff7 2019-02-05 stsp if (readdir_r(dir, de, &dep) != 0) {
792 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
796 ed83bff7 2019-02-05 stsp if (dep == NULL) {
801 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
802 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
803 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
804 ed83bff7 2019-02-05 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
809 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
812 f5d3d7af 2019-02-05 stsp if (new == NULL) {
813 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
818 f5d3d7af 2019-02-05 stsp dle = TAILQ_FIRST(&dirlist);
819 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
820 500cd40f 2019-02-04 stsp if (dle && *ie) {
821 18831e78 2019-02-10 stsp char *de_path;
823 f5d3d7af 2019-02-05 stsp de = dle->data;
824 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
825 18831e78 2019-02-10 stsp de->d_name) == -1) {
826 18831e78 2019-02-10 stsp err = got_error_from_errno();
829 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
830 18831e78 2019-02-10 stsp free(de_path);
831 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
832 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
835 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
836 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
837 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
838 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
839 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
840 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
843 d1f6d47b 2019-02-04 stsp *ie = next;
845 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
848 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
849 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
853 554b91b1 2019-02-04 stsp } else if (*ie) {
854 554b91b1 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
855 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
858 554b91b1 2019-02-04 stsp *ie = next;
859 554b91b1 2019-02-04 stsp } else if (dle) {
860 763e1377 2019-02-05 stsp de = dle->data;
861 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
864 554b91b1 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path, dir,
865 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
871 ed83bff7 2019-02-05 stsp TAILQ_FOREACH(dle, &dirlist, entry)
872 ed83bff7 2019-02-05 stsp free(dle->data);
873 f5d3d7af 2019-02-05 stsp got_pathlist_free(&dirlist);
874 d1f6d47b 2019-02-04 stsp return err;
877 d1f6d47b 2019-02-04 stsp const struct got_error *
878 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
879 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
880 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
882 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
883 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
884 927df6b7 2019-02-10 stsp return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
885 c7f4312f 2019-02-05 stsp repo, cb, cb_arg);
888 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);