odb.c 6.38 KB
Newer Older
1
/*
2 3 4
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
5
 *
6 7 8 9 10 11 12 13
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
14
 *
15 16 17 18
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
19
 *
20 21 22 23
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
24 25
 */

26
#include "common.h"
27 28
#include "git2/zlib.h"
#include "git2/object.h"
29
#include "fileops.h"
30
#include "hash.h"
31
#include "odb.h"
Vicent Marti committed
32
#include "delta-apply.h"
33

34
#include "git2/odb_backend.h"
35

36
static int format_object_header(char *hdr, size_t n, git_rawobj *obj)
37
{
38
	const char *type_str = git_object_type2string(obj->type);
39
	int len = snprintf(hdr, n, "%s %"PRIuZ, type_str, obj->len);
40

41 42
	assert(len > 0);             /* otherwise snprintf() is broken  */
	assert(((size_t) len) < n);  /* otherwise the caller is broken! */
43

44
	if (len < 0 || ((size_t) len) >= n)
45 46 47 48
		return GIT_ERROR;
	return len+1;
}

49
int git_odb__hash_obj(git_oid *id, char *hdr, size_t n, int *len, git_rawobj *obj)
50 51 52 53
{
	git_buf_vec vec[2];
	int  hdrlen;

Ramsay Jones committed
54
	assert(id && hdr && len && obj);
55

56
	if (!git_object_typeisloose(obj->type))
57 58 59 60 61
		return GIT_ERROR;

	if (!obj->data && obj->len != 0)
		return GIT_ERROR;

Ramsay Jones committed
62
	if ((hdrlen = format_object_header(hdr, n, obj)) < 0)
63 64
		return GIT_ERROR;

Ramsay Jones committed
65 66
	*len = hdrlen;

67 68 69 70 71 72 73 74 75 76
	vec[0].data = hdr;
	vec[0].len  = hdrlen;
	vec[1].data = obj->data;
	vec[1].len  = obj->len;

	git_hash_vec(id, vec, 2);

	return GIT_SUCCESS;
}

77 78 79 80 81 82
void git_rawobj_close(git_rawobj *obj)
{
	free(obj->data);
	obj->data = NULL;
}

83
int git_rawobj_hash(git_oid *id, git_rawobj *obj)
Ramsay Jones committed
84 85 86 87 88 89
{
	char hdr[64];
	int  hdrlen;

	assert(id && obj);

90
	return git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, obj);
91 92
}

93
int git_odb__inflate_buffer(void *in, size_t inlen, void *out, size_t outlen)
94
{
95
	z_stream zs;
96 97
	int status = Z_OK;

98
	memset(&zs, 0x0, sizeof(zs));
99

100 101
	zs.next_out  = out;
	zs.avail_out = outlen;
102

103 104
	zs.next_in  = in;
	zs.avail_in = inlen;
105

106 107
	if (inflateInit(&zs) < Z_OK)
		return GIT_ERROR;
108 109 110 111 112 113

	while (status == Z_OK)
		status = inflate(&zs, Z_FINISH);

	inflateEnd(&zs);

Vicent Marti committed
114
	if ((status != Z_STREAM_END) /*|| (zs.avail_in != 0) */)
115 116
		return GIT_ERROR;

117
	if (zs.total_out != outlen)
118 119 120 121 122 123 124 125
		return GIT_ERROR;

	return GIT_SUCCESS;
}




126

127 128 129 130 131 132 133
/***********************************************************
 *
 * OBJECT DATABASE PUBLIC API
 *
 * Public calls for the ODB functionality
 *
 ***********************************************************/
134

135 136 137 138
int backend_sort_cmp(const void *a, const void *b)
{
	const git_odb_backend *backend_a = *(const git_odb_backend **)(a);
	const git_odb_backend *backend_b = *(const git_odb_backend **)(b);
139

140
	return (backend_b->priority - backend_a->priority);
141 142
}

143
int git_odb_new(git_odb **out)
144
{
145 146 147
	git_odb *db = git__calloc(1, sizeof(*db));
	if (!db)
		return GIT_ENOMEM;
148

149 150 151 152
	if (git_vector_init(&db->backends, 4, backend_sort_cmp, NULL) < 0) {
		free(db);
		return GIT_ENOMEM;
	}
153

154
	*out = db;
155 156 157
	return GIT_SUCCESS;
}

