]>
Commit | Line | Data |
---|---|---|
c7c9e183 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
c7c9e183 | 3 | * |
bb742ede VM |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
c7c9e183 CMN |
6 | */ |
7 | ||
8 | #ifndef INCLUDE_pack_h__ | |
9 | #define INCLUDE_pack_h__ | |
10 | ||
eae0bfdc PP |
11 | #include "common.h" |
12 | ||
c7c9e183 CMN |
13 | #include "git2/oid.h" |
14 | ||
22a2d3d5 | 15 | #include "array.h" |
c7c9e183 CMN |
16 | #include "map.h" |
17 | #include "mwindow.h" | |
7d0cdf82 | 18 | #include "odb.h" |
22a2d3d5 | 19 | #include "offmap.h" |
0e040c03 | 20 | #include "oidmap.h" |
22a2d3d5 | 21 | #include "zstream.h" |
c7c9e183 | 22 | |
01ad7b3a BR |
23 | #define GIT_PACK_FILE_MODE 0444 |
24 | ||
c7c9e183 CMN |
25 | #define PACK_SIGNATURE 0x5041434b /* "PACK" */ |
26 | #define PACK_VERSION 2 | |
27 | #define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3)) | |
a070f152 | 28 | struct git_pack_header { |
c7c9e183 CMN |
29 | uint32_t hdr_signature; |
30 | uint32_t hdr_version; | |
31 | uint32_t hdr_entries; | |
32 | }; | |
33 | ||
34 | /* | |
35 | * The first four bytes of index formats later than version 1 should | |
36 | * start with this signature, as all older git binaries would find this | |
37 | * value illegal and abort reading the file. | |
38 | * | |
39 | * This is the case because the number of objects in a packfile | |
40 | * cannot exceed 1,431,660,000 as every object would need at least | |
41 | * 3 bytes of data and the overall packfile cannot exceed 4 GiB with | |
42 | * version 1 of the index file due to the offsets limited to 32 bits. | |
43 | * Clearly the signature exceeds this maximum. | |
44 | * | |
45 | * Very old git binaries will also compare the first 4 bytes to the | |
46 | * next 4 bytes in the index and abort with a "non-monotonic index" | |
47 | * error if the second 4 byte word is smaller than the first 4 | |
87d9869f | 48 | * byte word. This would be true in the proposed future index |
c7c9e183 CMN |
49 | * format as idx_signature would be greater than idx_version. |
50 | */ | |
51 | ||
52 | #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */ | |
53 | ||
a070f152 | 54 | struct git_pack_idx_header { |
c7c9e183 CMN |
55 | uint32_t idx_signature; |
56 | uint32_t idx_version; | |
57 | }; | |
58 | ||
c0f4a011 | 59 | typedef struct git_pack_cache_entry { |
0ed75620 | 60 | size_t last_usage; /* enough? */ |
525d961c | 61 | git_atomic refcount; |
c0f4a011 CMN |
62 | git_rawobj raw; |
63 | } git_pack_cache_entry; | |
64 | ||
2acdf4b8 | 65 | struct pack_chain_elem { |
22a2d3d5 UG |
66 | off64_t base_key; |
67 | off64_t offset; | |
2acdf4b8 | 68 | size_t size; |
ac3d33df | 69 | git_object_t type; |
2acdf4b8 CMN |
70 | }; |
71 | ||
72 | typedef git_array_t(struct pack_chain_elem) git_dependency_chain; | |
73 | ||
0ed75620 CMN |
74 | #define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024 |
75 | #define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */ | |
c8f79c2b CMN |
76 | |
77 | typedef struct { | |
78 | size_t memory_used; | |
79 | size_t memory_limit; | |
0ed75620 | 80 | size_t use_ctr; |
c8f79c2b CMN |
81 | git_mutex lock; |
82 | git_offmap *entries; | |
83 | } git_pack_cache; | |
84 | ||
a070f152 | 85 | struct git_pack_file { |
c7c9e183 | 86 | git_mwindow_file mwf; |
c7c9e183 | 87 | git_map index_map; |
53607868 | 88 | git_mutex lock; /* protect updates to mwf and index_map */ |
b3b66c57 | 89 | git_atomic refcount; |
c7c9e183 CMN |
90 | |
91 | uint32_t num_objects; | |
92 | uint32_t num_bad_objects; | |
93 | git_oid *bad_object_sha1; /* array of git_oid */ | |
94 | ||
95 | int index_version; | |
96 | git_time_t mtime; | |
c1af5a39 | 97 | unsigned pack_local:1, pack_keep:1, has_cache:1; |
0e040c03 | 98 | git_oidmap *idx_cache; |
60ecdf59 | 99 | git_oid **oids; |
c7c9e183 | 100 | |
c8f79c2b | 101 | git_pack_cache bases; /* delta base cache */ |
c0f4a011 | 102 | |
27051d4e ET |
103 | time_t last_freshen; /* last time the packfile was freshened */ |
104 | ||
c7c9e183 CMN |
105 | /* something like ".git/objects/pack/xxxxx.pack" */ |
106 | char pack_name[GIT_FLEX_ARRAY]; /* more */ | |
107 | }; | |
108 | ||
22a2d3d5 UG |
109 | /** |
110 | * Return the position where an OID (or a prefix) would be inserted within the | |
111 | * OID Lookup Table of an .idx file. This performs binary search between the lo | |
112 | * and hi indices. | |
113 | * | |
114 | * The stride parameter is provided because .idx files version 1 store the OIDs | |
115 | * interleaved with the 4-byte file offsets of the objects within the .pack | |
116 | * file (stride = 24), whereas files with version 2 store them in a contiguous | |
117 | * flat array (stride = 20). | |
118 | */ | |
119 | int git_pack__lookup_sha1(const void *oid_lookup_table, size_t stride, unsigned lo, | |
120 | unsigned hi, const unsigned char *oid_prefix); | |
121 | ||
a070f152 | 122 | struct git_pack_entry { |
22a2d3d5 | 123 | off64_t offset; |
c7c9e183 | 124 | git_oid sha1; |
a070f152 | 125 | struct git_pack_file *p; |
c7c9e183 CMN |
126 | }; |
127 | ||
46635339 | 128 | typedef struct git_packfile_stream { |
22a2d3d5 | 129 | off64_t curpos; |
46635339 | 130 | int done; |
22a2d3d5 | 131 | git_zstream zstream; |
46635339 CMN |
132 | struct git_pack_file *p; |
133 | git_mwindow *mw; | |
134 | } git_packfile_stream; | |
135 | ||
ac3d33df | 136 | size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_object_t type); |
51e82492 | 137 | |
b3b66c57 CMN |
138 | int git_packfile__name(char **out, const char *path); |
139 | ||
7d0cdf82 CMN |
140 | int git_packfile_unpack_header( |
141 | size_t *size_p, | |
ac3d33df | 142 | git_object_t *type_p, |
7d0cdf82 CMN |
143 | git_mwindow_file *mwf, |
144 | git_mwindow **w_curs, | |
22a2d3d5 | 145 | off64_t *curpos); |
7d0cdf82 | 146 | |
44f9f547 DMB |
147 | int git_packfile_resolve_header( |
148 | size_t *size_p, | |
ac3d33df | 149 | git_object_t *type_p, |
44f9f547 | 150 | struct git_pack_file *p, |
22a2d3d5 | 151 | off64_t offset); |
44f9f547 | 152 | |
22a2d3d5 | 153 | int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off64_t *obj_offset); |
7d0cdf82 | 154 | |
22a2d3d5 | 155 | int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos); |
46635339 | 156 | ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len); |
ac3d33df | 157 | void git_packfile_stream_dispose(git_packfile_stream *obj); |
46635339 | 158 | |
22a2d3d5 UG |
159 | int get_delta_base( |
160 | off64_t *delta_base_out, | |
161 | struct git_pack_file *p, | |
162 | git_mwindow **w_curs, | |
163 | off64_t *curpos, | |
164 | git_object_t type, | |
165 | off64_t delta_obj_offset); | |
7d0cdf82 | 166 | |
bf339ab0 | 167 | void git_packfile_close(struct git_pack_file *p, bool unlink_packfile); |
96c9b9f0 | 168 | void git_packfile_free(struct git_pack_file *p); |
5d2d21e5 RB |
169 | int git_packfile_alloc(struct git_pack_file **pack_out, const char *path); |
170 | ||
a070f152 CMN |
171 | int git_pack_entry_find( |
172 | struct git_pack_entry *e, | |
173 | struct git_pack_file *p, | |
174 | const git_oid *short_oid, | |
b8457baa | 175 | size_t len); |
521aedad CMN |
176 | int git_pack_foreach_entry( |
177 | struct git_pack_file *p, | |
c3fb7d04 | 178 | git_odb_foreach_cb cb, |
521aedad | 179 | void *data); |
a070f152 | 180 | |
c7c9e183 | 181 | #endif |