Initial commit.
[agg.git] / fs.c
blobd073c9e893ea768cc83e2629dbd45cc0f7da7746
1 #define _XOPEN_SOURCE
2 #include <sys/time.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <time.h>
10 #include "config.h"
11 #include "bool.h"
12 #include "fail.h"
13 #include "fs.h"
15 char feed_title[TEXT_BUFFER_SIZE] = { 0 };
16 time_t feed_date = 0;
17 time_t feed_date_new = 0;
18 bool feed_is_open = false;
20 char item_title[TEXT_BUFFER_SIZE] = { 0 };
21 char item_link [TEXT_BUFFER_SIZE] = { 0 };
22 char item_desc [TEXT_BUFFER_SIZE] = { 0 };
23 time_t item_date = 0;
25 void sanitize(char* str)
27 char* evil;
28 while ((evil = strchr(str, '/'))) *evil = '\\';
31 void set_item_date(const char* date)
33 struct tm t;
34 assert(strptime(date, "%a, %d %b %Y %T %z", &t));
35 item_date = mktime(&t);
36 assert(item_date != -1);
37 if (item_date <= feed_date) {
38 printf("No new feeds assumed.\n");
39 fail(0);
40 } else if (item_date > feed_date_new) {
41 feed_date_new = item_date;
45 void set_item_link(const char* link)
47 strncpy(item_link, link, TEXT_BUFFER_SIZE);
50 void set_item_desc(const char* desc)
52 strncpy(item_desc, desc, TEXT_BUFFER_SIZE);
55 void set_item_title(const char* title)
57 strncpy(item_title, title, TEXT_BUFFER_SIZE);
58 sanitize(item_title);
61 void feed_open(const char* title)
63 struct stat st;
65 strncpy(feed_title, title, TEXT_BUFFER_SIZE);
66 sanitize(feed_title);
68 if (!stat(feed_title, &st)) feed_date = st.st_mtime;
69 mkdir(feed_title, 0755);
70 assert(!chdir(feed_title));
71 feed_is_open = true;
76 void feed_flush()
78 struct timeval times[2];
79 times[0].tv_sec = feed_date_new;
80 times[0].tv_usec = 0;
81 times[1].tv_sec = feed_date_new;
82 times[1].tv_usec = 0;
84 if (!feed_is_open) return;
86 assert(!chdir(".."));
87 assert(!utimes(feed_title, times));
88 feed_is_open = false;
91 void item_flush()
93 struct timeval times[2];
94 FILE* f;
95 char buf[FILE_NAME_LENGTH + 1];
96 assert(feed_is_open);
98 if (item_title[0]) {
99 strncpy(buf, item_title, 32);
100 } else {
101 assert(item_desc[0]);
102 strncpy(buf, item_desc, 32);
105 assert(f = fopen(buf, "w"));
106 if (item_title[0]) {
107 fprintf(f, "%s\n\n", item_title);
109 if (item_desc[0]) {
110 fprintf(f, "%s\n\n", item_desc);
112 if (item_link[0]) {
113 fprintf(f, "Link: <a href=\"%s\">%s</a>\n", item_link, item_link);
115 fclose(f);
117 /* FIXME: item date might not have been set. */
118 times[0].tv_sec = item_date;
119 times[0].tv_usec = 0;
120 times[1].tv_sec = item_date;
121 times[1].tv_usec = 0;
123 assert(!utimes(buf, times));