Blame


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