filebuf.c 10.4 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6
 */
7
#include <stdarg.h>
8 9 10 11 12

#include "common.h"
#include "filebuf.h"
#include "fileops.h"

13 14
#define GIT_LOCK_FILE_MODE 0644

15 16
static const size_t WRITE_BUFFER_SIZE = (4096 * 2);

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
enum buferr_t {
	BUFERR_OK = 0,
	BUFERR_WRITE,
	BUFERR_ZLIB,
	BUFERR_MEM
};

#define ENSURE_BUF_OK(buf) if ((buf)->last_error != BUFERR_OK) { return -1; }

static int verify_last_error(git_filebuf *file)
{
	switch (file->last_error) {
	case BUFERR_WRITE:
		giterr_set(GITERR_OS, "Failed to write out file");
		return -1;

	case BUFERR_MEM:
		giterr_set_oom();
		return -1;

	case BUFERR_ZLIB:
		giterr_set(GITERR_ZLIB,
			"Buffer error when writing out ZLib data");
		return -1;

	default:
		return 0;
	}
}

47 48
static int lock_file(git_filebuf *file, int flags)
{
49
	if (git_path_exists(file->path_lock) == true) {
50
		if (flags & GIT_FILEBUF_FORCE)
Vicent Marti committed
51
			p_unlink(file->path_lock);
52
		else {
53
			giterr_clear(); /* actual OS error code just confuses */
54 55 56 57
			giterr_set(GITERR_OS,
				"Failed to lock file '%s' for writing", file->path_lock);
			return -1;
		}
58 59
	}

60 61
	/* create path to the file buffer is required */
	if (flags & GIT_FILEBUF_FORCE) {
62
		/* XXX: Should dirmode here be configurable? Or is 0777 always fine? */
63
		file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, GIT_LOCK_FILE_MODE);
64
	} else {
65
		file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE);
66
	}
67 68

	if (file->fd < 0)
69
		return -1;
70

71 72
	file->fd_is_open = true;

73
	if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == true) {
74 75
		git_file source;
		char buffer[2048];
Vicent Marti committed
76
		ssize_t read_bytes;
77

Vicent Marti committed
78
		source = p_open(file->path_original, O_RDONLY);
79 80
		if (source < 0) {
			giterr_set(GITERR_OS,
81 82
				"Failed to open file '%s' for reading",
				file->path_original);
83 84
			return -1;
		}
85

Vicent Marti committed
86
		while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) {
Vicent Marti committed
87
			p_write(file->fd, buffer, read_bytes);
88 89
			if (file->compute_digest)
				git_hash_update(&file->digest, buffer, read_bytes);
90 91
		}

Vicent Marti committed
92
		p_close(source);
Vicent Marti committed
93 94 95 96 97

		if (read_bytes < 0) {
			giterr_set(GITERR_OS, "Failed to read file '%s'", file->path_original);
			return -1;
		}
98 99
	}

100
	return 0;
101 102 103 104
}

void git_filebuf_cleanup(git_filebuf *file)
{
105
	if (file->fd_is_open && file->fd >= 0)
Vicent Marti committed
106
		p_close(file->fd);
107

108
	if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock))
Vicent Marti committed
109
		p_unlink(file->path_lock);
110

111 112 113
	if (file->compute_digest) {
		git_hash_ctx_cleanup(&file->digest);
		file->compute_digest = 0;
114
	}
115

116 117
	if (file->buffer)
		git__free(file->buffer);
118

119 120 121 122 123 124 125 126 127 128
	/* use the presence of z_buf to decide if we need to deflateEnd */
	if (file->z_buf) {
		git__free(file->z_buf);
		deflateEnd(&file->zs);
	}

	if (file->path_original)
		git__free(file->path_original);
	if (file->path_lock)
		git__free(file->path_lock);
129

130 131
	memset(file, 0x0, sizeof(git_filebuf));
	file->fd = -1;
132 133
}

Vicent Marti committed
134
GIT_INLINE(int) flush_buffer(git_filebuf *file)
135
{
Vicent Marti committed
136 137 138 139
	int result = file->write(file, file->buffer, file->buf_pos);
	file->buf_pos = 0;
	return result;
}
140

141 142 143 144 145
int git_filebuf_flush(git_filebuf *file)
{
	return flush_buffer(file);
}

146
static int write_normal(git_filebuf *file, void *source, size_t len)
Vicent Marti committed
147 148
{
	if (len > 0) {
149 150 151 152 153
		if (p_write(file->fd, (void *)source, len) < 0) {
			file->last_error = BUFERR_WRITE;
			return -1;
		}

154 155
		if (file->compute_digest)
			git_hash_update(&file->digest, source, len);
156 157
	}

158
	return 0;
159 160
}

