Blame


1 c48c4a9c 2018-03-11 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 c48c4a9c 2018-03-11 stsp *
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 c48c4a9c 2018-03-11 stsp *
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c48c4a9c 2018-03-11 stsp */
16 c48c4a9c 2018-03-11 stsp
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
20 c48c4a9c 2018-03-11 stsp
21 03df25b3 2019-05-11 stsp #include <errno.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 6fc93f37 2019-12-13 stsp #include <fcntl.h>
24 c48c4a9c 2018-03-11 stsp #include <stdio.h>
25 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
26 c48c4a9c 2018-03-11 stsp #include <string.h>
27 c48c4a9c 2018-03-11 stsp #include <sha1.h>
28 c34b20a2 2018-03-12 stsp #include <endian.h>
29 133d2798 2019-01-08 stsp #include <limits.h>
30 6fc93f37 2019-12-13 stsp #include <unistd.h>
31 c442a90d 2019-03-10 stsp #include <uuid.h>
32 c48c4a9c 2018-03-11 stsp
33 c48c4a9c 2018-03-11 stsp #include "got_error.h"
34 8da9e5f4 2019-01-12 stsp #include "got_object.h"
35 324d37e7 2019-05-11 stsp #include "got_path.h"
36 c48c4a9c 2018-03-11 stsp
37 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
38 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
39 c48c4a9c 2018-03-11 stsp
40 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
43 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 a46b9f33 2020-01-28 stsp #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
49 eb983b4b 2019-03-26 stsp
50 133d2798 2019-01-08 stsp struct got_fileindex {
51 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
52 a46b9f33 2020-01-28 stsp int nentries; /* Does not include entries marked for removal. */
53 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 133d2798 2019-01-08 stsp };
55 133d2798 2019-01-08 stsp
56 26a7fe28 2019-07-27 stsp uint16_t
57 26a7fe28 2019-07-27 stsp got_fileindex_perms_from_st(struct stat *sb)
58 26a7fe28 2019-07-27 stsp {
59 26a7fe28 2019-07-27 stsp uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
60 26a7fe28 2019-07-27 stsp return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 26a7fe28 2019-07-27 stsp }
62 26a7fe28 2019-07-27 stsp
63 26a7fe28 2019-07-27 stsp mode_t
64 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
65 26a7fe28 2019-07-27 stsp {
66 26a7fe28 2019-07-27 stsp mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
67 8957ae76 2019-08-08 stsp return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
68 26a7fe28 2019-07-27 stsp }
69 26a7fe28 2019-07-27 stsp
70 c48c4a9c 2018-03-11 stsp const struct got_error *
71 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
72 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
73 02c07007 2019-02-10 stsp int update_timestamps)
74 c48c4a9c 2018-03-11 stsp {
75 c48c4a9c 2018-03-11 stsp struct stat sb;
76 c48c4a9c 2018-03-11 stsp
77 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
78 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
79 b15816dd 2019-08-11 stsp errno == ENOENT))
80 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
81 03df25b3 2019-05-11 stsp } else {
82 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
83 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
84 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
85 03df25b3 2019-05-11 stsp }
86 c48c4a9c 2018-03-11 stsp
87 03df25b3 2019-05-11 stsp
88 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
89 13d9040b 2019-03-27 stsp if (update_timestamps) {
90 3f762da0 2019-08-03 stsp ie->ctime_sec = sb.st_ctime;
91 3f762da0 2019-08-03 stsp ie->ctime_nsec = sb.st_ctimensec;
92 3f762da0 2019-08-03 stsp ie->mtime_sec = sb.st_mtime;
93 3f762da0 2019-08-03 stsp ie->mtime_nsec = sb.st_mtimensec;
94 13d9040b 2019-03-27 stsp }
95 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
96 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
97 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
98 0553429d 2020-06-07 stsp if (S_ISLNK(sb.st_mode))
99 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_SYMLINK;
100 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 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
226 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, 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 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
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 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
448 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
449 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
450 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
451 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
452 a46b9f33 2020-01-28 stsp continue;
453 8bd8568c 2020-04-24 stsp }
454 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
455 133d2798 2019-01-08 stsp if (err)
456 133d2798 2019-01-08 stsp return err;
457 133d2798 2019-01-08 stsp }
458 c34b20a2 2018-03-12 stsp
459 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
460 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
461 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
462 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
463 52a74475 2018-12-24 stsp
464 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
465 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
466 27d0e5bd 2019-01-12 stsp
467 52a74475 2018-12-24 stsp return NULL;
468 52a74475 2018-12-24 stsp }
469 52a74475 2018-12-24 stsp
470 52a74475 2018-12-24 stsp static const struct got_error *
471 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
472 52a74475 2018-12-24 stsp {
473 52a74475 2018-12-24 stsp size_t n;
474 52a74475 2018-12-24 stsp
475 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
476 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
477 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
478 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
479 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
480 52a74475 2018-12-24 stsp return NULL;
481 52a74475 2018-12-24 stsp }
482 52a74475 2018-12-24 stsp
483 52a74475 2018-12-24 stsp static const struct got_error *
484 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
485 52a74475 2018-12-24 stsp {
486 52a74475 2018-12-24 stsp size_t n;
487 52a74475 2018-12-24 stsp
488 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
489 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
490 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
491 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
492 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
493 52a74475 2018-12-24 stsp return NULL;
494 52a74475 2018-12-24 stsp }
495 52a74475 2018-12-24 stsp
496 52a74475 2018-12-24 stsp static const struct got_error *
497 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
498 52a74475 2018-12-24 stsp {
499 52a74475 2018-12-24 stsp size_t n;
500 52a74475 2018-12-24 stsp
501 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
502 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
503 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
504 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
505 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
506 52a74475 2018-12-24 stsp return NULL;
507 52a74475 2018-12-24 stsp }
508 52a74475 2018-12-24 stsp
509 52a74475 2018-12-24 stsp static const struct got_error *
510 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
511 52a74475 2018-12-24 stsp {
512 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
513 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
514 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
515 52a74475 2018-12-24 stsp
516 52a74475 2018-12-24 stsp *path = malloc(totlen);
517 52a74475 2018-12-24 stsp if (*path == NULL)
518 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
519 52a74475 2018-12-24 stsp
520 52a74475 2018-12-24 stsp do {
521 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
522 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
523 52a74475 2018-12-24 stsp if (p == NULL) {
524 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
525 52a74475 2018-12-24 stsp break;
526 52a74475 2018-12-24 stsp }
527 6bf2c316 2019-08-02 stsp totlen += chunk_size;
528 52a74475 2018-12-24 stsp *path = p;
529 52a74475 2018-12-24 stsp }
530 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
531 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
532 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
533 6bf2c316 2019-08-02 stsp break;
534 6bf2c316 2019-08-02 stsp }
535 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
536 6bf2c316 2019-08-02 stsp len += chunk_size;
537 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
538 52a74475 2018-12-24 stsp
539 52a74475 2018-12-24 stsp if (err) {
540 52a74475 2018-12-24 stsp free(*path);
541 52a74475 2018-12-24 stsp *path = NULL;
542 52a74475 2018-12-24 stsp }
543 52a74475 2018-12-24 stsp return err;
544 52a74475 2018-12-24 stsp }
545 52a74475 2018-12-24 stsp
546 52a74475 2018-12-24 stsp static const struct got_error *
547 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
548 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
549 52a74475 2018-12-24 stsp {
550 52a74475 2018-12-24 stsp const struct got_error *err;
551 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
552 52a74475 2018-12-24 stsp size_t n;
553 52a74475 2018-12-24 stsp
554 3f762da0 2019-08-03 stsp *iep = NULL;
555 52a74475 2018-12-24 stsp
556 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
557 3f762da0 2019-08-03 stsp if (ie == NULL)
558 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
559 c34b20a2 2018-03-12 stsp
560 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, 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->ctime_nsec, 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_sec, ctx, infile);
567 52a74475 2018-12-24 stsp if (err)
568 52a74475 2018-12-24 stsp goto done;
569 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
570 52a74475 2018-12-24 stsp if (err)
571 52a74475 2018-12-24 stsp goto done;
572 52a74475 2018-12-24 stsp
573 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, 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->gid, ctx, infile);
577 52a74475 2018-12-24 stsp if (err)
578 52a74475 2018-12-24 stsp goto done;
579 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
580 52a74475 2018-12-24 stsp if (err)
581 52a74475 2018-12-24 stsp goto done;
582 52a74475 2018-12-24 stsp
583 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
584 52a74475 2018-12-24 stsp if (err)
585 52a74475 2018-12-24 stsp goto done;
586 52a74475 2018-12-24 stsp
587 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
588 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
589 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
590 52a74475 2018-12-24 stsp goto done;
591 52a74475 2018-12-24 stsp }
592 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
593 52a74475 2018-12-24 stsp
594 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
595 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
596 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
597 fc76cabb 2018-12-25 stsp goto done;
598 fc76cabb 2018-12-25 stsp }
599 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
600 fc76cabb 2018-12-25 stsp
601 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
602 52a74475 2018-12-24 stsp if (err)
603 52a74475 2018-12-24 stsp goto done;
604 52a74475 2018-12-24 stsp
605 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
606 df335242 2019-08-03 stsp if (err)
607 df335242 2019-08-03 stsp goto done;
608 df335242 2019-08-03 stsp
609 df335242 2019-08-03 stsp if (version >= 2) {
610 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
611 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
612 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
613 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
614 df335242 2019-08-03 stsp infile);
615 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
616 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
617 df335242 2019-08-03 stsp goto done;
618 df335242 2019-08-03 stsp }
619 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
620 df335242 2019-08-03 stsp }
621 df335242 2019-08-03 stsp } else {
622 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
623 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
624 df335242 2019-08-03 stsp }
625 df335242 2019-08-03 stsp
626 52a74475 2018-12-24 stsp done:
627 52a74475 2018-12-24 stsp if (err)
628 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
629 52a74475 2018-12-24 stsp else
630 3f762da0 2019-08-03 stsp *iep = ie;
631 52a74475 2018-12-24 stsp return err;
632 52a74475 2018-12-24 stsp }
633 52a74475 2018-12-24 stsp
634 52a74475 2018-12-24 stsp const struct got_error *
635 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
636 52a74475 2018-12-24 stsp {
637 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
638 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
639 52a74475 2018-12-24 stsp SHA1_CTX ctx;
640 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
641 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
642 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
643 52a74475 2018-12-24 stsp size_t n;
644 52a74475 2018-12-24 stsp int i;
645 52a74475 2018-12-24 stsp
646 52a74475 2018-12-24 stsp SHA1Init(&ctx);
647 52a74475 2018-12-24 stsp
648 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
649 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
650 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
651 b6d05318 2019-01-13 stsp return NULL;
652 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
653 b6d05318 2019-01-13 stsp }
654 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
655 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
656 51514078 2018-12-25 stsp if (n == 0) /* EOF */
657 51514078 2018-12-25 stsp return NULL;
658 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
659 51514078 2018-12-25 stsp }
660 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
661 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
662 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
663 b6d05318 2019-01-13 stsp return NULL;
664 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
665 b6d05318 2019-01-13 stsp }
666 52a74475 2018-12-24 stsp
667 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
668 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
669 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
670 52a74475 2018-12-24 stsp
671 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
672 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
673 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
674 52a74475 2018-12-24 stsp
675 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
676 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
677 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
678 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
679 52a74475 2018-12-24 stsp
680 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
681 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
682 52a74475 2018-12-24 stsp if (err)
683 52a74475 2018-12-24 stsp return err;
684 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
685 52a74475 2018-12-24 stsp if (err)
686 52a74475 2018-12-24 stsp return err;
687 52a74475 2018-12-24 stsp }
688 52a74475 2018-12-24 stsp
689 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
690 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
691 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
692 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
693 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
694 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
695 52a74475 2018-12-24 stsp
696 9d31a1d8 2018-03-11 stsp return NULL;
697 8da9e5f4 2019-01-12 stsp }
698 8da9e5f4 2019-01-12 stsp
699 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
700 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
701 50952927 2019-01-12 stsp {
702 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
703 50952927 2019-01-12 stsp
704 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
705 50952927 2019-01-12 stsp
706 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
707 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
708 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
709 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
710 50952927 2019-01-12 stsp
711 50952927 2019-01-12 stsp return next;
712 50952927 2019-01-12 stsp }
713 50952927 2019-01-12 stsp
714 8da9e5f4 2019-01-12 stsp static const struct got_error *
715 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
716 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
717 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
718 9d2a8e53 2019-01-28 stsp
719 9d2a8e53 2019-01-28 stsp static const struct got_error *
720 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
721 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
722 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
723 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
724 8da9e5f4 2019-01-12 stsp {
725 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
726 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
727 8da9e5f4 2019-01-12 stsp
728 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
729 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
730 8da9e5f4 2019-01-12 stsp char *subpath;
731 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
732 8da9e5f4 2019-01-12 stsp
733 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
734 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
735 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
736 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
737 8da9e5f4 2019-01-12 stsp
738 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
739 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
740 8da9e5f4 2019-01-12 stsp if (err) {
741 8da9e5f4 2019-01-12 stsp free(subpath);
742 8da9e5f4 2019-01-12 stsp return err;
743 8da9e5f4 2019-01-12 stsp }
744 8da9e5f4 2019-01-12 stsp
745 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
746 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
747 8da9e5f4 2019-01-12 stsp free(subpath);
748 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
749 8da9e5f4 2019-01-12 stsp if (err)
750 8da9e5f4 2019-01-12 stsp return err;
751 8da9e5f4 2019-01-12 stsp }
752 8da9e5f4 2019-01-12 stsp
753 56e0773d 2019-11-28 stsp (*tidx)++;
754 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
755 8da9e5f4 2019-01-12 stsp return NULL;
756 8da9e5f4 2019-01-12 stsp }
757 8da9e5f4 2019-01-12 stsp
758 8da9e5f4 2019-01-12 stsp static const struct got_error *
759 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
760 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
761 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
762 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
763 8da9e5f4 2019-01-12 stsp {
764 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
765 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
766 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
767 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
768 56e0773d 2019-11-28 stsp int tidx = 0;
769 8da9e5f4 2019-01-12 stsp
770 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
771 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
772 8da9e5f4 2019-01-12 stsp if (te && *ie) {
773 18831e78 2019-02-10 stsp char *te_path;
774 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
775 18831e78 2019-02-10 stsp int cmp;
776 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
777 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
778 18831e78 2019-02-10 stsp break;
779 18831e78 2019-02-10 stsp }
780 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
781 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
782 18831e78 2019-02-10 stsp free(te_path);
783 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
784 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
785 63c5ca5d 2019-08-24 stsp path_len) &&
786 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
787 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
788 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
789 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
790 c4cdcb68 2019-04-03 stsp path);
791 c4cdcb68 2019-04-03 stsp if (err || entry_name)
792 c4cdcb68 2019-04-03 stsp break;
793 c4cdcb68 2019-04-03 stsp }
794 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
795 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
796 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
797 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
798 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
799 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
800 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
801 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
802 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
803 c4cdcb68 2019-04-03 stsp if (err || entry_name)
804 c4cdcb68 2019-04-03 stsp break;
805 c4cdcb68 2019-04-03 stsp }
806 8da9e5f4 2019-01-12 stsp *ie = next;
807 8da9e5f4 2019-01-12 stsp } else {
808 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
809 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
810 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
811 c4cdcb68 2019-04-03 stsp if (err || entry_name)
812 c4cdcb68 2019-04-03 stsp break;
813 c4cdcb68 2019-04-03 stsp }
814 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
815 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
816 8da9e5f4 2019-01-12 stsp }
817 8da9e5f4 2019-01-12 stsp if (err)
818 8da9e5f4 2019-01-12 stsp break;
819 8da9e5f4 2019-01-12 stsp } else if (*ie) {
820 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
821 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
822 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
823 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
824 56e0773d 2019-11-28 stsp entry_name) == 0))) {
825 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
826 c4cdcb68 2019-04-03 stsp if (err || entry_name)
827 c4cdcb68 2019-04-03 stsp break;
828 c4cdcb68 2019-04-03 stsp }
829 8da9e5f4 2019-01-12 stsp *ie = next;
830 8da9e5f4 2019-01-12 stsp } else if (te) {
831 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
832 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
833 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
834 56e0773d 2019-11-28 stsp == 0)) {
835 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
836 c4cdcb68 2019-04-03 stsp if (err || entry_name)
837 c4cdcb68 2019-04-03 stsp break;
838 c4cdcb68 2019-04-03 stsp }
839 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
840 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
841 8da9e5f4 2019-01-12 stsp if (err)
842 8da9e5f4 2019-01-12 stsp break;
843 8da9e5f4 2019-01-12 stsp }
844 500cd40f 2019-02-04 stsp }
845 8da9e5f4 2019-01-12 stsp
846 8da9e5f4 2019-01-12 stsp return err;
847 8da9e5f4 2019-01-12 stsp }
848 8da9e5f4 2019-01-12 stsp
849 8da9e5f4 2019-01-12 stsp const struct got_error *
850 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
851 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
852 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
853 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
854 8da9e5f4 2019-01-12 stsp {
855 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
856 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
857 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
858 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
859 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
860 30a076bc 2019-07-27 stsp cb, cb_arg);
861 d1f6d47b 2019-02-04 stsp }
862 d1f6d47b 2019-02-04 stsp
863 d1f6d47b 2019-02-04 stsp static const struct got_error *
864 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
865 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
866 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
867 c577a9ce 2019-07-27 stsp
868 c577a9ce 2019-07-27 stsp static const struct got_error *
869 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
870 c577a9ce 2019-07-27 stsp {
871 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
872 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
873 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
874 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
875 c577a9ce 2019-07-27 stsp
876 c577a9ce 2019-07-27 stsp for (;;) {
877 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
878 c577a9ce 2019-07-27 stsp if (de == NULL) {
879 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
880 c577a9ce 2019-07-27 stsp break;
881 c577a9ce 2019-07-27 stsp }
882 c577a9ce 2019-07-27 stsp
883 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
884 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
885 c577a9ce 2019-07-27 stsp free(de);
886 c577a9ce 2019-07-27 stsp break;
887 c577a9ce 2019-07-27 stsp }
888 c577a9ce 2019-07-27 stsp if (dep == NULL) {
889 c577a9ce 2019-07-27 stsp free(de);
890 c577a9ce 2019-07-27 stsp break;
891 c577a9ce 2019-07-27 stsp }
892 c577a9ce 2019-07-27 stsp
893 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
894 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
895 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
896 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
897 c577a9ce 2019-07-27 stsp free(de);
898 c577a9ce 2019-07-27 stsp continue;
899 c577a9ce 2019-07-27 stsp }
900 c577a9ce 2019-07-27 stsp
901 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
902 c577a9ce 2019-07-27 stsp if (err) {
903 c577a9ce 2019-07-27 stsp free(de);
904 c577a9ce 2019-07-27 stsp break;
905 c577a9ce 2019-07-27 stsp }
906 c577a9ce 2019-07-27 stsp if (new == NULL) {
907 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
908 c577a9ce 2019-07-27 stsp free(de);
909 c577a9ce 2019-07-27 stsp break;
910 c577a9ce 2019-07-27 stsp }
911 c577a9ce 2019-07-27 stsp }
912 500cd40f 2019-02-04 stsp
913 c577a9ce 2019-07-27 stsp return err;
914 c577a9ce 2019-07-27 stsp }
915 c577a9ce 2019-07-27 stsp
916 c577a9ce 2019-07-27 stsp void
917 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
918 c577a9ce 2019-07-27 stsp {
919 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
920 c577a9ce 2019-07-27 stsp
921 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
922 c577a9ce 2019-07-27 stsp free(dle->data);
923 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
924 c577a9ce 2019-07-27 stsp }
925 c577a9ce 2019-07-27 stsp
926 d1f6d47b 2019-02-04 stsp static const struct got_error *
927 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
928 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
929 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
930 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
931 d1f6d47b 2019-02-04 stsp {
932 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
933 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
934 965988c5 2019-12-16 stsp DIR *subdir = NULL;
935 6fc93f37 2019-12-13 stsp int subdirfd = -1;
936 573463cc 2019-04-06 stsp
937 573463cc 2019-04-06 stsp *next = NULL;
938 d1f6d47b 2019-02-04 stsp
939 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
940 d1f6d47b 2019-02-04 stsp char *subpath;
941 c7f4312f 2019-02-05 stsp char *subdirpath;
942 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
943 d1f6d47b 2019-02-04 stsp
944 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
945 c577a9ce 2019-07-27 stsp
946 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
947 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
948 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
949 d1f6d47b 2019-02-04 stsp
950 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
951 c7f4312f 2019-02-05 stsp free(subpath);
952 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
953 c7f4312f 2019-02-05 stsp }
954 c7f4312f 2019-02-05 stsp
955 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
956 6fc93f37 2019-12-13 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
957 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
958 40b289d7 2019-09-07 stsp if (errno == EACCES) {
959 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
960 40b289d7 2019-09-07 stsp return NULL;
961 40b289d7 2019-09-07 stsp }
962 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
963 d1f6d47b 2019-02-04 stsp free(subpath);
964 c7f4312f 2019-02-05 stsp free(subdirpath);
965 ef5e02fd 2019-08-11 stsp return err;
966 d1f6d47b 2019-02-04 stsp }
967 d1f6d47b 2019-02-04 stsp
968 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
969 965988c5 2019-12-16 stsp if (subdir == NULL)
970 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
971 965988c5 2019-12-16 stsp subdirfd = -1;
972 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
973 c577a9ce 2019-07-27 stsp if (err) {
974 c577a9ce 2019-07-27 stsp free(subpath);
975 c577a9ce 2019-07-27 stsp free(subdirpath);
976 965988c5 2019-12-16 stsp closedir(subdir);
977 c577a9ce 2019-07-27 stsp return err;
978 c577a9ce 2019-07-27 stsp }
979 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
980 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
981 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
982 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
983 d1f6d47b 2019-02-04 stsp free(subpath);
984 c7f4312f 2019-02-05 stsp free(subdirpath);
985 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
986 d1f6d47b 2019-02-04 stsp if (err)
987 d1f6d47b 2019-02-04 stsp return err;
988 d1f6d47b 2019-02-04 stsp }
989 d1f6d47b 2019-02-04 stsp
990 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
991 d1f6d47b 2019-02-04 stsp return NULL;
992 d1f6d47b 2019-02-04 stsp }
993 d1f6d47b 2019-02-04 stsp
994 d1f6d47b 2019-02-04 stsp static const struct got_error *
995 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
996 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
997 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
998 6fc93f37 2019-12-13 stsp struct got_repository *repo,
999 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1000 d1f6d47b 2019-02-04 stsp {
1001 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1002 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1003 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1004 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1005 d1f6d47b 2019-02-04 stsp
1006 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1007 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1008 500cd40f 2019-02-04 stsp if (dle && *ie) {
1009 18831e78 2019-02-10 stsp char *de_path;
1010 e7a2f030 2019-02-05 stsp int cmp;
1011 f5d3d7af 2019-02-05 stsp de = dle->data;
1012 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1013 18831e78 2019-02-10 stsp de->d_name) == -1) {
1014 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1015 13ff9e90 2019-02-10 stsp break;
1016 18831e78 2019-02-10 stsp }
1017 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1018 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1019 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
1020 18831e78 2019-02-10 stsp free(de_path);
1021 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1022 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1023 7f91a133 2019-12-13 stsp dirfd);
1024 d1f6d47b 2019-02-04 stsp if (err)
1025 d1f6d47b 2019-02-04 stsp break;
1026 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1027 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1028 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1029 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1030 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1031 d1f6d47b 2019-02-04 stsp if (err)
1032 d1f6d47b 2019-02-04 stsp break;
1033 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1034 d1f6d47b 2019-02-04 stsp } else {
1035 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1036 d1f6d47b 2019-02-04 stsp if (err)
1037 d1f6d47b 2019-02-04 stsp break;
1038 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1039 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1040 d1f6d47b 2019-02-04 stsp }
1041 554b91b1 2019-02-04 stsp if (err)
1042 554b91b1 2019-02-04 stsp break;
1043 554b91b1 2019-02-04 stsp } else if (*ie) {
1044 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1045 554b91b1 2019-02-04 stsp if (err)
1046 554b91b1 2019-02-04 stsp break;
1047 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1048 554b91b1 2019-02-04 stsp } else if (dle) {
1049 763e1377 2019-02-05 stsp de = dle->data;
1050 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1051 d1f6d47b 2019-02-04 stsp if (err)
1052 d1f6d47b 2019-02-04 stsp break;
1053 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1054 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
1055 554b91b1 2019-02-04 stsp if (err)
1056 554b91b1 2019-02-04 stsp break;
1057 d1f6d47b 2019-02-04 stsp }
1058 500cd40f 2019-02-04 stsp }
1059 c577a9ce 2019-07-27 stsp
1060 d1f6d47b 2019-02-04 stsp return err;
1061 8da9e5f4 2019-01-12 stsp }
1062 8da9e5f4 2019-01-12 stsp
1063 d1f6d47b 2019-02-04 stsp const struct got_error *
1064 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1065 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1066 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1067 d1f6d47b 2019-02-04 stsp {
1068 c577a9ce 2019-07-27 stsp const struct got_error *err;
1069 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1070 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1071 965988c5 2019-12-16 stsp int fd2;
1072 965988c5 2019-12-16 stsp DIR *dir;
1073 c577a9ce 2019-07-27 stsp
1074 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1075 965988c5 2019-12-16 stsp
1076 965988c5 2019-12-16 stsp /*
1077 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1078 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1079 965988c5 2019-12-16 stsp */
1080 965988c5 2019-12-16 stsp fd2 = dup(fd);
1081 965988c5 2019-12-16 stsp if (fd2 == -1)
1082 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1083 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1084 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1085 3dcf3e74 2020-01-28 stsp close(fd2);
1086 3dcf3e74 2020-01-28 stsp return err;
1087 3dcf3e74 2020-01-28 stsp }
1088 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1089 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1090 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1091 3dcf3e74 2020-01-28 stsp close(fd2);
1092 3dcf3e74 2020-01-28 stsp return err;
1093 3dcf3e74 2020-01-28 stsp }
1094 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1095 965988c5 2019-12-16 stsp if (err) {
1096 965988c5 2019-12-16 stsp closedir(dir);
1097 c577a9ce 2019-07-27 stsp return err;
1098 965988c5 2019-12-16 stsp }
1099 965988c5 2019-12-16 stsp
1100 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1101 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1102 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1103 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1104 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1105 965988c5 2019-12-16 stsp
1106 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1107 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1108 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1109 c577a9ce 2019-07-27 stsp return err;
1110 d1f6d47b 2019-02-04 stsp }
1111 d1f6d47b 2019-02-04 stsp
1112 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);