Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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 /* An open pack file. */
18 struct got_pack {
19 char *path_packfile;
20 int fd;
21 uint8_t *map;
22 size_t filesize;
23 struct got_privsep_child *privsep_child;
24 struct got_delta_cache *delta_cache;
25 };
27 struct got_packidx;
29 const struct got_error *got_pack_close(struct got_pack *);
31 const struct got_error *got_pack_parse_offset_delta(off_t *, size_t *,
32 struct got_pack *, off_t, int);
33 const struct got_error *got_pack_parse_ref_delta(struct got_object_id *,
34 struct got_pack *, off_t, int);
35 const struct got_error *got_pack_resolve_delta_chain(struct got_delta_chain *,
36 struct got_packidx *, struct got_pack *, off_t, size_t, int, size_t,
37 unsigned int);
38 const struct got_error *got_pack_parse_object_type_and_size(uint8_t *,
39 uint64_t *, size_t *, struct got_pack *, off_t);
41 #define GOT_PACK_PREFIX "pack-"
42 #define GOT_PACKFILE_SUFFIX ".pack"
43 #define GOT_PACKIDX_SUFFIX ".idx"
44 #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
45 SHA1_DIGEST_STRING_LENGTH - 1 + \
46 strlen(GOT_PACKFILE_SUFFIX))
47 #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
48 SHA1_DIGEST_STRING_LENGTH - 1 + \
49 strlen(GOT_PACKIDX_SUFFIX))
51 /* See Documentation/technical/pack-format.txt in Git. */
53 struct got_packidx_trailer {
54 u_int8_t packfile_sha1[SHA1_DIGEST_LENGTH];
55 u_int8_t packidx_sha1[SHA1_DIGEST_LENGTH];
56 } __attribute__((__packed__));
58 struct got_packidx_object_id {
59 u_int8_t sha1[SHA1_DIGEST_LENGTH];
60 } __attribute__((__packed__));
62 /* Ignore pack index version 1 which is no longer written by Git. */
63 #define GOT_PACKIDX_VERSION 2
65 struct got_packidx_v2_hdr {
66 uint32_t *magic; /* big endian */
67 #define GOT_PACKIDX_V2_MAGIC 0xff744f63 /* "\377t0c" */
68 uint32_t *version;
70 /*
71 * Each entry N in the fanout table contains the number of objects in
72 * the packfile whose SHA1 begins with a byte less than or equal to N.
73 * The last entry (index 255) contains the number of objects in the
74 * pack file whose first SHA1 byte is <= 0xff, and thus records the
75 * total number of objects in the pack file. All pointer variables
76 * below point to tables with a corresponding number of entries.
77 */
78 uint32_t *fanout_table; /* values are big endian */
79 #define GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS (0xff + 1)
81 /* Sorted SHA1 checksums for each object in the pack file. */
82 struct got_packidx_object_id *sorted_ids;
84 /* CRC32 of the packed representation of each object. */
85 uint32_t *crc32;
87 /* Offset into the pack file for each object. */
88 uint32_t *offsets; /* values are big endian */
89 #define GOT_PACKIDX_OFFSET_VAL_MASK 0x7fffffff
90 #define GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX 0x80000000
92 /* Large offsets table is empty for pack files < 2 GB. */
93 uint64_t *large_offsets; /* values are big endian */
95 struct got_packidx_trailer *trailer;
96 };
98 /* An open pack index file. */
99 struct got_packidx {
100 char *path_packidx; /* actual on-disk path */
101 int fd;
102 uint8_t *map;
103 size_t len;
104 size_t nlargeobj;
105 struct got_packidx_v2_hdr hdr; /* convenient pointers into map */
106 };
108 struct got_packfile_hdr {
109 uint32_t signature;
110 #define GOT_PACKFILE_SIGNATURE 0x5041434b /* 'P' 'A' 'C' 'K' */
111 uint32_t version; /* big endian */
112 #define GOT_PACKFILE_VERSION 2
113 uint32_t nobjects; /* big endian */
114 };
116 struct got_packfile_obj_hdr {
117 /*
118 * The object size field uses a variable length encoding:
119 * size0...sizeN form a 4+7+7+...+7 bit integer, where size0 is the
120 * least significant part and sizeN is the most significant part.
121 * If the MSB of a size byte is set, an additional size byte follows.
122 * Of the 7 remaining bits of size0, the first 3 bits indicate the
123 * object's type, and the remaining 4 bits contribute to the size.
124 */
125 uint8_t *size; /* variable length */
126 #define GOT_PACK_OBJ_SIZE_MORE 0x80
127 #define GOT_PACK_OBJ_SIZE0_TYPE_MASK 0x70 /* See struct got_object->type */
128 #define GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT 4
129 #define GOT_PACK_OBJ_SIZE0_VAL_MASK 0x0f
130 #define GOT_PACK_OBJ_SIZE_VAL_MASK 0x7f
131 };
133 /* If object is not a DELTA type. */
134 struct got_packfile_object_data {
135 uint8_t *data; /* compressed */
136 };
138 /* If object is of type GOT_OBJ_TYPE_REF_DELTA. */
139 struct got_packfile_object_data_ref_delta {
140 uint8_t sha1[SHA1_DIGEST_LENGTH];
141 uint8_t *delta_data; /* compressed */
142 };
144 /* If object is of type GOT_OBJ_TYPE_OFFSET_DELTA. */
145 struct got_packfile_object_data_offset_delta {
146 /*
147 * This offset is interpreted as a negative offset from
148 * the got_packfile_obj_hdr corresponding to this object.
149 * The size provided in the header specifies the amount
150 * of compressed delta data that follows.
152 * This field uses a variable length encoding of N bytes,
153 * where the MSB is always set except for the last byte.
154 * The value is encoded as a series of N 7 bit integers,
155 * which are concatenated, and if N > 1 the value 2^7 +
156 * 2^14 + ... + 2^(7 * (n-1)) is added to the result.
157 */
158 uint8_t *offset; /* variable length */
159 #define GOT_PACK_OBJ_DELTA_OFF_MORE 0x80
160 #define GOT_PACK_OBJ_DELTA_OFF_VAL_MASK 0x7f
161 uint8_t *delta_data; /* compressed */
162 };
164 struct got_packfile_obj_data {
165 union {
166 struct got_packfile_object_data data;
167 struct got_packfile_object_data_ref_delta ref_delta;
168 struct got_packfile_object_data_offset_delta offset_delta;
169 } __attribute__((__packed__));
170 } __attribute__((__packed__));
172 const struct got_error *got_packidx_init_hdr(struct got_packidx *, int, off_t);
173 const struct got_error *got_packidx_open(struct got_packidx **,
174 int, const char *, int);
175 const struct got_error *got_packidx_close(struct got_packidx *);
176 const struct got_error *got_packidx_get_packfile_path(char **, const char *);
177 off_t got_packidx_get_object_offset(struct got_packidx *, int idx);
178 int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *);
179 const struct got_error *got_packidx_match_id_str_prefix(
180 struct got_object_id_queue *, struct got_packidx *, const char *);
182 const struct got_error *got_packfile_open_object(struct got_object **,
183 struct got_pack *, struct got_packidx *, int, struct got_object_id *);
184 const struct got_error *got_pack_get_delta_chain_max_size(uint64_t *,
185 struct got_delta_chain *, struct got_pack *);
186 const struct got_error *got_pack_get_max_delta_object_size(uint64_t *,
187 struct got_object *, struct got_pack *);
188 const struct got_error *got_pack_dump_delta_chain_to_file(size_t *,
189 struct got_delta_chain *, struct got_pack *, FILE *, FILE *, FILE *);
190 const struct got_error *got_pack_dump_delta_chain_to_mem(uint8_t **, size_t *,
191 struct got_delta_chain *, struct got_pack *);
192 const struct got_error *got_packfile_extract_object(struct got_pack *,
193 struct got_object *, FILE *, FILE *, FILE *);
194 const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
195 struct got_object *, struct got_pack *);
196 struct got_pack *got_repo_get_cached_pack(struct got_repository *, const char *);