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