161
static int write_deflate(git_filebuf *file, void *source, size_t len)
Vicent Marti committed
162 163 164 165
{
	z_stream *zs = &file->zs;

	if (len > 0 || file->flush_mode == Z_FINISH) {
166
		zs->next_in = source;
167
		zs->avail_in = (uInt)len;
Vicent Marti committed
168 169

		do {
170
			size_t have;
Vicent Marti committed
171 172

			zs->next_out = file->z_buf;
173
			zs->avail_out = (uInt)file->buf_size;
Vicent Marti committed
174

175 176 177 178
			if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) {
				file->last_error = BUFERR_ZLIB;
				return -1;
			}
Vicent Marti committed
179

180
			have = file->buf_size - (size_t)zs->avail_out;
Vicent Marti committed
181

182 183 184 185
			if (p_write(file->fd, file->z_buf, have) < 0) {
				file->last_error = BUFERR_WRITE;
				return -1;
			}
Vicent Marti committed
186

187
		} while (zs->avail_out == 0);
Vicent Marti committed
188 189 190

		assert(zs->avail_in == 0);

191 192
		if (file->compute_digest)
			git_hash_update(&file->digest, source, len);
Vicent Marti committed
193 194
	}

195
	return 0;
Vicent Marti committed
196 197
}

198 199
int git_filebuf_open(git_filebuf *file, const char *path, int flags)
{
200
	int compression;
201 202
	size_t path_len;

203 204 205 206
	/* opening an already open buffer is a programming error;
	 * assert that this never happens instead of returning
	 * an error code */
	assert(file && path && file->buffer == NULL);
207

208 209
	memset(file, 0x0, sizeof(git_filebuf));

210 211 212
	if (flags & GIT_FILEBUF_DO_NOT_BUFFER)
		file->do_not_buffer = true;

213 214 215
	file->buf_size = WRITE_BUFFER_SIZE;
	file->buf_pos = 0;
	file->fd = -1;
216
	file->last_error = BUFERR_OK;
217

Vicent Marti committed
218
	/* Allocate the main cache buffer */
219 220 221 222
	if (!file->do_not_buffer) {
		file->buffer = git__malloc(file->buf_size);
		GITERR_CHECK_ALLOC(file->buffer);
	}
223

Vicent Marti committed
224 225
	/* If we are hashing on-write, allocate a new hash context */
	if (flags & GIT_FILEBUF_HASH_CONTENTS) {
226
		file->compute_digest = 1;
227

228
		if (git_hash_ctx_init(&file->digest) < 0)
229
			goto cleanup;
230 231
	}

232
	compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT;
233

234 235
	/* If we are deflating on-write, */
	if (compression != 0) {
Vicent Marti committed
236
		/* Initialize the ZLib stream */
237
		if (deflateInit(&file->zs, compression) != Z_OK) {
238
			giterr_set(GITERR_ZLIB, "Failed to initialize zlib");
Vicent Marti committed
239 240
			goto cleanup;
		}
241

Vicent Marti committed
242 243
		/* Allocate the Zlib cache buffer */
		file->z_buf = git__malloc(file->buf_size);
244
		GITERR_CHECK_ALLOC(file->z_buf);
Vicent Marti committed
245 246 247 248 249 250

		/* Never flush */
		file->flush_mode = Z_NO_FLUSH;
		file->write = &write_deflate;
	} else {
		file->write = &write_normal;
251 252
	}

Vicent Marti committed
253 254
	/* If we are writing to a temp file */
	if (flags & GIT_FILEBUF_TEMPORARY) {
255
		git_buf tmp_path = GIT_BUF_INIT;
Vicent Marti committed
256 257

		/* Open the file as temporary for locking */
258
		file->fd = git_futils_mktmp(&tmp_path, path);
259

Vicent Marti committed
260
		if (file->fd < 0) {
261
			git_buf_free(&tmp_path);
Vicent Marti committed
262 263
			goto cleanup;
		}
264
		file->fd_is_open = true;
Vicent Marti committed
265 266 267

		/* No original path */
		file->path_original = NULL;
268
		file->path_lock = git_buf_detach(&tmp_path);
269
		GITERR_CHECK_ALLOC(file->path_lock);
Vicent Marti committed
270 271 272 273 274
	} else {
		path_len = strlen(path);

		/* Save the original path of the file */
		file->path_original = git__strdup(path);
275
		GITERR_CHECK_ALLOC(file->path_original);
Vicent Marti committed
276 277 278

		/* create the locking path by appending ".lock" to the original */
		file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH);
279
		GITERR_CHECK_ALLOC(file->path_lock);
Vicent Marti committed
280 281 282 283 284

		memcpy(file->path_lock, file->path_original, path_len);
		memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);

		/* open the file for locking */
285
		if (lock_file(file, flags) < 0)
Vicent Marti committed
286 287
			goto cleanup;
	}
288

289
	return 0;
290 291 292

cleanup:
	git_filebuf_cleanup(file);
293
	return -1;
294 295 296 297
}

int git_filebuf_hash(git_oid *oid, git_filebuf *file)
{
298
	assert(oid && file && file->compute_digest);
299

300 301 302 303
	flush_buffer(file);

	if (verify_last_error(file) < 0)
		return -1;
304

305 306 307
	git_hash_final(oid, &file->digest);
	git_hash_ctx_cleanup(&file->digest);
	file->compute_digest = 0;
308

309
	return 0;
310 311
}

