Debian Codegraph @

rplayd/cache.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: cache.c,v 1.4 1999/03/10 07:58:02 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
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 static DIR *cache_dir;
51 static char cache_path[MAXPATHLEN];
52 static char cache_directory[MAXPATHLEN];
53
54 int cache_max_size = RPLAY_CACHE_SIZE;
55 int cache_remove = 0;
56
57 /*
58 * initialize the cache
59 */
60 #ifdef __STDC__
61 void
62 cache_init(char *dir_name)
63 #else
64 void
65 cache_init(dir_name)
66 char *dir_name;
67 #endif
68 {
69 struct stat st;
70
71 strcpy(cache_directory, dir_name);
72 if (stat(cache_directory, &st) < 0)
73 {
74 report(REPORT_DEBUG, "creating cache directory `%s'\n", cache_directory);
75 if (mkdir(cache_directory, 0777) < 0)
76 {
77 report(REPORT_ERROR, "cache_init: cannot create cache directory '%s'\n", cache_directory);
78 }
79 }
80 else if (!S_ISDIR(st.st_mode))
81 {
82 report(REPORT_ERROR, "cache_init: %s not a directory\n", cache_directory);
83 done(1);
84 }
85 }
86
87 /*
88 * return the name of the first cache entry
89 */
90 char *
91 cache_first()
92 {
93 if (cache_dir)
94 {
95 rewinddir(cache_dir);
96 }
97 else
98 {
99 cache_dir = opendir(cache_directory);
100 if (cache_dir == NULL)
101 {
102 report(REPORT_ERROR, "cache_first: opendir %s: %s\n", cache_directory, sys_err_str(errno));
103 return NULL;
104 }
105 }
106
107 return cache_next();
108 }
109
110 /*
111 * return the name of the next cache entry
112 */
113 char *
114 cache_next()
115 {
116 struct dirent *dp;
117
118 do
119 {
120 dp = readdir(cache_dir);
121 if (dp == NULL)
122 {
123 closedir(cache_dir);
124 cache_dir = NULL;
125 return "";
126 }
127 }
128 while (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0);
129
130 SNPRINTF(SIZE(cache_path, sizeof(cache_path)), "%s/%s", cache_directory, dp->d_name);
131
132 return cache_path;
133 }
134
135 /*
136 * calculate the size of the cache
137 */
138 int
139 cache_size()
140 {
141 struct stat st;
142 int size = 0;
143 char *file;
144
145 for (file = cache_first(); file && *file; file = cache_next())
146 {
147 if (stat(file, &st) < 0)
148 {
149 report(REPORT_ERROR, "cache_size: stat %s: %s\n", file, sys_err_str(errno));
150 return -1;
151 }
152 size += (int) st.st_size;
153 }
154
155 return (int) size;
156 }
157
158 /*
159 * load files that are already in the cache
160 */
161 void
162 cache_read()
163 {
164 char *file;
165
166 for (file = cache_first(); file && *file; file = cache_next())
167 {
168 sound_insert(file, SOUND_READY, SOUND_FILE);
169 }
170 }
171
172 /*
173 * prepend the cache path to the sound
174 *
175 * this returns static data!
176 */
177 #ifdef __STDC__
178 char *
179 cache_name(char *sound)
180 #else
181 char *
182 cache_name(sound)
183 char *sound;
184 #endif
185 {
186 static char _cache_path[MAXPATHLEN];
187
188 SNPRINTF(SIZE(_cache_path, sizeof(_cache_path)), "%s/%s", cache_directory, sound);
189
190 return _cache_path;
191 }
192
193 /*
194 * free enough space in the cache for size bytes
195 */
196 #ifdef __STDC__
197 int
198 cache_free(int size)
199 #else
200 int
201 cache_free(size)
202 int size;
203 #endif
204 {
205 int curr_size, limit;
206 char *file;
207 SOUND *s;
208 struct stat st;
209 int n;
210
211 if (cache_max_size == 0)
212 {
213 return 0;
214 }
215
216 if (size > cache_max_size)
217 {
218 report(REPORT_DEBUG, "%d bytes cannot fit in the cache\n", size);
219 return -1;
220 }
221
222 curr_size = cache_size();
223
224 report(REPORT_DEBUG, "sound_count = %d, current cache size = %d bytes\n", sound_count, curr_size);
225
226 if (size + curr_size <= cache_max_size)
227 {
228 return 0;
229 }
230
231 /*
232 * set the initial limit
233 */
234 limit = sound_count / 2;
235
236 for (;;)
237 {
238 report(REPORT_DEBUG, "removing cache entries (limit = %d)\n", limit);
239
240 for (file = cache_first(); file && *file; file = cache_next())
241 {
242 s = sound_lookup(file, SOUND_DONT_COUNT, NULL);
243 if (s == NULL || s->status != SOUND_READY)
244 {
245 continue;
246 }
247 if (s->count < limit)
248 {
249 if (stat(file, &st) < 0)
250 {
251 report(REPORT_ERROR, "cache_free: stat %s: %s\n", file, sys_err_str(errno));
252 continue;
253 }
254 curr_size -= (int) st.st_size;
255 spool_remove(s);
256 sound_delete(s, 1);
257 report(REPORT_DEBUG, "removed %s size=%d count=%d\n", file, st.st_size, s->count);
258 }
259 }
260
261 if (size + curr_size <= cache_max_size)
262 {
263 break;
264 }
265 else if (limit == sound_count)
266 {
267 report(REPORT_ERROR, "cache_free: cannot make room for %d bytes in the cache\n", size);
268 return -1;
269 }
270 else
271 {
272 n = limit / 4;
273 limit += n ? n : 1;
274 limit = MIN(limit, sound_count);
275 }
276 }
277
278 return 0;
279 }
280
281 /*
282 * create room in the cache for the file of size bytes
283 */
284 #ifdef __STDC__
285 int
286 cache_create(char *name, int size)
287 #else
288 int
289 cache_create(name, size)
290 char *name;
291 int size;
292 #endif
293 {
294 int fd;
295
296 fd = open(name, O_RDWR | O_CREAT, 0666);
297 if (fd < 0)
298 {
299 report(REPORT_ERROR, "cache_create: open: %s\n", sys_err_str(errno));
300 return -1;
301 }
302
303 if (lseek(fd, size - 1, SEEK_SET) < 0)
304 {
305 report(REPORT_ERROR, "cache_create: lseek: %d %s\n", sys_err_str(errno));
306 return -1;
307 }
308
309 restart:
310 if (write(fd, "", 1) != 1)
311 {
312 if (errno == EINTR || errno == EAGAIN)
313 {
314 goto restart;
315 }
316 report(REPORT_ERROR, "cache_create: write: %s\n", sys_err_str(errno));
317 return -1;
318 }
319
320 if (lseek(fd, 0, SEEK_SET) < 0)
321 {
322 report(REPORT_ERROR, "cache_create: lseek: 0 %s\n", sys_err_str(errno));
323 unlink(name);
324 return -1;
325 }
326
327 return fd;
328 }
329
330 /* Optionally remove the cache directory and all its contents. */
331 void
332 cache_cleanup()
333 {
334 int size;
335 char *file;
336
337 if (*cache_directory == '\0') /* cache_init wasn't called */
338 {
339 return;
340 }
341
342 size = cache_size();
343 if (size == 0 || cache_remove)
344 {
345 report(REPORT_DEBUG, "cleaning `%s'\n", cache_directory);
346 for (file = cache_first(); file && *file; file = cache_next())
347 {
348 if (unlink(file) < 0)
349 {
350 report(REPORT_ERROR, "cache_cleanup: unlink %s: %s\n",
351 file, sys_err_str(errno));
352 }
353 }
354
355 if (rmdir(cache_directory) < 0)
356 {
357 report(REPORT_ERROR, "cache_cleanup: rmdir %s: %s\n",
358 cache_directory, sys_err_str(errno));
359 }
360 }
361 }