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