use file cache for distros
[ppastats.git] / src / fcache.c
1 /*
2  * Copyright (C) 2011-2012 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 <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <libgen.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "fcache.h"
29 #include "io.h"
30 #include "log.h"
31
32 static const char *cache_dir;
33
34 static const char *get_cache_dir()
35 {
36         char *home;
37
38         if (!cache_dir) {
39                 home = getenv("HOME");
40
41                 if (home) {
42                         cache_dir = malloc(strlen(home)
43                                            + 1 + strlen(".ppastats/cache")
44                                            + 1);
45                         sprintf((char *)cache_dir,
46                                 "%s/%s", home, ".ppastats/cache");
47
48                         mkdirs(cache_dir, 0777);
49                 } else {
50                         log_warn(_("$HOME not defined"));
51                 }
52         }
53
54         return cache_dir;
55 }
56
57 static char *key_to_path(const char *key)
58 {
59         char *path;
60         const char *dir;
61
62         dir = get_cache_dir();
63         if (!dir)
64                 return NULL;
65
66         path = malloc(strlen(dir) + 1 + strlen(key) + strlen("/data.json") + 1);
67         sprintf(path, "%s%s/data.json", dir, key);
68
69         return path;
70 }
71
72 char *fcache_get(const char *key)
73 {
74         char *path, *content;
75
76         path = key_to_path(key);
77         if (!path)
78                 return NULL;
79
80         content = file_get_content(path);
81         
82         if (content)
83                 log_debug(_("file cache hit %s"), key);
84         else
85                 log_debug(_("file cache miss %s %s"), key, path);
86         
87         free(path);
88         
89         return content;
90 }
91         
92 void fcache_put(const char *key, char *value)
93 {
94         char *path, *dir, *tmp;
95         FILE *f;
96
97         path = key_to_path(key);
98         if (!path)
99                 return ;
100
101         tmp = strdup(path);
102         dir = dirname(tmp);
103         mkdirs(dir, 0777);
104         free(tmp);
105
106         f = fopen(path, "w");
107
108         if (f) {
109                 fputs(value, f);
110
111                 fclose(f);
112         } else {
113                 log_err(_("Failed to open %s"), path);
114         }
115
116         free(path);
117 }
118
119 void fcache_cleanup()
120 {
121         free((char *)cache_dir);
122 }