line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
#include "indexer.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "git2/indexer.h" |
11
|
|
|
|
|
|
|
#include "git2/object.h" |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#include "commit.h" |
14
|
|
|
|
|
|
|
#include "tree.h" |
15
|
|
|
|
|
|
|
#include "tag.h" |
16
|
|
|
|
|
|
|
#include "pack.h" |
17
|
|
|
|
|
|
|
#include "mwindow.h" |
18
|
|
|
|
|
|
|
#include "posix.h" |
19
|
|
|
|
|
|
|
#include "pack.h" |
20
|
|
|
|
|
|
|
#include "filebuf.h" |
21
|
|
|
|
|
|
|
#include "oid.h" |
22
|
|
|
|
|
|
|
#include "oidarray.h" |
23
|
|
|
|
|
|
|
#include "oidmap.h" |
24
|
|
|
|
|
|
|
#include "zstream.h" |
25
|
|
|
|
|
|
|
#include "object.h" |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
extern git_mutex git__mwindow_mutex; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
size_t git_indexer__max_objects = UINT32_MAX; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#define UINT31_MAX (0x7FFFFFFF) |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
struct entry { |
34
|
|
|
|
|
|
|
git_oid oid; |
35
|
|
|
|
|
|
|
uint32_t crc; |
36
|
|
|
|
|
|
|
uint32_t offset; |
37
|
|
|
|
|
|
|
uint64_t offset_long; |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
struct git_indexer { |
41
|
|
|
|
|
|
|
unsigned int parsed_header :1, |
42
|
|
|
|
|
|
|
pack_committed :1, |
43
|
|
|
|
|
|
|
have_stream :1, |
44
|
|
|
|
|
|
|
have_delta :1, |
45
|
|
|
|
|
|
|
do_fsync :1, |
46
|
|
|
|
|
|
|
do_verify :1; |
47
|
|
|
|
|
|
|
struct git_pack_header hdr; |
48
|
|
|
|
|
|
|
struct git_pack_file *pack; |
49
|
|
|
|
|
|
|
unsigned int mode; |
50
|
|
|
|
|
|
|
off64_t off; |
51
|
|
|
|
|
|
|
off64_t entry_start; |
52
|
|
|
|
|
|
|
git_object_t entry_type; |
53
|
|
|
|
|
|
|
git_buf entry_data; |
54
|
|
|
|
|
|
|
git_packfile_stream stream; |
55
|
|
|
|
|
|
|
size_t nr_objects; |
56
|
|
|
|
|
|
|
git_vector objects; |
57
|
|
|
|
|
|
|
git_vector deltas; |
58
|
|
|
|
|
|
|
unsigned int fanout[256]; |
59
|
|
|
|
|
|
|
git_hash_ctx hash_ctx; |
60
|
|
|
|
|
|
|
git_oid hash; |
61
|
|
|
|
|
|
|
git_indexer_progress_cb progress_cb; |
62
|
|
|
|
|
|
|
void *progress_payload; |
63
|
|
|
|
|
|
|
char objbuf[8*1024]; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
/* OIDs referenced from pack objects. Used for verification. */ |
66
|
|
|
|
|
|
|
git_oidmap *expected_oids; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
/* Needed to look up objects which we want to inject to fix a thin pack */ |
69
|
|
|
|
|
|
|
git_odb *odb; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
/* Fields for calculating the packfile trailer (hash of everything before it) */ |
72
|
|
|
|
|
|
|
char inbuf[GIT_OID_RAWSZ]; |
73
|
|
|
|
|
|
|
size_t inbuf_len; |
74
|
|
|
|
|
|
|
git_hash_ctx trailer; |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
struct delta_info { |
78
|
|
|
|
|
|
|
off64_t delta_off; |
79
|
|
|
|
|
|
|
}; |
80
|
|
|
|
|
|
|
|
81
|
5
|
|
|
|
|
|
const git_oid *git_indexer_hash(const git_indexer *idx) |
82
|
|
|
|
|
|
|
{ |
83
|
5
|
|
|
|
|
|
return &idx->hash; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
5
|
|
|
|
|
|
static int parse_header(struct git_pack_header *hdr, struct git_pack_file *pack) |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
int error; |
89
|
|
|
|
|
|
|
git_map map; |
90
|
|
|
|
|
|
|
|
91
|
5
|
50
|
|
|
|
|
if ((error = p_mmap(&map, sizeof(*hdr), GIT_PROT_READ, GIT_MAP_SHARED, pack->mwf.fd, 0)) < 0) |
92
|
0
|
|
|
|
|
|
return error; |
93
|
|
|
|
|
|
|
|
94
|
5
|
|
|
|
|
|
memcpy(hdr, map.data, sizeof(*hdr)); |
95
|
5
|
|
|
|
|
|
p_munmap(&map); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
/* Verify we recognize this pack file format. */ |
98
|
5
|
50
|
|
|
|
|
if (hdr->hdr_signature != ntohl(PACK_SIGNATURE)) { |
99
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "wrong pack signature"); |
100
|
0
|
|
|
|
|
|
return -1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
5
|
50
|
|
|
|
|
if (!pack_version_ok(hdr->hdr_version)) { |
|
|
0
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "wrong pack version"); |
105
|
0
|
|
|
|
|
|
return -1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
5
|
|
|
|
|
|
return 0; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
160
|
|
|
|
|
|
static int objects_cmp(const void *a, const void *b) |
112
|
|
|
|
|
|
|
{ |
113
|
160
|
|
|
|
|
|
const struct entry *entrya = a; |
114
|
160
|
|
|
|
|
|
const struct entry *entryb = b; |
115
|
|
|
|
|
|
|
|
116
|
160
|
|
|
|
|
|
return git_oid__cmp(&entrya->oid, &entryb->oid); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
int git_indexer_options_init(git_indexer_options *opts, unsigned int version) |
120
|
|
|
|
|
|
|
{ |
121
|
0
|
0
|
|
|
|
|
GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
122
|
|
|
|
|
|
|
opts, version, git_indexer_options, GIT_INDEXER_OPTIONS_INIT); |
123
|
0
|
|
|
|
|
|
return 0; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#ifndef GIT_DEPRECATE_HARD |
127
|
0
|
|
|
|
|
|
int git_indexer_init_options(git_indexer_options *opts, unsigned int version) |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
|
|
|
return git_indexer_options_init(opts, version); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
#endif |
132
|
|
|
|
|
|
|
|
133
|
5
|
|
|
|
|
|
int git_indexer_new( |
134
|
|
|
|
|
|
|
git_indexer **out, |
135
|
|
|
|
|
|
|
const char *prefix, |
136
|
|
|
|
|
|
|
unsigned int mode, |
137
|
|
|
|
|
|
|
git_odb *odb, |
138
|
|
|
|
|
|
|
git_indexer_options *in_opts) |
139
|
|
|
|
|
|
|
{ |
140
|
5
|
|
|
|
|
|
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; |
141
|
|
|
|
|
|
|
git_indexer *idx; |
142
|
5
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT; |
143
|
|
|
|
|
|
|
static const char suff[] = "/pack"; |
144
|
5
|
|
|
|
|
|
int error, fd = -1; |
145
|
|
|
|
|
|
|
|
146
|
5
|
100
|
|
|
|
|
if (in_opts) |
147
|
4
|
|
|
|
|
|
memcpy(&opts, in_opts, sizeof(opts)); |
148
|
|
|
|
|
|
|
|
149
|
5
|
|
|
|
|
|
idx = git__calloc(1, sizeof(git_indexer)); |
150
|
5
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(idx); |
151
|
5
|
|
|
|
|
|
idx->odb = odb; |
152
|
5
|
|
|
|
|
|
idx->progress_cb = opts.progress_cb; |
153
|
5
|
|
|
|
|
|
idx->progress_payload = opts.progress_cb_payload; |
154
|
5
|
50
|
|
|
|
|
idx->mode = mode ? mode : GIT_PACK_FILE_MODE; |
155
|
5
|
|
|
|
|
|
git_buf_init(&idx->entry_data, 0); |
156
|
|
|
|
|
|
|
|
157
|
5
|
50
|
|
|
|
|
if ((error = git_hash_ctx_init(&idx->hash_ctx)) < 0 || |
|
|
50
|
|
|
|
|
|
158
|
5
|
50
|
|
|
|
|
(error = git_hash_ctx_init(&idx->trailer)) < 0 || |
159
|
5
|
|
|
|
|
|
(error = git_oidmap_new(&idx->expected_oids)) < 0) |
160
|
|
|
|
|
|
|
goto cleanup; |
161
|
|
|
|
|
|
|
|
162
|
5
|
|
|
|
|
|
idx->do_verify = opts.verify; |
163
|
|
|
|
|
|
|
|
164
|
5
|
50
|
|
|
|
|
if (git_repository__fsync_gitdir) |
165
|
0
|
|
|
|
|
|
idx->do_fsync = 1; |
166
|
|
|
|
|
|
|
|
167
|
5
|
|
|
|
|
|
error = git_buf_joinpath(&path, prefix, suff); |
168
|
5
|
50
|
|
|
|
|
if (error < 0) |
169
|
0
|
|
|
|
|
|
goto cleanup; |
170
|
|
|
|
|
|
|
|
171
|
5
|
|
|
|
|
|
fd = git_futils_mktmp(&tmp_path, git_buf_cstr(&path), idx->mode); |
172
|
5
|
|
|
|
|
|
git_buf_dispose(&path); |
173
|
5
|
50
|
|
|
|
|
if (fd < 0) |
174
|
0
|
|
|
|
|
|
goto cleanup; |
175
|
|
|
|
|
|
|
|
176
|
5
|
|
|
|
|
|
error = git_packfile_alloc(&idx->pack, git_buf_cstr(&tmp_path)); |
177
|
5
|
|
|
|
|
|
git_buf_dispose(&tmp_path); |
178
|
|
|
|
|
|
|
|
179
|
5
|
50
|
|
|
|
|
if (error < 0) |
180
|
0
|
|
|
|
|
|
goto cleanup; |
181
|
|
|
|
|
|
|
|
182
|
5
|
|
|
|
|
|
idx->pack->mwf.fd = fd; |
183
|
5
|
50
|
|
|
|
|
if ((error = git_mwindow_file_register(&idx->pack->mwf)) < 0) |
184
|
0
|
|
|
|
|
|
goto cleanup; |
185
|
|
|
|
|
|
|
|
186
|
5
|
|
|
|
|
|
*out = idx; |
187
|
5
|
|
|
|
|
|
return 0; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
cleanup: |
190
|
0
|
0
|
|
|
|
|
if (fd != -1) |
191
|
0
|
|
|
|
|
|
p_close(fd); |
192
|
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
if (git_buf_len(&tmp_path) > 0) |
194
|
0
|
|
|
|
|
|
p_unlink(git_buf_cstr(&tmp_path)); |
195
|
|
|
|
|
|
|
|
196
|
0
|
0
|
|
|
|
|
if (idx->pack != NULL) |
197
|
0
|
|
|
|
|
|
p_unlink(idx->pack->pack_name); |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
git_buf_dispose(&path); |
200
|
0
|
|
|
|
|
|
git_buf_dispose(&tmp_path); |
201
|
0
|
|
|
|
|
|
git__free(idx); |
202
|
5
|
|
|
|
|
|
return -1; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
void git_indexer__set_fsync(git_indexer *idx, int do_fsync) |
206
|
|
|
|
|
|
|
{ |
207
|
0
|
|
|
|
|
|
idx->do_fsync = !!do_fsync; |
208
|
0
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
/* Try to store the delta so we can try to resolve it later */ |
211
|
2
|
|
|
|
|
|
static int store_delta(git_indexer *idx) |
212
|
|
|
|
|
|
|
{ |
213
|
|
|
|
|
|
|
struct delta_info *delta; |
214
|
|
|
|
|
|
|
|
215
|
2
|
|
|
|
|
|
delta = git__calloc(1, sizeof(struct delta_info)); |
216
|
2
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(delta); |
217
|
2
|
|
|
|
|
|
delta->delta_off = idx->entry_start; |
218
|
|
|
|
|
|
|
|
219
|
2
|
50
|
|
|
|
|
if (git_vector_insert(&idx->deltas, delta) < 0) |
220
|
0
|
|
|
|
|
|
return -1; |
221
|
|
|
|
|
|
|
|
222
|
2
|
|
|
|
|
|
return 0; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
43
|
|
|
|
|
|
static int hash_header(git_hash_ctx *ctx, off64_t len, git_object_t type) |
226
|
|
|
|
|
|
|
{ |
227
|
|
|
|
|
|
|
char buffer[64]; |
228
|
|
|
|
|
|
|
size_t hdrlen; |
229
|
|
|
|
|
|
|
int error; |
230
|
|
|
|
|
|
|
|
231
|
43
|
50
|
|
|
|
|
if ((error = git_odb__format_object_header(&hdrlen, |
232
|
|
|
|
|
|
|
buffer, sizeof(buffer), (size_t)len, type)) < 0) |
233
|
0
|
|
|
|
|
|
return error; |
234
|
|
|
|
|
|
|
|
235
|
43
|
|
|
|
|
|
return git_hash_update(ctx, buffer, hdrlen); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
43
|
|
|
|
|
|
static int hash_object_stream(git_indexer*idx, git_packfile_stream *stream) |
239
|
|
|
|
|
|
|
{ |
240
|
|
|
|
|
|
|
ssize_t read; |
241
|
|
|
|
|
|
|
|
242
|
43
|
50
|
|
|
|
|
assert(idx && stream); |
|
|
50
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
do { |
245
|
86
|
50
|
|
|
|
|
if ((read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf))) < 0) |
246
|
0
|
|
|
|
|
|
break; |
247
|
|
|
|
|
|
|
|
248
|
86
|
50
|
|
|
|
|
if (idx->do_verify) |
249
|
0
|
|
|
|
|
|
git_buf_put(&idx->entry_data, idx->objbuf, read); |
250
|
|
|
|
|
|
|
|
251
|
86
|
|
|
|
|
|
git_hash_update(&idx->hash_ctx, idx->objbuf, read); |
252
|
86
|
100
|
|
|
|
|
} while (read > 0); |
253
|
|
|
|
|
|
|
|
254
|
43
|
50
|
|
|
|
|
if (read < 0) |
255
|
0
|
|
|
|
|
|
return (int)read; |
256
|
|
|
|
|
|
|
|
257
|
43
|
|
|
|
|
|
return 0; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
/* In order to create the packfile stream, we need to skip over the delta base description */ |
261
|
2
|
|
|
|
|
|
static int advance_delta_offset(git_indexer *idx, git_object_t type) |
262
|
|
|
|
|
|
|
{ |
263
|
2
|
|
|
|
|
|
git_mwindow *w = NULL; |
264
|
|
|
|
|
|
|
|
265
|
2
|
50
|
|
|
|
|
assert(type == GIT_OBJECT_REF_DELTA || type == GIT_OBJECT_OFS_DELTA); |
|
|
0
|
|
|
|
|
|
266
|
|
|
|
|
|
|
|
267
|
2
|
50
|
|
|
|
|
if (type == GIT_OBJECT_REF_DELTA) { |
268
|
2
|
|
|
|
|
|
idx->off += GIT_OID_RAWSZ; |
269
|
|
|
|
|
|
|
} else { |
270
|
|
|
|
|
|
|
off64_t base_off; |
271
|
0
|
|
|
|
|
|
int error = get_delta_base(&base_off, idx->pack, &w, &idx->off, type, idx->entry_start); |
272
|
0
|
|
|
|
|
|
git_mwindow_close(&w); |
273
|
0
|
0
|
|
|
|
|
if (error < 0) |
274
|
0
|
|
|
|
|
|
return error; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
2
|
|
|
|
|
|
return 0; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
/* Read from the stream and discard any output */ |
281
|
4
|
|
|
|
|
|
static int read_object_stream(git_indexer *idx, git_packfile_stream *stream) |
282
|
|
|
|
|
|
|
{ |
283
|
|
|
|
|
|
|
ssize_t read; |
284
|
|
|
|
|
|
|
|
285
|
4
|
50
|
|
|
|
|
assert(stream); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
do { |
288
|
6
|
|
|
|
|
|
read = git_packfile_stream_read(stream, idx->objbuf, sizeof(idx->objbuf)); |
289
|
6
|
100
|
|
|
|
|
} while (read > 0); |
290
|
|
|
|
|
|
|
|
291
|
4
|
100
|
|
|
|
|
if (read < 0) |
292
|
2
|
|
|
|
|
|
return (int)read; |
293
|
|
|
|
|
|
|
|
294
|
2
|
|
|
|
|
|
return 0; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
45
|
|
|
|
|
|
static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, off64_t start, off64_t size) |
298
|
|
|
|
|
|
|
{ |
299
|
|
|
|
|
|
|
void *ptr; |
300
|
|
|
|
|
|
|
uint32_t crc; |
301
|
|
|
|
|
|
|
unsigned int left, len; |
302
|
45
|
|
|
|
|
|
git_mwindow *w = NULL; |
303
|
|
|
|
|
|
|
|
304
|
45
|
|
|
|
|
|
crc = crc32(0L, Z_NULL, 0); |
305
|
90
|
100
|
|
|
|
|
while (size) { |
306
|
45
|
|
|
|
|
|
ptr = git_mwindow_open(mwf, &w, start, (size_t)size, &left); |
307
|
45
|
50
|
|
|
|
|
if (ptr == NULL) |
308
|
0
|
|
|
|
|
|
return -1; |
309
|
|
|
|
|
|
|
|
310
|
45
|
|
|
|
|
|
len = min(left, (unsigned int)size); |
311
|
45
|
|
|
|
|
|
crc = crc32(crc, ptr, len); |
312
|
45
|
|
|
|
|
|
size -= len; |
313
|
45
|
|
|
|
|
|
start += len; |
314
|
45
|
|
|
|
|
|
git_mwindow_close(&w); |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
45
|
|
|
|
|
|
*crc_out = htonl(crc); |
318
|
45
|
|
|
|
|
|
return 0; |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
0
|
|
|
|
|
|
static int add_expected_oid(git_indexer *idx, const git_oid *oid) |
322
|
|
|
|
|
|
|
{ |
323
|
|
|
|
|
|
|
/* |
324
|
|
|
|
|
|
|
* If we know about that object because it is stored in our ODB or |
325
|
|
|
|
|
|
|
* because we have already processed it as part of our pack file, we do |
326
|
|
|
|
|
|
|
* not have to expect it. |
327
|
|
|
|
|
|
|
*/ |
328
|
0
|
0
|
|
|
|
|
if ((!idx->odb || !git_odb_exists(idx->odb, oid)) && |
329
|
0
|
0
|
|
|
|
|
!git_oidmap_exists(idx->pack->idx_cache, oid) && |
330
|
0
|
|
|
|
|
|
!git_oidmap_exists(idx->expected_oids, oid)) { |
331
|
0
|
|
|
|
|
|
git_oid *dup = git__malloc(sizeof(*oid)); |
332
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(dup); |
333
|
0
|
|
|
|
|
|
git_oid_cpy(dup, oid); |
334
|
0
|
|
|
|
|
|
return git_oidmap_set(idx->expected_oids, dup, dup); |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
0
|
|
|
|
|
|
return 0; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
0
|
|
|
|
|
|
static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj) |
341
|
|
|
|
|
|
|
{ |
342
|
|
|
|
|
|
|
git_object *object; |
343
|
|
|
|
|
|
|
git_oid *expected; |
344
|
|
|
|
|
|
|
int error; |
345
|
|
|
|
|
|
|
|
346
|
0
|
0
|
|
|
|
|
if (obj->type != GIT_OBJECT_BLOB && |
|
|
0
|
|
|
|
|
|
347
|
0
|
0
|
|
|
|
|
obj->type != GIT_OBJECT_TREE && |
348
|
0
|
0
|
|
|
|
|
obj->type != GIT_OBJECT_COMMIT && |
349
|
0
|
|
|
|
|
|
obj->type != GIT_OBJECT_TAG) |
350
|
0
|
|
|
|
|
|
return 0; |
351
|
|
|
|
|
|
|
|
352
|
0
|
0
|
|
|
|
|
if ((error = git_object__from_raw(&object, obj->data, obj->len, obj->type)) < 0) |
353
|
0
|
|
|
|
|
|
goto out; |
354
|
|
|
|
|
|
|
|
355
|
0
|
0
|
|
|
|
|
if ((expected = git_oidmap_get(idx->expected_oids, &object->cached.oid)) != NULL) { |
356
|
0
|
|
|
|
|
|
git_oidmap_delete(idx->expected_oids, &object->cached.oid); |
357
|
0
|
|
|
|
|
|
git__free(expected); |
358
|
|
|
|
|
|
|
} |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
/* |
361
|
|
|
|
|
|
|
* Check whether this is a known object. If so, we can just continue as |
362
|
|
|
|
|
|
|
* we assume that the ODB has a complete graph. |
363
|
|
|
|
|
|
|
*/ |
364
|
0
|
0
|
|
|
|
|
if (idx->odb && git_odb_exists(idx->odb, &object->cached.oid)) |
|
|
0
|
|
|
|
|
|
365
|
0
|
|
|
|
|
|
return 0; |
366
|
|
|
|
|
|
|
|
367
|
0
|
|
|
|
|
|
switch (obj->type) { |
368
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
369
|
|
|
|
|
|
|
{ |
370
|
0
|
|
|
|
|
|
git_tree *tree = (git_tree *) object; |
371
|
|
|
|
|
|
|
git_tree_entry *entry; |
372
|
|
|
|
|
|
|
size_t i; |
373
|
|
|
|
|
|
|
|
374
|
0
|
0
|
|
|
|
|
git_array_foreach(tree->entries, i, entry) |
|
|
0
|
|
|
|
|
|
375
|
0
|
0
|
|
|
|
|
if (add_expected_oid(idx, entry->oid) < 0) |
376
|
0
|
|
|
|
|
|
goto out; |
377
|
|
|
|
|
|
|
|
378
|
0
|
|
|
|
|
|
break; |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
case GIT_OBJECT_COMMIT: |
381
|
|
|
|
|
|
|
{ |
382
|
0
|
|
|
|
|
|
git_commit *commit = (git_commit *) object; |
383
|
|
|
|
|
|
|
git_oid *parent_oid; |
384
|
|
|
|
|
|
|
size_t i; |
385
|
|
|
|
|
|
|
|
386
|
0
|
0
|
|
|
|
|
git_array_foreach(commit->parent_ids, i, parent_oid) |
|
|
0
|
|
|
|
|
|
387
|
0
|
0
|
|
|
|
|
if (add_expected_oid(idx, parent_oid) < 0) |
388
|
0
|
|
|
|
|
|
goto out; |
389
|
|
|
|
|
|
|
|
390
|
0
|
0
|
|
|
|
|
if (add_expected_oid(idx, &commit->tree_id) < 0) |
391
|
0
|
|
|
|
|
|
goto out; |
392
|
|
|
|
|
|
|
|
393
|
0
|
|
|
|
|
|
break; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
case GIT_OBJECT_TAG: |
396
|
|
|
|
|
|
|
{ |
397
|
0
|
|
|
|
|
|
git_tag *tag = (git_tag *) object; |
398
|
|
|
|
|
|
|
|
399
|
0
|
0
|
|
|
|
|
if (add_expected_oid(idx, &tag->target) < 0) |
400
|
0
|
|
|
|
|
|
goto out; |
401
|
|
|
|
|
|
|
|
402
|
0
|
|
|
|
|
|
break; |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
405
|
|
|
|
|
|
|
default: |
406
|
0
|
|
|
|
|
|
break; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
out: |
410
|
0
|
|
|
|
|
|
git_object_free(object); |
411
|
|
|
|
|
|
|
|
412
|
0
|
|
|
|
|
|
return error; |
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
|
415
|
43
|
|
|
|
|
|
static int store_object(git_indexer *idx) |
416
|
|
|
|
|
|
|
{ |
417
|
|
|
|
|
|
|
int i, error; |
418
|
|
|
|
|
|
|
git_oid oid; |
419
|
|
|
|
|
|
|
struct entry *entry; |
420
|
|
|
|
|
|
|
off64_t entry_size; |
421
|
|
|
|
|
|
|
struct git_pack_entry *pentry; |
422
|
43
|
|
|
|
|
|
off64_t entry_start = idx->entry_start; |
423
|
|
|
|
|
|
|
|
424
|
43
|
|
|
|
|
|
entry = git__calloc(1, sizeof(*entry)); |
425
|
43
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(entry); |
426
|
|
|
|
|
|
|
|
427
|
43
|
|
|
|
|
|
pentry = git__calloc(1, sizeof(struct git_pack_entry)); |
428
|
43
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(pentry); |
429
|
|
|
|
|
|
|
|
430
|
43
|
50
|
|
|
|
|
if (git_hash_final(&oid, &idx->hash_ctx)) { |
431
|
0
|
|
|
|
|
|
git__free(pentry); |
432
|
0
|
|
|
|
|
|
goto on_error; |
433
|
|
|
|
|
|
|
} |
434
|
43
|
|
|
|
|
|
entry_size = idx->off - entry_start; |
435
|
43
|
50
|
|
|
|
|
if (entry_start > UINT31_MAX) { |
436
|
0
|
|
|
|
|
|
entry->offset = UINT32_MAX; |
437
|
0
|
|
|
|
|
|
entry->offset_long = entry_start; |
438
|
|
|
|
|
|
|
} else { |
439
|
43
|
|
|
|
|
|
entry->offset = (uint32_t)entry_start; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
43
|
50
|
|
|
|
|
if (idx->do_verify) { |
443
|
0
|
|
|
|
|
|
git_rawobj rawobj = { |
444
|
0
|
|
|
|
|
|
idx->entry_data.ptr, |
445
|
0
|
|
|
|
|
|
idx->entry_data.size, |
446
|
0
|
|
|
|
|
|
idx->entry_type |
447
|
|
|
|
|
|
|
}; |
448
|
|
|
|
|
|
|
|
449
|
0
|
0
|
|
|
|
|
if ((error = check_object_connectivity(idx, &rawobj)) < 0) |
450
|
0
|
|
|
|
|
|
goto on_error; |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
43
|
|
|
|
|
|
git_oid_cpy(&pentry->sha1, &oid); |
454
|
43
|
|
|
|
|
|
pentry->offset = entry_start; |
455
|
|
|
|
|
|
|
|
456
|
43
|
50
|
|
|
|
|
if (git_oidmap_exists(idx->pack->idx_cache, &pentry->sha1)) { |
457
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "duplicate object %s found in pack", git_oid_tostr_s(&pentry->sha1)); |
458
|
0
|
|
|
|
|
|
git__free(pentry); |
459
|
0
|
|
|
|
|
|
goto on_error; |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
|
462
|
43
|
50
|
|
|
|
|
if ((error = git_oidmap_set(idx->pack->idx_cache, &pentry->sha1, pentry)) < 0) { |
463
|
0
|
|
|
|
|
|
git__free(pentry); |
464
|
0
|
|
|
|
|
|
git_error_set_oom(); |
465
|
0
|
|
|
|
|
|
goto on_error; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
43
|
|
|
|
|
|
git_oid_cpy(&entry->oid, &oid); |
469
|
|
|
|
|
|
|
|
470
|
43
|
50
|
|
|
|
|
if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) |
471
|
0
|
|
|
|
|
|
goto on_error; |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
/* Add the object to the list */ |
474
|
43
|
50
|
|
|
|
|
if (git_vector_insert(&idx->objects, entry) < 0) |
475
|
0
|
|
|
|
|
|
goto on_error; |
476
|
|
|
|
|
|
|
|
477
|
4911
|
100
|
|
|
|
|
for (i = oid.id[0]; i < 256; ++i) { |
478
|
4868
|
|
|
|
|
|
idx->fanout[i]++; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
43
|
|
|
|
|
|
return 0; |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
on_error: |
484
|
0
|
|
|
|
|
|
git__free(entry); |
485
|
|
|
|
|
|
|
|
486
|
43
|
|
|
|
|
|
return -1; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
0
|
|
|
|
|
|
GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id) |
490
|
|
|
|
|
|
|
{ |
491
|
0
|
|
|
|
|
|
return git_oidmap_exists(idx->pack->idx_cache, id); |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
2
|
|
|
|
|
|
static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, off64_t entry_start) |
495
|
|
|
|
|
|
|
{ |
496
|
|
|
|
|
|
|
int i; |
497
|
|
|
|
|
|
|
|
498
|
2
|
50
|
|
|
|
|
if (entry_start > UINT31_MAX) { |
499
|
0
|
|
|
|
|
|
entry->offset = UINT32_MAX; |
500
|
0
|
|
|
|
|
|
entry->offset_long = entry_start; |
501
|
|
|
|
|
|
|
} else { |
502
|
2
|
|
|
|
|
|
entry->offset = (uint32_t)entry_start; |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
|
505
|
2
|
|
|
|
|
|
pentry->offset = entry_start; |
506
|
|
|
|
|
|
|
|
507
|
4
|
|
|
|
|
|
if (git_oidmap_exists(idx->pack->idx_cache, &pentry->sha1) || |
508
|
2
|
|
|
|
|
|
git_oidmap_set(idx->pack->idx_cache, &pentry->sha1, pentry) < 0) { |
509
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "cannot insert object into pack"); |
510
|
0
|
|
|
|
|
|
return -1; |
511
|
|
|
|
|
|
|
} |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
/* Add the object to the list */ |
514
|
2
|
50
|
|
|
|
|
if (git_vector_insert(&idx->objects, entry) < 0) |
515
|
0
|
|
|
|
|
|
return -1; |
516
|
|
|
|
|
|
|
|
517
|
136
|
100
|
|
|
|
|
for (i = entry->oid.id[0]; i < 256; ++i) { |
518
|
134
|
|
|
|
|
|
idx->fanout[i]++; |
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
|
521
|
2
|
|
|
|
|
|
return 0; |
522
|
|
|
|
|
|
|
} |
523
|
|
|
|
|
|
|
|
524
|
2
|
|
|
|
|
|
static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start) |
525
|
|
|
|
|
|
|
{ |
526
|
|
|
|
|
|
|
git_oid oid; |
527
|
|
|
|
|
|
|
size_t entry_size; |
528
|
|
|
|
|
|
|
struct entry *entry; |
529
|
2
|
|
|
|
|
|
struct git_pack_entry *pentry = NULL; |
530
|
|
|
|
|
|
|
|
531
|
2
|
|
|
|
|
|
entry = git__calloc(1, sizeof(*entry)); |
532
|
2
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(entry); |
533
|
|
|
|
|
|
|
|
534
|
2
|
50
|
|
|
|
|
if (git_odb__hashobj(&oid, obj) < 0) { |
535
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "failed to hash object"); |
536
|
0
|
|
|
|
|
|
goto on_error; |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
|
539
|
2
|
|
|
|
|
|
pentry = git__calloc(1, sizeof(struct git_pack_entry)); |
540
|
2
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(pentry); |
541
|
|
|
|
|
|
|
|
542
|
2
|
|
|
|
|
|
git_oid_cpy(&pentry->sha1, &oid); |
543
|
2
|
|
|
|
|
|
git_oid_cpy(&entry->oid, &oid); |
544
|
2
|
|
|
|
|
|
entry->crc = crc32(0L, Z_NULL, 0); |
545
|
|
|
|
|
|
|
|
546
|
2
|
|
|
|
|
|
entry_size = (size_t)(idx->off - entry_start); |
547
|
2
|
50
|
|
|
|
|
if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) |
548
|
0
|
|
|
|
|
|
goto on_error; |
549
|
|
|
|
|
|
|
|
550
|
2
|
|
|
|
|
|
return save_entry(idx, entry, pentry, entry_start); |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
on_error: |
553
|
0
|
|
|
|
|
|
git__free(pentry); |
554
|
0
|
|
|
|
|
|
git__free(entry); |
555
|
0
|
|
|
|
|
|
git__free(obj->data); |
556
|
2
|
|
|
|
|
|
return -1; |
557
|
|
|
|
|
|
|
} |
558
|
|
|
|
|
|
|
|
559
|
52
|
|
|
|
|
|
static int do_progress_callback(git_indexer *idx, git_indexer_progress *stats) |
560
|
|
|
|
|
|
|
{ |
561
|
52
|
100
|
|
|
|
|
if (idx->progress_cb) |
562
|
41
|
|
|
|
|
|
return git_error_set_after_callback_function( |
563
|
41
|
|
|
|
|
|
idx->progress_cb(stats, idx->progress_payload), |
564
|
|
|
|
|
|
|
"indexer progress"); |
565
|
11
|
|
|
|
|
|
return 0; |
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
/* Hash everything but the last 20B of input */ |
569
|
91
|
|
|
|
|
|
static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size) |
570
|
|
|
|
|
|
|
{ |
571
|
|
|
|
|
|
|
size_t to_expell, to_keep; |
572
|
|
|
|
|
|
|
|
573
|
91
|
50
|
|
|
|
|
if (size == 0) |
574
|
0
|
|
|
|
|
|
return; |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
/* Easy case, dump the buffer and the data minus the last 20 bytes */ |
577
|
91
|
100
|
|
|
|
|
if (size >= GIT_OID_RAWSZ) { |
578
|
45
|
|
|
|
|
|
git_hash_update(&idx->trailer, idx->inbuf, idx->inbuf_len); |
579
|
45
|
|
|
|
|
|
git_hash_update(&idx->trailer, data, size - GIT_OID_RAWSZ); |
580
|
|
|
|
|
|
|
|
581
|
45
|
|
|
|
|
|
data += size - GIT_OID_RAWSZ; |
582
|
45
|
|
|
|
|
|
memcpy(idx->inbuf, data, GIT_OID_RAWSZ); |
583
|
45
|
|
|
|
|
|
idx->inbuf_len = GIT_OID_RAWSZ; |
584
|
45
|
|
|
|
|
|
return; |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
/* We can just append */ |
588
|
46
|
100
|
|
|
|
|
if (idx->inbuf_len + size <= GIT_OID_RAWSZ) { |
589
|
8
|
|
|
|
|
|
memcpy(idx->inbuf + idx->inbuf_len, data, size); |
590
|
8
|
|
|
|
|
|
idx->inbuf_len += size; |
591
|
8
|
|
|
|
|
|
return; |
592
|
|
|
|
|
|
|
} |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
/* We need to partially drain the buffer and then append */ |
595
|
38
|
|
|
|
|
|
to_keep = GIT_OID_RAWSZ - size; |
596
|
38
|
|
|
|
|
|
to_expell = idx->inbuf_len - to_keep; |
597
|
|
|
|
|
|
|
|
598
|
38
|
|
|
|
|
|
git_hash_update(&idx->trailer, idx->inbuf, to_expell); |
599
|
|
|
|
|
|
|
|
600
|
38
|
|
|
|
|
|
memmove(idx->inbuf, idx->inbuf + to_expell, to_keep); |
601
|
38
|
|
|
|
|
|
memcpy(idx->inbuf + to_keep, data, size); |
602
|
38
|
|
|
|
|
|
idx->inbuf_len += size - to_expell; |
603
|
|
|
|
|
|
|
} |
604
|
|
|
|
|
|
|
|
605
|
91
|
|
|
|
|
|
static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size) |
606
|
|
|
|
|
|
|
{ |
607
|
91
|
|
|
|
|
|
git_file fd = idx->pack->mwf.fd; |
608
|
|
|
|
|
|
|
size_t mmap_alignment; |
609
|
|
|
|
|
|
|
size_t page_offset; |
610
|
|
|
|
|
|
|
off64_t page_start; |
611
|
|
|
|
|
|
|
unsigned char *map_data; |
612
|
|
|
|
|
|
|
git_map map; |
613
|
|
|
|
|
|
|
int error; |
614
|
|
|
|
|
|
|
|
615
|
91
|
50
|
|
|
|
|
assert(data && size); |
|
|
50
|
|
|
|
|
|
616
|
|
|
|
|
|
|
|
617
|
91
|
50
|
|
|
|
|
if ((error = git__mmap_alignment(&mmap_alignment)) < 0) |
618
|
0
|
|
|
|
|
|
return error; |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
/* the offset needs to be at the mmap boundary for the platform */ |
621
|
91
|
|
|
|
|
|
page_offset = offset % mmap_alignment; |
622
|
91
|
|
|
|
|
|
page_start = offset - page_offset; |
623
|
|
|
|
|
|
|
|
624
|
91
|
50
|
|
|
|
|
if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0) |
625
|
0
|
|
|
|
|
|
return error; |
626
|
|
|
|
|
|
|
|
627
|
91
|
|
|
|
|
|
map_data = (unsigned char *)map.data; |
628
|
91
|
|
|
|
|
|
memcpy(map_data + page_offset, data, size); |
629
|
91
|
|
|
|
|
|
p_munmap(&map); |
630
|
|
|
|
|
|
|
|
631
|
91
|
|
|
|
|
|
return 0; |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
91
|
|
|
|
|
|
static int append_to_pack(git_indexer *idx, const void *data, size_t size) |
635
|
|
|
|
|
|
|
{ |
636
|
|
|
|
|
|
|
off64_t new_size; |
637
|
|
|
|
|
|
|
size_t mmap_alignment; |
638
|
|
|
|
|
|
|
size_t page_offset; |
639
|
|
|
|
|
|
|
off64_t page_start; |
640
|
91
|
|
|
|
|
|
off64_t current_size = idx->pack->mwf.size; |
641
|
91
|
|
|
|
|
|
int fd = idx->pack->mwf.fd; |
642
|
|
|
|
|
|
|
int error; |
643
|
|
|
|
|
|
|
|
644
|
91
|
50
|
|
|
|
|
if (!size) |
645
|
0
|
|
|
|
|
|
return 0; |
646
|
|
|
|
|
|
|
|
647
|
91
|
50
|
|
|
|
|
if ((error = git__mmap_alignment(&mmap_alignment)) < 0) |
648
|
0
|
|
|
|
|
|
return error; |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
/* Write a single byte to force the file system to allocate space now or |
651
|
|
|
|
|
|
|
* report an error, since we can't report errors when writing using mmap. |
652
|
|
|
|
|
|
|
* Round the size up to the nearest page so that we only need to perform file |
653
|
|
|
|
|
|
|
* I/O when we add a page, instead of whenever we write even a single byte. */ |
654
|
91
|
|
|
|
|
|
new_size = current_size + size; |
655
|
91
|
|
|
|
|
|
page_offset = new_size % mmap_alignment; |
656
|
91
|
|
|
|
|
|
page_start = new_size - page_offset; |
657
|
|
|
|
|
|
|
|
658
|
182
|
|
|
|
|
|
if (p_lseek(fd, page_start + mmap_alignment - 1, SEEK_SET) < 0 || |
659
|
91
|
|
|
|
|
|
p_write(idx->pack->mwf.fd, data, 1) < 0) { |
660
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "cannot extend packfile '%s'", idx->pack->pack_name); |
661
|
0
|
|
|
|
|
|
return -1; |
662
|
|
|
|
|
|
|
} |
663
|
|
|
|
|
|
|
|
664
|
91
|
|
|
|
|
|
return write_at(idx, data, idx->pack->mwf.size, size); |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
|
667
|
131
|
|
|
|
|
|
static int read_stream_object(git_indexer *idx, git_indexer_progress *stats) |
668
|
|
|
|
|
|
|
{ |
669
|
131
|
|
|
|
|
|
git_packfile_stream *stream = &idx->stream; |
670
|
131
|
|
|
|
|
|
off64_t entry_start = idx->off; |
671
|
|
|
|
|
|
|
size_t entry_size; |
672
|
|
|
|
|
|
|
git_object_t type; |
673
|
131
|
|
|
|
|
|
git_mwindow *w = NULL; |
674
|
|
|
|
|
|
|
int error; |
675
|
|
|
|
|
|
|
|
676
|
131
|
100
|
|
|
|
|
if (idx->pack->mwf.size <= idx->off + 20) |
677
|
84
|
|
|
|
|
|
return GIT_EBUFS; |
678
|
|
|
|
|
|
|
|
679
|
47
|
100
|
|
|
|
|
if (!idx->have_stream) { |
680
|
45
|
|
|
|
|
|
error = git_packfile_unpack_header(&entry_size, &type, &idx->pack->mwf, &w, &idx->off); |
681
|
45
|
50
|
|
|
|
|
if (error == GIT_EBUFS) { |
682
|
0
|
|
|
|
|
|
idx->off = entry_start; |
683
|
0
|
|
|
|
|
|
return error; |
684
|
|
|
|
|
|
|
} |
685
|
45
|
50
|
|
|
|
|
if (error < 0) |
686
|
0
|
|
|
|
|
|
return error; |
687
|
|
|
|
|
|
|
|
688
|
45
|
|
|
|
|
|
git_mwindow_close(&w); |
689
|
45
|
|
|
|
|
|
idx->entry_start = entry_start; |
690
|
45
|
|
|
|
|
|
git_hash_init(&idx->hash_ctx); |
691
|
45
|
|
|
|
|
|
git_buf_clear(&idx->entry_data); |
692
|
|
|
|
|
|
|
|
693
|
45
|
100
|
|
|
|
|
if (type == GIT_OBJECT_REF_DELTA || type == GIT_OBJECT_OFS_DELTA) { |
|
|
50
|
|
|
|
|
|
694
|
2
|
|
|
|
|
|
error = advance_delta_offset(idx, type); |
695
|
2
|
50
|
|
|
|
|
if (error == GIT_EBUFS) { |
696
|
0
|
|
|
|
|
|
idx->off = entry_start; |
697
|
0
|
|
|
|
|
|
return error; |
698
|
|
|
|
|
|
|
} |
699
|
2
|
50
|
|
|
|
|
if (error < 0) |
700
|
0
|
|
|
|
|
|
return error; |
701
|
|
|
|
|
|
|
|
702
|
2
|
|
|
|
|
|
idx->have_delta = 1; |
703
|
|
|
|
|
|
|
} else { |
704
|
43
|
|
|
|
|
|
idx->have_delta = 0; |
705
|
|
|
|
|
|
|
|
706
|
43
|
|
|
|
|
|
error = hash_header(&idx->hash_ctx, entry_size, type); |
707
|
43
|
50
|
|
|
|
|
if (error < 0) |
708
|
0
|
|
|
|
|
|
return error; |
709
|
|
|
|
|
|
|
} |
710
|
|
|
|
|
|
|
|
711
|
45
|
|
|
|
|
|
idx->have_stream = 1; |
712
|
45
|
|
|
|
|
|
idx->entry_type = type; |
713
|
|
|
|
|
|
|
|
714
|
45
|
|
|
|
|
|
error = git_packfile_stream_open(stream, idx->pack, idx->off); |
715
|
45
|
50
|
|
|
|
|
if (error < 0) |
716
|
0
|
|
|
|
|
|
return error; |
717
|
|
|
|
|
|
|
} |
718
|
|
|
|
|
|
|
|
719
|
47
|
100
|
|
|
|
|
if (idx->have_delta) { |
720
|
4
|
|
|
|
|
|
error = read_object_stream(idx, stream); |
721
|
|
|
|
|
|
|
} else { |
722
|
43
|
|
|
|
|
|
error = hash_object_stream(idx, stream); |
723
|
|
|
|
|
|
|
} |
724
|
|
|
|
|
|
|
|
725
|
47
|
|
|
|
|
|
idx->off = stream->curpos; |
726
|
47
|
100
|
|
|
|
|
if (error == GIT_EBUFS) |
727
|
2
|
|
|
|
|
|
return error; |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
/* We want to free the stream reasorces no matter what here */ |
730
|
45
|
|
|
|
|
|
idx->have_stream = 0; |
731
|
45
|
|
|
|
|
|
git_packfile_stream_dispose(stream); |
732
|
|
|
|
|
|
|
|
733
|
45
|
50
|
|
|
|
|
if (error < 0) |
734
|
0
|
|
|
|
|
|
return error; |
735
|
|
|
|
|
|
|
|
736
|
45
|
100
|
|
|
|
|
if (idx->have_delta) { |
737
|
2
|
|
|
|
|
|
error = store_delta(idx); |
738
|
|
|
|
|
|
|
} else { |
739
|
43
|
|
|
|
|
|
error = store_object(idx); |
740
|
|
|
|
|
|
|
} |
741
|
|
|
|
|
|
|
|
742
|
45
|
50
|
|
|
|
|
if (error < 0) |
743
|
0
|
|
|
|
|
|
return error; |
744
|
|
|
|
|
|
|
|
745
|
45
|
100
|
|
|
|
|
if (!idx->have_delta) { |
746
|
43
|
|
|
|
|
|
stats->indexed_objects++; |
747
|
|
|
|
|
|
|
} |
748
|
45
|
|
|
|
|
|
stats->received_objects++; |
749
|
|
|
|
|
|
|
|
750
|
45
|
50
|
|
|
|
|
if ((error = do_progress_callback(idx, stats)) != 0) |
751
|
0
|
|
|
|
|
|
return error; |
752
|
|
|
|
|
|
|
|
753
|
131
|
|
|
|
|
|
return 0; |
754
|
|
|
|
|
|
|
} |
755
|
|
|
|
|
|
|
|
756
|
91
|
|
|
|
|
|
int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_indexer_progress *stats) |
757
|
|
|
|
|
|
|
{ |
758
|
91
|
|
|
|
|
|
int error = -1; |
759
|
91
|
|
|
|
|
|
struct git_pack_header *hdr = &idx->hdr; |
760
|
91
|
|
|
|
|
|
git_mwindow_file *mwf = &idx->pack->mwf; |
761
|
|
|
|
|
|
|
|
762
|
91
|
50
|
|
|
|
|
assert(idx && data && stats); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
763
|
|
|
|
|
|
|
|
764
|
91
|
50
|
|
|
|
|
if ((error = append_to_pack(idx, data, size)) < 0) |
765
|
0
|
|
|
|
|
|
return error; |
766
|
|
|
|
|
|
|
|
767
|
91
|
|
|
|
|
|
hash_partially(idx, data, (int)size); |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
/* Make sure we set the new size of the pack */ |
770
|
91
|
|
|
|
|
|
idx->pack->mwf.size += size; |
771
|
|
|
|
|
|
|
|
772
|
91
|
100
|
|
|
|
|
if (!idx->parsed_header) { |
773
|
|
|
|
|
|
|
unsigned int total_objects; |
774
|
|
|
|
|
|
|
|
775
|
5
|
50
|
|
|
|
|
if ((unsigned)idx->pack->mwf.size < sizeof(struct git_pack_header)) |
776
|
0
|
|
|
|
|
|
return 0; |
777
|
|
|
|
|
|
|
|
778
|
5
|
50
|
|
|
|
|
if ((error = parse_header(&idx->hdr, idx->pack)) < 0) |
779
|
0
|
|
|
|
|
|
return error; |
780
|
|
|
|
|
|
|
|
781
|
5
|
|
|
|
|
|
idx->parsed_header = 1; |
782
|
5
|
|
|
|
|
|
idx->nr_objects = ntohl(hdr->hdr_entries); |
783
|
5
|
|
|
|
|
|
idx->off = sizeof(struct git_pack_header); |
784
|
|
|
|
|
|
|
|
785
|
5
|
50
|
|
|
|
|
if (idx->nr_objects <= git_indexer__max_objects) { |
786
|
5
|
|
|
|
|
|
total_objects = (unsigned int)idx->nr_objects; |
787
|
|
|
|
|
|
|
} else { |
788
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "too many objects"); |
789
|
0
|
|
|
|
|
|
return -1; |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
|
792
|
5
|
50
|
|
|
|
|
if (git_oidmap_new(&idx->pack->idx_cache) < 0) |
793
|
0
|
|
|
|
|
|
return -1; |
794
|
|
|
|
|
|
|
|
795
|
5
|
|
|
|
|
|
idx->pack->has_cache = 1; |
796
|
5
|
50
|
|
|
|
|
if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0) |
797
|
0
|
|
|
|
|
|
return -1; |
798
|
|
|
|
|
|
|
|
799
|
5
|
50
|
|
|
|
|
if (git_vector_init(&idx->deltas, total_objects / 2, NULL) < 0) |
800
|
0
|
|
|
|
|
|
return -1; |
801
|
|
|
|
|
|
|
|
802
|
5
|
|
|
|
|
|
stats->received_objects = 0; |
803
|
5
|
|
|
|
|
|
stats->local_objects = 0; |
804
|
5
|
|
|
|
|
|
stats->total_deltas = 0; |
805
|
5
|
|
|
|
|
|
stats->indexed_deltas = 0; |
806
|
5
|
|
|
|
|
|
stats->indexed_objects = 0; |
807
|
5
|
|
|
|
|
|
stats->total_objects = total_objects; |
808
|
|
|
|
|
|
|
|
809
|
5
|
50
|
|
|
|
|
if ((error = do_progress_callback(idx, stats)) != 0) |
810
|
0
|
|
|
|
|
|
return error; |
811
|
|
|
|
|
|
|
} |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
/* Now that we have data in the pack, let's try to parse it */ |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
/* As the file grows any windows we try to use will be out of date */ |
816
|
91
|
|
|
|
|
|
git_mwindow_free_all(mwf); |
817
|
|
|
|
|
|
|
|
818
|
136
|
100
|
|
|
|
|
while (stats->indexed_objects < idx->nr_objects) { |
819
|
131
|
100
|
|
|
|
|
if ((error = read_stream_object(idx, stats)) != 0) { |
820
|
86
|
50
|
|
|
|
|
if (error == GIT_EBUFS) |
821
|
86
|
|
|
|
|
|
break; |
822
|
|
|
|
|
|
|
else |
823
|
0
|
|
|
|
|
|
goto on_error; |
824
|
|
|
|
|
|
|
} |
825
|
|
|
|
|
|
|
} |
826
|
|
|
|
|
|
|
|
827
|
91
|
|
|
|
|
|
return 0; |
828
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
on_error: |
830
|
0
|
|
|
|
|
|
git_mwindow_free_all(mwf); |
831
|
0
|
|
|
|
|
|
return error; |
832
|
|
|
|
|
|
|
} |
833
|
|
|
|
|
|
|
|
834
|
10
|
|
|
|
|
|
static int index_path(git_buf *path, git_indexer *idx, const char *suffix) |
835
|
|
|
|
|
|
|
{ |
836
|
10
|
|
|
|
|
|
const char prefix[] = "pack-"; |
837
|
10
|
|
|
|
|
|
size_t slash = (size_t)path->size; |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
/* search backwards for '/' */ |
840
|
330
|
50
|
|
|
|
|
while (slash > 0 && path->ptr[slash - 1] != '/') |
|
|
100
|
|
|
|
|
|
841
|
320
|
|
|
|
|
|
slash--; |
842
|
|
|
|
|
|
|
|
843
|
10
|
50
|
|
|
|
|
if (git_buf_grow(path, slash + 1 + strlen(prefix) + |
844
|
10
|
|
|
|
|
|
GIT_OID_HEXSZ + strlen(suffix) + 1) < 0) |
845
|
0
|
|
|
|
|
|
return -1; |
846
|
|
|
|
|
|
|
|
847
|
10
|
|
|
|
|
|
git_buf_truncate(path, slash); |
848
|
10
|
|
|
|
|
|
git_buf_puts(path, prefix); |
849
|
10
|
|
|
|
|
|
git_oid_fmt(path->ptr + git_buf_len(path), &idx->hash); |
850
|
10
|
|
|
|
|
|
path->size += GIT_OID_HEXSZ; |
851
|
10
|
|
|
|
|
|
git_buf_puts(path, suffix); |
852
|
|
|
|
|
|
|
|
853
|
10
|
50
|
|
|
|
|
return git_buf_oom(path) ? -1 : 0; |
854
|
|
|
|
|
|
|
} |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
/** |
857
|
|
|
|
|
|
|
* Rewind the packfile by the trailer, as we might need to fix the |
858
|
|
|
|
|
|
|
* packfile by injecting objects at the tail and must overwrite it. |
859
|
|
|
|
|
|
|
*/ |
860
|
0
|
|
|
|
|
|
static void seek_back_trailer(git_indexer *idx) |
861
|
|
|
|
|
|
|
{ |
862
|
0
|
|
|
|
|
|
idx->pack->mwf.size -= GIT_OID_RAWSZ; |
863
|
0
|
|
|
|
|
|
git_mwindow_free_all(&idx->pack->mwf); |
864
|
0
|
|
|
|
|
|
} |
865
|
|
|
|
|
|
|
|
866
|
0
|
|
|
|
|
|
static int inject_object(git_indexer *idx, git_oid *id) |
867
|
|
|
|
|
|
|
{ |
868
|
|
|
|
|
|
|
git_odb_object *obj; |
869
|
|
|
|
|
|
|
struct entry *entry; |
870
|
0
|
|
|
|
|
|
struct git_pack_entry *pentry = NULL; |
871
|
0
|
|
|
|
|
|
git_oid foo = {{0}}; |
872
|
|
|
|
|
|
|
unsigned char hdr[64]; |
873
|
0
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
874
|
|
|
|
|
|
|
off64_t entry_start; |
875
|
|
|
|
|
|
|
const void *data; |
876
|
|
|
|
|
|
|
size_t len, hdr_len; |
877
|
|
|
|
|
|
|
int error; |
878
|
|
|
|
|
|
|
|
879
|
0
|
|
|
|
|
|
seek_back_trailer(idx); |
880
|
0
|
|
|
|
|
|
entry_start = idx->pack->mwf.size; |
881
|
|
|
|
|
|
|
|
882
|
0
|
0
|
|
|
|
|
if (git_odb_read(&obj, idx->odb, id) < 0) { |
883
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "missing delta bases"); |
884
|
0
|
|
|
|
|
|
return -1; |
885
|
|
|
|
|
|
|
} |
886
|
|
|
|
|
|
|
|
887
|
0
|
|
|
|
|
|
data = git_odb_object_data(obj); |
888
|
0
|
|
|
|
|
|
len = git_odb_object_size(obj); |
889
|
|
|
|
|
|
|
|
890
|
0
|
|
|
|
|
|
entry = git__calloc(1, sizeof(*entry)); |
891
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(entry); |
892
|
|
|
|
|
|
|
|
893
|
0
|
|
|
|
|
|
entry->crc = crc32(0L, Z_NULL, 0); |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
/* Write out the object header */ |
896
|
0
|
|
|
|
|
|
hdr_len = git_packfile__object_header(hdr, len, git_odb_object_type(obj)); |
897
|
0
|
0
|
|
|
|
|
if ((error = append_to_pack(idx, hdr, hdr_len)) < 0) |
898
|
0
|
|
|
|
|
|
goto cleanup; |
899
|
|
|
|
|
|
|
|
900
|
0
|
|
|
|
|
|
idx->pack->mwf.size += hdr_len; |
901
|
0
|
|
|
|
|
|
entry->crc = crc32(entry->crc, hdr, (uInt)hdr_len); |
902
|
|
|
|
|
|
|
|
903
|
0
|
0
|
|
|
|
|
if ((error = git_zstream_deflatebuf(&buf, data, len)) < 0) |
904
|
0
|
|
|
|
|
|
goto cleanup; |
905
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
/* And then the compressed object */ |
907
|
0
|
0
|
|
|
|
|
if ((error = append_to_pack(idx, buf.ptr, buf.size)) < 0) |
908
|
0
|
|
|
|
|
|
goto cleanup; |
909
|
|
|
|
|
|
|
|
910
|
0
|
|
|
|
|
|
idx->pack->mwf.size += buf.size; |
911
|
0
|
|
|
|
|
|
entry->crc = htonl(crc32(entry->crc, (unsigned char *)buf.ptr, (uInt)buf.size)); |
912
|
0
|
|
|
|
|
|
git_buf_dispose(&buf); |
913
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
/* Write a fake trailer so the pack functions play ball */ |
915
|
|
|
|
|
|
|
|
916
|
0
|
0
|
|
|
|
|
if ((error = append_to_pack(idx, &foo, GIT_OID_RAWSZ)) < 0) |
917
|
0
|
|
|
|
|
|
goto cleanup; |
918
|
|
|
|
|
|
|
|
919
|
0
|
|
|
|
|
|
idx->pack->mwf.size += GIT_OID_RAWSZ; |
920
|
|
|
|
|
|
|
|
921
|
0
|
|
|
|
|
|
pentry = git__calloc(1, sizeof(struct git_pack_entry)); |
922
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(pentry); |
923
|
|
|
|
|
|
|
|
924
|
0
|
|
|
|
|
|
git_oid_cpy(&pentry->sha1, id); |
925
|
0
|
|
|
|
|
|
git_oid_cpy(&entry->oid, id); |
926
|
0
|
|
|
|
|
|
idx->off = entry_start + hdr_len + len; |
927
|
|
|
|
|
|
|
|
928
|
0
|
|
|
|
|
|
error = save_entry(idx, entry, pentry, entry_start); |
929
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
cleanup: |
931
|
0
|
0
|
|
|
|
|
if (error) { |
932
|
0
|
|
|
|
|
|
git__free(entry); |
933
|
0
|
|
|
|
|
|
git__free(pentry); |
934
|
|
|
|
|
|
|
} |
935
|
|
|
|
|
|
|
|
936
|
0
|
|
|
|
|
|
git_odb_object_free(obj); |
937
|
0
|
|
|
|
|
|
return error; |
938
|
|
|
|
|
|
|
} |
939
|
|
|
|
|
|
|
|
940
|
0
|
|
|
|
|
|
static int fix_thin_pack(git_indexer *idx, git_indexer_progress *stats) |
941
|
|
|
|
|
|
|
{ |
942
|
0
|
|
|
|
|
|
int error, found_ref_delta = 0; |
943
|
|
|
|
|
|
|
unsigned int i; |
944
|
|
|
|
|
|
|
struct delta_info *delta; |
945
|
|
|
|
|
|
|
size_t size; |
946
|
|
|
|
|
|
|
git_object_t type; |
947
|
0
|
|
|
|
|
|
git_mwindow *w = NULL; |
948
|
0
|
|
|
|
|
|
off64_t curpos = 0; |
949
|
|
|
|
|
|
|
unsigned char *base_info; |
950
|
0
|
|
|
|
|
|
unsigned int left = 0; |
951
|
|
|
|
|
|
|
git_oid base; |
952
|
|
|
|
|
|
|
|
953
|
0
|
0
|
|
|
|
|
assert(git_vector_length(&idx->deltas) > 0); |
954
|
|
|
|
|
|
|
|
955
|
0
|
0
|
|
|
|
|
if (idx->odb == NULL) { |
956
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "cannot fix a thin pack without an ODB"); |
957
|
0
|
|
|
|
|
|
return -1; |
958
|
|
|
|
|
|
|
} |
959
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
/* Loop until we find the first REF delta */ |
961
|
0
|
0
|
|
|
|
|
git_vector_foreach(&idx->deltas, i, delta) { |
962
|
0
|
0
|
|
|
|
|
if (!delta) |
963
|
0
|
|
|
|
|
|
continue; |
964
|
|
|
|
|
|
|
|
965
|
0
|
|
|
|
|
|
curpos = delta->delta_off; |
966
|
0
|
|
|
|
|
|
error = git_packfile_unpack_header(&size, &type, &idx->pack->mwf, &w, &curpos); |
967
|
0
|
0
|
|
|
|
|
if (error < 0) |
968
|
0
|
|
|
|
|
|
return error; |
969
|
|
|
|
|
|
|
|
970
|
0
|
0
|
|
|
|
|
if (type == GIT_OBJECT_REF_DELTA) { |
971
|
0
|
|
|
|
|
|
found_ref_delta = 1; |
972
|
0
|
|
|
|
|
|
break; |
973
|
|
|
|
|
|
|
} |
974
|
|
|
|
|
|
|
} |
975
|
|
|
|
|
|
|
|
976
|
0
|
0
|
|
|
|
|
if (!found_ref_delta) { |
977
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "no REF_DELTA found, cannot inject object"); |
978
|
0
|
|
|
|
|
|
return -1; |
979
|
|
|
|
|
|
|
} |
980
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
/* curpos now points to the base information, which is an OID */ |
982
|
0
|
|
|
|
|
|
base_info = git_mwindow_open(&idx->pack->mwf, &w, curpos, GIT_OID_RAWSZ, &left); |
983
|
0
|
0
|
|
|
|
|
if (base_info == NULL) { |
984
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "failed to map delta information"); |
985
|
0
|
|
|
|
|
|
return -1; |
986
|
|
|
|
|
|
|
} |
987
|
|
|
|
|
|
|
|
988
|
0
|
|
|
|
|
|
git_oid_fromraw(&base, base_info); |
989
|
0
|
|
|
|
|
|
git_mwindow_close(&w); |
990
|
|
|
|
|
|
|
|
991
|
0
|
0
|
|
|
|
|
if (has_entry(idx, &base)) |
992
|
0
|
|
|
|
|
|
return 0; |
993
|
|
|
|
|
|
|
|
994
|
0
|
0
|
|
|
|
|
if (inject_object(idx, &base) < 0) |
995
|
0
|
|
|
|
|
|
return -1; |
996
|
|
|
|
|
|
|
|
997
|
0
|
|
|
|
|
|
stats->local_objects++; |
998
|
|
|
|
|
|
|
|
999
|
0
|
|
|
|
|
|
return 0; |
1000
|
|
|
|
|
|
|
} |
1001
|
|
|
|
|
|
|
|
1002
|
5
|
|
|
|
|
|
static int resolve_deltas(git_indexer *idx, git_indexer_progress *stats) |
1003
|
|
|
|
|
|
|
{ |
1004
|
|
|
|
|
|
|
unsigned int i; |
1005
|
|
|
|
|
|
|
int error; |
1006
|
|
|
|
|
|
|
struct delta_info *delta; |
1007
|
5
|
|
|
|
|
|
int progressed = 0, non_null = 0, progress_cb_result; |
1008
|
|
|
|
|
|
|
|
1009
|
7
|
100
|
|
|
|
|
while (idx->deltas.length > 0) { |
1010
|
4
|
|
|
|
|
|
progressed = 0; |
1011
|
4
|
|
|
|
|
|
non_null = 0; |
1012
|
8
|
100
|
|
|
|
|
git_vector_foreach(&idx->deltas, i, delta) { |
1013
|
4
|
|
|
|
|
|
git_rawobj obj = {0}; |
1014
|
|
|
|
|
|
|
|
1015
|
4
|
100
|
|
|
|
|
if (!delta) |
1016
|
2
|
|
|
|
|
|
continue; |
1017
|
|
|
|
|
|
|
|
1018
|
2
|
|
|
|
|
|
non_null = 1; |
1019
|
2
|
|
|
|
|
|
idx->off = delta->delta_off; |
1020
|
2
|
50
|
|
|
|
|
if ((error = git_packfile_unpack(&obj, idx->pack, &idx->off)) < 0) { |
1021
|
0
|
0
|
|
|
|
|
if (error == GIT_PASSTHROUGH) { |
1022
|
|
|
|
|
|
|
/* We have not seen the base object, we'll try again later. */ |
1023
|
0
|
|
|
|
|
|
continue; |
1024
|
|
|
|
|
|
|
} |
1025
|
0
|
|
|
|
|
|
return -1; |
1026
|
|
|
|
|
|
|
} |
1027
|
|
|
|
|
|
|
|
1028
|
2
|
50
|
|
|
|
|
if (idx->do_verify && check_object_connectivity(idx, &obj) < 0) |
|
|
0
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
/* TODO: error? continue? */ |
1030
|
0
|
|
|
|
|
|
continue; |
1031
|
|
|
|
|
|
|
|
1032
|
2
|
50
|
|
|
|
|
if (hash_and_save(idx, &obj, delta->delta_off) < 0) |
1033
|
0
|
|
|
|
|
|
continue; |
1034
|
|
|
|
|
|
|
|
1035
|
2
|
|
|
|
|
|
git__free(obj.data); |
1036
|
2
|
|
|
|
|
|
stats->indexed_objects++; |
1037
|
2
|
|
|
|
|
|
stats->indexed_deltas++; |
1038
|
2
|
|
|
|
|
|
progressed = 1; |
1039
|
2
|
50
|
|
|
|
|
if ((progress_cb_result = do_progress_callback(idx, stats)) < 0) |
1040
|
0
|
|
|
|
|
|
return progress_cb_result; |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
/* remove from the list */ |
1043
|
2
|
|
|
|
|
|
git_vector_set(NULL, &idx->deltas, i, NULL); |
1044
|
2
|
|
|
|
|
|
git__free(delta); |
1045
|
|
|
|
|
|
|
} |
1046
|
|
|
|
|
|
|
|
1047
|
|
|
|
|
|
|
/* if none were actually set, we're done */ |
1048
|
4
|
100
|
|
|
|
|
if (!non_null) |
1049
|
2
|
|
|
|
|
|
break; |
1050
|
|
|
|
|
|
|
|
1051
|
2
|
50
|
|
|
|
|
if (!progressed && (fix_thin_pack(idx, stats) < 0)) { |
|
|
0
|
|
|
|
|
|
1052
|
0
|
|
|
|
|
|
return -1; |
1053
|
|
|
|
|
|
|
} |
1054
|
|
|
|
|
|
|
} |
1055
|
|
|
|
|
|
|
|
1056
|
5
|
|
|
|
|
|
return 0; |
1057
|
|
|
|
|
|
|
} |
1058
|
|
|
|
|
|
|
|
1059
|
0
|
|
|
|
|
|
static int update_header_and_rehash(git_indexer *idx, git_indexer_progress *stats) |
1060
|
|
|
|
|
|
|
{ |
1061
|
|
|
|
|
|
|
void *ptr; |
1062
|
0
|
|
|
|
|
|
size_t chunk = 1024*1024; |
1063
|
0
|
|
|
|
|
|
off64_t hashed = 0; |
1064
|
0
|
|
|
|
|
|
git_mwindow *w = NULL; |
1065
|
|
|
|
|
|
|
git_mwindow_file *mwf; |
1066
|
|
|
|
|
|
|
unsigned int left; |
1067
|
|
|
|
|
|
|
|
1068
|
0
|
|
|
|
|
|
mwf = &idx->pack->mwf; |
1069
|
|
|
|
|
|
|
|
1070
|
0
|
|
|
|
|
|
git_hash_init(&idx->trailer); |
1071
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
/* Update the header to include the numer of local objects we injected */ |
1074
|
0
|
|
|
|
|
|
idx->hdr.hdr_entries = htonl(stats->total_objects + stats->local_objects); |
1075
|
0
|
0
|
|
|
|
|
if (write_at(idx, &idx->hdr, 0, sizeof(struct git_pack_header)) < 0) |
1076
|
0
|
|
|
|
|
|
return -1; |
1077
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
/* |
1079
|
|
|
|
|
|
|
* We now use the same technique as before to determine the |
1080
|
|
|
|
|
|
|
* hash. We keep reading up to the end and let |
1081
|
|
|
|
|
|
|
* hash_partially() keep the existing trailer out of the |
1082
|
|
|
|
|
|
|
* calculation. |
1083
|
|
|
|
|
|
|
*/ |
1084
|
0
|
|
|
|
|
|
git_mwindow_free_all(mwf); |
1085
|
0
|
|
|
|
|
|
idx->inbuf_len = 0; |
1086
|
0
|
0
|
|
|
|
|
while (hashed < mwf->size) { |
1087
|
0
|
|
|
|
|
|
ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left); |
1088
|
0
|
0
|
|
|
|
|
if (ptr == NULL) |
1089
|
0
|
|
|
|
|
|
return -1; |
1090
|
|
|
|
|
|
|
|
1091
|
0
|
|
|
|
|
|
hash_partially(idx, ptr, left); |
1092
|
0
|
|
|
|
|
|
hashed += left; |
1093
|
|
|
|
|
|
|
|
1094
|
0
|
|
|
|
|
|
git_mwindow_close(&w); |
1095
|
|
|
|
|
|
|
} |
1096
|
|
|
|
|
|
|
|
1097
|
0
|
|
|
|
|
|
return 0; |
1098
|
|
|
|
|
|
|
} |
1099
|
|
|
|
|
|
|
|
1100
|
5
|
|
|
|
|
|
int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) |
1101
|
|
|
|
|
|
|
{ |
1102
|
5
|
|
|
|
|
|
git_mwindow *w = NULL; |
1103
|
5
|
|
|
|
|
|
unsigned int i, long_offsets = 0, left; |
1104
|
|
|
|
|
|
|
int error; |
1105
|
|
|
|
|
|
|
struct git_pack_idx_header hdr; |
1106
|
5
|
|
|
|
|
|
git_buf filename = GIT_BUF_INIT; |
1107
|
|
|
|
|
|
|
struct entry *entry; |
1108
|
|
|
|
|
|
|
git_oid trailer_hash, file_hash; |
1109
|
5
|
|
|
|
|
|
git_filebuf index_file = {0}; |
1110
|
|
|
|
|
|
|
void *packfile_trailer; |
1111
|
|
|
|
|
|
|
|
1112
|
5
|
50
|
|
|
|
|
if (!idx->parsed_header) { |
1113
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "incomplete pack header"); |
1114
|
0
|
|
|
|
|
|
return -1; |
1115
|
|
|
|
|
|
|
} |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
/* Test for this before resolve_deltas(), as it plays with idx->off */ |
1118
|
5
|
50
|
|
|
|
|
if (idx->off + 20 < idx->pack->mwf.size) { |
1119
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "unexpected data at the end of the pack"); |
1120
|
0
|
|
|
|
|
|
return -1; |
1121
|
|
|
|
|
|
|
} |
1122
|
5
|
50
|
|
|
|
|
if (idx->off + 20 > idx->pack->mwf.size) { |
1123
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "missing trailer at the end of the pack"); |
1124
|
0
|
|
|
|
|
|
return -1; |
1125
|
|
|
|
|
|
|
} |
1126
|
|
|
|
|
|
|
|
1127
|
5
|
|
|
|
|
|
packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left); |
1128
|
5
|
50
|
|
|
|
|
if (packfile_trailer == NULL) { |
1129
|
0
|
|
|
|
|
|
git_mwindow_close(&w); |
1130
|
0
|
|
|
|
|
|
goto on_error; |
1131
|
|
|
|
|
|
|
} |
1132
|
|
|
|
|
|
|
|
1133
|
|
|
|
|
|
|
/* Compare the packfile trailer as it was sent to us and what we calculated */ |
1134
|
5
|
|
|
|
|
|
git_oid_fromraw(&file_hash, packfile_trailer); |
1135
|
5
|
|
|
|
|
|
git_mwindow_close(&w); |
1136
|
|
|
|
|
|
|
|
1137
|
5
|
|
|
|
|
|
git_hash_final(&trailer_hash, &idx->trailer); |
1138
|
5
|
50
|
|
|
|
|
if (git_oid_cmp(&file_hash, &trailer_hash)) { |
1139
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "packfile trailer mismatch"); |
1140
|
0
|
|
|
|
|
|
return -1; |
1141
|
|
|
|
|
|
|
} |
1142
|
|
|
|
|
|
|
|
1143
|
|
|
|
|
|
|
/* Freeze the number of deltas */ |
1144
|
5
|
|
|
|
|
|
stats->total_deltas = stats->total_objects - stats->indexed_objects; |
1145
|
|
|
|
|
|
|
|
1146
|
5
|
50
|
|
|
|
|
if ((error = resolve_deltas(idx, stats)) < 0) |
1147
|
0
|
|
|
|
|
|
return error; |
1148
|
|
|
|
|
|
|
|
1149
|
5
|
50
|
|
|
|
|
if (stats->indexed_objects != stats->total_objects) { |
1150
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "early EOF"); |
1151
|
0
|
|
|
|
|
|
return -1; |
1152
|
|
|
|
|
|
|
} |
1153
|
|
|
|
|
|
|
|
1154
|
5
|
50
|
|
|
|
|
if (stats->local_objects > 0) { |
1155
|
0
|
0
|
|
|
|
|
if (update_header_and_rehash(idx, stats) < 0) |
1156
|
0
|
|
|
|
|
|
return -1; |
1157
|
|
|
|
|
|
|
|
1158
|
0
|
|
|
|
|
|
git_hash_final(&trailer_hash, &idx->trailer); |
1159
|
0
|
|
|
|
|
|
write_at(idx, &trailer_hash, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ); |
1160
|
|
|
|
|
|
|
} |
1161
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
/* |
1163
|
|
|
|
|
|
|
* Is the resulting graph fully connected or are we still |
1164
|
|
|
|
|
|
|
* missing some objects? In the second case, we can |
1165
|
|
|
|
|
|
|
* bail out due to an incomplete and thus corrupt |
1166
|
|
|
|
|
|
|
* packfile. |
1167
|
|
|
|
|
|
|
*/ |
1168
|
5
|
50
|
|
|
|
|
if (git_oidmap_size(idx->expected_oids) > 0) { |
1169
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEXER, "packfile is missing %"PRIuZ" objects", |
1170
|
|
|
|
|
|
|
git_oidmap_size(idx->expected_oids)); |
1171
|
0
|
|
|
|
|
|
return -1; |
1172
|
|
|
|
|
|
|
} |
1173
|
|
|
|
|
|
|
|
1174
|
5
|
|
|
|
|
|
git_vector_sort(&idx->objects); |
1175
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
/* Use the trailer hash as the pack file name to ensure |
1177
|
|
|
|
|
|
|
* files with different contents have different names */ |
1178
|
5
|
|
|
|
|
|
git_oid_cpy(&idx->hash, &trailer_hash); |
1179
|
|
|
|
|
|
|
|
1180
|
5
|
|
|
|
|
|
git_buf_sets(&filename, idx->pack->pack_name); |
1181
|
5
|
|
|
|
|
|
git_buf_shorten(&filename, strlen("pack")); |
1182
|
5
|
|
|
|
|
|
git_buf_puts(&filename, "idx"); |
1183
|
5
|
50
|
|
|
|
|
if (git_buf_oom(&filename)) |
1184
|
0
|
|
|
|
|
|
return -1; |
1185
|
|
|
|
|
|
|
|
1186
|
5
|
50
|
|
|
|
|
if (git_filebuf_open(&index_file, filename.ptr, |
|
|
50
|
|
|
|
|
|
1187
|
|
|
|
|
|
|
GIT_FILEBUF_HASH_CONTENTS | |
1188
|
5
|
|
|
|
|
|
(idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), |
1189
|
|
|
|
|
|
|
idx->mode) < 0) |
1190
|
0
|
|
|
|
|
|
goto on_error; |
1191
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
/* Write out the header */ |
1193
|
5
|
|
|
|
|
|
hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); |
1194
|
5
|
|
|
|
|
|
hdr.idx_version = htonl(2); |
1195
|
5
|
|
|
|
|
|
git_filebuf_write(&index_file, &hdr, sizeof(hdr)); |
1196
|
|
|
|
|
|
|
|
1197
|
|
|
|
|
|
|
/* Write out the fanout table */ |
1198
|
1285
|
100
|
|
|
|
|
for (i = 0; i < 256; ++i) { |
1199
|
1280
|
|
|
|
|
|
uint32_t n = htonl(idx->fanout[i]); |
1200
|
1280
|
|
|
|
|
|
git_filebuf_write(&index_file, &n, sizeof(n)); |
1201
|
|
|
|
|
|
|
} |
1202
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
/* Write out the object names (SHA-1 hashes) */ |
1204
|
50
|
100
|
|
|
|
|
git_vector_foreach(&idx->objects, i, entry) { |
1205
|
45
|
|
|
|
|
|
git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid)); |
1206
|
|
|
|
|
|
|
} |
1207
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
/* Write out the CRC32 values */ |
1209
|
50
|
100
|
|
|
|
|
git_vector_foreach(&idx->objects, i, entry) { |
1210
|
45
|
|
|
|
|
|
git_filebuf_write(&index_file, &entry->crc, sizeof(uint32_t)); |
1211
|
|
|
|
|
|
|
} |
1212
|
|
|
|
|
|
|
|
1213
|
|
|
|
|
|
|
/* Write out the offsets */ |
1214
|
50
|
100
|
|
|
|
|
git_vector_foreach(&idx->objects, i, entry) { |
1215
|
|
|
|
|
|
|
uint32_t n; |
1216
|
|
|
|
|
|
|
|
1217
|
45
|
50
|
|
|
|
|
if (entry->offset == UINT32_MAX) |
1218
|
0
|
|
|
|
|
|
n = htonl(0x80000000 | long_offsets++); |
1219
|
|
|
|
|
|
|
else |
1220
|
45
|
|
|
|
|
|
n = htonl(entry->offset); |
1221
|
|
|
|
|
|
|
|
1222
|
45
|
|
|
|
|
|
git_filebuf_write(&index_file, &n, sizeof(uint32_t)); |
1223
|
|
|
|
|
|
|
} |
1224
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
/* Write out the long offsets */ |
1226
|
50
|
100
|
|
|
|
|
git_vector_foreach(&idx->objects, i, entry) { |
1227
|
|
|
|
|
|
|
uint32_t split[2]; |
1228
|
|
|
|
|
|
|
|
1229
|
45
|
50
|
|
|
|
|
if (entry->offset != UINT32_MAX) |
1230
|
45
|
|
|
|
|
|
continue; |
1231
|
|
|
|
|
|
|
|
1232
|
0
|
|
|
|
|
|
split[0] = htonl(entry->offset_long >> 32); |
1233
|
0
|
|
|
|
|
|
split[1] = htonl(entry->offset_long & 0xffffffff); |
1234
|
|
|
|
|
|
|
|
1235
|
0
|
|
|
|
|
|
git_filebuf_write(&index_file, &split, sizeof(uint32_t) * 2); |
1236
|
|
|
|
|
|
|
} |
1237
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
/* Write out the packfile trailer to the index */ |
1239
|
5
|
50
|
|
|
|
|
if (git_filebuf_write(&index_file, &trailer_hash, GIT_OID_RAWSZ) < 0) |
1240
|
0
|
|
|
|
|
|
goto on_error; |
1241
|
|
|
|
|
|
|
|
1242
|
|
|
|
|
|
|
/* Write out the hash of the idx */ |
1243
|
5
|
50
|
|
|
|
|
if (git_filebuf_hash(&trailer_hash, &index_file) < 0) |
1244
|
0
|
|
|
|
|
|
goto on_error; |
1245
|
|
|
|
|
|
|
|
1246
|
5
|
|
|
|
|
|
git_filebuf_write(&index_file, &trailer_hash, sizeof(git_oid)); |
1247
|
|
|
|
|
|
|
|
1248
|
|
|
|
|
|
|
/* Figure out what the final name should be */ |
1249
|
5
|
50
|
|
|
|
|
if (index_path(&filename, idx, ".idx") < 0) |
1250
|
0
|
|
|
|
|
|
goto on_error; |
1251
|
|
|
|
|
|
|
|
1252
|
|
|
|
|
|
|
/* Commit file */ |
1253
|
5
|
50
|
|
|
|
|
if (git_filebuf_commit_at(&index_file, filename.ptr) < 0) |
1254
|
0
|
|
|
|
|
|
goto on_error; |
1255
|
|
|
|
|
|
|
|
1256
|
5
|
|
|
|
|
|
git_mwindow_free_all(&idx->pack->mwf); |
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
/* Truncate file to undo rounding up to next page_size in append_to_pack */ |
1259
|
5
|
50
|
|
|
|
|
if (p_ftruncate(idx->pack->mwf.fd, idx->pack->mwf.size) < 0) { |
1260
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to truncate pack file '%s'", idx->pack->pack_name); |
1261
|
0
|
|
|
|
|
|
return -1; |
1262
|
|
|
|
|
|
|
} |
1263
|
|
|
|
|
|
|
|
1264
|
5
|
50
|
|
|
|
|
if (idx->do_fsync && p_fsync(idx->pack->mwf.fd) < 0) { |
|
|
0
|
|
|
|
|
|
1265
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to fsync packfile"); |
1266
|
0
|
|
|
|
|
|
goto on_error; |
1267
|
|
|
|
|
|
|
} |
1268
|
|
|
|
|
|
|
|
1269
|
|
|
|
|
|
|
/* We need to close the descriptor here so Windows doesn't choke on commit_at */ |
1270
|
5
|
50
|
|
|
|
|
if (p_close(idx->pack->mwf.fd) < 0) { |
1271
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to close packfile"); |
1272
|
0
|
|
|
|
|
|
goto on_error; |
1273
|
|
|
|
|
|
|
} |
1274
|
|
|
|
|
|
|
|
1275
|
5
|
|
|
|
|
|
idx->pack->mwf.fd = -1; |
1276
|
|
|
|
|
|
|
|
1277
|
5
|
50
|
|
|
|
|
if (index_path(&filename, idx, ".pack") < 0) |
1278
|
0
|
|
|
|
|
|
goto on_error; |
1279
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
/* And don't forget to rename the packfile to its new place. */ |
1281
|
5
|
50
|
|
|
|
|
if (p_rename(idx->pack->pack_name, git_buf_cstr(&filename)) < 0) |
1282
|
0
|
|
|
|
|
|
goto on_error; |
1283
|
|
|
|
|
|
|
|
1284
|
|
|
|
|
|
|
/* And fsync the parent directory if we're asked to. */ |
1285
|
5
|
|
|
|
|
|
if (idx->do_fsync && |
1286
|
0
|
|
|
|
|
|
git_futils_fsync_parent(git_buf_cstr(&filename)) < 0) |
1287
|
0
|
|
|
|
|
|
goto on_error; |
1288
|
|
|
|
|
|
|
|
1289
|
5
|
|
|
|
|
|
idx->pack_committed = 1; |
1290
|
|
|
|
|
|
|
|
1291
|
5
|
|
|
|
|
|
git_buf_dispose(&filename); |
1292
|
5
|
|
|
|
|
|
return 0; |
1293
|
|
|
|
|
|
|
|
1294
|
|
|
|
|
|
|
on_error: |
1295
|
0
|
|
|
|
|
|
git_mwindow_free_all(&idx->pack->mwf); |
1296
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&index_file); |
1297
|
0
|
|
|
|
|
|
git_buf_dispose(&filename); |
1298
|
5
|
|
|
|
|
|
return -1; |
1299
|
|
|
|
|
|
|
} |
1300
|
|
|
|
|
|
|
|
1301
|
5
|
|
|
|
|
|
void git_indexer_free(git_indexer *idx) |
1302
|
|
|
|
|
|
|
{ |
1303
|
|
|
|
|
|
|
const git_oid *key; |
1304
|
|
|
|
|
|
|
git_oid *value; |
1305
|
|
|
|
|
|
|
size_t iter; |
1306
|
|
|
|
|
|
|
|
1307
|
5
|
50
|
|
|
|
|
if (idx == NULL) |
1308
|
0
|
|
|
|
|
|
return; |
1309
|
|
|
|
|
|
|
|
1310
|
5
|
50
|
|
|
|
|
if (idx->have_stream) |
1311
|
0
|
|
|
|
|
|
git_packfile_stream_dispose(&idx->stream); |
1312
|
|
|
|
|
|
|
|
1313
|
5
|
|
|
|
|
|
git_vector_free_deep(&idx->objects); |
1314
|
|
|
|
|
|
|
|
1315
|
5
|
50
|
|
|
|
|
if (idx->pack->idx_cache) { |
1316
|
|
|
|
|
|
|
struct git_pack_entry *pentry; |
1317
|
50
|
100
|
|
|
|
|
git_oidmap_foreach_value(idx->pack->idx_cache, pentry, { |
1318
|
|
|
|
|
|
|
git__free(pentry); |
1319
|
|
|
|
|
|
|
}); |
1320
|
|
|
|
|
|
|
|
1321
|
5
|
|
|
|
|
|
git_oidmap_free(idx->pack->idx_cache); |
1322
|
|
|
|
|
|
|
} |
1323
|
|
|
|
|
|
|
|
1324
|
5
|
|
|
|
|
|
git_vector_free_deep(&idx->deltas); |
1325
|
|
|
|
|
|
|
|
1326
|
5
|
50
|
|
|
|
|
if (!git_mutex_lock(&git__mwindow_mutex)) { |
1327
|
5
|
50
|
|
|
|
|
if (!idx->pack_committed) |
1328
|
0
|
|
|
|
|
|
git_packfile_close(idx->pack, true); |
1329
|
|
|
|
|
|
|
|
1330
|
5
|
|
|
|
|
|
git_packfile_free(idx->pack); |
1331
|
|
|
|
|
|
|
git_mutex_unlock(&git__mwindow_mutex); |
1332
|
|
|
|
|
|
|
} |
1333
|
|
|
|
|
|
|
|
1334
|
5
|
|
|
|
|
|
iter = 0; |
1335
|
5
|
50
|
|
|
|
|
while (git_oidmap_iterate((void **) &value, idx->expected_oids, &iter, &key) == 0) |
1336
|
0
|
|
|
|
|
|
git__free(value); |
1337
|
|
|
|
|
|
|
|
1338
|
5
|
|
|
|
|
|
git_hash_ctx_cleanup(&idx->trailer); |
1339
|
5
|
|
|
|
|
|
git_hash_ctx_cleanup(&idx->hash_ctx); |
1340
|
5
|
|
|
|
|
|
git_buf_dispose(&idx->entry_data); |
1341
|
5
|
|
|
|
|
|
git_oidmap_free(idx->expected_oids); |
1342
|
5
|
|
|
|
|
|
git__free(idx); |
1343
|
|
|
|
|
|
|
} |