Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 /*
18 * State information for a tracked file in a work tree.
19 * When written to disk, multi-byte fields are written in big-endian.
20 * Some fields are based on results from stat(2). These are only used in
21 * order to detect modifications made to on-disk files, they are never
22 * applied back to the filesystem.
23 */
24 struct got_fileindex_entry {
25 RB_ENTRY(got_fileindex_entry) entry;
26 uint64_t ctime_sec;
27 uint64_t ctime_nsec;
28 uint64_t mtime_sec;
29 uint64_t mtime_nsec;
30 uint32_t uid;
31 uint32_t gid;
32 /*
33 * On-disk size is truncated to the lower 32 bits.
34 * The value is only used to check for modifications anyway.
35 */
36 uint32_t size;
38 uint16_t mode;
39 #define GOT_FILEIDX_MODE_FILE_TYPE 0x000f
40 #define GOT_FILEIDX_MODE_REGULAR_FILE 1
41 #define GOT_FILEIDX_MODE_SYMLINK 2
42 #define GOT_FILEIDX_MODE_PERMS 0xff10
43 #define GOT_FILEIDX_MODE_PERMS_SHIFT 4
45 /* SHA1 of corresponding blob in repository. */
46 uint8_t blob_sha1[SHA1_DIGEST_LENGTH];
48 /* SHA1 of corresponding base commit in repository. */
49 uint8_t commit_sha1[SHA1_DIGEST_LENGTH];
51 uint32_t flags;
53 /*
54 * UNIX-style path, relative to work tree root.
55 * Variable length, and NUL-padded to a multiple of 8 on disk.
56 */
57 char *path;
58 size_t path_len; /* strlen(path) -- kept in memory only! */
60 /* More data could be here if F_EXTENDED is set; To be determined... */
61 };
63 /* "Stages" of a file afflicted by a 3-way merge conflict. */
64 #define GOT_FILEIDX_STAGE_MERGED 0
65 #define GOT_FILEIDX_STAGE_ANCESTOR 1
66 #define GOT_FILEIDX_STAGE_OURS 2
67 #define GOT_FILEIDX_STAGE_THEIRS 3
69 struct got_fileindex;
71 RB_HEAD(got_fileindex_tree, got_fileindex_entry);
73 static inline int
74 got_fileindex_cmp(const struct got_fileindex_entry *e1,
75 const struct got_fileindex_entry *e2)
76 {
77 return got_path_cmp(e1->path, e2->path, e1->path_len, e2->path_len);
78 }
80 RB_PROTOTYPE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);
82 /* On-disk file index header structure. */
83 struct got_fileindex_hdr {
84 uint32_t signature; /* big-endian */
85 #define GOT_FILE_INDEX_SIGNATURE 0x676f7449 /* 'g', 'o', 't', 'I' */
86 uint32_t version; /* big-endian */
87 #define GOT_FILE_INDEX_VERSION 1
88 uint32_t nentries; /* big-endian */
89 /* list of concatenated fileindex entries */
90 uint8_t sha1[SHA1_DIGEST_LENGTH]; /* checksum of above on-disk data */
91 };
93 uint16_t got_fileindex_perms_from_st(struct stat *);
94 mode_t got_fileindex_perms_to_st(struct got_fileindex_entry *);
96 const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *,
97 const char *, uint8_t *, uint8_t *, int);
98 const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
99 const char *, const char *, uint8_t *, uint8_t *);
100 void got_fileindex_entry_free(struct got_fileindex_entry *);
101 struct got_fileindex *got_fileindex_alloc(void);
102 void got_fileindex_free(struct got_fileindex *);
103 const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
104 const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
105 struct got_fileindex_entry *);
106 void got_fileindex_entry_remove(struct got_fileindex *,
107 struct got_fileindex_entry *);
108 struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
109 const char *);
110 const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *);
111 typedef const struct got_error *(*got_fileindex_cb)(void *,
112 struct got_fileindex_entry *);
113 const struct got_error *got_fileindex_for_each_entry_safe(
114 struct got_fileindex *, got_fileindex_cb cb, void *);
116 typedef const struct got_error *(*got_fileindex_diff_tree_old_new_cb)(void *,
117 struct got_fileindex_entry *, struct got_tree_entry *, const char *);
118 typedef const struct got_error *(*got_fileindex_diff_tree_old_cb)(void *,
119 struct got_fileindex_entry *, const char *);
120 typedef const struct got_error *(*got_fileindex_diff_tree_new_cb)(void *,
121 struct got_tree_entry *, const char *);
122 struct got_fileindex_diff_tree_cb {
123 got_fileindex_diff_tree_old_new_cb diff_old_new;
124 got_fileindex_diff_tree_old_cb diff_old;
125 got_fileindex_diff_tree_new_cb diff_new;
126 };
127 const struct got_error *got_fileindex_diff_tree(struct got_fileindex *,
128 struct got_tree_object *, const char *, const char *,
129 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
131 typedef const struct got_error *(*got_fileindex_diff_dir_old_new_cb)(void *,
132 struct got_fileindex_entry *, struct dirent *, const char *);
133 typedef const struct got_error *(*got_fileindex_diff_dir_old_cb)(void *,
134 struct got_fileindex_entry *, const char *);
135 typedef const struct got_error *(*got_fileindex_diff_dir_new_cb)(void *,
136 struct dirent *, const char *);
137 struct got_fileindex_diff_dir_cb {
138 got_fileindex_diff_dir_old_new_cb diff_old_new;
139 got_fileindex_diff_dir_old_cb diff_old;
140 got_fileindex_diff_dir_new_cb diff_new;
141 };
142 const struct got_error *got_fileindex_diff_dir(struct got_fileindex *, DIR *,
143 const char *, const char *, struct got_repository *,
144 struct got_fileindex_diff_dir_cb *, void *);
146 int got_fileindex_entry_has_blob(struct got_fileindex_entry *);
147 int got_fileindex_entry_has_commit(struct got_fileindex_entry *);
148 int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *);
150 void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *);