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