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