]> git.proxmox.com Git - libgit2.git/blob - src/pack.h
pack: use a cache for delta bases when unpacking
[libgit2.git] / src / pack.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #ifndef INCLUDE_pack_h__
9 #define INCLUDE_pack_h__
10
11 #include <zlib.h>
12
13 #include "git2/oid.h"
14
15 #include "common.h"
16 #include "map.h"
17 #include "mwindow.h"
18 #include "odb.h"
19 #include "oidmap.h"
20 #include "array.h"
21
22 #define GIT_PACK_FILE_MODE 0444
23
24 #define PACK_SIGNATURE 0x5041434b /* "PACK" */
25 #define PACK_VERSION 2
26 #define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3))
27 struct git_pack_header {
28 uint32_t hdr_signature;
29 uint32_t hdr_version;
30 uint32_t hdr_entries;
31 };
32
33 /*
34 * The first four bytes of index formats later than version 1 should
35 * start with this signature, as all older git binaries would find this
36 * value illegal and abort reading the file.
37 *
38 * This is the case because the number of objects in a packfile
39 * cannot exceed 1,431,660,000 as every object would need at least
40 * 3 bytes of data and the overall packfile cannot exceed 4 GiB with
41 * version 1 of the index file due to the offsets limited to 32 bits.
42 * Clearly the signature exceeds this maximum.
43 *
44 * Very old git binaries will also compare the first 4 bytes to the
45 * next 4 bytes in the index and abort with a "non-monotonic index"
46 * error if the second 4 byte word is smaller than the first 4
47 * byte word. This would be true in the proposed future index
48 * format as idx_signature would be greater than idx_version.
49 */
50
51 #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
52
53 struct git_pack_idx_header {
54 uint32_t idx_signature;
55 uint32_t idx_version;
56 };
57
58 typedef struct git_pack_cache_entry {
59 size_t last_usage; /* enough? */
60 git_atomic refcount;
61 git_rawobj raw;
62 } git_pack_cache_entry;
63
64 struct pack_chain_elem {
65 int cached;
66 git_off_t base_key;
67 /* if we don't have it cached we have this */
68 git_off_t offset;
69 size_t size;
70 git_otype type;
71 /* if cached, we have this instead */
72 git_pack_cache_entry *cached_entry;
73 };
74
75 typedef git_array_t(struct pack_chain_elem) git_dependency_chain;
76
77 #include "offmap.h"
78
79 GIT__USE_OFFMAP;
80 GIT__USE_OIDMAP;
81
82 #define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024
83 #define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */
84
85 typedef struct {
86 size_t memory_used;
87 size_t memory_limit;
88 size_t use_ctr;
89 git_mutex lock;
90 git_offmap *entries;
91 } git_pack_cache;
92
93 struct git_pack_file {
94 git_mwindow_file mwf;
95 git_map index_map;
96 git_mutex lock; /* protect updates to mwf and index_map */
97
98 uint32_t num_objects;
99 uint32_t num_bad_objects;
100 git_oid *bad_object_sha1; /* array of git_oid */
101
102 int index_version;
103 git_time_t mtime;
104 unsigned pack_local:1, pack_keep:1, has_cache:1;
105 git_oidmap *idx_cache;
106 git_oid **oids;
107
108 git_pack_cache bases; /* delta base cache */
109
110 /* something like ".git/objects/pack/xxxxx.pack" */
111 char pack_name[GIT_FLEX_ARRAY]; /* more */
112 };
113
114 struct git_pack_entry {
115 git_off_t offset;
116 git_oid sha1;
117 struct git_pack_file *p;
118 };
119
120 typedef struct git_packfile_stream {
121 git_off_t curpos;
122 int done;
123 z_stream zstream;
124 struct git_pack_file *p;
125 git_mwindow *mw;
126 } git_packfile_stream;
127
128 size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_otype type);
129
130 int git_packfile_unpack_header(
131 size_t *size_p,
132 git_otype *type_p,
133 git_mwindow_file *mwf,
134 git_mwindow **w_curs,
135 git_off_t *curpos);
136
137 int git_packfile_resolve_header(
138 size_t *size_p,
139 git_otype *type_p,
140 struct git_pack_file *p,
141 git_off_t offset);
142
143 int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, git_off_t *obj_offset);
144 int packfile_unpack_compressed(
145 git_rawobj *obj,
146 struct git_pack_file *p,
147 git_mwindow **w_curs,
148 git_off_t *curpos,
149 size_t size,
150 git_otype type);
151
152 int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos);
153 ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len);
154 void git_packfile_stream_free(git_packfile_stream *obj);
155
156 git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
157 git_off_t *curpos, git_otype type,
158 git_off_t delta_obj_offset);
159
160 void git_packfile_free(struct git_pack_file *p);
161 int git_packfile_alloc(struct git_pack_file **pack_out, const char *path);
162
163 int git_pack_entry_find(
164 struct git_pack_entry *e,
165 struct git_pack_file *p,
166 const git_oid *short_oid,
167 size_t len);
168 int git_pack_foreach_entry(
169 struct git_pack_file *p,
170 git_odb_foreach_cb cb,
171 void *data);
172
173 #endif