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 0aeb8099 2020-07-23 stsp got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
59 4723f050 2020-07-23 stsp return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
60 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
64 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
66 4723f050 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
67 4723f050 2020-07-23 stsp ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
68 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS);
72 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
74 0aeb8099 2020-07-23 stsp mode_t perms = got_fileindex_entry_perms_get(ie);
75 4723f050 2020-07-23 stsp int type = got_fileindex_entry_filetype_get(ie);
76 4723f050 2020-07-23 stsp uint32_t ftype;
78 4723f050 2020-07-23 stsp if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
79 4723f050 2020-07-23 stsp type == GOT_FILEIDX_MODE_BAD_SYMLINK)
80 4723f050 2020-07-23 stsp ftype = S_IFREG;
82 4723f050 2020-07-23 stsp ftype = S_IFLNK;
84 4723f050 2020-07-23 stsp return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
87 c48c4a9c 2018-03-11 stsp const struct got_error *
88 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
89 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
90 02c07007 2019-02-10 stsp int update_timestamps)
92 c48c4a9c 2018-03-11 stsp struct stat sb;
94 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
95 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 b15816dd 2019-08-11 stsp errno == ENOENT))
97 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
98 aa9ad276 2020-07-25 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
100 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
101 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
102 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
106 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
107 13d9040b 2019-03-27 stsp if (update_timestamps) {
108 0823ffc2 2020-09-10 naddy ie->ctime_sec = sb.st_ctim.tv_sec;
109 0823ffc2 2020-09-10 naddy ie->ctime_nsec = sb.st_ctim.tv_nsec;
110 0823ffc2 2020-09-10 naddy ie->mtime_sec = sb.st_mtim.tv_sec;
111 0823ffc2 2020-09-10 naddy ie->mtime_nsec = sb.st_mtim.tv_nsec;
113 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
114 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
115 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
116 4723f050 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
117 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
118 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
119 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie, 0);
121 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
122 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
123 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie,
124 4723f050 2020-07-23 stsp sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 ddce0520 2019-03-26 stsp if (blob_sha1) {
129 3f762da0 2019-08-03 stsp memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
132 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 ddce0520 2019-03-26 stsp if (commit_sha1) {
135 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
138 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
140 51514078 2018-12-25 stsp return NULL;
144 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
146 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
149 51514078 2018-12-25 stsp const struct got_error *
150 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
151 3969253a 2020-03-07 stsp const char *relpath)
153 51514078 2018-12-25 stsp size_t len;
155 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
156 3f762da0 2019-08-03 stsp if (*ie == NULL)
157 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
159 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
160 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
161 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
163 3f762da0 2019-08-03 stsp *ie = NULL;
164 0a585a0d 2018-03-17 stsp return err;
167 4d555405 2019-08-03 stsp len = strlen(relpath);
168 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
169 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
170 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
172 3969253a 2020-03-07 stsp return NULL;
176 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
178 3f762da0 2019-08-03 stsp free(ie->path);
183 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
185 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
189 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
191 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
195 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
197 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
198 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
199 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
203 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
205 f5f1f9c2 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
209 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
211 6131ab45 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
212 6131ab45 2020-07-23 stsp ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
216 984c073d 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type)
218 984c073d 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
219 984c073d 2020-07-23 stsp ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
220 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
224 984c073d 2020-07-23 stsp got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
226 984c073d 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
227 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
231 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
233 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
237 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
239 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
243 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
245 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
248 50952927 2019-01-12 stsp static const struct got_error *
249 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
251 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
252 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
254 3f762da0 2019-08-03 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
255 133d2798 2019-01-08 stsp fileindex->nentries++;
256 133d2798 2019-01-08 stsp return NULL;
259 50952927 2019-01-12 stsp const struct got_error *
260 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
261 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
263 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
264 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
266 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
270 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
271 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
274 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
275 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
276 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
277 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
278 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
280 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
281 133d2798 2019-01-08 stsp fileindex->nentries--;
284 51514078 2018-12-25 stsp struct got_fileindex_entry *
285 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
286 d6c87207 2019-08-02 stsp size_t path_len)
288 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
289 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
290 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
291 133d2798 2019-01-08 stsp key.path = (char *)path;
292 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
293 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
294 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
295 a46b9f33 2020-01-28 stsp return NULL;
299 512f0d0e 2019-01-02 stsp const struct got_error *
300 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
301 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
303 133d2798 2019-01-08 stsp const struct got_error *err;
304 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
306 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
307 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
309 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
311 133d2798 2019-01-08 stsp return err;
313 b504a804 2019-01-08 stsp return NULL;
316 133d2798 2019-01-08 stsp struct got_fileindex *
317 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
319 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
321 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
322 133d2798 2019-01-08 stsp if (fileindex == NULL)
323 133d2798 2019-01-08 stsp return NULL;
325 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
326 133d2798 2019-01-08 stsp return fileindex;
330 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
332 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
334 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
335 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
336 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
338 9d31a1d8 2018-03-11 stsp free(fileindex);
341 c34b20a2 2018-03-12 stsp static const struct got_error *
342 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
346 c34b20a2 2018-03-12 stsp val = htobe64(val);
347 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
348 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
349 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
350 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
351 c34b20a2 2018-03-12 stsp return NULL;
354 c34b20a2 2018-03-12 stsp static const struct got_error *
355 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
359 c34b20a2 2018-03-12 stsp val = htobe32(val);
360 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
361 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
362 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
363 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
364 c34b20a2 2018-03-12 stsp return NULL;
367 c34b20a2 2018-03-12 stsp static const struct got_error *
368 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
372 c34b20a2 2018-03-12 stsp val = htobe16(val);
373 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
374 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
375 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
376 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
377 c34b20a2 2018-03-12 stsp return NULL;
380 c34b20a2 2018-03-12 stsp static const struct got_error *
381 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
383 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
384 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
386 c34b20a2 2018-03-12 stsp len = strlen(path);
387 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
389 3c5b70f2 2018-12-27 stsp if (pad == 0)
390 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
392 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
393 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
394 c34b20a2 2018-03-12 stsp if (n != len)
395 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
396 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
397 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
398 c34b20a2 2018-03-12 stsp if (n != pad)
399 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
400 c34b20a2 2018-03-12 stsp return NULL;
403 c34b20a2 2018-03-12 stsp static const struct got_error *
404 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
405 c34b20a2 2018-03-12 stsp FILE *outfile)
407 c34b20a2 2018-03-12 stsp const struct got_error *err;
409 df335242 2019-08-03 stsp uint32_t stage;
411 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
413 c34b20a2 2018-03-12 stsp return err;
414 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
416 c34b20a2 2018-03-12 stsp return err;
417 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
419 c34b20a2 2018-03-12 stsp return err;
420 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
422 c34b20a2 2018-03-12 stsp return err;
424 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
426 c34b20a2 2018-03-12 stsp return err;
427 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
429 c34b20a2 2018-03-12 stsp return err;
430 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
432 c34b20a2 2018-03-12 stsp return err;
434 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
436 c34b20a2 2018-03-12 stsp return err;
438 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
439 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
440 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
441 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
443 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
444 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
445 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
446 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
448 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
450 c34b20a2 2018-03-12 stsp return err;
452 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
454 df335242 2019-08-03 stsp return err;
456 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
457 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
458 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
459 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
460 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
462 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
463 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
466 df335242 2019-08-03 stsp return NULL;
469 9d31a1d8 2018-03-11 stsp const struct got_error *
470 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
472 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
473 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
474 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
475 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
477 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
479 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
481 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
482 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
483 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
485 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
486 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
487 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
488 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
489 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
490 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
491 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
492 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
493 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
494 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
495 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
496 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
498 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
499 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
500 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
501 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
502 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
505 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
507 133d2798 2019-01-08 stsp return err;
510 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
511 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
512 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
513 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
515 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
516 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
518 52a74475 2018-12-24 stsp return NULL;
521 52a74475 2018-12-24 stsp static const struct got_error *
522 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
526 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
527 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
528 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
529 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
530 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
531 52a74475 2018-12-24 stsp return NULL;
534 52a74475 2018-12-24 stsp static const struct got_error *
535 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
539 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
540 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
541 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
543 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
544 52a74475 2018-12-24 stsp return NULL;
547 52a74475 2018-12-24 stsp static const struct got_error *
548 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
552 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
553 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
554 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
556 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
557 52a74475 2018-12-24 stsp return NULL;
560 52a74475 2018-12-24 stsp static const struct got_error *
561 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
563 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
564 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
565 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
567 52a74475 2018-12-24 stsp *path = malloc(totlen);
568 52a74475 2018-12-24 stsp if (*path == NULL)
569 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
572 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
573 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
574 52a74475 2018-12-24 stsp if (p == NULL) {
575 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
578 6bf2c316 2019-08-02 stsp totlen += chunk_size;
581 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
582 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
583 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
586 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
587 6bf2c316 2019-08-02 stsp len += chunk_size;
588 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
591 52a74475 2018-12-24 stsp free(*path);
592 52a74475 2018-12-24 stsp *path = NULL;
594 52a74475 2018-12-24 stsp return err;
597 52a74475 2018-12-24 stsp static const struct got_error *
598 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
599 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
601 52a74475 2018-12-24 stsp const struct got_error *err;
602 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
605 3f762da0 2019-08-03 stsp *iep = NULL;
607 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
608 3f762da0 2019-08-03 stsp if (ie == NULL)
609 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
611 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
614 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
617 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
620 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
624 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
627 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
630 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
634 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
638 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
639 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
640 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
643 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
645 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
646 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
647 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
650 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
652 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
656 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
660 df335242 2019-08-03 stsp if (version >= 2) {
661 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
662 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
663 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
664 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
666 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
667 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
670 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
673 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
674 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
679 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
682 52a74475 2018-12-24 stsp return err;
685 52a74475 2018-12-24 stsp const struct got_error *
686 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
688 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
689 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
690 52a74475 2018-12-24 stsp SHA1_CTX ctx;
691 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
692 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
693 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
697 52a74475 2018-12-24 stsp SHA1Init(&ctx);
699 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
700 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
701 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
702 b6d05318 2019-01-13 stsp return NULL;
703 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
705 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
706 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
707 51514078 2018-12-25 stsp if (n == 0) /* EOF */
708 51514078 2018-12-25 stsp return NULL;
709 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
711 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
712 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
713 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
714 b6d05318 2019-01-13 stsp return NULL;
715 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
718 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
719 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
720 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
722 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
723 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
724 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
726 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
727 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
728 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
729 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
731 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
732 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
734 52a74475 2018-12-24 stsp return err;
735 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
737 52a74475 2018-12-24 stsp return err;
740 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
741 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
742 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
743 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
744 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
745 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
747 9d31a1d8 2018-03-11 stsp return NULL;
750 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
751 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
753 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
755 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
757 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
758 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
759 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
760 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
762 50952927 2019-01-12 stsp return next;
765 8da9e5f4 2019-01-12 stsp static const struct got_error *
766 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
767 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
768 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
770 9d2a8e53 2019-01-28 stsp static const struct got_error *
771 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
772 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
773 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
774 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
776 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
777 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
779 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
780 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
781 8da9e5f4 2019-01-12 stsp char *subpath;
782 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
784 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
785 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
786 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
787 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
789 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
790 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
792 8da9e5f4 2019-01-12 stsp free(subpath);
793 8da9e5f4 2019-01-12 stsp return err;
796 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
797 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
798 8da9e5f4 2019-01-12 stsp free(subpath);
799 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
801 8da9e5f4 2019-01-12 stsp return err;
805 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
806 8da9e5f4 2019-01-12 stsp return NULL;
809 8da9e5f4 2019-01-12 stsp static const struct got_error *
810 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
811 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
812 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
813 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
815 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
816 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
817 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
818 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
819 56e0773d 2019-11-28 stsp int tidx = 0;
821 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
822 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
823 8da9e5f4 2019-01-12 stsp if (te && *ie) {
824 18831e78 2019-02-10 stsp char *te_path;
825 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
827 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
828 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
831 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
832 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
833 18831e78 2019-02-10 stsp free(te_path);
834 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
835 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
836 63c5ca5d 2019-08-24 stsp path_len) &&
837 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
838 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
839 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
840 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
842 c4cdcb68 2019-04-03 stsp if (err || entry_name)
845 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
846 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
847 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
848 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
849 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
850 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
851 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
852 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
853 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
854 c4cdcb68 2019-04-03 stsp if (err || entry_name)
857 8da9e5f4 2019-01-12 stsp *ie = next;
859 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
860 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
861 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
862 c4cdcb68 2019-04-03 stsp if (err || entry_name)
865 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
866 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
870 8da9e5f4 2019-01-12 stsp } else if (*ie) {
871 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
872 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
873 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
874 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
875 56e0773d 2019-11-28 stsp entry_name) == 0))) {
876 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
877 c4cdcb68 2019-04-03 stsp if (err || entry_name)
880 8da9e5f4 2019-01-12 stsp *ie = next;
881 8da9e5f4 2019-01-12 stsp } else if (te) {
882 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
883 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
884 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
886 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
887 c4cdcb68 2019-04-03 stsp if (err || entry_name)
890 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
891 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
897 8da9e5f4 2019-01-12 stsp return err;
900 8da9e5f4 2019-01-12 stsp const struct got_error *
901 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
902 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
903 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
904 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
906 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
907 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
908 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
909 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
910 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
911 30a076bc 2019-07-27 stsp cb, cb_arg);
914 d1f6d47b 2019-02-04 stsp static const struct got_error *
915 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
916 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
917 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
919 c577a9ce 2019-07-27 stsp static const struct got_error *
920 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
922 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
923 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
924 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
925 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
928 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
929 c577a9ce 2019-07-27 stsp if (de == NULL) {
930 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
934 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
935 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
939 c577a9ce 2019-07-27 stsp if (dep == NULL) {
944 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
945 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
946 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
947 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
952 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
957 c577a9ce 2019-07-27 stsp if (new == NULL) {
958 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
964 c577a9ce 2019-07-27 stsp return err;
968 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
970 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
972 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
973 c577a9ce 2019-07-27 stsp free(dle->data);
974 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
977 d1f6d47b 2019-02-04 stsp static const struct got_error *
978 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
979 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
980 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
981 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
983 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
984 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
985 965988c5 2019-12-16 stsp DIR *subdir = NULL;
986 6fc93f37 2019-12-13 stsp int subdirfd = -1;
989 573463cc 2019-04-06 stsp *next = NULL;
991 20ccae39 2020-07-21 stsp if (de->d_type == DT_UNKNOWN) {
992 20ccae39 2020-07-21 stsp /* Occurs on NFS mounts without "readdir plus" RPC. */
993 20ccae39 2020-07-21 stsp char *dir_path;
994 20ccae39 2020-07-21 stsp if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
995 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
996 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, dir_path, de);
997 20ccae39 2020-07-21 stsp free(dir_path);
999 20ccae39 2020-07-21 stsp return err;
1001 20ccae39 2020-07-21 stsp type = de->d_type;
1003 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
1004 d1f6d47b 2019-02-04 stsp char *subpath;
1005 c7f4312f 2019-02-05 stsp char *subdirpath;
1006 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
1008 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
1010 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
1011 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1012 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1014 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1015 c7f4312f 2019-02-05 stsp free(subpath);
1016 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1019 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
1020 6fc93f37 2019-12-13 stsp O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
1021 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
1022 40b289d7 2019-09-07 stsp if (errno == EACCES) {
1023 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
1024 40b289d7 2019-09-07 stsp return NULL;
1026 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1027 d1f6d47b 2019-02-04 stsp free(subpath);
1028 c7f4312f 2019-02-05 stsp free(subdirpath);
1029 ef5e02fd 2019-08-11 stsp return err;
1032 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1033 965988c5 2019-12-16 stsp if (subdir == NULL)
1034 965988c5 2019-12-16 stsp return got_error_from_errno2("fdopendir", path);
1035 965988c5 2019-12-16 stsp subdirfd = -1;
1036 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1037 c577a9ce 2019-07-27 stsp if (err) {
1038 c577a9ce 2019-07-27 stsp free(subpath);
1039 c577a9ce 2019-07-27 stsp free(subdirpath);
1040 965988c5 2019-12-16 stsp closedir(subdir);
1041 c577a9ce 2019-07-27 stsp return err;
1043 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1044 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1045 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1046 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1047 d1f6d47b 2019-02-04 stsp free(subpath);
1048 c7f4312f 2019-02-05 stsp free(subdirpath);
1049 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
1051 d1f6d47b 2019-02-04 stsp return err;
1054 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1055 d1f6d47b 2019-02-04 stsp return NULL;
1058 d1f6d47b 2019-02-04 stsp static const struct got_error *
1059 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1060 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1061 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1062 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1063 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1065 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1066 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1067 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1068 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1070 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1071 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1073 3143d852 2020-06-25 stsp return err;
1076 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1077 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1078 500cd40f 2019-02-04 stsp if (dle && *ie) {
1079 18831e78 2019-02-10 stsp char *de_path;
1081 f5d3d7af 2019-02-05 stsp de = dle->data;
1082 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1083 18831e78 2019-02-10 stsp de->d_name) == -1) {
1084 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1087 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1088 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1089 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
1090 18831e78 2019-02-10 stsp free(de_path);
1091 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1092 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1096 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1097 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1098 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1099 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1100 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1103 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1105 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1108 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1109 6fc93f37 2019-12-13 stsp path, rootpath, repo, cb, cb_arg);
1113 554b91b1 2019-02-04 stsp } else if (*ie) {
1114 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1117 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1118 554b91b1 2019-02-04 stsp } else if (dle) {
1119 763e1377 2019-02-05 stsp de = dle->data;
1120 7f91a133 2019-12-13 stsp err = cb->diff_new(cb_arg, de, path, dirfd);
1123 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1124 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
1130 d1f6d47b 2019-02-04 stsp return err;
1133 d1f6d47b 2019-02-04 stsp const struct got_error *
1134 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1135 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1136 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1138 c577a9ce 2019-07-27 stsp const struct got_error *err;
1139 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1140 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1144 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1147 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1148 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1150 965988c5 2019-12-16 stsp fd2 = dup(fd);
1151 965988c5 2019-12-16 stsp if (fd2 == -1)
1152 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1153 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1154 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1155 3dcf3e74 2020-01-28 stsp close(fd2);
1156 3dcf3e74 2020-01-28 stsp return err;
1158 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1159 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1160 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1161 3dcf3e74 2020-01-28 stsp close(fd2);
1162 3dcf3e74 2020-01-28 stsp return err;
1164 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1165 965988c5 2019-12-16 stsp if (err) {
1166 965988c5 2019-12-16 stsp closedir(dir);
1167 c577a9ce 2019-07-27 stsp return err;
1170 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1171 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1172 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1173 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1174 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1176 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1177 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1178 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1179 c577a9ce 2019-07-27 stsp return err;
1182 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);