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 6fc93f37 2019-12-13 stsp #include <fcntl.h>
24 c48c4a9c 2018-03-11 stsp #include <stdio.h>
25 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
26 c48c4a9c 2018-03-11 stsp #include <string.h>
27 c48c4a9c 2018-03-11 stsp #include <sha1.h>
28 c34b20a2 2018-03-12 stsp #include <endian.h>
29 133d2798 2019-01-08 stsp #include <limits.h>
30 6fc93f37 2019-12-13 stsp #include <unistd.h>
31 c442a90d 2019-03-10 stsp #include <uuid.h>
33 c48c4a9c 2018-03-11 stsp #include "got_error.h"
34 8da9e5f4 2019-01-12 stsp #include "got_object.h"
35 324d37e7 2019-05-11 stsp #include "got_path.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
38 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
40 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
43 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 a46b9f33 2020-01-28 stsp #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
50 133d2798 2019-01-08 stsp struct got_fileindex {
51 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
52 a46b9f33 2020-01-28 stsp int nentries; /* Does not include entries marked for removal. */
53 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
57 26a7fe28 2019-07-27 stsp got_fileindex_perms_from_st(struct stat *sb)
59 26a7fe28 2019-07-27 stsp uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
60 26a7fe28 2019-07-27 stsp return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
64 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
66 26a7fe28 2019-07-27 stsp mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
67 8957ae76 2019-08-08 stsp return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
70 c48c4a9c 2018-03-11 stsp const struct got_error *
71 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
72 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
73 02c07007 2019-02-10 stsp int update_timestamps)
75 c48c4a9c 2018-03-11 stsp struct stat sb;
77 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
78 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
79 b15816dd 2019-08-11 stsp errno == ENOENT))
80 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
82 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
83 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
84 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
88 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
89 13d9040b 2019-03-27 stsp if (update_timestamps) {
90 3f762da0 2019-08-03 stsp ie->ctime_sec = sb.st_ctime;
91 3f762da0 2019-08-03 stsp ie->ctime_nsec = sb.st_ctimensec;
92 3f762da0 2019-08-03 stsp ie->mtime_sec = sb.st_mtime;
93 3f762da0 2019-08-03 stsp ie->mtime_nsec = sb.st_mtimensec;
95 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
96 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
97 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
98 0553429d 2020-06-07 stsp if (S_ISLNK(sb.st_mode))
99 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_SYMLINK;
101 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
102 ef8d6031 2020-07-23 stsp ie->mode |= got_fileindex_perms_from_st(&sb);
106 ddce0520 2019-03-26 stsp if (blob_sha1) {
107 3f762da0 2019-08-03 stsp memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
108 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
110 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
112 ddce0520 2019-03-26 stsp if (commit_sha1) {
113 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
114 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
116 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
118 51514078 2018-12-25 stsp return NULL;
122 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
124 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
127 51514078 2018-12-25 stsp const struct got_error *
128 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
129 3969253a 2020-03-07 stsp const char *relpath)
131 51514078 2018-12-25 stsp size_t len;
133 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
134 3f762da0 2019-08-03 stsp if (*ie == NULL)
135 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
137 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
138 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
139 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
141 3f762da0 2019-08-03 stsp *ie = NULL;
142 0a585a0d 2018-03-17 stsp return err;
145 4d555405 2019-08-03 stsp len = strlen(relpath);
146 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
147 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
148 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
150 3969253a 2020-03-07 stsp return NULL;
154 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
156 3f762da0 2019-08-03 stsp free(ie->path);
161 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
163 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
167 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
169 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
173 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
175 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
176 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
177 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
181 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
183 2e1fa222 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE);
186 2e1fa222 2020-07-23 stsp const struct got_error *
187 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
189 2e1fa222 2020-07-23 stsp switch (type) {
190 2e1fa222 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
191 2e1fa222 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
192 2e1fa222 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
195 2e1fa222 2020-07-23 stsp return got_error(GOT_ERR_BAD_FILETYPE);
198 2e1fa222 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE;
199 2e1fa222 2020-07-23 stsp ie->mode |= type;
200 2e1fa222 2020-07-23 stsp return NULL;
204 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
206 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
210 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
212 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
216 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
218 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
221 50952927 2019-01-12 stsp static const struct got_error *
222 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
224 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
225 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
227 3f762da0 2019-08-03 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
228 133d2798 2019-01-08 stsp fileindex->nentries++;
229 133d2798 2019-01-08 stsp return NULL;
232 50952927 2019-01-12 stsp const struct got_error *
233 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
234 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
236 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
237 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
239 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
243 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
244 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
247 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
248 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
249 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
250 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
251 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
253 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
254 133d2798 2019-01-08 stsp fileindex->nentries--;
257 51514078 2018-12-25 stsp struct got_fileindex_entry *
258 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
259 d6c87207 2019-08-02 stsp size_t path_len)
261 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
262 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
263 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
264 133d2798 2019-01-08 stsp key.path = (char *)path;
265 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
266 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
267 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
268 a46b9f33 2020-01-28 stsp return NULL;
272 512f0d0e 2019-01-02 stsp const struct got_error *
273 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
274 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
276 133d2798 2019-01-08 stsp const struct got_error *err;
277 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
279 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
280 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
282 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
284 133d2798 2019-01-08 stsp return err;
286 b504a804 2019-01-08 stsp return NULL;
289 133d2798 2019-01-08 stsp struct got_fileindex *
290 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
292 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
294 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
295 133d2798 2019-01-08 stsp if (fileindex == NULL)
296 133d2798 2019-01-08 stsp return NULL;
298 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
299 133d2798 2019-01-08 stsp return fileindex;
303 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
305 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
307 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
308 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
309 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
311 9d31a1d8 2018-03-11 stsp free(fileindex);
314 c34b20a2 2018-03-12 stsp static const struct got_error *
315 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
319 c34b20a2 2018-03-12 stsp val = htobe64(val);
320 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
321 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
322 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
323 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
324 c34b20a2 2018-03-12 stsp return NULL;
327 c34b20a2 2018-03-12 stsp static const struct got_error *
328 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
332 c34b20a2 2018-03-12 stsp val = htobe32(val);
333 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
334 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
335 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
336 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
337 c34b20a2 2018-03-12 stsp return NULL;
340 c34b20a2 2018-03-12 stsp static const struct got_error *
341 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
345 c34b20a2 2018-03-12 stsp val = htobe16(val);
346 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
347 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
348 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
349 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
350 c34b20a2 2018-03-12 stsp return NULL;
353 c34b20a2 2018-03-12 stsp static const struct got_error *
354 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
356 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
357 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
359 c34b20a2 2018-03-12 stsp len = strlen(path);
360 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
362 3c5b70f2 2018-12-27 stsp if (pad == 0)
363 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
365 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
366 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
367 c34b20a2 2018-03-12 stsp if (n != len)
368 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
369 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
370 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
371 c34b20a2 2018-03-12 stsp if (n != pad)
372 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
373 c34b20a2 2018-03-12 stsp return NULL;
376 c34b20a2 2018-03-12 stsp static const struct got_error *
377 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
378 c34b20a2 2018-03-12 stsp FILE *outfile)
380 c34b20a2 2018-03-12 stsp const struct got_error *err;
382 df335242 2019-08-03 stsp uint32_t stage;
384 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
386 c34b20a2 2018-03-12 stsp return err;
387 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
389 c34b20a2 2018-03-12 stsp return err;
390 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
392 c34b20a2 2018-03-12 stsp return err;
393 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
395 c34b20a2 2018-03-12 stsp return err;
397 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
399 c34b20a2 2018-03-12 stsp return err;
400 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
402 c34b20a2 2018-03-12 stsp return err;
403 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
405 c34b20a2 2018-03-12 stsp return err;
407 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
409 c34b20a2 2018-03-12 stsp return err;
411 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
412 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
413 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
414 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
416 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
417 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
418 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
419 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
421 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
423 c34b20a2 2018-03-12 stsp return err;
425 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
427 df335242 2019-08-03 stsp return err;
429 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
430 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
431 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
432 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
433 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
435 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
436 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
439 df335242 2019-08-03 stsp return NULL;
442 9d31a1d8 2018-03-11 stsp const struct got_error *
443 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
445 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
446 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
447 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
448 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
450 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
452 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
454 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
455 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
456 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
458 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
459 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
460 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
461 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
462 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
463 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
464 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
465 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
466 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
467 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
468 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
469 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
471 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
472 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
473 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
474 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
475 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
478 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
480 133d2798 2019-01-08 stsp return err;
483 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
484 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
485 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
486 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
488 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
489 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
491 52a74475 2018-12-24 stsp return NULL;
494 52a74475 2018-12-24 stsp static const struct got_error *
495 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
499 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
500 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
501 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
502 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
503 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
504 52a74475 2018-12-24 stsp return NULL;
507 52a74475 2018-12-24 stsp static const struct got_error *
508 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
512 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
513 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
514 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
515 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
516 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
517 52a74475 2018-12-24 stsp return NULL;
520 52a74475 2018-12-24 stsp static const struct got_error *
521 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
525 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
526 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
527 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
528 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
529 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
530 52a74475 2018-12-24 stsp return NULL;
533 52a74475 2018-12-24 stsp static const struct got_error *
534 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
536 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
537 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
538 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
540 52a74475 2018-12-24 stsp *path = malloc(totlen);
541 52a74475 2018-12-24 stsp if (*path == NULL)
542 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
545 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
546 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
547 52a74475 2018-12-24 stsp if (p == NULL) {
548 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
551 6bf2c316 2019-08-02 stsp totlen += chunk_size;
554 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
555 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
556 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
559 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
560 6bf2c316 2019-08-02 stsp len += chunk_size;
561 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
564 52a74475 2018-12-24 stsp free(*path);
565 52a74475 2018-12-24 stsp *path = NULL;
567 52a74475 2018-12-24 stsp return err;
570 52a74475 2018-12-24 stsp static const struct got_error *
571 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
572 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
574 52a74475 2018-12-24 stsp const struct got_error *err;
575 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
578 3f762da0 2019-08-03 stsp *iep = NULL;
580 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
581 3f762da0 2019-08-03 stsp if (ie == NULL)
582 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
584 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
587 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
590 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
593 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
597 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
600 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
603 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
607 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
611 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
612 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
613 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
616 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
618 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
619 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
620 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
623 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
625 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
629 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
633 df335242 2019-08-03 stsp if (version >= 2) {
634 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
635 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
636 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
637 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
639 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
640 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
643 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
646 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
647 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
652 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
655 52a74475 2018-12-24 stsp return err;
658 52a74475 2018-12-24 stsp const struct got_error *
659 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
661 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
662 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
663 52a74475 2018-12-24 stsp SHA1_CTX ctx;
664 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
665 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
666 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
670 52a74475 2018-12-24 stsp SHA1Init(&ctx);
672 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
673 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
674 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
675 b6d05318 2019-01-13 stsp return NULL;
676 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
678 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
679 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
680 51514078 2018-12-25 stsp if (n == 0) /* EOF */
681 51514078 2018-12-25 stsp return NULL;
682 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
684 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
685 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
686 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
687 b6d05318 2019-01-13 stsp return NULL;
688 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
691 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
692 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
693 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
695 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
696 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
697 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
699 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
700 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
701 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
702 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
704 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
705 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
707 52a74475 2018-12-24 stsp return err;
708 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
710 52a74475 2018-12-24 stsp return err;
713 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
714 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
715 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
716 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
717 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
718 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
720 9d31a1d8 2018-03-11 stsp return NULL;
723 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
724 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
726 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
728 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
730 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
731 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
732 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
733 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
735 50952927 2019-01-12 stsp return next;
738 8da9e5f4 2019-01-12 stsp static const struct got_error *
739 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
740 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
741 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
743 9d2a8e53 2019-01-28 stsp static const struct got_error *
744 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
745 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
746 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
747 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
749 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
750 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
752 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
753 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
754 8da9e5f4 2019-01-12 stsp char *subpath;
755 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
757 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
758 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
759 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
760 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
762 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
763 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
765 8da9e5f4 2019-01-12 stsp free(subpath);
766 8da9e5f4 2019-01-12 stsp return err;
769 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
770 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
771 8da9e5f4 2019-01-12 stsp free(subpath);
772 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
774 8da9e5f4 2019-01-12 stsp return err;
778 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
779 8da9e5f4 2019-01-12 stsp return NULL;
782 8da9e5f4 2019-01-12 stsp static const struct got_error *
783 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
784 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
785 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
786 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
788 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
789 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
790 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
791 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
792 56e0773d 2019-11-28 stsp int tidx = 0;
794 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
795 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
796 8da9e5f4 2019-01-12 stsp if (te && *ie) {
797 18831e78 2019-02-10 stsp char *te_path;
798 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
800 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
801 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
804 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
805 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
806 18831e78 2019-02-10 stsp free(te_path);
807 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
808 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
809 63c5ca5d 2019-08-24 stsp path_len) &&
810 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
811 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
812 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
813 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
815 c4cdcb68 2019-04-03 stsp if (err || entry_name)
818 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
819 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
820 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
821 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
822 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
823 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
824 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
825 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
826 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
827 c4cdcb68 2019-04-03 stsp if (err || entry_name)
830 8da9e5f4 2019-01-12 stsp *ie = next;
832 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
833 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
834 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
835 c4cdcb68 2019-04-03 stsp if (err || entry_name)
838 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
839 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
843 8da9e5f4 2019-01-12 stsp } else if (*ie) {
844 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
845 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
846 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
847 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
848 56e0773d 2019-11-28 stsp entry_name) == 0))) {
849 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
850 c4cdcb68 2019-04-03 stsp if (err || entry_name)
853 8da9e5f4 2019-01-12 stsp *ie = next;
854 8da9e5f4 2019-01-12 stsp } else if (te) {
855 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
856 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
857 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
859 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
860 c4cdcb68 2019-04-03 stsp if (err || entry_name)
863 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
864 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
870 8da9e5f4 2019-01-12 stsp return err;
873 8da9e5f4 2019-01-12 stsp const struct got_error *
874 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
875 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
876 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
877 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
879 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
880 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
881 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
882 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
883 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
884 30a076bc 2019-07-27 stsp cb, cb_arg);
887 d1f6d47b 2019-02-04 stsp static const struct got_error *
888 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
889 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
890 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
892 c577a9ce 2019-07-27 stsp static const struct got_error *
893 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
895 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
896 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
897 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
898 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
901 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
902 c577a9ce 2019-07-27 stsp if (de == NULL) {
903 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
907 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
908 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
912 c577a9ce 2019-07-27 stsp if (dep == NULL) {
917 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
918 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
919 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
920 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
925 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
930 c577a9ce 2019-07-27 stsp if (new == NULL) {
931 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
937 c577a9ce 2019-07-27 stsp return err;
941 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
943 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
945 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
946 c577a9ce 2019-07-27 stsp free(dle->data);
947 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
950 d1f6d47b 2019-02-04 stsp static const struct got_error *
951 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
952 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
953 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
954 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
956 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
957 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
958 965988c5 2019-12-16 stsp DIR *subdir = NULL;
959 6fc93f37 2019-12-13 stsp int subdirfd = -1;
962 573463cc 2019-04-06 stsp *next = NULL;
964 20ccae39 2020-07-21 stsp if (de->d_type == DT_UNKNOWN) {
965 20ccae39 2020-07-21 stsp /* Occurs on NFS mounts without "readdir plus" RPC. */
966 20ccae39 2020-07-21 stsp char *dir_path;
967 20ccae39 2020-07-21 stsp if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
968 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
969 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, dir_path, de);
970 20ccae39 2020-07-21 stsp free(dir_path);
972 20ccae39 2020-07-21 stsp return err;
974 20ccae39 2020-07-21 stsp type = de->d_type;
976 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
977 d1f6d47b 2019-02-04 stsp char *subpath;
978 c7f4312f 2019-02-05 stsp char *subdirpath;
979 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
981 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
983 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
984 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
985 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
987 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
988 c7f4312f 2019-02-05 stsp free(subpath);
989 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
992 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
993 6fc93f37 2019-12-13 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
994 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
995 40b289d7 2019-09-07 stsp if (errno == EACCES) {
996 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
997 40b289d7 2019-09-07 stsp return NULL;
999 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1000 d1f6d47b 2019-02-04 stsp free(subpath);
1001 c7f4312f 2019-02-05 stsp free(subdirpath);
1002 ef5e02fd 2019-08-11 stsp return err;
1005 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1006 965988c5 2019-12-16 stsp if (subdir == NULL)
1007 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
1008 965988c5 2019-12-16 stsp subdirfd = -1;
1009 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1010 c577a9ce 2019-07-27 stsp if (err) {
1011 c577a9ce 2019-07-27 stsp free(subpath);
1012 c577a9ce 2019-07-27 stsp free(subdirpath);
1013 965988c5 2019-12-16 stsp closedir(subdir);
1014 c577a9ce 2019-07-27 stsp return err;
1016 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1017 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1018 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1019 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1020 d1f6d47b 2019-02-04 stsp free(subpath);
1021 c7f4312f 2019-02-05 stsp free(subdirpath);
1022 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
1024 d1f6d47b 2019-02-04 stsp return err;
1027 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1028 d1f6d47b 2019-02-04 stsp return NULL;
1031 d1f6d47b 2019-02-04 stsp static const struct got_error *
1032 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1033 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1034 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1035 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1036 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1038 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1039 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1040 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1041 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1043 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1044 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1046 3143d852 2020-06-25 stsp return err;
1049 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1050 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1051 500cd40f 2019-02-04 stsp if (dle && *ie) {
1052 18831e78 2019-02-10 stsp char *de_path;
1054 f5d3d7af 2019-02-05 stsp de = dle->data;
1055 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1056 18831e78 2019-02-10 stsp de->d_name) == -1) {
1057 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1060 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1061 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1062 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
1063 18831e78 2019-02-10 stsp free(de_path);
1064 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1065 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1069 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1070 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1071 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1072 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1073 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1076 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1078 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1081 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1082 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1086 554b91b1 2019-02-04 stsp } else if (*ie) {
1087 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1090 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1091 554b91b1 2019-02-04 stsp } else if (dle) {
1092 763e1377 2019-02-05 stsp de = dle->data;
1093 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1096 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1097 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
1103 d1f6d47b 2019-02-04 stsp return err;
1106 d1f6d47b 2019-02-04 stsp const struct got_error *
1107 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1108 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1109 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1111 c577a9ce 2019-07-27 stsp const struct got_error *err;
1112 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1113 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1117 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1120 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1121 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1123 965988c5 2019-12-16 stsp fd2 = dup(fd);
1124 965988c5 2019-12-16 stsp if (fd2 == -1)
1125 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1126 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1127 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1128 3dcf3e74 2020-01-28 stsp close(fd2);
1129 3dcf3e74 2020-01-28 stsp return err;
1131 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1132 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1133 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1134 3dcf3e74 2020-01-28 stsp close(fd2);
1135 3dcf3e74 2020-01-28 stsp return err;
1137 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1138 965988c5 2019-12-16 stsp if (err) {
1139 965988c5 2019-12-16 stsp closedir(dir);
1140 c577a9ce 2019-07-27 stsp return err;
1143 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1144 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1145 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1146 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1147 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1149 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1150 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1151 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1152 c577a9ce 2019-07-27 stsp return err;
1155 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);