Added README.
[agg.git] / fs.c
blob4ab6f3a9416a12598224c23ac4f96474844ddc40
1 /* agg - the news aggregator
2 * Copyright (C) 2011 Andreas Waidler <arandes@programmers.at>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #define _XOPEN_SOURCE
17 #include <sys/time.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <time.h>
25 #include "config.h"
26 #include "bool.h"
27 #include "fail.h"
28 #include "fs.h"
30 char feed_title[TEXT_BUFFER_SIZE] = { 0 };
31 time_t feed_date = 0;
32 time_t feed_date_new = 0;
33 bool feed_is_open = false;
35 char item_title[TEXT_BUFFER_SIZE] = { 0 };
36 char item_link [TEXT_BUFFER_SIZE] = { 0 };
37 char item_desc [TEXT_BUFFER_SIZE] = { 0 };
38 time_t item_date = 0;
40 void sanitize(char* str)
42 char* evil;
43 while ((evil = strchr(str, '/'))) *evil = '\\';
46 void set_item_date(const char* date)
48 struct tm t;
49 assert(strptime(date, "%a, %d %b %Y %T %z", &t));
50 item_date = mktime(&t);
51 assert(item_date != -1);
52 if (item_date <= feed_date) {
53 printf("No new feeds assumed.\n");
54 fail(0);
55 } else if (item_date > feed_date_new) {
56 feed_date_new = item_date;
60 void set_item_link(const char* link)
62 strncpy(item_link, link, TEXT_BUFFER_SIZE);
65 void set_item_desc(const char* desc)
67 strncpy(item_desc, desc, TEXT_BUFFER_SIZE);
70 void set_item_title(const char* title)
72 strncpy(item_title, title, TEXT_BUFFER_SIZE);
73 sanitize(item_title);
76 void feed_open(const char* title)
78 struct stat st;
80 strncpy(feed_title, title, TEXT_BUFFER_SIZE);
81 sanitize(feed_title);
83 if (!stat(feed_title, &st)) feed_date = st.st_mtime;
84 mkdir(feed_title, 0755);
85 assert(!chdir(feed_title));
86 feed_is_open = true;
91 void feed_flush()
93 struct timeval times[2];
94 times[0].tv_sec = feed_date_new;
95 times[0].tv_usec = 0;
96 times[1].tv_sec = feed_date_new;
97 times[1].tv_usec = 0;
99 if (!feed_is_open) return;
101 assert(!chdir(".."));
102 assert(!utimes(feed_title, times));
103 feed_is_open = false;
106 void item_flush()
108 struct timeval times[2];
109 FILE* f;
110 char buf[FILE_NAME_LENGTH + 1] = { 0 };
111 assert(feed_is_open);
113 if (item_title[0]) {
114 strncpy(buf, item_title, FILE_NAME_LENGTH);
115 } else {
116 assert(item_desc[0]);
117 strncpy(buf, item_desc, FILE_NAME_LENGTH);
120 assert(f = fopen(buf, "w"));
121 if (item_title[0]) {
122 fprintf(f, "%s\n\n", item_title);
124 if (item_desc[0]) {
125 fprintf(f, "%s\n\n", item_desc);
127 if (item_link[0]) {
128 fprintf(f, "Link: <a href=\"%s\">%s</a>\n", item_link, item_link);
130 fclose(f);
132 /* FIXME: item date might not have been set. */
133 times[0].tv_sec = item_date;
134 times[0].tv_usec = 0;
135 times[1].tv_sec = item_date;
136 times[1].tv_usec = 0;
138 assert(!utimes(buf, times));