odb_pack.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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 57ad5e3..ce2dd4a 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -405,7 +405,7 @@ static int pack_window_close_lru(
return GIT_SUCCESS;
}
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to close pack window");
}
static void pack_window_close(struct pack_window **w_cursor)
@@ -532,11 +532,11 @@ static int pack_index_check(const char *path, struct pack_file *p)
int error;
if (fd < 0)
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to check index. File missing or corrupted");
if (gitfo_fstat(fd, &st) < GIT_SUCCESS) {
gitfo_close(fd);
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to check index. File appears to be corrupted");
}
if (!git__is_sizet(st.st_size))
@@ -546,14 +546,14 @@ static int pack_index_check(const char *path, struct pack_file *p)
if (idx_size < 4 * 256 + 20 + 20) {
gitfo_close(fd);
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Object is corrupted");
}
error = gitfo_map_ro(&p->index_map, fd, 0, idx_size);
gitfo_close(fd);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to check index");
hdr = idx_map = p->index_map.data;
@@ -562,7 +562,7 @@ static int pack_index_check(const char *path, struct pack_file *p)
if (version < 2 || version > 2) {
gitfo_free_map(&p->index_map);
- return GIT_EOBJCORRUPTED; /* unsupported index version */
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Unsupported index version");
}
} else
@@ -578,7 +578,7 @@ static int pack_index_check(const char *path, struct pack_file *p)
uint32_t n = ntohl(index[i]);
if (n < nr) {
gitfo_free_map(&p->index_map);
- return GIT_EOBJCORRUPTED; /* non-monotonic index */
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Index is non-monotonic");
}
nr = n;
}
@@ -593,7 +593,7 @@ static int pack_index_check(const char *path, struct pack_file *p)
*/
if (idx_size != 4*256 + nr * 24 + 20 + 20) {
gitfo_free_map(&p->index_map);
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Object is corrupted");
}
} else if (version == 2) {
/*
@@ -617,14 +617,14 @@ static int pack_index_check(const char *path, struct pack_file *p)
if (idx_size < min_size || idx_size > max_size) {
gitfo_free_map(&p->index_map);
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Wrong index size");
}
/* Make sure that off_t is big enough to access the whole pack...
* Is this an issue in libgit2? It shouldn't. */
if (idx_size != min_size && (sizeof(off_t) <= 4)) {
gitfo_free_map(&p->index_map);
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to check index. off_t not big enough to access the whole pack");
}
}
@@ -647,7 +647,7 @@ static int pack_index_open(struct pack_file *p)
error = pack_index_check(idx_name, p);
free(idx_name);
- return error;
+ return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to open index");
}
@@ -726,12 +726,12 @@ static int packfile_open(struct pack_file *p)
unsigned char *idx_sha1;
if (!p->index_map.data && pack_index_open(p) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to open packfile. File not found");
/* TODO: open with noatime */
p->pack_fd = gitfo_open(p->pack_name, O_RDONLY);
if (p->pack_fd < 0 || gitfo_fstat(p->pack_fd, &st) < GIT_SUCCESS)
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to open packfile. File appears to be corrupted");
/* If we created the struct before we had the pack we lack size. */
if (!p->pack_size) {
@@ -784,7 +784,7 @@ static int packfile_open(struct pack_file *p)
cleanup:
gitfo_close(p->pack_fd);
p->pack_fd = -1;
- return GIT_EPACKCORRUPTED;
+ return git__throw(GIT_EPACKCORRUPTED, "Failed to packfile. Pack is corrupted");
}
static int packfile_check(struct pack_file **pack_out, const char *path)
@@ -804,7 +804,7 @@ static int packfile_check(struct pack_file **pack_out, const char *path)
path_len -= STRLEN(".idx");
if (path_len < 1) {
free(p);
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to check packfile. Wrong path name");
}
memcpy(p->pack_name, path, path_len);
@@ -816,7 +816,7 @@ static int packfile_check(struct pack_file **pack_out, const char *path)
strcpy(p->pack_name + path_len, ".pack");
if (gitfo_stat(p->pack_name, &st) < GIT_SUCCESS || !S_ISREG(st.st_mode)) {
free(p);
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to check packfile. File not found");
}
/* ok, it looks sane as far as we can check without
@@ -853,7 +853,7 @@ static int packfile_load__cb(void *_data, char *path)
error = packfile_check(&pack, path);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to load packfile");
if (git_vector_insert(&backend->packs, pack) < GIT_SUCCESS) {
free(pack);
@@ -872,7 +872,7 @@ static int packfile_refresh_all(struct pack_backend *backend)
return GIT_SUCCESS;
if (gitfo_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to refresh packfiles. Backend not found");
if (st.st_mtime != backend->pack_folder_mtime) {
char path[GIT_PATH_MAX];
@@ -881,7 +881,7 @@ static int packfile_refresh_all(struct pack_backend *backend)
/* reload all packs */
error = gitfo_dirent(path, GIT_PATH_MAX, packfile_load__cb, (void *)backend);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to refresh packfiles");
git_vector_sort(&backend->packs);
backend->pack_folder_mtime = st.st_mtime;
@@ -936,7 +936,7 @@ static int pack_entry_find_offset(
int error;
if ((error = pack_index_open(p)) < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to find offset for pack entry");
assert(p->index_map.data);
@@ -969,7 +969,7 @@ static int pack_entry_find_offset(
int pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, oid);
if (pos < 0)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to find offset for pack entry. Entry not found");
*offset_out = nth_packed_object_offset(p, pos);
return GIT_SUCCESS;
@@ -992,7 +992,7 @@ static int pack_entry_find_offset(
} while (lo < hi);
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to find offset for pack entry. Entry not found");
#endif
}
@@ -1009,17 +1009,17 @@ static int pack_entry_find1(
unsigned i;
for (i = 0; i < p->num_bad_objects; i++)
if (git_oid_cmp(oid, &p->bad_object_sha1[i]) == 0)
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to find pack entry. Bad object found");
}
if (pack_entry_find_offset(&offset, p, oid) < GIT_SUCCESS)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to find pack entry. Couldn't find offset");
/* we found an entry in the index;
* make sure the packfile backing the index
* still exists on disk */
if (p->pack_fd == -1 && packfile_open(p) < GIT_SUCCESS)
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to find pack entry. Packfile doesn't exist on disk");
e->offset = offset;
e->p = p;
@@ -1034,7 +1034,7 @@ static int pack_entry_find(struct pack_entry *e, struct pack_backend *backend, c
size_t i;
if ((error = packfile_refresh_all(backend)) < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to find pack entry");
if (backend->last_found &&
pack_entry_find1(e, backend->last_found, oid) == GIT_SUCCESS)
@@ -1053,7 +1053,7 @@ static int pack_entry_find(struct pack_entry *e, struct pack_backend *backend, c
}
}
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Failed to find pack entry");
}
@@ -1125,7 +1125,7 @@ static int packfile_unpack_header(
used = packfile_unpack_header1(size_p, type_p, base, left);
if (used == 0)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to unpack packfile header. Header length is zero");
*curpos += used;
return GIT_SUCCESS;
@@ -1153,7 +1153,7 @@ static int packfile_unpack_compressed(
st = inflateInit(&stream);
if (st != Z_OK) {
free(buffer);
- return GIT_EZLIB;
+ return git__throw(GIT_EZLIB, "Failed to unpack compressed packfile. Error in zlib");
}
do {
@@ -1171,7 +1171,7 @@ static int packfile_unpack_compressed(
if ((st != Z_STREAM_END) || stream.total_out != size) {
free(buffer);
- return GIT_EZLIB;
+ return git__throw(GIT_EZLIB, "Failed to unpack compressed packfile. Error in zlib");
}
obj->type = type;
@@ -1215,7 +1215,7 @@ static off_t get_delta_base(
} else if (type == GIT_OBJ_REF_DELTA) {
/* The base entry _must_ be in the same pack */
if (pack_entry_find_offset(&base_offset, p, (git_oid *)base_info) < GIT_SUCCESS)
- return GIT_EPACKCORRUPTED;
+ return git__throw(GIT_EPACKCORRUPTED, "Failed to get base entry delta. Base entry is not in the same pack");
*curpos += 20;
} else
return 0;
@@ -1239,7 +1239,7 @@ static int packfile_unpack_delta(
base_offset = get_delta_base(backend, p, w_curs, &curpos, delta_type, obj_offset);
if (base_offset == 0)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to get delta for unpacked packfile. Offset is zero");
pack_window_close(w_curs);
error = packfile_unpack(&base, backend, p, base_offset);
@@ -1247,12 +1247,12 @@ static int packfile_unpack_delta(
/* TODO: git.git tries to load the base from other packfiles
* or loose objects */
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to get delta for unpacked packfile");
error = packfile_unpack_compressed(&delta, backend, p, w_curs, curpos, delta_size, delta_type);
if (error < GIT_SUCCESS) {
free(base.data);
- return error;
+ return git__rethrow(error, "Failed to get delta for unpacked packfile");
}
obj->type = base.type;
@@ -1265,7 +1265,7 @@ static int packfile_unpack_delta(
/* TODO: we might want to cache this shit. eventually */
//add_delta_base_cache(p, base_offset, base, base_size, *type);
- return error;
+ return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to get delta for unpacked packfile");
}
static int packfile_unpack(
@@ -1291,7 +1291,7 @@ static int packfile_unpack(
error = packfile_unpack_header(&size, &type, backend, p, &w_curs, &curpos);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to unpack packfile");
switch (type) {
case GIT_OBJ_OFS_DELTA:
@@ -1316,7 +1316,7 @@ static int packfile_unpack(
}
pack_window_close(&w_curs);
- return error;
+ return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to unpack packfile");
}
@@ -1352,10 +1352,10 @@ int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_od
int error;
if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to read pack backend");
if ((error = packfile_unpack(&raw, (struct pack_backend *)backend, e.p, e.offset)) < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to read pack backend");
*buffer_p = raw.data;
*len_p = raw.len;