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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 c48c4a9c 2018-03-11 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
21 c48c4a9c 2018-03-11 stsp
22 03df25b3 2019-05-11 stsp #include <errno.h>
23 d1f6d47b 2019-02-04 stsp #include <dirent.h>
24 6fc93f37 2019-12-13 stsp #include <fcntl.h>
25 c48c4a9c 2018-03-11 stsp #include <stdio.h>
26 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
27 c48c4a9c 2018-03-11 stsp #include <string.h>
28 133d2798 2019-01-08 stsp #include <limits.h>
29 6fc93f37 2019-12-13 stsp #include <unistd.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 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
37 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
38 c48c4a9c 2018-03-11 stsp
39 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
40 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
41 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
42 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 a46b9f33 2020-01-28 stsp #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
48 a769b60b 2021-06-27 stsp #define GOT_FILEIDX_F_SKIPPED 0x00200000
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 0aeb8099 2020-07-23 stsp mode_t
57 0aeb8099 2020-07-23 stsp got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
58 26a7fe28 2019-07-27 stsp {
59 4723f050 2020-07-23 stsp return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
61 26a7fe28 2019-07-27 stsp }
62 26a7fe28 2019-07-27 stsp
63 cf34e6e7 2020-07-23 stsp static void
64 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
65 4723f050 2020-07-23 stsp {
66 4723f050 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 4723f050 2020-07-23 stsp ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS);
69 4723f050 2020-07-23 stsp }
70 4723f050 2020-07-23 stsp
71 26a7fe28 2019-07-27 stsp mode_t
72 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
73 26a7fe28 2019-07-27 stsp {
74 0aeb8099 2020-07-23 stsp mode_t perms = got_fileindex_entry_perms_get(ie);
75 4723f050 2020-07-23 stsp int type = got_fileindex_entry_filetype_get(ie);
76 4723f050 2020-07-23 stsp uint32_t ftype;
77 4723f050 2020-07-23 stsp
78 4723f050 2020-07-23 stsp if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 4723f050 2020-07-23 stsp type == GOT_FILEIDX_MODE_BAD_SYMLINK)
80 4723f050 2020-07-23 stsp ftype = S_IFREG;
81 4723f050 2020-07-23 stsp else
82 4723f050 2020-07-23 stsp ftype = S_IFLNK;
83 4723f050 2020-07-23 stsp
84 4723f050 2020-07-23 stsp return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
85 26a7fe28 2019-07-27 stsp }
86 26a7fe28 2019-07-27 stsp
87 c48c4a9c 2018-03-11 stsp const struct got_error *
88 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
89 437adc9d 2020-12-10 yzhong int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
90 437adc9d 2020-12-10 yzhong uint8_t *commit_sha1, int update_timestamps)
91 c48c4a9c 2018-03-11 stsp {
92 c48c4a9c 2018-03-11 stsp struct stat sb;
93 c48c4a9c 2018-03-11 stsp
94 437adc9d 2020-12-10 yzhong if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
95 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 b15816dd 2019-08-11 stsp errno == ENOENT))
97 437adc9d 2020-12-10 yzhong return got_error_from_errno2("fstatat", ondisk_path);
98 aa9ad276 2020-07-25 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
99 03df25b3 2019-05-11 stsp } else {
100 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
101 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
102 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
103 03df25b3 2019-05-11 stsp }
104 c48c4a9c 2018-03-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 0529f8df 2023-03-10 thomas memmove(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 9fad5d8c 2022-04-16 thomas if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
267 9fad5d8c 2022-04-16 thomas return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
268 9fad5d8c 2022-04-16 thomas
269 133d2798 2019-01-08 stsp fileindex->nentries++;
270 133d2798 2019-01-08 stsp return NULL;
271 50952927 2019-01-12 stsp }
272 50952927 2019-01-12 stsp
273 50952927 2019-01-12 stsp const struct got_error *
274 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
275 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
276 50952927 2019-01-12 stsp {
277 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
278 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
279 50952927 2019-01-12 stsp
280 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
281 9d31a1d8 2018-03-11 stsp }
282 9d31a1d8 2018-03-11 stsp
283 133d2798 2019-01-08 stsp void
284 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
285 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
286 512f0d0e 2019-01-02 stsp {
287 a46b9f33 2020-01-28 stsp /*
288 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
289 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
290 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
291 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
292 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
293 a46b9f33 2020-01-28 stsp */
294 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
295 133d2798 2019-01-08 stsp fileindex->nentries--;
296 512f0d0e 2019-01-02 stsp }
297 512f0d0e 2019-01-02 stsp
298 51514078 2018-12-25 stsp struct got_fileindex_entry *
299 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
300 d6c87207 2019-08-02 stsp size_t path_len)
301 51514078 2018-12-25 stsp {
302 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
303 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
304 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
305 133d2798 2019-01-08 stsp key.path = (char *)path;
306 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
307 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
308 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
309 a46b9f33 2020-01-28 stsp return NULL;
310 a46b9f33 2020-01-28 stsp return ie;
311 b504a804 2019-01-08 stsp }
312 51514078 2018-12-25 stsp
313 512f0d0e 2019-01-02 stsp const struct got_error *
314 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
315 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
316 512f0d0e 2019-01-02 stsp {
317 133d2798 2019-01-08 stsp const struct got_error *err;
318 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
319 9d31a1d8 2018-03-11 stsp
320 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
321 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
322 a46b9f33 2020-01-28 stsp continue;
323 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
324 133d2798 2019-01-08 stsp if (err)
325 133d2798 2019-01-08 stsp return err;
326 b504a804 2019-01-08 stsp }
327 b504a804 2019-01-08 stsp return NULL;
328 9d31a1d8 2018-03-11 stsp }
329 9d31a1d8 2018-03-11 stsp
330 133d2798 2019-01-08 stsp struct got_fileindex *
331 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
332 6b798c3c 2019-01-08 stsp {
333 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
334 133d2798 2019-01-08 stsp
335 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
336 133d2798 2019-01-08 stsp if (fileindex == NULL)
337 133d2798 2019-01-08 stsp return NULL;
338 133d2798 2019-01-08 stsp
339 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
340 133d2798 2019-01-08 stsp return fileindex;
341 6b798c3c 2019-01-08 stsp }
342 6b798c3c 2019-01-08 stsp
343 9d31a1d8 2018-03-11 stsp void
344 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
345 9d31a1d8 2018-03-11 stsp {
346 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
347 133d2798 2019-01-08 stsp
348 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
349 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
350 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
351 133d2798 2019-01-08 stsp }
352 9d31a1d8 2018-03-11 stsp free(fileindex);
353 9d31a1d8 2018-03-11 stsp }
354 9d31a1d8 2018-03-11 stsp
355 c34b20a2 2018-03-12 stsp static const struct got_error *
356 b16893ba 2023-02-24 thomas write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
357 c34b20a2 2018-03-12 stsp {
358 c34b20a2 2018-03-12 stsp size_t n;
359 c34b20a2 2018-03-12 stsp
360 c34b20a2 2018-03-12 stsp val = htobe64(val);
361 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
362 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
363 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
364 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
365 c34b20a2 2018-03-12 stsp return NULL;
366 c34b20a2 2018-03-12 stsp }
367 c34b20a2 2018-03-12 stsp
368 c34b20a2 2018-03-12 stsp static const struct got_error *
369 b16893ba 2023-02-24 thomas write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
370 c34b20a2 2018-03-12 stsp {
371 c34b20a2 2018-03-12 stsp size_t n;
372 c34b20a2 2018-03-12 stsp
373 c34b20a2 2018-03-12 stsp val = htobe32(val);
374 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
375 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
376 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
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 b16893ba 2023-02-24 thomas write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
383 c34b20a2 2018-03-12 stsp {
384 c34b20a2 2018-03-12 stsp size_t n;
385 c34b20a2 2018-03-12 stsp
386 c34b20a2 2018-03-12 stsp val = htobe16(val);
387 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
388 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
389 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
390 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
391 c34b20a2 2018-03-12 stsp return NULL;
392 c34b20a2 2018-03-12 stsp }
393 c34b20a2 2018-03-12 stsp
394 c34b20a2 2018-03-12 stsp static const struct got_error *
395 b16893ba 2023-02-24 thomas write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
396 c34b20a2 2018-03-12 stsp {
397 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
398 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
399 c34b20a2 2018-03-12 stsp
400 c34b20a2 2018-03-12 stsp len = strlen(path);
401 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
402 3c5b70f2 2018-12-27 stsp pad++;
403 3c5b70f2 2018-12-27 stsp if (pad == 0)
404 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
405 c34b20a2 2018-03-12 stsp
406 b16893ba 2023-02-24 thomas got_hash_update(ctx, path, len);
407 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
408 c34b20a2 2018-03-12 stsp if (n != len)
409 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
410 b16893ba 2023-02-24 thomas got_hash_update(ctx, zero, pad);
411 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
412 c34b20a2 2018-03-12 stsp if (n != pad)
413 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
414 c34b20a2 2018-03-12 stsp return NULL;
415 c34b20a2 2018-03-12 stsp }
416 c34b20a2 2018-03-12 stsp
417 c34b20a2 2018-03-12 stsp static const struct got_error *
418 b16893ba 2023-02-24 thomas write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
419 c34b20a2 2018-03-12 stsp FILE *outfile)
420 c34b20a2 2018-03-12 stsp {
421 c34b20a2 2018-03-12 stsp const struct got_error *err;
422 23b19d00 2018-03-12 stsp size_t n;
423 df335242 2019-08-03 stsp uint32_t stage;
424 c34b20a2 2018-03-12 stsp
425 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
426 c34b20a2 2018-03-12 stsp if (err)
427 c34b20a2 2018-03-12 stsp return err;
428 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
429 c34b20a2 2018-03-12 stsp if (err)
430 c34b20a2 2018-03-12 stsp return err;
431 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
432 c34b20a2 2018-03-12 stsp if (err)
433 c34b20a2 2018-03-12 stsp return err;
434 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
435 c34b20a2 2018-03-12 stsp if (err)
436 c34b20a2 2018-03-12 stsp return err;
437 c34b20a2 2018-03-12 stsp
438 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
439 c34b20a2 2018-03-12 stsp if (err)
440 c34b20a2 2018-03-12 stsp return err;
441 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
442 c34b20a2 2018-03-12 stsp if (err)
443 c34b20a2 2018-03-12 stsp return err;
444 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
445 c34b20a2 2018-03-12 stsp if (err)
446 c34b20a2 2018-03-12 stsp return err;
447 c34b20a2 2018-03-12 stsp
448 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
449 c34b20a2 2018-03-12 stsp if (err)
450 c34b20a2 2018-03-12 stsp return err;
451 c34b20a2 2018-03-12 stsp
452 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
453 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
454 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
455 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
456 fc76cabb 2018-12-25 stsp
457 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
458 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
459 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
460 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
461 c34b20a2 2018-03-12 stsp
462 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
463 c34b20a2 2018-03-12 stsp if (err)
464 c34b20a2 2018-03-12 stsp return err;
465 c34b20a2 2018-03-12 stsp
466 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
467 df335242 2019-08-03 stsp if (err)
468 df335242 2019-08-03 stsp return err;
469 df335242 2019-08-03 stsp
470 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
471 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
472 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
473 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
474 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
475 df335242 2019-08-03 stsp outfile);
476 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
477 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
478 df335242 2019-08-03 stsp }
479 df335242 2019-08-03 stsp
480 df335242 2019-08-03 stsp return NULL;
481 c34b20a2 2018-03-12 stsp }
482 c34b20a2 2018-03-12 stsp
483 9d31a1d8 2018-03-11 stsp const struct got_error *
484 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
485 9d31a1d8 2018-03-11 stsp {
486 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
487 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
488 b16893ba 2023-02-24 thomas struct got_hash ctx;
489 b16893ba 2023-02-24 thomas uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
490 c34b20a2 2018-03-12 stsp size_t n;
491 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
492 c34b20a2 2018-03-12 stsp
493 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
494 c34b20a2 2018-03-12 stsp
495 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
496 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
497 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
498 c34b20a2 2018-03-12 stsp
499 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
500 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
501 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
502 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
503 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
504 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
505 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
506 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
507 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
508 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
509 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
510 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
511 c34b20a2 2018-03-12 stsp
512 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
513 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
514 a769b60b 2021-06-27 stsp ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
515 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
516 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
517 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
518 a46b9f33 2020-01-28 stsp continue;
519 8bd8568c 2020-04-24 stsp }
520 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
521 133d2798 2019-01-08 stsp if (err)
522 133d2798 2019-01-08 stsp return err;
523 133d2798 2019-01-08 stsp }
524 c34b20a2 2018-03-12 stsp
525 b16893ba 2023-02-24 thomas got_hash_final(&ctx, hash);
526 b16893ba 2023-02-24 thomas n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
527 b16893ba 2023-02-24 thomas if (n != SHA1_DIGEST_LENGTH)
528 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
529 52a74475 2018-12-24 stsp
530 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
531 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
532 27d0e5bd 2019-01-12 stsp
533 52a74475 2018-12-24 stsp return NULL;
534 52a74475 2018-12-24 stsp }
535 52a74475 2018-12-24 stsp
536 52a74475 2018-12-24 stsp static const struct got_error *
537 b16893ba 2023-02-24 thomas read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
538 52a74475 2018-12-24 stsp {
539 52a74475 2018-12-24 stsp size_t n;
540 52a74475 2018-12-24 stsp
541 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
542 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
543 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
544 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
545 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
546 52a74475 2018-12-24 stsp return NULL;
547 52a74475 2018-12-24 stsp }
548 52a74475 2018-12-24 stsp
549 52a74475 2018-12-24 stsp static const struct got_error *
550 b16893ba 2023-02-24 thomas read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
551 52a74475 2018-12-24 stsp {
552 52a74475 2018-12-24 stsp size_t n;
553 52a74475 2018-12-24 stsp
554 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
555 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
556 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
558 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
559 52a74475 2018-12-24 stsp return NULL;
560 52a74475 2018-12-24 stsp }
561 52a74475 2018-12-24 stsp
562 52a74475 2018-12-24 stsp static const struct got_error *
563 b16893ba 2023-02-24 thomas read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
564 52a74475 2018-12-24 stsp {
565 52a74475 2018-12-24 stsp size_t n;
566 52a74475 2018-12-24 stsp
567 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
568 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
569 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
570 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
571 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
572 52a74475 2018-12-24 stsp return NULL;
573 52a74475 2018-12-24 stsp }
574 52a74475 2018-12-24 stsp
575 52a74475 2018-12-24 stsp static const struct got_error *
576 b16893ba 2023-02-24 thomas read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
577 52a74475 2018-12-24 stsp {
578 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
579 dcc9ed63 2023-07-29 thomas char p[PATH_MAX];
580 dcc9ed63 2023-07-29 thomas size_t n, len = 0;
581 52a74475 2018-12-24 stsp
582 52a74475 2018-12-24 stsp do {
583 dcc9ed63 2023-07-29 thomas if (len + chunk_size > sizeof(p))
584 dcc9ed63 2023-07-29 thomas return got_error(GOT_ERR_FILEIDX_BAD);
585 dcc9ed63 2023-07-29 thomas
586 dcc9ed63 2023-07-29 thomas n = fread(&p[len], 1, chunk_size, infile);
587 dcc9ed63 2023-07-29 thomas if (n != chunk_size)
588 dcc9ed63 2023-07-29 thomas return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
589 dcc9ed63 2023-07-29 thomas
590 dcc9ed63 2023-07-29 thomas got_hash_update(ctx, &p[len], chunk_size);
591 6bf2c316 2019-08-02 stsp len += chunk_size;
592 dcc9ed63 2023-07-29 thomas } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL);
593 52a74475 2018-12-24 stsp
594 dcc9ed63 2023-07-29 thomas *path = strdup(p);
595 dcc9ed63 2023-07-29 thomas if (*path == NULL)
596 dcc9ed63 2023-07-29 thomas return got_error_from_errno("strdup");
597 dcc9ed63 2023-07-29 thomas return NULL;
598 52a74475 2018-12-24 stsp }
599 52a74475 2018-12-24 stsp
600 52a74475 2018-12-24 stsp static const struct got_error *
601 b16893ba 2023-02-24 thomas read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
602 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
603 52a74475 2018-12-24 stsp {
604 52a74475 2018-12-24 stsp const struct got_error *err;
605 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
606 52a74475 2018-12-24 stsp size_t n;
607 52a74475 2018-12-24 stsp
608 3f762da0 2019-08-03 stsp *iep = NULL;
609 52a74475 2018-12-24 stsp
610 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
611 3f762da0 2019-08-03 stsp if (ie == NULL)
612 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
613 c34b20a2 2018-03-12 stsp
614 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
615 52a74475 2018-12-24 stsp if (err)
616 52a74475 2018-12-24 stsp goto done;
617 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
618 52a74475 2018-12-24 stsp if (err)
619 52a74475 2018-12-24 stsp goto done;
620 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
621 52a74475 2018-12-24 stsp if (err)
622 52a74475 2018-12-24 stsp goto done;
623 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
624 52a74475 2018-12-24 stsp if (err)
625 52a74475 2018-12-24 stsp goto done;
626 52a74475 2018-12-24 stsp
627 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, 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_val32(&ie->gid, 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_val32(&ie->size, 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_val16(&ie->mode, ctx, infile);
638 52a74475 2018-12-24 stsp if (err)
639 52a74475 2018-12-24 stsp goto done;
640 52a74475 2018-12-24 stsp
641 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
642 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
643 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
644 52a74475 2018-12-24 stsp goto done;
645 52a74475 2018-12-24 stsp }
646 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
647 52a74475 2018-12-24 stsp
648 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
649 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
650 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
651 fc76cabb 2018-12-25 stsp goto done;
652 fc76cabb 2018-12-25 stsp }
653 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
654 fc76cabb 2018-12-25 stsp
655 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
656 52a74475 2018-12-24 stsp if (err)
657 52a74475 2018-12-24 stsp goto done;
658 52a74475 2018-12-24 stsp
659 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
660 df335242 2019-08-03 stsp if (err)
661 df335242 2019-08-03 stsp goto done;
662 df335242 2019-08-03 stsp
663 df335242 2019-08-03 stsp if (version >= 2) {
664 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
665 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
666 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
667 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
668 df335242 2019-08-03 stsp infile);
669 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
670 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
671 df335242 2019-08-03 stsp goto done;
672 df335242 2019-08-03 stsp }
673 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->staged_blob_sha1,
674 b16893ba 2023-02-24 thomas SHA1_DIGEST_LENGTH);
675 df335242 2019-08-03 stsp }
676 df335242 2019-08-03 stsp } else {
677 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
678 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
679 df335242 2019-08-03 stsp }
680 df335242 2019-08-03 stsp
681 52a74475 2018-12-24 stsp done:
682 52a74475 2018-12-24 stsp if (err)
683 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
684 52a74475 2018-12-24 stsp else
685 3f762da0 2019-08-03 stsp *iep = ie;
686 52a74475 2018-12-24 stsp return err;
687 52a74475 2018-12-24 stsp }
688 52a74475 2018-12-24 stsp
689 52a74475 2018-12-24 stsp const struct got_error *
690 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
691 52a74475 2018-12-24 stsp {
692 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
693 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
694 b16893ba 2023-02-24 thomas struct got_hash ctx;
695 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
696 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
697 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
698 52a74475 2018-12-24 stsp size_t n;
699 52a74475 2018-12-24 stsp int i;
700 52a74475 2018-12-24 stsp
701 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
702 52a74475 2018-12-24 stsp
703 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
704 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
705 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
706 b6d05318 2019-01-13 stsp return NULL;
707 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
708 b6d05318 2019-01-13 stsp }
709 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
710 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
711 51514078 2018-12-25 stsp if (n == 0) /* EOF */
712 51514078 2018-12-25 stsp return NULL;
713 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
714 51514078 2018-12-25 stsp }
715 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
716 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
717 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
718 b6d05318 2019-01-13 stsp return NULL;
719 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
720 b6d05318 2019-01-13 stsp }
721 52a74475 2018-12-24 stsp
722 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
723 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
724 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
725 52a74475 2018-12-24 stsp
726 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
727 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
728 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
729 52a74475 2018-12-24 stsp
730 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
731 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
732 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
733 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
734 52a74475 2018-12-24 stsp
735 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
736 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
737 52a74475 2018-12-24 stsp if (err)
738 52a74475 2018-12-24 stsp return err;
739 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
740 991857c7 2023-04-14 thomas if (err) {
741 991857c7 2023-04-14 thomas got_fileindex_entry_free(ie);
742 52a74475 2018-12-24 stsp return err;
743 991857c7 2023-04-14 thomas }
744 52a74475 2018-12-24 stsp }
745 52a74475 2018-12-24 stsp
746 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
747 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
748 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
749 b16893ba 2023-02-24 thomas got_hash_final(&ctx, sha1);
750 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
751 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
752 52a74475 2018-12-24 stsp
753 9d31a1d8 2018-03-11 stsp return NULL;
754 8da9e5f4 2019-01-12 stsp }
755 8da9e5f4 2019-01-12 stsp
756 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
757 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
758 50952927 2019-01-12 stsp {
759 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
760 50952927 2019-01-12 stsp
761 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
762 50952927 2019-01-12 stsp
763 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
764 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
765 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
766 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
767 50952927 2019-01-12 stsp
768 50952927 2019-01-12 stsp return next;
769 50952927 2019-01-12 stsp }
770 50952927 2019-01-12 stsp
771 8da9e5f4 2019-01-12 stsp static const struct got_error *
772 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
773 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
774 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
775 9d2a8e53 2019-01-28 stsp
776 9d2a8e53 2019-01-28 stsp static const struct got_error *
777 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
778 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
779 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
780 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
781 8da9e5f4 2019-01-12 stsp {
782 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
783 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
784 8da9e5f4 2019-01-12 stsp
785 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
786 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
787 8da9e5f4 2019-01-12 stsp char *subpath;
788 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
789 8da9e5f4 2019-01-12 stsp
790 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
791 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
792 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
793 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
794 8da9e5f4 2019-01-12 stsp
795 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
796 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
797 8da9e5f4 2019-01-12 stsp if (err) {
798 8da9e5f4 2019-01-12 stsp free(subpath);
799 8da9e5f4 2019-01-12 stsp return err;
800 8da9e5f4 2019-01-12 stsp }
801 8da9e5f4 2019-01-12 stsp
802 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
803 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
804 8da9e5f4 2019-01-12 stsp free(subpath);
805 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
806 8da9e5f4 2019-01-12 stsp if (err)
807 8da9e5f4 2019-01-12 stsp return err;
808 8da9e5f4 2019-01-12 stsp }
809 8da9e5f4 2019-01-12 stsp
810 56e0773d 2019-11-28 stsp (*tidx)++;
811 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
812 8da9e5f4 2019-01-12 stsp return NULL;
813 8da9e5f4 2019-01-12 stsp }
814 8da9e5f4 2019-01-12 stsp
815 8da9e5f4 2019-01-12 stsp static const struct got_error *
816 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
817 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
818 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
819 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
820 8da9e5f4 2019-01-12 stsp {
821 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
822 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
823 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
824 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
825 56e0773d 2019-11-28 stsp int tidx = 0;
826 8da9e5f4 2019-01-12 stsp
827 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
828 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
829 8da9e5f4 2019-01-12 stsp if (te && *ie) {
830 18831e78 2019-02-10 stsp char *te_path;
831 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
832 18831e78 2019-02-10 stsp int cmp;
833 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
834 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
835 18831e78 2019-02-10 stsp break;
836 18831e78 2019-02-10 stsp }
837 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
838 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
839 18831e78 2019-02-10 stsp free(te_path);
840 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
841 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
842 63c5ca5d 2019-08-24 stsp path_len) &&
843 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
844 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
845 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
846 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
847 c4cdcb68 2019-04-03 stsp path);
848 c4cdcb68 2019-04-03 stsp if (err || entry_name)
849 c4cdcb68 2019-04-03 stsp break;
850 c4cdcb68 2019-04-03 stsp }
851 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
852 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
853 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
854 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
855 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
856 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
857 194cb7cb 2021-01-19 stsp path_len) && entry_name == NULL) {
858 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
859 c4cdcb68 2019-04-03 stsp if (err || entry_name)
860 c4cdcb68 2019-04-03 stsp break;
861 c4cdcb68 2019-04-03 stsp }
862 8da9e5f4 2019-01-12 stsp *ie = next;
863 8da9e5f4 2019-01-12 stsp } else {
864 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
865 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
866 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
867 c4cdcb68 2019-04-03 stsp if (err || entry_name)
868 c4cdcb68 2019-04-03 stsp break;
869 c4cdcb68 2019-04-03 stsp }
870 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
871 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
872 8da9e5f4 2019-01-12 stsp }
873 8da9e5f4 2019-01-12 stsp if (err)
874 8da9e5f4 2019-01-12 stsp break;
875 8da9e5f4 2019-01-12 stsp } else if (*ie) {
876 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
877 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
878 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
879 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
880 56e0773d 2019-11-28 stsp entry_name) == 0))) {
881 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
882 c4cdcb68 2019-04-03 stsp if (err || entry_name)
883 c4cdcb68 2019-04-03 stsp break;
884 c4cdcb68 2019-04-03 stsp }
885 8da9e5f4 2019-01-12 stsp *ie = next;
886 8da9e5f4 2019-01-12 stsp } else if (te) {
887 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
888 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
889 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
890 56e0773d 2019-11-28 stsp == 0)) {
891 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
892 c4cdcb68 2019-04-03 stsp if (err || entry_name)
893 c4cdcb68 2019-04-03 stsp break;
894 c4cdcb68 2019-04-03 stsp }
895 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
896 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
897 8da9e5f4 2019-01-12 stsp if (err)
898 8da9e5f4 2019-01-12 stsp break;
899 8da9e5f4 2019-01-12 stsp }
900 500cd40f 2019-02-04 stsp }
901 8da9e5f4 2019-01-12 stsp
902 8da9e5f4 2019-01-12 stsp return err;
903 8da9e5f4 2019-01-12 stsp }
904 8da9e5f4 2019-01-12 stsp
905 8da9e5f4 2019-01-12 stsp const struct got_error *
906 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
907 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
908 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
909 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
910 8da9e5f4 2019-01-12 stsp {
911 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
912 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
913 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
914 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
915 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
916 30a076bc 2019-07-27 stsp cb, cb_arg);
917 d1f6d47b 2019-02-04 stsp }
918 d1f6d47b 2019-02-04 stsp
919 d1f6d47b 2019-02-04 stsp static const struct got_error *
920 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
921 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
922 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
923 c577a9ce 2019-07-27 stsp
924 c577a9ce 2019-07-27 stsp static const struct got_error *
925 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
926 c577a9ce 2019-07-27 stsp {
927 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
928 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
929 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
930 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
931 c577a9ce 2019-07-27 stsp
932 c577a9ce 2019-07-27 stsp for (;;) {
933 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
934 c577a9ce 2019-07-27 stsp if (de == NULL) {
935 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
936 c577a9ce 2019-07-27 stsp break;
937 c577a9ce 2019-07-27 stsp }
938 c577a9ce 2019-07-27 stsp
939 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
940 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
941 c577a9ce 2019-07-27 stsp free(de);
942 c577a9ce 2019-07-27 stsp break;
943 c577a9ce 2019-07-27 stsp }
944 c577a9ce 2019-07-27 stsp if (dep == NULL) {
945 c577a9ce 2019-07-27 stsp free(de);
946 c577a9ce 2019-07-27 stsp break;
947 c577a9ce 2019-07-27 stsp }
948 c577a9ce 2019-07-27 stsp
949 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
950 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
951 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
952 ad10f64e 2023-07-19 thomas strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
953 ad10f64e 2023-07-19 thomas (path[0] == '\0' &&
954 ad10f64e 2023-07-19 thomas strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
955 c577a9ce 2019-07-27 stsp free(de);
956 c577a9ce 2019-07-27 stsp continue;
957 c577a9ce 2019-07-27 stsp }
958 c577a9ce 2019-07-27 stsp
959 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
960 c577a9ce 2019-07-27 stsp if (err) {
961 c577a9ce 2019-07-27 stsp free(de);
962 c577a9ce 2019-07-27 stsp break;
963 c577a9ce 2019-07-27 stsp }
964 c577a9ce 2019-07-27 stsp if (new == NULL) {
965 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
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 }
970 500cd40f 2019-02-04 stsp
971 c577a9ce 2019-07-27 stsp return err;
972 c577a9ce 2019-07-27 stsp }
973 8528beef 2021-10-15 thomas
974 8528beef 2021-10-15 thomas static int
975 8528beef 2021-10-15 thomas have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
976 8528beef 2021-10-15 thomas {
977 8528beef 2021-10-15 thomas struct got_fileindex_entry *ie;
978 8528beef 2021-10-15 thomas size_t path_len = strlen(path);
979 8528beef 2021-10-15 thomas int cmp;
980 c577a9ce 2019-07-27 stsp
981 8528beef 2021-10-15 thomas ie = RB_ROOT(&fileindex->entries);
982 8528beef 2021-10-15 thomas while (ie) {
983 8528beef 2021-10-15 thomas if (got_path_is_child(ie->path, path, path_len))
984 8528beef 2021-10-15 thomas return 1;
985 8528beef 2021-10-15 thomas cmp = got_path_cmp(path, ie->path, path_len,
986 8528beef 2021-10-15 thomas got_fileindex_entry_path_len(ie));
987 8528beef 2021-10-15 thomas if (cmp < 0)
988 8528beef 2021-10-15 thomas ie = RB_LEFT(ie, entry);
989 8528beef 2021-10-15 thomas else if (cmp > 0)
990 8528beef 2021-10-15 thomas ie = RB_RIGHT(ie, entry);
991 8528beef 2021-10-15 thomas else
992 8528beef 2021-10-15 thomas break;
993 8528beef 2021-10-15 thomas }
994 8528beef 2021-10-15 thomas
995 8528beef 2021-10-15 thomas return 0;
996 8528beef 2021-10-15 thomas }
997 8528beef 2021-10-15 thomas
998 d1f6d47b 2019-02-04 stsp static const struct got_error *
999 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1000 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1001 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
1002 6092c299 2021-10-04 thomas int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1003 d1f6d47b 2019-02-04 stsp {
1004 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1005 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
1006 965988c5 2019-12-16 stsp DIR *subdir = NULL;
1007 6fc93f37 2019-12-13 stsp int subdirfd = -1;
1008 573463cc 2019-04-06 stsp
1009 573463cc 2019-04-06 stsp *next = NULL;
1010 20ccae39 2020-07-21 stsp
1011 8528beef 2021-10-15 thomas /* Must traverse ignored directories if they contain tracked files. */
1012 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && ignore &&
1013 8528beef 2021-10-15 thomas have_tracked_file_in_dir(fileindex, path))
1014 8528beef 2021-10-15 thomas ignore = 0;
1015 8528beef 2021-10-15 thomas
1016 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && !ignore) {
1017 d1f6d47b 2019-02-04 stsp char *subpath;
1018 c7f4312f 2019-02-05 stsp char *subdirpath;
1019 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
1020 d1f6d47b 2019-02-04 stsp
1021 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
1022 c577a9ce 2019-07-27 stsp
1023 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
1024 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1025 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1026 d1f6d47b 2019-02-04 stsp
1027 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1028 c7f4312f 2019-02-05 stsp free(subpath);
1029 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1030 c7f4312f 2019-02-05 stsp }
1031 c7f4312f 2019-02-05 stsp
1032 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
1033 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1034 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
1035 40b289d7 2019-09-07 stsp if (errno == EACCES) {
1036 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
1037 40b289d7 2019-09-07 stsp return NULL;
1038 40b289d7 2019-09-07 stsp }
1039 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1040 d1f6d47b 2019-02-04 stsp free(subpath);
1041 c7f4312f 2019-02-05 stsp free(subdirpath);
1042 ef5e02fd 2019-08-11 stsp return err;
1043 d1f6d47b 2019-02-04 stsp }
1044 d1f6d47b 2019-02-04 stsp
1045 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1046 965988c5 2019-12-16 stsp if (subdir == NULL)
1047 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
1048 965988c5 2019-12-16 stsp subdirfd = -1;
1049 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1050 c577a9ce 2019-07-27 stsp if (err) {
1051 c577a9ce 2019-07-27 stsp free(subpath);
1052 c577a9ce 2019-07-27 stsp free(subdirpath);
1053 965988c5 2019-12-16 stsp closedir(subdir);
1054 c577a9ce 2019-07-27 stsp return err;
1055 c577a9ce 2019-07-27 stsp }
1056 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1057 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1058 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1059 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1060 d1f6d47b 2019-02-04 stsp free(subpath);
1061 c7f4312f 2019-02-05 stsp free(subdirpath);
1062 21c2d8be 2023-01-10 thomas got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1063 d1f6d47b 2019-02-04 stsp if (err)
1064 d1f6d47b 2019-02-04 stsp return err;
1065 d1f6d47b 2019-02-04 stsp }
1066 d1f6d47b 2019-02-04 stsp
1067 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1068 d1f6d47b 2019-02-04 stsp return NULL;
1069 d1f6d47b 2019-02-04 stsp }
1070 d1f6d47b 2019-02-04 stsp
1071 d1f6d47b 2019-02-04 stsp static const struct got_error *
1072 1f480907 2022-01-27 thomas dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1073 1f480907 2022-01-27 thomas {
1074 1f480907 2022-01-27 thomas const struct got_error *err;
1075 1f480907 2022-01-27 thomas char *dir_path;
1076 1f480907 2022-01-27 thomas int type;
1077 1f480907 2022-01-27 thomas
1078 1f480907 2022-01-27 thomas if (de->d_type != DT_UNKNOWN)
1079 1f480907 2022-01-27 thomas return NULL;
1080 1f480907 2022-01-27 thomas
1081 1f480907 2022-01-27 thomas /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1082 1f480907 2022-01-27 thomas if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1083 1f480907 2022-01-27 thomas return got_error_from_errno("asprintf");
1084 1f480907 2022-01-27 thomas err = got_path_dirent_type(&type, dir_path, de);
1085 1f480907 2022-01-27 thomas free(dir_path);
1086 1f480907 2022-01-27 thomas if (err)
1087 1f480907 2022-01-27 thomas return err;
1088 1f480907 2022-01-27 thomas
1089 1f480907 2022-01-27 thomas de->d_type = type;
1090 1f480907 2022-01-27 thomas return NULL;
1091 1f480907 2022-01-27 thomas }
1092 1f480907 2022-01-27 thomas
1093 1f480907 2022-01-27 thomas static const struct got_error *
1094 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1095 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1096 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1097 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1098 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1099 d1f6d47b 2019-02-04 stsp {
1100 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1101 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1102 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1103 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1104 6092c299 2021-10-04 thomas int ignore;
1105 3143d852 2020-06-25 stsp
1106 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1107 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1108 3143d852 2020-06-25 stsp if (err)
1109 3143d852 2020-06-25 stsp return err;
1110 3143d852 2020-06-25 stsp }
1111 d1f6d47b 2019-02-04 stsp
1112 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1113 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1114 500cd40f 2019-02-04 stsp if (dle && *ie) {
1115 18831e78 2019-02-10 stsp char *de_path;
1116 e7a2f030 2019-02-05 stsp int cmp;
1117 f5d3d7af 2019-02-05 stsp de = dle->data;
1118 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1119 1f480907 2022-01-27 thomas if (err)
1120 1f480907 2022-01-27 thomas break;
1121 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1122 18831e78 2019-02-10 stsp de->d_name) == -1) {
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1124 13ff9e90 2019-02-10 stsp break;
1125 18831e78 2019-02-10 stsp }
1126 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1127 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1128 dd038bc6 2021-09-21 thomas.ad strlen(path) + 1 + strlen(de->d_name));
1129 18831e78 2019-02-10 stsp free(de_path);
1130 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1131 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1132 7f91a133 2019-12-13 stsp dirfd);
1133 d1f6d47b 2019-02-04 stsp if (err)
1134 d1f6d47b 2019-02-04 stsp break;
1135 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1136 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1137 6092c299 2021-10-04 thomas path, rootpath, repo, 0, cb, cb_arg);
1138 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1139 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1140 d1f6d47b 2019-02-04 stsp if (err)
1141 d1f6d47b 2019-02-04 stsp break;
1142 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1143 d1f6d47b 2019-02-04 stsp } else {
1144 6092c299 2021-10-04 thomas err = cb->diff_new(&ignore, cb_arg, de, path,
1145 6092c299 2021-10-04 thomas dirfd);
1146 d1f6d47b 2019-02-04 stsp if (err)
1147 d1f6d47b 2019-02-04 stsp break;
1148 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1149 6092c299 2021-10-04 thomas path, rootpath, repo, ignore, cb, cb_arg);
1150 d1f6d47b 2019-02-04 stsp }
1151 554b91b1 2019-02-04 stsp if (err)
1152 554b91b1 2019-02-04 stsp break;
1153 554b91b1 2019-02-04 stsp } else if (*ie) {
1154 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1155 554b91b1 2019-02-04 stsp if (err)
1156 554b91b1 2019-02-04 stsp break;
1157 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1158 554b91b1 2019-02-04 stsp } else if (dle) {
1159 763e1377 2019-02-05 stsp de = dle->data;
1160 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1161 1f480907 2022-01-27 thomas if (err)
1162 1f480907 2022-01-27 thomas break;
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 b6b86fd1 2022-08-30 thomas /*
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 21c2d8be 2023-01-10 thomas got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1222 c577a9ce 2019-07-27 stsp return err;
1223 d1f6d47b 2019-02-04 stsp }
1224 d1f6d47b 2019-02-04 stsp
1225 43010591 2023-02-17 thomas struct got_object_id *
1226 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1227 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1228 43010591 2023-02-17 thomas {
1229 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1230 43010591 2023-02-17 thomas memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1231 43010591 2023-02-17 thomas return id;
1232 43010591 2023-02-17 thomas }
1233 43010591 2023-02-17 thomas
1234 43010591 2023-02-17 thomas struct got_object_id *
1235 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(struct got_object_id *id,
1236 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1237 43010591 2023-02-17 thomas {
1238 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1239 43010591 2023-02-17 thomas memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1240 43010591 2023-02-17 thomas return id;
1241 43010591 2023-02-17 thomas }
1242 43010591 2023-02-17 thomas
1243 43010591 2023-02-17 thomas struct got_object_id *
1244 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(struct got_object_id *id,
1245 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1246 43010591 2023-02-17 thomas {
1247 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1248 43010591 2023-02-17 thomas memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1249 43010591 2023-02-17 thomas return id;
1250 43010591 2023-02-17 thomas }
1251 43010591 2023-02-17 thomas
1252 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);