agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit e5ae393962889325af44afacad3eb8435a93b247
parent 9d9e0a8558fa5ba87af9b1a48ee3137de7044d81
Author: Marc Coquand <marc@coquand.email>
Date:   Tue, 29 Jul 2025 00:20:28 +0200

Refactor

Diffstat:
Mmain.c | 76++++++++++++++++++++++------------------------------------------------------
1 file changed, 22 insertions(+), 54 deletions(-)
diff --git a/main.c b/main.c
@@ -373,34 +373,21 @@ fuse_removexattr(const char *path, const char *attribute)
 	memory_region *mreg = create_region();
 	int status = -EINVAL;
 	pthread_mutex_lock(&entries_mutex);
+	struct tree_node *node = get_node_by_path(mreg, path);
+	if (!node) {
+		status = -ENONET;
+		goto cleanup_return;
+	}
 
 	if (strcmp(attribute, "user.categories") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		status = set_node_categories(mreg, node, "", 0);
 		goto cleanup_return;
 	}
 	else if (strcmp(attribute, "user.class") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		status = delete_node_class(mreg, node);
 		goto cleanup_return;
 	}
 	else if (starts_with_str(attribute, "user.")) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		icalcomponent *ic = get_icalcomponent_from_node(mreg, node);
 
 		const char *key = attribute + 5;
@@ -433,23 +420,17 @@ fuse_setxattr(const char *path, const char *attribute, const char *value,
 	if (strlen(value) < 64 * 1024) {
 		status = -EMSGSIZE;
 	}
-	if (strcmp(attribute, "user.categories") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
+	struct tree_node *node = get_node_by_path(mreg, path);
+	if (!node) {
+		status = -ENONET;
+		goto cleanup_return;
+	}
 
+	if (strcmp(attribute, "user.categories") == 0) {
 		status = set_node_categories(mreg, node, value, s);
 		goto cleanup_return;
 	}
 	else if (strcmp(attribute, "user.class") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		enum icalproperty_class iclass = parse_ical_class(value);
 
 		if (iclass == ICAL_CLASS_X) {
@@ -461,12 +442,6 @@ fuse_setxattr(const char *path, const char *attribute, const char *value,
 		goto cleanup_return;
 	}
 	else if (starts_with_str(attribute, "user.")) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		icalcomponent *ic = get_icalcomponent_from_node(mreg, node);
 
 		const char *rkey = attribute + 5;
@@ -560,14 +535,15 @@ fuse_getxattr(const char *path, const char *attribute, 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;
+	int status = 0;
+
+	struct tree_node *node = get_node_by_path(mreg, path);
+	if (!node) {
+		status = -ENONET;
+		goto cleanup_return;
+	}
 
 	if (strcmp(attribute, "user.categories") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
 
 		const char *cats = get_node_categories(mreg, node);
 
@@ -587,12 +563,6 @@ fuse_getxattr(const char *path, const char *attribute, char *buf, size_t s)
 		goto cleanup_return;
 	}
 	else if (strcmp(attribute, "user.class") == 0) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		const char *c = get_node_class(mreg, node);
 		if (!c || *c == '\0') {
 			status = -ENODATA;
@@ -609,12 +579,6 @@ fuse_getxattr(const char *path, const char *attribute, char *buf, size_t s)
 		goto cleanup_return;
 	}
 	else if (starts_with_str(attribute, "user.")) {
-		struct tree_node *node = get_node_by_path(mreg, path);
-		if (!node) {
-			status = -ENONET;
-			goto cleanup_return;
-		}
-
 		icalcomponent *ic = get_icalcomponent_from_node(mreg, node);
 
 		const char *key = attribute + 5;
@@ -630,6 +594,10 @@ fuse_getxattr(const char *path, const char *attribute, char *buf, size_t s)
 		status = string_len;
 		goto cleanup_return;
 	}
+	else {
+		status = -ENODATA;
+		goto cleanup_return;
+	}
 
 cleanup_return:
 	pthread_mutex_unlock(&entries_mutex);