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