agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit 97a34c3d98a936d1a33fd19d9068e934dd075b08
parent 9723ca9f6310bf9353ec7985328e8a84b9100462
Author: Marc Coquand <marc@coquand.email>
Date:   Sun,  1 Jun 2025 21:26:13 +0100

Add unlink functionality

Diffstat:
MREADME.md | 3++-
Mmain.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
@@ -70,10 +70,11 @@ POC stage, still very early alpha.
 Planned features:
 - [ ] VJOURNAL - Now the first true journaling filesystem!
 	- [x] Create
-	- [ ] Delete
+	- [x] Delete
 	- [x] Read
 	- [x] Rename
 	- [x] Write
+	- [ ] Set creation dates - not mandatory but supresses a few warnings
 	- [ ] Directory structure
 - [ ] VTODO
 - [ ] Categories (Maybe via symlinks?)
diff --git a/main.c b/main.c
@@ -892,6 +892,53 @@ do_journal_entry_rename(time_t new_created_at, const char *old, const char *new)
 	return 0;
 }
 
+// Aka remove or delete
+static int
+journal_unlink(const char *file)
+{
+	pthread_mutex_lock(&entries_mutex);
+	LOG("DELETE %s", file);
+	int res = 0;
+
+	// Strip path, validate just the new basename
+	const char *keyname = strrchr(file, '/');
+	keyname++;
+
+	journal_entry *entry = hashmap_get(entries_fuse_key, keyname);
+	if (!entry) {
+		LOG("Entry does not exist");
+		pthread_mutex_unlock(&entries_mutex);
+		return -EIO;
+	}
+	LOG("Found entry to delete");
+
+	char *filepath = get_original_filepath(entry);
+	if (!filepath) {
+		LOG("Entry not found");
+		pthread_mutex_unlock(&entries_mutex);
+		return -EIO;
+	}
+	LOG("Deleting file %s", filepath);
+
+	res = remove(filepath);
+	if (res != 0) {
+		LOG("Failed to delete file");
+		pthread_mutex_unlock(&entries_mutex);
+
+		free(filepath);
+		return -EIO;
+	}
+
+	hashmap_remove(entries_original_key, keyname);
+	// Frees
+	hashmap_remove(entries_fuse_key, keyname);
+	free(filepath);
+
+	LOG("Delete successful");
+	pthread_mutex_unlock(&entries_mutex);
+	return 0;
+}
+
 static int
 journal_rename(const char *old, const char *new, unsigned int flags)
 {
@@ -1029,8 +1076,8 @@ watch_ics_dir(void *arg)
 		return NULL;
 	}
 
-	int wd =
-	    inotify_add_watch(fd, ICS_DIR, IN_CREATE | IN_DELETE | IN_MODIFY);
+	// TODO: Fix IN_DELETE
+	int wd = inotify_add_watch(fd, ICS_DIR, IN_CREATE | IN_MODIFY);
 
 	if (wd < 0) {
 		perror("inotify_add_watch");
@@ -1083,6 +1130,7 @@ static const struct fuse_operations journal_oper = {.getattr = journal_getattr,
 						    .read = journal_read,
 						    .write = journal_write,
 						    .create = journal_create,
+						    .unlink = journal_unlink,
 						    .rename = journal_rename};
 
 int