Blame


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