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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include "Cache.h"
#include <dirent.h>
#include <openssl/evp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
static char cache_dir[512] = {0};
static int cache_ttl_search_val = 3600;
static int cache_ttl_infobox_val = 86400;
void set_cache_ttl_search(int ttl) { cache_ttl_search_val = ttl; }
void set_cache_ttl_infobox(int ttl) { cache_ttl_infobox_val = ttl; }
int get_cache_ttl_search(void) { return cache_ttl_search_val; }
int get_cache_ttl_infobox(void) { return cache_ttl_infobox_val; }
static void md5_hash(const char *str, char *output) {
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (!ctx)
return;
EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
EVP_DigestUpdate(ctx, str, strlen(str));
EVP_DigestFinal_ex(ctx, hash, &hash_len);
EVP_MD_CTX_free(ctx);
for (unsigned int i = 0; i < hash_len; i++) {
sprintf(output + (i * 2), "%02x", hash[i]);
}
}
static time_t get_file_mtime(const char *filepath) {
struct stat st;
if (stat(filepath, &st) == 0) {
return st.st_mtime;
}
return 0;
}
int cache_init(const char *dir) {
if (!dir || strlen(dir) == 0) {
strcpy(cache_dir, "/tmp/omnisearch_cache");
} else {
strncpy(cache_dir, dir, sizeof(cache_dir) - 1);
cache_dir[sizeof(cache_dir) - 1] = '\0';
}
struct stat st;
if (stat(cache_dir, &st) != 0) {
if (mkdir(cache_dir, 0755) != 0) {
fprintf(stderr, "Failed to create cache directory: %s\n", cache_dir);
return -1;
}
} else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "Cache path exists but is not a directory: %s\n",
cache_dir);
return -1;
}
char subdirs[] = "0123456789abcdef";
for (int i = 0; subdirs[i]; i++) {
char subdir_path[1024];
snprintf(subdir_path, sizeof(subdir_path), "%s/%c", cache_dir, subdirs[i]);
if (stat(subdir_path, &st) != 0) {
mkdir(subdir_path, 0755);
}
}
return 0;
}
void cache_shutdown(void) { cache_dir[0] = '\0'; }
char *cache_compute_key(const char *query, int page, const char *engine_name) {
char key_buffer[1024];
snprintf(key_buffer, sizeof(key_buffer), "%s_%d_%s", query ? query : "", page,
engine_name ? engine_name : "");
char *hash = malloc(33);
if (!hash) {
return NULL;
}
md5_hash(key_buffer, hash);
return hash;
}
int cache_get(const char *key, time_t max_age, char **out_data,
size_t *out_size) {
if (!key || !out_data || !out_size || cache_dir[0] == '\0') {
return -1;
}
char filepath[1024];
snprintf(filepath, sizeof(filepath), "%s/%c/%s.cache", cache_dir, key[0],
key);
time_t file_mtime = get_file_mtime(filepath);
if (file_mtime == 0) {
return -1;
}
time_t now = time(NULL);
if (max_age > 0 && (now - file_mtime) > max_age) {
remove(filepath);
return -1;
}
FILE *fp = fopen(filepath, "rb");
if (!fp) {
return -1;
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (size <= 0) {
fclose(fp);
return -1;
}
char *data = malloc(size + 1);
if (!data) {
fclose(fp);
return -1;
}
size_t read_size = fread(data, 1, size, fp);
fclose(fp);
if (read_size != (size_t)size) {
free(data);
return -1;
}
data[size] = '\0';
*out_data = data;
*out_size = size;
return 0;
}
int cache_set(const char *key, const char *data, size_t size) {
if (!key || !data || size == 0 || cache_dir[0] == '\0') {
return -1;
}
char filepath[1024];
snprintf(filepath, sizeof(filepath), "%s/%c/%s.cache", cache_dir, key[0],
key);
FILE *fp = fopen(filepath, "wb");
if (!fp) {
return -1;
}
size_t written = fwrite(data, 1, size, fp);
fclose(fp);
if (written != size) {
remove(filepath);
return -1;
}
return 0;
}
void cache_cleanup(time_t max_age) {
if (cache_dir[0] == '\0' || max_age <= 0) {
return;
}
time_t now = time(NULL);
time_t cutoff = now - max_age;
char subdirs[] = "0123456789abcdef";
for (int d = 0; subdirs[d]; d++) {
char subdir_path[1024];
snprintf(subdir_path, sizeof(subdir_path), "%s/%c", cache_dir, subdirs[d]);
DIR *dir = opendir(subdir_path);
if (!dir)
continue;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
size_t len = strlen(entry->d_name);
if (len > 7 && strcmp(entry->d_name + len - 7, ".cache") == 0) {
char filepath[2048];
snprintf(filepath, sizeof(filepath), "%s/%s", subdir_path,
entry->d_name);
struct stat st;
if (stat(filepath, &st) == 0 && st.st_mtime < cutoff) {
remove(filepath);
}
}
}
closedir(dir);
}
}
|