Now testing a seperate method fs_buffer.
[agg.git] / src / fs.c
blob5c5539821ac2e7f53f3711b469d01eb1afcb99f1
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 fs_buffer(char* dest, const char* src, size_t size)
43 assert(dest);
44 assert(src);
45 assert(size);
47 strncpy(dest, src, size);
49 /* Destination is not null-terminated iff
50 * strlen(src) >= size. */
51 if (dest[size - 1] != '\0') {
52 dest[size - 4] = '.';
53 dest[size - 3] = '.';
54 dest[size - 2] = '.';
55 dest[size - 1] = '\0';
58 assert(strlen(dest) < size);
61 void sanitize(char* str)
63 char* evil;
64 while ((evil = strchr(str, '/'))) *evil = '\\';
67 void set_item_date(const char* date)
69 struct tm t;
70 assert(item_title[0]);
71 assert(strptime(date, "%a, %d %b %Y %T %z", &t));
72 item_date = mktime(&t);
73 assert(item_date != -1);
74 if (item_date <= feed_date) {
75 printf("No new feeds assumed.\n");
76 fail(0);
77 } else if (item_date > feed_date_new) {
78 feed_date_new = item_date;
82 void set_item_link(const char* link)
84 assert(item_title[0]);
85 fs_buffer(item_link, link, TEXT_BUFFER_SIZE);
88 void set_item_desc(const char* desc)
90 fs_buffer(item_desc, desc, TEXT_BUFFER_SIZE);
93 void set_item_title(const char* title)
95 assert(item_desc[0] == 0);
96 fs_buffer(item_title, title, TEXT_BUFFER_SIZE);
99 const char* get_item_title()
101 return item_title;
104 void feed_open(const char* title)
106 struct stat st;
108 fs_buffer(feed_title, title, TEXT_BUFFER_SIZE);
109 sanitize(feed_title);
111 if (!stat(feed_title, &st)) feed_date = st.st_mtime;
112 mkdir(feed_title, 0755);
113 assert(!chdir(feed_title));
114 feed_is_open = true;
119 void feed_flush()
121 struct timeval times[2];
122 times[0].tv_sec = feed_date_new;
123 times[0].tv_usec = 0;
124 times[1].tv_sec = feed_date_new;
125 times[1].tv_usec = 0;
127 if (!feed_is_open) return;
129 assert(!chdir(".."));
130 assert(!utimes(feed_title, times));
131 feed_is_open = false;
134 void item_flush()
136 struct timeval times[2];
137 FILE* f;
138 char buf[FILE_NAME_LENGTH + 1] = { 0 };
139 const char* name_src;
140 assert(feed_is_open);
143 name_src = item_title[0] ? item_title : item_desc;
144 assert(name_src[0]);
145 fs_buffer(buf, name_src, FILE_NAME_LENGTH + 1);
146 sanitize(buf);
148 assert(mkdir(buf, 0755) == 0);
149 assert(chdir(buf) == 0);
150 if (item_title[0]) {
151 assert(f = fopen("title", "w"));
152 fprintf(f, "%s", item_title);
153 fclose(f);
155 if (item_desc[0]) {
156 assert(f = fopen("desc", "w"));
157 fprintf(f, "%s", item_desc);
158 fclose(f);
160 if (item_link[0]) {
161 assert(f = fopen("link", "w"));
162 fprintf(f, "%s", item_link);
163 fclose(f);
165 assert(chdir("..") == 0);
167 /* FIXME: item date might not have been set. */
168 times[0].tv_sec = item_date;
169 times[0].tv_usec = 0;
170 times[1].tv_sec = item_date;
171 times[1].tv_usec = 0;
173 assert(!utimes(buf, times));
175 item_title[0] = 0;
176 item_desc[0] = 0;
177 item_link[0] = 0;