2 * Copyright (C) 2010-2013 jeanfi@gmail.com
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.
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.
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
23 #include <sys/types.h>
32 #include "ttrss_cache.h"
34 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
35 static char *cache_dir;
37 static const char *get_cache_dir()
42 home = getenv("HOME");
47 cache_dir = path_append(home, ".prss/cache");
48 mkdirs(cache_dir, 0777);
54 static gchar *content_get_path(const struct headline *h)
56 const char *cache_dir;
58 cache_dir = get_cache_dir();
60 return g_strdup_printf("%s/%d", cache_dir, h->id);
65 static void file_set_content(const char *path, const char *content)
69 log_debug("file_set_content(): path=%s", path);
71 fp = fopen(path, "w");
73 fwrite(content, 1, strlen(content), fp);
78 void cache_put(const struct headline *h, const char *content)
82 path = content_get_path(h);
85 pthread_mutex_lock(&lock);
86 file_set_content(path, content);
87 pthread_mutex_unlock(&lock);
93 int cache_exists(const struct headline *h)
99 path = content_get_path(h);
101 pthread_mutex_lock(&lock);
102 if (stat(path, &s) == -1)
106 pthread_mutex_unlock(&lock);
113 char *cache_get(const struct headline *h)
115 char *content, *path;
118 path = content_get_path(h);
120 pthread_mutex_lock(&lock);
121 content = file_get_content(path);
122 pthread_mutex_unlock(&lock);