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 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 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
58 51514078 2018-12-25 stsp else
59 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
60 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
61 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_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 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
89 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_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 a7f9d64d 2019-01-13 stsp entry->flags |= GOT_FILEIDX_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 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
304 c34b20a2 2018-03-12 stsp
305 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
306 c34b20a2 2018-03-12 stsp
307 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
308 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
309 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
310 c34b20a2 2018-03-12 stsp
311 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
312 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
313 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
314 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
315 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
316 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
317 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
318 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
319 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
320 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
321 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
322 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
323 c34b20a2 2018-03-12 stsp
324 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
325 a7f9d64d 2019-01-13 stsp entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
326 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
327 133d2798 2019-01-08 stsp if (err)
328 133d2798 2019-01-08 stsp return err;
329 133d2798 2019-01-08 stsp }
330 c34b20a2 2018-03-12 stsp
331 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
332 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
333 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
334 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
335 52a74475 2018-12-24 stsp
336 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
337 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
338 27d0e5bd 2019-01-12 stsp
339 52a74475 2018-12-24 stsp return NULL;
340 52a74475 2018-12-24 stsp }
341 52a74475 2018-12-24 stsp
342 52a74475 2018-12-24 stsp static const struct got_error *
343 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
344 52a74475 2018-12-24 stsp {
345 52a74475 2018-12-24 stsp size_t n;
346 52a74475 2018-12-24 stsp
347 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
348 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
349 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
350 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
351 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
352 52a74475 2018-12-24 stsp return NULL;
353 52a74475 2018-12-24 stsp }
354 52a74475 2018-12-24 stsp
355 52a74475 2018-12-24 stsp static const struct got_error *
356 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
357 52a74475 2018-12-24 stsp {
358 52a74475 2018-12-24 stsp size_t n;
359 52a74475 2018-12-24 stsp
360 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
361 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
362 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
363 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
364 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
365 52a74475 2018-12-24 stsp return NULL;
366 52a74475 2018-12-24 stsp }
367 52a74475 2018-12-24 stsp
368 52a74475 2018-12-24 stsp static const struct got_error *
369 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
370 52a74475 2018-12-24 stsp {
371 52a74475 2018-12-24 stsp size_t n;
372 52a74475 2018-12-24 stsp
373 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
374 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
375 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
376 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
377 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
378 52a74475 2018-12-24 stsp return NULL;
379 52a74475 2018-12-24 stsp }
380 52a74475 2018-12-24 stsp
381 52a74475 2018-12-24 stsp static const struct got_error *
382 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
383 52a74475 2018-12-24 stsp {
384 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
385 52a74475 2018-12-24 stsp uint8_t buf[8];
386 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
387 52a74475 2018-12-24 stsp
388 52a74475 2018-12-24 stsp *path = malloc(totlen);
389 52a74475 2018-12-24 stsp if (*path == NULL)
390 52a74475 2018-12-24 stsp return got_error_from_errno();
391 52a74475 2018-12-24 stsp
392 52a74475 2018-12-24 stsp do {
393 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
394 52a74475 2018-12-24 stsp if (n != sizeof(buf))
395 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
396 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
397 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
398 52a74475 2018-12-24 stsp if (p == NULL) {
399 52a74475 2018-12-24 stsp err = got_error_from_errno();
400 52a74475 2018-12-24 stsp break;
401 52a74475 2018-12-24 stsp }
402 52a74475 2018-12-24 stsp totlen += sizeof(buf);
403 52a74475 2018-12-24 stsp *path = p;
404 52a74475 2018-12-24 stsp }
405 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
406 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
407 52a74475 2018-12-24 stsp len += sizeof(buf);
408 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
409 52a74475 2018-12-24 stsp
410 52a74475 2018-12-24 stsp if (err) {
411 52a74475 2018-12-24 stsp free(*path);
412 52a74475 2018-12-24 stsp *path = NULL;
413 52a74475 2018-12-24 stsp }
414 52a74475 2018-12-24 stsp return err;
415 52a74475 2018-12-24 stsp }
416 52a74475 2018-12-24 stsp
417 52a74475 2018-12-24 stsp static const struct got_error *
418 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
419 52a74475 2018-12-24 stsp FILE *infile)
420 52a74475 2018-12-24 stsp {
421 52a74475 2018-12-24 stsp const struct got_error *err;
422 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
423 52a74475 2018-12-24 stsp size_t n;
424 52a74475 2018-12-24 stsp
425 52a74475 2018-12-24 stsp *entryp = NULL;
426 52a74475 2018-12-24 stsp
427 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
428 52a74475 2018-12-24 stsp if (entry == NULL)
429 52a74475 2018-12-24 stsp return got_error_from_errno();
430 c34b20a2 2018-03-12 stsp
431 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
432 52a74475 2018-12-24 stsp if (err)
433 52a74475 2018-12-24 stsp goto done;
434 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
435 52a74475 2018-12-24 stsp if (err)
436 52a74475 2018-12-24 stsp goto done;
437 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_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->mtime_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
444 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
445 52a74475 2018-12-24 stsp if (err)
446 52a74475 2018-12-24 stsp goto done;
447 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
448 52a74475 2018-12-24 stsp if (err)
449 52a74475 2018-12-24 stsp goto done;
450 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
451 52a74475 2018-12-24 stsp if (err)
452 52a74475 2018-12-24 stsp goto done;
453 52a74475 2018-12-24 stsp
454 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
455 52a74475 2018-12-24 stsp if (err)
456 52a74475 2018-12-24 stsp goto done;
457 52a74475 2018-12-24 stsp
458 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
459 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
460 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
461 52a74475 2018-12-24 stsp goto done;
462 52a74475 2018-12-24 stsp }
463 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
464 52a74475 2018-12-24 stsp
465 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
466 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
467 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
468 fc76cabb 2018-12-25 stsp goto done;
469 fc76cabb 2018-12-25 stsp }
470 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
471 fc76cabb 2018-12-25 stsp
472 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
473 52a74475 2018-12-24 stsp if (err)
474 52a74475 2018-12-24 stsp goto done;
475 52a74475 2018-12-24 stsp
476 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
477 52a74475 2018-12-24 stsp done:
478 52a74475 2018-12-24 stsp if (err)
479 52a74475 2018-12-24 stsp free(entry);
480 52a74475 2018-12-24 stsp else
481 52a74475 2018-12-24 stsp *entryp = entry;
482 52a74475 2018-12-24 stsp return err;
483 52a74475 2018-12-24 stsp }
484 52a74475 2018-12-24 stsp
485 52a74475 2018-12-24 stsp const struct got_error *
486 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
487 52a74475 2018-12-24 stsp {
488 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
489 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
490 52a74475 2018-12-24 stsp SHA1_CTX ctx;
491 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
492 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
493 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
494 52a74475 2018-12-24 stsp size_t n;
495 52a74475 2018-12-24 stsp int i;
496 52a74475 2018-12-24 stsp
497 52a74475 2018-12-24 stsp SHA1Init(&ctx);
498 52a74475 2018-12-24 stsp
499 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
500 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
501 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
502 b6d05318 2019-01-13 stsp return NULL;
503 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
504 b6d05318 2019-01-13 stsp }
505 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
506 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
507 51514078 2018-12-25 stsp if (n == 0) /* EOF */
508 51514078 2018-12-25 stsp return NULL;
509 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
510 51514078 2018-12-25 stsp }
511 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
512 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
513 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
514 b6d05318 2019-01-13 stsp return NULL;
515 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 b6d05318 2019-01-13 stsp }
517 52a74475 2018-12-24 stsp
518 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
519 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
520 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
521 52a74475 2018-12-24 stsp
522 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
523 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
524 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
525 52a74475 2018-12-24 stsp
526 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
527 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
528 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
529 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
530 52a74475 2018-12-24 stsp
531 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
532 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
533 52a74475 2018-12-24 stsp if (err)
534 52a74475 2018-12-24 stsp return err;
535 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
536 52a74475 2018-12-24 stsp if (err)
537 52a74475 2018-12-24 stsp return err;
538 52a74475 2018-12-24 stsp }
539 52a74475 2018-12-24 stsp
540 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
541 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
542 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
544 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
545 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
546 52a74475 2018-12-24 stsp
547 9d31a1d8 2018-03-11 stsp return NULL;
548 9d31a1d8 2018-03-11 stsp }
549 8da9e5f4 2019-01-12 stsp
550 8da9e5f4 2019-01-12 stsp static int
551 8da9e5f4 2019-01-12 stsp in_same_subdir(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 (!got_path_is_child(ie->path, parent_path, parent_len))
558 8da9e5f4 2019-01-12 stsp return 0;
559 133d2798 2019-01-08 stsp
560 8da9e5f4 2019-01-12 stsp ie_name = ie->path + parent_len;
561 8da9e5f4 2019-01-12 stsp while (ie_name[0] == '/')
562 8da9e5f4 2019-01-12 stsp ie_name++;
563 8da9e5f4 2019-01-12 stsp
564 50952927 2019-01-12 stsp return strchr(ie_name, '/') == NULL;
565 8da9e5f4 2019-01-12 stsp }
566 8da9e5f4 2019-01-12 stsp
567 bd4792ec 2019-01-13 stsp /*
568 bd4792ec 2019-01-13 stsp * Decide whether ie or te are equivalent, and if they aren't,
569 bd4792ec 2019-01-13 stsp * then decide which should be processed first.
570 bd4792ec 2019-01-13 stsp */
571 8da9e5f4 2019-01-12 stsp static int
572 8da9e5f4 2019-01-12 stsp cmp_entries(struct got_fileindex_entry *ie, const char *parent_path,
573 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te)
574 8da9e5f4 2019-01-12 stsp {
575 8da9e5f4 2019-01-12 stsp size_t parent_len = strlen(parent_path);
576 bd4792ec 2019-01-13 stsp int cmp;
577 8da9e5f4 2019-01-12 stsp
578 8da9e5f4 2019-01-12 stsp if (!in_same_subdir(ie, parent_path, te)) {
579 bd4792ec 2019-01-13 stsp cmp = strncmp(ie->path, parent_path, parent_len);
580 21908da4 2019-01-13 stsp if (cmp == 0) {
581 21908da4 2019-01-13 stsp char *ie_name = ie->path + parent_len;
582 21908da4 2019-01-13 stsp while (ie_name[0] == '/')
583 21908da4 2019-01-13 stsp ie_name++;
584 21908da4 2019-01-13 stsp cmp = strcmp(ie_name, te->name);
585 21908da4 2019-01-13 stsp }
586 bd4792ec 2019-01-13 stsp } else {
587 bd4792ec 2019-01-13 stsp char *ie_name = ie->path + parent_len;
588 bd4792ec 2019-01-13 stsp while (ie_name[0] == '/')
589 bd4792ec 2019-01-13 stsp ie_name++;
590 bd4792ec 2019-01-13 stsp cmp = strcmp(ie_name, te->name);
591 8da9e5f4 2019-01-12 stsp }
592 bd4792ec 2019-01-13 stsp return cmp;
593 8da9e5f4 2019-01-12 stsp
594 8da9e5f4 2019-01-12 stsp }
595 8da9e5f4 2019-01-12 stsp
596 8da9e5f4 2019-01-12 stsp static const struct got_error *
597 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
598 8da9e5f4 2019-01-12 stsp struct got_tree_object *, const char *, struct got_repository *,
599 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *, void *);
600 8da9e5f4 2019-01-12 stsp
601 50952927 2019-01-12 stsp struct got_fileindex_entry *
602 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
603 50952927 2019-01-12 stsp {
604 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
605 50952927 2019-01-12 stsp
606 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
607 50952927 2019-01-12 stsp
608 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
609 a7f9d64d 2019-01-13 stsp while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
610 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
611 50952927 2019-01-12 stsp
612 50952927 2019-01-12 stsp return next;
613 50952927 2019-01-12 stsp }
614 50952927 2019-01-12 stsp
615 8da9e5f4 2019-01-12 stsp static const struct got_error *
616 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
617 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
618 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
619 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
620 8da9e5f4 2019-01-12 stsp {
621 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
622 8da9e5f4 2019-01-12 stsp
623 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
624 8da9e5f4 2019-01-12 stsp char *subpath;
625 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
626 8da9e5f4 2019-01-12 stsp
627 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
628 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
629 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
630 8da9e5f4 2019-01-12 stsp
631 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
632 8da9e5f4 2019-01-12 stsp if (err) {
633 8da9e5f4 2019-01-12 stsp free(subpath);
634 8da9e5f4 2019-01-12 stsp return err;
635 8da9e5f4 2019-01-12 stsp }
636 8da9e5f4 2019-01-12 stsp
637 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
638 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
639 8da9e5f4 2019-01-12 stsp free(subpath);
640 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
641 8da9e5f4 2019-01-12 stsp if (err)
642 8da9e5f4 2019-01-12 stsp return err;
643 8da9e5f4 2019-01-12 stsp }
644 8da9e5f4 2019-01-12 stsp
645 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
646 8da9e5f4 2019-01-12 stsp return NULL;
647 8da9e5f4 2019-01-12 stsp }
648 8da9e5f4 2019-01-12 stsp
649 8da9e5f4 2019-01-12 stsp static const struct got_error *
650 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
651 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
652 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
653 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
654 8da9e5f4 2019-01-12 stsp {
655 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
656 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
657 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
658 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
659 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
660 8da9e5f4 2019-01-12 stsp
661 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
662 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
663 8da9e5f4 2019-01-12 stsp do {
664 8da9e5f4 2019-01-12 stsp if (te && *ie) {
665 8da9e5f4 2019-01-12 stsp int cmp = cmp_entries(*ie, path, te);
666 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
667 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
668 8da9e5f4 2019-01-12 stsp path);
669 8da9e5f4 2019-01-12 stsp if (err)
670 8da9e5f4 2019-01-12 stsp break;
671 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
672 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
673 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
674 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
675 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
676 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
677 8da9e5f4 2019-01-12 stsp if (err)
678 8da9e5f4 2019-01-12 stsp break;
679 8da9e5f4 2019-01-12 stsp *ie = next;
680 8da9e5f4 2019-01-12 stsp } else {
681 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
682 8da9e5f4 2019-01-12 stsp if (err)
683 8da9e5f4 2019-01-12 stsp break;
684 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
685 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
686 8da9e5f4 2019-01-12 stsp }
687 8da9e5f4 2019-01-12 stsp if (err)
688 8da9e5f4 2019-01-12 stsp break;
689 8da9e5f4 2019-01-12 stsp } else if (*ie) {
690 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
691 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
692 8da9e5f4 2019-01-12 stsp if (err)
693 8da9e5f4 2019-01-12 stsp break;
694 8da9e5f4 2019-01-12 stsp *ie = next;
695 8da9e5f4 2019-01-12 stsp } else if (te) {
696 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
697 8da9e5f4 2019-01-12 stsp if (err)
698 8da9e5f4 2019-01-12 stsp break;
699 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
700 8da9e5f4 2019-01-12 stsp cb_arg);
701 8da9e5f4 2019-01-12 stsp if (err)
702 8da9e5f4 2019-01-12 stsp break;
703 8da9e5f4 2019-01-12 stsp }
704 8da9e5f4 2019-01-12 stsp } while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te);
705 8da9e5f4 2019-01-12 stsp
706 8da9e5f4 2019-01-12 stsp return err;
707 8da9e5f4 2019-01-12 stsp }
708 8da9e5f4 2019-01-12 stsp
709 8da9e5f4 2019-01-12 stsp const struct got_error *
710 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
711 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
712 8da9e5f4 2019-01-12 stsp struct got_fileindex_diff_cb *cb, void *cb_arg)
713 8da9e5f4 2019-01-12 stsp {
714 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
715 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
716 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
717 8da9e5f4 2019-01-12 stsp }
718 8da9e5f4 2019-01-12 stsp
719 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);