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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/***
This file is part of PulseAudio.
Copyright 2009 Nokia Corporation
Contact: Maemo Multimedia <multimedia@maemo.org>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <pulse/xmalloc.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/core-error.h>
#include <pulsecore/hashmap.h>
#include "database.h"
typedef struct simple_data {
char *filename;
char *tmp_filename;
pa_hashmap *map;
pa_bool_t read_only;
} simple_data;
typedef struct entry {
pa_datum key;
pa_datum data;
} entry;
void pa_datum_free(pa_datum *d) {
pa_assert(d);
pa_xfree(d->data);
d->data = NULL;
d->size = 0;
}
static int compare_func(const void *a, const void *b) {
const pa_datum *aa, *bb;
aa = (const pa_datum*)a;
bb = (const pa_datum*)b;
if (aa->size != bb->size)
return aa->size > bb->size ? 1 : -1;
return memcmp(aa->data, bb->data, aa->size);
}
/* pa_idxset_string_hash_func modified for our use */
static unsigned hash_func(const void *p) {
const pa_datum *d;
unsigned hash = 0;
const char *c;
unsigned i;
d = (const pa_datum*)p;
c = d->data;
for (i = 0; i < d->size; i++) {
hash = 31 * hash + (unsigned) *c;
c++;
}
return hash;
}
static entry* new_entry(const pa_datum *key, const pa_datum *data) {
entry *e;
pa_assert(key);
pa_assert(data);
e = pa_xnew0(entry, 1);
e->key.data = key->size > 0 ? pa_xmemdup(key->data, key->size) : NULL;
e->key.size = key->size;
e->data.data = data->size > 0 ? pa_xmemdup(data->data, data->size) : NULL;
e->data.size = data->size;
return e;
}
static void free_entry(entry *e) {
if (e) {
if (e->key.data)
pa_xfree(e->key.data);
if (e->data.data)
pa_xfree(e->data.data);
pa_xfree(e);
}
}
static int read_uint(FILE *f, uint32_t *res) {
size_t items = 0;
uint8_t values[4];
uint32_t tmp;
int i;
items = fread(&values, sizeof(values), sizeof(uint8_t), f);
if (feof(f)) /* EOF */
return 0;
if (ferror(f))
return -1;
for (i = 0; i < 4; ++i) {
tmp = values[i];
*res += (tmp << (i*8));
}
return items;
}
static int read_data(FILE *f, void **data, ssize_t *length) {
size_t items = 0;
uint32_t data_len = 0;
pa_assert(f);
*data = NULL;
*length = 0;
if ((items = read_uint(f, &data_len)) <= 0)
return -1;
if (data_len > 0) {
*data = pa_xmalloc0(data_len);
items = fread(*data, data_len, 1, f);
if (feof(f)) /* EOF */
goto reset;
if (ferror(f))
goto reset;
*length = data_len;
} else { /* no data? */
return -1;
}
return 0;
reset:
pa_xfree(*data);
*data = NULL;
*length = 0;
return -1;
}
static int fill_data(simple_data *db, FILE *f) {
pa_datum key;
pa_datum data;
void *d = NULL;
ssize_t l = 0;
pa_bool_t append = FALSE;
enum { FIELD_KEY = 0, FIELD_DATA } field = FIELD_KEY;
pa_assert(db);
pa_assert(db->map);
errno = 0;
key.size = 0;
key.data = NULL;
while (!read_data(f, &d, &l)) {
switch (field) {
case FIELD_KEY:
key.data = d;
key.size = l;
field = FIELD_DATA;
break;
case FIELD_DATA:
data.data = d;
data.size = l;
append = TRUE;
break;
}
if (append) {
entry *e = pa_xnew0(entry, 1);
e->key.data = key.data;
e->key.size = key.size;
e->data.data = data.data;
e->data.size = data.size;
pa_hashmap_put(db->map, &e->key, e);
append = FALSE;
field = FIELD_KEY;
}
}
if (ferror(f)) {
pa_log_warn("read error. %s", pa_cstrerror(errno));
pa_database_clear((pa_database*)db);
}
if (field == FIELD_DATA && d)
pa_xfree(d);
return pa_hashmap_size(db->map);
}
pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
FILE *f;
char *path;
simple_data *db;
pa_assert(fn);
path = pa_sprintf_malloc("%s."CANONICAL_HOST".simple", fn);
errno = 0;
f = fopen(path, "r");
if (f || errno == ENOENT) { /* file not found is ok */
db = pa_xnew0(simple_data, 1);
db->map = pa_hashmap_new(hash_func, compare_func);
db->filename = pa_xstrdup(path);
db->tmp_filename = pa_sprintf_malloc(".%s.tmp", db->filename);
db->read_only = !for_write;
if (f) {
fill_data(db, f);
fclose(f);
}
} else {
if (errno == 0)
errno = EIO;
db = NULL;
}
pa_xfree(path);
return (pa_database*) db;
}
void pa_database_close(pa_database *database) {
simple_data *db = (simple_data*)database;
pa_assert(db);
pa_database_sync(database);
pa_database_clear(database);
pa_xfree(db->filename);
pa_xfree(db->tmp_filename);
pa_hashmap_free(db->map, NULL, NULL);
pa_xfree(db);
}
pa_datum* pa_database_get(pa_database *database, const pa_datum *key, pa_datum* data) {
simple_data *db = (simple_data*)database;
entry *e;
pa_assert(db);
pa_assert(key);
pa_assert(data);
e = pa_hashmap_get(db->map, key);
if (!e)
return NULL;
data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
data->size = e->data.size;
return data;
}
int pa_database_set(pa_database *database, const pa_datum *key, const pa_datum* data, pa_bool_t overwrite) {
simple_data *db = (simple_data*)database;
entry *e;
int ret = 0;
pa_assert(db);
pa_assert(key);
pa_assert(data);
if (db->read_only)
return -1;
e = new_entry(key, data);
if (pa_hashmap_put(db->map, &e->key, e) < 0) {
/* entry with same key exists in hashmap */
entry *r;
if (overwrite) {
r = pa_hashmap_remove(db->map, key);
pa_hashmap_put(db->map, &e->key, e);
} else {
/* wont't overwrite, so clean new entry */
r = e;
ret = -1;
}
free_entry(r);
}
return ret;
}
int pa_database_unset(pa_database *database, const pa_datum *key) {
simple_data *db = (simple_data*)database;
entry *e;
pa_assert(db);
pa_assert(key);
e = pa_hashmap_remove(db->map, key);
if (!e)
return -1;
free_entry(e);
return 0;
}
int pa_database_clear(pa_database *database) {
simple_data *db = (simple_data*)database;
entry *e;
pa_assert(db);
while ((e = pa_hashmap_steal_first(db->map)))
free_entry(e);
return 0;
}
signed pa_database_size(pa_database *database) {
simple_data *db = (simple_data*)database;
pa_assert(db);
return (signed) pa_hashmap_size(db->map);
}
pa_datum* pa_database_first(pa_database *database, pa_datum *key, pa_datum *data) {
simple_data *db = (simple_data*)database;
entry *e;
pa_assert(db);
pa_assert(key);
e = pa_hashmap_first(db->map);
if (!e)
return NULL;
key->data = e->key.size > 0 ? pa_xmemdup(e->key.data, e->key.size) : NULL;
key->size = e->key.size;
if (data) {
data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
data->size = e->data.size;
}
return key;
}
pa_datum* pa_database_next(pa_database *database, const pa_datum *key, pa_datum *next, pa_datum *data) {
simple_data *db = (simple_data*)database;
entry *e;
entry *search;
void *state;
pa_bool_t pick_now;
pa_assert(db);
pa_assert(next);
if (!key)
return pa_database_first(database, next, data);
search = pa_hashmap_get(db->map, key);
state = NULL;
pick_now = FALSE;
while ((e = pa_hashmap_iterate(db->map, &state, NULL))) {
if (pick_now)
break;
if (search == e)
pick_now = TRUE;
}
if (!pick_now || !e)
return NULL;
next->data = e->key.size > 0 ? pa_xmemdup(e->key.data, e->key.size) : NULL;
next->size = e->key.size;
if (data) {
data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
data->size = e->data.size;
}
return next;
}
static int write_uint(FILE *f, const uint32_t num) {
size_t items;
uint8_t values[4];
int i;
errno = 0;
for (i = 0; i < 4; i++)
values[i] = (num >> (i*8)) & 0xFF;
items = fwrite(&values, sizeof(values), sizeof(uint8_t), f);
if (ferror(f))
return -1;
return items;
}
static int write_data(FILE *f, void *data, const size_t length) {
size_t items;
uint32_t len;
len = length;
if ((items = write_uint(f, len)) <= 0)
return -1;
items = fwrite(data, length, 1, f);
if (ferror(f) || items != 1)
return -1;
return 0;
}
static int write_entry(FILE *f, const entry *e) {
pa_assert(f);
pa_assert(e);
if (write_data(f, e->key.data, e->key.size) < 0)
return -1;
if (write_data(f, e->data.data, e->data.size) < 0)
return -1;
return 0;
}
int pa_database_sync(pa_database *database) {
simple_data *db = (simple_data*)database;
FILE *f;
void *state;
entry *e;
pa_assert(db);
if (db->read_only)
return 0;
errno = 0;
f = fopen(db->tmp_filename, "w");
if (!f)
goto fail;
state = NULL;
while((e = pa_hashmap_iterate(db->map, &state, NULL))) {
if (write_entry(f, e) < 0) {
pa_log_warn("error while writing to file. %s", pa_cstrerror(errno));
goto fail;
}
}
fclose(f);
f = NULL;
if (rename(db->tmp_filename, db->filename) < 0) {
pa_log_warn("error while renaming file. %s", pa_cstrerror(errno));
goto fail;
}
return 0;
fail:
if (f)
fclose(f);
return -1;
}
|