Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2012-05-19 03:46:11 +0400
committerVicent Marti <tanoku@gmail.com>2012-05-19 03:46:11 +0400
commit5b9fac39d8a76b9139667c26a63e6b3f204b3977 (patch)
treee6ba28025f92c16563c4ffa8bc60b95f17d69691 /src/filebuf.c
parent7ef9f1b5606c2672105ecbbf34c022a71ef212fe (diff)
parentad5df35a47d56c3d716d7a56eac4aeb611987c11 (diff)
Merge branch 'development'v0.17.0
Conflicts: .travis.yml
Diffstat (limited to 'src/filebuf.c')
-rw-r--r--src/filebuf.c245
1 files changed, 148 insertions, 97 deletions
diff --git a/src/filebuf.c b/src/filebuf.c
index 447d8a089..6538aea66 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 the libgit2 contributors
+ * Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
@@ -14,13 +14,46 @@
static const size_t WRITE_BUFFER_SIZE = (4096 * 2);
+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;
+ }
+}
+
static int lock_file(git_filebuf *file, int flags)
{
- if (git_path_exists(file->path_lock) == 0) {
+ if (git_path_exists(file->path_lock) == true) {
if (flags & GIT_FILEBUF_FORCE)
p_unlink(file->path_lock);
- else
- return git__throw(GIT_EOSERR, "Failed to lock file");
+ else {
+ giterr_set(GITERR_OS,
+ "Failed to lock file '%s' for writing", file->path_lock);
+ return -1;
+ }
}
/* create path to the file buffer is required */
@@ -32,16 +65,22 @@ static int lock_file(git_filebuf *file, int flags)
}
if (file->fd < 0)
- return git__throw(GIT_EOSERR, "Failed to create lock");
+ return -1;
+
+ file->fd_is_open = true;
- if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == 0) {
+ if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == true) {
git_file source;
char buffer[2048];
size_t read_bytes;
source = p_open(file->path_original, O_RDONLY);
- if (source < 0)
- return git__throw(GIT_EOSERR, "Failed to lock file. Could not open %s", file->path_original);
+ if (source < 0) {
+ giterr_set(GITERR_OS,
+ "Failed to open file '%s' for reading",
+ file->path_original);
+ return -1;
+ }
while ((read_bytes = p_read(source, buffer, 2048)) > 0) {
p_write(file->fd, buffer, read_bytes);
@@ -52,15 +91,15 @@ static int lock_file(git_filebuf *file, int flags)
p_close(source);
}
- return GIT_SUCCESS;
+ return 0;
}
void git_filebuf_cleanup(git_filebuf *file)
{
- if (file->fd >= 0)
+ if (file->fd_is_open && file->fd >= 0)
p_close(file->fd);
- if (file->fd >= 0 && file->path_lock && git_path_exists(file->path_lock) == GIT_SUCCESS)
+ if (file->fd_is_open && file->path_lock && git_path_exists(file->path_lock))
p_unlink(file->path_lock);
if (file->digest)
@@ -93,20 +132,21 @@ GIT_INLINE(int) flush_buffer(git_filebuf *file)
static int write_normal(git_filebuf *file, void *source, size_t len)
{
- int result = 0;
-
if (len > 0) {
- result = p_write(file->fd, (void *)source, len);
+ if (p_write(file->fd, (void *)source, len) < 0) {
+ file->last_error = BUFERR_WRITE;
+ return -1;
+ }
+
if (file->digest)
git_hash_update(file->digest, source, len);
}
- return result;
+ return 0;
}
static int write_deflate(git_filebuf *file, void *source, size_t len)
{
- int result = Z_OK;
z_stream *zs = &file->zs;
if (len > 0 || file->flush_mode == Z_FINISH) {
@@ -119,14 +159,17 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
zs->next_out = file->z_buf;
zs->avail_out = (uInt)file->buf_size;
- result = deflate(zs, file->flush_mode);
- if (result == Z_STREAM_ERROR)
- return git__throw(GIT_ERROR, "Failed to deflate input");
+ if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) {
+ file->last_error = BUFERR_ZLIB;
+ return -1;
+ }
have = file->buf_size - (size_t)zs->avail_out;
- if (p_write(file->fd, file->z_buf, have) < GIT_SUCCESS)
- return git__throw(GIT_EOSERR, "Failed to write to file");
+ if (p_write(file->fd, file->z_buf, have) < 0) {
+ file->last_error = BUFERR_WRITE;
+ return -1;
+ }
} while (zs->avail_out == 0);
@@ -136,38 +179,39 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
git_hash_update(file->digest, source, len);
}
- return GIT_SUCCESS;
+ return 0;
}
int git_filebuf_open(git_filebuf *file, const char *path, int flags)
{
- int error, compression;
+ int compression;
size_t path_len;
- assert(file && path);
-
- if (file->buffer)
- return git__throw(GIT_EINVALIDARGS, "Tried to reopen an open filebuf");
+ /* 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);
memset(file, 0x0, sizeof(git_filebuf));
+ if (flags & GIT_FILEBUF_DO_NOT_BUFFER)
+ file->do_not_buffer = true;
+
file->buf_size = WRITE_BUFFER_SIZE;
file->buf_pos = 0;
file->fd = -1;
+ file->last_error = BUFERR_OK;
/* Allocate the main cache buffer */
- file->buffer = git__malloc(file->buf_size);
- if (file->buffer == NULL){
- error = GIT_ENOMEM;
- goto cleanup;
+ if (!file->do_not_buffer) {
+ file->buffer = git__malloc(file->buf_size);
+ GITERR_CHECK_ALLOC(file->buffer);
}
/* If we are hashing on-write, allocate a new hash context */
if (flags & GIT_FILEBUF_HASH_CONTENTS) {
- if ((file->digest = git_hash_new_ctx()) == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ file->digest = git_hash_new_ctx();
+ GITERR_CHECK_ALLOC(file->digest);
}
compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT;
@@ -176,16 +220,13 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
if (compression != 0) {
/* Initialize the ZLib stream */
if (deflateInit(&file->zs, compression) != Z_OK) {
- error = git__throw(GIT_EZLIB, "Failed to initialize zlib");
+ giterr_set(GITERR_ZLIB, "Failed to initialize zlib");
goto cleanup;
}
/* Allocate the Zlib cache buffer */
file->z_buf = git__malloc(file->buf_size);
- if (file->z_buf == NULL){
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ GITERR_CHECK_ALLOC(file->z_buf);
/* Never flush */
file->flush_mode = Z_NO_FLUSH;
@@ -200,104 +241,101 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
/* Open the file as temporary for locking */
file->fd = git_futils_mktmp(&tmp_path, path);
+
if (file->fd < 0) {
git_buf_free(&tmp_path);
- error = GIT_EOSERR;
goto cleanup;
}
+ file->fd_is_open = true;
/* No original path */
file->path_original = NULL;
file->path_lock = git_buf_detach(&tmp_path);
-
- if (file->path_lock == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ GITERR_CHECK_ALLOC(file->path_lock);
} else {
path_len = strlen(path);
/* Save the original path of the file */
file->path_original = git__strdup(path);
- if (file->path_original == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ GITERR_CHECK_ALLOC(file->path_original);
/* create the locking path by appending ".lock" to the original */
file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH);
- if (file->path_lock == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ GITERR_CHECK_ALLOC(file->path_lock);
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 */
- if ((error = lock_file(file, flags)) < GIT_SUCCESS)
+ if (lock_file(file, flags) < 0)
goto cleanup;
}
- return GIT_SUCCESS;
+ return 0;
cleanup:
git_filebuf_cleanup(file);
- return git__rethrow(error, "Failed to open file buffer for '%s'", path);
+ return -1;
}
int git_filebuf_hash(git_oid *oid, git_filebuf *file)
{
- int error;
-
assert(oid && file && file->digest);
- if ((error = flush_buffer(file)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to get hash for file");
+ flush_buffer(file);
+
+ if (verify_last_error(file) < 0)
+ return -1;
git_hash_final(oid, file->digest);
git_hash_free_ctx(file->digest);
file->digest = NULL;
- return GIT_SUCCESS;
+ return 0;
}
int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode)
{
git__free(file->path_original);
file->path_original = git__strdup(path);
- if (file->path_original == NULL)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(file->path_original);
return git_filebuf_commit(file, mode);
}
int git_filebuf_commit(git_filebuf *file, mode_t mode)
{
- int error;
-
/* temporary files cannot be committed */
assert(file && file->path_original);
file->flush_mode = Z_FINISH;
- if ((error = flush_buffer(file)) < GIT_SUCCESS)
- goto cleanup;
+ flush_buffer(file);
+
+ if (verify_last_error(file) < 0)
+ goto on_error;
p_close(file->fd);
file->fd = -1;
+ file->fd_is_open = false;
if (p_chmod(file->path_lock, mode)) {
- error = git__throw(GIT_EOSERR, "Failed to chmod locked file before committing");
- goto cleanup;
+ giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
+ goto on_error;
}
- error = p_rename(file->path_lock, file->path_original);
+ p_unlink(file->path_original);
-cleanup:
+ 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;
+ }
+
+ git_filebuf_cleanup(file);
+ return 0;
+
+on_error:
git_filebuf_cleanup(file);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to commit locked file from buffer");
- return GIT_SUCCESS;
+ return -1;
}
GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
@@ -308,22 +346,25 @@ GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
{
- int error;
const unsigned char *buf = buff;
+ ENSURE_BUF_OK(file);
+
+ if (file->do_not_buffer)
+ return file->write(file, (void *)buff, len);
+
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);
- return GIT_SUCCESS;
+ return 0;
}
add_to_cache(file, buf, space_left);
-
- if ((error = flush_buffer(file)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to write to buffer");
+ if (flush_buffer(file) < 0)
+ return -1;
len -= space_left;
buf += space_left;
@@ -332,32 +373,37 @@ int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
{
- int error;
size_t space_left = file->buf_size - file->buf_pos;
*buffer = NULL;
- if (len > file->buf_size)
- return GIT_ENOMEM;
+ ENSURE_BUF_OK(file);
+
+ if (len > file->buf_size) {
+ file->last_error = BUFERR_MEM;
+ return -1;
+ }
if (space_left <= len) {
- if ((error = flush_buffer(file)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to reserve buffer");
+ if (flush_buffer(file) < 0)
+ return -1;
}
*buffer = (file->buffer + file->buf_pos);
file->buf_pos += len;
- return GIT_SUCCESS;
+ return 0;
}
int git_filebuf_printf(git_filebuf *file, const char *format, ...)
{
va_list arglist;
size_t space_left;
- int len, error;
+ int len, res;
char *tmp_buffer;
+ ENSURE_BUF_OK(file);
+
space_left = file->buf_size - file->buf_pos;
do {
@@ -365,24 +411,28 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
len = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist);
va_end(arglist);
- if (len < 0)
- return git__throw(GIT_EOSERR, "Failed to format string");
+ if (len < 0) {
+ file->last_error = BUFERR_MEM;
+ return -1;
+ }
if ((size_t)len + 1 <= space_left) {
file->buf_pos += len;
- return GIT_SUCCESS;
+ return 0;
}
- if ((error = flush_buffer(file)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to output to buffer");
+ if (flush_buffer(file) < 0)
+ return -1;
space_left = file->buf_size - file->buf_pos;
} while ((size_t)len + 1 <= space_left);
tmp_buffer = git__malloc(len + 1);
- if (!tmp_buffer)
- return GIT_ENOMEM;
+ if (!tmp_buffer) {
+ file->last_error = BUFERR_MEM;
+ return -1;
+ }
va_start(arglist, format);
len = p_vsnprintf(tmp_buffer, len + 1, format, arglist);
@@ -390,12 +440,13 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
if (len < 0) {
git__free(tmp_buffer);
- return git__throw(GIT_EOSERR, "Failed to format string");
+ file->last_error = BUFERR_MEM;
+ return -1;
}
- error = git_filebuf_write(file, tmp_buffer, len);
+ res = git_filebuf_write(file, tmp_buffer, len);
git__free(tmp_buffer);
- return error;
+ return res;
}