1526e25ad437776bb8256631a8cbc909553f7080
[prss.git] / src / ttrss_cache.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include <glib.h>
28
29 #include "io.h"
30 #include "log.h"
31 #include "ttrss_cache.h"
32
33 static char *cache_dir;
34
35 static const char *get_cache_dir()
36 {
37         char *home;
38
39         if (!cache_dir) {
40                 home = getenv("HOME");
41
42                 if (!home)
43                         return NULL;
44
45                 cache_dir = path_append(home, ".prss/cache");
46                 mkdirs(cache_dir, 0777);
47         }
48
49         return cache_dir;
50 }
51
52 static gchar *content_get_path(const struct headline *h)
53 {
54         const char *cache_dir;
55
56         cache_dir = get_cache_dir();
57         if (cache_dir)
58                 return g_strdup_printf("%s/%d", cache_dir, h->id);
59
60         return NULL;
61 }
62
63 static void file_set_content(const char *path, const char *content)
64 {
65         FILE *fp;
66
67         log_debug("file_set_content(): path=%s", path);
68
69         fp = fopen(path, "w");
70         if (fp) {
71                 fwrite(content, 1, strlen(content), fp);
72                 fclose(fp);
73         }
74 }
75
76 void cache_put(const struct headline *h, const char *content)
77 {
78         char *path;
79
80         path = content_get_path(h);
81         
82         if (path)
83                 file_set_content(path, content);
84
85         g_free(path);
86 }
87
88 int cache_exists(const struct headline *h)
89 {
90         struct stat s;
91         char *path;
92         int result;
93
94         path = content_get_path(h);
95
96         if (stat(path, &s) == -1)
97                 result = 0;
98         else
99                 result = 1;
100
101         g_free(path);
102
103         return result;
104 }
105
106 char *cache_get(const struct headline *h)
107 {
108         char *content, *path;
109
110         path = content_get_path(h);
111         if (path)
112                 content = file_get_content(path);
113         else
114                 content = NULL;
115
116         g_free(path);
117
118         return content;
119 }