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