2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
29 c48c4a9c 2018-03-11 stsp #include "got_error.h"
30 8da9e5f4 2019-01-12 stsp #include "got_object.h"
32 133d2798 2019-01-08 stsp #include "got_lib_path.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
34 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
36 133d2798 2019-01-08 stsp struct got_fileindex {
37 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
38 133d2798 2019-01-08 stsp int nentries;
39 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
42 c48c4a9c 2018-03-11 stsp const struct got_error *
43 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
44 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
46 c48c4a9c 2018-03-11 stsp struct stat sb;
48 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
49 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
51 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
52 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
53 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
54 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
55 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
56 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
57 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
58 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
59 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
61 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
62 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
63 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
64 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
65 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
67 51514078 2018-12-25 stsp return NULL;
70 51514078 2018-12-25 stsp const struct got_error *
71 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
72 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
73 51514078 2018-12-25 stsp uint8_t *commit_sha1)
77 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
78 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
79 0a585a0d 2018-03-17 stsp return got_error_from_errno();
81 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
82 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
83 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
84 c48c4a9c 2018-03-11 stsp free(*entry);
85 c48c4a9c 2018-03-11 stsp *entry = NULL;
89 c34b20a2 2018-03-12 stsp len = strlen(relpath);
90 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
91 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
92 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
94 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
95 51514078 2018-12-25 stsp commit_sha1);
99 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
101 c48c4a9c 2018-03-11 stsp free(entry->path);
102 c48c4a9c 2018-03-11 stsp free(entry);
105 50952927 2019-01-12 stsp static const struct got_error *
106 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
108 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
109 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
111 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
112 133d2798 2019-01-08 stsp fileindex->nentries++;
113 133d2798 2019-01-08 stsp return NULL;
116 50952927 2019-01-12 stsp const struct got_error *
117 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
118 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
120 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
121 a7f9d64d 2019-01-13 stsp entry->flags |= GOT_FILEIDX_F_INTENT_TO_ADD;
123 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
127 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
128 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
130 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
131 133d2798 2019-01-08 stsp fileindex->nentries--;
134 51514078 2018-12-25 stsp struct got_fileindex_entry *
135 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
137 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
138 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
139 133d2798 2019-01-08 stsp key.path = (char *)path;
140 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
143 512f0d0e 2019-01-02 stsp const struct got_error *
144 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
145 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
147 133d2798 2019-01-08 stsp const struct got_error *err;
148 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
150 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
151 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
153 133d2798 2019-01-08 stsp return err;
155 b504a804 2019-01-08 stsp return NULL;
158 133d2798 2019-01-08 stsp struct got_fileindex *
159 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
161 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
163 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
164 133d2798 2019-01-08 stsp if (fileindex == NULL)
165 133d2798 2019-01-08 stsp return NULL;
167 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
168 133d2798 2019-01-08 stsp return fileindex;
172 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
174 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
176 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
177 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
178 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
180 9d31a1d8 2018-03-11 stsp free(fileindex);
183 c34b20a2 2018-03-12 stsp static const struct got_error *
184 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
188 c34b20a2 2018-03-12 stsp val = htobe64(val);
189 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
190 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
191 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
192 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
193 c34b20a2 2018-03-12 stsp return NULL;
196 c34b20a2 2018-03-12 stsp static const struct got_error *
197 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
201 c34b20a2 2018-03-12 stsp val = htobe32(val);
202 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
203 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
204 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
205 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
206 c34b20a2 2018-03-12 stsp return NULL;
209 c34b20a2 2018-03-12 stsp static const struct got_error *
210 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
214 c34b20a2 2018-03-12 stsp val = htobe16(val);
215 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
216 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
217 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
218 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
219 c34b20a2 2018-03-12 stsp return NULL;
222 c34b20a2 2018-03-12 stsp static const struct got_error *
223 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
225 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
226 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
228 c34b20a2 2018-03-12 stsp len = strlen(path);
229 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
231 3c5b70f2 2018-12-27 stsp if (pad == 0)
232 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
234 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
235 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
236 c34b20a2 2018-03-12 stsp if (n != len)
237 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
238 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
239 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
240 c34b20a2 2018-03-12 stsp if (n != pad)
241 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
242 c34b20a2 2018-03-12 stsp return NULL;
245 c34b20a2 2018-03-12 stsp static const struct got_error *
246 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
247 c34b20a2 2018-03-12 stsp FILE *outfile)
249 c34b20a2 2018-03-12 stsp const struct got_error *err;
252 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
254 c34b20a2 2018-03-12 stsp return err;
255 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
257 c34b20a2 2018-03-12 stsp return err;
258 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
260 c34b20a2 2018-03-12 stsp return err;
261 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
263 c34b20a2 2018-03-12 stsp return err;
265 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
267 c34b20a2 2018-03-12 stsp return err;
268 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
270 c34b20a2 2018-03-12 stsp return err;
271 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
273 c34b20a2 2018-03-12 stsp return err;
275 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
277 c34b20a2 2018-03-12 stsp return err;
279 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
280 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
281 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
282 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
284 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
285 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
286 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
287 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
289 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
291 c34b20a2 2018-03-12 stsp return err;
293 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
294 c34b20a2 2018-03-12 stsp return err;
297 9d31a1d8 2018-03-11 stsp const struct got_error *
298 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
300 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
301 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
302 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
303 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
305 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
307 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
309 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
310 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
311 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
313 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
314 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
315 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
316 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
317 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
318 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
319 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
320 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
321 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
322 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
323 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
324 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
326 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
327 a7f9d64d 2019-01-13 stsp entry->flags &= ~GOT_FILEIDX_F_INTENT_TO_ADD;
328 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
330 133d2798 2019-01-08 stsp return err;
333 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
334 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
335 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
336 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
338 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
339 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
341 52a74475 2018-12-24 stsp return NULL;
344 52a74475 2018-12-24 stsp static const struct got_error *
345 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
349 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
350 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
351 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
352 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
353 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
354 52a74475 2018-12-24 stsp return NULL;
357 52a74475 2018-12-24 stsp static const struct got_error *
358 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
362 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
363 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
364 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
365 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
366 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
367 52a74475 2018-12-24 stsp return NULL;
370 52a74475 2018-12-24 stsp static const struct got_error *
371 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
375 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
376 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
377 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
378 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
379 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
380 52a74475 2018-12-24 stsp return NULL;
383 52a74475 2018-12-24 stsp static const struct got_error *
384 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
386 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
387 52a74475 2018-12-24 stsp uint8_t buf[8];
388 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
390 52a74475 2018-12-24 stsp *path = malloc(totlen);
391 52a74475 2018-12-24 stsp if (*path == NULL)
392 52a74475 2018-12-24 stsp return got_error_from_errno();
395 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
396 52a74475 2018-12-24 stsp if (n != sizeof(buf))
397 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
398 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
399 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
400 52a74475 2018-12-24 stsp if (p == NULL) {
401 52a74475 2018-12-24 stsp err = got_error_from_errno();
404 52a74475 2018-12-24 stsp totlen += sizeof(buf);
407 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
408 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
409 52a74475 2018-12-24 stsp len += sizeof(buf);
410 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
413 52a74475 2018-12-24 stsp free(*path);
414 52a74475 2018-12-24 stsp *path = NULL;
416 52a74475 2018-12-24 stsp return err;
419 52a74475 2018-12-24 stsp static const struct got_error *
420 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
421 52a74475 2018-12-24 stsp FILE *infile)
423 52a74475 2018-12-24 stsp const struct got_error *err;
424 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
427 52a74475 2018-12-24 stsp *entryp = NULL;
429 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
430 52a74475 2018-12-24 stsp if (entry == NULL)
431 52a74475 2018-12-24 stsp return got_error_from_errno();
433 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
436 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
439 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
442 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
446 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
449 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
452 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
456 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
460 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
461 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
462 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
465 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
467 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
468 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
469 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
472 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
474 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
478 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
481 52a74475 2018-12-24 stsp free(entry);
483 52a74475 2018-12-24 stsp *entryp = entry;
484 52a74475 2018-12-24 stsp return err;
487 52a74475 2018-12-24 stsp const struct got_error *
488 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
490 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
491 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
492 52a74475 2018-12-24 stsp SHA1_CTX ctx;
493 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
494 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
495 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
499 52a74475 2018-12-24 stsp SHA1Init(&ctx);
501 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
502 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
503 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
504 b6d05318 2019-01-13 stsp return NULL;
505 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
508 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
509 51514078 2018-12-25 stsp if (n == 0) /* EOF */
510 51514078 2018-12-25 stsp return NULL;
511 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
513 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
514 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
515 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
516 b6d05318 2019-01-13 stsp return NULL;
517 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
520 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
521 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
522 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
524 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
525 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
526 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
528 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
529 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
530 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
531 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
533 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
534 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
536 52a74475 2018-12-24 stsp return err;
537 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
539 52a74475 2018-12-24 stsp return err;
542 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
543 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
544 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
545 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
546 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
547 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
549 9d31a1d8 2018-03-11 stsp return NULL;
552 50952927 2019-01-12 stsp struct got_fileindex_entry *
553 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
555 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
557 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
559 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
560 a7f9d64d 2019-01-13 stsp while (next && (next->flags & GOT_FILEIDX_F_INTENT_TO_ADD))
561 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
563 50952927 2019-01-12 stsp return next;
566 8da9e5f4 2019-01-12 stsp static const struct got_error *
567 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
568 9d2a8e53 2019-01-28 stsp struct got_tree_object *, const char *, struct got_repository *,
569 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *, void *);
571 9d2a8e53 2019-01-28 stsp static const struct got_error *
572 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
573 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
574 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
575 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
577 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
579 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
580 8da9e5f4 2019-01-12 stsp char *subpath;
581 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
583 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
584 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
585 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
587 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
589 8da9e5f4 2019-01-12 stsp free(subpath);
590 8da9e5f4 2019-01-12 stsp return err;
593 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
594 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
595 8da9e5f4 2019-01-12 stsp free(subpath);
596 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
598 8da9e5f4 2019-01-12 stsp return err;
601 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
602 8da9e5f4 2019-01-12 stsp return NULL;
606 bad322f2 2019-01-28 stsp * Decide whether a fileindex entry path is equivalent to a tree entry path,
607 bad322f2 2019-01-28 stsp * and if it is not, then decide which of the two should be processed first.
610 bad322f2 2019-01-28 stsp cmp_entries(const char *ie_path, const char *parent_path,
611 bad322f2 2019-01-28 stsp size_t parent_len, const char *te_name)
613 bad322f2 2019-01-28 stsp int cmp = strncmp(ie_path, parent_path, parent_len);
614 62d20534 2019-01-28 stsp if (cmp == 0) {
615 bad322f2 2019-01-28 stsp const char *ie_name = ie_path + parent_len;
616 70e2377c 2019-01-28 stsp while (ie_name[0] == '/')
618 bad322f2 2019-01-28 stsp cmp = strcmp(ie_name, te_name);
620 70e2377c 2019-01-28 stsp return cmp;
624 8da9e5f4 2019-01-12 stsp static const struct got_error *
625 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
626 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
627 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
628 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
630 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
631 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
632 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
633 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
634 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
636 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
637 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
638 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
639 8da9e5f4 2019-01-12 stsp if (te && *ie) {
640 bad322f2 2019-01-28 stsp int cmp = cmp_entries((*ie)->path, path, path_len,
642 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
643 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
647 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
648 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
649 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
650 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
651 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
652 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
655 8da9e5f4 2019-01-12 stsp *ie = next;
657 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
660 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
661 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
665 8da9e5f4 2019-01-12 stsp } else if (*ie) {
666 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
667 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
670 8da9e5f4 2019-01-12 stsp *ie = next;
671 8da9e5f4 2019-01-12 stsp } else if (te) {
672 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
675 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
682 8da9e5f4 2019-01-12 stsp return err;
685 8da9e5f4 2019-01-12 stsp const struct got_error *
686 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
687 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
688 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
690 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
691 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
692 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
695 d1f6d47b 2019-02-04 stsp static const struct got_error *
696 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
697 d1f6d47b 2019-02-04 stsp const char *, struct got_repository *, struct got_fileindex_diff_dir_cb *,
700 500cd40f 2019-02-04 stsp struct dirlist_entry {
701 500cd40f 2019-02-04 stsp SIMPLEQ_ENTRY(dirlist_entry) entry;
702 500cd40f 2019-02-04 stsp struct dirent *de;
704 500cd40f 2019-02-04 stsp SIMPLEQ_HEAD(dirlist_head, dirlist_entry);
706 d1f6d47b 2019-02-04 stsp static const struct got_error *
707 500cd40f 2019-02-04 stsp walk_dir(struct dirlist_entry **next, struct got_fileindex *fileindex,
708 500cd40f 2019-02-04 stsp struct got_fileindex_entry **ie, struct dirlist_entry *dle,
709 500cd40f 2019-02-04 stsp const char *path, DIR *dir, struct got_repository *repo,
710 d1f6d47b 2019-02-04 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
712 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
714 500cd40f 2019-02-04 stsp if (dle->de->d_type == DT_DIR) {
715 d1f6d47b 2019-02-04 stsp char *subpath;
716 d1f6d47b 2019-02-04 stsp DIR *subdir;
718 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
719 500cd40f 2019-02-04 stsp path[0] == '\0' ? "" : "/", dle->de->d_name) == -1)
720 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
722 d1f6d47b 2019-02-04 stsp subdir = opendir(subpath);
723 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
724 d1f6d47b 2019-02-04 stsp free(subpath);
725 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
728 d1f6d47b 2019-02-04 stsp err = diff_fileindex_dir(fileindex, ie, subdir, subpath, repo,
729 d1f6d47b 2019-02-04 stsp cb, cb_arg);
730 d1f6d47b 2019-02-04 stsp free(subpath);
731 d1f6d47b 2019-02-04 stsp closedir(subdir);
733 d1f6d47b 2019-02-04 stsp return err;
736 500cd40f 2019-02-04 stsp *next = SIMPLEQ_NEXT(dle, entry);
737 500cd40f 2019-02-04 stsp return NULL;
740 500cd40f 2019-02-04 stsp static const struct got_error *
741 500cd40f 2019-02-04 stsp insert_dirent(struct dirlist_head *dirlist, struct dirent *de)
743 500cd40f 2019-02-04 stsp struct dirlist_entry *dle, *prev = NULL;
746 500cd40f 2019-02-04 stsp * Keep dirents sorted in same order as used by file index.
747 500cd40f 2019-02-04 stsp * Git orders tree object entries based on length and then memcmp().
749 500cd40f 2019-02-04 stsp SIMPLEQ_FOREACH(dle, dirlist, entry) {
752 500cd40f 2019-02-04 stsp if (dle->de->d_namlen > de->d_namlen) {
753 500cd40f 2019-02-04 stsp prev = dle;
757 500cd40f 2019-02-04 stsp cmp = strcmp(dle->de->d_name, de->d_name);
758 500cd40f 2019-02-04 stsp if (cmp == 0)
759 500cd40f 2019-02-04 stsp return NULL; /* duplicate, should not happen */
760 500cd40f 2019-02-04 stsp else if (cmp > 0)
763 500cd40f 2019-02-04 stsp prev = dle;
766 500cd40f 2019-02-04 stsp dle = malloc(sizeof(*dle));
767 500cd40f 2019-02-04 stsp if (dle == NULL)
768 500cd40f 2019-02-04 stsp return got_error_from_errno();
769 500cd40f 2019-02-04 stsp dle->de = de;
771 500cd40f 2019-02-04 stsp SIMPLEQ_INSERT_AFTER(dirlist, prev, dle, entry);
773 500cd40f 2019-02-04 stsp SIMPLEQ_INSERT_HEAD(dirlist, dle, entry);
775 d1f6d47b 2019-02-04 stsp return NULL;
778 d1f6d47b 2019-02-04 stsp static const struct got_error *
779 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
780 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry **ie, DIR *dir, const char *path,
781 d1f6d47b 2019-02-04 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
782 d1f6d47b 2019-02-04 stsp void *cb_arg)
784 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
785 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
786 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
787 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
788 500cd40f 2019-02-04 stsp struct dirlist_head dirlist;
789 500cd40f 2019-02-04 stsp struct dirlist_entry *dle;
791 500cd40f 2019-02-04 stsp SIMPLEQ_INIT(&dirlist);
793 500cd40f 2019-02-04 stsp while (1) {
794 500cd40f 2019-02-04 stsp de = readdir(dir);
795 500cd40f 2019-02-04 stsp if (de == NULL)
798 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
799 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
800 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
801 500cd40f 2019-02-04 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0))
804 500cd40f 2019-02-04 stsp insert_dirent(&dirlist, de);
807 500cd40f 2019-02-04 stsp dle = SIMPLEQ_FIRST(&dirlist);
808 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
809 500cd40f 2019-02-04 stsp if (dle && *ie) {
810 d1f6d47b 2019-02-04 stsp int cmp = cmp_entries((*ie)->path, path, path_len,
811 500cd40f 2019-02-04 stsp dle->de->d_name);
812 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
813 500cd40f 2019-02-04 stsp err = cb->diff_old_new(cb_arg, *ie, dle->de,
817 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
818 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
819 d1f6d47b 2019-02-04 stsp dir, repo, cb, cb_arg);
820 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
821 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
822 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
825 d1f6d47b 2019-02-04 stsp *ie = next;
827 500cd40f 2019-02-04 stsp err = cb->diff_new(cb_arg, dle->de, path);
830 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
831 d1f6d47b 2019-02-04 stsp dir, repo, cb, cb_arg);
838 500cd40f 2019-02-04 stsp while (!SIMPLEQ_EMPTY(&dirlist)) {
839 500cd40f 2019-02-04 stsp dle = SIMPLEQ_FIRST(&dirlist);
840 500cd40f 2019-02-04 stsp SIMPLEQ_REMOVE_HEAD(&dirlist, entry);
844 d1f6d47b 2019-02-04 stsp return err;
847 d1f6d47b 2019-02-04 stsp const struct got_error *
848 d1f6d47b 2019-02-04 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *dir,
849 d1f6d47b 2019-02-04 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
850 d1f6d47b 2019-02-04 stsp void *cb_arg)
852 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
853 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
854 d1f6d47b 2019-02-04 stsp return diff_fileindex_dir(fileindex, &min, dir, "", repo, cb, cb_arg);
857 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);