312
int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode)
Vicent Marti committed
313
{
314
	git__free(file->path_original);
Vicent Marti committed
315
	file->path_original = git__strdup(path);
316
	GITERR_CHECK_ALLOC(file->path_original);
Vicent Marti committed
317

318
	return git_filebuf_commit(file, mode);
Vicent Marti committed
319 320
}

321
int git_filebuf_commit(git_filebuf *file, mode_t mode)
322
{
Vicent Marti committed
323 324
	/* temporary files cannot be committed */
	assert(file && file->path_original);
Vicent Marti committed
325 326

	file->flush_mode = Z_FINISH;
327 328 329 330
	flush_buffer(file);

	if (verify_last_error(file) < 0)
		goto on_error;
331

332
	file->fd_is_open = false;
333

334 335 336 337 338 339 340
	if (p_close(file->fd) < 0) {
		giterr_set(GITERR_OS, "Failed to close file at '%s'", file->path_lock);
		goto on_error;
	}

	file->fd = -1;

341
	if (p_chmod(file->path_lock, mode)) {
342 343
		giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
		goto on_error;
344 345
	}

346 347
	p_unlink(file->path_original);

348 349 350 351
	if (p_rename(file->path_lock, file->path_original) < 0) {
		giterr_set(GITERR_OS, "Failed to rename lockfile to '%s'", file->path_original);
		goto on_error;
	}
352 353

	git_filebuf_cleanup(file);
354 355 356 357 358
	return 0;

on_error:
	git_filebuf_cleanup(file);
	return -1;
359 360
}

Vicent Marti committed
361
GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
362 363 364 365 366
{
	memcpy(file->buffer + file->buf_pos, buf, len);
	file->buf_pos += len;
}

Vicent Marti committed
367
int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
368
{
Vicent Marti committed
369
	const unsigned char *buf = buff;
370

371 372
	ENSURE_BUF_OK(file);

373 374 375
	if (file->do_not_buffer)
		return file->write(file, (void *)buff, len);

376 377 378 379 380 381
	for (;;) {
		size_t space_left = file->buf_size - file->buf_pos;

		/* cache if it's small */
		if (space_left > len) {
			add_to_cache(file, buf, len);
382
			return 0;
383 384
		}

385
		add_to_cache(file, buf, space_left);
386 387
		if (flush_buffer(file) < 0)
			return -1;
388

389 390
		len -= space_left;
		buf += space_left;
391 392 393 394 395 396 397 398 399
	}
}

int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
{
	size_t space_left = file->buf_size - file->buf_pos;

	*buffer = NULL;

400 401 402 403 404 405
	ENSURE_BUF_OK(file);

	if (len > file->buf_size) {
		file->last_error = BUFERR_MEM;
		return -1;
	}
406 407

	if (space_left <= len) {
408 409
		if (flush_buffer(file) < 0)
			return -1;
410 411 412 413 414
	}

	*buffer = (file->buffer + file->buf_pos);
	file->buf_pos += len;

415
	return 0;
416 417
}

418 419 420
int git_filebuf_printf(git_filebuf *file, const char *format, ...)
{
	va_list arglist;
421
	size_t space_left;
422
	int len, res;
423
	char *tmp_buffer;
424

425 426
	ENSURE_BUF_OK(file);

427 428 429 430 431 432 433
	space_left = file->buf_size - file->buf_pos;

	do {
		va_start(arglist, format);
		len = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
		va_end(arglist);

434 435 436 437
		if (len < 0) {
			file->last_error = BUFERR_MEM;
			return -1;
		}
438

439
		if ((size_t)len + 1 <= space_left) {
440
			file->buf_pos += len;
441
			return 0;
442
		}
443

444 445
		if (flush_buffer(file) < 0)
			return -1;
446

447 448
		space_left = file->buf_size - file->buf_pos;

449
	} while ((size_t)len + 1 <= space_left);
450

451
	tmp_buffer = git__malloc(len + 1);
452 453 454 455
	if (!tmp_buffer) {
		file->last_error = BUFERR_MEM;
		return -1;
	}
456 457 458 459 460 461

	va_start(arglist, format);
	len = p_vsnprintf(tmp_buffer, len + 1, format, arglist);
	va_end(arglist);

	if (len < 0) {
462
		git__free(tmp_buffer);
463 464
		file->last_error = BUFERR_MEM;
		return -1;
465 466
	}

467
	res = git_filebuf_write(file, tmp_buffer, len);
468
	git__free(tmp_buffer);
469

470
	return res;
471 472
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file)
{
	int res;
	struct stat st;

	if (file->fd_is_open)
		res = p_fstat(file->fd, &st);
	else
		res = p_stat(file->path_original, &st);

	if (res < 0) {
		giterr_set(GITERR_OS, "Could not get stat info for '%s'",
			file->path_original);
		return res;
	}

	if (mtime)
		*mtime = st.st_mtime;
	if (size)
		*size = (size_t)st.st_size;

	return 0;
}