Blame


1 c48c4a9c 2018-03-11 stsp /*
2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 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 c48c4a9c 2018-03-11 stsp #include <stdio.h>
22 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
23 c48c4a9c 2018-03-11 stsp #include <string.h>
24 c48c4a9c 2018-03-11 stsp #include <sha1.h>
25 c34b20a2 2018-03-12 stsp #include <endian.h>
26 133d2798 2019-01-08 stsp #include <limits.h>
27 c48c4a9c 2018-03-11 stsp
28 c48c4a9c 2018-03-11 stsp #include "got_error.h"
29 8da9e5f4 2019-01-12 stsp #include "got_object.h"
30 c48c4a9c 2018-03-11 stsp
31 133d2798 2019-01-08 stsp #include "got_lib_path.h"
32 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
33 c48c4a9c 2018-03-11 stsp
34 133d2798 2019-01-08 stsp struct got_fileindex {
35 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
36 133d2798 2019-01-08 stsp int nentries;
37 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
38 133d2798 2019-01-08 stsp };
39 133d2798 2019-01-08 stsp
40 c48c4a9c 2018-03-11 stsp const struct got_error *
41 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
42 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
43 c48c4a9c 2018-03-11 stsp {
44 c48c4a9c 2018-03-11 stsp struct stat sb;
45 c48c4a9c 2018-03-11 stsp
46 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
47 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
48 c48c4a9c 2018-03-11 stsp
49 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
50 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
51 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
52 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
53 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
54 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
55 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
56 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
57 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
58 51514078 2018-12-25 stsp else
59 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
60 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
61 51514078 2018-12-25 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
62 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
63 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
64 51514078 2018-12-25 stsp
65 51514078 2018-12-25 stsp return NULL;
66 51514078 2018-12-25 stsp }
67 51514078 2018-12-25 stsp
68 51514078 2018-12-25 stsp const struct got_error *
69 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
70 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
71 51514078 2018-12-25 stsp uint8_t *commit_sha1)
72 51514078 2018-12-25 stsp {
73 51514078 2018-12-25 stsp size_t len;
74 51514078 2018-12-25 stsp
75 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
76 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
77 0a585a0d 2018-03-17 stsp return got_error_from_errno();
78 c48c4a9c 2018-03-11 stsp
79 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
80 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
81 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
82 c48c4a9c 2018-03-11 stsp free(*entry);
83 c48c4a9c 2018-03-11 stsp *entry = NULL;
84 0a585a0d 2018-03-17 stsp return err;
85 c48c4a9c 2018-03-11 stsp }
86 51514078 2018-12-25 stsp
87 c34b20a2 2018-03-12 stsp len = strlen(relpath);
88 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
89 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
90 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
91 c48c4a9c 2018-03-11 stsp
92 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
93 51514078 2018-12-25 stsp commit_sha1);
94 c48c4a9c 2018-03-11 stsp }
95 c48c4a9c 2018-03-11 stsp
96 c48c4a9c 2018-03-11 stsp void
97 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
98 c48c4a9c 2018-03-11 stsp {
99 c48c4a9c 2018-03-11 stsp free(entry->path);
100 c48c4a9c 2018-03-11 stsp free(entry);
101 c48c4a9c 2018-03-11 stsp }
102 9d31a1d8 2018-03-11 stsp
103 50952927 2019-01-12 stsp static const struct got_error *
104 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
105 9d31a1d8 2018-03-11 stsp {
106 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
108 133d2798 2019-01-08 stsp
109 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 133d2798 2019-01-08 stsp fileindex->nentries++;
111 133d2798 2019-01-08 stsp return NULL;
112 50952927 2019-01-12 stsp }
113 50952927 2019-01-12 stsp
114 50952927 2019-01-12 stsp const struct got_error *
115 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
116 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
117 50952927 2019-01-12 stsp {
118 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
119 50952927 2019-01-12 stsp entry->flags |= GOT_INDEX_ENTRY_F_INTENT_TO_ADD;
120 50952927 2019-01-12 stsp
121 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
122 9d31a1d8 2018-03-11 stsp }
123 9d31a1d8 2018-03-11 stsp
124 133d2798 2019-01-08 stsp void
125 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
126 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
127 512f0d0e 2019-01-02 stsp {
128 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
129 133d2798 2019-01-08 stsp fileindex->nentries--;
130 512f0d0e 2019-01-02 stsp }
131 512f0d0e 2019-01-02 stsp
132 51514078 2018-12-25 stsp struct got_fileindex_entry *
133 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
134 51514078 2018-12-25 stsp {
135 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
136 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
137 133d2798 2019-01-08 stsp key.path = (char *)path;
138 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
139 b504a804 2019-01-08 stsp }
140 51514078 2018-12-25 stsp
141 512f0d0e 2019-01-02 stsp const struct got_error *
142 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
143 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
144 512f0d0e 2019-01-02 stsp {
145 133d2798 2019-01-08 stsp const struct got_error *err;
146 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
147 9d31a1d8 2018-03-11 stsp
148 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
149 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
150 133d2798 2019-01-08 stsp if (err)
151 133d2798 2019-01-08 stsp return err;
152 b504a804 2019-01-08 stsp }
153 b504a804 2019-01-08 stsp return NULL;
154 9d31a1d8 2018-03-11 stsp }
155 9d31a1d8 2018-03-11 stsp
156 133d2798 2019-01-08 stsp struct got_fileindex *
157 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
158 6b798c3c 2019-01-08 stsp {
159 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
160 133d2798 2019-01-08 stsp
161 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
162 133d2798 2019-01-08 stsp if (fileindex == NULL)
163 133d2798 2019-01-08 stsp return NULL;
164 133d2798 2019-01-08 stsp
165 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
166 133d2798 2019-01-08 stsp return fileindex;
167 6b798c3c 2019-01-08 stsp }
168 6b798c3c 2019-01-08 stsp
169 9d31a1d8 2018-03-11 stsp void
170 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
171 9d31a1d8 2018-03-11 stsp {
172 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
173 133d2798 2019-01-08 stsp
174 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
175 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
176 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
177 133d2798 2019-01-08 stsp }
178 9d31a1d8 2018-03-11 stsp free(fileindex);
179 9d31a1d8 2018-03-11 stsp }
180 9d31a1d8 2018-03-11 stsp
181 c34b20a2 2018-03-12 stsp static const struct got_error *
182 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
183 c34b20a2 2018-03-12 stsp {
184 c34b20a2 2018-03-12 stsp size_t n;
185 c34b20a2 2018-03-12 stsp
186 c34b20a2 2018-03-12 stsp val = htobe64(val);
187 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
188 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
189 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
190 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
191 c34b20a2 2018-03-12 stsp return NULL;
192 c34b20a2 2018-03-12 stsp }
193 c34b20a2 2018-03-12 stsp
194 c34b20a2 2018-03-12 stsp static const struct got_error *
195 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
196 c34b20a2 2018-03-12 stsp {
197 c34b20a2 2018-03-12 stsp size_t n;
198 c34b20a2 2018-03-12 stsp
199 c34b20a2 2018-03-12 stsp val = htobe32(val);
200 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
201 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
202 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
203 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
204 c34b20a2 2018-03-12 stsp return NULL;
205 c34b20a2 2018-03-12 stsp }
206 c34b20a2 2018-03-12 stsp
207 c34b20a2 2018-03-12 stsp static const struct got_error *
208 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
209 c34b20a2 2018-03-12 stsp {
210 c34b20a2 2018-03-12 stsp size_t n;
211 c34b20a2 2018-03-12 stsp
212 c34b20a2 2018-03-12 stsp val = htobe16(val);
213 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
214 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
215 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
216 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
217 c34b20a2 2018-03-12 stsp return NULL;
218 c34b20a2 2018-03-12 stsp }
219 c34b20a2 2018-03-12 stsp
220 c34b20a2 2018-03-12 stsp static const struct got_error *
221 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
222 c34b20a2 2018-03-12 stsp {
223 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
224 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
225 c34b20a2 2018-03-12 stsp
226 c34b20a2 2018-03-12 stsp len = strlen(path);
227 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
228 3c5b70f2 2018-12-27 stsp pad++;
229 3c5b70f2 2018-12-27 stsp if (pad == 0)
230 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
231 c34b20a2 2018-03-12 stsp
232 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
233 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
234 c34b20a2 2018-03-12 stsp if (n != len)
235 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
236 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
237 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
238 c34b20a2 2018-03-12 stsp if (n != pad)
239 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
240 c34b20a2 2018-03-12 stsp return NULL;
241 c34b20a2 2018-03-12 stsp }
242 c34b20a2 2018-03-12 stsp
243 c34b20a2 2018-03-12 stsp static const struct got_error *
244 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
245 c34b20a2 2018-03-12 stsp FILE *outfile)
246 c34b20a2 2018-03-12 stsp {
247 c34b20a2 2018-03-12 stsp const struct got_error *err;
248 23b19d00 2018-03-12 stsp size_t n;
249 c34b20a2 2018-03-12 stsp
250 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
251 c34b20a2 2018-03-12 stsp if (err)
252 c34b20a2 2018-03-12 stsp return err;
253 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
254 c34b20a2 2018-03-12 stsp if (err)
255 c34b20a2 2018-03-12 stsp return err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_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->mtime_nsec, outfile);
260 c34b20a2 2018-03-12 stsp if (err)
261 c34b20a2 2018-03-12 stsp return err;
262 c34b20a2 2018-03-12 stsp
263 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
264 c34b20a2 2018-03-12 stsp if (err)
265 c34b20a2 2018-03-12 stsp return err;
266 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
267 c34b20a2 2018-03-12 stsp if (err)
268 c34b20a2 2018-03-12 stsp return err;
269 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
270 c34b20a2 2018-03-12 stsp if (err)
271 c34b20a2 2018-03-12 stsp return err;
272 c34b20a2 2018-03-12 stsp
273 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
274 c34b20a2 2018-03-12 stsp if (err)
275 c34b20a2 2018-03-12 stsp return err;
276 c34b20a2 2018-03-12 stsp
277 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
278 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
279 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
280 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
281 fc76cabb 2018-12-25 stsp
282 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
283 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
284 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
285 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
286 c34b20a2 2018-03-12 stsp
287 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
288 c34b20a2 2018-03-12 stsp if (err)
289 c34b20a2 2018-03-12 stsp return err;
290 c34b20a2 2018-03-12 stsp
291 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
292 c34b20a2 2018-03-12 stsp return err;
293 c34b20a2 2018-03-12 stsp }
294 c34b20a2 2018-03-12 stsp
295 9d31a1d8 2018-03-11 stsp const struct got_error *
296 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
297 9d31a1d8 2018-03-11 stsp {
298 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
299 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
300 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
301 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
302 c34b20a2 2018-03-12 stsp size_t n;
303 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
304 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
305 c34b20a2 2018-03-12 stsp uint8_t buf[len];
306 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
307 c34b20a2 2018-03-12 stsp
308 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
309 c34b20a2 2018-03-12 stsp
310 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
311 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
312 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
313 c34b20a2 2018-03-12 stsp
314 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
315 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
316 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
317 c34b20a2 2018-03-12 stsp if (n != len)
318 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
319 c34b20a2 2018-03-12 stsp
320 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
321 50952927 2019-01-12 stsp entry->flags &= ~GOT_INDEX_ENTRY_F_INTENT_TO_ADD;
322 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
323 133d2798 2019-01-08 stsp if (err)
324 133d2798 2019-01-08 stsp return err;
325 133d2798 2019-01-08 stsp }
326 c34b20a2 2018-03-12 stsp
327 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
328 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
329 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
330 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
331 52a74475 2018-12-24 stsp
332 52a74475 2018-12-24 stsp return NULL;
333 52a74475 2018-12-24 stsp }
334 52a74475 2018-12-24 stsp
335 52a74475 2018-12-24 stsp static const struct got_error *
336 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
337 52a74475 2018-12-24 stsp {
338 52a74475 2018-12-24 stsp size_t n;
339 52a74475 2018-12-24 stsp
340 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
341 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
342 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
343 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
344 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
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_val32(uint32_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 = be32toh(*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_val16(uint16_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 = be16toh(*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_path(char **path, SHA1_CTX *ctx, FILE *infile)
376 52a74475 2018-12-24 stsp {
377 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
378 52a74475 2018-12-24 stsp uint8_t buf[8];
379 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
380 52a74475 2018-12-24 stsp
381 52a74475 2018-12-24 stsp *path = malloc(totlen);
382 52a74475 2018-12-24 stsp if (*path == NULL)
383 52a74475 2018-12-24 stsp return got_error_from_errno();
384 52a74475 2018-12-24 stsp
385 52a74475 2018-12-24 stsp do {
386 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
387 52a74475 2018-12-24 stsp if (n != sizeof(buf))
388 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
389 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
390 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
391 52a74475 2018-12-24 stsp if (p == NULL) {
392 52a74475 2018-12-24 stsp err = got_error_from_errno();
393 52a74475 2018-12-24 stsp break;
394 52a74475 2018-12-24 stsp }
395 52a74475 2018-12-24 stsp totlen += sizeof(buf);
396 52a74475 2018-12-24 stsp *path = p;
397 52a74475 2018-12-24 stsp }
398 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
399 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
400 52a74475 2018-12-24 stsp len += sizeof(buf);
401 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
402 52a74475 2018-12-24 stsp
403 52a74475 2018-12-24 stsp if (err) {
404 52a74475 2018-12-24 stsp free(*path);
405 52a74475 2018-12-24 stsp *path = NULL;
406 52a74475 2018-12-24 stsp }
407 52a74475 2018-12-24 stsp return err;
408 52a74475 2018-12-24 stsp }
409 52a74475 2018-12-24 stsp
410 52a74475 2018-12-24 stsp static const struct got_error *
411 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
412 52a74475 2018-12-24 stsp FILE *infile)
413 52a74475 2018-12-24 stsp {
414 52a74475 2018-12-24 stsp const struct got_error *err;
415 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
416 52a74475 2018-12-24 stsp size_t n;
417 52a74475 2018-12-24 stsp
418 52a74475 2018-12-24 stsp *entryp = NULL;
419 52a74475 2018-12-24 stsp
420 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
421 52a74475 2018-12-24 stsp if (entry == NULL)
422 52a74475 2018-12-24 stsp return got_error_from_errno();
423 c34b20a2 2018-03-12 stsp
424 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
425 52a74475 2018-12-24 stsp if (err)
426 52a74475 2018-12-24 stsp goto done;
427 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
428 52a74475 2018-12-24 stsp if (err)
429 52a74475 2018-12-24 stsp goto done;
430 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
431 52a74475 2018-12-24 stsp if (err)
432 52a74475 2018-12-24 stsp goto done;
433 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
434 52a74475 2018-12-24 stsp if (err)
435 52a74475 2018-12-24 stsp goto done;
436 52a74475 2018-12-24 stsp
437 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, 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_val32(&entry->gid, 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_val32(&entry->size, ctx, infile);
444 52a74475 2018-12-24 stsp if (err)
445 52a74475 2018-12-24 stsp goto done;
446 52a74475 2018-12-24 stsp
447 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
448 52a74475 2018-12-24 stsp if (err)
449 52a74475 2018-12-24 stsp goto done;
450 52a74475 2018-12-24 stsp
451 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
452 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
453 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
454 52a74475 2018-12-24 stsp goto done;
455 52a74475 2018-12-24 stsp }
456 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
457 52a74475 2018-12-24 stsp
458 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
459 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
460 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
461 fc76cabb 2018-12-25 stsp goto done;
462 fc76cabb 2018-12-25 stsp }
463 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
464 fc76cabb 2018-12-25 stsp
465 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
466 52a74475 2018-12-24 stsp if (err)
467 52a74475 2018-12-24 stsp goto done;
468 52a74475 2018-12-24 stsp
469 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
470 52a74475 2018-12-24 stsp done:
471 52a74475 2018-12-24 stsp if (err)
472 52a74475 2018-12-24 stsp free(entry);
473 52a74475 2018-12-24 stsp else
474 52a74475 2018-12-24 stsp *entryp = entry;
475 52a74475 2018-12-24 stsp return err;
476 52a74475 2018-12-24 stsp }
477 52a74475 2018-12-24 stsp
478 52a74475 2018-12-24 stsp const struct got_error *
479 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
480 52a74475 2018-12-24 stsp {
481 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
482 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
483 52a74475 2018-12-24 stsp SHA1_CTX ctx;
484 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
485 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
486 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
487 52a74475 2018-12-24 stsp size_t n;
488 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
489 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
490 52a74475 2018-12-24 stsp uint8_t buf[len];
491 52a74475 2018-12-24 stsp int i;
492 52a74475 2018-12-24 stsp
493 52a74475 2018-12-24 stsp SHA1Init(&ctx);
494 52a74475 2018-12-24 stsp
495 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
496 51514078 2018-12-25 stsp if (n != len) {
497 51514078 2018-12-25 stsp if (n == 0) /* EOF */
498 51514078 2018-12-25 stsp return NULL;
499 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
500 51514078 2018-12-25 stsp }
501 52a74475 2018-12-24 stsp
502 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
503 52a74475 2018-12-24 stsp
504 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
505 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
506 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
507 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
508 52a74475 2018-12-24 stsp
509 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
510 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
511 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
512 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
513 52a74475 2018-12-24 stsp
514 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
515 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
516 52a74475 2018-12-24 stsp if (err)
517 52a74475 2018-12-24 stsp return err;
518 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
519 52a74475 2018-12-24 stsp if (err)
520 52a74475 2018-12-24 stsp return err;
521 52a74475 2018-12-24 stsp }
522 52a74475 2018-12-24 stsp
523 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
524 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
525 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
526 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
527 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
528 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
529 52a74475 2018-12-24 stsp
530 9d31a1d8 2018-03-11 stsp return NULL;
531 9d31a1d8 2018-03-11 stsp }
532 8da9e5f4 2019-01-12 stsp
533 8da9e5f4 2019-01-12 stsp static int
534 8da9e5f4 2019-01-12 stsp in_same_subdir(struct got_fileindex_entry *ie, const char *parent_path,
535 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te)
536 8da9e5f4 2019-01-12 stsp {
537 8da9e5f4 2019-01-12 stsp size_t parent_len = strlen(parent_path);
538 8da9e5f4 2019-01-12 stsp char *ie_name;
539 8da9e5f4 2019-01-12 stsp
540 8da9e5f4 2019-01-12 stsp if (!got_path_is_child(ie->path, parent_path, parent_len))
541 8da9e5f4 2019-01-12 stsp return 0;
542 133d2798 2019-01-08 stsp
543 8da9e5f4 2019-01-12 stsp ie_name = ie->path + parent_len;
544 8da9e5f4 2019-01-12 stsp while (ie_name[0] == '/')
545 8da9e5f4 2019-01-12 stsp ie_name++;
546 8da9e5f4 2019-01-12 stsp
547 50952927 2019-01-12 stsp return strchr(ie_name, '/') == NULL;
548 8da9e5f4 2019-01-12 stsp }
549 8da9e5f4 2019-01-12 stsp
550 8da9e5f4 2019-01-12 stsp static int
551 8da9e5f4 2019-01-12 stsp cmp_entries(struct got_fileindex_entry *ie, const char *parent_path,
552 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te)
553 8da9e5f4 2019-01-12 stsp {
554 8da9e5f4 2019-01-12 stsp size_t parent_len = strlen(parent_path);
555 8da9e5f4 2019-01-12 stsp char *ie_name;
556 8da9e5f4 2019-01-12 stsp
557 8da9e5f4 2019-01-12 stsp if (!in_same_subdir(ie, parent_path, te)) {
558 8da9e5f4 2019-01-12 stsp if (parent_path[0])
559 50952927 2019-01-12 stsp return strcmp(ie->path, parent_path);
560 50952927 2019-01-12 stsp return strcmp(ie->path, te->name);
561 8da9e5f4 2019-01-12 stsp }
562 8da9e5f4 2019-01-12 stsp
563 8da9e5f4 2019-01-12 stsp ie_name = ie->path + parent_len;
564 8da9e5f4 2019-01-12 stsp while (ie_name[0] == '/')
565 8da9e5f4 2019-01-12 stsp ie_name++;
566 8da9e5f4 2019-01-12 stsp
567 50952927 2019-01-12 stsp return strcmp(ie_name, te->name);
568 8da9e5f4 2019-01-12 stsp }
569 8da9e5f4 2019-01-12 stsp
570 8da9e5f4 2019-01-12 stsp static const struct got_error *
571 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
572 8da9e5f4 2019-01-12 stsp struct got_tree_object *, const char *, struct got_repository *,
573 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *, void *);
574 8da9e5f4 2019-01-12 stsp
575 50952927 2019-01-12 stsp struct got_fileindex_entry *
576 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
577 50952927 2019-01-12 stsp {
578 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
579 50952927 2019-01-12 stsp
580 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
581 50952927 2019-01-12 stsp
582 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
583 50952927 2019-01-12 stsp while (next && (next->flags & GOT_INDEX_ENTRY_F_INTENT_TO_ADD))
584 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
585 50952927 2019-01-12 stsp
586 50952927 2019-01-12 stsp return next;
587 50952927 2019-01-12 stsp }
588 50952927 2019-01-12 stsp
589 8da9e5f4 2019-01-12 stsp static const struct got_error *
590 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
591 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
592 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
593 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
594 8da9e5f4 2019-01-12 stsp {
595 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
596 8da9e5f4 2019-01-12 stsp
597 8da9e5f4 2019-01-12 stsp if (te && S_ISREG(te->mode)) {
598 8da9e5f4 2019-01-12 stsp *next = SIMPLEQ_NEXT(te, entry);
599 8da9e5f4 2019-01-12 stsp return NULL;
600 8da9e5f4 2019-01-12 stsp }
601 8da9e5f4 2019-01-12 stsp
602 8da9e5f4 2019-01-12 stsp while (te && S_ISDIR(te->mode)) {
603 8da9e5f4 2019-01-12 stsp char *subpath;
604 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
605 8da9e5f4 2019-01-12 stsp
606 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
607 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
608 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
609 8da9e5f4 2019-01-12 stsp
610 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
611 8da9e5f4 2019-01-12 stsp if (err) {
612 8da9e5f4 2019-01-12 stsp free(subpath);
613 8da9e5f4 2019-01-12 stsp return err;
614 8da9e5f4 2019-01-12 stsp }
615 8da9e5f4 2019-01-12 stsp
616 8da9e5f4 2019-01-12 stsp if (*ie == NULL || !in_same_subdir(*ie, path, te)) {
617 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
618 8da9e5f4 2019-01-12 stsp if (err)
619 8da9e5f4 2019-01-12 stsp return err;
620 8da9e5f4 2019-01-12 stsp }
621 8da9e5f4 2019-01-12 stsp
622 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
623 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
624 8da9e5f4 2019-01-12 stsp free(subpath);
625 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
626 8da9e5f4 2019-01-12 stsp if (err)
627 8da9e5f4 2019-01-12 stsp return err;
628 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_NEXT(te, entry);
629 8da9e5f4 2019-01-12 stsp }
630 8da9e5f4 2019-01-12 stsp
631 8da9e5f4 2019-01-12 stsp *next = te;
632 8da9e5f4 2019-01-12 stsp return NULL;
633 8da9e5f4 2019-01-12 stsp }
634 8da9e5f4 2019-01-12 stsp
635 8da9e5f4 2019-01-12 stsp static const struct got_error *
636 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
637 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
638 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
639 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
640 8da9e5f4 2019-01-12 stsp {
641 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
642 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
643 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
644 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
645 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
646 8da9e5f4 2019-01-12 stsp
647 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
648 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
649 8da9e5f4 2019-01-12 stsp do {
650 8da9e5f4 2019-01-12 stsp if (te && *ie) {
651 8da9e5f4 2019-01-12 stsp int cmp = cmp_entries(*ie, path, te);
652 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
653 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
654 8da9e5f4 2019-01-12 stsp path);
655 8da9e5f4 2019-01-12 stsp if (err)
656 8da9e5f4 2019-01-12 stsp break;
657 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
658 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
659 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
660 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
661 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
662 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
663 8da9e5f4 2019-01-12 stsp if (err)
664 8da9e5f4 2019-01-12 stsp break;
665 8da9e5f4 2019-01-12 stsp *ie = next;
666 8da9e5f4 2019-01-12 stsp } else {
667 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
668 8da9e5f4 2019-01-12 stsp if (err)
669 8da9e5f4 2019-01-12 stsp break;
670 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
671 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
672 8da9e5f4 2019-01-12 stsp }
673 8da9e5f4 2019-01-12 stsp if (err)
674 8da9e5f4 2019-01-12 stsp break;
675 8da9e5f4 2019-01-12 stsp } else if (*ie) {
676 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
677 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
678 8da9e5f4 2019-01-12 stsp if (err)
679 8da9e5f4 2019-01-12 stsp break;
680 8da9e5f4 2019-01-12 stsp *ie = next;
681 8da9e5f4 2019-01-12 stsp } else if (te) {
682 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
683 8da9e5f4 2019-01-12 stsp if (err)
684 8da9e5f4 2019-01-12 stsp break;
685 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
686 8da9e5f4 2019-01-12 stsp cb_arg);
687 8da9e5f4 2019-01-12 stsp if (err)
688 8da9e5f4 2019-01-12 stsp break;
689 8da9e5f4 2019-01-12 stsp }
690 8da9e5f4 2019-01-12 stsp } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
691 8da9e5f4 2019-01-12 stsp
692 8da9e5f4 2019-01-12 stsp return err;
693 8da9e5f4 2019-01-12 stsp }
694 8da9e5f4 2019-01-12 stsp
695 8da9e5f4 2019-01-12 stsp const struct got_error *
696 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
697 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
698 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
699 8da9e5f4 2019-01-12 stsp {
700 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
701 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
702 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
703 8da9e5f4 2019-01-12 stsp }
704 8da9e5f4 2019-01-12 stsp
705 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);