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