agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit 417be015e6aa1bccb5e3e4b4834eecaf5373f77d
parent ff7848fd0df5b5722d978d0265bbc47178e7d23a
Author: Marc Coquand <marc@coquand.email>
Date:   Sun, 15 Jun 2025 14:52:04 +0100

Bugfixes

Diffstat:
Mjournal_entry.c | 36++++++++++++++++++++++++++++++++++++
Mjournal_entry.h | 6++++++
Mmain.c | 88++++++++++++++++++++++---------------------------------------------------------
3 files changed, 66 insertions(+), 64 deletions(-)
diff --git a/journal_entry.c b/journal_entry.c
@@ -165,6 +165,9 @@ parse_ics_to_journal_entry_component(const char *filename,
 		LOG("No journal component");
 		return -2;
 	}
+	if (icalcomponent_get_description(component) == NULL) {
+		icalcomponent_set_description(component, "");
+	}
 	entry->component = component;
 	return 0;
 }
@@ -636,3 +639,36 @@ remove_file_extension(char *path)
 	}
 }
 
+void
+update_or_create_fuse_entry_from_original(const char *filename)
+{
+	char *base_name = strrchr(filename, '/');
+	struct journal_entry *new_entry =
+	    xcalloc(1, sizeof(struct journal_entry));
+
+	if (load_journal_entry_from_ics_file(base_name, new_entry) != 0) {
+		// Parsing failed, clean up and maybe remove old entry
+		// if any
+		LOG("PARSING FAILED");
+		free_journal_entry(new_entry);
+		return;
+	}
+	hashmap_insert(entries_original_ics, new_entry->filename_original,
+		       new_entry);
+	hashmap_insert(entries_fuse, new_entry->filename, new_entry);
+
+	LOG("File updated");
+}
+
+int
+update_fuse_entry_delete(const char *file)
+{
+	// Strip path, validate just the new basename
+	const char *keyname = strrchr(file, '/');
+	keyname++;
+	LOG("Found entry to delete");
+	hashmap_remove(entries_original_ics, keyname);
+	// Frees
+	hashmap_remove(entries_fuse, keyname);
+	return 0;
+}
diff --git a/journal_entry.h b/journal_entry.h
@@ -130,5 +130,11 @@ create_vjournal_entry(char *summary);
 void
 remove_file_extension(char *path);
 
+void
+update_or_create_fuse_entry_from_original(const char *filename);
+
+int
+update_fuse_entry_delete(const char *file);
+
 #endif // JOURNAL_ENTRY_H
 
diff --git a/main.c b/main.c
@@ -255,39 +255,36 @@ journal_read(const char *path, char *buf, size_t size, off_t offset,
 		ret_code = -ENOENT;
 		goto unlock_and_return;
 	}
-
-	const char *summary = icalcomponent_get_summary(entry->component);
-	if (summary == NULL) {
-		summary = "";
-	}
+	LOG("entry = %p", entry);
+	LOG("entry->component = %p", entry->component);
 	const char *description =
 	    icalcomponent_get_description(entry->component);
-	if (description == NULL) {
+	if (!description) {
 		description = "";
 	}
+	LOG("description = %p", description);
 
-	// Calculate required length for full content
-	char *full_content = NULL;
-	size_t needed_len = asprintf(&full_content, "%s", description);
-	if (needed_len == -1) {
-		LOG("Out of memory");
-		exit(1);
-	}
+	// We now treat description as the full content
+	const char *content = description;
+	size_t content_len = strlen(content);
 
-	if (offset >= needed_len) {
-		// EOF
-		free(full_content);
+	if ((size_t)offset >= content_len) {
 		ret_code = 0;
 		goto unlock_and_return;
 	}
 
-	if (offset + size > (size_t)needed_len)
-		size = needed_len - offset;
+	if (offset + size > content_len) {
+		LOG("Size is set");
+		size = content_len - offset;
+	}
+
+	if (size > 0) {
 
-	// Write to read buffer only the requested amount
-	memcpy(buf, full_content + offset, size);
-	free(full_content);
+		LOG("Memcopying");
+		memcpy(buf, content + offset, size);
+	}
 
+	LOG("Done");
 	ret_code = size;
 
 unlock_and_return:
@@ -437,49 +434,6 @@ journal_create(const char *filename, mode_t mode, struct fuse_file_info *info)
 	return 0;
 }
 
-void
-update_or_create_fuse_entry_from_original(const char *filename)
-{
-	pthread_mutex_lock(&entries_mutex);
-	char *base_name = strrchr(filename, '/');
-	struct journal_entry *new_entry =
-	    xcalloc(1, sizeof(struct journal_entry));
-
-	if (load_journal_entry_from_ics_file(base_name, new_entry) != 0) {
-		// Parsing failed, clean up and maybe remove old entry
-		// if any
-		LOG("PARSING FAILED");
-		free_journal_entry(new_entry);
-		pthread_mutex_unlock(&entries_mutex);
-		return;
-	}
-	pthread_mutex_unlock(&entries_mutex);
-	return;
-
-	hashmap_insert(entries_original_ics, new_entry->filename_original,
-		       new_entry);
-	hashmap_insert(entries_fuse, new_entry->filename, new_entry);
-
-	LOG("File updated");
-
-	pthread_mutex_unlock(&entries_mutex);
-}
-
-int
-update_fuse_entry_delete(const char *file)
-{
-	pthread_mutex_lock(&entries_mutex);
-	// Strip path, validate just the new basename
-	const char *keyname = strrchr(file, '/');
-	keyname++;
-	LOG("Found entry to delete");
-	hashmap_remove(entries_original_ics, keyname);
-	// Frees
-	hashmap_remove(entries_fuse, keyname);
-	pthread_mutex_unlock(&entries_mutex);
-	return 0;
-}
-
 void *
 watch_ics_dir(void *arg)
 {
@@ -525,15 +479,21 @@ watch_ics_dir(void *arg)
 				    event->name);
 
 				if (event->mask & IN_DELETE) {
+					pthread_mutex_lock(&entries_mutex);
 					update_fuse_entry_delete(full_path);
+					pthread_mutex_unlock(&entries_mutex);
 				}
 				else if (event->mask & IN_MODIFY) {
+					pthread_mutex_lock(&entries_mutex);
 
 					// Reload entries on any change
 					update_or_create_fuse_entry_from_original(
 					    full_path);
+
+					pthread_mutex_unlock(&entries_mutex);
 				}
 				else if (event->mask & IN_CREATE) {
+					// TODO
 				}
 			}
 			free(full_path);