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