X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fttrss_cache.c;fp=src%2Fttrss_cache.c;h=1526e25ad437776bb8256631a8cbc909553f7080;hb=a3f79452df23ac3db9e7a827e16097322de377c0;hp=0000000000000000000000000000000000000000;hpb=6ccb17c2d686381c564a33e84962199ef3e4019f;p=prss.git diff --git a/src/ttrss_cache.c b/src/ttrss_cache.c new file mode 100644 index 0000000..1526e25 --- /dev/null +++ b/src/ttrss_cache.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "io.h" +#include "log.h" +#include "ttrss_cache.h" + +static char *cache_dir; + +static const char *get_cache_dir() +{ + char *home; + + if (!cache_dir) { + home = getenv("HOME"); + + if (!home) + return NULL; + + cache_dir = path_append(home, ".prss/cache"); + mkdirs(cache_dir, 0777); + } + + return cache_dir; +} + +static gchar *content_get_path(const struct headline *h) +{ + const char *cache_dir; + + cache_dir = get_cache_dir(); + if (cache_dir) + return g_strdup_printf("%s/%d", cache_dir, h->id); + + return NULL; +} + +static void file_set_content(const char *path, const char *content) +{ + FILE *fp; + + log_debug("file_set_content(): path=%s", path); + + fp = fopen(path, "w"); + if (fp) { + fwrite(content, 1, strlen(content), fp); + fclose(fp); + } +} + +void cache_put(const struct headline *h, const char *content) +{ + char *path; + + path = content_get_path(h); + + if (path) + file_set_content(path, content); + + g_free(path); +} + +int cache_exists(const struct headline *h) +{ + struct stat s; + char *path; + int result; + + path = content_get_path(h); + + if (stat(path, &s) == -1) + result = 0; + else + result = 1; + + g_free(path); + + return result; +} + +char *cache_get(const struct headline *h) +{ + char *content, *path; + + path = content_get_path(h); + if (path) + content = file_get_content(path); + else + content = NULL; + + g_free(path); + + return content; +}