agendafs
A filesystem for your calendar.
git clone git://mccd.space/agendafscommit ed8423a8474094d9eb6999b6695be5026deb69e8
parent 92d8f0a630d770f321a5078d2b0ce6b326b6a401
Author: Marc Coquand <marc@coquand.email>
Date: Mon, 14 Jul 2025 15:20:35 +0200
Refactor
Diffstat:
4 files changed, 32 insertions(+), 64 deletions(-)
diff --git a/journal_entry.c b/journal_entry.c
@@ -423,14 +423,11 @@ move_node(struct tree_node *old_parent, struct tree_node *new_parent,
// Returns -1 on failure, 0 on success
int
-load_journal_entry_from_ics_file(char *filename, struct journal_entry *entry)
+load_journal_entry_from_ics_file(const char *filename,
+ struct journal_entry *entry)
{
- char *filepath = NULL;
+ path *filepath = append_path(ICS_DIR, filename);
- if (asprintf(&filepath, "%s/%s", ICS_DIR, filename) == -1) {
- perror("aspintf failed");
- return -1;
- }
struct stat fileStat;
if (stat(filepath, &fileStat) == -1) {
LOG("Can not stat %s. skipping", filepath);
@@ -438,12 +435,7 @@ load_journal_entry_from_ics_file(char *filename, struct journal_entry *entry)
return -1;
}
- entry->filename_original = strdup(filename);
- if (!entry->filename_original) {
- perror("strdup failed");
- free(filepath);
- return -1;
- }
+ entry->filename_original = xstrdup(filename);
if (parse_ics_to_journal_entry_component(filepath, entry) != 0) {
LOG("Failed to parse entry: %s", filename);
@@ -498,8 +490,7 @@ get_parent_uid(icalcomponent *component)
return NULL;
}
-// Initializes values
-// Should only be run once at the start
+
void
load_journal_entries()
{
@@ -524,7 +515,7 @@ load_journal_entries()
}
struct dirent *entry;
- // First pass: Store everything in root tree, fill up hashmap
+ LOG("Load files to root directory");
while ((entry = readdir(dir))) {
if (entry->d_type == DT_REG && strstr(entry->d_name, ".ics")) {
@@ -541,19 +532,16 @@ load_journal_entries()
struct tree_node *new_node =
create_file_node(new_entry);
- size_t res = hashmap_insert(
- entries_original_ics, new_entry->filename_original,
- new_node);
- assert(res == 0);
+ hashmap_insert(entries_original_ics,
+ new_entry->filename_original, new_node);
LOG("Inserted filename_original: %s",
new_entry->filename_original);
}
}
closedir(dir);
- LOG("Setting up directories");
+ LOG("Set up directories according to parent-child");
- // Second pass, reattach according to parent-child
size_t n_keys = 0;
char **keys = hashmap_get_keys(entries_original_ics, &n_keys);
for (size_t i = 0; i < n_keys; i++) {
@@ -579,6 +567,7 @@ load_journal_entries()
}
}
}
+
for (int i = 0; i < n_keys; i++) {
free(keys[i]);
}
@@ -938,12 +927,10 @@ load_agendafs_environment(char *ics_dir_env)
int
do_journal_entry_rename(const char *old, const char *new)
{
- char *new_copy = strdup(new);
- if (!new_copy)
- return -ENOMEM;
- char *new_filename = get_filename(new_copy);
+ char *new_copy = xstrdup(new);
+ const char *new_filename = get_filename(new_copy);
- char *old_filename = get_filename(old);
+ const char *old_filename = get_filename(old);
if (!old_filename)
return -EINVAL;
@@ -954,11 +941,10 @@ do_journal_entry_rename(const char *old, const char *new)
free(parent_path);
if (new_parent_node == NULL) {
-
return -EINVAL;
}
- char *new_summary = strdup(new_filename);
+ char *new_summary = xstrdup(new_filename);
// Remove everything after extension
// TODO: Store the extension as a separate field in the ics file
@@ -966,40 +952,25 @@ do_journal_entry_rename(const char *old, const char *new)
if (dot) {
*dot = '\0';
}
+
LOG("Summary is now %s", new_summary);
struct tree_node *entry_node = get_node_by_path(old);
- if (!entry_node) {
- free(new_copy);
- free(new_summary);
-
- return -EIO;
- }
+ assert(entry_node);
struct tree_node *old_parent_node = entry_node->parent;
struct journal_entry *old_entry = entry_node->data;
- if (!old_entry) {
- LOG("Entry not found '%s'", old);
- free(new_copy);
- free(new_summary);
- return -EIO;
- }
+ assert(old_entry);
struct journal_entry *new_entry = copy_journal_entry(old_entry);
- if (!new_entry) {
- free(new_copy);
- free(new_summary);
- return -EIO;
- }
-
free(parent_path);
// TODO: Filename must be unique
- asprintf(&new_entry->filename, "%s", new_filename);
- LOG("New filename is now %s", new_filename);
+ size_t wRes = asprintf(&new_entry->filename, "%s", new_filename);
+ assert(wRes != -1);
+ LOG("New filename is now %s", new_filename);
LOG("Removed original entry from fuse");
-
LOG("Inserting %s", new_entry->filename_original);
hashmap_insert(entries_original_ics, new_entry->filename_original,
entry_node);
@@ -1102,12 +1073,7 @@ int
create_entry_from_fuse(const char *fuse_path)
{
// TODO: Move to path.c
- const char *entry_prefix = "/";
- if (strncmp(fuse_path, entry_prefix, strlen(entry_prefix)) != 0) {
- return -EINVAL;
- }
- char *new_basename = strrchr(fuse_path, '/');
- new_basename++;
+ const char *new_basename = get_filename(fuse_path);
path *without_extension = without_file_extension(new_basename);
@@ -1211,7 +1177,7 @@ create_directory_from_fuse_path(const char *fuse_path)
void
update_or_create_fuse_entry_from_original(const char *filepath_original)
{
- char *filename_original = get_filename(filepath_original);
+ const char *filename_original = get_filename(filepath_original);
LOG("Filename original is %s", filename_original);
struct journal_entry *updated_entry =
diff --git a/journal_entry.h b/journal_entry.h
@@ -51,7 +51,7 @@ get_original_filepath(const struct journal_entry *entry);
void
free_journal_entry(void *val);
-char *
+const char *
get_filename(const char *full_path);
struct journal_entry *
@@ -76,7 +76,8 @@ write_entry_to_ical_file(const struct journal_entry *entry);
// Returns -1 on failure, 0 on success
int
-load_journal_entry_from_ics_file(char *filename, struct journal_entry *entry);
+load_journal_entry_from_ics_file(const char *filename,
+ struct journal_entry *entry);
// We assume that there is only one entry in the file
int
@@ -104,7 +105,8 @@ delete_from_original_path(const char *filepath);
// Returns -1 on failure, 0 on success
int
-load_journal_entry_from_ics_file(char *filename, struct journal_entry *entry);
+load_journal_entry_from_ics_file(const char *filename,
+ struct journal_entry *entry);
icalcomponent *
create_vjournal_directory(char *summary);
diff --git a/path.c b/path.c
@@ -29,7 +29,7 @@ free_segments(char **segments, size_t count)
}
path *
-append_path(path *parentp, char *childp)
+append_path(const path *parentp, const char *childp)
{
char *filepath = NULL;
size_t res = asprintf(&filepath, "%s/%s", parentp, childp);
@@ -37,7 +37,7 @@ append_path(path *parentp, char *childp)
return filepath;
}
-path *
+const path *
get_filename(const char *full_path)
{
char *base_name = strrchr(full_path, '/');
@@ -91,7 +91,7 @@ split_path(const path *p, char **segments)
}
size_t
-write_to_file(const char *filepath, const char *content)
+write_to_file(const path *filepath, const char *content)
{
FILE *f = fopen(filepath, "w");
if (!f) {
diff --git a/path.h b/path.h
@@ -14,9 +14,9 @@ void
free_segments(path **, size_t);
path *
-append_path(path *, char *);
+append_path(const path *, const char *);
-path *
+const path *
get_filename(const path *);
size_t