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