index.c: Move to new error handling mechanism
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
diff --git a/src/index.c b/src/index.c
index c9d6eb5..c738c24 100644
--- a/src/index.c
+++ b/src/index.c
@@ -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 */
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->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)
git_index_entry entry;
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);
}
@@ -435,7 +435,7 @@ int git_index_append(git_index *index, const char *path, int stage)
git_index_entry entry;
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);
}
@@ -574,13 +574,13 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
len = strlen(buffer) + 1;
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)
return GIT_ENOMEM;
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);
if (!lost->path)
@@ -596,7 +596,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
len = (endptr + 1) - (char *) buffer;
if (size <= len)
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to read unmerged entries");
size -= len;
buffer += len;
@@ -606,8 +606,7 @@ static int read_unmerged(git_index *index, const char *buffer, size_t size)
if (!lost->mode[i])
continue;
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);
size -= 20;
buffer += 20;
@@ -662,7 +661,7 @@ static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffe
path_end = memchr(path_ptr, '\0', buffer_size);
if (path_end == NULL)
- return 0;
+ return 0;
path_length = path_end - path_ptr;
}
@@ -688,12 +687,12 @@ static int read_header(struct index_header *dest, const void *buffer)
dest->signature = ntohl(source->signature);
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);
if (dest->version != INDEX_VERSION_NUMBER_EXT &&
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);
return GIT_SUCCESS;
@@ -744,13 +743,13 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
#define seek_forward(_increase) { \
if (_increase >= buffer_size) \
- return GIT_EOBJCORRUPTED; \
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \
buffer += _increase; \
buffer_size -= _increase;\
}
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
* the provided SHA1 in the footer */
@@ -758,7 +757,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
/* Parse header */
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);
@@ -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 */
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)
return GIT_ENOMEM;
@@ -786,7 +785,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
}
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! */
while (buffer_size > INDEX_FOOTER_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 */
if (extension_size == 0)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero");
seek_forward(extension_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. */
git_oid_mkraw(&checksum_expected, (const unsigned char *)buffer);
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
@@ -918,7 +917,7 @@ static int write_index(git_index *index, git_filebuf *file)
error = write_entries(index, file);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to write index");
/* TODO: write extensions (tree cache) */
@@ -928,5 +927,5 @@ static int write_index(git_index *index, git_filebuf *file)
/* write it at the end of the file */
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");
}