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