agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit efb6463b6e6ad131ae8d5d1501cba0b0afbfac9f
parent 1c531b7b619cb6e2a0d943e87aef435e625d68ee
Author: Marc Coquand <marc@coquand.email>
Date:   Tue, 22 Jul 2025 15:22:28 +0200

Code cleanup

Diffstat:
Mmain.c | 112+++++++++++++++++++++++++++++++++++--------------------------------------------
1 file changed, 50 insertions(+), 62 deletions(-)
diff --git a/main.c b/main.c
@@ -48,7 +48,7 @@ journal_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
 		// TODO: Probably should be set with an option?
 		stbuf->st_uid = getuid();
 		stbuf->st_gid = getgid();
-		goto unlock_and_return;
+		goto cleanup_return;
 	}
 
 	LOG("Getting node");
@@ -56,7 +56,7 @@ journal_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
 	if (!node) {
 		LOG("Entry not found");
 		ret_code = -ENOENT;
-		goto unlock_and_return;
+		goto cleanup_return;
 	}
 
 	LOG("icalcomponent from node");
@@ -90,7 +90,7 @@ journal_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
 
 	ret_code = 0;
 
-unlock_and_return:
+cleanup_return:
 	pthread_mutex_unlock(&entries_mutex);
 	rfree_all(mreg);
 	return ret_code;
@@ -111,6 +111,8 @@ journal_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
 	pthread_mutex_lock(&entries_mutex);
 	memory_region *mreg = create_region();
+	int status = 0;
+
 	LOG("READDIR %s", path);
 	filler(buf, ".", NULL, 0, 0);
 	filler(buf, "..", NULL, 0, 0);
@@ -166,14 +168,15 @@ journal_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 			}
 		}
 		else {
-			pthread_mutex_unlock(&entries_mutex);
-			rfree_all(mreg);
-			return -ENOENT;
+			status = -ENOENT;
+			goto cleanup_return;
 		}
 	}
+
+cleanup_return:
 	pthread_mutex_unlock(&entries_mutex);
 	rfree_all(mreg);
-	return 0;
+	return status;
 }
 
 static int
@@ -194,16 +197,12 @@ journal_mkdir(const char *path, mode_t mode)
 
 	memory_region *mreg = create_region();
 	pthread_mutex_lock(&entries_mutex);
+	int status = 0;
 	LOG("MKDIR %s", path);
-	if (create_directory_from_fuse_path(mreg, path) != 0) {
-		pthread_mutex_unlock(&entries_mutex);
-
-		rfree_all(mreg);
-		return -EIO;
-	};
+	status = create_directory_from_fuse_path(mreg, path);
 	pthread_mutex_unlock(&entries_mutex);
 	rfree_all(mreg);
-	return 0;
+	return status;
 }
 
 static int
