Now failing on duplicate item titles.
[agg.git] / src / fs.c
blob96c9c1b00ab38fd84f6bb82fc450c77a71653b1b
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;
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 fs_buffer(char* dest, const char* src, size_t size)
42 assert(dest);
43 assert(src);
44 assert(size);
46 strncpy(dest, src, size);
48 /* Destination is not null-terminated iff
49 * strlen(src) >= size. */
50 if (dest[size - 1] != '\0') {
51 dest[size - 4] = '.';
52 dest[size - 3] = '.';
53 dest[size - 2] = '.';
54 dest[size - 1] = '\0';
57 assert(strlen(dest) < size);
60 void sanitize(char* str)
62 char* evil;
63 while ((evil = strchr(str, '/'))) *evil = '\\';
66 void set_item_date(const char* date)
68 struct tm t;
69 assert(strptime(date, "%a, %d %b %Y %T %z", &t));
70 item_date = mktime(&t);
71 assert(item_date != -1);
74 void set_item_link(const char* link)
76 fs_buffer(item_link, link, TEXT_BUFFER_SIZE);
79 void set_item_desc(const char* desc)
81 fs_buffer(item_desc, desc, TEXT_BUFFER_SIZE);
84 void set_item_title(const char* title)
86 assert(!item_title[0]);
87 fs_buffer(item_title, title, TEXT_BUFFER_SIZE);
90 const char* get_item_title()
92 return item_title;
95 void feed_open(const char* title)
97 struct stat st;
99 if (feed_title[0]) {
100 /* Feed contained title twice. The
101 * directory may just have been created.
102 * Deletion may lead to loss of previous
103 * mtime. We don't care; that feed is
104 * fucked up anyways. */
105 chdir("..");
106 rmdir(feed_title);
107 fail(111);
110 fs_buffer(feed_title, title, TEXT_BUFFER_SIZE);
111 sanitize(feed_title);
113 if (!stat(feed_title, &st)) feed_date = st.st_mtime;
114 mkdir(feed_title, 0755);
115 assert(!chdir(feed_title));
120 void feed_flush()
122 struct timeval times[2];
123 times[0].tv_sec = feed_date_new;
124 times[0].tv_usec = 0;
125 times[1].tv_sec = feed_date_new;
126 times[1].tv_usec = 0;
128 if (!feed_title[0]) return;
130 assert(!chdir(".."));
131 assert(!utimes(feed_title, times));
132 feed_title[0] = '\0';
136 void fs_set(const char* name, const char* value)
138 FILE* f;
140 assert(name);
141 assert(value);
143 /* Only write if attribute has a value. */
144 if (value[0]) {
145 assert(f = fopen(name, "w"));
146 fprintf(f, "%s", value);
147 fclose(f);
151 void item_flush()
153 struct timeval times[2];
154 char buf[FILE_NAME_LENGTH + 1] = { 0 };
155 const char* name_src;
156 assert(feed_title[0]);
158 /* snip: from set_item_date */
159 /* assert(item_title[0]); */
160 if (item_date > 0 && item_date <= feed_date) {
161 printf("No new feeds assumed.\n");
162 fail(0);
163 } else if (item_date > feed_date_new) {
164 feed_date_new = item_date;
166 /* snap */
168 name_src = item_title[0] ? item_title : item_desc;
169 assert(name_src[0]);
170 fs_buffer(buf, name_src, FILE_NAME_LENGTH + 1);
171 sanitize(buf);
173 assert(mkdir(buf, 0755) == 0);
174 assert(chdir(buf) == 0);
175 fs_set("title", item_title);
176 fs_set("desc", item_desc);
177 fs_set("link", item_link);
178 assert(chdir("..") == 0);
180 /* FIXME: item date might not have been set. */
183 times[0].tv_sec = item_date;
184 times[0].tv_usec = 0;
185 times[1].tv_sec = item_date;
186 times[1].tv_usec = 0;
188 assert(!utimes(buf, times));
190 item_title[0] = 0;
191 item_desc[0] = 0;
192 item_link[0] = 0;