simplify error handling in got_object_blob_open()
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
diff --git a/lib/object.c b/lib/object.c
index b422dcf..5ccc438 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1034,63 +1034,54 @@ got_object_blob_open(struct got_blob_object **blob,
(*blob)->read_buf = calloc(1, blocksize);
if ((*blob)->read_buf == NULL) {
err = got_error_from_errno();
- free(*blob);
- *blob = NULL;
- return err;
+ goto done;
}
if (obj->flags & GOT_OBJ_FLAG_PACKED) {
err = got_packfile_extract_object(&((*blob)->f), obj, repo);
- if (err) {
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
- return err;
- }
+ if (err)
+ goto done;
} else {
int fd;
FILE *f = NULL;
size_t size;
+
err = open_loose_object(&fd, obj, repo);
- if (err) {
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
- return err;
- }
+ if (err)
+ goto done;
+
f = fdopen(fd, "rb");
if (f == NULL) {
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
- return err;
+ err = got_error_from_errno();
+ close(fd);
+ goto done;
}
(*blob)->f = got_opentemp();
if ((*blob)->f == NULL) {
err = got_error_from_errno();
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
close(fd);
fclose(f);
- return err;
+ goto done;
}
err = got_inflate_to_file(&size, f, (*blob)->f);
- if (err != NULL) {
- fclose((*blob)->f);
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
- fclose(f);
- return err;
- }
+ fclose(f);
+ if (err != NULL)
+ goto done;
}
(*blob)->hdrlen = obj->hdrlen;
(*blob)->blocksize = blocksize;
memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
+done:
+ if (err && *blob) {
+ if ((*blob)->f)
+ fclose((*blob)->f);
+ free((*blob)->read_buf);
+ free(*blob);
+ *blob = NULL;
+ }
return err;
}