| 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 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include "config.h" |
| 28 |
#endif |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <sys/param.h> |
| 32 |
#include <sys/file.h> |
| 33 |
#include <sys/errno.h> |
| 34 |
#ifdef ultrix |
| 35 |
#include <fcntl.h> |
| 36 |
#else |
| 37 |
#include <sys/fcntl.h> |
| 38 |
#endif |
| 39 |
#include <unistd.h> |
| 40 |
#include <stdio.h> |
| 41 |
#ifdef HAVE_STRING_H |
| 42 |
#include <string.h> |
| 43 |
#endif |
| 44 |
#include <dirent.h> |
| 45 |
#include "rplayd.h" |
| 46 |
#include "sound.h" |
| 47 |
#include "spool.h" |
| 48 |
#include "cache.h" |
| 49 |
|
| •50 |
|
| 51 |
#ifndef MAXPATHLEN |
| 52 |
#define MAXPATHLEN 4096 |
| 53 |
#endif |
| 54 |
|
| 55 |
static DIR *cache_dir; |
| 56 |
static char cache_path[MAXPATHLEN]; |
| 57 |
static char cache_directory[MAXPATHLEN]; |
| 58 |
|
| 59 |
int cache_max_size = RPLAY_CACHE_SIZE; |
| 60 |
int cache_remove = 0; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#ifdef __STDC__ |
| 66 |
void |
| 67 |
cache_init(char *dir_name) |
| 68 |
#else |
| 69 |
void |
| 70 |
cache_init(dir_name) |
| 71 |
char *dir_name; |
| 72 |
#endif |
| 73 |
{ |
| 74 |
struct stat st; |
| 75 |
|
| 76 |
strcpy(cache_directory, dir_name); |
| 77 |
if (stat(cache_directory, &st) < 0) |
| 78 |
{ |
| 79 |
report(REPORT_DEBUG, "creating cache directory `%s'\n", cache_directory); |
| 80 |
if (mkdir(cache_directory, 0777) < 0) |
| 81 |
{ |
| 82 |
report(REPORT_ERROR, "cache_init: cannot create cache directory '%s'\n", cache_directory); |
| 83 |
} |
| 84 |
} |
| 85 |
else if (!S_ISDIR(st.st_mode)) |
| 86 |
{ |
| 87 |
report(REPORT_ERROR, "cache_init: %s not a directory\n", cache_directory); |
| 88 |
done(1); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
char * |
| 96 |
cache_first() |
| 97 |
{ |
| 98 |
if (cache_dir) |
| 99 |
{ |
| 100 |
rewinddir(cache_dir); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
cache_dir = opendir(cache_directory); |
| 105 |
if (cache_dir == NULL) |
| 106 |
{ |
| 107 |
report(REPORT_ERROR, "cache_first: opendir %s: %s\n", cache_directory, sys_err_str(errno)); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return cache_next(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
char * |
| 119 |
cache_next() |
| 120 |
{ |
| 121 |
struct dirent *dp; |
| 122 |
|
| 123 |
do |
| 124 |
{ |
| 125 |
dp = readdir(cache_dir); |
| 126 |
if (dp == NULL) |
| 127 |
{ |
| 128 |
closedir(cache_dir); |
| 129 |
cache_dir = NULL; |
| 130 |
return ""; |
| 131 |
} |
| 132 |
} |
| 133 |
while (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0); |
| 134 |
|
| 135 |
SNPRINTF(SIZE(cache_path, sizeof(cache_path)), "%s/%s", cache_directory, dp->d_name); |
| 136 |
|
| 137 |
return cache_path; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
int |
| 144 |
cache_size() |
| 145 |
{ |
| 146 |
struct stat st; |
| 147 |
int size = 0; |
| 148 |
char *file; |
| 149 |
|
| 150 |
for (file = cache_first(); file && *file; file = cache_next()) |
| 151 |
{ |
| 152 |
if (stat(file, &st) < 0) |
| 153 |
{ |
| 154 |
report(REPORT_ERROR, "cache_size: stat %s: %s\n", file, sys_err_str(errno)); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
size += (int) st.st_size; |
| 158 |
} |
| 159 |
|
| 160 |
return (int) size; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void |
| 167 |
cache_read() |
| 168 |
{ |
| 169 |
char *file; |
| 170 |
|
| 171 |
for (file = cache_first(); file && *file; file = cache_next()) |
| 172 |
{ |
| 173 |
sound_insert(file, SOUND_READY, SOUND_FILE); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
#ifdef __STDC__ |
| 183 |
char * |
| 184 |
cache_name(char *sound) |
| 185 |
#else |
| 186 |
char * |
| 187 |
cache_name(sound) |
| 188 |
char *sound; |
| 189 |
#endif |
| 190 |
{ |
| 191 |
static char _cache_path[MAXPATHLEN]; |
| 192 |
|
| 193 |
SNPRINTF(SIZE(_cache_path, sizeof(_cache_path)), "%s/%s", cache_directory, sound); |
| 194 |
|
| 195 |
return _cache_path; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#ifdef __STDC__ |
| 202 |
int |
| 203 |
cache_free(int size) |
| 204 |
#else |
| 205 |
int |
| 206 |
cache_free(size) |
| 207 |
int size; |
| 208 |
#endif |
| 209 |
{ |
| 210 |
int curr_size, limit; |
| 211 |
char *file; |
| 212 |
SOUND *s; |
| 213 |
struct stat st; |
| 214 |
int n; |
| 215 |
|
| 216 |
if (cache_max_size == 0) |
| 217 |
{ |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
if (size > cache_max_size) |
| 222 |
{ |
| 223 |
report(REPORT_DEBUG, "%d bytes cannot fit in the cache\n", size); |
| 224 |
return -1; |
| 225 |
} |
| 226 |
|
| 227 |
curr_size = cache_size(); |
| 228 |
|
| 229 |
report(REPORT_DEBUG, "sound_count = %d, current cache size = %d bytes\n", sound_count, curr_size); |
| 230 |
|
| 231 |
if (size + curr_size <= cache_max_size) |
| 232 |
{ |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
limit = sound_count / 2; |
| 240 |
|
| 241 |
for (;;) |
| 242 |
{ |
| 243 |
report(REPORT_DEBUG, "removing cache entries (limit = %d)\n", limit); |
| 244 |
|
| 245 |
for (file = cache_first(); file && *file; file = cache_next()) |
| 246 |
{ |
| 247 |
s = sound_lookup(file, SOUND_DONT_COUNT, NULL); |
| 248 |
if (s == NULL || s->status != SOUND_READY) |
| 249 |
{ |
| 250 |
continue; |
| 251 |
} |
| 252 |
if (s->count < limit) |
| 253 |
{ |
| 254 |
if (stat(file, &st) < 0) |
| 255 |
{ |
| 256 |
report(REPORT_ERROR, "cache_free: stat %s: %s\n", file, sys_err_str(errno)); |
| 257 |
continue; |
| 258 |
} |
| 259 |
curr_size -= (int) st.st_size; |
| 260 |
spool_remove(s); |
| 261 |
sound_delete(s, 1); |
| 262 |
report(REPORT_DEBUG, "removed %s size=%d count=%d\n", file, st.st_size, s->count); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (size + curr_size <= cache_max_size) |
| 267 |
{ |
| 268 |
break; |
| 269 |
} |
| 270 |
else if (limit == sound_count) |
| 271 |
{ |
| 272 |
report(REPORT_ERROR, "cache_free: cannot make room for %d bytes in the cache\n", size); |
| 273 |
return -1; |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
n = limit / 4; |
| 278 |
limit += n ? n : 1; |
| 279 |
limit = MIN(limit, sound_count); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#ifdef __STDC__ |
| 290 |
int |
| 291 |
cache_create(char *name, int size) |
| 292 |
#else |
| 293 |
int |
| 294 |
cache_create(name, size) |
| 295 |
char *name; |
| 296 |
int size; |
| 297 |
#endif |
| 298 |
{ |
| 299 |
int fd; |
| 300 |
|
| 301 |
fd = open(name, O_RDWR | O_CREAT, 0666); |
| 302 |
if (fd < 0) |
| 303 |
{ |
| 304 |
report(REPORT_ERROR, "cache_create: open: %s\n", sys_err_str(errno)); |
| 305 |
return -1; |
| 306 |
} |
| 307 |
|
| 308 |
if (lseek(fd, size - 1, SEEK_SET) < 0) |
| 309 |
{ |
| 310 |
report(REPORT_ERROR, "cache_create: lseek: %d %s\n", sys_err_str(errno)); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
restart: |
| 315 |
if (write(fd, "", 1) != 1) |
| 316 |
{ |
| 317 |
if (errno == EINTR || errno == EAGAIN) |
| 318 |
{ |
| 319 |
goto restart; |
| 320 |
} |
| 321 |
report(REPORT_ERROR, "cache_create: write: %s\n", sys_err_str(errno)); |
| 322 |
return -1; |
| 323 |
} |
| 324 |
|
| 325 |
if (lseek(fd, 0, SEEK_SET) < 0) |
| 326 |
{ |
| 327 |
report(REPORT_ERROR, "cache_create: lseek: 0 %s\n", sys_err_str(errno)); |
| 328 |
unlink(name); |
| 329 |
return -1; |
| 330 |
} |
| 331 |
|
| 332 |
return fd; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
void |
| 337 |
cache_cleanup() |
| 338 |
{ |
| 339 |
int size; |
| 340 |
char *file; |
| 341 |
|
| 342 |
if (*cache_directory == '\0') |
| 343 |
{ |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
size = cache_size(); |
| 348 |
if (size == 0 || cache_remove) |
| 349 |
{ |
| 350 |
report(REPORT_DEBUG, "cleaning `%s'\n", cache_directory); |
| 351 |
for (file = cache_first(); file && *file; file = cache_next()) |
| 352 |
{ |
| 353 |
if (unlink(file) < 0) |
| 354 |
{ |
| 355 |
report(REPORT_ERROR, "cache_cleanup: unlink %s: %s\n", |
| 356 |
file, sys_err_str(errno)); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if (rmdir(cache_directory) < 0) |
| 361 |
{ |
| 362 |
report(REPORT_ERROR, "cache_cleanup: rmdir %s: %s\n", |
| 363 |
cache_directory, sys_err_str(errno)); |
| 364 |
} |
| 365 |
} |
| 366 |
} |