]> git.ipfire.org Git - location/libloc.git/commitdiff
database: Refactor error handling on create
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 23 Aug 2022 09:46:33 +0000 (09:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 23 Aug 2022 09:46:33 +0000 (09:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c

index 374da576739c717b700d8bc4bc5a827456910ff9..4043086afddf2198763d8c303c7d6a5c08b6ee47 100644 (file)
@@ -409,38 +409,6 @@ static int loc_database_read(struct loc_database* db, FILE* f) {
        return 0;
 }
 
-LOC_EXPORT int loc_database_new(struct loc_ctx* ctx, struct loc_database** database, FILE* f) {
-       // Fail on invalid file handle
-       if (!f)
-               return -EINVAL;
-
-       struct loc_database* db = calloc(1, sizeof(*db));
-       if (!db)
-               return -ENOMEM;
-
-       // Reference context
-       db->ctx = loc_ref(ctx);
-       db->refcount = 1;
-
-       DEBUG(db->ctx, "Database object allocated at %p\n", db);
-
-       int r = loc_database_read(db, f);
-       if (r) {
-               loc_database_unref(db);
-               return r;
-       }
-
-       *database = db;
-
-       return 0;
-}
-
-LOC_EXPORT struct loc_database* loc_database_ref(struct loc_database* db) {
-       db->refcount++;
-
-       return db;
-}
-
 static void loc_database_free(struct loc_database* db) {
        DEBUG(db->ctx, "Releasing database %p\n", db);
 
@@ -474,6 +442,48 @@ static void loc_database_free(struct loc_database* db) {
        free(db);
 }
 
+LOC_EXPORT int loc_database_new(struct loc_ctx* ctx, struct loc_database** database, FILE* f) {
+       struct loc_database* db = NULL;
+       int r;
+
+       // Fail on invalid file handle
+       if (!f) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       // Allocate the database object
+       db = calloc(1, sizeof(*db));
+       if (!db)
+               goto ERROR;
+
+       // Reference context
+       db->ctx = loc_ref(ctx);
+       db->refcount = 1;
+
+       DEBUG(db->ctx, "Database object allocated at %p\n", db);
+
+       // Try to open the database
+       r = loc_database_read(db, f);
+       if (r)
+               goto ERROR;
+
+       *database = db;
+       return 0;
+
+ERROR:
+       if (db)
+               loc_database_free(db);
+
+       return r;
+}
+
+LOC_EXPORT struct loc_database* loc_database_ref(struct loc_database* db) {
+       db->refcount++;
+
+       return db;
+}
+
 LOC_EXPORT struct loc_database* loc_database_unref(struct loc_database* db) {
        if (--db->refcount > 0)
                return NULL;