agendafs
A filesystem for your calendar.
git clone git://mccd.space/agendafscommit 589679c0ed66b976f3fa4dc42f1e05451413821a
parent 28e05e1ede376bd9460cbb8afe7a185046f30cc4
Author: Marc Coquand <marc@coquand.email>
Date: Tue, 29 Jul 2025 10:17:02 +0200
Simplify
Diffstat:
7 files changed, 24 insertions(+), 103 deletions(-)
diff --git a/agenda_entry.c b/agenda_entry.c
@@ -42,21 +42,12 @@ free_agenda_entry(struct agenda_entry *entry)
return;
}
-// Frees old filename and copies over the new
-void
-set_filename(struct agenda_entry *e, const char *new)
-{
- free(e->filename);
- e->filename = xstrdup(new);
-}
-
struct agenda_entry *
create_agenda_entry(memory_region *mreg, const char *filename,
const char *filename_vdir)
{
- struct agenda_entry *copy = xmalloc(sizeof(struct agenda_entry));
- copy->filename = xstrdup(filename);
- copy->filename_vdir = xstrdup(filename_vdir);
- region_register(mreg, copy, (void *)free_agenda_entry);
+ struct agenda_entry *copy = rmalloc(mreg, sizeof(struct agenda_entry));
+ copy->filename = rstrdup(mreg, filename);
+ copy->filename_vdir = rstrdup(mreg, filename_vdir);
return copy;
}
diff --git a/fuse_node.c b/fuse_node.c
@@ -50,8 +50,6 @@ get_node_by_path(memory_region *mreg, const char *path)
char *segments[255];
size_t count = split_path(mreg, path, segments);
- LOG("Segments are defined");
-
struct tree_node *current = fuse_root;
for (size_t i = 0; i < count && current; ++i) {
@@ -74,7 +72,6 @@ get_node_by_path(memory_region *mreg, const char *path)
}
current = next;
}
- LOG("Done");
return current;
}
@@ -595,8 +592,11 @@ insert_fuse_node_to_path(memory_region *mreg, const char *fuse_path,
return 0;
}
+enum ENTRY_TYPE { ENTRY_DIRECTORY, ENTRY_FILE };
+
int
-create_entry_from_fuse(memory_region *mreg, const char *fuse_path)
+create_entry_from_fuse(memory_region *mreg, const char *fuse_path,
+ enum ENTRY_TYPE etype)
{
const char *entry_prefix = "/";
if (strncmp(fuse_path, entry_prefix, strlen(entry_prefix)) != 0) {
@@ -604,49 +604,16 @@ create_entry_from_fuse(memory_region *mreg, const char *fuse_path)
}
const char *new_filename = get_filename(fuse_path);
- icalcomponent *new_component =
- create_vjournal_entry(mreg, new_filename);
-
- char *new_filname_vdir = NULL;
- rasprintf(mreg, &new_filname_vdir, "%s.ics",
- icalcomponent_get_uid(new_component));
-
- struct agenda_entry *new_entry =
- create_agenda_entry(mreg, new_filename, new_filname_vdir);
+ icalcomponent *new_component = NULL;
- LOG("Inserting vjournal directory");
-
- struct tree_node *new_node = create_fuse_node(new_entry);
-
- int status =
- insert_fuse_node_to_path(mreg, fuse_path, new_node, new_component);
-
- if (status != 0) {
- return -ENOENT;
+ assert(etype == ENTRY_DIRECTORY || etype == ENTRY_FILE);
+ if (etype == ENTRY_DIRECTORY) {
+ new_component = create_vjournal_directory(mreg, new_filename);
}
-
- if (write_ical_file(mreg, new_node, new_component) != 0) {
- LOG("Write to entry failed");
- delete_fuse_node(new_node);
- return -EIO;
+ else if (etype == ENTRY_FILE) {
+ new_component = create_vjournal_entry(mreg, new_filename);
}
- LOG("FILE CREATED");
- return 0;
-}
-
-int
-create_directory_from_fuse_path(memory_region *mreg, const char *fuse_path)
-{
-
- const char *entry_prefix = "/";
- if (strncmp(fuse_path, entry_prefix, strlen(entry_prefix)) != 0) {
- return -EINVAL;
- }
- const char *new_filename = get_filename(fuse_path);
- icalcomponent *new_component =
- create_vjournal_directory(mreg, new_filename);
-
char *new_filname_vdir = NULL;
rasprintf(mreg, &new_filname_vdir, "%s.ics",
icalcomponent_get_uid(new_component));
@@ -671,7 +638,7 @@ create_directory_from_fuse_path(memory_region *mreg, const char *fuse_path)
return -EIO;
}
- LOG("DIRECTORY CREATED");
+ LOG("FILE CREATED");
return 0;
}
@@ -787,6 +754,7 @@ delete_vdir_entry(memory_region *mreg, struct tree_node *node)
return res;
}
+// Caller needs to ensure the node has no children.
int
delete_from_vdir_path(memory_region *mreg, const char *filepath)
{
@@ -796,12 +764,7 @@ delete_from_vdir_path(memory_region *mreg, const char *filepath)
return -ENOENT;
}
- LOG("Found node to delete");
-
- // TODO: Subdirectories SHOULD NOT BE REMOVED HERE
- // Instead, update the children so they have a new parent
- // which is the parent node.
- // Requires a change_parent function.
+ assert(node->child_count > 0);
delete_fuse_node(node);
return 0;
}
diff --git a/fuse_node.h b/fuse_node.h
@@ -95,8 +95,11 @@ insert_fuse_node_to_path(memory_region *mreg, const char *fuse_path,
struct tree_node *child_node,
icalcomponent *child_ics);
+enum ENTRY_TYPE { ENTRY_DIRECTORY, ENTRY_FILE };
+
int
-create_entry_from_fuse(memory_region *mreg, const char *fuse_path);
+create_entry_from_fuse(memory_region *mreg, const char *fuse_path,
+ enum ENTRY_TYPE etype);
int
create_directory_from_fuse_path(memory_region *mreg, const char *fuse_path);
diff --git a/fuse_node_store.c b/fuse_node_store.c
@@ -65,13 +65,6 @@ get_entry(const struct tree_node *node)
return node->data;
}
-const char *
-oget_node_filename(struct tree_node *node)
-{
- const struct agenda_entry *entry = get_entry(node);
- return entry->filename;
-}
-
bool
is_root_node(const struct tree_node *node)
{
@@ -137,7 +130,7 @@ upsert_fuse_node(const struct agenda_entry *entry)
size_t
add_fuse_child(struct tree_node *parent, struct tree_node *child)
{
- const char *filename = oget_node_filename(child);
+ const char *filename = get_node_filename(child);
char *new_filename = xstrdup(filename);
@@ -147,7 +140,7 @@ add_fuse_child(struct tree_node *parent, struct tree_node *child)
bool is_unique = true;
for (size_t i = 0; i < parent->child_count; i++) {
const char *sibling_filename =
- oget_node_filename(parent->children[i]);
+ get_node_filename(parent->children[i]);
if (strcmp(new_filename, sibling_filename) == 0) {
is_unique = false;
diff --git a/main.c b/main.c
@@ -149,7 +149,7 @@ fuse_mkdir(const char *path, mode_t mode)
pthread_mutex_lock(&entries_mutex);
int status = 0;
- status = create_directory_from_fuse_path(mreg, path);
+ status = create_entry_from_fuse(mreg, path, ENTRY_DIRECTORY);
pthread_mutex_unlock(&entries_mutex);
rfree_all(mreg);
@@ -358,7 +358,7 @@ fuse_create(const char *filepath, mode_t mode, struct fuse_file_info *info)
}
// TODO: Handle mode and fuse_file_info
- create_entry_from_fuse(mreg, filepath);
+ create_entry_from_fuse(mreg, filepath, ENTRY_FILE);
cleanup_return:
pthread_mutex_unlock(&entries_mutex);
diff --git a/mregion.c b/mregion.c
@@ -122,32 +122,6 @@ rasprintf(memory_region *region, char **strp, const char *fmt, ...)
return ret;
}
-void
-rappend(char **dest, const char *fmt, ...)
-{
- va_list args;
-
- // Determine formatted size
- va_start(args, fmt);
- int add_len = vsnprintf(NULL, 0, fmt, args);
- va_end(args);
-
- if (add_len < 0)
- return;
-
- size_t old_len = *dest ? strlen(*dest) : 0;
- char *newbuf = realloc(*dest, old_len + add_len + 1);
- if (!newbuf)
- return;
-
- // Write formatted string to end
- va_start(args, fmt);
- vsnprintf(newbuf + old_len, add_len + 1, fmt, args);
- va_end(args);
-
- *dest = newbuf;
-}
-
// iCal functions
icalparser *
diff --git a/mregion.h b/mregion.h
@@ -45,9 +45,6 @@ rstrdup(memory_region *region, const char *str);
int
rasprintf(memory_region *region, char **strp, const char *fmt, ...);
-void
-rappend(char **dest, const char *fmt, ...);
-
// iCal functions
icalparser *