@@ -219,7 +218,7 @@ journal_read(const char *path, char *buf, size_t size, off_t offset,
 	const struct tree_node *n = get_node_by_path(mreg, path);
 	if (!n) {
 		ret_code = -ENOENT;
-		goto unlock_and_return;
+		goto cleanup_return;
 	}
 	LOG("entry = %p", n);
 	icalcomponent *ic = get_icalcomponent_from_node(mreg, n);
@@ -235,7 +234,7 @@ journal_read(const char *path, char *buf, size_t size, off_t offset,
 
 	if ((size_t)offset >= content_len) {
 		ret_code = 0;
-		goto unlock_and_return;
+		goto cleanup_return;
 	}
 
 	if (offset + size > content_len) {
@@ -252,7 +251,7 @@ journal_read(const char *path, char *buf, size_t size, off_t offset,
 	LOG("Done");
 	ret_code = size;
 
-unlock_and_return:
+cleanup_return:
 	pthread_mutex_unlock(&entries_mutex);
 	rfree_all(mreg);
 	return ret_code;
@@ -440,23 +439,21 @@ journal_listxattr(const char *path, char *list, size_t size)
 	LOG("LISTXATTR %s %zu", list, size);
 	FILE *stream;
 	char *membuffer = NULL;
+	int return_i = 0;
 	size_t membuffer_len = 0;
-
 	memory_region *mreg = create_region();
 	pthread_mutex_lock(&entries_mutex);
+
 	struct tree_node *node = get_node_by_path(mreg, path);
 	if (!node) {
-		pthread_mutex_unlock(&entries_mutex);
-		rfree_all(mreg);
-
-		return -ENONET;
+		return_i = -ENONET;
+		goto cleanup_return;
 	}
 
 	stream = open_memstream(&membuffer, &membuffer_len);
 	if (stream == NULL) {
-		pthread_mutex_unlock(&entries_mutex);
-		rfree_all(mreg);
-		return -EIO;
+		return_i = -EIO;
+		goto cleanup_return;
 	}
 	char *cats = get_node_categories(mreg, node);
 	if (cats != NULL && strcmp("", cats) != 0) {
@@ -472,11 +469,8 @@ journal_listxattr(const char *path, char *list, size_t size)
 	}
 
 	if (fclose(stream) != 0) {
-		free(membuffer);
-
-		pthread_mutex_unlock(&entries_mutex);
-		rfree_all(mreg);
-		return -EIO;
+		return_i = -EIO;
+		goto cleanup_return;
 	}
 
 	if (size > 0) {
@@ -488,33 +482,36 @@ journal_listxattr(const char *path, char *list, size_t size)
 			list[size - 1] = '\0';
 		}
 	}
+	return_i = membuffer_len;
 
-	free(membuffer);
+cleanup_return:
+	rfree_all(mreg);
+	if (membuffer)
+		free(membuffer);
 	pthread_mutex_unlock(&entries_mutex);
-	return membuffer_len;
+	return return_i;
 }
 
 static int
 journal_getxattr(const char *path, const char *header, char *buf, size_t s)
 {
 	LOG("GETXATTR '%s' '%s' '%zu'\n", path, header, s);
+	pthread_mutex_lock(&entries_mutex);
 	memory_region *mreg = create_region();
+	int status = -ENODATA;
 
 	if (strcmp(header, "user.categories") == 0) {
-		pthread_mutex_lock(&entries_mutex);
 		struct tree_node *node = get_node_by_path(mreg, path);
 		if (!node) {
-			pthread_mutex_unlock(&entries_mutex);
-			rfree_all(mreg);
-			return -ENONET;
+			status = -ENONET;
+			goto cleanup_return;
 		}
 
 		const char *cats = get_node_categories(mreg, node);
-		pthread_mutex_unlock(&entries_mutex);
 
 		if (!cats || *cats == '\0') {
-			rfree_all(mreg);
-			return -ENODATA;
+			status = -ENODATA;
+			goto cleanup_return;
 		}
 
 		int string_len = strlen(cats);
@@ -524,25 +521,20 @@ journal_getxattr(const char *path, const char *header, char *buf, size_t s)
 		if (s > 0) {
 			snprintf(buf, s, "%s", cats);
 		}
-		rfree_all(mreg);
-		return string_len;
+		status = string_len;
+		goto cleanup_return;
 	}
 	else if (strcmp(header, "user.class") == 0) {
-		pthread_mutex_lock(&entries_mutex);
 		struct tree_node *node = get_node_by_path(mreg, path);
 		if (!node) {
-			pthread_mutex_unlock(&entries_mutex);
-
-			rfree_all(mreg);
-			return -ENONET;
+			status = -ENONET;
+			goto cleanup_return;
 		}
 
 		const char *c = get_node_class(mreg, node);
-		pthread_mutex_unlock(&entries_mutex);
 		if (!c || *c == '\0') {
-
-			rfree_all(mreg);
-			return -ENODATA;
+			status = -ENODATA;
+			goto cleanup_return;
 		}
 
 		int string_len = strlen(c);
@@ -551,12 +543,14 @@ journal_getxattr(const char *path, const char *header, char *buf, size_t s)
 			snprintf(buf, s, "%s", c);
 		}
 
-		rfree_all(mreg);
-		return string_len;
+		status = string_len;
+		goto cleanup_return;
 	}
 
+cleanup_return:
+	pthread_mutex_unlock(&entries_mutex);
 	rfree_all(mreg);
-	return -ENODATA;
+	return status;
 }
 
 static void
@@ -565,14 +559,13 @@ handle_ics_event(struct inotify_event *event)
 	if (!event->len || !strstr(event->name, ".ics"))
 		return;
 
+	memory_region *mreg = create_region();
+	pthread_mutex_lock(&entries_mutex);
 	LOG("Detected change in ICS file: %s\n", event->name);
 
-	memory_region *mreg = create_region();
 	char *full_path = NULL;
 	rasprintf(mreg, &full_path, "%s/%s", ICS_DIR, event->name);
 
-	pthread_mutex_lock(&entries_mutex);
-
 	if (event->mask & IN_DELETE) {
 		delete_from_original_path(mreg, full_path);
 	}
@@ -665,7 +658,6 @@ enum {
 
 static struct fuse_opt agendafs_opts[] = {
     CUSTOMFS_OPT("icsdir=%s", ics_directory, 0),
-
     FUSE_OPT_KEY("-V", KEY_VERSION),
     FUSE_OPT_KEY("--version", KEY_VERSION),
     FUSE_OPT_KEY("-h", KEY_HELP),
@@ -678,7 +670,6 @@ agendafs_opt_proc(void *data, const char *arg, int key,
 {
 	switch (key) {
 	case KEY_HELP:
-		LOG("Is KEY_HELP");
 		fprintf(stderr,
 			"usage: %s mountpoint [options]\n"
 			"\n"
@@ -690,7 +681,6 @@ agendafs_opt_proc(void *data, const char *arg, int key,
 		exit(1);
 
 	case KEY_VERSION:
-		LOG("Is KEY_VERSION");
 		fprintf(stderr, "agendafs version %s\n", "v0.01 ALPHA");
 		fuse_opt_add_arg(outargs, "--version");
 		fuse_main(outargs->argc, outargs->argv, &journal_oper, NULL);
@@ -707,8 +697,7 @@ main(int argc, char *argv[])
 	tzset();
 
 	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
-	struct agendafs_config conf;
-	memset(&conf, 0, sizeof(conf));
+	struct agendafs_config conf = {0};
 
 	fuse_opt_parse(&args, &conf, agendafs_opts, agendafs_opt_proc);
 
@@ -748,6 +737,5 @@ main(int argc, char *argv[])
 	free_tree(fuse_root);
 	LOG("Hashmap and tree freed");
 
-	LOG("Regexp freed");
 	exit(ret);
 }