/
boxcache.c
84 lines (68 loc) · 1.37 KB
/
boxcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "boxcache.h"
#include "boxutils.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
static char *cache_dir = NULL;
static time_t expire = 0;
void cache_init(const char * path, int expire_time)
{
//one-time initialization
if(!cache_dir && path) {
cache_dir = strdup(path);
expire = expire_time * 60;
}
}
char * make_path(const char * key)
{
if(!cache_dir) return NULL;
if(strstr(key, "../")) return NULL;
return pathappend(cache_dir, key);
}
char * cache_get(const char * key)
{
struct stat sb;
int sres;
char * fname = make_path(key);
FILE * kf;
char * v;
off_t flen, er;
kf = fopen(fname, "r");
if(!kf) { free(fname); return NULL; }
sres = stat(fname, &sb);
if(sres) { fclose(kf); free(fname); return NULL; }
flen = sb.st_size;
if(expire && ((time(NULL) - sb.st_mtime) > expire)) {
fclose(kf);
unlink(fname);
free(fname);
return NULL;
}
v = malloc(flen+1);
if (v) {
er = fread(v, 1, flen, kf);
v[er] = 0;
}
fclose(kf);
free(fname);
return v;
}
void cache_rm(const char * key)
{
char * fname = make_path(key);
if(fname) {
unlink(fname);
free(fname);
}
}
void cache_put(const char * key, const char * val)
{
char * fname = make_path(key);
FILE * kf = fopen(fname, "w");
free(fname);
if(!kf) return;
fwrite(val, strlen(val), 1, kf);
fclose(kf);
}