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