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