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