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