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 03df25b3 2019-05-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 984c073d 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, 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 3f762da0 2019-08-03 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
268 133d2798 2019-01-08 stsp fileindex->nentries++;
269 133d2798 2019-01-08 stsp return NULL;
270 50952927 2019-01-12 stsp }
271 50952927 2019-01-12 stsp
272 50952927 2019-01-12 stsp const struct got_error *
273 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
274 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
275 50952927 2019-01-12 stsp {
276 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
277 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
278 50952927 2019-01-12 stsp
279 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
280 9d31a1d8 2018-03-11 stsp }
281 9d31a1d8 2018-03-11 stsp
282 133d2798 2019-01-08 stsp void
283 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
284 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
285 512f0d0e 2019-01-02 stsp {
286 a46b9f33 2020-01-28 stsp /*
287 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
288 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
289 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
290 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
291 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
292 a46b9f33 2020-01-28 stsp */
293 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
294 133d2798 2019-01-08 stsp fileindex->nentries--;
295 512f0d0e 2019-01-02 stsp }
296 512f0d0e 2019-01-02 stsp
297 51514078 2018-12-25 stsp struct got_fileindex_entry *
298 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
299 d6c87207 2019-08-02 stsp size_t path_len)
300 51514078 2018-12-25 stsp {
301 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
302 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
303 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
304 133d2798 2019-01-08 stsp key.path = (char *)path;
305 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
306 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
307 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
308 a46b9f33 2020-01-28 stsp return NULL;
309 a46b9f33 2020-01-28 stsp return ie;
310 b504a804 2019-01-08 stsp }
311 51514078 2018-12-25 stsp
312 512f0d0e 2019-01-02 stsp const struct got_error *
313 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
314 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
315 512f0d0e 2019-01-02 stsp {
316 133d2798 2019-01-08 stsp const struct got_error *err;
317 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
318 9d31a1d8 2018-03-11 stsp
319 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
320 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
321 a46b9f33 2020-01-28 stsp continue;
322 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
323 133d2798 2019-01-08 stsp if (err)
324 133d2798 2019-01-08 stsp return err;
325 b504a804 2019-01-08 stsp }
326 b504a804 2019-01-08 stsp return NULL;
327 9d31a1d8 2018-03-11 stsp }
328 9d31a1d8 2018-03-11 stsp
329 133d2798 2019-01-08 stsp struct got_fileindex *
330 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
331 6b798c3c 2019-01-08 stsp {
332 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
333 133d2798 2019-01-08 stsp
334 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
335 133d2798 2019-01-08 stsp if (fileindex == NULL)
336 133d2798 2019-01-08 stsp return NULL;
337 133d2798 2019-01-08 stsp
338 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
339 133d2798 2019-01-08 stsp return fileindex;
340 6b798c3c 2019-01-08 stsp }
341 6b798c3c 2019-01-08 stsp
342 9d31a1d8 2018-03-11 stsp void
343 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
344 9d31a1d8 2018-03-11 stsp {
345 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
346 133d2798 2019-01-08 stsp
347 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
348 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
349 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
350 133d2798 2019-01-08 stsp }
351 9d31a1d8 2018-03-11 stsp free(fileindex);
352 9d31a1d8 2018-03-11 stsp }
353 9d31a1d8 2018-03-11 stsp
354 c34b20a2 2018-03-12 stsp static const struct got_error *
355 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
356 c34b20a2 2018-03-12 stsp {
357 c34b20a2 2018-03-12 stsp size_t n;
358 c34b20a2 2018-03-12 stsp
359 c34b20a2 2018-03-12 stsp val = htobe64(val);
360 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
361 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
362 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
363 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
364 c34b20a2 2018-03-12 stsp return NULL;
365 c34b20a2 2018-03-12 stsp }
366 c34b20a2 2018-03-12 stsp
367 c34b20a2 2018-03-12 stsp static const struct got_error *
368 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
369 c34b20a2 2018-03-12 stsp {
370 c34b20a2 2018-03-12 stsp size_t n;
371 c34b20a2 2018-03-12 stsp
372 c34b20a2 2018-03-12 stsp val = htobe32(val);
373 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
374 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
375 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
376 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
377 c34b20a2 2018-03-12 stsp return NULL;
378 c34b20a2 2018-03-12 stsp }
379 c34b20a2 2018-03-12 stsp
380 c34b20a2 2018-03-12 stsp static const struct got_error *
381 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
382 c34b20a2 2018-03-12 stsp {
383 c34b20a2 2018-03-12 stsp size_t n;
384 c34b20a2 2018-03-12 stsp
385 c34b20a2 2018-03-12 stsp val = htobe16(val);
386 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
387 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
388 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
389 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
390 c34b20a2 2018-03-12 stsp return NULL;
391 c34b20a2 2018-03-12 stsp }
392 c34b20a2 2018-03-12 stsp
393 c34b20a2 2018-03-12 stsp static const struct got_error *
394 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
395 c34b20a2 2018-03-12 stsp {
396 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
397 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
398 c34b20a2 2018-03-12 stsp
399 c34b20a2 2018-03-12 stsp len = strlen(path);
400 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
401 3c5b70f2 2018-12-27 stsp pad++;
402 3c5b70f2 2018-12-27 stsp if (pad == 0)
403 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
404 c34b20a2 2018-03-12 stsp
405 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
406 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
407 c34b20a2 2018-03-12 stsp if (n != len)
408 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
409 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
410 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
411 c34b20a2 2018-03-12 stsp if (n != pad)
412 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
413 c34b20a2 2018-03-12 stsp return NULL;
414 c34b20a2 2018-03-12 stsp }
415 c34b20a2 2018-03-12 stsp
416 c34b20a2 2018-03-12 stsp static const struct got_error *
417 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
418 c34b20a2 2018-03-12 stsp FILE *outfile)
419 c34b20a2 2018-03-12 stsp {
420 c34b20a2 2018-03-12 stsp const struct got_error *err;
421 23b19d00 2018-03-12 stsp size_t n;
422 df335242 2019-08-03 stsp uint32_t stage;
423 c34b20a2 2018-03-12 stsp
424 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
425 c34b20a2 2018-03-12 stsp if (err)
426 c34b20a2 2018-03-12 stsp return err;
427 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, 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->mtime_sec, 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_nsec, outfile);
434 c34b20a2 2018-03-12 stsp if (err)
435 c34b20a2 2018-03-12 stsp return err;
436 c34b20a2 2018-03-12 stsp
437 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
438 c34b20a2 2018-03-12 stsp if (err)
439 c34b20a2 2018-03-12 stsp return err;
440 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, 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->size, outfile);
444 c34b20a2 2018-03-12 stsp if (err)
445 c34b20a2 2018-03-12 stsp return err;
446 c34b20a2 2018-03-12 stsp
447 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
448 c34b20a2 2018-03-12 stsp if (err)
449 c34b20a2 2018-03-12 stsp return err;
450 c34b20a2 2018-03-12 stsp
451 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
452 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
453 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
454 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
455 fc76cabb 2018-12-25 stsp
456 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
457 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
458 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
459 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
460 c34b20a2 2018-03-12 stsp
461 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
462 c34b20a2 2018-03-12 stsp if (err)
463 c34b20a2 2018-03-12 stsp return err;
464 c34b20a2 2018-03-12 stsp
465 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
466 df335242 2019-08-03 stsp if (err)
467 df335242 2019-08-03 stsp return err;
468 df335242 2019-08-03 stsp
469 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
470 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
471 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
472 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
473 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
474 df335242 2019-08-03 stsp outfile);
475 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
476 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
477 df335242 2019-08-03 stsp }
478 df335242 2019-08-03 stsp
479 df335242 2019-08-03 stsp return NULL;
480 c34b20a2 2018-03-12 stsp }
481 c34b20a2 2018-03-12 stsp
482 9d31a1d8 2018-03-11 stsp const struct got_error *
483 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
484 9d31a1d8 2018-03-11 stsp {
485 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
486 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
487 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
488 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
489 c34b20a2 2018-03-12 stsp size_t n;
490 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
491 c34b20a2 2018-03-12 stsp
492 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
493 c34b20a2 2018-03-12 stsp
494 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
495 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
496 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
497 c34b20a2 2018-03-12 stsp
498 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
499 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
500 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
501 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
502 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
503 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
504 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
505 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
506 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
507 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
508 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
509 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
510 c34b20a2 2018-03-12 stsp
511 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
512 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
513 a769b60b 2021-06-27 stsp ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
514 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
515 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
516 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
517 a46b9f33 2020-01-28 stsp continue;
518 8bd8568c 2020-04-24 stsp }
519 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
520 133d2798 2019-01-08 stsp if (err)
521 133d2798 2019-01-08 stsp return err;
522 133d2798 2019-01-08 stsp }
523 c34b20a2 2018-03-12 stsp
524 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
525 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
526 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
527 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
528 52a74475 2018-12-24 stsp
529 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
530 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
531 27d0e5bd 2019-01-12 stsp
532 52a74475 2018-12-24 stsp return NULL;
533 52a74475 2018-12-24 stsp }
534 52a74475 2018-12-24 stsp
535 52a74475 2018-12-24 stsp static const struct got_error *
536 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
537 52a74475 2018-12-24 stsp {
538 52a74475 2018-12-24 stsp size_t n;
539 52a74475 2018-12-24 stsp
540 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
541 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
542 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
544 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
545 52a74475 2018-12-24 stsp return NULL;
546 52a74475 2018-12-24 stsp }
547 52a74475 2018-12-24 stsp
548 52a74475 2018-12-24 stsp static const struct got_error *
549 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
550 52a74475 2018-12-24 stsp {
551 52a74475 2018-12-24 stsp size_t n;
552 52a74475 2018-12-24 stsp
553 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
554 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
555 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
556 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
557 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
558 52a74475 2018-12-24 stsp return NULL;
559 52a74475 2018-12-24 stsp }
560 52a74475 2018-12-24 stsp
561 52a74475 2018-12-24 stsp static const struct got_error *
562 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
563 52a74475 2018-12-24 stsp {
564 52a74475 2018-12-24 stsp size_t n;
565 52a74475 2018-12-24 stsp
566 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
567 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
568 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
569 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
570 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
571 52a74475 2018-12-24 stsp return NULL;
572 52a74475 2018-12-24 stsp }
573 52a74475 2018-12-24 stsp
574 52a74475 2018-12-24 stsp static const struct got_error *
575 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
576 52a74475 2018-12-24 stsp {
577 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
578 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
579 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
580 52a74475 2018-12-24 stsp
581 52a74475 2018-12-24 stsp *path = malloc(totlen);
582 52a74475 2018-12-24 stsp if (*path == NULL)
583 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
584 52a74475 2018-12-24 stsp
585 52a74475 2018-12-24 stsp do {
586 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
587 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
588 52a74475 2018-12-24 stsp if (p == NULL) {
589 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
590 52a74475 2018-12-24 stsp break;
591 52a74475 2018-12-24 stsp }
592 6bf2c316 2019-08-02 stsp totlen += chunk_size;
593 52a74475 2018-12-24 stsp *path = p;
594 52a74475 2018-12-24 stsp }
595 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
596 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
597 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
598 6bf2c316 2019-08-02 stsp break;
599 6bf2c316 2019-08-02 stsp }
600 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
601 6bf2c316 2019-08-02 stsp len += chunk_size;
602 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
603 52a74475 2018-12-24 stsp
604 52a74475 2018-12-24 stsp if (err) {
605 52a74475 2018-12-24 stsp free(*path);
606 52a74475 2018-12-24 stsp *path = NULL;
607 52a74475 2018-12-24 stsp }
608 52a74475 2018-12-24 stsp return err;
609 52a74475 2018-12-24 stsp }
610 52a74475 2018-12-24 stsp
611 52a74475 2018-12-24 stsp static const struct got_error *
612 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
613 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
614 52a74475 2018-12-24 stsp {
615 52a74475 2018-12-24 stsp const struct got_error *err;
616 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
617 52a74475 2018-12-24 stsp size_t n;
618 52a74475 2018-12-24 stsp
619 3f762da0 2019-08-03 stsp *iep = NULL;
620 52a74475 2018-12-24 stsp
621 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
622 3f762da0 2019-08-03 stsp if (ie == NULL)
623 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
624 c34b20a2 2018-03-12 stsp
625 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
626 52a74475 2018-12-24 stsp if (err)
627 52a74475 2018-12-24 stsp goto done;
628 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, 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->mtime_sec, 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_nsec, ctx, infile);
635 52a74475 2018-12-24 stsp if (err)
636 52a74475 2018-12-24 stsp goto done;
637 52a74475 2018-12-24 stsp
638 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
639 52a74475 2018-12-24 stsp if (err)
640 52a74475 2018-12-24 stsp goto done;
641 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, 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->size, ctx, infile);
645 52a74475 2018-12-24 stsp if (err)
646 52a74475 2018-12-24 stsp goto done;
647 52a74475 2018-12-24 stsp
648 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
649 52a74475 2018-12-24 stsp if (err)
650 52a74475 2018-12-24 stsp goto done;
651 52a74475 2018-12-24 stsp
652 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
653 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
654 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
655 52a74475 2018-12-24 stsp goto done;
656 52a74475 2018-12-24 stsp }
657 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
658 52a74475 2018-12-24 stsp
659 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
660 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
661 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
662 fc76cabb 2018-12-25 stsp goto done;
663 fc76cabb 2018-12-25 stsp }
664 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
665 fc76cabb 2018-12-25 stsp
666 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
667 52a74475 2018-12-24 stsp if (err)
668 52a74475 2018-12-24 stsp goto done;
669 52a74475 2018-12-24 stsp
670 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
671 df335242 2019-08-03 stsp if (err)
672 df335242 2019-08-03 stsp goto done;
673 df335242 2019-08-03 stsp
674 df335242 2019-08-03 stsp if (version >= 2) {
675 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
676 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
677 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
678 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
679 df335242 2019-08-03 stsp infile);
680 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
681 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
682 df335242 2019-08-03 stsp goto done;
683 df335242 2019-08-03 stsp }
684 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
685 df335242 2019-08-03 stsp }
686 df335242 2019-08-03 stsp } else {
687 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
688 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
689 df335242 2019-08-03 stsp }
690 df335242 2019-08-03 stsp
691 52a74475 2018-12-24 stsp done:
692 52a74475 2018-12-24 stsp if (err)
693 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
694 52a74475 2018-12-24 stsp else
695 3f762da0 2019-08-03 stsp *iep = ie;
696 52a74475 2018-12-24 stsp return err;
697 52a74475 2018-12-24 stsp }
698 52a74475 2018-12-24 stsp
699 52a74475 2018-12-24 stsp const struct got_error *
700 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
701 52a74475 2018-12-24 stsp {
702 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
703 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
704 52a74475 2018-12-24 stsp SHA1_CTX ctx;
705 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
706 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
707 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
708 52a74475 2018-12-24 stsp size_t n;
709 52a74475 2018-12-24 stsp int i;
710 52a74475 2018-12-24 stsp
711 52a74475 2018-12-24 stsp SHA1Init(&ctx);
712 52a74475 2018-12-24 stsp
713 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
714 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
715 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
716 b6d05318 2019-01-13 stsp return NULL;
717 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
718 b6d05318 2019-01-13 stsp }
719 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
720 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
721 51514078 2018-12-25 stsp if (n == 0) /* EOF */
722 51514078 2018-12-25 stsp return NULL;
723 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
724 51514078 2018-12-25 stsp }
725 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
726 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
727 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
728 b6d05318 2019-01-13 stsp return NULL;
729 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
730 b6d05318 2019-01-13 stsp }
731 52a74475 2018-12-24 stsp
732 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
733 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
734 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
735 52a74475 2018-12-24 stsp
736 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
737 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
738 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
739 52a74475 2018-12-24 stsp
740 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
741 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
742 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
743 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
744 52a74475 2018-12-24 stsp
745 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
746 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
747 52a74475 2018-12-24 stsp if (err)
748 52a74475 2018-12-24 stsp return err;
749 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
750 52a74475 2018-12-24 stsp if (err)
751 52a74475 2018-12-24 stsp return err;
752 52a74475 2018-12-24 stsp }
753 52a74475 2018-12-24 stsp
754 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
755 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
756 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
757 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
758 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
759 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
760 52a74475 2018-12-24 stsp
761 9d31a1d8 2018-03-11 stsp return NULL;
762 8da9e5f4 2019-01-12 stsp }
763 8da9e5f4 2019-01-12 stsp
764 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
765 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
766 50952927 2019-01-12 stsp {
767 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
768 50952927 2019-01-12 stsp
769 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
770 50952927 2019-01-12 stsp
771 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
772 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
773 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
774 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
775 50952927 2019-01-12 stsp
776 50952927 2019-01-12 stsp return next;
777 50952927 2019-01-12 stsp }
778 50952927 2019-01-12 stsp
779 8da9e5f4 2019-01-12 stsp static const struct got_error *
780 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
781 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
782 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
783 9d2a8e53 2019-01-28 stsp
784 9d2a8e53 2019-01-28 stsp static const struct got_error *
785 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
786 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
787 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
788 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
789 8da9e5f4 2019-01-12 stsp {
790 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
791 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
792 8da9e5f4 2019-01-12 stsp
793 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
794 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
795 8da9e5f4 2019-01-12 stsp char *subpath;
796 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
797 8da9e5f4 2019-01-12 stsp
798 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
799 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
800 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
801 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
802 8da9e5f4 2019-01-12 stsp
803 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
804 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
805 8da9e5f4 2019-01-12 stsp if (err) {
806 8da9e5f4 2019-01-12 stsp free(subpath);
807 8da9e5f4 2019-01-12 stsp return err;
808 8da9e5f4 2019-01-12 stsp }
809 8da9e5f4 2019-01-12 stsp
810 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
811 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
812 8da9e5f4 2019-01-12 stsp free(subpath);
813 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
814 8da9e5f4 2019-01-12 stsp if (err)
815 8da9e5f4 2019-01-12 stsp return err;
816 8da9e5f4 2019-01-12 stsp }
817 8da9e5f4 2019-01-12 stsp
818 56e0773d 2019-11-28 stsp (*tidx)++;
819 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
820 8da9e5f4 2019-01-12 stsp return NULL;
821 8da9e5f4 2019-01-12 stsp }
822 8da9e5f4 2019-01-12 stsp
823 8da9e5f4 2019-01-12 stsp static const struct got_error *
824 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
825 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
826 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
827 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
828 8da9e5f4 2019-01-12 stsp {
829 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
830 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
831 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
832 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
833 56e0773d 2019-11-28 stsp int tidx = 0;
834 8da9e5f4 2019-01-12 stsp
835 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
836 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
837 8da9e5f4 2019-01-12 stsp if (te && *ie) {
838 18831e78 2019-02-10 stsp char *te_path;
839 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
840 18831e78 2019-02-10 stsp int cmp;
841 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
842 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
843 18831e78 2019-02-10 stsp break;
844 18831e78 2019-02-10 stsp }
845 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
846 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
847 18831e78 2019-02-10 stsp free(te_path);
848 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
849 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
850 63c5ca5d 2019-08-24 stsp path_len) &&
851 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
852 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
853 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
854 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
855 c4cdcb68 2019-04-03 stsp path);
856 c4cdcb68 2019-04-03 stsp if (err || entry_name)
857 c4cdcb68 2019-04-03 stsp break;
858 c4cdcb68 2019-04-03 stsp }
859 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
860 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
861 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
862 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
863 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
864 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
865 194cb7cb 2021-01-19 stsp path_len) && entry_name == NULL) {
866 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
867 c4cdcb68 2019-04-03 stsp if (err || entry_name)
868 c4cdcb68 2019-04-03 stsp break;
869 c4cdcb68 2019-04-03 stsp }
870 8da9e5f4 2019-01-12 stsp *ie = next;
871 8da9e5f4 2019-01-12 stsp } else {
872 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
873 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
874 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
875 c4cdcb68 2019-04-03 stsp if (err || entry_name)
876 c4cdcb68 2019-04-03 stsp break;
877 c4cdcb68 2019-04-03 stsp }
878 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
879 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
880 8da9e5f4 2019-01-12 stsp }
881 8da9e5f4 2019-01-12 stsp if (err)
882 8da9e5f4 2019-01-12 stsp break;
883 8da9e5f4 2019-01-12 stsp } else if (*ie) {
884 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
885 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
886 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
887 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
888 56e0773d 2019-11-28 stsp entry_name) == 0))) {
889 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
890 c4cdcb68 2019-04-03 stsp if (err || entry_name)
891 c4cdcb68 2019-04-03 stsp break;
892 c4cdcb68 2019-04-03 stsp }
893 8da9e5f4 2019-01-12 stsp *ie = next;
894 8da9e5f4 2019-01-12 stsp } else if (te) {
895 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
896 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
897 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
898 56e0773d 2019-11-28 stsp == 0)) {
899 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
900 c4cdcb68 2019-04-03 stsp if (err || entry_name)
901 c4cdcb68 2019-04-03 stsp break;
902 c4cdcb68 2019-04-03 stsp }
903 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
904 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
905 8da9e5f4 2019-01-12 stsp if (err)
906 8da9e5f4 2019-01-12 stsp break;
907 8da9e5f4 2019-01-12 stsp }
908 500cd40f 2019-02-04 stsp }
909 8da9e5f4 2019-01-12 stsp
910 8da9e5f4 2019-01-12 stsp return err;
911 8da9e5f4 2019-01-12 stsp }
912 8da9e5f4 2019-01-12 stsp
913 8da9e5f4 2019-01-12 stsp const struct got_error *
914 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
915 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
916 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
917 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
918 8da9e5f4 2019-01-12 stsp {
919 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
920 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
921 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
922 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
923 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
924 30a076bc 2019-07-27 stsp cb, cb_arg);
925 d1f6d47b 2019-02-04 stsp }
926 d1f6d47b 2019-02-04 stsp
927 d1f6d47b 2019-02-04 stsp static const struct got_error *
928 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
929 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
930 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
931 c577a9ce 2019-07-27 stsp
932 c577a9ce 2019-07-27 stsp static const struct got_error *
933 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
934 c577a9ce 2019-07-27 stsp {
935 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
936 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
937 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
938 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
939 c577a9ce 2019-07-27 stsp
940 c577a9ce 2019-07-27 stsp for (;;) {
941 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
942 c577a9ce 2019-07-27 stsp if (de == NULL) {
943 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
944 c577a9ce 2019-07-27 stsp break;
945 c577a9ce 2019-07-27 stsp }
946 c577a9ce 2019-07-27 stsp
947 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
948 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
949 c577a9ce 2019-07-27 stsp free(de);
950 c577a9ce 2019-07-27 stsp break;
951 c577a9ce 2019-07-27 stsp }
952 c577a9ce 2019-07-27 stsp if (dep == NULL) {
953 c577a9ce 2019-07-27 stsp free(de);
954 c577a9ce 2019-07-27 stsp break;
955 c577a9ce 2019-07-27 stsp }
956 c577a9ce 2019-07-27 stsp
957 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
958 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
959 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
960 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
961 c577a9ce 2019-07-27 stsp free(de);
962 c577a9ce 2019-07-27 stsp continue;
963 c577a9ce 2019-07-27 stsp }
964 c577a9ce 2019-07-27 stsp
965 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
966 c577a9ce 2019-07-27 stsp if (err) {
967 c577a9ce 2019-07-27 stsp free(de);
968 c577a9ce 2019-07-27 stsp break;
969 c577a9ce 2019-07-27 stsp }
970 c577a9ce 2019-07-27 stsp if (new == NULL) {
971 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
972 c577a9ce 2019-07-27 stsp free(de);
973 c577a9ce 2019-07-27 stsp break;
974 c577a9ce 2019-07-27 stsp }
975 c577a9ce 2019-07-27 stsp }
976 500cd40f 2019-02-04 stsp
977 c577a9ce 2019-07-27 stsp return err;
978 c577a9ce 2019-07-27 stsp }
979 c577a9ce 2019-07-27 stsp
980 c577a9ce 2019-07-27 stsp void
981 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
982 c577a9ce 2019-07-27 stsp {
983 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
984 c577a9ce 2019-07-27 stsp
985 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
986 c577a9ce 2019-07-27 stsp free(dle->data);
987 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
988 c577a9ce 2019-07-27 stsp }
989 c577a9ce 2019-07-27 stsp
990 d1f6d47b 2019-02-04 stsp static const struct got_error *
991 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
992 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
993 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
994 62da3196 2021-10-01 stsp int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
995 d1f6d47b 2019-02-04 stsp {
996 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
997 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
998 965988c5 2019-12-16 stsp DIR *subdir = NULL;
999 6fc93f37 2019-12-13 stsp int subdirfd = -1;
1000 20ccae39 2020-07-21 stsp int type;
1001 573463cc 2019-04-06 stsp
1002 573463cc 2019-04-06 stsp *next = NULL;
1003 d1f6d47b 2019-02-04 stsp
1004 20ccae39 2020-07-21 stsp if (de->d_type == DT_UNKNOWN) {
1005 20ccae39 2020-07-21 stsp /* Occurs on NFS mounts without "readdir plus" RPC. */
1006 20ccae39 2020-07-21 stsp char *dir_path;
1007 20ccae39 2020-07-21 stsp if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1008 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
1009 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, dir_path, de);
1010 20ccae39 2020-07-21 stsp free(dir_path);
1011 20ccae39 2020-07-21 stsp if (err)
1012 20ccae39 2020-07-21 stsp return err;
1013 20ccae39 2020-07-21 stsp } else
1014 20ccae39 2020-07-21 stsp type = de->d_type;
1015 20ccae39 2020-07-21 stsp
1016 62da3196 2021-10-01 stsp if (type == DT_DIR && !ignore) {
1017 d1f6d47b 2019-02-04 stsp char *subpath;
1018 c7f4312f 2019-02-05 stsp char *subdirpath;
1019 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
1020 d1f6d47b 2019-02-04 stsp
1021 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
1022 c577a9ce 2019-07-27 stsp
1023 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
1024 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1025 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1026 d1f6d47b 2019-02-04 stsp
1027 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1028 c7f4312f 2019-02-05 stsp free(subpath);
1029 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1030 c7f4312f 2019-02-05 stsp }
1031 c7f4312f 2019-02-05 stsp
1032 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
1033 6fc93f37 2019-12-13 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1034 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
1035 40b289d7 2019-09-07 stsp if (errno == EACCES) {
1036 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
1037 40b289d7 2019-09-07 stsp return NULL;
1038 40b289d7 2019-09-07 stsp }
1039 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1040 d1f6d47b 2019-02-04 stsp free(subpath);
1041 c7f4312f 2019-02-05 stsp free(subdirpath);
1042 ef5e02fd 2019-08-11 stsp return err;
1043 d1f6d47b 2019-02-04 stsp }
1044 d1f6d47b 2019-02-04 stsp
1045 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1046 965988c5 2019-12-16 stsp if (subdir == NULL)
1047 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
1048 965988c5 2019-12-16 stsp subdirfd = -1;
1049 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1050 c577a9ce 2019-07-27 stsp if (err) {
1051 c577a9ce 2019-07-27 stsp free(subpath);
1052 c577a9ce 2019-07-27 stsp free(subdirpath);
1053 965988c5 2019-12-16 stsp closedir(subdir);
1054 c577a9ce 2019-07-27 stsp return err;
1055 c577a9ce 2019-07-27 stsp }
1056 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1057 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1058 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1059 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1060 d1f6d47b 2019-02-04 stsp free(subpath);
1061 c7f4312f 2019-02-05 stsp free(subdirpath);
1062 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
1063 d1f6d47b 2019-02-04 stsp if (err)
1064 d1f6d47b 2019-02-04 stsp return err;
1065 d1f6d47b 2019-02-04 stsp }
1066 d1f6d47b 2019-02-04 stsp
1067 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1068 d1f6d47b 2019-02-04 stsp return NULL;
1069 d1f6d47b 2019-02-04 stsp }
1070 d1f6d47b 2019-02-04 stsp
1071 d1f6d47b 2019-02-04 stsp static const struct got_error *
1072 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1073 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1074 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1075 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1076 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1077 d1f6d47b 2019-02-04 stsp {
1078 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1079 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1080 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1081 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1082 62da3196 2021-10-01 stsp int ignore;
1083 3143d852 2020-06-25 stsp
1084 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1085 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1086 3143d852 2020-06-25 stsp if (err)
1087 3143d852 2020-06-25 stsp return err;
1088 3143d852 2020-06-25 stsp }
1089 d1f6d47b 2019-02-04 stsp
1090 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1091 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1092 500cd40f 2019-02-04 stsp if (dle && *ie) {
1093 18831e78 2019-02-10 stsp char *de_path;
1094 e7a2f030 2019-02-05 stsp int cmp;
1095 f5d3d7af 2019-02-05 stsp de = dle->data;
1096 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1097 18831e78 2019-02-10 stsp de->d_name) == -1) {
1098 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1099 13ff9e90 2019-02-10 stsp break;
1100 18831e78 2019-02-10 stsp }
1101 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1102 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1103 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
1104 18831e78 2019-02-10 stsp free(de_path);
1105 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1106 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1107 7f91a133 2019-12-13 stsp dirfd);
1108 d1f6d47b 2019-02-04 stsp if (err)
1109 d1f6d47b 2019-02-04 stsp break;
1110 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1111 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1112 62da3196 2021-10-01 stsp path, rootpath, repo, 0, cb, cb_arg);
1113 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1114 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1115 d1f6d47b 2019-02-04 stsp if (err)
1116 d1f6d47b 2019-02-04 stsp break;
1117 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1118 d1f6d47b 2019-02-04 stsp } else {
1119 62da3196 2021-10-01 stsp err = cb->diff_new(&ignore, cb_arg, de, path,
1120 62da3196 2021-10-01 stsp dirfd);
1121 d1f6d47b 2019-02-04 stsp if (err)
1122 d1f6d47b 2019-02-04 stsp break;
1123 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1124 62da3196 2021-10-01 stsp path, rootpath, repo, ignore, cb, cb_arg);
1125 d1f6d47b 2019-02-04 stsp }
1126 554b91b1 2019-02-04 stsp if (err)
1127 554b91b1 2019-02-04 stsp break;
1128 554b91b1 2019-02-04 stsp } else if (*ie) {
1129 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1130 554b91b1 2019-02-04 stsp if (err)
1131 554b91b1 2019-02-04 stsp break;
1132 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1133 554b91b1 2019-02-04 stsp } else if (dle) {
1134 763e1377 2019-02-05 stsp de = dle->data;
1135 62da3196 2021-10-01 stsp err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1136 d1f6d47b 2019-02-04 stsp if (err)
1137 d1f6d47b 2019-02-04 stsp break;
1138 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1139 62da3196 2021-10-01 stsp rootpath, repo, ignore, cb, cb_arg);
1140 554b91b1 2019-02-04 stsp if (err)
1141 554b91b1 2019-02-04 stsp break;
1142 d1f6d47b 2019-02-04 stsp }
1143 500cd40f 2019-02-04 stsp }
1144 c577a9ce 2019-07-27 stsp
1145 d1f6d47b 2019-02-04 stsp return err;
1146 8da9e5f4 2019-01-12 stsp }
1147 8da9e5f4 2019-01-12 stsp
1148 d1f6d47b 2019-02-04 stsp const struct got_error *
1149 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1150 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1151 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1152 d1f6d47b 2019-02-04 stsp {
1153 c577a9ce 2019-07-27 stsp const struct got_error *err;
1154 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1155 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1156 965988c5 2019-12-16 stsp int fd2;
1157 965988c5 2019-12-16 stsp DIR *dir;
1158 c577a9ce 2019-07-27 stsp
1159 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1160 965988c5 2019-12-16 stsp
1161 965988c5 2019-12-16 stsp /*
1162 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1163 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1164 965988c5 2019-12-16 stsp */
1165 965988c5 2019-12-16 stsp fd2 = dup(fd);
1166 965988c5 2019-12-16 stsp if (fd2 == -1)
1167 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1168 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1169 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1170 3dcf3e74 2020-01-28 stsp close(fd2);
1171 3dcf3e74 2020-01-28 stsp return err;
1172 3dcf3e74 2020-01-28 stsp }
1173 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1174 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1175 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1176 3dcf3e74 2020-01-28 stsp close(fd2);
1177 3dcf3e74 2020-01-28 stsp return err;
1178 3dcf3e74 2020-01-28 stsp }
1179 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1180 965988c5 2019-12-16 stsp if (err) {
1181 965988c5 2019-12-16 stsp closedir(dir);
1182 c577a9ce 2019-07-27 stsp return err;
1183 965988c5 2019-12-16 stsp }
1184 965988c5 2019-12-16 stsp
1185 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1186 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1187 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1188 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1189 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1190 965988c5 2019-12-16 stsp
1191 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1192 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1193 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1194 c577a9ce 2019-07-27 stsp return err;
1195 d1f6d47b 2019-02-04 stsp }
1196 d1f6d47b 2019-02-04 stsp
1197 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);