changeset 280:11c824a82e63

core: fix save_open functions
author David Demelier <markand@malikania.fr>
date Tue, 15 Dec 2020 22:07:18 +0100
parents 5217c195c5b9
children 87b8c7510717
files libmlk-core/core/save.c libmlk-core/core/save.h
diffstat 2 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libmlk-core/core/save.c	Tue Dec 15 22:07:01 2020 +0100
+++ b/libmlk-core/core/save.c	Tue Dec 15 22:07:18 2020 +0100
@@ -52,7 +52,7 @@
 static const char *
 path(unsigned int idx)
 {
-	return util_pathf("%s/%u", sys_dir(SYS_DIR_SAVE), idx);
+	return util_pathf("%s%u.db", sys_dir(SYS_DIR_SAVE), idx);
 }
 
 static bool
@@ -111,16 +111,28 @@
 	}
 
 	if (sqlite3_open_v2(path, (sqlite3**)&db->handle, flags, NULL) != SQLITE_OK)
-		return errorf("%s", sqlite3_errmsg(db->handle));
+		goto sqlite3_err;
 
-	if (mode == SAVE_MODE_WRITE) {
-		if (!execu(db, sql_init)) {
-			sqlite3_close(db->handle);
-			return false;
-		}
-	}
+	if (mode == SAVE_MODE_WRITE && !execu(db, sql_init))
+		goto sqlite3_err;
 
 	return verify(db);
+
+sqlite3_err:
+	errorf("%s", sqlite3_errmsg(db->handle));
+	sqlite3_close(db->handle);
+
+	memset(db, 0, sizeof (*db));
+
+	return false;
+}
+
+bool
+save_ok(const struct save *db)
+{
+	assert(db);
+
+	return db && db->handle;
 }
 
 bool
--- a/libmlk-core/core/save.h	Tue Dec 15 22:07:01 2020 +0100
+++ b/libmlk-core/core/save.h	Tue Dec 15 22:07:18 2020 +0100
@@ -48,6 +48,9 @@
 save_open_path(struct save *db, const char *path, enum save_mode mode);
 
 bool
+save_ok(const struct save *db);
+
+bool
 save_set_property(struct save *db, const struct save_property *prop);
 
 bool