diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-07-18 20:18:53 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-07-18 20:18:53 -0400 |
commit | 16714b9d818c059605667fa7f589e6e9f6593833 (patch) | |
tree | 532c4eaf60f1b7521ed7dac1581ac09a1a82fa01 /index.c | |
parent | 78861aff93db4f705b0dfdaf545c26689593fdec (diff) |
creates index, but only for most recent post
Diffstat (limited to 'index.c')
-rw-r--r-- | index.c | 62 |
1 files changed, 35 insertions, 27 deletions
@@ -10,10 +10,39 @@ #include <errno.h> +static void insert_into(int blogdir, char *index_dir, const char *uri, const char *title) +{ + char index_path[FILENAME_MAX] = { 0 }; + snprintf(index_path, sizeof(index_path), "%s/index.html", index_dir); + + /* check for exisint index */ + int old_index = openat(blogdir, index_path, O_RDONLY); + + char new_path[FILENAME_MAX] = { 0 }; + snprintf(new_path, sizeof(new_path), "%s.new", index_path); + int new_index = openat(blogdir, new_path, O_WRONLY | O_CREAT, 0644); + + dprintf(new_index, HTML_HEAD, "index"); + dprintf(new_index, "<a href=\"/%s\">%s</a><br>", uri, title); + + if (old_index != -1) { + /* add old entries */ + close(old_index); + } + + dprintf(new_index, HTML_TAIL, current_year()); + + close(new_index); + + renameat(blogdir, new_path, blogdir, index_path); +} + void add_to_index(const char *path, const char *title) { + /* printf("Status: 200 OK\r\n"); printf("Content-Type: text/plain\r\n\r\n"); + */ int blogdir = open(DATA_DIRECTORY, O_DIRECTORY); if (blogdir == -1) { @@ -27,33 +56,12 @@ void add_to_index(const char *path, const char *title) return; } - char index_path[FILENAME_MAX] = { 0 }; - snprintf(index_path, sizeof(index_path), "%s/index.html", dirname(dir)); - - /* open index.html */ - int index_fd = openat(blogdir, index_path, O_RDWR | O_CREAT, 0644); - if (index_fd == -1) { - printf("open(index_path): %s\n", strerror(errno)); - return; - } - - /* find the end of text */ - off_t eot = 0; - - if (eot == 0) { - dprintf(index_fd, HTML_HEAD, "index"); - } else { - ftruncate(index_fd, eot); - } - - /* add link */ - dprintf(index_fd, "<a href=\"/%s\">%s</a><br>", path, title); - - /* (re-)add footer */ - dprintf(index_fd, HTML_TAIL, current_year()); - - close(index_fd); - + char *day = dirname(dir); + insert_into(blogdir, day, path, title); + char *month = dirname(day); + insert_into(blogdir, month, path, title); + char *year = dirname(month); + insert_into(blogdir, year, path, title); free(dir); close(blogdir); |