158
int git_odb_add_backend(git_odb *odb, git_odb_backend *backend)
Ramsay Jones committed
159
{
160
	assert(odb && backend);
Ramsay Jones committed
161

162 163
	if (backend->odb != NULL && backend->odb != odb)
		return GIT_EBUSY;
Ramsay Jones committed
164

165
	backend->odb = odb;
Ramsay Jones committed
166

167 168
	if (git_vector_insert(&odb->backends, backend) < 0)
		return GIT_ENOMEM;
Ramsay Jones committed
169

170
	git_vector_sort(&odb->backends);
Ramsay Jones committed
171 172 173 174
	return GIT_SUCCESS;
}


175
int git_odb_open(git_odb **out, const char *objects_dir)
Ramsay Jones committed
176
{
177 178 179
	git_odb *db;
	git_odb_backend *loose, *packed;
	int error;
Ramsay Jones committed
180

181 182
	if ((error = git_odb_new(&db)) < 0)
		return error;
Ramsay Jones committed
183

184 185 186 187 188
	/* add the loose object backend */
	if (git_odb_backend_loose(&loose, objects_dir) == 0) {
		error = git_odb_add_backend(db, loose);
		if (error < 0)
			goto cleanup;
Ramsay Jones committed
189 190
	}

191 192 193 194 195
	/* add the packed file backend */
	if (git_odb_backend_pack(&packed, objects_dir) == 0) {
		error = git_odb_add_backend(db, packed);
		if (error < 0)
			goto cleanup;
Ramsay Jones committed
196 197
	}

198 199
	/* TODO: add altenernates as new backends;
	 * how elevant is that? very elegant. */
Ramsay Jones committed
200

201
	*out = db;
Ramsay Jones committed
202 203
	return GIT_SUCCESS;

204 205 206 207
cleanup:
	git_odb_close(db);
	return error;
}
Ramsay Jones committed
208

209
void git_odb_close(git_odb *db)
210
{
211
	unsigned int i;
212

213 214
	if (db == NULL)
		return;
215

216 217
	for (i = 0; i < db->backends.length; ++i) {
		git_odb_backend *b = git_vector_get(&db->backends, i);
218

219 220
		if (b->free) b->free(b);
		else free(b);
221 222
	}

223 224
	git_vector_free(&db->backends);
	free(db);
225 226
}

227
int git_odb_exists(git_odb *db, const git_oid *id)
228
{
229 230
	unsigned int i;
	int found = 0;
231

232
	assert(db && id);
233

234 235
	for (i = 0; i < db->backends.length && !found; ++i) {
		git_odb_backend *b = git_vector_get(&db->backends, i);
236

237 238
		if (b->exists != NULL)
			found = b->exists(b, id);
239 240
	}

241
	return found;
242 243
}

244
int git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id)
245
{
246 247
	unsigned int i;
	int error = GIT_ENOTFOUND;
248

249
	assert(out && db && id);
250

251 252
	for (i = 0; i < db->backends.length && error < 0; ++i) {
		git_odb_backend *b = git_vector_get(&db->backends, i);
253

254 255
		if (b->read_header != NULL)
			error = b->read_header(out, b, id);
256 257
	}

258 259 260 261 262 263 264
	/*
	 * no backend could read only the header.
	 * try reading the whole object and freeing the contents
	 */
	if (error < 0) {
		error = git_odb_read(out, db, id);
		git_rawobj_close(out);
265 266
	}

267
	return error;
268 269
}

270
int git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id)
271
{
272 273
	unsigned int i;
	int error = GIT_ENOTFOUND;
274

275
	assert(out && db && id);
276

277 278 279 280 281
	for (i = 0; i < db->backends.length && error < 0; ++i) {
		git_odb_backend *b = git_vector_get(&db->backends, i);

		assert(b->read != NULL);
		error = b->read(out, b, id);
282
	}
283 284

	return error;
285 286
}

287
int git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj)
288
{
289 290
	unsigned int i;
	int error = GIT_ERROR;
291

292
	assert(obj && db && id);
293

294 295
	for (i = 0; i < db->backends.length && error < 0; ++i) {
		git_odb_backend *b = git_vector_get(&db->backends, i);
296

297 298
		if (b->write != NULL)
			error = b->write(id, b, obj);
299 300 301
	}

	return error;
302 303
}