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