2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
21 03df25b3 2019-05-11 stsp #include <errno.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 c48c4a9c 2018-03-11 stsp #include <stdio.h>
24 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
25 c48c4a9c 2018-03-11 stsp #include <string.h>
26 c48c4a9c 2018-03-11 stsp #include <sha1.h>
27 c34b20a2 2018-03-12 stsp #include <endian.h>
28 133d2798 2019-01-08 stsp #include <limits.h>
29 c442a90d 2019-03-10 stsp #include <uuid.h>
31 c48c4a9c 2018-03-11 stsp #include "got_error.h"
32 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
36 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
38 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 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
47 133d2798 2019-01-08 stsp struct got_fileindex {
48 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
49 133d2798 2019-01-08 stsp int nentries;
50 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
54 26a7fe28 2019-07-27 stsp got_fileindex_perms_from_st(struct stat *sb)
56 26a7fe28 2019-07-27 stsp uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
57 26a7fe28 2019-07-27 stsp return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
63 26a7fe28 2019-07-27 stsp mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
64 8957ae76 2019-08-08 stsp return (S_IFREG | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
67 c48c4a9c 2018-03-11 stsp const struct got_error *
68 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
69 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
70 02c07007 2019-02-10 stsp int update_timestamps)
72 c48c4a9c 2018-03-11 stsp struct stat sb;
74 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
75 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
76 b15816dd 2019-08-11 stsp errno == ENOENT))
77 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
79 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
80 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
81 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
85 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
86 13d9040b 2019-03-27 stsp if (update_timestamps) {
87 3f762da0 2019-08-03 stsp ie->ctime_sec = sb.st_ctime;
88 3f762da0 2019-08-03 stsp ie->ctime_nsec = sb.st_ctimensec;
89 3f762da0 2019-08-03 stsp ie->mtime_sec = sb.st_mtime;
90 3f762da0 2019-08-03 stsp ie->mtime_nsec = sb.st_mtimensec;
92 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
93 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
94 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
95 13d9040b 2019-03-27 stsp if (sb.st_mode & S_IFLNK)
96 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_SYMLINK;
98 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
99 3f762da0 2019-08-03 stsp ie->mode |= got_fileindex_perms_from_st(&sb);
102 ddce0520 2019-03-26 stsp if (blob_sha1) {
103 3f762da0 2019-08-03 stsp memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
104 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
106 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
108 ddce0520 2019-03-26 stsp if (commit_sha1) {
109 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
110 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
112 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
114 51514078 2018-12-25 stsp return NULL;
118 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
120 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
123 51514078 2018-12-25 stsp const struct got_error *
124 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
125 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
126 51514078 2018-12-25 stsp uint8_t *commit_sha1)
128 51514078 2018-12-25 stsp size_t len;
130 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
131 3f762da0 2019-08-03 stsp if (*ie == NULL)
132 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
134 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
135 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
136 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
138 3f762da0 2019-08-03 stsp *ie = NULL;
139 0a585a0d 2018-03-17 stsp return err;
142 4d555405 2019-08-03 stsp len = strlen(relpath);
143 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
144 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
145 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
147 3f762da0 2019-08-03 stsp return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
148 02c07007 2019-02-10 stsp commit_sha1, 1);
152 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
154 3f762da0 2019-08-03 stsp free(ie->path);
159 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
161 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
165 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
167 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
171 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
173 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
174 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
175 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
179 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
181 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
185 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
187 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
191 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
193 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
196 50952927 2019-01-12 stsp static const struct got_error *
197 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
199 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
200 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
202 3f762da0 2019-08-03 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
203 133d2798 2019-01-08 stsp fileindex->nentries++;
204 133d2798 2019-01-08 stsp return NULL;
207 50952927 2019-01-12 stsp const struct got_error *
208 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
209 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
211 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
212 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
214 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
218 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
219 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
221 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
222 133d2798 2019-01-08 stsp fileindex->nentries--;
225 51514078 2018-12-25 stsp struct got_fileindex_entry *
226 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
227 d6c87207 2019-08-02 stsp size_t path_len)
229 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
230 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
231 133d2798 2019-01-08 stsp key.path = (char *)path;
232 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
233 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
236 512f0d0e 2019-01-02 stsp const struct got_error *
237 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
238 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
240 133d2798 2019-01-08 stsp const struct got_error *err;
241 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
243 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
244 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
246 133d2798 2019-01-08 stsp return err;
248 b504a804 2019-01-08 stsp return NULL;
251 133d2798 2019-01-08 stsp struct got_fileindex *
252 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
254 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
256 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
257 133d2798 2019-01-08 stsp if (fileindex == NULL)
258 133d2798 2019-01-08 stsp return NULL;
260 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
261 133d2798 2019-01-08 stsp return fileindex;
265 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
267 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
269 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
270 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
271 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
273 9d31a1d8 2018-03-11 stsp free(fileindex);
276 c34b20a2 2018-03-12 stsp static const struct got_error *
277 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
281 c34b20a2 2018-03-12 stsp val = htobe64(val);
282 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
283 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
284 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
285 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
286 c34b20a2 2018-03-12 stsp return NULL;
289 c34b20a2 2018-03-12 stsp static const struct got_error *
290 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
294 c34b20a2 2018-03-12 stsp val = htobe32(val);
295 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
296 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
297 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
298 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
299 c34b20a2 2018-03-12 stsp return NULL;
302 c34b20a2 2018-03-12 stsp static const struct got_error *
303 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
307 c34b20a2 2018-03-12 stsp val = htobe16(val);
308 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
309 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
310 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
311 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
312 c34b20a2 2018-03-12 stsp return NULL;
315 c34b20a2 2018-03-12 stsp static const struct got_error *
316 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
318 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
319 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
321 c34b20a2 2018-03-12 stsp len = strlen(path);
322 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
324 3c5b70f2 2018-12-27 stsp if (pad == 0)
325 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
327 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
328 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
329 c34b20a2 2018-03-12 stsp if (n != len)
330 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
331 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
332 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
333 c34b20a2 2018-03-12 stsp if (n != pad)
334 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
335 c34b20a2 2018-03-12 stsp return NULL;
338 c34b20a2 2018-03-12 stsp static const struct got_error *
339 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
340 c34b20a2 2018-03-12 stsp FILE *outfile)
342 c34b20a2 2018-03-12 stsp const struct got_error *err;
344 df335242 2019-08-03 stsp uint32_t stage;
346 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
348 c34b20a2 2018-03-12 stsp return err;
349 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
351 c34b20a2 2018-03-12 stsp return err;
352 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
354 c34b20a2 2018-03-12 stsp return err;
355 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
357 c34b20a2 2018-03-12 stsp return err;
359 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
361 c34b20a2 2018-03-12 stsp return err;
362 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
364 c34b20a2 2018-03-12 stsp return err;
365 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
367 c34b20a2 2018-03-12 stsp return err;
369 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
371 c34b20a2 2018-03-12 stsp return err;
373 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
374 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
375 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
376 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
378 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
379 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
380 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
381 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
383 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
385 c34b20a2 2018-03-12 stsp return err;
387 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
389 df335242 2019-08-03 stsp return err;
391 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
392 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
393 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
394 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
395 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
397 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
398 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
401 df335242 2019-08-03 stsp return NULL;
404 9d31a1d8 2018-03-11 stsp const struct got_error *
405 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
407 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
408 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
409 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
410 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
412 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
414 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
416 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
417 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
418 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
420 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
421 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
422 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
423 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
424 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
425 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
426 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
427 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
428 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
429 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
430 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
431 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
433 3f762da0 2019-08-03 stsp RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
434 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
435 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
437 133d2798 2019-01-08 stsp return err;
440 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
441 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
442 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
443 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
445 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
446 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
448 52a74475 2018-12-24 stsp return NULL;
451 52a74475 2018-12-24 stsp static const struct got_error *
452 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
456 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
457 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
458 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
459 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
460 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
461 52a74475 2018-12-24 stsp return NULL;
464 52a74475 2018-12-24 stsp static const struct got_error *
465 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
469 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
470 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
471 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
472 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
473 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
474 52a74475 2018-12-24 stsp return NULL;
477 52a74475 2018-12-24 stsp static const struct got_error *
478 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
482 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
483 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
484 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
485 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
486 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
487 52a74475 2018-12-24 stsp return NULL;
490 52a74475 2018-12-24 stsp static const struct got_error *
491 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
493 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
494 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
495 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
497 52a74475 2018-12-24 stsp *path = malloc(totlen);
498 52a74475 2018-12-24 stsp if (*path == NULL)
499 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
502 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
503 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
504 52a74475 2018-12-24 stsp if (p == NULL) {
505 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
508 6bf2c316 2019-08-02 stsp totlen += chunk_size;
511 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
512 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
513 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
516 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
517 6bf2c316 2019-08-02 stsp len += chunk_size;
518 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
521 52a74475 2018-12-24 stsp free(*path);
522 52a74475 2018-12-24 stsp *path = NULL;
524 52a74475 2018-12-24 stsp return err;
527 52a74475 2018-12-24 stsp static const struct got_error *
528 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
529 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
531 52a74475 2018-12-24 stsp const struct got_error *err;
532 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
535 3f762da0 2019-08-03 stsp *iep = NULL;
537 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
538 3f762da0 2019-08-03 stsp if (ie == NULL)
539 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
541 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
544 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
547 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
550 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
554 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
557 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
560 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
564 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
568 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
569 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
570 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
575 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
576 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
577 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
582 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
586 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
590 df335242 2019-08-03 stsp if (version >= 2) {
591 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
592 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
593 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
594 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
596 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
597 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
600 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
603 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
604 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
609 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
612 52a74475 2018-12-24 stsp return err;
615 52a74475 2018-12-24 stsp const struct got_error *
616 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
618 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
619 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
620 52a74475 2018-12-24 stsp SHA1_CTX ctx;
621 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
622 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
623 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
627 52a74475 2018-12-24 stsp SHA1Init(&ctx);
629 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
630 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
631 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
632 b6d05318 2019-01-13 stsp return NULL;
633 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
635 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
636 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
637 51514078 2018-12-25 stsp if (n == 0) /* EOF */
638 51514078 2018-12-25 stsp return NULL;
639 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
641 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
642 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
643 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
644 b6d05318 2019-01-13 stsp return NULL;
645 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
648 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
649 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
650 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
652 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
653 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
654 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
656 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
657 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
658 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
659 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
661 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
662 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
664 52a74475 2018-12-24 stsp return err;
665 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
667 52a74475 2018-12-24 stsp return err;
670 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
671 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
672 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
673 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
674 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
675 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
677 9d31a1d8 2018-03-11 stsp return NULL;
680 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
681 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
683 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
685 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
687 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
688 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
689 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
691 50952927 2019-01-12 stsp return next;
694 8da9e5f4 2019-01-12 stsp static const struct got_error *
695 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
696 30a076bc 2019-07-27 stsp const struct got_tree_entries *, const char *, const char *,
697 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
699 9d2a8e53 2019-01-28 stsp static const struct got_error *
700 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
701 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
702 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
703 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
705 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
707 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
708 8da9e5f4 2019-01-12 stsp char *subpath;
709 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
711 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
712 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
713 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
715 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
717 8da9e5f4 2019-01-12 stsp free(subpath);
718 8da9e5f4 2019-01-12 stsp return err;
721 30a076bc 2019-07-27 stsp err = diff_fileindex_tree(fileindex, ie,
722 30a076bc 2019-07-27 stsp got_object_tree_get_entries(subtree), subpath, entry_name,
723 30a076bc 2019-07-27 stsp repo, cb, cb_arg);
724 8da9e5f4 2019-01-12 stsp free(subpath);
725 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
727 8da9e5f4 2019-01-12 stsp return err;
730 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
731 8da9e5f4 2019-01-12 stsp return NULL;
734 8da9e5f4 2019-01-12 stsp static const struct got_error *
735 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
736 30a076bc 2019-07-27 stsp struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
737 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
738 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
740 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
741 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
742 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
743 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
745 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
746 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
747 8da9e5f4 2019-01-12 stsp if (te && *ie) {
748 18831e78 2019-02-10 stsp char *te_path;
750 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
751 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
754 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
755 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
756 18831e78 2019-02-10 stsp free(te_path);
757 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
758 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
759 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
760 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
761 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
763 c4cdcb68 2019-04-03 stsp if (err || entry_name)
766 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
767 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
768 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
769 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
770 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
771 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
772 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
773 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
774 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
775 c4cdcb68 2019-04-03 stsp if (err || entry_name)
778 8da9e5f4 2019-01-12 stsp *ie = next;
780 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
781 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
782 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
783 c4cdcb68 2019-04-03 stsp if (err || entry_name)
786 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
787 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
791 8da9e5f4 2019-01-12 stsp } else if (*ie) {
792 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
793 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
794 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
795 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
796 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
797 c4cdcb68 2019-04-03 stsp if (err || entry_name)
800 8da9e5f4 2019-01-12 stsp *ie = next;
801 8da9e5f4 2019-01-12 stsp } else if (te) {
802 c4cdcb68 2019-04-03 stsp if (entry_name == NULL ||
803 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0) {
804 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
805 c4cdcb68 2019-04-03 stsp if (err || entry_name)
808 c4cdcb68 2019-04-03 stsp err = walk_tree(&te, fileindex, ie, te, path,
809 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
815 8da9e5f4 2019-01-12 stsp return err;
818 8da9e5f4 2019-01-12 stsp const struct got_error *
819 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
820 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
821 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
822 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
824 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
825 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
826 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
827 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
828 30a076bc 2019-07-27 stsp return diff_fileindex_tree(fileindex, &ie,
829 30a076bc 2019-07-27 stsp got_object_tree_get_entries(tree), path, entry_name, repo,
830 30a076bc 2019-07-27 stsp cb, cb_arg);
833 d1f6d47b 2019-02-04 stsp static const struct got_error *
834 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
835 c577a9ce 2019-07-27 stsp struct got_pathlist_head *, const char *, const char *,
836 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
838 c577a9ce 2019-07-27 stsp static const struct got_error *
839 c577a9ce 2019-07-27 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
841 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
842 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
843 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
844 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
847 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
848 c577a9ce 2019-07-27 stsp if (de == NULL) {
849 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
853 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
854 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
858 c577a9ce 2019-07-27 stsp if (dep == NULL) {
863 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
864 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
865 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
866 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
871 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
876 c577a9ce 2019-07-27 stsp if (new == NULL) {
877 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
883 c577a9ce 2019-07-27 stsp return err;
887 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
889 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
891 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
892 c577a9ce 2019-07-27 stsp free(dle->data);
893 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
896 d1f6d47b 2019-02-04 stsp static const struct got_error *
897 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
898 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
899 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
900 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
902 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
903 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
905 573463cc 2019-04-06 stsp *next = NULL;
907 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
908 d1f6d47b 2019-02-04 stsp char *subpath;
909 c7f4312f 2019-02-05 stsp char *subdirpath;
910 d1f6d47b 2019-02-04 stsp DIR *subdir;
911 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
913 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
915 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
916 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
917 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
919 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
920 c7f4312f 2019-02-05 stsp free(subpath);
921 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
924 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
925 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
926 ef5e02fd 2019-08-11 stsp err = got_error_from_errno2("opendir", subdirpath);
927 d1f6d47b 2019-02-04 stsp free(subpath);
928 c7f4312f 2019-02-05 stsp free(subdirpath);
929 ef5e02fd 2019-08-11 stsp return err;
932 c577a9ce 2019-07-27 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
934 c577a9ce 2019-07-27 stsp free(subpath);
935 c577a9ce 2019-07-27 stsp free(subdirpath);
936 c577a9ce 2019-07-27 stsp closedir(subdir);
937 c577a9ce 2019-07-27 stsp return err;
939 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
940 39beb6da 2019-07-27 stsp subpath, repo, cb, cb_arg);
941 d1f6d47b 2019-02-04 stsp free(subpath);
942 c7f4312f 2019-02-05 stsp free(subdirpath);
943 d1f6d47b 2019-02-04 stsp closedir(subdir);
944 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
946 d1f6d47b 2019-02-04 stsp return err;
949 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
950 d1f6d47b 2019-02-04 stsp return NULL;
953 d1f6d47b 2019-02-04 stsp static const struct got_error *
954 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
955 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
956 39beb6da 2019-07-27 stsp const char *rootpath, const char *path, struct got_repository *repo,
957 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
959 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
960 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
961 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
962 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
964 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
965 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
966 500cd40f 2019-02-04 stsp if (dle && *ie) {
967 18831e78 2019-02-10 stsp char *de_path;
969 f5d3d7af 2019-02-05 stsp de = dle->data;
970 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
971 18831e78 2019-02-10 stsp de->d_name) == -1) {
972 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
975 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
976 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
977 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
978 18831e78 2019-02-10 stsp free(de_path);
979 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
980 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
983 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
984 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
985 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
986 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
987 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
990 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
992 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
995 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
996 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
1000 554b91b1 2019-02-04 stsp } else if (*ie) {
1001 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1004 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1005 554b91b1 2019-02-04 stsp } else if (dle) {
1006 763e1377 2019-02-05 stsp de = dle->data;
1007 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
1010 39beb6da 2019-07-27 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
1011 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
1017 d1f6d47b 2019-02-04 stsp return err;
1020 d1f6d47b 2019-02-04 stsp const struct got_error *
1021 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
1022 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1023 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1025 c577a9ce 2019-07-27 stsp const struct got_error *err;
1026 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1027 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1029 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1030 c577a9ce 2019-07-27 stsp err = read_dirlist(&dirlist, rootdir, path);
1032 c577a9ce 2019-07-27 stsp return err;
1033 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1034 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1035 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1036 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
1037 39beb6da 2019-07-27 stsp repo, cb, cb_arg);
1038 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1039 c577a9ce 2019-07-27 stsp return err;
1042 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);