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