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