agendafs

A filesystem for your calendar.

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

Refactor

Diffstat:
Mjournal_entry.c | 13+++----------
Mpath.c | 13+++++++++++++
Mpath.h | 2++
3 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/journal_entry.c b/journal_entry.c
@@ -911,20 +911,12 @@ write_entry_to_ical_file(const struct journal_entry *entry)
 
 	LOG("Got ical_str %s", ical_str);
 
-	FILE *f = fopen(filepath, "w");
-	if (!f) {
-		perror("Failed to open ICS file");
-		free(ical_str);
-		free(filepath);
-		return -EIO;
-	}
+	int res = write_to_file(filepath, ical_str);
 
-	fputs(ical_str, f);
-	fclose(f);
 	free(ical_str);
 	free(filepath);
 
-	return 0;
+	return res;
 }
 
 size_t
@@ -1118,6 +1110,7 @@ match_by_uid(icalcomponent *component, const char *target_uuid)
 int
 create_entry_from_fuse(const char *fuse_path)
 {
+	// TODO: Move to path.c
 	const char *entry_prefix = "/";
 	if (strncmp(fuse_path, entry_prefix, strlen(entry_prefix)) != 0) {
 		return -EINVAL;
diff --git a/path.c b/path.c
@@ -1,5 +1,6 @@
 #include "path.h"
 #include "util.h"
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -89,3 +90,15 @@ split_path(const path *p, char **segments)
 	return count;
 }
 
+size_t
+write_to_file(const char *filepath, const char *content)
+{
+	FILE *f = fopen(filepath, "w");
+	if (!f) {
+		perror("Failed to open file");
+		return -EIO;
+	}
+	fputs(content, f);
+	fclose(f);
+	return 0;
+}
diff --git a/path.h b/path.h
@@ -23,4 +23,6 @@ size_t
 get_parent_path(const char *, char **);
 size_t
 split_path(const path *, char **);
+size_t
+write_to_file(const char *filepath, const char *content);
 #endif // path_h_INCLUDED