save bpph list into file cache
[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         if (!key || !*key || *key != '/')
63                 return NULL;
64
65         dir = get_cache_dir();
66         if (!dir)
67                 return NULL;
68
69         path = malloc(strlen(dir) + 1 + strlen(key) + strlen("/data") + 1);
70         sprintf(path, "%s%s.data", dir, key);
71
72         return path;
73 }
74
75 char *fcache_get(const char *key)
76 {
77         char *path, *content;
78
79         path = key_to_path(key);
80         if (!path) {
81                 log_err(_("file cache, invalid key: %s"), key);
82                 return NULL;
83         }
84
85         content = file_get_content(path);
86
87         if (content)
88                 log_debug(_("file cache hit %s"), key);
89         else
90                 log_debug(_("file cache miss %s %s"), key, path);
91
92         free(path);
93
94         return content;
95 }
96
97 void fcache_put(const char *key, const char *value)
98 {
99         char *path, *dir, *tmp;
100         FILE *f;
101
102         path = key_to_path(key);
103         if (!path)
104                 return ;
105
106         tmp = strdup(path);
107         dir = dirname(tmp);
108         mkdirs(dir, 0777);
109         free(tmp);
110
111         f = fopen(path, "w");
112
113         if (f) {
114                 fputs(value, f);
115
116                 fclose(f);
117         } else {
118                 log_err(_("Failed to open %s"), path);
119         }
120
121         free(path);
122 }
123
124 void fcache_cleanup()
125 {
126         free((char *)cache_dir);
127 }