fixed code style
[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 #include <pthread.h>
29
30 #include "io.h"
31 #include "log.h"
32 #include "ttrss_cache.h"
33
34 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
35 static char *cache_dir;
36
37 static const char *get_cache_dir()
38 {
39         char *home;
40
41         if (!cache_dir) {
42                 home = getenv("HOME");
43
44                 if (!home)
45                         return NULL;
46
47                 cache_dir = path_append(home, ".prss/cache");
48                 mkdirs(cache_dir, 0777);
49         }
50
51         return cache_dir;
52 }
53
54 static gchar *content_get_path(int id)
55 {
56         const char *cache_dir;
57
58         cache_dir = get_cache_dir();
59         if (cache_dir)
60                 return g_strdup_printf("%s/%d", cache_dir, id);
61
62         return NULL;
63 }
64
65 static void file_set_content(const char *path, const char *content)
66 {
67         FILE *fp;
68
69         log_debug("file_set_content(): path=%s", path);
70
71         fp = fopen(path, "w");
72         if (fp) {
73                 fwrite(content, 1, strlen(content), fp);
74                 fclose(fp);
75         }
76 }
77
78 void cache_put(int id, const char *content)
79 {
80         char *path;
81
82         path = content_get_path(id);
83
84         if (path) {
85                 pthread_mutex_lock(&lock);
86                 file_set_content(path, content);
87                 pthread_mutex_unlock(&lock);
88         }
89
90         g_free(path);
91 }
92
93 int cache_exists(const struct headline *h)
94 {
95         struct stat s;
96         char *path;
97         int result;
98
99         path = content_get_path(h->id);
100
101         pthread_mutex_lock(&lock);
102         if (stat(path, &s) == -1)
103                 result = 0;
104         else
105                 result = 1;
106         pthread_mutex_unlock(&lock);
107
108         g_free(path);
109
110         return result;
111 }
112
113 char *cache_get(const struct headline *h)
114 {
115         char *content, *path;
116
117
118         path = content_get_path(h->id);
119         if (path) {
120                 pthread_mutex_lock(&lock);
121                 content = file_get_content(path);
122                 pthread_mutex_unlock(&lock);
123         } else {
124                 content = NULL;
125         }
126
127         g_free(path);
128
129         return content;
130 }