2 * Copyright (C) 2011-2014 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
21 #define _(String) gettext(String)
32 static const char *cache_dir;
34 static const char *get_cache_dir()
39 home = getenv("HOME");
42 cache_dir = malloc(strlen(home)
43 + 1 + strlen(".ppastats/cache")
45 sprintf((char *)cache_dir,
46 "%s/%s", home, ".ppastats/cache");
48 mkdirs(cache_dir, 0777);
50 log_warn(_("$HOME not defined"));
57 static char *key_to_path(const char *key)
62 if (!key || !*key || *key != '/')
65 dir = get_cache_dir();
69 path = malloc(strlen(dir) + 1 + strlen(key) + strlen("/data") + 1);
70 sprintf(path, "%s%s.data", dir, key);
75 char *fcache_get(const char *key)
79 path = key_to_path(key);
81 log_err(_("file cache, invalid key: %s"), key);
85 content = file_get_content(path);
88 log_debug(_("file cache hit %s"), key);
90 log_debug(_("file cache miss %s %s"), key, path);
97 void fcache_put(const char *key, const char *value)
99 char *path, *dir, *tmp;
102 path = key_to_path(key);
111 f = fopen(path, "w");
118 log_err(_("Failed to open %s"), path);
124 void fcache_cleanup()
126 free((char *)cache_dir);