summaryrefslogtreecommitdiff
path: root/debian/patches/0073-stream-restore-modify-to-use-pa_database.patch
blob: 96dc5903c9741ff021d551c2b2c855e44d9e77d3 (plain)
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
From 4576196fc6d6c32ef11031427864823168a4a934 Mon Sep 17 00:00:00 2001
From: Juho Hamalainen <jusa@IT-W1767.(none)>
Date: Fri, 3 Jul 2009 14:37:11 +0300
Subject: [PATCH] stream-restore: modify to use pa_database

---
 src/modules/module-stream-restore.c |  202 +++++++++++++++-------------------
 1 files changed, 89 insertions(+), 113 deletions(-)

diff --git a/src/modules/module-stream-restore.c b/src/modules/module-stream-restore.c
index c477a66..ec1f255 100644
--- a/src/modules/module-stream-restore.c
+++ b/src/modules/module-stream-restore.c
@@ -30,7 +30,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <gdbm.h>
 
 #include <pulse/xmalloc.h>
 #include <pulse/volume.h>
@@ -50,6 +49,7 @@
 #include <pulsecore/protocol-native.h>
 #include <pulsecore/pstream.h>
 #include <pulsecore/pstream-util.h>
+#include <pulsecore/database.h>
 
 #include "module-stream-restore-symdef.h"
 
