Fixed acceptance tests and requiring now that item title/desc comes first.
[agg.git] / src / fs.c
blobc679134274f4f6eb0fefde10a2aa05a805ef2aae
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 <sys/stat.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <time.h>
26 #include "config.h"
27 #include "bool.h"
28 #include "fail.h"
29 #include "fs.h"
31 char feed_title[TEXT_BUFFER_SIZE] = { 0 };
32 time_t feed_date = 0;
33 time_t feed_date_new = 0;
34 bool feed_is_open = false;
36 char item_title[TEXT_BUFFER_SIZE] = { 0 };
37 char item_link [TEXT_BUFFER_SIZE] = { 0 };
38 char item_desc [TEXT_BUFFER_SIZE] = { 0 };
39 time_t item_date = 0;
41 void sanitize(char* str)
43 char* evil;
44 while ((evil = strchr(str, '/'))) *evil = '\\';
47 void set_item_date(const char* date)
49 struct tm t;
50 assert(item_title[0]); /* Has to be set first. */
51 assert(strptime(date, "%a, %d %b %Y %T %z", &t));
52 item_date = mktime(&t);
53 assert(item_date != -1);
54 if (item_date <= feed_date) {
55 printf("No new feeds assumed.\n");
56 fail(0);
57 } else if (item_date > feed_date_new) {
58 feed_date_new = item_date;
62 void set_item_link(const char* link)
64 assert(item_title[0]); /* Has to be set first. */
65 strncpy(item_link, link, TEXT_BUFFER_SIZE);
68 void set_item_desc(const char* desc)
70 strncpy(item_desc, desc, TEXT_BUFFER_SIZE);
73 void set_item_title(const char* title)
75 strncpy(item_title, title, TEXT_BUFFER_SIZE - 1);
77 assert(item_desc[0] == 0); /* Has to be set first. */
78 if (strlen(title) >= TEXT_BUFFER_SIZE) {
79 item_title[TEXT_BUFFER_SIZE - 4] = '.';
80 item_title[TEXT_BUFFER_SIZE - 3] = '.';
81 item_title[TEXT_BUFFER_SIZE - 2] = '.';
84 sanitize(item_title);
87 const char* get_item_title()
89 return item_title;
92 void feed_open(const char* title)
94 struct stat st;
96 strncpy(feed_title, title, TEXT_BUFFER_SIZE);
97 sanitize(feed_title);
99 if (!stat(feed_title, &st)) feed_date = st.st_mtime;
100 mkdir(feed_title, 0755);
101 assert(!chdir(feed_title));
102 feed_is_open = true;
107 void feed_flush()
109 struct timeval times[2];
110 times[0].tv_sec = feed_date_new;
111 times[0].tv_usec = 0;
112 times[1].tv_sec = feed_date_new;
113 times[1].tv_usec = 0;
115 if (!feed_is_open) return;
117 assert(!chdir(".."));
118 assert(!utimes(feed_title, times));
119 feed_is_open = false;
122 void item_flush()
124 struct timeval times[2];
125 FILE* f;
126 char buf[FILE_NAME_LENGTH + 1] = { 0 };
127 assert(feed_is_open);
129 if (item_title[0]) {
130 strncpy(buf, item_title, FILE_NAME_LENGTH);
131 } else {
132 assert(item_desc[0]);
133 strncpy(buf, item_desc, FILE_NAME_LENGTH);
136 assert(f = fopen(buf, "w"));
137 if (item_title[0]) {
138 fprintf(f, "%s\n\n", item_title);
140 if (item_desc[0]) {
141 fprintf(f, "%s\n\n", item_desc);
143 if (item_link[0]) {
144 fprintf(f, "Link: <a href=\"%s\">%s</a>\n", item_link, item_link);
146 fclose(f);
148 /* FIXME: item date might not have been set. */
149 times[0].tv_sec = item_date;
150 times[0].tv_usec = 0;
151 times[1].tv_sec = item_date;
152 times[1].tv_usec = 0;
154 assert(!utimes(buf, times));
156 item_title[0] = 0;
157 item_desc[0] = 0;
158 item_link[0] = 0;