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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
20 03df25b3 2019-05-11 stsp #include <errno.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 6fc93f37 2019-12-13 stsp #include <fcntl.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 133d2798 2019-01-08 stsp #include <limits.h>
27 6fc93f37 2019-12-13 stsp #include <unistd.h>
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.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 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
41 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
42 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
46 a46b9f33 2020-01-28 stsp #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
47 a769b60b 2021-06-27 stsp #define GOT_FILEIDX_F_SKIPPED 0x00200000
49 133d2798 2019-01-08 stsp struct got_fileindex {
50 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
51 a46b9f33 2020-01-28 stsp int nentries; /* Does not include entries marked for removal. */
52 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
56 0aeb8099 2020-07-23 stsp got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
58 4723f050 2020-07-23 stsp return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
59 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
63 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
65 4723f050 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
66 4723f050 2020-07-23 stsp ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
67 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS);
71 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
73 0aeb8099 2020-07-23 stsp mode_t perms = got_fileindex_entry_perms_get(ie);
74 4723f050 2020-07-23 stsp int type = got_fileindex_entry_filetype_get(ie);
75 4723f050 2020-07-23 stsp uint32_t ftype;
77 4723f050 2020-07-23 stsp if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
78 4723f050 2020-07-23 stsp type == GOT_FILEIDX_MODE_BAD_SYMLINK)
79 4723f050 2020-07-23 stsp ftype = S_IFREG;
81 4723f050 2020-07-23 stsp ftype = S_IFLNK;
83 4723f050 2020-07-23 stsp return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
86 c48c4a9c 2018-03-11 stsp const struct got_error *
87 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
88 437adc9d 2020-12-10 yzhong int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
89 437adc9d 2020-12-10 yzhong uint8_t *commit_sha1, int update_timestamps)
91 c48c4a9c 2018-03-11 stsp struct stat sb;
93 437adc9d 2020-12-10 yzhong if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
94 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
95 b15816dd 2019-08-11 stsp errno == ENOENT))
96 437adc9d 2020-12-10 yzhong return got_error_from_errno2("fstatat", ondisk_path);
97 aa9ad276 2020-07-25 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
99 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
100 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
101 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 13d9040b 2019-03-27 stsp if (update_timestamps) {
107 0823ffc2 2020-09-10 naddy ie->ctime_sec = sb.st_ctim.tv_sec;
108 0823ffc2 2020-09-10 naddy ie->ctime_nsec = sb.st_ctim.tv_nsec;
109 0823ffc2 2020-09-10 naddy ie->mtime_sec = sb.st_mtim.tv_sec;
110 0823ffc2 2020-09-10 naddy ie->mtime_nsec = sb.st_mtim.tv_nsec;
112 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
113 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
114 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
115 4723f050 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
116 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
117 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
118 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie, 0);
120 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
121 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
122 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie,
123 4723f050 2020-07-23 stsp sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
127 ddce0520 2019-03-26 stsp if (blob_sha1) {
128 3f762da0 2019-08-03 stsp memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
133 ddce0520 2019-03-26 stsp if (commit_sha1) {
134 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
139 51514078 2018-12-25 stsp return NULL;
143 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
149 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
151 a769b60b 2021-06-27 stsp ie->flags |= GOT_FILEIDX_F_SKIPPED;
154 51514078 2018-12-25 stsp const struct got_error *
155 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
156 3969253a 2020-03-07 stsp const char *relpath)
158 51514078 2018-12-25 stsp size_t len;
160 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
161 3f762da0 2019-08-03 stsp if (*ie == NULL)
162 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
164 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
165 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
166 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
168 3f762da0 2019-08-03 stsp *ie = NULL;
169 0a585a0d 2018-03-17 stsp return err;
172 4d555405 2019-08-03 stsp len = strlen(relpath);
173 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
174 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
175 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
177 3969253a 2020-03-07 stsp return NULL;
181 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
183 3f762da0 2019-08-03 stsp free(ie->path);
188 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
190 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
194 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
196 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
200 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
202 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
203 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
204 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
208 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
210 f5f1f9c2 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
214 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
216 6131ab45 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
217 6131ab45 2020-07-23 stsp ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
221 48b4f239 2021-12-31 thomas got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
222 48b4f239 2021-12-31 thomas int type)
224 984c073d 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
225 984c073d 2020-07-23 stsp ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
226 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
230 984c073d 2020-07-23 stsp got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
232 984c073d 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
233 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
237 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
239 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
243 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
245 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
249 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
251 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
255 a769b60b 2021-06-27 stsp got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
257 a769b60b 2021-06-27 stsp return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
260 50952927 2019-01-12 stsp static const struct got_error *
261 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
263 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
264 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
266 9fad5d8c 2022-04-16 thomas if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
267 9fad5d8c 2022-04-16 thomas return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
269 133d2798 2019-01-08 stsp fileindex->nentries++;
270 133d2798 2019-01-08 stsp return NULL;
273 50952927 2019-01-12 stsp const struct got_error *
274 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
275 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
277 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
278 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
280 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
284 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
285 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
288 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
289 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
290 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
291 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
292 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
294 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
295 133d2798 2019-01-08 stsp fileindex->nentries--;
298 51514078 2018-12-25 stsp struct got_fileindex_entry *
299 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
300 d6c87207 2019-08-02 stsp size_t path_len)
302 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
303 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
304 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
305 133d2798 2019-01-08 stsp key.path = (char *)path;
306 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
307 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
308 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
309 a46b9f33 2020-01-28 stsp return NULL;
313 512f0d0e 2019-01-02 stsp const struct got_error *
314 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
315 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
317 133d2798 2019-01-08 stsp const struct got_error *err;
318 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
320 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
321 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
323 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
325 133d2798 2019-01-08 stsp return err;
327 b504a804 2019-01-08 stsp return NULL;
330 133d2798 2019-01-08 stsp struct got_fileindex *
331 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
333 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
335 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
336 133d2798 2019-01-08 stsp if (fileindex == NULL)
337 133d2798 2019-01-08 stsp return NULL;
339 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
340 133d2798 2019-01-08 stsp return fileindex;
344 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
346 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
348 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
349 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
350 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
352 9d31a1d8 2018-03-11 stsp free(fileindex);
355 c34b20a2 2018-03-12 stsp static const struct got_error *
356 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
360 c34b20a2 2018-03-12 stsp val = htobe64(val);
361 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
362 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
363 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
364 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
365 c34b20a2 2018-03-12 stsp return NULL;
368 c34b20a2 2018-03-12 stsp static const struct got_error *
369 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
373 c34b20a2 2018-03-12 stsp val = htobe32(val);
374 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
375 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
376 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
377 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
378 c34b20a2 2018-03-12 stsp return NULL;
381 c34b20a2 2018-03-12 stsp static const struct got_error *
382 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
386 c34b20a2 2018-03-12 stsp val = htobe16(val);
387 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
388 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
389 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
390 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
391 c34b20a2 2018-03-12 stsp return NULL;
394 c34b20a2 2018-03-12 stsp static const struct got_error *
395 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
397 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
398 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
400 c34b20a2 2018-03-12 stsp len = strlen(path);
401 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
403 3c5b70f2 2018-12-27 stsp if (pad == 0)
404 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
406 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
407 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
408 c34b20a2 2018-03-12 stsp if (n != len)
409 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
410 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
411 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
412 c34b20a2 2018-03-12 stsp if (n != pad)
413 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
414 c34b20a2 2018-03-12 stsp return NULL;
417 c34b20a2 2018-03-12 stsp static const struct got_error *
418 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
419 c34b20a2 2018-03-12 stsp FILE *outfile)
421 c34b20a2 2018-03-12 stsp const struct got_error *err;
423 df335242 2019-08-03 stsp uint32_t stage;
425 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
427 c34b20a2 2018-03-12 stsp return err;
428 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
430 c34b20a2 2018-03-12 stsp return err;
431 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
433 c34b20a2 2018-03-12 stsp return err;
434 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
436 c34b20a2 2018-03-12 stsp return err;
438 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
440 c34b20a2 2018-03-12 stsp return err;
441 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
443 c34b20a2 2018-03-12 stsp return err;
444 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
446 c34b20a2 2018-03-12 stsp return err;
448 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
450 c34b20a2 2018-03-12 stsp return err;
452 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
453 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
454 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
455 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
457 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
458 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
459 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
460 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
462 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
464 c34b20a2 2018-03-12 stsp return err;
466 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
468 df335242 2019-08-03 stsp return err;
470 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
471 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
472 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
473 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
474 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
476 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
477 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
480 df335242 2019-08-03 stsp return NULL;
483 9d31a1d8 2018-03-11 stsp const struct got_error *
484 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
486 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
487 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
488 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
489 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
491 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
493 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
495 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
496 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
497 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
499 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
500 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
501 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
502 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
503 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
504 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
505 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
506 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
507 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
508 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
509 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
510 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
512 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
513 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
514 a769b60b 2021-06-27 stsp ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
515 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
516 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
517 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
520 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
522 133d2798 2019-01-08 stsp return err;
525 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
526 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
527 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
528 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
530 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
531 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
533 52a74475 2018-12-24 stsp return NULL;
536 52a74475 2018-12-24 stsp static const struct got_error *
537 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
541 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
542 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
543 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
544 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
545 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
546 52a74475 2018-12-24 stsp return NULL;
549 52a74475 2018-12-24 stsp static const struct got_error *
550 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
554 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
555 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
556 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
558 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
559 52a74475 2018-12-24 stsp return NULL;
562 52a74475 2018-12-24 stsp static const struct got_error *
563 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
567 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
568 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
569 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
570 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
571 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
572 52a74475 2018-12-24 stsp return NULL;
575 52a74475 2018-12-24 stsp static const struct got_error *
576 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
578 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
579 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
580 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
582 52a74475 2018-12-24 stsp *path = malloc(totlen);
583 52a74475 2018-12-24 stsp if (*path == NULL)
584 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
587 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
588 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
589 52a74475 2018-12-24 stsp if (p == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
593 6bf2c316 2019-08-02 stsp totlen += chunk_size;
596 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
597 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
598 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
601 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
602 6bf2c316 2019-08-02 stsp len += chunk_size;
603 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
606 52a74475 2018-12-24 stsp free(*path);
607 52a74475 2018-12-24 stsp *path = NULL;
609 52a74475 2018-12-24 stsp return err;
612 52a74475 2018-12-24 stsp static const struct got_error *
613 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
614 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
616 52a74475 2018-12-24 stsp const struct got_error *err;
617 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
620 3f762da0 2019-08-03 stsp *iep = NULL;
622 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
623 3f762da0 2019-08-03 stsp if (ie == NULL)
624 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
626 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
629 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
632 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
635 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
639 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
642 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
645 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
649 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
653 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
654 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
655 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
658 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
660 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
661 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
662 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
665 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
667 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
671 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
675 df335242 2019-08-03 stsp if (version >= 2) {
676 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
677 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
678 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
679 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
681 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
682 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
685 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
688 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
689 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
694 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
697 52a74475 2018-12-24 stsp return err;
700 52a74475 2018-12-24 stsp const struct got_error *
701 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
703 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
704 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
705 52a74475 2018-12-24 stsp SHA1_CTX ctx;
706 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
707 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
708 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
712 52a74475 2018-12-24 stsp SHA1Init(&ctx);
714 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
715 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
716 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
717 b6d05318 2019-01-13 stsp return NULL;
718 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
720 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
721 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
722 51514078 2018-12-25 stsp if (n == 0) /* EOF */
723 51514078 2018-12-25 stsp return NULL;
724 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
726 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
727 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
728 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
729 b6d05318 2019-01-13 stsp return NULL;
730 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
733 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
734 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
735 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
737 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
738 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
739 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
741 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
742 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
743 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
744 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
746 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
747 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
749 52a74475 2018-12-24 stsp return err;
750 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
752 52a74475 2018-12-24 stsp return err;
755 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
756 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
757 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
758 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
759 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
760 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
762 9d31a1d8 2018-03-11 stsp return NULL;
765 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
766 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
768 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
770 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
772 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
773 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
774 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
775 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
777 50952927 2019-01-12 stsp return next;
780 8da9e5f4 2019-01-12 stsp static const struct got_error *
781 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
782 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
783 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
785 9d2a8e53 2019-01-28 stsp static const struct got_error *
786 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
787 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
788 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
789 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
791 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
792 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
794 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
795 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
796 8da9e5f4 2019-01-12 stsp char *subpath;
797 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
799 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
800 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
801 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
802 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
804 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
805 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
807 8da9e5f4 2019-01-12 stsp free(subpath);
808 8da9e5f4 2019-01-12 stsp return err;
811 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
812 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
813 8da9e5f4 2019-01-12 stsp free(subpath);
814 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
816 8da9e5f4 2019-01-12 stsp return err;
820 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
821 8da9e5f4 2019-01-12 stsp return NULL;
824 8da9e5f4 2019-01-12 stsp static const struct got_error *
825 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
826 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
827 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
828 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
830 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
831 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
832 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
833 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
834 56e0773d 2019-11-28 stsp int tidx = 0;
836 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
837 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
838 8da9e5f4 2019-01-12 stsp if (te && *ie) {
839 18831e78 2019-02-10 stsp char *te_path;
840 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
842 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
843 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
846 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
847 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
848 18831e78 2019-02-10 stsp free(te_path);
849 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
850 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
851 63c5ca5d 2019-08-24 stsp path_len) &&
852 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
853 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
854 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
855 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
857 c4cdcb68 2019-04-03 stsp if (err || entry_name)
860 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
861 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
862 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
863 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
864 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
865 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
866 194cb7cb 2021-01-19 stsp path_len) && entry_name == NULL) {
867 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
868 c4cdcb68 2019-04-03 stsp if (err || entry_name)
871 8da9e5f4 2019-01-12 stsp *ie = next;
873 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
874 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
875 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
876 c4cdcb68 2019-04-03 stsp if (err || entry_name)
879 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
880 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
884 8da9e5f4 2019-01-12 stsp } else if (*ie) {
885 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
886 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
887 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
888 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
889 56e0773d 2019-11-28 stsp entry_name) == 0))) {
890 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
891 c4cdcb68 2019-04-03 stsp if (err || entry_name)
894 8da9e5f4 2019-01-12 stsp *ie = next;
895 8da9e5f4 2019-01-12 stsp } else if (te) {
896 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
897 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
898 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
900 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
901 c4cdcb68 2019-04-03 stsp if (err || entry_name)
904 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
905 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
911 8da9e5f4 2019-01-12 stsp return err;
914 8da9e5f4 2019-01-12 stsp const struct got_error *
915 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
916 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
917 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
918 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
920 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
921 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
922 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
923 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
924 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
925 30a076bc 2019-07-27 stsp cb, cb_arg);
928 d1f6d47b 2019-02-04 stsp static const struct got_error *
929 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
930 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
931 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
933 c577a9ce 2019-07-27 stsp static const struct got_error *
934 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
936 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
937 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
938 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
939 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
942 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
943 c577a9ce 2019-07-27 stsp if (de == NULL) {
944 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
948 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
949 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
953 c577a9ce 2019-07-27 stsp if (dep == NULL) {
958 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
959 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
960 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
961 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
966 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
971 c577a9ce 2019-07-27 stsp if (new == NULL) {
972 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
978 c577a9ce 2019-07-27 stsp return err;
981 8528beef 2021-10-15 thomas static int
982 8528beef 2021-10-15 thomas have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
984 8528beef 2021-10-15 thomas struct got_fileindex_entry *ie;
985 8528beef 2021-10-15 thomas size_t path_len = strlen(path);
988 8528beef 2021-10-15 thomas ie = RB_ROOT(&fileindex->entries);
989 8528beef 2021-10-15 thomas while (ie) {
990 8528beef 2021-10-15 thomas if (got_path_is_child(ie->path, path, path_len))
991 8528beef 2021-10-15 thomas return 1;
992 8528beef 2021-10-15 thomas cmp = got_path_cmp(path, ie->path, path_len,
993 8528beef 2021-10-15 thomas got_fileindex_entry_path_len(ie));
994 8528beef 2021-10-15 thomas if (cmp < 0)
995 8528beef 2021-10-15 thomas ie = RB_LEFT(ie, entry);
996 8528beef 2021-10-15 thomas else if (cmp > 0)
997 8528beef 2021-10-15 thomas ie = RB_RIGHT(ie, entry);
1002 8528beef 2021-10-15 thomas return 0;
1005 d1f6d47b 2019-02-04 stsp static const struct got_error *
1006 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1007 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1008 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
1009 6092c299 2021-10-04 thomas int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1011 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1012 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
1013 965988c5 2019-12-16 stsp DIR *subdir = NULL;
1014 6fc93f37 2019-12-13 stsp int subdirfd = -1;
1016 573463cc 2019-04-06 stsp *next = NULL;
1018 8528beef 2021-10-15 thomas /* Must traverse ignored directories if they contain tracked files. */
1019 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && ignore &&
1020 8528beef 2021-10-15 thomas have_tracked_file_in_dir(fileindex, path))
1021 8528beef 2021-10-15 thomas ignore = 0;
1023 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && !ignore) {
1024 d1f6d47b 2019-02-04 stsp char *subpath;
1025 c7f4312f 2019-02-05 stsp char *subdirpath;
1026 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
1028 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
1030 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
1031 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1032 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1034 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1035 c7f4312f 2019-02-05 stsp free(subpath);
1036 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1039 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
1040 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1041 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
1042 40b289d7 2019-09-07 stsp if (errno == EACCES) {
1043 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
1044 40b289d7 2019-09-07 stsp return NULL;
1046 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1047 d1f6d47b 2019-02-04 stsp free(subpath);
1048 c7f4312f 2019-02-05 stsp free(subdirpath);
1049 ef5e02fd 2019-08-11 stsp return err;
1052 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1053 965988c5 2019-12-16 stsp if (subdir == NULL)
1054 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
1055 965988c5 2019-12-16 stsp subdirfd = -1;
1056 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1057 c577a9ce 2019-07-27 stsp if (err) {
1058 c577a9ce 2019-07-27 stsp free(subpath);
1059 c577a9ce 2019-07-27 stsp free(subdirpath);
1060 965988c5 2019-12-16 stsp closedir(subdir);
1061 c577a9ce 2019-07-27 stsp return err;
1063 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1064 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1065 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1066 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1067 d1f6d47b 2019-02-04 stsp free(subpath);
1068 c7f4312f 2019-02-05 stsp free(subdirpath);
1069 21c2d8be 2023-01-10 thomas got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1071 d1f6d47b 2019-02-04 stsp return err;
1074 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1075 d1f6d47b 2019-02-04 stsp return NULL;
1078 d1f6d47b 2019-02-04 stsp static const struct got_error *
1079 1f480907 2022-01-27 thomas dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1081 1f480907 2022-01-27 thomas const struct got_error *err;
1082 1f480907 2022-01-27 thomas char *dir_path;
1083 1f480907 2022-01-27 thomas int type;
1085 1f480907 2022-01-27 thomas if (de->d_type != DT_UNKNOWN)
1086 1f480907 2022-01-27 thomas return NULL;
1088 1f480907 2022-01-27 thomas /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1089 1f480907 2022-01-27 thomas if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1090 1f480907 2022-01-27 thomas return got_error_from_errno("asprintf");
1091 1f480907 2022-01-27 thomas err = got_path_dirent_type(&type, dir_path, de);
1092 1f480907 2022-01-27 thomas free(dir_path);
1093 1f480907 2022-01-27 thomas if (err)
1094 1f480907 2022-01-27 thomas return err;
1096 1f480907 2022-01-27 thomas de->d_type = type;
1097 1f480907 2022-01-27 thomas return NULL;
1100 1f480907 2022-01-27 thomas static const struct got_error *
1101 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1102 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1103 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1104 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1105 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1107 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1108 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1109 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1110 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1111 6092c299 2021-10-04 thomas int ignore;
1113 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1114 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1116 3143d852 2020-06-25 stsp return err;
1119 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1120 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1121 500cd40f 2019-02-04 stsp if (dle && *ie) {
1122 18831e78 2019-02-10 stsp char *de_path;
1124 f5d3d7af 2019-02-05 stsp de = dle->data;
1125 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1126 1f480907 2022-01-27 thomas if (err)
1128 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1129 18831e78 2019-02-10 stsp de->d_name) == -1) {
1130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1133 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1134 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1135 dd038bc6 2021-09-21 thomas.ad strlen(path) + 1 + strlen(de->d_name));
1136 18831e78 2019-02-10 stsp free(de_path);
1137 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1138 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1142 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1143 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1144 6092c299 2021-10-04 thomas path, rootpath, repo, 0, cb, cb_arg);
1145 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1146 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1149 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1151 6092c299 2021-10-04 thomas err = cb->diff_new(&ignore, cb_arg, de, path,
1155 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1156 6092c299 2021-10-04 thomas path, rootpath, repo, ignore, cb, cb_arg);
1160 554b91b1 2019-02-04 stsp } else if (*ie) {
1161 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1164 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1165 554b91b1 2019-02-04 stsp } else if (dle) {
1166 763e1377 2019-02-05 stsp de = dle->data;
1167 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1168 1f480907 2022-01-27 thomas if (err)
1170 6092c299 2021-10-04 thomas err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1173 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1174 6092c299 2021-10-04 thomas rootpath, repo, ignore, cb, cb_arg);
1180 d1f6d47b 2019-02-04 stsp return err;
1183 d1f6d47b 2019-02-04 stsp const struct got_error *
1184 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1185 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1186 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1188 c577a9ce 2019-07-27 stsp const struct got_error *err;
1189 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1190 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1194 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1197 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1198 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1200 965988c5 2019-12-16 stsp fd2 = dup(fd);
1201 965988c5 2019-12-16 stsp if (fd2 == -1)
1202 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1203 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1204 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1205 3dcf3e74 2020-01-28 stsp close(fd2);
1206 3dcf3e74 2020-01-28 stsp return err;
1208 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1209 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1210 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1211 3dcf3e74 2020-01-28 stsp close(fd2);
1212 3dcf3e74 2020-01-28 stsp return err;
1214 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1215 965988c5 2019-12-16 stsp if (err) {
1216 965988c5 2019-12-16 stsp closedir(dir);
1217 c577a9ce 2019-07-27 stsp return err;
1220 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1221 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1222 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1223 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1224 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1226 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1227 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1228 21c2d8be 2023-01-10 thomas got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1229 c577a9ce 2019-07-27 stsp return err;
1232 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);