Commit d320c52d by Jakob Pfender Committed by Vicent Marti

index.c: Move to new error handling mechanism

parent ec9edd56
...@@ -411,7 +411,7 @@ static int index_init_entry(git_index_entry *entry, git_index *index, const char ...@@ -411,7 +411,7 @@ static int index_init_entry(git_index_entry *entry, git_index *index, const char
/* write the blob to disk and get the oid */ /* write the blob to disk and get the oid */
if ((error = git_blob_create_fromfile(&entry->oid, index->repository, rel_path)) < GIT_SUCCESS) if ((error = git_blob_create_fromfile(&entry->oid, index->repository, rel_path)) < GIT_SUCCESS)
return error; return git__rethrow(error, "Failed to initialize index entry");
entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT); entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
entry->path = (char *)rel_path; /* do not duplicate; index_insert already does this */ entry->path = (char *)rel_path; /* do not duplicate; index_insert already does this */
...@@ -424,7 +424,7 @@ int git_index_add(git_index *index, const char *path, int stage) ...@@ -424,7 +424,7 @@ int git_index_add(git_index *index, const char *path, int stage)
git_index_entry entry; git_index_entry entry;
if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS) if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
return error; return git__rethrow(error, "Failed to add to index");
return index_insert(index, &entry, 1); return index_insert(index, &entry, 1);
} }
...@@ -435,7 +435,7 @@ int git_index_append(git_index *index, const char *path, int stage) ...@@ -435,7 +435,7 @@ int git_index_append(git_index *index, const char *path, int stage)
git_index_entry entry; git_index_entry entry;
if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS) if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
return error; return git__rethrow(error, "Failed to append to index");
return index_insert(index, &entry, 0); return index_insert(index, &entry, 0);
} }
...@@ -574,13 +574,13 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size) ...@@ -574,13 +574,13 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
len = strlen(buffer) + 1; len = strlen(buffer) + 1;
if (size <= len) if (size <= len)
return GIT_ERROR; return git__throw(GIT_ERROR, "Failed to read unmerged entries");
if ((lost = git__malloc(sizeof(git_index_entry_unmerged))) == NULL) if ((lost = git__malloc(sizeof(git_index_entry_unmerged))) == NULL)
return GIT_ENOMEM; return GIT_ENOMEM;
if (git_vector_insert(&index->unmerged, lost) < GIT_SUCCESS) if (git_vector_insert(&index->unmerged, lost) < GIT_SUCCESS)
return GIT_ERROR; return git__throw(GIT_ERROR, "Failed to read unmerged entries");
lost->path = git__strdup(buffer); lost->path = git__strdup(buffer);
if (!lost->path) if (!lost->path)
...@@ -596,7 +596,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size) ...@@ -596,7 +596,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
len = (endptr + 1) - (char *) buffer; len = (endptr + 1) - (char *) buffer;
if (size <= len) if (size <= len)
return GIT_ERROR; return git__throw(GIT_ERROR, "Failed to read unmerged entries");
size -= len; size -= len;
buffer += len; buffer += len;
...@@ -606,8 +606,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size) ...@@ -606,8 +606,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
if (!lost->mode[i]) if (!lost->mode[i])
continue; continue;
if (size < 20) if (size < 20)
return GIT_ERROR; return git__throw(GIT_ERROR, "Failed to read unmerged entries");
git_oid_mkraw(&lost->oid[i], (unsigned char *) buffer); git_oid_mkraw(&lost->oid[i], (unsigned char *) buffer);
size -= 20; size -= 20;
buffer += 20; buffer += 20;
...@@ -688,12 +687,12 @@ static int read_header(struct index_header *dest, const void *buffer) ...@@ -688,12 +687,12 @@ static int read_header(struct index_header *dest, const void *buffer)
dest->signature = ntohl(source->signature); dest->signature = ntohl(source->signature);
if (dest->signature != INDEX_HEADER_SIG) if (dest->signature != INDEX_HEADER_SIG)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to read header. Invalid signature");
dest->version = ntohl(source->version); dest->version = ntohl(source->version);
if (dest->version != INDEX_VERSION_NUMBER_EXT && if (dest->version != INDEX_VERSION_NUMBER_EXT &&
dest->version != INDEX_VERSION_NUMBER) dest->version != INDEX_VERSION_NUMBER)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to read header. Invalid index version number");
dest->entry_count = ntohl(source->entry_count); dest->entry_count = ntohl(source->entry_count);
return GIT_SUCCESS; return GIT_SUCCESS;
...@@ -744,13 +743,13 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) ...@@ -744,13 +743,13 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
#define seek_forward(_increase) { \ #define seek_forward(_increase) { \
if (_increase >= buffer_size) \ if (_increase >= buffer_size) \
return GIT_EOBJCORRUPTED; \ return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \
buffer += _increase; \ buffer += _increase; \
buffer_size -= _increase;\ buffer_size -= _increase;\
} }
if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE) if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer too small");
/* Precalculate the SHA1 of the files's contents -- we'll match it to /* Precalculate the SHA1 of the files's contents -- we'll match it to
* the provided SHA1 in the footer */ * the provided SHA1 in the footer */
...@@ -758,7 +757,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) ...@@ -758,7 +757,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* Parse header */ /* Parse header */
if (read_header(&header, buffer) < GIT_SUCCESS) if (read_header(&header, buffer) < GIT_SUCCESS)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header is corrupted");
seek_forward(INDEX_HEADER_SIZE); seek_forward(INDEX_HEADER_SIZE);
...@@ -777,7 +776,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) ...@@ -777,7 +776,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* 0 bytes read means an object corruption */ /* 0 bytes read means an object corruption */
if (entry_size == 0) if (entry_size == 0)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Entry size is zero");
if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS) if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
return GIT_ENOMEM; return GIT_ENOMEM;
...@@ -786,7 +785,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) ...@@ -786,7 +785,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
} }
if (i != header.entry_count) if (i != header.entry_count)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header entries changed while parsing");
/* There's still space for some extensions! */ /* There's still space for some extensions! */
while (buffer_size > INDEX_FOOTER_SIZE) { while (buffer_size > INDEX_FOOTER_SIZE) {
...@@ -796,19 +795,19 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) ...@@ -796,19 +795,19 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* see if we have read any bytes from the extension */ /* see if we have read any bytes from the extension */
if (extension_size == 0) if (extension_size == 0)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero");
seek_forward(extension_size); seek_forward(extension_size);
} }
if (buffer_size != INDEX_FOOTER_SIZE) if (buffer_size != INDEX_FOOTER_SIZE)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer size does not match index footer size");
/* 160-bit SHA-1 over the content of the index file before this checksum. */ /* 160-bit SHA-1 over the content of the index file before this checksum. */
git_oid_mkraw(&checksum_expected, (const unsigned char *)buffer); git_oid_mkraw(&checksum_expected, (const unsigned char *)buffer);
if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0) if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
return GIT_EOBJCORRUPTED; return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Calculated checksum does not match expected checksum");
#undef seek_forward #undef seek_forward
...@@ -918,7 +917,7 @@ static int write_index(git_index *index, git_filebuf *file) ...@@ -918,7 +917,7 @@ static int write_index(git_index *index, git_filebuf *file)
error = write_entries(index, file); error = write_entries(index, file);
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
return error; return git__rethrow(error, "Failed to write index");
/* TODO: write extensions (tree cache) */ /* TODO: write extensions (tree cache) */
...@@ -928,5 +927,5 @@ static int write_index(git_index *index, git_filebuf *file) ...@@ -928,5 +927,5 @@ static int write_index(git_index *index, git_filebuf *file)
/* write it at the end of the file */ /* write it at the end of the file */
git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ); git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
return error; return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write index");
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment