agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit 8445ac56d625bfa81e154b3d1378ce4d06345fe2
parent 7c89fd90061ae1c97d96826111fc763190651821
Author: Marc Coquand <marc@coquand.email>
Date:   Sun,  1 Jun 2025 11:06:44 +0100

fix: Update

Diffstat:
Mmain.c | 44++++++++++++++++++++++++++++----------------
1 file changed, 28 insertions(+), 16 deletions(-)
diff --git a/main.c b/main.c
@@ -34,6 +34,7 @@ typedef struct {
 	time_t created_at;
 } journal_entry;
 
+// We create two maps that link to the same entries
 struct hashmap *entries_fuse_key = NULL;
 struct hashmap *entries_original_key = NULL;
 
@@ -182,24 +183,23 @@ static char *escape_text(const char *src)
 	return escaped;
 }
 
-static journal_entry *get_entry_from_path(const char *path)
+static journal_entry *get_entry_from_fuse_path(const char *path)
 {
 	if (!path || path[0] != '/') {
-		LOG("get_entry_from_path: invalid path '%s'\n",
-		    path ? path : "NULL");
+		LOG("invalid path '%s'\n", path ? path : "NULL");
 		return NULL;
 	}
 
 	const char *key = path + 1; // Strip leading '/'
 	if (strlen(key) == 0) {
-		LOG("get_entry_from_path: empty key for path '%s'\n", path);
+		LOG("empty key for path '%s'\n", path);
 		return NULL;
 	}
 
-	LOG("get_entry_from_path: looking up key '%s'\n", key);
+	LOG("looking up key '%s'\n", key);
 	journal_entry *entry = hashmap_get(entries_fuse_key, key);
 	if (!entry)
-		LOG("get_entry_from_path: no entry found for key '%s'\n", key);
+		LOG("no entry found for key '%s'\n", key);
 	return entry;
 }
 
@@ -482,8 +482,9 @@ int journal_getattr(const char *path, struct stat *stbuf,
 		goto unlock_and_return;
 	}
 
-	journal_entry *entry = get_entry_from_path(path);
+	journal_entry *entry = get_entry_from_fuse_path(path);
 	if (!entry) {
+		LOG("Entry not found");
 		ret_code = -ENOENT;
 		goto unlock_and_return;
 	}
@@ -494,6 +495,7 @@ int journal_getattr(const char *path, struct stat *stbuf,
 			    entry->description);
 	stbuf->st_size = (off_t)size;
 
+	LOG("Entry found %s, %s", entry->filename, entry->description);
 	stbuf->st_mtime = entry->timestamp;
 	stbuf->st_ctime = entry->timestamp;
 	stbuf->st_atime = time(NULL);
@@ -532,7 +534,7 @@ static int journal_open(const char *path, struct fuse_file_info *fi)
 {
 	pthread_mutex_lock(&entries_mutex);
 	LOG("Opening journal for %s", path);
-	journal_entry *entry = get_entry_from_path(path);
+	journal_entry *entry = get_entry_from_fuse_path(path);
 	int ret = entry ? 0 : -ENOENT;
 	pthread_mutex_unlock(&entries_mutex);
 	return ret;
@@ -545,12 +547,14 @@ static int journal_read(const char *path, char *buf, size_t size, off_t offset,
 	pthread_mutex_lock(&entries_mutex);
 	LOG("READ on path %s", path);
 
-	journal_entry *entry = get_entry_from_path(path);
+	journal_entry *entry = get_entry_from_fuse_path(path);
 	if (!entry) {
 		ret_code = -ENOENT;
 		goto unlock_and_return;
 	}
 
+	LOG("Got entry %s", entry->description);
+
 	// Calculate required length for full content
 	int needed_len = (size_t)snprintf(NULL, 0, "%s\n---\n%s",
 					  entry->summary, entry->description);
@@ -689,7 +693,7 @@ static int journal_write(const char *path, const char *buf, size_t size,
 			 off_t offset, struct fuse_file_info *fi)
 {
 	pthread_mutex_lock(&entries_mutex);
-	journal_entry *entry = get_entry_from_path(path);
+	journal_entry *entry = get_entry_from_fuse_path(path);
 	if (!entry) {
 		pthread_mutex_unlock(&entries_mutex);
 		return -ENOENT;
@@ -768,10 +772,17 @@ void update_fuse_entry_from_original(const char *filename)
 
 	new_entry->filename = strndup(entry->filename, strlen(entry->filename));
 	new_entry->filename_original =
-	    strndup(entry->filename_original, strlen(entry->filename));
+	    strndup(entry->filename_original, strlen(entry->filename_original));
+
+	LOG("Updating file path %s, new entry %s %s. New summary: %s , new "
+	    "desc: %s ",
+	    filename, new_entry->filename, new_entry->filename_original,
+	    new_entry->summary, new_entry->description);
+
+	hashmap_insert(entries_original_key, new_entry->filename_original,
+		       new_entry);
+	hashmap_insert(entries_fuse_key, new_entry->filename, new_entry);
 
-	hashmap_insert(entries_original_key, filename, new_entry);
-	hashmap_insert(entries_fuse_key, filename, new_entry);
 	LOG("File updated");
 
 	pthread_mutex_unlock(&entries_mutex);
@@ -787,6 +798,7 @@ void *watch_ics_dir(void *arg)
 
 	int wd =
 	    inotify_add_watch(fd, ICS_DIR, IN_CREATE | IN_DELETE | IN_MODIFY);
+
 	if (wd < 0) {
 		perror("inotify_add_watch");
 		close(fd);
@@ -816,8 +828,8 @@ void *watch_ics_dir(void *arg)
 				 event->name);
 
 			if (event->len && strstr(event->name, ".ics")) {
-				printf("Detected change in ICS file: %s\n",
-				       event->name);
+				LOG("Detected change in ICS file: %s\n",
+				    event->name);
 				update_fuse_entry_from_original(
 				    full_path); // Reload entries on any
 						// change
@@ -854,7 +866,7 @@ int main(int argc, char *argv[])
 
 	pthread_cancel(watcher_thread);	    // Stop the watcher when FUSE ends
 	pthread_join(watcher_thread, NULL); // Clean up
-	LOG("pthread cleaned up");
+	LOG("Pthread freed");
 
 	hashmap_free(entries_fuse_key);
 	hashmap_free(entries_original_key);