@@ -90,7 +90,7 @@ struct userdata {
         *source_output_new_hook_slot,
         *connection_unlink_hook_slot;
     pa_time_event *save_time_event;
-    GDBM_FILE gdbm_file;
+    pa_database *database;
 
     pa_bool_t restore_device:1;
     pa_bool_t restore_volume:1;
@@ -101,7 +101,7 @@ struct userdata {
     pa_idxset *subscribed;
 
     pa_hook_slot *sink_proplist_changed_slot;
-    GDBM_FILE gdbm_route_file;
+    pa_database *route_database;
     char *route;
     pa_cvolume route_volume;
 };
@@ -153,28 +153,28 @@ static int _ignore_rule_hack(const char *name) {
 }
 
 static struct route_entry* read_route_entry(struct userdata *u, const char *route) {
-    datum key, data;
+    pa_datum key, data;
     struct route_entry *e;
 
     pa_assert(u);
     pa_assert(route);
 
-    key.dptr = (char*) route;
-    key.dsize = (int) strlen(route);
+    key.data = (char*) route;
+    key.size = strlen(route);
 
-    data = gdbm_fetch(u->gdbm_route_file, key);
+    memset(&data, 0, sizeof(data));
 
-    if (!data.dptr)
+    if (!pa_database_get(u->route_database, &key, &data))
         goto fail;
 
-    if (data.dsize != sizeof(struct route_entry)) {
+    if (data.size != sizeof(struct route_entry)) {
         /* This is probably just a database upgrade, hence let's not
          * consider this more than a debug message */
-        pa_log_debug("Database contains entry for route %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", route, (unsigned long) data.dsize, (unsigned long) sizeof(struct route_entry));
+        pa_log_debug("Database contains entry for route %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", route, (unsigned long) data.size, (unsigned long) sizeof(struct route_entry));
         goto fail;
     }
 
-    e = (struct route_entry*) data.dptr;
+    e = (struct route_entry*) data.data;
 
     if (e->version != ROUTE_ENTRY_VERSION) {
         pa_log_debug("Version of database entry for route %s doesn't match our version. Probably due to upgrade, ignoring.", route);
@@ -190,14 +190,14 @@ static struct route_entry* read_route_entry(struct userdata *u, const char *rout
 
 fail:
 
-    pa_xfree(data.dptr);
+    pa_xfree(data.data);
     return NULL;
 }
 
 static void route_set_role_volume(struct userdata *u, const char *name) {
     struct entry *old;
     struct entry entry;
-    datum key, data;
+    pa_datum key, data;
 
     pa_assert(u);
     pa_assert(name);
@@ -219,15 +219,15 @@ static void route_set_role_volume(struct userdata *u, const char *name) {
         pa_xfree(old);
     }
 
-    key.dptr = (void*) name;
-    key.dsize = (int) strlen(name);
+    key.data = (void*) name;
+    key.size = (int) strlen(name);
 
-    data.dptr = (void*) &entry;
-    data.dsize = sizeof(entry);
+    data.data = (void*) &entry;
+    data.size = sizeof(entry);
 
     pa_log_info("Storing volume/mute/device for stream %s.", name);
 
-    gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
+    pa_database_set(u->database, &key, &data, TRUE);
 
     apply_entry(u, name, &entry);
 }
@@ -282,8 +282,8 @@ static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, pa_usec_t t,
     u->core->mainloop->time_free(u->save_time_event);
     u->save_time_event = NULL;
 
-    gdbm_sync(u->gdbm_file);
-    gdbm_sync(u->gdbm_route_file);
+    pa_database_sync(u->database);
+    pa_database_sync(u->route_database);
 
     pa_log_info("Synced.");
 }
@@ -314,28 +314,28 @@ static char *get_name(pa_proplist *p, const char *prefix) {
 }
 
 static struct entry* read_entry(struct userdata *u, const char *name) {
-    datum key, data;
+    pa_datum key, data;
     struct entry *e;
 
     pa_assert(u);
     pa_assert(name);
 
-    key.dptr = (char*) name;
-    key.dsize = (int) strlen(name);
+    key.data = (char*) name;
+    key.size = (int) strlen(name);
 
-    data = gdbm_fetch(u->gdbm_file, key);
+    memset(&data, 0, sizeof(data));
 
-    if (!data.dptr)
+    if (!pa_database_get(u->database, &key, &data))
         goto fail;
 
-    if (data.dsize != sizeof(struct entry)) {
+    if (data.size != sizeof(struct entry)) {
         /* This is probably just a database upgrade, hence let's not
          * consider this more than a debug message */
-        pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
+        pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
         goto fail;
     }
 
-    e = (struct entry*) data.dptr;
+    e = (struct entry*) data.data;
 
     if (e->version != ENTRY_VERSION) {
         pa_log_debug("Version of database entry for stream %s doesn't match our version. Probably due to upgrade, ignoring.", name);
@@ -366,7 +366,7 @@ static struct entry* read_entry(struct userdata *u, const char *name) {
 
 fail:
 
-    pa_xfree(data.dptr);
+    pa_datum_free(&data);
     return NULL;
 }
 
@@ -395,7 +395,7 @@ static void trigger_save(struct userdata *u) {
 
     if (pa_cvolume_valid(&u->route_volume)) {
         struct route_entry entry;
-        datum key, data;
+        pa_datum key, data;
         char t[256];
 
         memset(&entry, 0, sizeof(entry));
@@ -403,13 +403,13 @@ static void trigger_save(struct userdata *u) {
         entry.volume = u->route_volume;
         pa_log_debug("Route %s volume=%s", u->route, pa_cvolume_snprint(t, sizeof(t), &u->route_volume));
 
-        key.dptr = (void*) u->route;
-        key.dsize = (int) strlen(u->route);
+        key.data = (void*) u->route;
+        key.size = (int) strlen(u->route);
 
-        data.dptr = (void*) &entry;
-        data.dsize = sizeof(entry);
+        data.data = (void*) &entry;
+        data.size = sizeof(entry);
 
-        gdbm_store(u->gdbm_route_file, key, data, GDBM_REPLACE);
+        pa_database_set(u->route_database, &key, &data, TRUE);
     }
 
 end:
@@ -449,7 +449,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
     struct userdata *u = userdata;
     struct entry entry, *old;
     char *name;
-    datum key, data;
+    pa_datum key, data;
 
     pa_assert(c);
     pa_assert(u);
@@ -525,15 +525,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
     if (!_ignore_rule_hack(name) && pa_cvolume_valid(&entry.volume))
         u->route_volume = entry.volume;
 
-    key.dptr = name;
-    key.dsize = (int) strlen(name);
+    key.data = name;
+    key.size = (int) strlen(name);
 
-    data.dptr = (void*) &entry;
-    data.dsize = sizeof(entry);
+    data.data = (void*) &entry;
+    data.size = sizeof(entry);
 
     pa_log_info("Storing volume/mute/device for stream %s.", name);
 
-    gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
+    pa_database_set(u->database, &key, &data, TRUE);
 
     pa_xfree(name);
 
@@ -658,25 +658,6 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
 
 #define EXT_VERSION 2
 
-static void clear_db(struct userdata *u) {
-    datum key;
-
-    pa_assert(u);
-
-    key = gdbm_firstkey(u->gdbm_file);
-    while (key.dptr) {
-        datum next_key;
-        next_key = gdbm_nextkey(u->gdbm_file, key);
-
-        gdbm_delete(u->gdbm_file, key);
-        pa_xfree(key.dptr);
-
-        key = next_key;
-    }
-
-    gdbm_reorganize(u->gdbm_file);
-}
-
 static int fill_db(struct userdata *u, const char *filename) {
     FILE *f;
     int n = 0;
@@ -722,7 +703,7 @@ static int fill_db(struct userdata *u, const char *filename) {
 
         *d = 0;
         if (pa_atod(v, &db) >= 0) {
-            datum key, data;
+            pa_datum key, data;
             struct entry e;
 
             memset(&e, 0, sizeof(e));
@@ -733,16 +714,16 @@ static int fill_db(struct userdata *u, const char *filename) {
             e.volume_is_absolute = TRUE;
             e.volume_is_absolute_valid = TRUE;
 
-            key.dptr = (void*) ln;
-            key.dsize = (int) strlen(ln);
+            key.data = (void*) ln;
+            key.size = (int) strlen(ln);
 
-            data.dptr = (void*) &e;
-            data.dsize = sizeof(e);
+            data.data = (void*) &e;
+            data.size = sizeof(e);
 
             if (!_ignore_rule_hack(ln) && pa_cvolume_valid(&e.volume))
                 u->route_volume = e.volume;
 
-            if (gdbm_store(u->gdbm_file, key, data, GDBM_INSERT) == 0)
+            if (pa_database_set(u->database, &key, &data, TRUE) == 0)
                 pa_log_debug("Setting %s to %fdb", ln, db);
         }
     }
@@ -835,20 +816,22 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
     }
 }
 
-#if 0
-static void dump_database(struct userdata *u) {
-    datum key;
+#ifdef DEBUG_VOLUME
+PA_GCC_UNUSED static void stream_restore_dump_database(struct userdata *u) {
+    pa_datum key;
+    pa_bool_t done;
+
+    done = !pa_database_first(u->database, &key, NULL);
 
-    key = gdbm_firstkey(u->gdbm_file);
-    while (key.dptr) {
-        datum next_key;
+    while (!done) {
+        pa_datum next_key;
         struct entry *e;
         char *name;
 
-        next_key = gdbm_nextkey(u->gdbm_file, key);
+        done = !pa_database_next(u->database, &key, &next_key, NULL);
 
-        name = pa_xstrndup(key.dptr, key.dsize);
-        pa_xfree(key.dptr);
+        name = pa_xstrndup(key.data, key.size);
+        pa_datum_free(&key);
 
         if ((e = read_entry(u, name))) {
             char t[256];
@@ -896,21 +879,23 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
         }
 
         case SUBCOMMAND_READ: {
-            datum key;
+            pa_datum key;
+            pa_bool_t done;
 
             if (!pa_tagstruct_eof(t))
                 goto fail;
 
-            key = gdbm_firstkey(u->gdbm_file);
-            while (key.dptr) {
-                datum next_key;
+            done = !pa_database_first(u->database, &key, NULL);
+
+            while (!done) {
+                pa_datum next_key;
                 struct entry *e;
                 char *name;
 
-                next_key = gdbm_nextkey(u->gdbm_file, key);
+                done = !pa_database_next(u->database, &key, &next_key, NULL);
 
-                name = pa_xstrndup(key.dptr, (size_t) key.dsize);
-                pa_xfree(key.dptr);
+                name = pa_xstrndup(key.data, (size_t) key.size);
+                pa_datum_free(&key);
 
                 if ((e = read_entry(u, name))) {
                     pa_cvolume r;
@@ -949,13 +934,13 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
                 goto fail;
 
             if (mode == PA_UPDATE_SET)
-                clear_db(u);
+                pa_database_clear(u->database);
 
             while (!pa_tagstruct_eof(t)) {
                 const char *name, *device;
                 pa_bool_t muted, volume_is_absolute;
                 struct entry entry;
-                datum key, data;
+                pa_datum key, data;
                 pa_cvolume r;
                 int k;
 
@@ -1002,13 +987,13 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
                 if (!_ignore_rule_hack(name) && pa_cvolume_valid(&entry.volume))
                     u->route_volume = entry.volume;
 
-                key.dptr = (void*) name;
-                key.dsize = (int) strlen(name);
+                key.data = (void*) name;
+                key.size = (int) strlen(name);
 
-                data.dptr = (void*) &entry;
-                data.dsize = sizeof(entry);
+                data.data = (void*) &entry;
+                data.size = sizeof(entry);
 
-                if ((k = gdbm_store(u->gdbm_file, key, data, mode == PA_UPDATE_REPLACE ? GDBM_REPLACE : GDBM_INSERT)) == 0)
+                if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0)
                     if (apply_immediately)
                         apply_entry(u, name, &entry);
             }
@@ -1022,15 +1007,15 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
 
             while (!pa_tagstruct_eof(t)) {
                 const char *name;
-                datum key;
+                pa_datum key;
 
                 if (pa_tagstruct_gets(t, &name) < 0)
                     goto fail;
 
-                key.dptr = (void*) name;
-                key.dsize = (int) strlen(name);
+                key.data = (void*) name;
+                key.size = (int) strlen(name);
 
-                gdbm_delete(u->gdbm_file, key);
+                pa_database_unset(u->database, &key);
             }
 
             trigger_save(u);
@@ -1085,7 +1070,6 @@ int pa__init(pa_module*m) {
     pa_source_output *so;
     uint32_t idx;
     pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE, restore_route_volume = TRUE;
-    int gdbm_cache_size;
     const char* ext_supported[] = { "stream-restore-1", "stream-restore-2", NULL };
 
     pa_assert(m);
@@ -1116,7 +1100,7 @@ int pa__init(pa_module*m) {
     u->restore_route_volume = restore_route_volume;
     pa_cvolume_init(&u->route_volume);
 
-    u->gdbm_file = NULL;
+    u->database = NULL;
     u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
 
     u->protocol = pa_native_protocol_get(m->core);
@@ -1142,46 +1126,38 @@ int pa__init(pa_module*m) {
      * files are CPU dependant, and we don't want things to go wrong
      * if we are on a multiarch system. */
 
-    fn = pa_sprintf_malloc("stream-volumes."CANONICAL_HOST".gdbm");
+    fn = pa_sprintf_malloc("stream-volumes."CANONICAL_HOST".bin");
     fname = pa_state_path(fn, TRUE);
     pa_xfree(fn);
 
     if (!fname)
         goto fail;
 
-    if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
-        pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
+    if (!(u->database = pa_database_open(fname, TRUE))) {
+        pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
         pa_xfree(fname);
         goto fail;
     }
 
-    /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
-    gdbm_cache_size = 10;
-    gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
-
     pa_log_info("Sucessfully opened database file '%s'.", fname);
     pa_xfree(fname);
 
     if (fill_db(u, pa_modargs_get_value(ma, "fallback_table", NULL)) < 0)
         goto fail;
 
-    fn = pa_sprintf_malloc("x-maemo-route-volumes."CANONICAL_HOST".gdbm");
+    fn = pa_sprintf_malloc("x-maemo-route-volumes."CANONICAL_HOST".bin");
     fname = pa_state_path(fn, TRUE);
     pa_xfree(fn);
 
     if (!fname)
         goto fail;
 
-    if (!(u->gdbm_route_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
-        pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
+    if (!(u->route_database = pa_database_open(fname, TRUE))) {
+        pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
         pa_xfree(fname);
         goto fail;
     }
 
-    /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
-    gdbm_cache_size = 10;
-    gdbm_setopt(u->gdbm_route_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
-
     pa_log_info("Sucessfully opened database file '%s'.", fname);
     pa_xfree(fname);
 
@@ -1229,11 +1205,11 @@ void pa__done(pa_module*m) {
     if (u->save_time_event)
         u->core->mainloop->time_free(u->save_time_event);
 
-    if (u->gdbm_file)
-        gdbm_close(u->gdbm_file);
+    if (u->database)
+        pa_database_close(u->database);
 
-    if (u->gdbm_route_file)
-        gdbm_close(u->gdbm_route_file);
+    if (u->route_database)
+        pa_database_close(u->route_database);
 
     if (u->protocol) {
         pa_native_protocol_remove_ext(u->protocol, m);
-- 
1.5.6.3