Blame


1 c48c4a9c 2018-03-11 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 c48c4a9c 2018-03-11 stsp *
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 c48c4a9c 2018-03-11 stsp *
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c48c4a9c 2018-03-11 stsp */
16 c48c4a9c 2018-03-11 stsp
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
20 c48c4a9c 2018-03-11 stsp
21 03df25b3 2019-05-11 stsp #include <errno.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 c48c4a9c 2018-03-11 stsp #include <stdio.h>
24 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
25 c48c4a9c 2018-03-11 stsp #include <string.h>
26 c48c4a9c 2018-03-11 stsp #include <sha1.h>
27 c34b20a2 2018-03-12 stsp #include <endian.h>
28 133d2798 2019-01-08 stsp #include <limits.h>
29 c442a90d 2019-03-10 stsp #include <uuid.h>
30 c48c4a9c 2018-03-11 stsp
31 c48c4a9c 2018-03-11 stsp #include "got_error.h"
32 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
34 c48c4a9c 2018-03-11 stsp
35 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
36 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
37 c48c4a9c 2018-03-11 stsp
38 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_STAGE 0x00003000
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_EXTENDED 0x00004000
42 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 eb983b4b 2019-03-26 stsp
48 133d2798 2019-01-08 stsp struct got_fileindex {
49 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
50 133d2798 2019-01-08 stsp int nentries;
51 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
52 133d2798 2019-01-08 stsp };
53 133d2798 2019-01-08 stsp
54 26a7fe28 2019-07-27 stsp uint16_t
55 26a7fe28 2019-07-27 stsp got_fileindex_perms_from_st(struct stat *sb)
56 26a7fe28 2019-07-27 stsp {
57 26a7fe28 2019-07-27 stsp uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
58 26a7fe28 2019-07-27 stsp return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
59 26a7fe28 2019-07-27 stsp }
60 26a7fe28 2019-07-27 stsp
61 26a7fe28 2019-07-27 stsp mode_t
62 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
63 26a7fe28 2019-07-27 stsp {
64 26a7fe28 2019-07-27 stsp mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
65 26a7fe28 2019-07-27 stsp return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
66 26a7fe28 2019-07-27 stsp }
67 26a7fe28 2019-07-27 stsp
68 c48c4a9c 2018-03-11 stsp const struct got_error *
69 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
70 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
71 02c07007 2019-02-10 stsp int update_timestamps)
72 c48c4a9c 2018-03-11 stsp {
73 c48c4a9c 2018-03-11 stsp struct stat sb;
74 c48c4a9c 2018-03-11 stsp
75 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
76 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
77 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
78 03df25b3 2019-05-11 stsp } else {
79 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
80 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
81 13d9040b 2019-03-27 stsp entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
82 03df25b3 2019-05-11 stsp }
83 c48c4a9c 2018-03-11 stsp
84 03df25b3 2019-05-11 stsp
85 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
86 13d9040b 2019-03-27 stsp if (update_timestamps) {
87 13d9040b 2019-03-27 stsp entry->ctime_sec = sb.st_ctime;
88 13d9040b 2019-03-27 stsp entry->ctime_nsec = sb.st_ctimensec;
89 13d9040b 2019-03-27 stsp entry->mtime_sec = sb.st_mtime;
90 13d9040b 2019-03-27 stsp entry->mtime_nsec = sb.st_mtimensec;
91 13d9040b 2019-03-27 stsp }
92 13d9040b 2019-03-27 stsp entry->uid = sb.st_uid;
93 13d9040b 2019-03-27 stsp entry->gid = sb.st_gid;
94 13d9040b 2019-03-27 stsp entry->size = (sb.st_size & 0xffffffff);
95 13d9040b 2019-03-27 stsp if (sb.st_mode & S_IFLNK)
96 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
97 13d9040b 2019-03-27 stsp else
98 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
99 26a7fe28 2019-07-27 stsp entry->mode |= got_fileindex_perms_from_st(&sb);
100 02c07007 2019-02-10 stsp }
101 ddce0520 2019-03-26 stsp
102 ddce0520 2019-03-26 stsp if (blob_sha1) {
103 ddce0520 2019-03-26 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
104 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
105 ddce0520 2019-03-26 stsp } else
106 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_BLOB;
107 ddce0520 2019-03-26 stsp
108 ddce0520 2019-03-26 stsp if (commit_sha1) {
109 ddce0520 2019-03-26 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
110 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
111 ddce0520 2019-03-26 stsp } else
112 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
113 51514078 2018-12-25 stsp
114 51514078 2018-12-25 stsp return NULL;
115 51514078 2018-12-25 stsp }
116 51514078 2018-12-25 stsp
117 2ec1f75b 2019-03-26 stsp void
118 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
119 2ec1f75b 2019-03-26 stsp {
120 2ec1f75b 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
121 2ec1f75b 2019-03-26 stsp }
122 2ec1f75b 2019-03-26 stsp
123 51514078 2018-12-25 stsp const struct got_error *
124 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
125 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
126 51514078 2018-12-25 stsp uint8_t *commit_sha1)
127 51514078 2018-12-25 stsp {
128 51514078 2018-12-25 stsp size_t len;
129 51514078 2018-12-25 stsp
130 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
131 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
132 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
133 c48c4a9c 2018-03-11 stsp
134 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
135 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
136 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
137 c48c4a9c 2018-03-11 stsp free(*entry);
138 c48c4a9c 2018-03-11 stsp *entry = NULL;
139 0a585a0d 2018-03-17 stsp return err;
140 c48c4a9c 2018-03-11 stsp }
141 51514078 2018-12-25 stsp
142 c34b20a2 2018-03-12 stsp len = strlen(relpath);
143 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
144 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
145 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
146 c48c4a9c 2018-03-11 stsp
147 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
148 02c07007 2019-02-10 stsp commit_sha1, 1);
149 c48c4a9c 2018-03-11 stsp }
150 c48c4a9c 2018-03-11 stsp
151 c48c4a9c 2018-03-11 stsp void
152 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
153 c48c4a9c 2018-03-11 stsp {
154 c48c4a9c 2018-03-11 stsp free(entry->path);
155 c48c4a9c 2018-03-11 stsp free(entry);
156 d00136be 2019-03-26 stsp }
157 d00136be 2019-03-26 stsp
158 d00136be 2019-03-26 stsp int
159 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
160 d00136be 2019-03-26 stsp {
161 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
162 d00136be 2019-03-26 stsp }
163 d00136be 2019-03-26 stsp
164 d00136be 2019-03-26 stsp int
165 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
166 d00136be 2019-03-26 stsp {
167 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
168 c48c4a9c 2018-03-11 stsp }
169 9d31a1d8 2018-03-11 stsp
170 2ec1f75b 2019-03-26 stsp int
171 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
172 2ec1f75b 2019-03-26 stsp {
173 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
174 2ec1f75b 2019-03-26 stsp }
175 2ec1f75b 2019-03-26 stsp
176 50952927 2019-01-12 stsp static const struct got_error *
177 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
178 9d31a1d8 2018-03-11 stsp {
179 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
180 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
181 133d2798 2019-01-08 stsp
182 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
183 133d2798 2019-01-08 stsp fileindex->nentries++;
184 133d2798 2019-01-08 stsp return NULL;
185 50952927 2019-01-12 stsp }
186 50952927 2019-01-12 stsp
187 50952927 2019-01-12 stsp const struct got_error *
188 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
189 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
190 50952927 2019-01-12 stsp {
191 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
192 e288864f 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
193 50952927 2019-01-12 stsp
194 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
195 9d31a1d8 2018-03-11 stsp }
196 9d31a1d8 2018-03-11 stsp
197 133d2798 2019-01-08 stsp void
198 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
199 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
200 512f0d0e 2019-01-02 stsp {
201 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
202 133d2798 2019-01-08 stsp fileindex->nentries--;
203 512f0d0e 2019-01-02 stsp }
204 512f0d0e 2019-01-02 stsp
205 51514078 2018-12-25 stsp struct got_fileindex_entry *
206 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
207 51514078 2018-12-25 stsp {
208 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
209 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
210 133d2798 2019-01-08 stsp key.path = (char *)path;
211 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
212 b504a804 2019-01-08 stsp }
213 51514078 2018-12-25 stsp
214 512f0d0e 2019-01-02 stsp const struct got_error *
215 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
216 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
217 512f0d0e 2019-01-02 stsp {
218 133d2798 2019-01-08 stsp const struct got_error *err;
219 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
220 9d31a1d8 2018-03-11 stsp
221 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
222 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
223 133d2798 2019-01-08 stsp if (err)
224 133d2798 2019-01-08 stsp return err;
225 b504a804 2019-01-08 stsp }
226 b504a804 2019-01-08 stsp return NULL;
227 9d31a1d8 2018-03-11 stsp }
228 9d31a1d8 2018-03-11 stsp
229 133d2798 2019-01-08 stsp struct got_fileindex *
230 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
231 6b798c3c 2019-01-08 stsp {
232 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
233 133d2798 2019-01-08 stsp
234 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
235 133d2798 2019-01-08 stsp if (fileindex == NULL)
236 133d2798 2019-01-08 stsp return NULL;
237 133d2798 2019-01-08 stsp
238 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
239 133d2798 2019-01-08 stsp return fileindex;
240 6b798c3c 2019-01-08 stsp }
241 6b798c3c 2019-01-08 stsp
242 9d31a1d8 2018-03-11 stsp void
243 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
244 9d31a1d8 2018-03-11 stsp {
245 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
246 133d2798 2019-01-08 stsp
247 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
248 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
249 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
250 133d2798 2019-01-08 stsp }
251 9d31a1d8 2018-03-11 stsp free(fileindex);
252 9d31a1d8 2018-03-11 stsp }
253 9d31a1d8 2018-03-11 stsp
254 c34b20a2 2018-03-12 stsp static const struct got_error *
255 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
256 c34b20a2 2018-03-12 stsp {
257 c34b20a2 2018-03-12 stsp size_t n;
258 c34b20a2 2018-03-12 stsp
259 c34b20a2 2018-03-12 stsp val = htobe64(val);
260 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
261 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
262 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
263 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
264 c34b20a2 2018-03-12 stsp return NULL;
265 c34b20a2 2018-03-12 stsp }
266 c34b20a2 2018-03-12 stsp
267 c34b20a2 2018-03-12 stsp static const struct got_error *
268 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
269 c34b20a2 2018-03-12 stsp {
270 c34b20a2 2018-03-12 stsp size_t n;
271 c34b20a2 2018-03-12 stsp
272 c34b20a2 2018-03-12 stsp val = htobe32(val);
273 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
274 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
275 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
276 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp return NULL;
278 c34b20a2 2018-03-12 stsp }
279 c34b20a2 2018-03-12 stsp
280 c34b20a2 2018-03-12 stsp static const struct got_error *
281 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
282 c34b20a2 2018-03-12 stsp {
283 c34b20a2 2018-03-12 stsp size_t n;
284 c34b20a2 2018-03-12 stsp
285 c34b20a2 2018-03-12 stsp val = htobe16(val);
286 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
287 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
288 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
289 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
290 c34b20a2 2018-03-12 stsp return NULL;
291 c34b20a2 2018-03-12 stsp }
292 c34b20a2 2018-03-12 stsp
293 c34b20a2 2018-03-12 stsp static const struct got_error *
294 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
295 c34b20a2 2018-03-12 stsp {
296 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
297 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
298 c34b20a2 2018-03-12 stsp
299 c34b20a2 2018-03-12 stsp len = strlen(path);
300 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
301 3c5b70f2 2018-12-27 stsp pad++;
302 3c5b70f2 2018-12-27 stsp if (pad == 0)
303 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
304 c34b20a2 2018-03-12 stsp
305 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
306 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
307 c34b20a2 2018-03-12 stsp if (n != len)
308 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
309 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
310 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
311 c34b20a2 2018-03-12 stsp if (n != pad)
312 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
313 c34b20a2 2018-03-12 stsp return NULL;
314 c34b20a2 2018-03-12 stsp }
315 c34b20a2 2018-03-12 stsp
316 c34b20a2 2018-03-12 stsp static const struct got_error *
317 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
318 c34b20a2 2018-03-12 stsp FILE *outfile)
319 c34b20a2 2018-03-12 stsp {
320 c34b20a2 2018-03-12 stsp const struct got_error *err;
321 23b19d00 2018-03-12 stsp size_t n;
322 c34b20a2 2018-03-12 stsp
323 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
324 c34b20a2 2018-03-12 stsp if (err)
325 c34b20a2 2018-03-12 stsp return err;
326 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
327 c34b20a2 2018-03-12 stsp if (err)
328 c34b20a2 2018-03-12 stsp return err;
329 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
330 c34b20a2 2018-03-12 stsp if (err)
331 c34b20a2 2018-03-12 stsp return err;
332 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
333 c34b20a2 2018-03-12 stsp if (err)
334 c34b20a2 2018-03-12 stsp return err;
335 c34b20a2 2018-03-12 stsp
336 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
337 c34b20a2 2018-03-12 stsp if (err)
338 c34b20a2 2018-03-12 stsp return err;
339 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
340 c34b20a2 2018-03-12 stsp if (err)
341 c34b20a2 2018-03-12 stsp return err;
342 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
343 c34b20a2 2018-03-12 stsp if (err)
344 c34b20a2 2018-03-12 stsp return err;
345 c34b20a2 2018-03-12 stsp
346 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
347 c34b20a2 2018-03-12 stsp if (err)
348 c34b20a2 2018-03-12 stsp return err;
349 c34b20a2 2018-03-12 stsp
350 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
351 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
352 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
353 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
354 fc76cabb 2018-12-25 stsp
355 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
356 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
357 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
358 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
359 c34b20a2 2018-03-12 stsp
360 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
361 c34b20a2 2018-03-12 stsp if (err)
362 c34b20a2 2018-03-12 stsp return err;
363 c34b20a2 2018-03-12 stsp
364 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
365 c34b20a2 2018-03-12 stsp return err;
366 c34b20a2 2018-03-12 stsp }
367 c34b20a2 2018-03-12 stsp
368 9d31a1d8 2018-03-11 stsp const struct got_error *
369 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
370 9d31a1d8 2018-03-11 stsp {
371 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
372 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
373 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
374 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
375 c34b20a2 2018-03-12 stsp size_t n;
376 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
377 c34b20a2 2018-03-12 stsp
378 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
379 c34b20a2 2018-03-12 stsp
380 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
381 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
382 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
383 c34b20a2 2018-03-12 stsp
384 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
385 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
386 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
387 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
388 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
389 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
390 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
391 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
392 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
393 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
394 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
395 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
396 c34b20a2 2018-03-12 stsp
397 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
398 e288864f 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
399 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
400 133d2798 2019-01-08 stsp if (err)
401 133d2798 2019-01-08 stsp return err;
402 133d2798 2019-01-08 stsp }
403 c34b20a2 2018-03-12 stsp
404 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
405 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
406 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
407 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
408 52a74475 2018-12-24 stsp
409 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
410 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
411 27d0e5bd 2019-01-12 stsp
412 52a74475 2018-12-24 stsp return NULL;
413 52a74475 2018-12-24 stsp }
414 52a74475 2018-12-24 stsp
415 52a74475 2018-12-24 stsp static const struct got_error *
416 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
417 52a74475 2018-12-24 stsp {
418 52a74475 2018-12-24 stsp size_t n;
419 52a74475 2018-12-24 stsp
420 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
421 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
422 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
423 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
424 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
425 52a74475 2018-12-24 stsp return NULL;
426 52a74475 2018-12-24 stsp }
427 52a74475 2018-12-24 stsp
428 52a74475 2018-12-24 stsp static const struct got_error *
429 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
430 52a74475 2018-12-24 stsp {
431 52a74475 2018-12-24 stsp size_t n;
432 52a74475 2018-12-24 stsp
433 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
434 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
435 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
436 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
437 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
438 52a74475 2018-12-24 stsp return NULL;
439 52a74475 2018-12-24 stsp }
440 52a74475 2018-12-24 stsp
441 52a74475 2018-12-24 stsp static const struct got_error *
442 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
443 52a74475 2018-12-24 stsp {
444 52a74475 2018-12-24 stsp size_t n;
445 52a74475 2018-12-24 stsp
446 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
447 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
448 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
449 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
450 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
451 52a74475 2018-12-24 stsp return NULL;
452 52a74475 2018-12-24 stsp }
453 52a74475 2018-12-24 stsp
454 52a74475 2018-12-24 stsp static const struct got_error *
455 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
456 52a74475 2018-12-24 stsp {
457 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
458 52a74475 2018-12-24 stsp uint8_t buf[8];
459 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
460 52a74475 2018-12-24 stsp
461 52a74475 2018-12-24 stsp *path = malloc(totlen);
462 52a74475 2018-12-24 stsp if (*path == NULL)
463 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
464 52a74475 2018-12-24 stsp
465 52a74475 2018-12-24 stsp do {
466 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
467 52a74475 2018-12-24 stsp if (n != sizeof(buf))
468 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
469 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
470 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
471 52a74475 2018-12-24 stsp if (p == NULL) {
472 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
473 52a74475 2018-12-24 stsp break;
474 52a74475 2018-12-24 stsp }
475 52a74475 2018-12-24 stsp totlen += sizeof(buf);
476 52a74475 2018-12-24 stsp *path = p;
477 52a74475 2018-12-24 stsp }
478 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
479 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
480 52a74475 2018-12-24 stsp len += sizeof(buf);
481 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
482 52a74475 2018-12-24 stsp
483 52a74475 2018-12-24 stsp if (err) {
484 52a74475 2018-12-24 stsp free(*path);
485 52a74475 2018-12-24 stsp *path = NULL;
486 52a74475 2018-12-24 stsp }
487 52a74475 2018-12-24 stsp return err;
488 52a74475 2018-12-24 stsp }
489 52a74475 2018-12-24 stsp
490 52a74475 2018-12-24 stsp static const struct got_error *
491 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
492 52a74475 2018-12-24 stsp FILE *infile)
493 52a74475 2018-12-24 stsp {
494 52a74475 2018-12-24 stsp const struct got_error *err;
495 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
496 52a74475 2018-12-24 stsp size_t n;
497 52a74475 2018-12-24 stsp
498 52a74475 2018-12-24 stsp *entryp = NULL;
499 52a74475 2018-12-24 stsp
500 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
501 52a74475 2018-12-24 stsp if (entry == NULL)
502 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
503 c34b20a2 2018-03-12 stsp
504 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
505 52a74475 2018-12-24 stsp if (err)
506 52a74475 2018-12-24 stsp goto done;
507 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
508 52a74475 2018-12-24 stsp if (err)
509 52a74475 2018-12-24 stsp goto done;
510 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
511 52a74475 2018-12-24 stsp if (err)
512 52a74475 2018-12-24 stsp goto done;
513 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
514 52a74475 2018-12-24 stsp if (err)
515 52a74475 2018-12-24 stsp goto done;
516 52a74475 2018-12-24 stsp
517 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
518 52a74475 2018-12-24 stsp if (err)
519 52a74475 2018-12-24 stsp goto done;
520 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
521 52a74475 2018-12-24 stsp if (err)
522 52a74475 2018-12-24 stsp goto done;
523 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
524 52a74475 2018-12-24 stsp if (err)
525 52a74475 2018-12-24 stsp goto done;
526 52a74475 2018-12-24 stsp
527 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
528 52a74475 2018-12-24 stsp if (err)
529 52a74475 2018-12-24 stsp goto done;
530 52a74475 2018-12-24 stsp
531 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
532 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
533 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
534 52a74475 2018-12-24 stsp goto done;
535 52a74475 2018-12-24 stsp }
536 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
537 52a74475 2018-12-24 stsp
538 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
539 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
540 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
541 fc76cabb 2018-12-25 stsp goto done;
542 fc76cabb 2018-12-25 stsp }
543 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
544 fc76cabb 2018-12-25 stsp
545 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
546 52a74475 2018-12-24 stsp if (err)
547 52a74475 2018-12-24 stsp goto done;
548 52a74475 2018-12-24 stsp
549 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
550 52a74475 2018-12-24 stsp done:
551 52a74475 2018-12-24 stsp if (err)
552 52a74475 2018-12-24 stsp free(entry);
553 52a74475 2018-12-24 stsp else
554 52a74475 2018-12-24 stsp *entryp = entry;
555 52a74475 2018-12-24 stsp return err;
556 52a74475 2018-12-24 stsp }
557 52a74475 2018-12-24 stsp
558 52a74475 2018-12-24 stsp const struct got_error *
559 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
560 52a74475 2018-12-24 stsp {
561 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
562 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
563 52a74475 2018-12-24 stsp SHA1_CTX ctx;
564 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
565 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
566 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
567 52a74475 2018-12-24 stsp size_t n;
568 52a74475 2018-12-24 stsp int i;
569 52a74475 2018-12-24 stsp
570 52a74475 2018-12-24 stsp SHA1Init(&ctx);
571 52a74475 2018-12-24 stsp
572 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
573 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
574 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
575 b6d05318 2019-01-13 stsp return NULL;
576 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
577 b6d05318 2019-01-13 stsp }
578 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
579 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
580 51514078 2018-12-25 stsp if (n == 0) /* EOF */
581 51514078 2018-12-25 stsp return NULL;
582 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
583 51514078 2018-12-25 stsp }
584 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
585 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
586 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
587 b6d05318 2019-01-13 stsp return NULL;
588 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
589 b6d05318 2019-01-13 stsp }
590 52a74475 2018-12-24 stsp
591 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
592 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
593 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
594 52a74475 2018-12-24 stsp
595 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
596 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
597 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
598 52a74475 2018-12-24 stsp
599 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
600 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
601 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
602 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
603 52a74475 2018-12-24 stsp
604 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
605 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
606 52a74475 2018-12-24 stsp if (err)
607 52a74475 2018-12-24 stsp return err;
608 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
609 52a74475 2018-12-24 stsp if (err)
610 52a74475 2018-12-24 stsp return err;
611 52a74475 2018-12-24 stsp }
612 52a74475 2018-12-24 stsp
613 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
614 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
615 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
616 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
617 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
618 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
619 52a74475 2018-12-24 stsp
620 9d31a1d8 2018-03-11 stsp return NULL;
621 8da9e5f4 2019-01-12 stsp }
622 8da9e5f4 2019-01-12 stsp
623 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
624 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
625 50952927 2019-01-12 stsp {
626 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
627 50952927 2019-01-12 stsp
628 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
629 50952927 2019-01-12 stsp
630 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
631 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
632 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
633 50952927 2019-01-12 stsp
634 50952927 2019-01-12 stsp return next;
635 50952927 2019-01-12 stsp }
636 50952927 2019-01-12 stsp
637 8da9e5f4 2019-01-12 stsp static const struct got_error *
638 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
639 30a076bc 2019-07-27 stsp const struct got_tree_entries *, const char *, const char *,
640 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
641 9d2a8e53 2019-01-28 stsp
642 9d2a8e53 2019-01-28 stsp static const struct got_error *
643 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
644 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
645 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
646 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
647 8da9e5f4 2019-01-12 stsp {
648 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
649 8da9e5f4 2019-01-12 stsp
650 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
651 8da9e5f4 2019-01-12 stsp char *subpath;
652 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
653 8da9e5f4 2019-01-12 stsp
654 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
655 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
656 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
657 8da9e5f4 2019-01-12 stsp
658 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
659 8da9e5f4 2019-01-12 stsp if (err) {
660 8da9e5f4 2019-01-12 stsp free(subpath);
661 8da9e5f4 2019-01-12 stsp return err;
662 8da9e5f4 2019-01-12 stsp }
663 8da9e5f4 2019-01-12 stsp
664 30a076bc 2019-07-27 stsp err = diff_fileindex_tree(fileindex, ie,
665 30a076bc 2019-07-27 stsp got_object_tree_get_entries(subtree), subpath, entry_name,
666 30a076bc 2019-07-27 stsp repo, cb, cb_arg);
667 8da9e5f4 2019-01-12 stsp free(subpath);
668 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
669 8da9e5f4 2019-01-12 stsp if (err)
670 8da9e5f4 2019-01-12 stsp return err;
671 8da9e5f4 2019-01-12 stsp }
672 8da9e5f4 2019-01-12 stsp
673 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
674 8da9e5f4 2019-01-12 stsp return NULL;
675 8da9e5f4 2019-01-12 stsp }
676 8da9e5f4 2019-01-12 stsp
677 8da9e5f4 2019-01-12 stsp static const struct got_error *
678 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
679 30a076bc 2019-07-27 stsp struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
680 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
681 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
682 8da9e5f4 2019-01-12 stsp {
683 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
684 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
685 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
686 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
687 8da9e5f4 2019-01-12 stsp
688 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
689 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
690 8da9e5f4 2019-01-12 stsp if (te && *ie) {
691 18831e78 2019-02-10 stsp char *te_path;
692 18831e78 2019-02-10 stsp int cmp;
693 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
694 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
695 18831e78 2019-02-10 stsp break;
696 18831e78 2019-02-10 stsp }
697 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
698 18831e78 2019-02-10 stsp free(te_path);
699 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
700 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
701 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
702 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
703 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
704 c4cdcb68 2019-04-03 stsp path);
705 c4cdcb68 2019-04-03 stsp if (err || entry_name)
706 c4cdcb68 2019-04-03 stsp break;
707 c4cdcb68 2019-04-03 stsp }
708 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
709 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
710 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
711 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
712 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
713 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
714 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
715 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
716 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
717 c4cdcb68 2019-04-03 stsp if (err || entry_name)
718 c4cdcb68 2019-04-03 stsp break;
719 c4cdcb68 2019-04-03 stsp }
720 8da9e5f4 2019-01-12 stsp *ie = next;
721 8da9e5f4 2019-01-12 stsp } else {
722 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
723 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
724 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
725 c4cdcb68 2019-04-03 stsp if (err || entry_name)
726 c4cdcb68 2019-04-03 stsp break;
727 c4cdcb68 2019-04-03 stsp }
728 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
729 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
730 8da9e5f4 2019-01-12 stsp }
731 8da9e5f4 2019-01-12 stsp if (err)
732 8da9e5f4 2019-01-12 stsp break;
733 8da9e5f4 2019-01-12 stsp } else if (*ie) {
734 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
735 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
736 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
737 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
738 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
739 c4cdcb68 2019-04-03 stsp if (err || entry_name)
740 c4cdcb68 2019-04-03 stsp break;
741 c4cdcb68 2019-04-03 stsp }
742 8da9e5f4 2019-01-12 stsp *ie = next;
743 8da9e5f4 2019-01-12 stsp } else if (te) {
744 c4cdcb68 2019-04-03 stsp if (entry_name == NULL ||
745 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0) {
746 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
747 c4cdcb68 2019-04-03 stsp if (err || entry_name)
748 c4cdcb68 2019-04-03 stsp break;
749 c4cdcb68 2019-04-03 stsp }
750 c4cdcb68 2019-04-03 stsp err = walk_tree(&te, fileindex, ie, te, path,
751 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
752 8da9e5f4 2019-01-12 stsp if (err)
753 8da9e5f4 2019-01-12 stsp break;
754 8da9e5f4 2019-01-12 stsp }
755 500cd40f 2019-02-04 stsp }
756 8da9e5f4 2019-01-12 stsp
757 8da9e5f4 2019-01-12 stsp return err;
758 8da9e5f4 2019-01-12 stsp }
759 8da9e5f4 2019-01-12 stsp
760 8da9e5f4 2019-01-12 stsp const struct got_error *
761 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
762 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
763 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
764 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
765 8da9e5f4 2019-01-12 stsp {
766 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
767 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
768 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
769 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
770 30a076bc 2019-07-27 stsp return diff_fileindex_tree(fileindex, &ie,
771 30a076bc 2019-07-27 stsp got_object_tree_get_entries(tree), path, entry_name, repo,
772 30a076bc 2019-07-27 stsp cb, cb_arg);
773 d1f6d47b 2019-02-04 stsp }
774 d1f6d47b 2019-02-04 stsp
775 d1f6d47b 2019-02-04 stsp static const struct got_error *
776 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
777 c577a9ce 2019-07-27 stsp struct got_pathlist_head *, const char *, const char *,
778 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
779 c577a9ce 2019-07-27 stsp
780 c577a9ce 2019-07-27 stsp static const struct got_error *
781 c577a9ce 2019-07-27 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
782 c577a9ce 2019-07-27 stsp {
783 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
784 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
785 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
786 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
787 c577a9ce 2019-07-27 stsp
788 c577a9ce 2019-07-27 stsp for (;;) {
789 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
790 c577a9ce 2019-07-27 stsp if (de == NULL) {
791 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
792 c577a9ce 2019-07-27 stsp break;
793 c577a9ce 2019-07-27 stsp }
794 c577a9ce 2019-07-27 stsp
795 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
796 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
797 c577a9ce 2019-07-27 stsp free(de);
798 c577a9ce 2019-07-27 stsp break;
799 c577a9ce 2019-07-27 stsp }
800 c577a9ce 2019-07-27 stsp if (dep == NULL) {
801 c577a9ce 2019-07-27 stsp free(de);
802 c577a9ce 2019-07-27 stsp break;
803 c577a9ce 2019-07-27 stsp }
804 c577a9ce 2019-07-27 stsp
805 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
806 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
807 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
808 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
809 c577a9ce 2019-07-27 stsp free(de);
810 c577a9ce 2019-07-27 stsp continue;
811 c577a9ce 2019-07-27 stsp }
812 c577a9ce 2019-07-27 stsp
813 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
814 c577a9ce 2019-07-27 stsp if (err) {
815 c577a9ce 2019-07-27 stsp free(de);
816 c577a9ce 2019-07-27 stsp break;
817 c577a9ce 2019-07-27 stsp }
818 c577a9ce 2019-07-27 stsp if (new == NULL) {
819 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
820 c577a9ce 2019-07-27 stsp free(de);
821 c577a9ce 2019-07-27 stsp break;
822 c577a9ce 2019-07-27 stsp }
823 c577a9ce 2019-07-27 stsp }
824 500cd40f 2019-02-04 stsp
825 c577a9ce 2019-07-27 stsp return err;
826 c577a9ce 2019-07-27 stsp }
827 c577a9ce 2019-07-27 stsp
828 c577a9ce 2019-07-27 stsp void
829 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
830 c577a9ce 2019-07-27 stsp {
831 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
832 c577a9ce 2019-07-27 stsp
833 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
834 c577a9ce 2019-07-27 stsp free(dle->data);
835 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
836 c577a9ce 2019-07-27 stsp }
837 c577a9ce 2019-07-27 stsp
838 d1f6d47b 2019-02-04 stsp static const struct got_error *
839 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
840 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
841 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
842 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
843 d1f6d47b 2019-02-04 stsp {
844 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
845 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
846 573463cc 2019-04-06 stsp
847 573463cc 2019-04-06 stsp *next = NULL;
848 d1f6d47b 2019-02-04 stsp
849 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
850 d1f6d47b 2019-02-04 stsp char *subpath;
851 c7f4312f 2019-02-05 stsp char *subdirpath;
852 d1f6d47b 2019-02-04 stsp DIR *subdir;
853 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
854 d1f6d47b 2019-02-04 stsp
855 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
856 c577a9ce 2019-07-27 stsp
857 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
858 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
859 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
860 d1f6d47b 2019-02-04 stsp
861 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
862 c7f4312f 2019-02-05 stsp free(subpath);
863 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
864 c7f4312f 2019-02-05 stsp }
865 c7f4312f 2019-02-05 stsp
866 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
867 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
868 d1f6d47b 2019-02-04 stsp free(subpath);
869 c7f4312f 2019-02-05 stsp free(subdirpath);
870 638f9024 2019-05-13 stsp return got_error_from_errno2("opendir", subdirpath);
871 d1f6d47b 2019-02-04 stsp }
872 d1f6d47b 2019-02-04 stsp
873 c577a9ce 2019-07-27 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
874 c577a9ce 2019-07-27 stsp if (err) {
875 c577a9ce 2019-07-27 stsp free(subpath);
876 c577a9ce 2019-07-27 stsp free(subdirpath);
877 c577a9ce 2019-07-27 stsp closedir(subdir);
878 c577a9ce 2019-07-27 stsp return err;
879 c577a9ce 2019-07-27 stsp }
880 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
881 39beb6da 2019-07-27 stsp subpath, repo, cb, cb_arg);
882 d1f6d47b 2019-02-04 stsp free(subpath);
883 c7f4312f 2019-02-05 stsp free(subdirpath);
884 d1f6d47b 2019-02-04 stsp closedir(subdir);
885 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
886 d1f6d47b 2019-02-04 stsp if (err)
887 d1f6d47b 2019-02-04 stsp return err;
888 d1f6d47b 2019-02-04 stsp }
889 d1f6d47b 2019-02-04 stsp
890 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
891 d1f6d47b 2019-02-04 stsp return NULL;
892 d1f6d47b 2019-02-04 stsp }
893 d1f6d47b 2019-02-04 stsp
894 d1f6d47b 2019-02-04 stsp static const struct got_error *
895 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
896 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
897 39beb6da 2019-07-27 stsp const char *rootpath, const char *path, struct got_repository *repo,
898 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
899 d1f6d47b 2019-02-04 stsp {
900 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
901 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
902 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
903 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
904 d1f6d47b 2019-02-04 stsp
905 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
906 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
907 500cd40f 2019-02-04 stsp if (dle && *ie) {
908 18831e78 2019-02-10 stsp char *de_path;
909 e7a2f030 2019-02-05 stsp int cmp;
910 f5d3d7af 2019-02-05 stsp de = dle->data;
911 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
912 18831e78 2019-02-10 stsp de->d_name) == -1) {
913 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
914 13ff9e90 2019-02-10 stsp break;
915 18831e78 2019-02-10 stsp }
916 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
917 18831e78 2019-02-10 stsp free(de_path);
918 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
919 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
920 d1f6d47b 2019-02-04 stsp if (err)
921 d1f6d47b 2019-02-04 stsp break;
922 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
923 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
924 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
925 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
926 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
927 d1f6d47b 2019-02-04 stsp if (err)
928 d1f6d47b 2019-02-04 stsp break;
929 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
930 d1f6d47b 2019-02-04 stsp } else {
931 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
932 d1f6d47b 2019-02-04 stsp if (err)
933 d1f6d47b 2019-02-04 stsp break;
934 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
935 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
936 d1f6d47b 2019-02-04 stsp }
937 554b91b1 2019-02-04 stsp if (err)
938 554b91b1 2019-02-04 stsp break;
939 554b91b1 2019-02-04 stsp } else if (*ie) {
940 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
941 554b91b1 2019-02-04 stsp if (err)
942 554b91b1 2019-02-04 stsp break;
943 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
944 554b91b1 2019-02-04 stsp } else if (dle) {
945 763e1377 2019-02-05 stsp de = dle->data;
946 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
947 d1f6d47b 2019-02-04 stsp if (err)
948 d1f6d47b 2019-02-04 stsp break;
949 39beb6da 2019-07-27 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
950 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
951 554b91b1 2019-02-04 stsp if (err)
952 554b91b1 2019-02-04 stsp break;
953 d1f6d47b 2019-02-04 stsp }
954 500cd40f 2019-02-04 stsp }
955 c577a9ce 2019-07-27 stsp
956 d1f6d47b 2019-02-04 stsp return err;
957 8da9e5f4 2019-01-12 stsp }
958 8da9e5f4 2019-01-12 stsp
959 d1f6d47b 2019-02-04 stsp const struct got_error *
960 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
961 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
962 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
963 d1f6d47b 2019-02-04 stsp {
964 c577a9ce 2019-07-27 stsp const struct got_error *err;
965 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
966 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
967 c577a9ce 2019-07-27 stsp
968 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
969 c577a9ce 2019-07-27 stsp err = read_dirlist(&dirlist, rootdir, path);
970 c577a9ce 2019-07-27 stsp if (err)
971 c577a9ce 2019-07-27 stsp return err;
972 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
973 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
974 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
975 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
976 39beb6da 2019-07-27 stsp repo, cb, cb_arg);
977 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
978 c577a9ce 2019-07-27 stsp return err;
979 d1f6d47b 2019-02-04 stsp }
980 d1f6d47b 2019-02-04 stsp
981 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);