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