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 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 c48c4a9c 2018-03-11 stsp #include <stdio.h>
23 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
24 c48c4a9c 2018-03-11 stsp #include <string.h>
25 c48c4a9c 2018-03-11 stsp #include <sha1.h>
26 c34b20a2 2018-03-12 stsp #include <endian.h>
27 133d2798 2019-01-08 stsp #include <limits.h>
28 c442a90d 2019-03-10 stsp #include <uuid.h>
29 c48c4a9c 2018-03-11 stsp
30 c48c4a9c 2018-03-11 stsp #include "got_error.h"
31 8da9e5f4 2019-01-12 stsp #include "got_object.h"
32 c48c4a9c 2018-03-11 stsp
33 133d2798 2019-01-08 stsp #include "got_lib_path.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
35 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
36 c48c4a9c 2018-03-11 stsp
37 133d2798 2019-01-08 stsp struct got_fileindex {
38 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
39 133d2798 2019-01-08 stsp int nentries;
40 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
41 133d2798 2019-01-08 stsp };
42 133d2798 2019-01-08 stsp
43 c48c4a9c 2018-03-11 stsp const struct got_error *
44 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
45 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
46 02c07007 2019-02-10 stsp int update_timestamps)
47 c48c4a9c 2018-03-11 stsp {
48 c48c4a9c 2018-03-11 stsp struct stat sb;
49 c48c4a9c 2018-03-11 stsp
50 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
51 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
52 c48c4a9c 2018-03-11 stsp
53 02c07007 2019-02-10 stsp if (update_timestamps) {
54 02c07007 2019-02-10 stsp entry->ctime_sec = sb.st_ctime;
55 02c07007 2019-02-10 stsp entry->ctime_nsec = sb.st_ctimensec;
56 02c07007 2019-02-10 stsp entry->mtime_sec = sb.st_mtime;
57 02c07007 2019-02-10 stsp entry->mtime_nsec = sb.st_mtimensec;
58 02c07007 2019-02-10 stsp }
59 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
60 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
61 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
62 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
63 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
64 51514078 2018-12-25 stsp else
65 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
66 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
67 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
68 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
69 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
70 51514078 2018-12-25 stsp
71 51514078 2018-12-25 stsp return NULL;
72 51514078 2018-12-25 stsp }
73 51514078 2018-12-25 stsp
74 51514078 2018-12-25 stsp const struct got_error *
75 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
76 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
77 51514078 2018-12-25 stsp uint8_t *commit_sha1)
78 51514078 2018-12-25 stsp {
79 51514078 2018-12-25 stsp size_t len;
80 51514078 2018-12-25 stsp
81 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
82 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
83 0a585a0d 2018-03-17 stsp return got_error_from_errno();
84 c48c4a9c 2018-03-11 stsp
85 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
86 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
87 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
88 c48c4a9c 2018-03-11 stsp free(*entry);
89 c48c4a9c 2018-03-11 stsp *entry = NULL;
90 0a585a0d 2018-03-17 stsp return err;
91 c48c4a9c 2018-03-11 stsp }
92 51514078 2018-12-25 stsp
93 c34b20a2 2018-03-12 stsp len = strlen(relpath);
94 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
95 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
96 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
97 c48c4a9c 2018-03-11 stsp
98 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
99 02c07007 2019-02-10 stsp commit_sha1, 1);
100 c48c4a9c 2018-03-11 stsp }
101 c48c4a9c 2018-03-11 stsp
102 c48c4a9c 2018-03-11 stsp void
103 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
104 c48c4a9c 2018-03-11 stsp {
105 c48c4a9c 2018-03-11 stsp free(entry->path);
106 c48c4a9c 2018-03-11 stsp free(entry);
107 c48c4a9c 2018-03-11 stsp }
108 9d31a1d8 2018-03-11 stsp
109 50952927 2019-01-12 stsp static const struct got_error *
110 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
111 9d31a1d8 2018-03-11 stsp {
112 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
113 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
114 133d2798 2019-01-08 stsp
115 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
116 133d2798 2019-01-08 stsp fileindex->nentries++;
117 133d2798 2019-01-08 stsp return NULL;
118 50952927 2019-01-12 stsp }
119 50952927 2019-01-12 stsp
120 50952927 2019-01-12 stsp const struct got_error *
121 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
122 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
123 50952927 2019-01-12 stsp {
124 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
125 a7f9d64d 2019-01-13 stsp entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
126 50952927 2019-01-12 stsp
127 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
128 9d31a1d8 2018-03-11 stsp }
129 9d31a1d8 2018-03-11 stsp
130 133d2798 2019-01-08 stsp void
131 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
132 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
133 512f0d0e 2019-01-02 stsp {
134 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
135 133d2798 2019-01-08 stsp fileindex->nentries--;
136 512f0d0e 2019-01-02 stsp }
137 512f0d0e 2019-01-02 stsp
138 51514078 2018-12-25 stsp struct got_fileindex_entry *
139 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
140 51514078 2018-12-25 stsp {
141 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
142 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
143 133d2798 2019-01-08 stsp key.path = (char *)path;
144 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
145 b504a804 2019-01-08 stsp }
146 51514078 2018-12-25 stsp
147 512f0d0e 2019-01-02 stsp const struct got_error *
148 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
149 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
150 512f0d0e 2019-01-02 stsp {
151 133d2798 2019-01-08 stsp const struct got_error *err;
152 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
153 9d31a1d8 2018-03-11 stsp
154 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
155 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
156 133d2798 2019-01-08 stsp if (err)
157 133d2798 2019-01-08 stsp return err;
158 b504a804 2019-01-08 stsp }
159 b504a804 2019-01-08 stsp return NULL;
160 9d31a1d8 2018-03-11 stsp }
161 9d31a1d8 2018-03-11 stsp
162 133d2798 2019-01-08 stsp struct got_fileindex *
163 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
164 6b798c3c 2019-01-08 stsp {
165 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
166 133d2798 2019-01-08 stsp
167 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
168 133d2798 2019-01-08 stsp if (fileindex == NULL)
169 133d2798 2019-01-08 stsp return NULL;
170 133d2798 2019-01-08 stsp
171 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
172 133d2798 2019-01-08 stsp return fileindex;
173 6b798c3c 2019-01-08 stsp }
174 6b798c3c 2019-01-08 stsp
175 9d31a1d8 2018-03-11 stsp void
176 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
177 9d31a1d8 2018-03-11 stsp {
178 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
179 133d2798 2019-01-08 stsp
180 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
181 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
182 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
183 133d2798 2019-01-08 stsp }
184 9d31a1d8 2018-03-11 stsp free(fileindex);
185 9d31a1d8 2018-03-11 stsp }
186 9d31a1d8 2018-03-11 stsp
187 c34b20a2 2018-03-12 stsp static const struct got_error *
188 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
189 c34b20a2 2018-03-12 stsp {
190 c34b20a2 2018-03-12 stsp size_t n;
191 c34b20a2 2018-03-12 stsp
192 c34b20a2 2018-03-12 stsp val = htobe64(val);
193 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
194 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
195 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
196 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
197 c34b20a2 2018-03-12 stsp return NULL;
198 c34b20a2 2018-03-12 stsp }
199 c34b20a2 2018-03-12 stsp
200 c34b20a2 2018-03-12 stsp static const struct got_error *
201 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
202 c34b20a2 2018-03-12 stsp {
203 c34b20a2 2018-03-12 stsp size_t n;
204 c34b20a2 2018-03-12 stsp
205 c34b20a2 2018-03-12 stsp val = htobe32(val);
206 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
207 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
208 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
209 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
210 c34b20a2 2018-03-12 stsp return NULL;
211 c34b20a2 2018-03-12 stsp }
212 c34b20a2 2018-03-12 stsp
213 c34b20a2 2018-03-12 stsp static const struct got_error *
214 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
215 c34b20a2 2018-03-12 stsp {
216 c34b20a2 2018-03-12 stsp size_t n;
217 c34b20a2 2018-03-12 stsp
218 c34b20a2 2018-03-12 stsp val = htobe16(val);
219 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
220 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
221 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
222 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
223 c34b20a2 2018-03-12 stsp return NULL;
224 c34b20a2 2018-03-12 stsp }
225 c34b20a2 2018-03-12 stsp
226 c34b20a2 2018-03-12 stsp static const struct got_error *
227 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
228 c34b20a2 2018-03-12 stsp {
229 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
230 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
231 c34b20a2 2018-03-12 stsp
232 c34b20a2 2018-03-12 stsp len = strlen(path);
233 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
234 3c5b70f2 2018-12-27 stsp pad++;
235 3c5b70f2 2018-12-27 stsp if (pad == 0)
236 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
237 c34b20a2 2018-03-12 stsp
238 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
239 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
240 c34b20a2 2018-03-12 stsp if (n != len)
241 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
242 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
243 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
244 c34b20a2 2018-03-12 stsp if (n != pad)
245 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
246 c34b20a2 2018-03-12 stsp return NULL;
247 c34b20a2 2018-03-12 stsp }
248 c34b20a2 2018-03-12 stsp
249 c34b20a2 2018-03-12 stsp static const struct got_error *
250 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
251 c34b20a2 2018-03-12 stsp FILE *outfile)
252 c34b20a2 2018-03-12 stsp {
253 c34b20a2 2018-03-12 stsp const struct got_error *err;
254 23b19d00 2018-03-12 stsp size_t n;
255 c34b20a2 2018-03-12 stsp
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
257 c34b20a2 2018-03-12 stsp if (err)
258 c34b20a2 2018-03-12 stsp return err;
259 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
260 c34b20a2 2018-03-12 stsp if (err)
261 c34b20a2 2018-03-12 stsp return err;
262 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
263 c34b20a2 2018-03-12 stsp if (err)
264 c34b20a2 2018-03-12 stsp return err;
265 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
266 c34b20a2 2018-03-12 stsp if (err)
267 c34b20a2 2018-03-12 stsp return err;
268 c34b20a2 2018-03-12 stsp
269 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
270 c34b20a2 2018-03-12 stsp if (err)
271 c34b20a2 2018-03-12 stsp return err;
272 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
273 c34b20a2 2018-03-12 stsp if (err)
274 c34b20a2 2018-03-12 stsp return err;
275 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
276 c34b20a2 2018-03-12 stsp if (err)
277 c34b20a2 2018-03-12 stsp return err;
278 c34b20a2 2018-03-12 stsp
279 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
280 c34b20a2 2018-03-12 stsp if (err)
281 c34b20a2 2018-03-12 stsp return err;
282 c34b20a2 2018-03-12 stsp
283 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
284 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
285 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
286 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
287 fc76cabb 2018-12-25 stsp
288 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
289 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
290 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
291 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
292 c34b20a2 2018-03-12 stsp
293 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
294 c34b20a2 2018-03-12 stsp if (err)
295 c34b20a2 2018-03-12 stsp return err;
296 c34b20a2 2018-03-12 stsp
297 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
298 c34b20a2 2018-03-12 stsp return err;
299 c34b20a2 2018-03-12 stsp }
300 c34b20a2 2018-03-12 stsp
301 9d31a1d8 2018-03-11 stsp const struct got_error *
302 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
303 9d31a1d8 2018-03-11 stsp {
304 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
305 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
306 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
307 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
308 c34b20a2 2018-03-12 stsp size_t n;
309 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
310 c34b20a2 2018-03-12 stsp
311 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
312 c34b20a2 2018-03-12 stsp
313 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
314 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
315 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
316 c34b20a2 2018-03-12 stsp
317 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
318 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
319 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
320 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
321 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
322 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
323 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
324 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
325 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
326 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
327 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
328 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
329 c34b20a2 2018-03-12 stsp
330 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
331 a7f9d64d 2019-01-13 stsp entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
332 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
333 133d2798 2019-01-08 stsp if (err)
334 133d2798 2019-01-08 stsp return err;
335 133d2798 2019-01-08 stsp }
336 c34b20a2 2018-03-12 stsp
337 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
338 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
339 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
340 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
341 52a74475 2018-12-24 stsp
342 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
343 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
344 27d0e5bd 2019-01-12 stsp
345 52a74475 2018-12-24 stsp return NULL;
346 52a74475 2018-12-24 stsp }
347 52a74475 2018-12-24 stsp
348 52a74475 2018-12-24 stsp static const struct got_error *
349 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
350 52a74475 2018-12-24 stsp {
351 52a74475 2018-12-24 stsp size_t n;
352 52a74475 2018-12-24 stsp
353 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
354 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
355 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
356 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
357 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
358 52a74475 2018-12-24 stsp return NULL;
359 52a74475 2018-12-24 stsp }
360 52a74475 2018-12-24 stsp
361 52a74475 2018-12-24 stsp static const struct got_error *
362 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
363 52a74475 2018-12-24 stsp {
364 52a74475 2018-12-24 stsp size_t n;
365 52a74475 2018-12-24 stsp
366 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
367 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
368 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
369 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
370 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
371 52a74475 2018-12-24 stsp return NULL;
372 52a74475 2018-12-24 stsp }
373 52a74475 2018-12-24 stsp
374 52a74475 2018-12-24 stsp static const struct got_error *
375 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
376 52a74475 2018-12-24 stsp {
377 52a74475 2018-12-24 stsp size_t n;
378 52a74475 2018-12-24 stsp
379 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
380 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
381 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
382 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
383 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
384 52a74475 2018-12-24 stsp return NULL;
385 52a74475 2018-12-24 stsp }
386 52a74475 2018-12-24 stsp
387 52a74475 2018-12-24 stsp static const struct got_error *
388 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
389 52a74475 2018-12-24 stsp {
390 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
391 52a74475 2018-12-24 stsp uint8_t buf[8];
392 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
393 52a74475 2018-12-24 stsp
394 52a74475 2018-12-24 stsp *path = malloc(totlen);
395 52a74475 2018-12-24 stsp if (*path == NULL)
396 52a74475 2018-12-24 stsp return got_error_from_errno();
397 52a74475 2018-12-24 stsp
398 52a74475 2018-12-24 stsp do {
399 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
400 52a74475 2018-12-24 stsp if (n != sizeof(buf))
401 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
402 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
403 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
404 52a74475 2018-12-24 stsp if (p == NULL) {
405 52a74475 2018-12-24 stsp err = got_error_from_errno();
406 52a74475 2018-12-24 stsp break;
407 52a74475 2018-12-24 stsp }
408 52a74475 2018-12-24 stsp totlen += sizeof(buf);
409 52a74475 2018-12-24 stsp *path = p;
410 52a74475 2018-12-24 stsp }
411 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
412 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
413 52a74475 2018-12-24 stsp len += sizeof(buf);
414 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
415 52a74475 2018-12-24 stsp
416 52a74475 2018-12-24 stsp if (err) {
417 52a74475 2018-12-24 stsp free(*path);
418 52a74475 2018-12-24 stsp *path = NULL;
419 52a74475 2018-12-24 stsp }
420 52a74475 2018-12-24 stsp return err;
421 52a74475 2018-12-24 stsp }
422 52a74475 2018-12-24 stsp
423 52a74475 2018-12-24 stsp static const struct got_error *
424 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
425 52a74475 2018-12-24 stsp FILE *infile)
426 52a74475 2018-12-24 stsp {
427 52a74475 2018-12-24 stsp const struct got_error *err;
428 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
429 52a74475 2018-12-24 stsp size_t n;
430 52a74475 2018-12-24 stsp
431 52a74475 2018-12-24 stsp *entryp = NULL;
432 52a74475 2018-12-24 stsp
433 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
434 52a74475 2018-12-24 stsp if (entry == NULL)
435 52a74475 2018-12-24 stsp return got_error_from_errno();
436 c34b20a2 2018-03-12 stsp
437 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
438 52a74475 2018-12-24 stsp if (err)
439 52a74475 2018-12-24 stsp goto done;
440 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
441 52a74475 2018-12-24 stsp if (err)
442 52a74475 2018-12-24 stsp goto done;
443 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
444 52a74475 2018-12-24 stsp if (err)
445 52a74475 2018-12-24 stsp goto done;
446 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
447 52a74475 2018-12-24 stsp if (err)
448 52a74475 2018-12-24 stsp goto done;
449 52a74475 2018-12-24 stsp
450 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
451 52a74475 2018-12-24 stsp if (err)
452 52a74475 2018-12-24 stsp goto done;
453 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
454 52a74475 2018-12-24 stsp if (err)
455 52a74475 2018-12-24 stsp goto done;
456 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
457 52a74475 2018-12-24 stsp if (err)
458 52a74475 2018-12-24 stsp goto done;
459 52a74475 2018-12-24 stsp
460 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
461 52a74475 2018-12-24 stsp if (err)
462 52a74475 2018-12-24 stsp goto done;
463 52a74475 2018-12-24 stsp
464 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
465 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
466 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
467 52a74475 2018-12-24 stsp goto done;
468 52a74475 2018-12-24 stsp }
469 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
470 52a74475 2018-12-24 stsp
471 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
472 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
473 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
474 fc76cabb 2018-12-25 stsp goto done;
475 fc76cabb 2018-12-25 stsp }
476 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
477 fc76cabb 2018-12-25 stsp
478 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
479 52a74475 2018-12-24 stsp if (err)
480 52a74475 2018-12-24 stsp goto done;
481 52a74475 2018-12-24 stsp
482 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
483 52a74475 2018-12-24 stsp done:
484 52a74475 2018-12-24 stsp if (err)
485 52a74475 2018-12-24 stsp free(entry);
486 52a74475 2018-12-24 stsp else
487 52a74475 2018-12-24 stsp *entryp = entry;
488 52a74475 2018-12-24 stsp return err;
489 52a74475 2018-12-24 stsp }
490 52a74475 2018-12-24 stsp
491 52a74475 2018-12-24 stsp const struct got_error *
492 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
493 52a74475 2018-12-24 stsp {
494 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
495 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
496 52a74475 2018-12-24 stsp SHA1_CTX ctx;
497 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
498 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
499 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
500 52a74475 2018-12-24 stsp size_t n;
501 52a74475 2018-12-24 stsp int i;
502 52a74475 2018-12-24 stsp
503 52a74475 2018-12-24 stsp SHA1Init(&ctx);
504 52a74475 2018-12-24 stsp
505 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
506 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
507 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
508 b6d05318 2019-01-13 stsp return NULL;
509 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
510 b6d05318 2019-01-13 stsp }
511 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
512 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
513 51514078 2018-12-25 stsp if (n == 0) /* EOF */
514 51514078 2018-12-25 stsp return NULL;
515 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 51514078 2018-12-25 stsp }
517 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
518 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
519 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
520 b6d05318 2019-01-13 stsp return NULL;
521 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
522 b6d05318 2019-01-13 stsp }
523 52a74475 2018-12-24 stsp
524 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
525 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
526 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
527 52a74475 2018-12-24 stsp
528 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
529 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
530 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
531 52a74475 2018-12-24 stsp
532 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
533 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
534 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
535 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
536 52a74475 2018-12-24 stsp
537 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
538 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
539 52a74475 2018-12-24 stsp if (err)
540 52a74475 2018-12-24 stsp return err;
541 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
542 52a74475 2018-12-24 stsp if (err)
543 52a74475 2018-12-24 stsp return err;
544 52a74475 2018-12-24 stsp }
545 52a74475 2018-12-24 stsp
546 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
547 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
548 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
549 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
550 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
551 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
552 52a74475 2018-12-24 stsp
553 9d31a1d8 2018-03-11 stsp return NULL;
554 8da9e5f4 2019-01-12 stsp }
555 8da9e5f4 2019-01-12 stsp
556 50952927 2019-01-12 stsp struct got_fileindex_entry *
557 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
558 50952927 2019-01-12 stsp {
559 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
560 50952927 2019-01-12 stsp
561 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
562 50952927 2019-01-12 stsp
563 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
564 a7f9d64d 2019-01-13 stsp while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
565 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
566 50952927 2019-01-12 stsp
567 50952927 2019-01-12 stsp return next;
568 50952927 2019-01-12 stsp }
569 50952927 2019-01-12 stsp
570 8da9e5f4 2019-01-12 stsp static const struct got_error *
571 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
572 9d2a8e53 2019-01-28 stsp struct got_tree_object *, const char *, struct got_repository *,
573 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *, void *);
574 9d2a8e53 2019-01-28 stsp
575 9d2a8e53 2019-01-28 stsp static const struct got_error *
576 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
577 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
578 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
579 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
580 8da9e5f4 2019-01-12 stsp {
581 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
582 8da9e5f4 2019-01-12 stsp
583 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
584 8da9e5f4 2019-01-12 stsp char *subpath;
585 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
586 8da9e5f4 2019-01-12 stsp
587 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
588 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
589 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
590 8da9e5f4 2019-01-12 stsp
591 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
592 8da9e5f4 2019-01-12 stsp if (err) {
593 8da9e5f4 2019-01-12 stsp free(subpath);
594 8da9e5f4 2019-01-12 stsp return err;
595 8da9e5f4 2019-01-12 stsp }
596 8da9e5f4 2019-01-12 stsp
597 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
598 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
599 8da9e5f4 2019-01-12 stsp free(subpath);
600 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
601 8da9e5f4 2019-01-12 stsp if (err)
602 8da9e5f4 2019-01-12 stsp return err;
603 8da9e5f4 2019-01-12 stsp }
604 8da9e5f4 2019-01-12 stsp
605 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
606 8da9e5f4 2019-01-12 stsp return NULL;
607 8da9e5f4 2019-01-12 stsp }
608 8da9e5f4 2019-01-12 stsp
609 8da9e5f4 2019-01-12 stsp static const struct got_error *
610 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
611 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
612 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
613 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
614 8da9e5f4 2019-01-12 stsp {
615 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
616 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
617 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
618 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
619 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
620 8da9e5f4 2019-01-12 stsp
621 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
622 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
623 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
624 8da9e5f4 2019-01-12 stsp if (te && *ie) {
625 18831e78 2019-02-10 stsp char *te_path;
626 18831e78 2019-02-10 stsp int cmp;
627 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
628 18831e78 2019-02-10 stsp err = got_error_from_errno();
629 18831e78 2019-02-10 stsp break;
630 18831e78 2019-02-10 stsp }
631 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
632 18831e78 2019-02-10 stsp free(te_path);
633 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
634 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
635 8da9e5f4 2019-01-12 stsp path);
636 8da9e5f4 2019-01-12 stsp if (err)
637 8da9e5f4 2019-01-12 stsp break;
638 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
639 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
640 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
641 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
642 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
643 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
644 8da9e5f4 2019-01-12 stsp if (err)
645 8da9e5f4 2019-01-12 stsp break;
646 8da9e5f4 2019-01-12 stsp *ie = next;
647 8da9e5f4 2019-01-12 stsp } else {
648 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
649 8da9e5f4 2019-01-12 stsp if (err)
650 8da9e5f4 2019-01-12 stsp break;
651 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
652 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
653 8da9e5f4 2019-01-12 stsp }
654 8da9e5f4 2019-01-12 stsp if (err)
655 8da9e5f4 2019-01-12 stsp break;
656 8da9e5f4 2019-01-12 stsp } else if (*ie) {
657 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
658 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
659 8da9e5f4 2019-01-12 stsp if (err)
660 8da9e5f4 2019-01-12 stsp break;
661 8da9e5f4 2019-01-12 stsp *ie = next;
662 8da9e5f4 2019-01-12 stsp } else if (te) {
663 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
664 8da9e5f4 2019-01-12 stsp if (err)
665 8da9e5f4 2019-01-12 stsp break;
666 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
667 8da9e5f4 2019-01-12 stsp cb_arg);
668 8da9e5f4 2019-01-12 stsp if (err)
669 8da9e5f4 2019-01-12 stsp break;
670 8da9e5f4 2019-01-12 stsp }
671 500cd40f 2019-02-04 stsp }
672 8da9e5f4 2019-01-12 stsp
673 8da9e5f4 2019-01-12 stsp return err;
674 8da9e5f4 2019-01-12 stsp }
675 8da9e5f4 2019-01-12 stsp
676 8da9e5f4 2019-01-12 stsp const struct got_error *
677 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
678 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
679 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
680 8da9e5f4 2019-01-12 stsp {
681 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
682 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
683 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
684 d1f6d47b 2019-02-04 stsp }
685 d1f6d47b 2019-02-04 stsp
686 d1f6d47b 2019-02-04 stsp static const struct got_error *
687 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
688 c7f4312f 2019-02-05 stsp const char *, const char *, struct got_repository *,
689 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *, void *);
690 500cd40f 2019-02-04 stsp
691 d1f6d47b 2019-02-04 stsp static const struct got_error *
692 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
693 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
694 c7f4312f 2019-02-05 stsp const char *path, DIR *dir, const char *rootpath,
695 c7f4312f 2019-02-05 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
696 c7f4312f 2019-02-05 stsp void *cb_arg)
697 d1f6d47b 2019-02-04 stsp {
698 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
699 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
700 d1f6d47b 2019-02-04 stsp
701 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
702 d1f6d47b 2019-02-04 stsp char *subpath;
703 c7f4312f 2019-02-05 stsp char *subdirpath;
704 d1f6d47b 2019-02-04 stsp DIR *subdir;
705 d1f6d47b 2019-02-04 stsp
706 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
707 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
708 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
709 d1f6d47b 2019-02-04 stsp
710 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
711 c7f4312f 2019-02-05 stsp free(subpath);
712 c7f4312f 2019-02-05 stsp return got_error_from_errno();
713 c7f4312f 2019-02-05 stsp }
714 c7f4312f 2019-02-05 stsp
715 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
716 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
717 d1f6d47b 2019-02-04 stsp free(subpath);
718 c7f4312f 2019-02-05 stsp free(subdirpath);
719 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
720 d1f6d47b 2019-02-04 stsp }
721 d1f6d47b 2019-02-04 stsp
722 c7f4312f 2019-02-05 stsp err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
723 c7f4312f 2019-02-05 stsp subpath, repo, cb, cb_arg);
724 d1f6d47b 2019-02-04 stsp free(subpath);
725 c7f4312f 2019-02-05 stsp free(subdirpath);
726 d1f6d47b 2019-02-04 stsp closedir(subdir);
727 d1f6d47b 2019-02-04 stsp if (err)
728 d1f6d47b 2019-02-04 stsp return err;
729 d1f6d47b 2019-02-04 stsp }
730 d1f6d47b 2019-02-04 stsp
731 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
732 d1f6d47b 2019-02-04 stsp return NULL;
733 d1f6d47b 2019-02-04 stsp }
734 d1f6d47b 2019-02-04 stsp
735 d1f6d47b 2019-02-04 stsp static const struct got_error *
736 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
737 c7f4312f 2019-02-05 stsp struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
738 c7f4312f 2019-02-05 stsp const char *path, struct got_repository *repo,
739 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
740 d1f6d47b 2019-02-04 stsp {
741 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
742 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
743 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
744 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
745 f5d3d7af 2019-02-05 stsp struct got_pathlist_head dirlist;
746 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
747 d1f6d47b 2019-02-04 stsp
748 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&dirlist);
749 500cd40f 2019-02-04 stsp
750 500cd40f 2019-02-04 stsp while (1) {
751 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
752 ed83bff7 2019-02-05 stsp struct dirent *dep = NULL;
753 f5d3d7af 2019-02-05 stsp
754 ed83bff7 2019-02-05 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
755 ed83bff7 2019-02-05 stsp if (de == NULL) {
756 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
757 ed83bff7 2019-02-05 stsp goto done;
758 ed83bff7 2019-02-05 stsp }
759 ed83bff7 2019-02-05 stsp
760 ed83bff7 2019-02-05 stsp if (readdir_r(dir, de, &dep) != 0) {
761 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
762 95e06996 2019-02-05 stsp free(de);
763 ed83bff7 2019-02-05 stsp goto done;
764 ed83bff7 2019-02-05 stsp }
765 ed83bff7 2019-02-05 stsp if (dep == NULL) {
766 ed83bff7 2019-02-05 stsp free(de);
767 500cd40f 2019-02-04 stsp break;
768 ed83bff7 2019-02-05 stsp }
769 500cd40f 2019-02-04 stsp
770 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
771 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
772 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
773 ed83bff7 2019-02-05 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
774 ed83bff7 2019-02-05 stsp free(de);
775 b1ec3986 2019-02-04 stsp continue;
776 ed83bff7 2019-02-05 stsp }
777 b25ae4fa 2019-02-04 stsp
778 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
779 f5d3d7af 2019-02-05 stsp if (err)
780 f5d3d7af 2019-02-05 stsp goto done;
781 f5d3d7af 2019-02-05 stsp if (new == NULL) {
782 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
783 f5d3d7af 2019-02-05 stsp goto done;
784 f5d3d7af 2019-02-05 stsp }
785 500cd40f 2019-02-04 stsp }
786 b25ae4fa 2019-02-04 stsp
787 f5d3d7af 2019-02-05 stsp dle = TAILQ_FIRST(&dirlist);
788 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
789 500cd40f 2019-02-04 stsp if (dle && *ie) {
790 18831e78 2019-02-10 stsp char *de_path;
791 e7a2f030 2019-02-05 stsp int cmp;
792 f5d3d7af 2019-02-05 stsp de = dle->data;
793 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
794 18831e78 2019-02-10 stsp de->d_name) == -1) {
795 18831e78 2019-02-10 stsp err = got_error_from_errno();
796 13ff9e90 2019-02-10 stsp break;
797 18831e78 2019-02-10 stsp }
798 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
799 18831e78 2019-02-10 stsp free(de_path);
800 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
801 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
802 d1f6d47b 2019-02-04 stsp if (err)
803 d1f6d47b 2019-02-04 stsp break;
804 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
805 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
806 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
807 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
808 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
809 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
810 d1f6d47b 2019-02-04 stsp if (err)
811 d1f6d47b 2019-02-04 stsp break;
812 d1f6d47b 2019-02-04 stsp *ie = next;
813 d1f6d47b 2019-02-04 stsp } else {
814 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
815 d1f6d47b 2019-02-04 stsp if (err)
816 d1f6d47b 2019-02-04 stsp break;
817 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
818 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
819 d1f6d47b 2019-02-04 stsp }
820 554b91b1 2019-02-04 stsp if (err)
821 554b91b1 2019-02-04 stsp break;
822 554b91b1 2019-02-04 stsp } else if (*ie) {
823 554b91b1 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
824 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
825 554b91b1 2019-02-04 stsp if (err)
826 554b91b1 2019-02-04 stsp break;
827 554b91b1 2019-02-04 stsp *ie = next;
828 554b91b1 2019-02-04 stsp } else if (dle) {
829 763e1377 2019-02-05 stsp de = dle->data;
830 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
831 d1f6d47b 2019-02-04 stsp if (err)
832 d1f6d47b 2019-02-04 stsp break;
833 554b91b1 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path, dir,
834 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
835 554b91b1 2019-02-04 stsp if (err)
836 554b91b1 2019-02-04 stsp break;
837 d1f6d47b 2019-02-04 stsp }
838 500cd40f 2019-02-04 stsp }
839 f5d3d7af 2019-02-05 stsp done:
840 ed83bff7 2019-02-05 stsp TAILQ_FOREACH(dle, &dirlist, entry)
841 ed83bff7 2019-02-05 stsp free(dle->data);
842 f5d3d7af 2019-02-05 stsp got_pathlist_free(&dirlist);
843 d1f6d47b 2019-02-04 stsp return err;
844 8da9e5f4 2019-01-12 stsp }
845 8da9e5f4 2019-01-12 stsp
846 d1f6d47b 2019-02-04 stsp const struct got_error *
847 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
848 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
849 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
850 d1f6d47b 2019-02-04 stsp {
851 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
852 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
853 927df6b7 2019-02-10 stsp return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
854 c7f4312f 2019-02-05 stsp repo, cb, cb_arg);
855 d1f6d47b 2019-02-04 stsp }
856 d1f6d47b 2019-02-04 stsp
857 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);