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