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