agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit 92d8f0a630d770f321a5078d2b0ce6b326b6a401
parent 781ac11a0bc4ed02d5adc4f9aae0cea751260db2
Author: Marc Coquand <marc@coquand.email>
Date:   Mon, 14 Jul 2025 14:45:14 +0200

Use asserts to verify if malloc fails

Just makes the code cleaner and less verbose

Diffstat:
Mjournal_entry.c | 49+++++++++++++++++--------------------------------
Mpath.c | 8++++----
2 files changed, 21 insertions(+), 36 deletions(-)
diff --git a/journal_entry.c b/journal_entry.c
@@ -4,6 +4,7 @@
 #include "path.h"
 #include "tree.h"
 #include "util.h"
+#include <assert.h>
 #include <dirent.h>
 #include <errno.h>
 #include <libical/ical.h>
@@ -88,10 +89,9 @@ get_node_by_uuid(const char *target_uuid)
 	char *filename_original = NULL;
 
 	// Filenames are uid.ics, so we levarage that
-	if (asprintf(&filename_original, "%s.ics", target_uuid) == 0) {
-		LOG("EMEM");
-		exit(1);
-	}
+	size_t wRes = asprintf(&filename_original, "%s.ics", target_uuid);
+	assert(wRes != -1);
+
 	LOG("Looking for UID: '%s'", target_uuid);
 	LOG("Full filename: '%s'", filename_original);
 
@@ -541,14 +541,11 @@ load_journal_entries()
 			struct tree_node *new_node =
 			    create_file_node(new_entry);
 
-			if (add_child(fuse_tree_root, new_node) != 0 ||
-			    hashmap_insert(entries_original_ics,
-					   new_entry->filename_original,
-					   new_node) != 0) {
-				LOG("Failed to insert entry");
-				free_journal_entry(new_entry);
-				continue;
-			}
+			size_t res = hashmap_insert(
+			    entries_original_ics, new_entry->filename_original,
+			    new_node);
+			assert(res == 0);
+
 			LOG("Inserted filename_original: %s",
 			    new_entry->filename_original);
 		}
@@ -596,10 +593,7 @@ write_entry_content(char *buf, struct journal_entry *entry)
 	// Allocate memory for the content
 	size_t size = asprintf(&buf, "%s",
 			       icalcomponent_get_description(entry->component));
-	if (size == -1) {
-		LOG("Out of memory");
-		exit(1);
-	}
+	assert(size != -1);
 	return size;
 }
 
@@ -608,10 +602,8 @@ get_entry_content_size(struct journal_entry *entry)
 {
 	size_t size = snprintf(NULL, 0, "%s",
 			       icalcomponent_get_description(entry->component));
-	if (size == -1) {
-		LOG("Out of mem");
-		exit(1);
-	}
+
+	assert(size != -1);
 	return size;
 }
 
@@ -1031,9 +1023,9 @@ create_new_unique_ics_uid()
 	uuid_generate(uuid);
 	uuid_unparse(uuid, uuid_str);
 	char *res = NULL;
-	if (asprintf(&res, "%s-caldavfs", uuid_str) == -1) {
-		return NULL;
-	}
+	size_t wRes = asprintf(&res, "%s-caldavfs", uuid_str);
+	assert(wRes != -1);
+
 	return res;
 }
 
@@ -1103,7 +1095,6 @@ match_by_uid(icalcomponent *component, const char *target_uuid)
 {
 	const char *uid = icalcomponent_get_uid(component);
 	return uid && strcmp(uid, target_uuid) == 0;
-	return false;
 }
 
 // TODO: Handle duplicates in filename
@@ -1157,13 +1148,7 @@ create_entry_from_fuse(const char *fuse_path)
 	hashmap_insert(entries_original_ics, new_entry->filename_original,
 		       new_node);
 
-	if (write_entry_to_ical_file(new_entry) != 0) {
-		LOG("Write to entry failed");
-		return -EIO;
-	}
-
-	LOG("File updated");
-	return 0;
+	return write_entry_to_ical_file(new_entry);
 }
 
 int
@@ -1206,9 +1191,9 @@ create_directory_from_fuse_path(const char *fuse_path)
 	}
 	if (!node_is_directory(parent)) {
 		LOG("Parent is not directory ");
-
 		return -ENOTDIR;
 	}
+
 	add_child_to_node(parent, new_node);
 	hashmap_insert(entries_original_ics, new_entry->filename_original,
 		       new_node);
diff --git a/path.c b/path.c
@@ -1,5 +1,6 @@
 #include "path.h"
 #include "util.h"
+#include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,10 +32,8 @@ path *
 append_path(path *parentp, char *childp)
 {
 	char *filepath = NULL;
-	if (asprintf(&filepath, "%s/%s", parentp, childp) == -1) {
-		LOG("Memory alloc failed");
-		exit(1);
-	}
+	size_t res = asprintf(&filepath, "%s/%s", parentp, childp);
+	assert(res != -1);
 	return filepath;
 }
 
@@ -66,6 +65,7 @@ get_parent_path(const char *path, char **buffer)
 	}
 	*last_slash = '\0';
 	int result = asprintf(buffer, "%s", path_copy);
+	assert(result != -1);
 	free(path_copy);
 
 	return result;