portable: sha{1,2} fixes
[got-portable.git] / lib / got_lib_fileindex.h
blobffe9361ee22cfae0355ac626b5e8b456f4408e7d
1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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.
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;
33 * On-disk size is truncated to the lower 32 bits.
34 * The value is only used to check for modifications anyway.
36 uint32_t size;
38 uint16_t mode;
39 #define GOT_FILEIDX_MODE_FILE_TYPE 0x000f
40 #define GOT_FILEIDX_MODE_FILE_TYPE_ONDISK 0x0003
41 #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED 0x000c
42 #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT 2
43 #define GOT_FILEIDX_MODE_REGULAR_FILE 1
44 #define GOT_FILEIDX_MODE_SYMLINK 2
45 #define GOT_FILEIDX_MODE_BAD_SYMLINK 3
46 #define GOT_FILEIDX_MODE_PERMS 0xfff0
47 #define GOT_FILEIDX_MODE_PERMS_SHIFT 4
50 * id of corresponding blob in repository. only the actual
51 * hash is written to the disk.
53 struct got_object_id blob;
56 * id of corresponding base commit in repository. only the
57 * actual hash is written to the disk.
59 struct got_object_id commit;
61 uint32_t flags;
64 * UNIX-style path, relative to work tree root.
65 * Variable length, and NUL-padded to a multiple of 8 on disk.
67 char *path;
70 * (since GOT_FILE_INDEX_VERSION 2)
71 * Hash of staged blob in repository if stage equals either
72 * GOT_FILEIDX_STAGE_MODIFY or GOT_FILEIDX_STAGE_ADD.
73 * Otherwise, this field is not written to disk.
74 * Only the actual hash is written to the disk.
76 struct got_object_id staged_blob;
79 /* Modifications explicitly staged for commit. */
80 #define GOT_FILEIDX_STAGE_NONE 0
81 #define GOT_FILEIDX_STAGE_MODIFY 1
82 #define GOT_FILEIDX_STAGE_ADD 2
83 #define GOT_FILEIDX_STAGE_DELETE 3
85 struct got_fileindex;
87 RB_HEAD(got_fileindex_tree, got_fileindex_entry);
89 size_t got_fileindex_entry_path_len(const struct got_fileindex_entry *);
91 static inline int
92 got_fileindex_cmp(const struct got_fileindex_entry *e1,
93 const struct got_fileindex_entry *e2)
95 return got_path_cmp(e1->path, e2->path,
96 got_fileindex_entry_path_len(e1),
97 got_fileindex_entry_path_len(e2));
100 RB_PROTOTYPE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);
102 /* On-disk file index header structure. */
103 struct got_fileindex_hdr {
104 uint32_t signature; /* big-endian */
105 #define GOT_FILE_INDEX_SIGNATURE 0x676f7449 /* 'g', 'o', 't', 'I' */
106 uint32_t version; /* big-endian */
107 #define GOT_FILE_INDEX_VERSION 3
108 uint32_t nentries; /* big-endian */
109 uint32_t algo; /* big-endian -- since v3 */
110 /* list of concatenated fileindex entries */
112 * checksum of above on-disk data, the actual length of the
113 * hash depends on the algorithm.
115 uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
118 mode_t got_fileindex_entry_perms_get(struct got_fileindex_entry *);
119 uint16_t got_fileindex_perms_from_st(struct stat *);
120 mode_t got_fileindex_perms_to_st(struct got_fileindex_entry *);
122 const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *,
123 int, const char *, struct got_object_id *, struct got_object_id *, int);
124 void got_fileindex_entry_mark_skipped(struct got_fileindex_entry *);
125 const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
126 const char *);
127 void got_fileindex_entry_free(struct got_fileindex_entry *);
129 struct got_fileindex *got_fileindex_alloc(enum got_hash_algorithm);
130 void got_fileindex_free(struct got_fileindex *);
131 const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
132 const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
133 struct got_fileindex_entry *);
134 void got_fileindex_entry_remove(struct got_fileindex *,
135 struct got_fileindex_entry *);
136 struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
137 const char *, size_t);
138 const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *,
139 enum got_hash_algorithm);
140 uint32_t got_fileindex_get_version(struct got_fileindex *);
141 typedef const struct got_error *(*got_fileindex_cb)(void *,
142 struct got_fileindex_entry *);
143 const struct got_error *got_fileindex_for_each_entry_safe(
144 struct got_fileindex *, got_fileindex_cb cb, void *);
146 typedef const struct got_error *(*got_fileindex_diff_tree_old_new_cb)(void *,
147 struct got_fileindex_entry *, struct got_tree_entry *, const char *);
148 typedef const struct got_error *(*got_fileindex_diff_tree_old_cb)(void *,
149 struct got_fileindex_entry *, const char *);
150 typedef const struct got_error *(*got_fileindex_diff_tree_new_cb)(void *,
151 struct got_tree_entry *, const char *);
152 struct got_fileindex_diff_tree_cb {
153 got_fileindex_diff_tree_old_new_cb diff_old_new;
154 got_fileindex_diff_tree_old_cb diff_old;
155 got_fileindex_diff_tree_new_cb diff_new;
157 const struct got_error *got_fileindex_diff_tree(struct got_fileindex *,
158 struct got_tree_object *, const char *, const char *,
159 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
161 typedef const struct got_error *(*got_fileindex_diff_dir_old_new_cb)(void *,
162 struct got_fileindex_entry *, struct dirent *, const char *, int);
163 typedef const struct got_error *(*got_fileindex_diff_dir_old_cb)(void *,
164 struct got_fileindex_entry *, const char *);
165 typedef const struct got_error *(*got_fileindex_diff_dir_new_cb)(int *, void *,
166 struct dirent *, const char *, int);
167 typedef const struct got_error *(*got_fileindex_diff_dir_traverse)(void *,
168 const char *, int);
169 struct got_fileindex_diff_dir_cb {
170 got_fileindex_diff_dir_old_new_cb diff_old_new;
171 got_fileindex_diff_dir_old_cb diff_old;
172 got_fileindex_diff_dir_new_cb diff_new;
173 got_fileindex_diff_dir_traverse diff_traverse;
175 const struct got_error *got_fileindex_diff_dir(struct got_fileindex *, int,
176 const char *, const char *, struct got_repository *,
177 struct got_fileindex_diff_dir_cb *, void *);
179 int got_fileindex_entry_has_blob(struct got_fileindex_entry *);
180 int got_fileindex_entry_has_commit(struct got_fileindex_entry *);
181 int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *);
182 int got_fileindex_entry_was_skipped(struct got_fileindex_entry *);
183 uint32_t got_fileindex_entry_stage_get(const struct got_fileindex_entry *);
184 void got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t);
185 int got_fileindex_entry_filetype_get(struct got_fileindex_entry *);
186 void got_fileindex_entry_filetype_set(struct got_fileindex_entry *, int);
187 void got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *, int);
188 int got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *);
190 void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *);
193 * Retrieve staged, blob or commit id from a fileindex entry, and return
194 * the given object id.
196 struct got_object_id *got_fileindex_entry_get_staged_blob_id(
197 struct got_object_id *, struct got_fileindex_entry *);
198 struct got_object_id *got_fileindex_entry_get_blob_id(struct got_object_id *,
199 struct got_fileindex_entry *);
200 struct got_object_id *got_fileindex_entry_get_commit_id(struct got_object_id *,
201 struct got_fileindex_entry *);