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