agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit fe691ed505fbc9386a30f35fc60daf51890f0014
parent aaca639fd5fbeab1865fe06e97e3edc0b69a7404
Author: Marc Coquand <marc@coquand.email>
Date:   Fri, 30 May 2025 21:12:21 +0100

Change formatting

Diffstat:
Mmain.c | 58++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 40 insertions(+), 18 deletions(-)
diff --git a/main.c b/main.c
@@ -119,6 +119,8 @@ static int parse_ics_file(const char *file_path, journal_entry *entry) {
             free(file_content);
             regfree(&regex_summary);
             regfree(&regex_description);
+            regfree(&regex_dtstamp);
+            regfree(&regex_lastmod);
             return -1;
         }
 
@@ -137,6 +139,8 @@ static int parse_ics_file(const char *file_path, journal_entry *entry) {
         free(file_content);
         regfree(&regex_summary);
         regfree(&regex_description);
+        regfree(&regex_dtstamp);
+        regfree(&regex_lastmod);
         return -1;
     }
     char *escaped_comma_pos = NULL;
@@ -239,7 +243,15 @@ static int journal_getattr(const char *path, struct stat *stbuf, struct fuse_fil
         if (strcmp(path, entry_path) == 0) {
             stbuf->st_mode = S_IFREG | 0444;
             stbuf->st_nlink = 1;
-            stbuf->st_size = strlen(entries[i].summary) + strlen(entries[i].description) + 2;
+            struct tm *tm_info = localtime(&entries[i].timestamp);
+            char timestamp_str[64];
+            strftime(timestamp_str, sizeof(timestamp_str), "%b %d, %Y at %H:%M", tm_info);
+
+            // Calculate the file size as in journal_read
+            int size = snprintf(NULL, 0, "%s\n%s\n---\n%s\n",
+                                entries[i].summary, timestamp_str, entries[i].description);
+
+            stbuf->st_size = (off_t)size;
 
             // Set the times
             stbuf->st_mtime = entries[i].timestamp;  // Use the timestamp as the modification time
@@ -287,37 +299,47 @@ static int journal_read(const char *path, char *buf, size_t size, off_t offset,
     for (size_t i = 0; i < entry_count; i++) {
         char entry_path[256];
         snprintf(entry_path, sizeof(entry_path), "/%s.txt", entries[i].filename);
-        if (strcmp(path, entry_path) == 0) {
-            // Concatenate SUMMARY and DESCRIPTION with proper handling
-            size_t summary_len = strlen(entries[i].summary);
-            size_t description_len = strlen(entries[i].description);
-            size_t total_len = summary_len + description_len + 2; // +1 for newline, +1 for null-terminator
 
-            char *full_content = malloc(total_len + 1);  // Allocate space for the entire string
+        if (strcmp(path, entry_path) == 0) {
+            // Format timestamp
+            struct tm *tm_info = localtime(&entries[i].timestamp);
+            char timestamp_str[64];
+            strftime(timestamp_str, sizeof(timestamp_str), "%b %d, %Y at %H:%M", tm_info);
+
+            // Build full content string using snprintf(NULL, 0) to get length
+            int needed_len = snprintf(NULL, 0, "%s\n%s\n---\n%s\n",
+                                      entries[i].summary, timestamp_str, entries[i].description);
+            if (needed_len < 0)
+                return -EIO; // snprintf error
+
+            char *full_content = malloc(needed_len + 1);
             if (!full_content) {
-                perror("Memory allocation failed for full content");
+                perror("Memory allocation failed");
                 return -ENOMEM;
             }
 
-            // Copy the summary and description, add a newline between them
-            snprintf(full_content, total_len + 1, "%s\n%s", entries[i].summary, entries[i].description);
+            snprintf(full_content, needed_len + 1, "%s\n%s\n---\n%s\n",
+                     entries[i].summary, timestamp_str, entries[i].description);
 
-            // Handle the offset
-            size_t len = strlen(full_content);
-            size_t to_copy = (offset + size > len) ? len - offset : size;
+            size_t len = (size_t)needed_len;
 
-            if (offset < len) {
-                memcpy(buf, full_content + offset, to_copy);
-            } else {
-                to_copy = 0;  // Nothing to read if offset is beyond the length of the file
+            if (offset >= len) {
+                free(full_content);
+                return 0;  // EOF
             }
 
+            if (offset + size > len)
+                size = len - offset;
+
+            memcpy(buf, full_content + offset, size);
+
             free(full_content);
-            return to_copy;
+            return size;
         }
     }
     return -ENOENT;
 }
+
 static const struct fuse_operations journal_oper = {
     .getattr = journal_getattr,
     .readdir = journal_readdir,