-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.c
291 lines (248 loc) · 6.07 KB
/
cache.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2011 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "siren.h"
#define CACHE_BUFSIZE 4096
#define CACHE_VERSION 2
static int cache_open_read(const char *);
static int cache_open_write(const char *);
static int cache_read_number(unsigned int *);
static int cache_read_string(char **);
static void cache_write_number(unsigned int);
static void cache_write_string(const char *);
static unsigned int cache_version;
static FILE *cache_fp;
static size_t cache_bufidx;
static size_t cache_buflen;
static size_t cache_bufsize;
static char *cache_buf;
void
cache_close(void)
{
fclose(cache_fp);
free(cache_buf);
}
int
cache_open(enum cache_mode mode)
{
int ret;
char *path;
path = conf_get_path(CACHE_FILE);
if (mode == CACHE_MODE_READ)
ret = cache_open_read(path);
else
ret = cache_open_write(path);
free(path);
return ret;
}
static int
cache_open_read(const char *path)
{
cache_fp = fopen(path, "r");
if (cache_fp == NULL) {
if (errno != ENOENT) {
LOG_ERR("fopen: %s", path);
msg_err("Cannot open metadata cache file");
}
return -1;
}
cache_bufidx = 0;
cache_buflen = 0;
cache_bufsize = CACHE_BUFSIZE;
cache_buf = xmalloc(cache_bufsize);
if (cache_read_number(&cache_version) == -1) {
msg_errx("Cannot read metadata cache file");
goto error;
}
LOG_INFO("reading version %u", cache_version);
if (cache_version > CACHE_VERSION) {
LOG_ERRX("unsupported metadata cache version");
msg_errx("Unsupported metadata cache version");
goto error;
}
return 0;
error:
free(cache_buf);
fclose(cache_fp);
return -1;
}
static int
cache_open_write(const char *path)
{
cache_fp = fopen(path, "w");
if (cache_fp == NULL) {
LOG_ERR("fopen: %s", path);
msg_err("Cannot open metadata cache file");
return -1;
}
LOG_INFO("writing version %u", CACHE_VERSION);
cache_buf = NULL;
cache_write_number(CACHE_VERSION);
return 0;
}
int
cache_read_entry(struct track *t)
{
int ret;
t->ip = NULL;
t->ipdata = NULL;
ret = 0;
ret |= cache_read_string(&t->path);
if (cache_version < 2)
t->albumartist = NULL;
else
ret |= cache_read_string(&t->albumartist);
ret |= cache_read_string(&t->artist);
ret |= cache_read_string(&t->album);
ret |= cache_read_string(&t->date);
if (cache_version == 0)
t->discnumber = NULL;
else
ret |= cache_read_string(&t->discnumber);
if (cache_version < 2)
t->disctotal = NULL;
else
ret |= cache_read_string(&t->disctotal);
ret |= cache_read_string(&t->tracknumber);
if (cache_version < 2)
t->tracktotal = NULL;
else
ret |= cache_read_string(&t->tracktotal);
ret |= cache_read_string(&t->title);
ret |= cache_read_number(&t->duration);
ret |= cache_read_string(&t->genre);
if (cache_version < 2)
t->comment = NULL;
else
ret |= cache_read_string(&t->comment);
return ret;
}
static int
cache_read_field(char **field)
{
size_t fieldlen;
char *sep;
for (;;) {
/* Fill the buffer. */
if (cache_buflen < cache_bufsize) {
cache_buflen += fread(cache_buf + cache_buflen, 1,
cache_bufsize - cache_buflen, cache_fp);
if (ferror(cache_fp)) {
LOG_ERR("fread");
return -1;
}
if (feof(cache_fp) && cache_buflen == 0)
return -1;
}
/* Find the field separator. */
sep = memchr(cache_buf + cache_bufidx, '\0',
cache_buflen - cache_bufidx);
if (sep != NULL)
break;
if (feof(cache_fp)) {
LOG_ERRX("no field separator at EOF");
return -1;
}
if (cache_bufidx > 0) {
memmove(cache_buf, cache_buf + cache_bufidx,
cache_buflen - cache_bufidx);
cache_buflen -= cache_bufidx;
cache_bufidx = 0;
} else {
if (SIZE_MAX - cache_bufsize < CACHE_BUFSIZE) {
LOG_ERRX("buffer size too large");
return -1;
}
cache_bufsize += CACHE_BUFSIZE;
cache_buf = xrealloc(cache_buf, cache_bufsize);
}
}
*field = cache_buf + cache_bufidx;
fieldlen = (sep - (cache_buf + cache_bufidx)) + 1;
if (cache_bufidx + fieldlen < cache_buflen)
cache_bufidx += fieldlen;
else {
cache_bufidx = 0;
cache_buflen = 0;
}
return 0;
}
static int
cache_read_number(unsigned int *num)
{
char *field;
const char *errstr;
if (cache_read_field(&field) == -1)
return -1;
*num = strtonum(field, 0, UINT_MAX, &errstr);
if (errstr != NULL) {
LOG_ERRX("%s: number is %s", field, errstr);
return -1;
}
return 0;
}
static int
cache_read_string(char **str)
{
char *field;
if (cache_read_field(&field) == -1) {
*str = NULL;
return -1;
}
*str = (field[0] == '\0') ? NULL : xstrdup(field);
return 0;
}
void
cache_update(void)
{
if (cache_fp != NULL && cache_version < CACHE_VERSION)
track_update_metadata(1);
}
void
cache_write_entry(const struct track *t)
{
cache_write_string(t->path);
cache_write_string(t->albumartist);
cache_write_string(t->artist);
cache_write_string(t->album);
cache_write_string(t->date);
cache_write_string(t->discnumber);
cache_write_string(t->disctotal);
cache_write_string(t->tracknumber);
cache_write_string(t->tracktotal);
cache_write_string(t->title);
cache_write_number(t->duration);
cache_write_string(t->genre);
cache_write_string(t->comment);
}
static void
cache_write_number(unsigned int num)
{
fprintf(cache_fp, "%u%c", num, '\0');
}
static void
cache_write_string(const char *str)
{
if (str != NULL)
fputs(str, cache_fp);
putc('\0', cache_fp);
}