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 "common.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include |
11
|
|
|
|
|
|
|
#include "git2/object.h" |
12
|
|
|
|
|
|
|
#include "git2/sys/odb_backend.h" |
13
|
|
|
|
|
|
|
#include "futils.h" |
14
|
|
|
|
|
|
|
#include "hash.h" |
15
|
|
|
|
|
|
|
#include "odb.h" |
16
|
|
|
|
|
|
|
#include "delta.h" |
17
|
|
|
|
|
|
|
#include "filebuf.h" |
18
|
|
|
|
|
|
|
#include "object.h" |
19
|
|
|
|
|
|
|
#include "zstream.h" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include "git2/odb_backend.h" |
22
|
|
|
|
|
|
|
#include "git2/types.h" |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
/* maximum possible header length */ |
25
|
|
|
|
|
|
|
#define MAX_HEADER_LEN 64 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
typedef struct { /* object header data */ |
28
|
|
|
|
|
|
|
git_object_t type; /* object type */ |
29
|
|
|
|
|
|
|
size_t size; /* object size */ |
30
|
|
|
|
|
|
|
} obj_hdr; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
typedef struct { |
33
|
|
|
|
|
|
|
git_odb_stream stream; |
34
|
|
|
|
|
|
|
git_filebuf fbuf; |
35
|
|
|
|
|
|
|
} loose_writestream; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
typedef struct { |
38
|
|
|
|
|
|
|
git_odb_stream stream; |
39
|
|
|
|
|
|
|
git_map map; |
40
|
|
|
|
|
|
|
char start[MAX_HEADER_LEN]; |
41
|
|
|
|
|
|
|
size_t start_len; |
42
|
|
|
|
|
|
|
size_t start_read; |
43
|
|
|
|
|
|
|
git_zstream zstream; |
44
|
|
|
|
|
|
|
} loose_readstream; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
typedef struct loose_backend { |
47
|
|
|
|
|
|
|
git_odb_backend parent; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
int object_zlib_level; /** loose object zlib compression level. */ |
50
|
|
|
|
|
|
|
int fsync_object_files; /** loose object file fsync flag. */ |
51
|
|
|
|
|
|
|
mode_t object_file_mode; |
52
|
|
|
|
|
|
|
mode_t object_dir_mode; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
size_t objects_dirlen; |
55
|
|
|
|
|
|
|
char objects_dir[GIT_FLEX_ARRAY]; |
56
|
|
|
|
|
|
|
} loose_backend; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
/* State structure for exploring directories, |
59
|
|
|
|
|
|
|
* in order to locate objects matching a short oid. |
60
|
|
|
|
|
|
|
*/ |
61
|
|
|
|
|
|
|
typedef struct { |
62
|
|
|
|
|
|
|
size_t dir_len; |
63
|
|
|
|
|
|
|
unsigned char short_oid[GIT_OID_HEXSZ]; /* hex formatted oid to match */ |
64
|
|
|
|
|
|
|
size_t short_oid_len; |
65
|
|
|
|
|
|
|
int found; /* number of matching |
66
|
|
|
|
|
|
|
* objects already found */ |
67
|
|
|
|
|
|
|
unsigned char res_oid[GIT_OID_HEXSZ]; /* hex formatted oid of |
68
|
|
|
|
|
|
|
* the object found */ |
69
|
|
|
|
|
|
|
} loose_locate_object_state; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
/*********************************************************** |
73
|
|
|
|
|
|
|
* |
74
|
|
|
|
|
|
|
* MISCELLANEOUS HELPER FUNCTIONS |
75
|
|
|
|
|
|
|
* |
76
|
|
|
|
|
|
|
***********************************************************/ |
77
|
|
|
|
|
|
|
|
78
|
1569
|
|
|
|
|
|
static int object_file_name( |
79
|
|
|
|
|
|
|
git_buf *name, const loose_backend *be, const git_oid *id) |
80
|
|
|
|
|
|
|
{ |
81
|
|
|
|
|
|
|
size_t alloclen; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
/* expand length for object root + 40 hex sha1 chars + 2 * '/' + '\0' */ |
84
|
1569
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, be->objects_dirlen, GIT_OID_HEXSZ); |
|
|
50
|
|
|
|
|
|
85
|
1569
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 3); |
|
|
50
|
|
|
|
|
|
86
|
1569
|
50
|
|
|
|
|
if (git_buf_grow(name, alloclen) < 0) |
87
|
0
|
|
|
|
|
|
return -1; |
88
|
|
|
|
|
|
|
|
89
|
1569
|
|
|
|
|
|
git_buf_set(name, be->objects_dir, be->objects_dirlen); |
90
|
1569
|
|
|
|
|
|
git_path_to_dir(name); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
/* loose object filename: aa/aaa... (41 bytes) */ |
93
|
1569
|
|
|
|
|
|
git_oid_pathfmt(name->ptr + name->size, id); |
94
|
1569
|
|
|
|
|
|
name->size += GIT_OID_HEXSZ + 1; |
95
|
1569
|
|
|
|
|
|
name->ptr[name->size] = '\0'; |
96
|
|
|
|
|
|
|
|
97
|
1569
|
|
|
|
|
|
return 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
157
|
|
|
|
|
|
static int object_mkdir(const git_buf *name, const loose_backend *be) |
101
|
|
|
|
|
|
|
{ |
102
|
157
|
|
|
|
|
|
return git_futils_mkdir_relative( |
103
|
157
|
|
|
|
|
|
name->ptr + be->objects_dirlen, be->objects_dir, be->object_dir_mode, |
104
|
|
|
|
|
|
|
GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR, NULL); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
static int parse_header_packlike( |
108
|
|
|
|
|
|
|
obj_hdr *out, size_t *out_len, const unsigned char *data, size_t len) |
109
|
|
|
|
|
|
|
{ |
110
|
|
|
|
|
|
|
unsigned long c; |
111
|
0
|
|
|
|
|
|
size_t shift, size, used = 0; |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
if (len == 0) |
114
|
0
|
|
|
|
|
|
goto on_error; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
c = data[used++]; |
117
|
0
|
|
|
|
|
|
out->type = (c >> 4) & 7; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
size = c & 15; |
120
|
0
|
|
|
|
|
|
shift = 4; |
121
|
0
|
0
|
|
|
|
|
while (c & 0x80) { |
122
|
0
|
0
|
|
|
|
|
if (len <= used) |
123
|
0
|
|
|
|
|
|
goto on_error; |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
if (sizeof(size_t) * 8 <= shift) |
126
|
0
|
|
|
|
|
|
goto on_error; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
c = data[used++]; |
129
|
0
|
|
|
|
|
|
size += (c & 0x7f) << shift; |
130
|
0
|
|
|
|
|
|
shift += 7; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
out->size = size; |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if (out_len) |
136
|
0
|
|
|
|
|
|
*out_len = used; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return 0; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
on_error: |
141
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, "failed to parse loose object: invalid header"); |
142
|
0
|
|
|
|
|
|
return -1; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
1128
|
|
|
|
|
|
static int parse_header( |
146
|
|
|
|
|
|
|
obj_hdr *out, |
147
|
|
|
|
|
|
|
size_t *out_len, |
148
|
|
|
|
|
|
|
const unsigned char *_data, |
149
|
|
|
|
|
|
|
size_t data_len) |
150
|
|
|
|
|
|
|
{ |
151
|
1128
|
|
|
|
|
|
const char *data = (char *)_data; |
152
|
|
|
|
|
|
|
size_t i, typename_len, size_idx, size_len; |
153
|
|
|
|
|
|
|
int64_t size; |
154
|
|
|
|
|
|
|
|
155
|
1128
|
|
|
|
|
|
*out_len = 0; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
/* find the object type name */ |
158
|
6429
|
50
|
|
|
|
|
for (i = 0, typename_len = 0; i < data_len; i++, typename_len++) { |
159
|
6429
|
100
|
|
|
|
|
if (data[i] == ' ') |
160
|
1128
|
|
|
|
|
|
break; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
1128
|
50
|
|
|
|
|
if (typename_len == data_len) |
164
|
0
|
|
|
|
|
|
goto on_error; |
165
|
|
|
|
|
|
|
|
166
|
1128
|
|
|
|
|
|
out->type = git_object_stringn2type(data, typename_len); |
167
|
|
|
|
|
|
|
|
168
|
1128
|
|
|
|
|
|
size_idx = typename_len + 1; |
169
|
3761
|
50
|
|
|
|
|
for (i = size_idx, size_len = 0; i < data_len; i++, size_len++) { |
170
|
3761
|
100
|
|
|
|
|
if (data[i] == '\0') |
171
|
1128
|
|
|
|
|
|
break; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
1128
|
50
|
|
|
|
|
if (i == data_len) |
175
|
0
|
|
|
|
|
|
goto on_error; |
176
|
|
|
|
|
|
|
|
177
|
1128
|
50
|
|
|
|
|
if (git__strntol64(&size, &data[size_idx], size_len, NULL, 10) < 0 || |
|
|
50
|
|
|
|
|
|
178
|
1128
|
|
|
|
|
|
size < 0) |
179
|
|
|
|
|
|
|
goto on_error; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
if ((uint64_t)size > SIZE_MAX) { |
182
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, "object is larger than available memory"); |
183
|
|
|
|
|
|
|
return -1; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
1128
|
|
|
|
|
|
out->size = (size_t)size; |
187
|
|
|
|
|
|
|
|
188
|
1128
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(out_len, i, 1)) |
|
|
50
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
goto on_error; |
190
|
|
|
|
|
|
|
|
191
|
1128
|
|
|
|
|
|
return 0; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
on_error: |
194
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, "failed to parse loose object: invalid header"); |
195
|
1128
|
|
|
|
|
|
return -1; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
1128
|
|
|
|
|
|
static int is_zlib_compressed_data(unsigned char *data, size_t data_len) |
199
|
|
|
|
|
|
|
{ |
200
|
|
|
|
|
|
|
unsigned int w; |
201
|
|
|
|
|
|
|
|
202
|
1128
|
50
|
|
|
|
|
if (data_len < 2) |
203
|
0
|
|
|
|
|
|
return 0; |
204
|
|
|
|
|
|
|
|
205
|
1128
|
|
|
|
|
|
w = ((unsigned int)(data[0]) << 8) + data[1]; |
206
|
1128
|
50
|
|
|
|
|
return (data[0] & 0x8F) == 0x08 && !(w % 31); |
|
|
50
|
|
|
|
|
|
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
/*********************************************************** |
210
|
|
|
|
|
|
|
* |
211
|
|
|
|
|
|
|
* ODB OBJECT READING & WRITING |
212
|
|
|
|
|
|
|
* |
213
|
|
|
|
|
|
|
* Backend for the public API; read headers and full objects |
214
|
|
|
|
|
|
|
* from the ODB. Write raw data to the ODB. |
215
|
|
|
|
|
|
|
* |
216
|
|
|
|
|
|
|
***********************************************************/ |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
/* |
220
|
|
|
|
|
|
|
* At one point, there was a loose object format that was intended to |
221
|
|
|
|
|
|
|
* mimic the format used in pack-files. This was to allow easy copying |
222
|
|
|
|
|
|
|
* of loose object data into packs. This format is no longer used, but |
223
|
|
|
|
|
|
|
* we must still read it. |
224
|
|
|
|
|
|
|
*/ |
225
|
0
|
|
|
|
|
|
static int read_loose_packlike(git_rawobj *out, git_buf *obj) |
226
|
|
|
|
|
|
|
{ |
227
|
0
|
|
|
|
|
|
git_buf body = GIT_BUF_INIT; |
228
|
|
|
|
|
|
|
const unsigned char *obj_data; |
229
|
|
|
|
|
|
|
obj_hdr hdr; |
230
|
|
|
|
|
|
|
size_t obj_len, head_len, alloc_size; |
231
|
|
|
|
|
|
|
int error; |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
obj_data = (unsigned char *)obj->ptr; |
234
|
0
|
|
|
|
|
|
obj_len = obj->size; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
/* |
237
|
|
|
|
|
|
|
* read the object header, which is an (uncompressed) |
238
|
|
|
|
|
|
|
* binary encoding of the object type and size. |
239
|
|
|
|
|
|
|
*/ |
240
|
0
|
0
|
|
|
|
|
if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0) |
241
|
0
|
|
|
|
|
|
goto done; |
242
|
|
|
|
|
|
|
|
243
|
0
|
0
|
|
|
|
|
if (!git_object_typeisloose(hdr.type) || head_len > obj_len) { |
|
|
0
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object"); |
245
|
0
|
|
|
|
|
|
error = -1; |
246
|
0
|
|
|
|
|
|
goto done; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
obj_data += head_len; |
250
|
0
|
|
|
|
|
|
obj_len -= head_len; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
/* |
253
|
|
|
|
|
|
|
* allocate a buffer and inflate the data into it |
254
|
|
|
|
|
|
|
*/ |
255
|
0
|
0
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) || |
|
|
0
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
git_buf_init(&body, alloc_size) < 0) { |
257
|
0
|
|
|
|
|
|
error = -1; |
258
|
0
|
|
|
|
|
|
goto done; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
0
|
0
|
|
|
|
|
if ((error = git_zstream_inflatebuf(&body, obj_data, obj_len)) < 0) |
262
|
0
|
|
|
|
|
|
goto done; |
263
|
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
out->len = hdr.size; |
265
|
0
|
|
|
|
|
|
out->type = hdr.type; |
266
|
0
|
|
|
|
|
|
out->data = git_buf_detach(&body); |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
done: |
269
|
0
|
|
|
|
|
|
git_buf_dispose(&body); |
270
|
0
|
|
|
|
|
|
return error; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
745
|
|
|
|
|
|
static int read_loose_standard(git_rawobj *out, git_buf *obj) |
274
|
|
|
|
|
|
|
{ |
275
|
745
|
|
|
|
|
|
git_zstream zstream = GIT_ZSTREAM_INIT; |
276
|
745
|
|
|
|
|
|
unsigned char head[MAX_HEADER_LEN], *body = NULL; |
277
|
|
|
|
|
|
|
size_t decompressed, head_len, body_len, alloc_size; |
278
|
|
|
|
|
|
|
obj_hdr hdr; |
279
|
|
|
|
|
|
|
int error; |
280
|
|
|
|
|
|
|
|
281
|
1490
|
50
|
|
|
|
|
if ((error = git_zstream_init(&zstream, GIT_ZSTREAM_INFLATE)) < 0 || |
|
|
50
|
|
|
|
|
|
282
|
745
|
|
|
|
|
|
(error = git_zstream_set_input(&zstream, git_buf_cstr(obj), git_buf_len(obj))) < 0) |
283
|
|
|
|
|
|
|
goto done; |
284
|
|
|
|
|
|
|
|
285
|
745
|
|
|
|
|
|
decompressed = sizeof(head); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
/* |
288
|
|
|
|
|
|
|
* inflate the initial part of the compressed buffer in order to |
289
|
|
|
|
|
|
|
* parse the header; read the largest header possible, then push the |
290
|
|
|
|
|
|
|
* remainder into the body buffer. |
291
|
|
|
|
|
|
|
*/ |
292
|
745
|
50
|
|
|
|
|
if ((error = git_zstream_get_output(head, &decompressed, &zstream)) < 0 || |
|
|
50
|
|
|
|
|
|
293
|
745
|
|
|
|
|
|
(error = parse_header(&hdr, &head_len, head, decompressed)) < 0) |
294
|
|
|
|
|
|
|
goto done; |
295
|
|
|
|
|
|
|
|
296
|
745
|
50
|
|
|
|
|
if (!git_object_typeisloose(hdr.type)) { |
297
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object"); |
298
|
0
|
|
|
|
|
|
error = -1; |
299
|
0
|
|
|
|
|
|
goto done; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
/* |
303
|
|
|
|
|
|
|
* allocate a buffer and inflate the object data into it |
304
|
|
|
|
|
|
|
* (including the initial sequence in the head buffer). |
305
|
|
|
|
|
|
|
*/ |
306
|
745
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) || |
|
|
50
|
|
|
|
|
|
307
|
745
|
|
|
|
|
|
(body = git__malloc(alloc_size)) == NULL) { |
308
|
0
|
|
|
|
|
|
error = -1; |
309
|
0
|
|
|
|
|
|
goto done; |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
|
312
|
745
|
50
|
|
|
|
|
assert(decompressed >= head_len); |
313
|
745
|
|
|
|
|
|
body_len = decompressed - head_len; |
314
|
|
|
|
|
|
|
|
315
|
745
|
100
|
|
|
|
|
if (body_len) |
316
|
730
|
|
|
|
|
|
memcpy(body, head + head_len, body_len); |
317
|
|
|
|
|
|
|
|
318
|
745
|
|
|
|
|
|
decompressed = hdr.size - body_len; |
319
|
745
|
50
|
|
|
|
|
if ((error = git_zstream_get_output(body + body_len, &decompressed, &zstream)) < 0) |
320
|
0
|
|
|
|
|
|
goto done; |
321
|
|
|
|
|
|
|
|
322
|
745
|
50
|
|
|
|
|
if (!git_zstream_done(&zstream)) { |
323
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ZLIB, "failed to finish zlib inflation: stream aborted prematurely"); |
324
|
0
|
|
|
|
|
|
error = -1; |
325
|
0
|
|
|
|
|
|
goto done; |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
745
|
|
|
|
|
|
body[hdr.size] = '\0'; |
329
|
|
|
|
|
|
|
|
330
|
745
|
|
|
|
|
|
out->data = body; |
331
|
745
|
|
|
|
|
|
out->len = hdr.size; |
332
|
745
|
|
|
|
|
|
out->type = hdr.type; |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
done: |
335
|
745
|
50
|
|
|
|
|
if (error < 0) |
336
|
0
|
|
|
|
|
|
git__free(body); |
337
|
|
|
|
|
|
|
|
338
|
745
|
|
|
|
|
|
git_zstream_free(&zstream); |
339
|
745
|
|
|
|
|
|
return error; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
745
|
|
|
|
|
|
static int read_loose(git_rawobj *out, git_buf *loc) |
343
|
|
|
|
|
|
|
{ |
344
|
|
|
|
|
|
|
int error; |
345
|
745
|
|
|
|
|
|
git_buf obj = GIT_BUF_INIT; |
346
|
|
|
|
|
|
|
|
347
|
745
|
50
|
|
|
|
|
assert(out && loc); |
|
|
50
|
|
|
|
|
|
348
|
|
|
|
|
|
|
|
349
|
745
|
50
|
|
|
|
|
if (git_buf_oom(loc)) |
350
|
0
|
|
|
|
|
|
return -1; |
351
|
|
|
|
|
|
|
|
352
|
745
|
|
|
|
|
|
out->data = NULL; |
353
|
745
|
|
|
|
|
|
out->len = 0; |
354
|
745
|
|
|
|
|
|
out->type = GIT_OBJECT_INVALID; |
355
|
|
|
|
|
|
|
|
356
|
745
|
50
|
|
|
|
|
if ((error = git_futils_readbuffer(&obj, loc->ptr)) < 0) |
357
|
0
|
|
|
|
|
|
goto done; |
358
|
|
|
|
|
|
|
|
359
|
745
|
50
|
|
|
|
|
if (!is_zlib_compressed_data((unsigned char *)obj.ptr, obj.size)) |
360
|
0
|
|
|
|
|
|
error = read_loose_packlike(out, &obj); |
361
|
|
|
|
|
|
|
else |
362
|
745
|
|
|
|
|
|
error = read_loose_standard(out, &obj); |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
done: |
365
|
745
|
|
|
|
|
|
git_buf_dispose(&obj); |
366
|
745
|
|
|
|
|
|
return error; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
0
|
|
|
|
|
|
static int read_header_loose_packlike( |
370
|
|
|
|
|
|
|
git_rawobj *out, const unsigned char *data, size_t len) |
371
|
|
|
|
|
|
|
{ |
372
|
|
|
|
|
|
|
obj_hdr hdr; |
373
|
|
|
|
|
|
|
size_t header_len; |
374
|
|
|
|
|
|
|
int error; |
375
|
|
|
|
|
|
|
|
376
|
0
|
0
|
|
|
|
|
if ((error = parse_header_packlike(&hdr, &header_len, data, len)) < 0) |
377
|
0
|
|
|
|
|
|
return error; |
378
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
out->len = hdr.size; |
380
|
0
|
|
|
|
|
|
out->type = hdr.type; |
381
|
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
return error; |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
|
385
|
383
|
|
|
|
|
|
static int read_header_loose_standard( |
386
|
|
|
|
|
|
|
git_rawobj *out, const unsigned char *data, size_t len) |
387
|
|
|
|
|
|
|
{ |
388
|
383
|
|
|
|
|
|
git_zstream zs = GIT_ZSTREAM_INIT; |
389
|
|
|
|
|
|
|
obj_hdr hdr; |
390
|
|
|
|
|
|
|
unsigned char inflated[MAX_HEADER_LEN]; |
391
|
383
|
|
|
|
|
|
size_t header_len, inflated_len = sizeof(inflated); |
392
|
|
|
|
|
|
|
int error; |
393
|
|
|
|
|
|
|
|
394
|
383
|
50
|
|
|
|
|
if ((error = git_zstream_init(&zs, GIT_ZSTREAM_INFLATE)) < 0 || |
|
|
50
|
|
|
|
|
|
395
|
383
|
50
|
|
|
|
|
(error = git_zstream_set_input(&zs, data, len)) < 0 || |
396
|
383
|
50
|
|
|
|
|
(error = git_zstream_get_output_chunk(inflated, &inflated_len, &zs)) < 0 || |
397
|
383
|
|
|
|
|
|
(error = parse_header(&hdr, &header_len, inflated, inflated_len)) < 0) |
398
|
|
|
|
|
|
|
goto done; |
399
|
|
|
|
|
|
|
|
400
|
383
|
|
|
|
|
|
out->len = hdr.size; |
401
|
383
|
|
|
|
|
|
out->type = hdr.type; |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
done: |
404
|
383
|
|
|
|
|
|
git_zstream_free(&zs); |
405
|
383
|
|
|
|
|
|
return error; |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
383
|
|
|
|
|
|
static int read_header_loose(git_rawobj *out, git_buf *loc) |
409
|
|
|
|
|
|
|
{ |
410
|
|
|
|
|
|
|
unsigned char obj[1024]; |
411
|
|
|
|
|
|
|
ssize_t obj_len; |
412
|
|
|
|
|
|
|
int fd, error; |
413
|
|
|
|
|
|
|
|
414
|
383
|
50
|
|
|
|
|
assert(out && loc); |
|
|
50
|
|
|
|
|
|
415
|
|
|
|
|
|
|
|
416
|
383
|
50
|
|
|
|
|
if (git_buf_oom(loc)) |
417
|
0
|
|
|
|
|
|
return -1; |
418
|
|
|
|
|
|
|
|
419
|
383
|
|
|
|
|
|
out->data = NULL; |
420
|
|
|
|
|
|
|
|
421
|
383
|
50
|
|
|
|
|
if ((error = fd = git_futils_open_ro(loc->ptr)) < 0) |
422
|
0
|
|
|
|
|
|
goto done; |
423
|
|
|
|
|
|
|
|
424
|
383
|
50
|
|
|
|
|
if ((obj_len = p_read(fd, obj, sizeof(obj))) < 0) { |
425
|
0
|
|
|
|
|
|
error = (int)obj_len; |
426
|
0
|
|
|
|
|
|
goto done; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
383
|
50
|
|
|
|
|
if (!is_zlib_compressed_data(obj, (size_t)obj_len)) |
430
|
0
|
|
|
|
|
|
error = read_header_loose_packlike(out, obj, (size_t)obj_len); |
431
|
|
|
|
|
|
|
else |
432
|
383
|
|
|
|
|
|
error = read_header_loose_standard(out, obj, (size_t)obj_len); |
433
|
|
|
|
|
|
|
|
434
|
383
|
50
|
|
|
|
|
if (!error && !git_object_typeisloose(out->type)) { |
|
|
50
|
|
|
|
|
|
435
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ZLIB, "failed to read loose object header"); |
436
|
0
|
|
|
|
|
|
error = -1; |
437
|
0
|
|
|
|
|
|
goto done; |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
done: |
441
|
383
|
50
|
|
|
|
|
if (fd >= 0) |
442
|
383
|
|
|
|
|
|
p_close(fd); |
443
|
383
|
|
|
|
|
|
return error; |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
1126
|
|
|
|
|
|
static int locate_object( |
447
|
|
|
|
|
|
|
git_buf *object_location, |
448
|
|
|
|
|
|
|
loose_backend *backend, |
449
|
|
|
|
|
|
|
const git_oid *oid) |
450
|
|
|
|
|
|
|
{ |
451
|
1126
|
|
|
|
|
|
int error = object_file_name(object_location, backend, oid); |
452
|
|
|
|
|
|
|
|
453
|
1126
|
50
|
|
|
|
|
if (!error && !git_path_exists(object_location->ptr)) |
|
|
100
|
|
|
|
|
|
454
|
2
|
|
|
|
|
|
return GIT_ENOTFOUND; |
455
|
|
|
|
|
|
|
|
456
|
1124
|
|
|
|
|
|
return error; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
/* Explore an entry of a directory and see if it matches a short oid */ |
460
|
10
|
|
|
|
|
|
static int fn_locate_object_short_oid(void *state, git_buf *pathbuf) { |
461
|
10
|
|
|
|
|
|
loose_locate_object_state *sstate = (loose_locate_object_state *)state; |
462
|
|
|
|
|
|
|
|
463
|
10
|
50
|
|
|
|
|
if (git_buf_len(pathbuf) - sstate->dir_len != GIT_OID_HEXSZ - 2) { |
464
|
|
|
|
|
|
|
/* Entry cannot be an object. Continue to next entry */ |
465
|
0
|
|
|
|
|
|
return 0; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
10
|
50
|
|
|
|
|
if (git_path_isdir(pathbuf->ptr) == false) { |
469
|
|
|
|
|
|
|
/* We are already in the directory matching the 2 first hex characters, |
470
|
|
|
|
|
|
|
* compare the first ncmp characters of the oids */ |
471
|
10
|
100
|
|
|
|
|
if (!memcmp(sstate->short_oid + 2, |
472
|
10
|
|
|
|
|
|
(unsigned char *)pathbuf->ptr + sstate->dir_len, |
473
|
10
|
|
|
|
|
|
sstate->short_oid_len - 2)) { |
474
|
|
|
|
|
|
|
|
475
|
8
|
50
|
|
|
|
|
if (!sstate->found) { |
476
|
8
|
|
|
|
|
|
sstate->res_oid[0] = sstate->short_oid[0]; |
477
|
8
|
|
|
|
|
|
sstate->res_oid[1] = sstate->short_oid[1]; |
478
|
8
|
|
|
|
|
|
memcpy(sstate->res_oid+2, pathbuf->ptr+sstate->dir_len, GIT_OID_HEXSZ-2); |
479
|
|
|
|
|
|
|
} |
480
|
8
|
|
|
|
|
|
sstate->found++; |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
} |
483
|
|
|
|
|
|
|
|
484
|
10
|
50
|
|
|
|
|
if (sstate->found > 1) |
485
|
0
|
|
|
|
|
|
return GIT_EAMBIGUOUS; |
486
|
|
|
|
|
|
|
|
487
|
10
|
|
|
|
|
|
return 0; |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
/* Locate an object matching a given short oid */ |
491
|
16
|
|
|
|
|
|
static int locate_object_short_oid( |
492
|
|
|
|
|
|
|
git_buf *object_location, |
493
|
|
|
|
|
|
|
git_oid *res_oid, |
494
|
|
|
|
|
|
|
loose_backend *backend, |
495
|
|
|
|
|
|
|
const git_oid *short_oid, |
496
|
|
|
|
|
|
|
size_t len) |
497
|
|
|
|
|
|
|
{ |
498
|
16
|
|
|
|
|
|
char *objects_dir = backend->objects_dir; |
499
|
16
|
|
|
|
|
|
size_t dir_len = strlen(objects_dir), alloc_len; |
500
|
|
|
|
|
|
|
loose_locate_object_state state; |
501
|
|
|
|
|
|
|
int error; |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
/* prealloc memory for OBJ_DIR/xx/xx..38x..xx */ |
504
|
16
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, dir_len, GIT_OID_HEXSZ); |
|
|
50
|
|
|
|
|
|
505
|
16
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 3); |
|
|
50
|
|
|
|
|
|
506
|
16
|
50
|
|
|
|
|
if (git_buf_grow(object_location, alloc_len) < 0) |
507
|
0
|
|
|
|
|
|
return -1; |
508
|
|
|
|
|
|
|
|
509
|
16
|
|
|
|
|
|
git_buf_set(object_location, objects_dir, dir_len); |
510
|
16
|
|
|
|
|
|
git_path_to_dir(object_location); |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
/* save adjusted position at end of dir so it can be restored later */ |
513
|
16
|
|
|
|
|
|
dir_len = git_buf_len(object_location); |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
/* Convert raw oid to hex formatted oid */ |
516
|
16
|
|
|
|
|
|
git_oid_fmt((char *)state.short_oid, short_oid); |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
/* Explore OBJ_DIR/xx/ where xx is the beginning of hex formatted short oid */ |
519
|
16
|
50
|
|
|
|
|
if (git_buf_put(object_location, (char *)state.short_oid, 3) < 0) |
520
|
0
|
|
|
|
|
|
return -1; |
521
|
16
|
|
|
|
|
|
object_location->ptr[object_location->size - 1] = '/'; |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
/* Check that directory exists */ |
524
|
16
|
100
|
|
|
|
|
if (git_path_isdir(object_location->ptr) == false) |
525
|
6
|
|
|
|
|
|
return git_odb__error_notfound("no matching loose object for prefix", |
526
|
|
|
|
|
|
|
short_oid, len); |
527
|
|
|
|
|
|
|
|
528
|
10
|
|
|
|
|
|
state.dir_len = git_buf_len(object_location); |
529
|
10
|
|
|
|
|
|
state.short_oid_len = len; |
530
|
10
|
|
|
|
|
|
state.found = 0; |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
/* Explore directory to find a unique object matching short_oid */ |
533
|
10
|
|
|
|
|
|
error = git_path_direach( |
534
|
|
|
|
|
|
|
object_location, 0, fn_locate_object_short_oid, &state); |
535
|
10
|
50
|
|
|
|
|
if (error < 0 && error != GIT_EAMBIGUOUS) |
|
|
0
|
|
|
|
|
|
536
|
0
|
|
|
|
|
|
return error; |
537
|
|
|
|
|
|
|
|
538
|
10
|
100
|
|
|
|
|
if (!state.found) |
539
|
2
|
|
|
|
|
|
return git_odb__error_notfound("no matching loose object for prefix", |
540
|
|
|
|
|
|
|
short_oid, len); |
541
|
|
|
|
|
|
|
|
542
|
8
|
50
|
|
|
|
|
if (state.found > 1) |
543
|
0
|
|
|
|
|
|
return git_odb__error_ambiguous("multiple matches in loose objects"); |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
/* Convert obtained hex formatted oid to raw */ |
546
|
8
|
|
|
|
|
|
error = git_oid_fromstr(res_oid, (char *)state.res_oid); |
547
|
8
|
50
|
|
|
|
|
if (error) |
548
|
0
|
|
|
|
|
|
return error; |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
/* Update the location according to the oid obtained */ |
551
|
8
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, dir_len, GIT_OID_HEXSZ); |
|
|
50
|
|
|
|
|
|
552
|
8
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); |
|
|
50
|
|
|
|
|
|
553
|
|
|
|
|
|
|
|
554
|
8
|
|
|
|
|
|
git_buf_truncate(object_location, dir_len); |
555
|
8
|
50
|
|
|
|
|
if (git_buf_grow(object_location, alloc_len) < 0) |
556
|
0
|
|
|
|
|
|
return -1; |
557
|
|
|
|
|
|
|
|
558
|
8
|
|
|
|
|
|
git_oid_pathfmt(object_location->ptr + dir_len, res_oid); |
559
|
|
|
|
|
|
|
|
560
|
8
|
|
|
|
|
|
object_location->size += GIT_OID_HEXSZ + 1; |
561
|
8
|
|
|
|
|
|
object_location->ptr[object_location->size] = '\0'; |
562
|
|
|
|
|
|
|
|
563
|
16
|
|
|
|
|
|
return 0; |
564
|
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
/*********************************************************** |
575
|
|
|
|
|
|
|
* |
576
|
|
|
|
|
|
|
* LOOSE BACKEND PUBLIC API |
577
|
|
|
|
|
|
|
* |
578
|
|
|
|
|
|
|
* Implement the git_odb_backend API calls |
579
|
|
|
|
|
|
|
* |
580
|
|
|
|
|
|
|
***********************************************************/ |
581
|
|
|
|
|
|
|
|
582
|
383
|
|
|
|
|
|
static int loose_backend__read_header(size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) |
583
|
|
|
|
|
|
|
{ |
584
|
383
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
585
|
|
|
|
|
|
|
git_rawobj raw; |
586
|
|
|
|
|
|
|
int error; |
587
|
|
|
|
|
|
|
|
588
|
383
|
50
|
|
|
|
|
assert(backend && oid); |
|
|
50
|
|
|
|
|
|
589
|
|
|
|
|
|
|
|
590
|
383
|
|
|
|
|
|
raw.len = 0; |
591
|
383
|
|
|
|
|
|
raw.type = GIT_OBJECT_INVALID; |
592
|
|
|
|
|
|
|
|
593
|
383
|
50
|
|
|
|
|
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) { |
594
|
0
|
|
|
|
|
|
error = git_odb__error_notfound("no matching loose object", |
595
|
|
|
|
|
|
|
oid, GIT_OID_HEXSZ); |
596
|
383
|
50
|
|
|
|
|
} else if ((error = read_header_loose(&raw, &object_path)) == 0) { |
597
|
383
|
|
|
|
|
|
*len_p = raw.len; |
598
|
383
|
|
|
|
|
|
*type_p = raw.type; |
599
|
|
|
|
|
|
|
} |
600
|
|
|
|
|
|
|
|
601
|
383
|
|
|
|
|
|
git_buf_dispose(&object_path); |
602
|
|
|
|
|
|
|
|
603
|
383
|
|
|
|
|
|
return error; |
604
|
|
|
|
|
|
|
} |
605
|
|
|
|
|
|
|
|
606
|
739
|
|
|
|
|
|
static int loose_backend__read(void **buffer_p, size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid) |
607
|
|
|
|
|
|
|
{ |
608
|
739
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
609
|
|
|
|
|
|
|
git_rawobj raw; |
610
|
739
|
|
|
|
|
|
int error = 0; |
611
|
|
|
|
|
|
|
|
612
|
739
|
50
|
|
|
|
|
assert(backend && oid); |
|
|
50
|
|
|
|
|
|
613
|
|
|
|
|
|
|
|
614
|
739
|
100
|
|
|
|
|
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) { |
615
|
2
|
|
|
|
|
|
error = git_odb__error_notfound("no matching loose object", |
616
|
|
|
|
|
|
|
oid, GIT_OID_HEXSZ); |
617
|
737
|
50
|
|
|
|
|
} else if ((error = read_loose(&raw, &object_path)) == 0) { |
618
|
737
|
|
|
|
|
|
*buffer_p = raw.data; |
619
|
737
|
|
|
|
|
|
*len_p = raw.len; |
620
|
737
|
|
|
|
|
|
*type_p = raw.type; |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
|
623
|
739
|
|
|
|
|
|
git_buf_dispose(&object_path); |
624
|
|
|
|
|
|
|
|
625
|
739
|
|
|
|
|
|
return error; |
626
|
|
|
|
|
|
|
} |
627
|
|
|
|
|
|
|
|
628
|
138
|
|
|
|
|
|
static int loose_backend__read_prefix( |
629
|
|
|
|
|
|
|
git_oid *out_oid, |
630
|
|
|
|
|
|
|
void **buffer_p, |
631
|
|
|
|
|
|
|
size_t *len_p, |
632
|
|
|
|
|
|
|
git_object_t *type_p, |
633
|
|
|
|
|
|
|
git_odb_backend *backend, |
634
|
|
|
|
|
|
|
const git_oid *short_oid, |
635
|
|
|
|
|
|
|
size_t len) |
636
|
|
|
|
|
|
|
{ |
637
|
138
|
|
|
|
|
|
int error = 0; |
638
|
|
|
|
|
|
|
|
639
|
138
|
50
|
|
|
|
|
assert(len >= GIT_OID_MINPREFIXLEN && len <= GIT_OID_HEXSZ); |
|
|
50
|
|
|
|
|
|
640
|
|
|
|
|
|
|
|
641
|
138
|
100
|
|
|
|
|
if (len == GIT_OID_HEXSZ) { |
642
|
|
|
|
|
|
|
/* We can fall back to regular read method */ |
643
|
122
|
|
|
|
|
|
error = loose_backend__read(buffer_p, len_p, type_p, backend, short_oid); |
644
|
122
|
50
|
|
|
|
|
if (!error) |
645
|
122
|
|
|
|
|
|
git_oid_cpy(out_oid, short_oid); |
646
|
|
|
|
|
|
|
} else { |
647
|
16
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
648
|
|
|
|
|
|
|
git_rawobj raw; |
649
|
|
|
|
|
|
|
|
650
|
16
|
50
|
|
|
|
|
assert(backend && short_oid); |
|
|
50
|
|
|
|
|
|
651
|
|
|
|
|
|
|
|
652
|
16
|
100
|
|
|
|
|
if ((error = locate_object_short_oid(&object_path, out_oid, |
653
|
8
|
50
|
|
|
|
|
(loose_backend *)backend, short_oid, len)) == 0 && |
654
|
|
|
|
|
|
|
(error = read_loose(&raw, &object_path)) == 0) |
655
|
|
|
|
|
|
|
{ |
656
|
8
|
|
|
|
|
|
*buffer_p = raw.data; |
657
|
8
|
|
|
|
|
|
*len_p = raw.len; |
658
|
8
|
|
|
|
|
|
*type_p = raw.type; |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
|
661
|
16
|
|
|
|
|
|
git_buf_dispose(&object_path); |
662
|
|
|
|
|
|
|
} |
663
|
|
|
|
|
|
|
|
664
|
138
|
|
|
|
|
|
return error; |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
|
667
|
4
|
|
|
|
|
|
static int loose_backend__exists(git_odb_backend *backend, const git_oid *oid) |
668
|
|
|
|
|
|
|
{ |
669
|
4
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
670
|
|
|
|
|
|
|
int error; |
671
|
|
|
|
|
|
|
|
672
|
4
|
50
|
|
|
|
|
assert(backend && oid); |
|
|
50
|
|
|
|
|
|
673
|
|
|
|
|
|
|
|
674
|
4
|
|
|
|
|
|
error = locate_object(&object_path, (loose_backend *)backend, oid); |
675
|
|
|
|
|
|
|
|
676
|
4
|
|
|
|
|
|
git_buf_dispose(&object_path); |
677
|
|
|
|
|
|
|
|
678
|
4
|
|
|
|
|
|
return !error; |
679
|
|
|
|
|
|
|
} |
680
|
|
|
|
|
|
|
|
681
|
0
|
|
|
|
|
|
static int loose_backend__exists_prefix( |
682
|
|
|
|
|
|
|
git_oid *out, git_odb_backend *backend, const git_oid *short_id, size_t len) |
683
|
|
|
|
|
|
|
{ |
684
|
0
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
685
|
|
|
|
|
|
|
int error; |
686
|
|
|
|
|
|
|
|
687
|
0
|
0
|
|
|
|
|
assert(backend && out && short_id && len >= GIT_OID_MINPREFIXLEN); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
688
|
|
|
|
|
|
|
|
689
|
0
|
|
|
|
|
|
error = locate_object_short_oid( |
690
|
|
|
|
|
|
|
&object_path, out, (loose_backend *)backend, short_id, len); |
691
|
|
|
|
|
|
|
|
692
|
0
|
|
|
|
|
|
git_buf_dispose(&object_path); |
693
|
|
|
|
|
|
|
|
694
|
0
|
|
|
|
|
|
return error; |
695
|
|
|
|
|
|
|
} |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
struct foreach_state { |
698
|
|
|
|
|
|
|
size_t dir_len; |
699
|
|
|
|
|
|
|
git_odb_foreach_cb cb; |
700
|
|
|
|
|
|
|
void *data; |
701
|
|
|
|
|
|
|
}; |
702
|
|
|
|
|
|
|
|
703
|
121
|
|
|
|
|
|
GIT_INLINE(int) filename_to_oid(git_oid *oid, const char *ptr) |
704
|
|
|
|
|
|
|
{ |
705
|
121
|
|
|
|
|
|
int v, i = 0; |
706
|
121
|
50
|
|
|
|
|
if (strlen(ptr) != GIT_OID_HEXSZ+1) |
707
|
0
|
|
|
|
|
|
return -1; |
708
|
|
|
|
|
|
|
|
709
|
121
|
50
|
|
|
|
|
if (ptr[2] != '/') { |
710
|
0
|
|
|
|
|
|
return -1; |
711
|
|
|
|
|
|
|
} |
712
|
|
|
|
|
|
|
|
713
|
121
|
|
|
|
|
|
v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i+1]); |
714
|
121
|
50
|
|
|
|
|
if (v < 0) |
715
|
0
|
|
|
|
|
|
return -1; |
716
|
|
|
|
|
|
|
|
717
|
121
|
|
|
|
|
|
oid->id[0] = (unsigned char) v; |
718
|
|
|
|
|
|
|
|
719
|
121
|
|
|
|
|
|
ptr += 3; |
720
|
2420
|
100
|
|
|
|
|
for (i = 0; i < 38; i += 2) { |
721
|
2299
|
|
|
|
|
|
v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i + 1]); |
722
|
2299
|
50
|
|
|
|
|
if (v < 0) |
723
|
0
|
|
|
|
|
|
return -1; |
724
|
|
|
|
|
|
|
|
725
|
2299
|
|
|
|
|
|
oid->id[1 + i/2] = (unsigned char) v; |
726
|
|
|
|
|
|
|
} |
727
|
|
|
|
|
|
|
|
728
|
121
|
|
|
|
|
|
return 0; |
729
|
|
|
|
|
|
|
} |
730
|
|
|
|
|
|
|
|
731
|
121
|
|
|
|
|
|
static int foreach_object_dir_cb(void *_state, git_buf *path) |
732
|
|
|
|
|
|
|
{ |
733
|
|
|
|
|
|
|
git_oid oid; |
734
|
121
|
|
|
|
|
|
struct foreach_state *state = (struct foreach_state *) _state; |
735
|
|
|
|
|
|
|
|
736
|
121
|
50
|
|
|
|
|
if (filename_to_oid(&oid, path->ptr + state->dir_len) < 0) |
737
|
0
|
|
|
|
|
|
return 0; |
738
|
|
|
|
|
|
|
|
739
|
121
|
|
|
|
|
|
return git_error_set_after_callback_function( |
740
|
121
|
|
|
|
|
|
state->cb(&oid, state->data), "git_odb_foreach"); |
741
|
|
|
|
|
|
|
} |
742
|
|
|
|
|
|
|
|
743
|
101
|
|
|
|
|
|
static int foreach_cb(void *_state, git_buf *path) |
744
|
|
|
|
|
|
|
{ |
745
|
101
|
|
|
|
|
|
struct foreach_state *state = (struct foreach_state *) _state; |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
/* non-dir is some stray file, ignore it */ |
748
|
101
|
50
|
|
|
|
|
if (!git_path_isdir(git_buf_cstr(path))) |
749
|
0
|
|
|
|
|
|
return 0; |
750
|
|
|
|
|
|
|
|
751
|
101
|
|
|
|
|
|
return git_path_direach(path, 0, foreach_object_dir_cb, state); |
752
|
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
|
754
|
1
|
|
|
|
|
|
static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data) |
755
|
|
|
|
|
|
|
{ |
756
|
|
|
|
|
|
|
char *objects_dir; |
757
|
|
|
|
|
|
|
int error; |
758
|
1
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
759
|
|
|
|
|
|
|
struct foreach_state state; |
760
|
1
|
|
|
|
|
|
loose_backend *backend = (loose_backend *) _backend; |
761
|
|
|
|
|
|
|
|
762
|
1
|
50
|
|
|
|
|
assert(backend && cb); |
|
|
50
|
|
|
|
|
|
763
|
|
|
|
|
|
|
|
764
|
1
|
|
|
|
|
|
objects_dir = backend->objects_dir; |
765
|
|
|
|
|
|
|
|
766
|
1
|
|
|
|
|
|
git_buf_sets(&buf, objects_dir); |
767
|
1
|
|
|
|
|
|
git_path_to_dir(&buf); |
768
|
1
|
50
|
|
|
|
|
if (git_buf_oom(&buf)) |
769
|
0
|
|
|
|
|
|
return -1; |
770
|
|
|
|
|
|
|
|
771
|
1
|
|
|
|
|
|
memset(&state, 0, sizeof(state)); |
772
|
1
|
|
|
|
|
|
state.cb = cb; |
773
|
1
|
|
|
|
|
|
state.data = data; |
774
|
1
|
|
|
|
|
|
state.dir_len = git_buf_len(&buf); |
775
|
|
|
|
|
|
|
|
776
|
1
|
|
|
|
|
|
error = git_path_direach(&buf, 0, foreach_cb, &state); |
777
|
|
|
|
|
|
|
|
778
|
1
|
|
|
|
|
|
git_buf_dispose(&buf); |
779
|
|
|
|
|
|
|
|
780
|
1
|
|
|
|
|
|
return error; |
781
|
|
|
|
|
|
|
} |
782
|
|
|
|
|
|
|
|
783
|
43
|
|
|
|
|
|
static int loose_backend__writestream_finalize(git_odb_stream *_stream, const git_oid *oid) |
784
|
|
|
|
|
|
|
{ |
785
|
43
|
|
|
|
|
|
loose_writestream *stream = (loose_writestream *)_stream; |
786
|
43
|
|
|
|
|
|
loose_backend *backend = (loose_backend *)_stream->backend; |
787
|
43
|
|
|
|
|
|
git_buf final_path = GIT_BUF_INIT; |
788
|
43
|
|
|
|
|
|
int error = 0; |
789
|
|
|
|
|
|
|
|
790
|
86
|
|
|
|
|
|
if (object_file_name(&final_path, backend, oid) < 0 || |
791
|
43
|
|
|
|
|
|
object_mkdir(&final_path, backend) < 0) |
792
|
0
|
|
|
|
|
|
error = -1; |
793
|
|
|
|
|
|
|
else |
794
|
43
|
|
|
|
|
|
error = git_filebuf_commit_at( |
795
|
43
|
|
|
|
|
|
&stream->fbuf, final_path.ptr); |
796
|
|
|
|
|
|
|
|
797
|
43
|
|
|
|
|
|
git_buf_dispose(&final_path); |
798
|
|
|
|
|
|
|
|
799
|
43
|
|
|
|
|
|
return error; |
800
|
|
|
|
|
|
|
} |
801
|
|
|
|
|
|
|
|
802
|
125
|
|
|
|
|
|
static int loose_backend__writestream_write(git_odb_stream *_stream, const char *data, size_t len) |
803
|
|
|
|
|
|
|
{ |
804
|
125
|
|
|
|
|
|
loose_writestream *stream = (loose_writestream *)_stream; |
805
|
125
|
|
|
|
|
|
return git_filebuf_write(&stream->fbuf, data, len); |
806
|
|
|
|
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
63
|
|
|
|
|
|
static void loose_backend__writestream_free(git_odb_stream *_stream) |
809
|
|
|
|
|
|
|
{ |
810
|
63
|
|
|
|
|
|
loose_writestream *stream = (loose_writestream *)_stream; |
811
|
|
|
|
|
|
|
|
812
|
63
|
|
|
|
|
|
git_filebuf_cleanup(&stream->fbuf); |
813
|
63
|
|
|
|
|
|
git__free(stream); |
814
|
63
|
|
|
|
|
|
} |
815
|
|
|
|
|
|
|
|
816
|
177
|
|
|
|
|
|
static int filebuf_flags(loose_backend *backend) |
817
|
|
|
|
|
|
|
{ |
818
|
177
|
|
|
|
|
|
int flags = GIT_FILEBUF_TEMPORARY | |
819
|
177
|
|
|
|
|
|
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT); |
820
|
|
|
|
|
|
|
|
821
|
177
|
50
|
|
|
|
|
if (backend->fsync_object_files || git_repository__fsync_gitdir) |
|
|
50
|
|
|
|
|
|
822
|
0
|
|
|
|
|
|
flags |= GIT_FILEBUF_FSYNC; |
823
|
|
|
|
|
|
|
|
824
|
177
|
|
|
|
|
|
return flags; |
825
|
|
|
|
|
|
|
} |
826
|
|
|
|
|
|
|
|
827
|
63
|
|
|
|
|
|
static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_object_size_t length, git_object_t type) |
828
|
|
|
|
|
|
|
{ |
829
|
|
|
|
|
|
|
loose_backend *backend; |
830
|
63
|
|
|
|
|
|
loose_writestream *stream = NULL; |
831
|
|
|
|
|
|
|
char hdr[MAX_HEADER_LEN]; |
832
|
63
|
|
|
|
|
|
git_buf tmp_path = GIT_BUF_INIT; |
833
|
|
|
|
|
|
|
size_t hdrlen; |
834
|
|
|
|
|
|
|
int error; |
835
|
|
|
|
|
|
|
|
836
|
63
|
50
|
|
|
|
|
assert(_backend); |
837
|
|
|
|
|
|
|
|
838
|
63
|
|
|
|
|
|
backend = (loose_backend *)_backend; |
839
|
63
|
|
|
|
|
|
*stream_out = NULL; |
840
|
|
|
|
|
|
|
|
841
|
63
|
50
|
|
|
|
|
if ((error = git_odb__format_object_header(&hdrlen, |
842
|
|
|
|
|
|
|
hdr, sizeof(hdr), length, type)) < 0) |
843
|
0
|
|
|
|
|
|
return error; |
844
|
|
|
|
|
|
|
|
845
|
63
|
|
|
|
|
|
stream = git__calloc(1, sizeof(loose_writestream)); |
846
|
63
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(stream); |
847
|
|
|
|
|
|
|
|
848
|
63
|
|
|
|
|
|
stream->stream.backend = _backend; |
849
|
63
|
|
|
|
|
|
stream->stream.read = NULL; /* read only */ |
850
|
63
|
|
|
|
|
|
stream->stream.write = &loose_backend__writestream_write; |
851
|
63
|
|
|
|
|
|
stream->stream.finalize_write = &loose_backend__writestream_finalize; |
852
|
63
|
|
|
|
|
|
stream->stream.free = &loose_backend__writestream_free; |
853
|
63
|
|
|
|
|
|
stream->stream.mode = GIT_STREAM_WRONLY; |
854
|
|
|
|
|
|
|
|
855
|
126
|
|
|
|
|
|
if (git_buf_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 || |
856
|
63
|
|
|
|
|
|
git_filebuf_open(&stream->fbuf, tmp_path.ptr, filebuf_flags(backend), |
857
|
63
|
50
|
|
|
|
|
backend->object_file_mode) < 0 || |
858
|
63
|
|
|
|
|
|
stream->stream.write((git_odb_stream *)stream, hdr, hdrlen) < 0) |
859
|
|
|
|
|
|
|
{ |
860
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&stream->fbuf); |
861
|
0
|
|
|
|
|
|
git__free(stream); |
862
|
0
|
|
|
|
|
|
stream = NULL; |
863
|
|
|
|
|
|
|
} |
864
|
63
|
|
|
|
|
|
git_buf_dispose(&tmp_path); |
865
|
63
|
|
|
|
|
|
*stream_out = (git_odb_stream *)stream; |
866
|
|
|
|
|
|
|
|
867
|
63
|
50
|
|
|
|
|
return !stream ? -1 : 0; |
868
|
|
|
|
|
|
|
} |
869
|
|
|
|
|
|
|
|
870
|
0
|
|
|
|
|
|
static int loose_backend__readstream_read( |
871
|
|
|
|
|
|
|
git_odb_stream *_stream, |
872
|
|
|
|
|
|
|
char *buffer, |
873
|
|
|
|
|
|
|
size_t buffer_len) |
874
|
|
|
|
|
|
|
{ |
875
|
0
|
|
|
|
|
|
loose_readstream *stream = (loose_readstream *)_stream; |
876
|
0
|
|
|
|
|
|
size_t start_remain = stream->start_len - stream->start_read; |
877
|
0
|
|
|
|
|
|
int total = 0, error; |
878
|
|
|
|
|
|
|
|
879
|
0
|
|
|
|
|
|
buffer_len = min(buffer_len, INT_MAX); |
880
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
/* |
882
|
|
|
|
|
|
|
* if we read more than just the header in the initial read, play |
883
|
|
|
|
|
|
|
* that back for the caller. |
884
|
|
|
|
|
|
|
*/ |
885
|
0
|
0
|
|
|
|
|
if (start_remain && buffer_len) { |
|
|
0
|
|
|
|
|
|
886
|
0
|
|
|
|
|
|
size_t chunk = min(start_remain, buffer_len); |
887
|
0
|
|
|
|
|
|
memcpy(buffer, stream->start + stream->start_read, chunk); |
888
|
|
|
|
|
|
|
|
889
|
0
|
|
|
|
|
|
buffer += chunk; |
890
|
0
|
|
|
|
|
|
stream->start_read += chunk; |
891
|
|
|
|
|
|
|
|
892
|
0
|
|
|
|
|
|
total += (int)chunk; |
893
|
0
|
|
|
|
|
|
buffer_len -= chunk; |
894
|
|
|
|
|
|
|
} |
895
|
|
|
|
|
|
|
|
896
|
0
|
0
|
|
|
|
|
if (buffer_len) { |
897
|
0
|
|
|
|
|
|
size_t chunk = buffer_len; |
898
|
|
|
|
|
|
|
|
899
|
0
|
0
|
|
|
|
|
if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0) |
900
|
0
|
|
|
|
|
|
return error; |
901
|
|
|
|
|
|
|
|
902
|
0
|
|
|
|
|
|
total += (int)chunk; |
903
|
|
|
|
|
|
|
} |
904
|
|
|
|
|
|
|
|
905
|
0
|
|
|
|
|
|
return (int)total; |
906
|
|
|
|
|
|
|
} |
907
|
|
|
|
|
|
|
|
908
|
0
|
|
|
|
|
|
static void loose_backend__readstream_free(git_odb_stream *_stream) |
909
|
|
|
|
|
|
|
{ |
910
|
0
|
|
|
|
|
|
loose_readstream *stream = (loose_readstream *)_stream; |
911
|
|
|
|
|
|
|
|
912
|
0
|
|
|
|
|
|
git_futils_mmap_free(&stream->map); |
913
|
0
|
|
|
|
|
|
git_zstream_free(&stream->zstream); |
914
|
0
|
|
|
|
|
|
git__free(stream); |
915
|
0
|
|
|
|
|
|
} |
916
|
|
|
|
|
|
|
|
917
|
0
|
|
|
|
|
|
static int loose_backend__readstream_packlike( |
918
|
|
|
|
|
|
|
obj_hdr *hdr, |
919
|
|
|
|
|
|
|
loose_readstream *stream) |
920
|
|
|
|
|
|
|
{ |
921
|
|
|
|
|
|
|
const unsigned char *data; |
922
|
|
|
|
|
|
|
size_t data_len, head_len; |
923
|
|
|
|
|
|
|
int error; |
924
|
|
|
|
|
|
|
|
925
|
0
|
|
|
|
|
|
data = stream->map.data; |
926
|
0
|
|
|
|
|
|
data_len = stream->map.len; |
927
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
/* |
929
|
|
|
|
|
|
|
* read the object header, which is an (uncompressed) |
930
|
|
|
|
|
|
|
* binary encoding of the object type and size. |
931
|
|
|
|
|
|
|
*/ |
932
|
0
|
0
|
|
|
|
|
if ((error = parse_header_packlike(hdr, &head_len, data, data_len)) < 0) |
933
|
0
|
|
|
|
|
|
return error; |
934
|
|
|
|
|
|
|
|
935
|
0
|
0
|
|
|
|
|
if (!git_object_typeisloose(hdr->type)) { |
936
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object"); |
937
|
0
|
|
|
|
|
|
return -1; |
938
|
|
|
|
|
|
|
} |
939
|
|
|
|
|
|
|
|
940
|
0
|
|
|
|
|
|
return git_zstream_set_input(&stream->zstream, |
941
|
|
|
|
|
|
|
data + head_len, data_len - head_len); |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
0
|
|
|
|
|
|
static int loose_backend__readstream_standard( |
945
|
|
|
|
|
|
|
obj_hdr *hdr, |
946
|
|
|
|
|
|
|
loose_readstream *stream) |
947
|
|
|
|
|
|
|
{ |
948
|
|
|
|
|
|
|
unsigned char head[MAX_HEADER_LEN]; |
949
|
|
|
|
|
|
|
size_t init, head_len; |
950
|
|
|
|
|
|
|
int error; |
951
|
|
|
|
|
|
|
|
952
|
0
|
0
|
|
|
|
|
if ((error = git_zstream_set_input(&stream->zstream, |
953
|
0
|
|
|
|
|
|
stream->map.data, stream->map.len)) < 0) |
954
|
0
|
|
|
|
|
|
return error; |
955
|
|
|
|
|
|
|
|
956
|
0
|
|
|
|
|
|
init = sizeof(head); |
957
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
/* |
959
|
|
|
|
|
|
|
* inflate the initial part of the compressed buffer in order to |
960
|
|
|
|
|
|
|
* parse the header; read the largest header possible, then store |
961
|
|
|
|
|
|
|
* it in the `start` field of the stream object. |
962
|
|
|
|
|
|
|
*/ |
963
|
0
|
0
|
|
|
|
|
if ((error = git_zstream_get_output(head, &init, &stream->zstream)) < 0 || |
|
|
0
|
|
|
|
|
|
964
|
0
|
|
|
|
|
|
(error = parse_header(hdr, &head_len, head, init)) < 0) |
965
|
0
|
|
|
|
|
|
return error; |
966
|
|
|
|
|
|
|
|
967
|
0
|
0
|
|
|
|
|
if (!git_object_typeisloose(hdr->type)) { |
968
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object"); |
969
|
0
|
|
|
|
|
|
return -1; |
970
|
|
|
|
|
|
|
} |
971
|
|
|
|
|
|
|
|
972
|
0
|
0
|
|
|
|
|
if (init > head_len) { |
973
|
0
|
|
|
|
|
|
stream->start_len = init - head_len; |
974
|
0
|
|
|
|
|
|
memcpy(stream->start, head + head_len, init - head_len); |
975
|
|
|
|
|
|
|
} |
976
|
|
|
|
|
|
|
|
977
|
0
|
|
|
|
|
|
return 0; |
978
|
|
|
|
|
|
|
} |
979
|
|
|
|
|
|
|
|
980
|
0
|
|
|
|
|
|
static int loose_backend__readstream( |
981
|
|
|
|
|
|
|
git_odb_stream **stream_out, |
982
|
|
|
|
|
|
|
size_t *len_out, |
983
|
|
|
|
|
|
|
git_object_t *type_out, |
984
|
|
|
|
|
|
|
git_odb_backend *_backend, |
985
|
|
|
|
|
|
|
const git_oid *oid) |
986
|
|
|
|
|
|
|
{ |
987
|
|
|
|
|
|
|
loose_backend *backend; |
988
|
0
|
|
|
|
|
|
loose_readstream *stream = NULL; |
989
|
0
|
|
|
|
|
|
git_hash_ctx *hash_ctx = NULL; |
990
|
0
|
|
|
|
|
|
git_buf object_path = GIT_BUF_INIT; |
991
|
|
|
|
|
|
|
obj_hdr hdr; |
992
|
0
|
|
|
|
|
|
int error = 0; |
993
|
|
|
|
|
|
|
|
994
|
0
|
0
|
|
|
|
|
assert(stream_out && len_out && type_out && _backend && oid); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
995
|
|
|
|
|
|
|
|
996
|
0
|
|
|
|
|
|
backend = (loose_backend *)_backend; |
997
|
0
|
|
|
|
|
|
*stream_out = NULL; |
998
|
0
|
|
|
|
|
|
*len_out = 0; |
999
|
0
|
|
|
|
|
|
*type_out = GIT_OBJECT_INVALID; |
1000
|
|
|
|
|
|
|
|
1001
|
0
|
0
|
|
|
|
|
if (locate_object(&object_path, backend, oid) < 0) { |
1002
|
0
|
|
|
|
|
|
error = git_odb__error_notfound("no matching loose object", |
1003
|
|
|
|
|
|
|
oid, GIT_OID_HEXSZ); |
1004
|
0
|
|
|
|
|
|
goto done; |
1005
|
|
|
|
|
|
|
} |
1006
|
|
|
|
|
|
|
|
1007
|
0
|
|
|
|
|
|
stream = git__calloc(1, sizeof(loose_readstream)); |
1008
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(stream); |
1009
|
|
|
|
|
|
|
|
1010
|
0
|
|
|
|
|
|
hash_ctx = git__malloc(sizeof(git_hash_ctx)); |
1011
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(hash_ctx); |
1012
|
|
|
|
|
|
|
|
1013
|
0
|
0
|
|
|
|
|
if ((error = git_hash_ctx_init(hash_ctx)) < 0 || |
|
|
0
|
|
|
|
|
|
1014
|
0
|
0
|
|
|
|
|
(error = git_futils_mmap_ro_file(&stream->map, object_path.ptr)) < 0 || |
1015
|
0
|
|
|
|
|
|
(error = git_zstream_init(&stream->zstream, GIT_ZSTREAM_INFLATE)) < 0) |
1016
|
|
|
|
|
|
|
goto done; |
1017
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
/* check for a packlike loose object */ |
1019
|
0
|
0
|
|
|
|
|
if (!is_zlib_compressed_data(stream->map.data, stream->map.len)) |
1020
|
0
|
|
|
|
|
|
error = loose_backend__readstream_packlike(&hdr, stream); |
1021
|
|
|
|
|
|
|
else |
1022
|
0
|
|
|
|
|
|
error = loose_backend__readstream_standard(&hdr, stream); |
1023
|
|
|
|
|
|
|
|
1024
|
0
|
0
|
|
|
|
|
if (error < 0) |
1025
|
0
|
|
|
|
|
|
goto done; |
1026
|
|
|
|
|
|
|
|
1027
|
0
|
|
|
|
|
|
stream->stream.backend = _backend; |
1028
|
0
|
|
|
|
|
|
stream->stream.hash_ctx = hash_ctx; |
1029
|
0
|
|
|
|
|
|
stream->stream.read = &loose_backend__readstream_read; |
1030
|
0
|
|
|
|
|
|
stream->stream.free = &loose_backend__readstream_free; |
1031
|
|
|
|
|
|
|
|
1032
|
0
|
|
|
|
|
|
*stream_out = (git_odb_stream *)stream; |
1033
|
0
|
|
|
|
|
|
*len_out = hdr.size; |
1034
|
0
|
|
|
|
|
|
*type_out = hdr.type; |
1035
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
done: |
1037
|
0
|
0
|
|
|
|
|
if (error < 0) { |
1038
|
0
|
0
|
|
|
|
|
if (stream) { |
1039
|
0
|
|
|
|
|
|
git_futils_mmap_free(&stream->map); |
1040
|
0
|
|
|
|
|
|
git_zstream_free(&stream->zstream); |
1041
|
0
|
|
|
|
|
|
git__free(stream); |
1042
|
|
|
|
|
|
|
} |
1043
|
0
|
0
|
|
|
|
|
if (hash_ctx) { |
1044
|
0
|
|
|
|
|
|
git_hash_ctx_cleanup(hash_ctx); |
1045
|
0
|
|
|
|
|
|
git__free(hash_ctx); |
1046
|
|
|
|
|
|
|
} |
1047
|
|
|
|
|
|
|
} |
1048
|
|
|
|
|
|
|
|
1049
|
0
|
|
|
|
|
|
git_buf_dispose(&object_path); |
1050
|
0
|
|
|
|
|
|
return error; |
1051
|
|
|
|
|
|
|
} |
1052
|
|
|
|
|
|
|
|
1053
|
114
|
|
|
|
|
|
static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_object_t type) |
1054
|
|
|
|
|
|
|
{ |
1055
|
114
|
|
|
|
|
|
int error = 0; |
1056
|
114
|
|
|
|
|
|
git_buf final_path = GIT_BUF_INIT; |
1057
|
|
|
|
|
|
|
char header[MAX_HEADER_LEN]; |
1058
|
|
|
|
|
|
|
size_t header_len; |
1059
|
114
|
|
|
|
|
|
git_filebuf fbuf = GIT_FILEBUF_INIT; |
1060
|
|
|
|
|
|
|
loose_backend *backend; |
1061
|
|
|
|
|
|
|
|
1062
|
114
|
|
|
|
|
|
backend = (loose_backend *)_backend; |
1063
|
|
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
/* prepare the header for the file */ |
1065
|
114
|
50
|
|
|
|
|
if ((error = git_odb__format_object_header(&header_len, |
1066
|
|
|
|
|
|
|
header, sizeof(header), len, type)) < 0) |
1067
|
0
|
|
|
|
|
|
goto cleanup; |
1068
|
|
|
|
|
|
|
|
1069
|
228
|
|
|
|
|
|
if (git_buf_joinpath(&final_path, backend->objects_dir, "tmp_object") < 0 || |
1070
|
114
|
|
|
|
|
|
git_filebuf_open(&fbuf, final_path.ptr, filebuf_flags(backend), |
1071
|
|
|
|
|
|
|
backend->object_file_mode) < 0) |
1072
|
|
|
|
|
|
|
{ |
1073
|
0
|
|
|
|
|
|
error = -1; |
1074
|
0
|
|
|
|
|
|
goto cleanup; |
1075
|
|
|
|
|
|
|
} |
1076
|
|
|
|
|
|
|
|
1077
|
114
|
|
|
|
|
|
git_filebuf_write(&fbuf, header, header_len); |
1078
|
114
|
|
|
|
|
|
git_filebuf_write(&fbuf, data, len); |
1079
|
|
|
|
|
|
|
|
1080
|
228
|
|
|
|
|
|
if (object_file_name(&final_path, backend, oid) < 0 || |
1081
|
228
|
50
|
|
|
|
|
object_mkdir(&final_path, backend) < 0 || |
1082
|
114
|
|
|
|
|
|
git_filebuf_commit_at(&fbuf, final_path.ptr) < 0) |
1083
|
0
|
|
|
|
|
|
error = -1; |
1084
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
cleanup: |
1086
|
114
|
50
|
|
|
|
|
if (error < 0) |
1087
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&fbuf); |
1088
|
114
|
|
|
|
|
|
git_buf_dispose(&final_path); |
1089
|
114
|
|
|
|
|
|
return error; |
1090
|
|
|
|
|
|
|
} |
1091
|
|
|
|
|
|
|
|
1092
|
286
|
|
|
|
|
|
static int loose_backend__freshen( |
1093
|
|
|
|
|
|
|
git_odb_backend *_backend, |
1094
|
|
|
|
|
|
|
const git_oid *oid) |
1095
|
|
|
|
|
|
|
{ |
1096
|
286
|
|
|
|
|
|
loose_backend *backend = (loose_backend *)_backend; |
1097
|
286
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
1098
|
|
|
|
|
|
|
int error; |
1099
|
|
|
|
|
|
|
|
1100
|
286
|
50
|
|
|
|
|
if (object_file_name(&path, backend, oid) < 0) |
1101
|
0
|
|
|
|
|
|
return -1; |
1102
|
|
|
|
|
|
|
|
1103
|
286
|
|
|
|
|
|
error = git_futils_touch(path.ptr, NULL); |
1104
|
286
|
|
|
|
|
|
git_buf_dispose(&path); |
1105
|
|
|
|
|
|
|
|
1106
|
286
|
|
|
|
|
|
return error; |
1107
|
|
|
|
|
|
|
} |
1108
|
|
|
|
|
|
|
|
1109
|
36
|
|
|
|
|
|
static void loose_backend__free(git_odb_backend *_backend) |
1110
|
|
|
|
|
|
|
{ |
1111
|
|
|
|
|
|
|
loose_backend *backend; |
1112
|
36
|
50
|
|
|
|
|
assert(_backend); |
1113
|
36
|
|
|
|
|
|
backend = (loose_backend *)_backend; |
1114
|
|
|
|
|
|
|
|
1115
|
36
|
|
|
|
|
|
git__free(backend); |
1116
|
36
|
|
|
|
|
|
} |
1117
|
|
|
|
|
|
|
|
1118
|
36
|
|
|
|
|
|
int git_odb_backend_loose( |
1119
|
|
|
|
|
|
|
git_odb_backend **backend_out, |
1120
|
|
|
|
|
|
|
const char *objects_dir, |
1121
|
|
|
|
|
|
|
int compression_level, |
1122
|
|
|
|
|
|
|
int do_fsync, |
1123
|
|
|
|
|
|
|
unsigned int dir_mode, |
1124
|
|
|
|
|
|
|
unsigned int file_mode) |
1125
|
|
|
|
|
|
|
{ |
1126
|
|
|
|
|
|
|
loose_backend *backend; |
1127
|
|
|
|
|
|
|
size_t objects_dirlen, alloclen; |
1128
|
|
|
|
|
|
|
|
1129
|
36
|
50
|
|
|
|
|
assert(backend_out && objects_dir); |
|
|
50
|
|
|
|
|
|
1130
|
|
|
|
|
|
|
|
1131
|
36
|
|
|
|
|
|
objects_dirlen = strlen(objects_dir); |
1132
|
|
|
|
|
|
|
|
1133
|
36
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(loose_backend), objects_dirlen); |
|
|
50
|
|
|
|
|
|
1134
|
36
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 2); |
|
|
50
|
|
|
|
|
|
1135
|
36
|
|
|
|
|
|
backend = git__calloc(1, alloclen); |
1136
|
36
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(backend); |
1137
|
|
|
|
|
|
|
|
1138
|
36
|
|
|
|
|
|
backend->parent.version = GIT_ODB_BACKEND_VERSION; |
1139
|
36
|
|
|
|
|
|
backend->objects_dirlen = objects_dirlen; |
1140
|
36
|
|
|
|
|
|
memcpy(backend->objects_dir, objects_dir, objects_dirlen); |
1141
|
36
|
100
|
|
|
|
|
if (backend->objects_dir[backend->objects_dirlen - 1] != '/') |
1142
|
3
|
|
|
|
|
|
backend->objects_dir[backend->objects_dirlen++] = '/'; |
1143
|
|
|
|
|
|
|
|
1144
|
36
|
100
|
|
|
|
|
if (compression_level < 0) |
1145
|
35
|
|
|
|
|
|
compression_level = Z_BEST_SPEED; |
1146
|
|
|
|
|
|
|
|
1147
|
36
|
50
|
|
|
|
|
if (dir_mode == 0) |
1148
|
36
|
|
|
|
|
|
dir_mode = GIT_OBJECT_DIR_MODE; |
1149
|
|
|
|
|
|
|
|
1150
|
36
|
50
|
|
|
|
|
if (file_mode == 0) |
1151
|
36
|
|
|
|
|
|
file_mode = GIT_OBJECT_FILE_MODE; |
1152
|
|
|
|
|
|
|
|
1153
|
36
|
|
|
|
|
|
backend->object_zlib_level = compression_level; |
1154
|
36
|
|
|
|
|
|
backend->fsync_object_files = do_fsync; |
1155
|
36
|
|
|
|
|
|
backend->object_dir_mode = dir_mode; |
1156
|
36
|
|
|
|
|
|
backend->object_file_mode = file_mode; |
1157
|
|
|
|
|
|
|
|
1158
|
36
|
|
|
|
|
|
backend->parent.read = &loose_backend__read; |
1159
|
36
|
|
|
|
|
|
backend->parent.write = &loose_backend__write; |
1160
|
36
|
|
|
|
|
|
backend->parent.read_prefix = &loose_backend__read_prefix; |
1161
|
36
|
|
|
|
|
|
backend->parent.read_header = &loose_backend__read_header; |
1162
|
36
|
|
|
|
|
|
backend->parent.writestream = &loose_backend__writestream; |
1163
|
36
|
|
|
|
|
|
backend->parent.readstream = &loose_backend__readstream; |
1164
|
36
|
|
|
|
|
|
backend->parent.exists = &loose_backend__exists; |
1165
|
36
|
|
|
|
|
|
backend->parent.exists_prefix = &loose_backend__exists_prefix; |
1166
|
36
|
|
|
|
|
|
backend->parent.foreach = &loose_backend__foreach; |
1167
|
36
|
|
|
|
|
|
backend->parent.freshen = &loose_backend__freshen; |
1168
|
36
|
|
|
|
|
|
backend->parent.free = &loose_backend__free; |
1169
|
|
|
|
|
|
|
|
1170
|
36
|
|
|
|
|
|
*backend_out = (git_odb_backend *)backend; |
1171
|
36
|
|
|
|
|
|
return 0; |
1172
|
|
|
|
|
|
|
} |