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 03df25b3 2019-05-11 stsp #include <errno.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 c48c4a9c 2018-03-11 stsp #include <stdio.h>
24 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
25 c48c4a9c 2018-03-11 stsp #include <string.h>
26 c48c4a9c 2018-03-11 stsp #include <sha1.h>
27 c34b20a2 2018-03-12 stsp #include <endian.h>
28 133d2798 2019-01-08 stsp #include <limits.h>
29 c442a90d 2019-03-10 stsp #include <uuid.h>
31 c48c4a9c 2018-03-11 stsp #include "got_error.h"
32 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
36 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
38 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_STAGE 0x00003000
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_EXTENDED 0x00004000
42 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
46 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 133d2798 2019-01-08 stsp struct got_fileindex {
49 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
50 133d2798 2019-01-08 stsp int nentries;
51 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 c48c4a9c 2018-03-11 stsp const struct got_error *
55 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
56 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
57 02c07007 2019-02-10 stsp int update_timestamps)
59 c48c4a9c 2018-03-11 stsp struct stat sb;
61 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
62 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
63 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
65 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
66 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
67 13d9040b 2019-03-27 stsp entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
71 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
72 13d9040b 2019-03-27 stsp if (update_timestamps) {
73 13d9040b 2019-03-27 stsp entry->ctime_sec = sb.st_ctime;
74 13d9040b 2019-03-27 stsp entry->ctime_nsec = sb.st_ctimensec;
75 13d9040b 2019-03-27 stsp entry->mtime_sec = sb.st_mtime;
76 13d9040b 2019-03-27 stsp entry->mtime_nsec = sb.st_mtimensec;
78 13d9040b 2019-03-27 stsp entry->uid = sb.st_uid;
79 13d9040b 2019-03-27 stsp entry->gid = sb.st_gid;
80 13d9040b 2019-03-27 stsp entry->size = (sb.st_size & 0xffffffff);
81 13d9040b 2019-03-27 stsp if (sb.st_mode & S_IFLNK)
82 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
84 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
85 13d9040b 2019-03-27 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
86 13d9040b 2019-03-27 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
89 ddce0520 2019-03-26 stsp if (blob_sha1) {
90 ddce0520 2019-03-26 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
91 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
93 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_BLOB;
95 ddce0520 2019-03-26 stsp if (commit_sha1) {
96 ddce0520 2019-03-26 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
97 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
99 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
101 51514078 2018-12-25 stsp return NULL;
105 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
107 2ec1f75b 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
110 51514078 2018-12-25 stsp const struct got_error *
111 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
112 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
113 51514078 2018-12-25 stsp uint8_t *commit_sha1)
115 51514078 2018-12-25 stsp size_t len;
117 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
118 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
119 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
121 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
122 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
123 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
124 c48c4a9c 2018-03-11 stsp free(*entry);
125 c48c4a9c 2018-03-11 stsp *entry = NULL;
126 0a585a0d 2018-03-17 stsp return err;
129 c34b20a2 2018-03-12 stsp len = strlen(relpath);
130 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
131 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
132 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
134 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
135 02c07007 2019-02-10 stsp commit_sha1, 1);
139 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
141 c48c4a9c 2018-03-11 stsp free(entry->path);
142 c48c4a9c 2018-03-11 stsp free(entry);
146 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
148 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
152 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
154 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
158 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
160 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
163 50952927 2019-01-12 stsp static const struct got_error *
164 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
166 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
167 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
169 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
170 133d2798 2019-01-08 stsp fileindex->nentries++;
171 133d2798 2019-01-08 stsp return NULL;
174 50952927 2019-01-12 stsp const struct got_error *
175 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
176 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
178 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
179 e288864f 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
181 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
185 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
186 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
188 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
189 133d2798 2019-01-08 stsp fileindex->nentries--;
192 51514078 2018-12-25 stsp struct got_fileindex_entry *
193 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
195 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
196 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
197 133d2798 2019-01-08 stsp key.path = (char *)path;
198 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
201 512f0d0e 2019-01-02 stsp const struct got_error *
202 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
203 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
205 133d2798 2019-01-08 stsp const struct got_error *err;
206 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
208 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
209 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
211 133d2798 2019-01-08 stsp return err;
213 b504a804 2019-01-08 stsp return NULL;
216 133d2798 2019-01-08 stsp struct got_fileindex *
217 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
219 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
221 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
222 133d2798 2019-01-08 stsp if (fileindex == NULL)
223 133d2798 2019-01-08 stsp return NULL;
225 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
226 133d2798 2019-01-08 stsp return fileindex;
230 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
232 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
234 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
235 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
236 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
238 9d31a1d8 2018-03-11 stsp free(fileindex);
241 c34b20a2 2018-03-12 stsp static const struct got_error *
242 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
246 c34b20a2 2018-03-12 stsp val = htobe64(val);
247 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
248 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
249 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
250 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
251 c34b20a2 2018-03-12 stsp return NULL;
254 c34b20a2 2018-03-12 stsp static const struct got_error *
255 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
259 c34b20a2 2018-03-12 stsp val = htobe32(val);
260 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
261 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
262 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
263 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
264 c34b20a2 2018-03-12 stsp return NULL;
267 c34b20a2 2018-03-12 stsp static const struct got_error *
268 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
272 c34b20a2 2018-03-12 stsp val = htobe16(val);
273 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
274 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
275 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
276 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp return NULL;
280 c34b20a2 2018-03-12 stsp static const struct got_error *
281 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
283 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
284 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
286 c34b20a2 2018-03-12 stsp len = strlen(path);
287 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
289 3c5b70f2 2018-12-27 stsp if (pad == 0)
290 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
292 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
293 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
294 c34b20a2 2018-03-12 stsp if (n != len)
295 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
296 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
297 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
298 c34b20a2 2018-03-12 stsp if (n != pad)
299 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
300 c34b20a2 2018-03-12 stsp return NULL;
303 c34b20a2 2018-03-12 stsp static const struct got_error *
304 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
305 c34b20a2 2018-03-12 stsp FILE *outfile)
307 c34b20a2 2018-03-12 stsp const struct got_error *err;
310 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
312 c34b20a2 2018-03-12 stsp return err;
313 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
315 c34b20a2 2018-03-12 stsp return err;
316 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
318 c34b20a2 2018-03-12 stsp return err;
319 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
321 c34b20a2 2018-03-12 stsp return err;
323 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
325 c34b20a2 2018-03-12 stsp return err;
326 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
328 c34b20a2 2018-03-12 stsp return err;
329 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
331 c34b20a2 2018-03-12 stsp return err;
333 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
335 c34b20a2 2018-03-12 stsp return err;
337 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
338 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
339 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
340 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
342 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
343 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
344 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
345 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
347 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
349 c34b20a2 2018-03-12 stsp return err;
351 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
352 c34b20a2 2018-03-12 stsp return err;
355 9d31a1d8 2018-03-11 stsp const struct got_error *
356 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
358 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
359 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
360 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
361 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
363 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
365 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
367 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
368 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
369 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
371 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
372 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
373 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
374 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
375 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
376 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
377 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
378 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
379 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
380 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
381 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
382 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
384 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
385 e288864f 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
386 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
388 133d2798 2019-01-08 stsp return err;
391 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
392 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
393 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
394 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
396 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
397 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
399 52a74475 2018-12-24 stsp return NULL;
402 52a74475 2018-12-24 stsp static const struct got_error *
403 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
407 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
408 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
409 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
410 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
411 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
412 52a74475 2018-12-24 stsp return NULL;
415 52a74475 2018-12-24 stsp static const struct got_error *
416 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
420 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
421 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
422 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
423 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
424 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
425 52a74475 2018-12-24 stsp return NULL;
428 52a74475 2018-12-24 stsp static const struct got_error *
429 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
433 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
434 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
435 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
436 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
437 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
438 52a74475 2018-12-24 stsp return NULL;
441 52a74475 2018-12-24 stsp static const struct got_error *
442 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
444 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
445 52a74475 2018-12-24 stsp uint8_t buf[8];
446 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
448 52a74475 2018-12-24 stsp *path = malloc(totlen);
449 52a74475 2018-12-24 stsp if (*path == NULL)
450 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
453 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
454 52a74475 2018-12-24 stsp if (n != sizeof(buf))
455 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
456 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
457 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
458 52a74475 2018-12-24 stsp if (p == NULL) {
459 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
462 52a74475 2018-12-24 stsp totlen += sizeof(buf);
465 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
466 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
467 52a74475 2018-12-24 stsp len += sizeof(buf);
468 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
471 52a74475 2018-12-24 stsp free(*path);
472 52a74475 2018-12-24 stsp *path = NULL;
474 52a74475 2018-12-24 stsp return err;
477 52a74475 2018-12-24 stsp static const struct got_error *
478 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
479 52a74475 2018-12-24 stsp FILE *infile)
481 52a74475 2018-12-24 stsp const struct got_error *err;
482 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
485 52a74475 2018-12-24 stsp *entryp = NULL;
487 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
488 52a74475 2018-12-24 stsp if (entry == NULL)
489 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
491 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
494 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
497 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
500 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
504 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
507 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
510 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
514 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
518 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
519 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
520 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
523 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
525 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
526 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
527 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
530 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
532 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
536 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
539 52a74475 2018-12-24 stsp free(entry);
541 52a74475 2018-12-24 stsp *entryp = entry;
542 52a74475 2018-12-24 stsp return err;
545 52a74475 2018-12-24 stsp const struct got_error *
546 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
548 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
549 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
550 52a74475 2018-12-24 stsp SHA1_CTX ctx;
551 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
552 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
553 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
557 52a74475 2018-12-24 stsp SHA1Init(&ctx);
559 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
560 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
561 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
562 b6d05318 2019-01-13 stsp return NULL;
563 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
565 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
566 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
567 51514078 2018-12-25 stsp if (n == 0) /* EOF */
568 51514078 2018-12-25 stsp return NULL;
569 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
571 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
572 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
573 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
574 b6d05318 2019-01-13 stsp return NULL;
575 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
578 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
579 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
580 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
582 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
583 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
584 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
586 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
587 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
588 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
589 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
591 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
592 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
594 52a74475 2018-12-24 stsp return err;
595 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
597 52a74475 2018-12-24 stsp return err;
600 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
601 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
602 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
603 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
604 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
605 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
607 9d31a1d8 2018-03-11 stsp return NULL;
610 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
611 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
613 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
615 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
617 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
618 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
619 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
621 50952927 2019-01-12 stsp return next;
624 8da9e5f4 2019-01-12 stsp static const struct got_error *
625 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
626 c4cdcb68 2019-04-03 stsp struct got_tree_object *, const char *, const char *,
627 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
629 9d2a8e53 2019-01-28 stsp static const struct got_error *
630 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
631 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
632 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
633 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
635 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
637 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
638 8da9e5f4 2019-01-12 stsp char *subpath;
639 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
641 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
642 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
643 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
645 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
647 8da9e5f4 2019-01-12 stsp free(subpath);
648 8da9e5f4 2019-01-12 stsp return err;
651 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
652 c4cdcb68 2019-04-03 stsp subpath, entry_name, repo, cb, cb_arg);
653 8da9e5f4 2019-01-12 stsp free(subpath);
654 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
656 8da9e5f4 2019-01-12 stsp return err;
659 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
660 8da9e5f4 2019-01-12 stsp return NULL;
663 8da9e5f4 2019-01-12 stsp static const struct got_error *
664 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
665 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
666 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
667 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
669 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
670 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
671 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
672 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
673 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
675 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
676 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
677 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
678 8da9e5f4 2019-01-12 stsp if (te && *ie) {
679 18831e78 2019-02-10 stsp char *te_path;
681 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
682 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
685 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
686 18831e78 2019-02-10 stsp free(te_path);
687 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
688 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
689 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
690 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
691 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
693 c4cdcb68 2019-04-03 stsp if (err || entry_name)
696 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
697 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
698 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
699 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
700 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
701 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
702 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
703 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
704 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
705 c4cdcb68 2019-04-03 stsp if (err || entry_name)
708 8da9e5f4 2019-01-12 stsp *ie = next;
710 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
711 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
712 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
713 c4cdcb68 2019-04-03 stsp if (err || entry_name)
716 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
717 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
721 8da9e5f4 2019-01-12 stsp } else if (*ie) {
722 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
723 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
724 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
725 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
726 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
727 c4cdcb68 2019-04-03 stsp if (err || entry_name)
730 8da9e5f4 2019-01-12 stsp *ie = next;
731 8da9e5f4 2019-01-12 stsp } else if (te) {
732 c4cdcb68 2019-04-03 stsp if (entry_name == NULL ||
733 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0) {
734 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
735 c4cdcb68 2019-04-03 stsp if (err || entry_name)
738 c4cdcb68 2019-04-03 stsp err = walk_tree(&te, fileindex, ie, te, path,
739 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
745 8da9e5f4 2019-01-12 stsp return err;
748 8da9e5f4 2019-01-12 stsp const struct got_error *
749 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
750 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
751 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
752 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
754 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
755 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
756 c4cdcb68 2019-04-03 stsp return diff_fileindex_tree(fileindex, &min, tree, path, entry_name,
757 c4cdcb68 2019-04-03 stsp repo, cb, cb_arg);
760 d1f6d47b 2019-02-04 stsp static const struct got_error *
761 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
762 c7f4312f 2019-02-05 stsp const char *, const char *, struct got_repository *,
763 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *, void *);
765 d1f6d47b 2019-02-04 stsp static const struct got_error *
766 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
767 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
768 c7f4312f 2019-02-05 stsp const char *path, DIR *dir, const char *rootpath,
769 c7f4312f 2019-02-05 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
770 c7f4312f 2019-02-05 stsp void *cb_arg)
772 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
773 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
775 573463cc 2019-04-06 stsp *next = NULL;
777 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
778 d1f6d47b 2019-02-04 stsp char *subpath;
779 c7f4312f 2019-02-05 stsp char *subdirpath;
780 d1f6d47b 2019-02-04 stsp DIR *subdir;
782 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
783 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
784 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
786 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
787 c7f4312f 2019-02-05 stsp free(subpath);
788 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
791 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
792 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
793 d1f6d47b 2019-02-04 stsp free(subpath);
794 c7f4312f 2019-02-05 stsp free(subdirpath);
795 638f9024 2019-05-13 stsp return got_error_from_errno2("opendir", subdirpath);
798 c7f4312f 2019-02-05 stsp err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
799 c7f4312f 2019-02-05 stsp subpath, repo, cb, cb_arg);
800 d1f6d47b 2019-02-04 stsp free(subpath);
801 c7f4312f 2019-02-05 stsp free(subdirpath);
802 d1f6d47b 2019-02-04 stsp closedir(subdir);
804 d1f6d47b 2019-02-04 stsp return err;
807 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
808 d1f6d47b 2019-02-04 stsp return NULL;
811 d1f6d47b 2019-02-04 stsp static const struct got_error *
812 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
813 c7f4312f 2019-02-05 stsp struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
814 c7f4312f 2019-02-05 stsp const char *path, struct got_repository *repo,
815 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
817 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
818 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
819 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
820 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
821 f5d3d7af 2019-02-05 stsp struct got_pathlist_head dirlist;
822 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
824 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&dirlist);
827 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
828 ed83bff7 2019-02-05 stsp struct dirent *dep = NULL;
830 ed83bff7 2019-02-05 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
831 ed83bff7 2019-02-05 stsp if (de == NULL) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
836 ed83bff7 2019-02-05 stsp if (readdir_r(dir, de, &dep) != 0) {
837 638f9024 2019-05-13 stsp err = got_error_from_errno("readdir_r");
841 ed83bff7 2019-02-05 stsp if (dep == NULL) {
846 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
847 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
848 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
849 ed83bff7 2019-02-05 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
854 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
859 f5d3d7af 2019-02-05 stsp if (new == NULL) {
860 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
866 f5d3d7af 2019-02-05 stsp dle = TAILQ_FIRST(&dirlist);
867 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
868 500cd40f 2019-02-04 stsp if (dle && *ie) {
869 18831e78 2019-02-10 stsp char *de_path;
871 f5d3d7af 2019-02-05 stsp de = dle->data;
872 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
873 18831e78 2019-02-10 stsp de->d_name) == -1) {
874 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
877 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
878 18831e78 2019-02-10 stsp free(de_path);
879 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
880 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
883 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
884 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
885 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
886 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
887 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
888 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
891 d1f6d47b 2019-02-04 stsp *ie = next;
893 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
896 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
897 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
901 554b91b1 2019-02-04 stsp } else if (*ie) {
902 554b91b1 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
903 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
906 554b91b1 2019-02-04 stsp *ie = next;
907 554b91b1 2019-02-04 stsp } else if (dle) {
908 763e1377 2019-02-05 stsp de = dle->data;
909 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
912 554b91b1 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path, dir,
913 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
919 ed83bff7 2019-02-05 stsp TAILQ_FOREACH(dle, &dirlist, entry)
920 ed83bff7 2019-02-05 stsp free(dle->data);
921 f5d3d7af 2019-02-05 stsp got_pathlist_free(&dirlist);
922 d1f6d47b 2019-02-04 stsp return err;
925 d1f6d47b 2019-02-04 stsp const struct got_error *
926 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
927 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
928 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
930 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
931 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
932 927df6b7 2019-02-10 stsp return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
933 c7f4312f 2019-02-05 stsp repo, cb, cb_arg);
936 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);