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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*-*- Mode: C; c-basic-offset: 8 -*-*/

/***
  This file is part of libkanberra.

  Copyright 2008 Lennart Poettering

  libkanberra 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.

  libkanberra 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 libkanberra. If not, see
  <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

#include <tdb.h>

#include "malloc.h"
#include "macro.h"
#include "mutex.h"
#include "kanberra.h"
#include "sound-theme-spec.h"
#include "cache.h"

#define FILENAME "event-sound-cache.tdb"
#define UPDATE_INTERVAL 10

/* This part is not portable due to pthread_once usage, should be abstracted
 * when we port this to platforms that do not have POSIX threading */

static ka_mutex *mutex = NULL;
static struct tdb_context *database = NULL;

static void allocate_mutex_once(void) {
        mutex = ka_mutex_new();
}

static int allocate_mutex(void) {
        static pthread_once_t once = PTHREAD_ONCE_INIT;

        if (pthread_once(&once, allocate_mutex_once) != 0)
                return KA_ERROR_OOM;

        if (!mutex)
                return KA_ERROR_OOM;

        return 0;
}

static int get_cache_home(char **e) {
        const char *env, *subdir;
        char *r;

        ka_return_val_if_fail(e, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if ((env = getenv("XDG_CACHE_HOME")) && *env == '/')
                subdir = "";
        else if ((env = getenv("HOME")) && *env == '/')
                subdir = "/.cache";
        else {
                *e = NULL;
                return KA_SUCCESS;
        }

        if (!(r = ka_new(char, strlen(env) + strlen(subdir) + 1)))
                return KA_ERROR_OOM;

        sprintf(r, "%s%s", env, subdir);
        *e = r;

        return KA_SUCCESS;
}

static int sensible_gethostbyname(char *n, size_t l) {

        if (gethostname(n, l) < 0)
                return -1;

        n[l-1] = 0;

        if (strlen(n) >= l-1) {
                errno = ENAMETOOLONG;
                return -1;
        }

        if (!n[0]) {
                errno = ENOENT;
                return -1;
        }

        return 0;
}

static int get_machine_id(char **id) {
        FILE *f;
        size_t l;

        ka_return_val_if_fail(id, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        /* First we try the D-Bus machine id */

        if ((f = fopen(KA_MACHINE_ID, "r"))) {
                char ln[34] = "", *r;

                r = fgets(ln, sizeof(ln)-1, f);
                fclose(f);

                if (r) {
                        ln[strcspn(ln, " \n\r\t")] = 0;

                        if (!(*id = ka_strdup(ln)))
                                return KA_ERROR_OOM;

                        return KA_SUCCESS;
                }
        }

        /* Then we try the host name */

        l = 100;

        for (;;) {
                if (!(*id = ka_new(char, l)))
                        return KA_ERROR_OOM;

                if (sensible_gethostbyname(*id, l) >= 0)
                        return KA_SUCCESS;

                ka_free(*id);

                if (errno != EINVAL && errno != ENAMETOOLONG)
                        break;

                l *= 2;
        }

        /* Then we use the POSIX host id */

        *id = ka_sprintf_malloc("%08lx", (unsigned long) gethostid());
        return KA_SUCCESS;
}

static int db_open(void) {
        int ret;
        char *c, *id, *pn;

        if ((ret = allocate_mutex()) < 0)
                return ret;

        ka_mutex_lock(mutex);

        if (database) {
                ret = KA_SUCCESS;
                goto finish;
        }

        if ((ret = get_cache_home(&c)) < 0)
                goto finish;

        if (!c) {
                ret = KA_ERROR_NOTFOUND;
                goto finish;
        }

        /* Try to create, just in case it doesn't exist yet. We don't do
         * this recursively however. */
        mkdir(c, 0755);

        if ((ret = get_machine_id(&id)) < 0) {
                ka_free(c);
                goto finish;
        }

        /* This data is machine specific, hence we include some kind of
         * stable machine id here in the name. Also, we don't want to care
         * abouth endianess/packing issues, hence we include the compiler
         * target in the name, too. */

        pn = ka_sprintf_malloc("%s/" FILENAME ".%s." CANONICAL_HOST, c, id);
        ka_free(c);
        ka_free(id);

        if (!pn) {
                ret = KA_ERROR_OOM;
                goto finish;
        }

        /* We pass TDB_NOMMAP here as long as rhbz 460851 is not fixed in
         * tdb. */
        database = tdb_open(pn, 0, TDB_NOMMAP, O_RDWR|O_CREAT|O_NOCTTY
#ifdef O_CLOEXEC
                            | O_CLOEXEC<--- Skipping configuration 'O_CLOEXEC;PACKAGE' since the value of 'O_CLOEXEC' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif
                            , 0644);
        ka_free(pn);

        if (!database) {
                ret = KA_ERROR_CORRUPT;
                goto finish;
        }

        ret = KA_SUCCESS;

finish:
        ka_mutex_unlock(mutex);

        return ret;
}

#ifdef KA_GCC_DESTRUCTOR

static void db_close(void) KA_GCC_DESTRUCTOR;

static void db_close(void) {
        /* Only here to make this valgrind clean */

        if (!getenv("VALGRIND"))
                return;

        if (mutex) {
                ka_mutex_free(mutex);
                mutex = NULL;
        }

        if (database) {
                tdb_close(database);
                database = NULL;
        }
}

#endif

static int db_lookup(const void *key, size_t klen, void **data, size_t *dlen) {
        int ret;
        TDB_DATA k, d;

        ka_return_val_if_fail(key, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(klen > 0, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(data, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(dlen, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if ((ret = db_open()) < 0)
                return ret;

        k.dptr = (void*) key;
        k.dsize = klen;

        ka_mutex_lock(mutex);

        ka_assert(database);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        d = tdb_fetch(database, k);
        if (!d.dptr) {
                ret = KA_ERROR_NOTFOUND;
                goto finish;
        }

        *data = d.dptr;
        *dlen = d.dsize;

finish:
        ka_mutex_unlock(mutex);

        return ret;
}

static int db_store(const void *key, size_t klen, const void *data, size_t dlen) {
        int ret;
        TDB_DATA k, d;

        ka_return_val_if_fail(key, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(klen > 0, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(data || dlen == 0, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if ((ret = db_open()) < 0)
                return ret;

        k.dptr = (void*) key;
        k.dsize = klen;

        d.dptr = (void*) data;
        d.dsize = dlen;

        ka_mutex_lock(mutex);

        ka_assert(database);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        if (tdb_store(database, k, d, TDB_REPLACE) < 0) {
                ret = KA_ERROR_CORRUPT;
                goto finish;
        }

        ret = KA_SUCCESS;

finish:
        ka_mutex_unlock(mutex);

        return ret;
}

static int db_remove(const void *key, size_t klen) {
        int ret;
        TDB_DATA k;

        ka_return_val_if_fail(key, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(klen > 0, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if ((ret = db_open()) < 0)
                return ret;

        k.dptr = (void*) key;
        k.dsize = klen;

        ka_mutex_lock(mutex);

        ka_assert(database);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        if (tdb_delete(database, k) < 0) {
                ret = KA_ERROR_CORRUPT;
                goto finish;
        }

        ret = KA_SUCCESS;

finish:
        ka_mutex_unlock(mutex);

        return ret;
}

static char *build_key(
                const char *theme,
                const char *name,
                const char *locale,
                const char *profile,
                size_t *klen) {

        char *key, *k;
        size_t tl, nl, ll, pl;

        tl = strlen(theme);
        nl = strlen(name);
        ll = strlen(locale);
        pl = strlen(profile);
        *klen = tl+1+nl+1+ll+1+pl+1;

        if (!(key = ka_new(char, *klen)))
                return NULL;

        k = key;
        strcpy(k, theme);
        k += tl+1;
        strcpy(k, name);
        k += nl+1;
        strcpy(k, locale);
        k += ll+1;
        strcpy(k, profile);

        return key;
}

static int get_last_change(time_t *t) {
        int ret;
        char *e, *k;
        struct stat st;
        static time_t last_check = 0, last_change = 0;
        time_t now;
        const char *g;

        ka_return_val_if_fail(t, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if ((ret = allocate_mutex()) < 0)
                return ret;

        ka_mutex_lock(mutex);

        ka_assert_se(time(&now) != (time_t) -1);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if (now < last_check + UPDATE_INTERVAL) {
                *t = last_change;
                ret = KA_SUCCESS;
                goto finish;
        }

        if ((ret = ka_get_data_home(&e)) < 0)
                goto finish;

        *t = 0;

        if (e) {
                if (!(k = ka_new(char, strlen(e) + sizeof("/sounds")))) {
                        ka_free(e);
                        ret = KA_ERROR_OOM;
                        goto finish;
                }

                sprintf(k, "%s/sounds", e);
                ka_free(e);

                if (stat(k, &st) >= 0)
                        *t = st.st_mtime;

                ka_free(k);
        }

        g = ka_get_data_dirs();

        for (;;) {
                size_t j = strcspn(g, ":");

                if (g[0] == '/' && j > 0) {

                        if (!(k = ka_new(char, j + sizeof("/sounds")))) {
                                ret = KA_ERROR_OOM;
                                goto finish;
                        }

                        memcpy(k, g, j);
                        strcpy(k+j, "/sounds");

                        if (stat(k, &st) >= 0)
                                if (st.st_mtime >= *t)
                                        *t = st.st_mtime;

                        ka_free(k);
                }

                if (g[j] == 0)
                        break;

                g += j+1;
        }

        last_change = *t;
        last_check = now;

        ret = 0;

finish:

        ka_mutex_unlock(mutex);

        return ret;
}

int ka_cache_lookup_sound(
                ka_sound_file **f,
                ka_sound_file_open_callback_t sfopen,
                char **sound_path,
                const char *theme,
                const char *name,
                const char *locale,
                const char *profile) {

        char *key = NULL;
        void *data = NULL;
        size_t klen, dlen;
        int ret;
        uint32_t timestamp;
        time_t last_change, now;
        ka_bool_t remove_entry = FALSE;

        ka_return_val_if_fail(f, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(sfopen, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(theme, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(name && *name, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(locale, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(profile, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if (sound_path)
                *sound_path = NULL;

        if (!(key = build_key(theme, name, locale, profile, &klen)))
                return KA_ERROR_OOM;

        ret = db_lookup(key, klen, &data, &dlen);

        if (ret < 0)
                goto finish;

        ka_assert(data);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if (dlen < sizeof(uint32_t) ||
            (dlen > sizeof(uint32_t) && ((char*) data)[dlen-1] != 0)) {

                /* Corrupt entry */
                ret = KA_ERROR_NOTFOUND;
                remove_entry = TRUE;
                goto finish;
        }

        memcpy(&timestamp, data, sizeof(timestamp));

        if ((ret = get_last_change(&last_change)) < 0)
                goto finish;

        ka_assert_se(time(&now) != (time_t) -1);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        /* Hmm, is the entry older than the last change to our sound theme
         * dirs? Also, check for clock skews */
        if ((time_t) timestamp < last_change || ((time_t) timestamp > now)) {
                remove_entry = TRUE;
                ret = KA_ERROR_NOTFOUND;
                goto finish;
        }

        if (dlen <= sizeof(uint32_t)) {
                /* Negative caching entry. */
                *f = NULL;
                ret = KA_SUCCESS;
                goto finish;
        }

        if (sound_path) {
                if (!(*sound_path = ka_strdup((const char*) data + sizeof(uint32_t)))) {
                        ret = KA_ERROR_OOM;
                        goto finish;
                }
        }

        if ((ret = sfopen(f, (const char*) data + sizeof(uint32_t))) < 0)
                remove_entry = TRUE;

finish:

        if (remove_entry)
                db_remove(key, klen);

        if (sound_path && ret < 0)
                ka_free(*sound_path);

        ka_free(key);
        ka_free(data);

        return ret;
}

int ka_cache_store_sound(
                const char *theme,
                const char *name,
                const char *locale,
                const char *profile,
                const char *fname) {

        char *key;
        void *data;
        size_t klen, dlen;
        int ret;
        time_t now;

        ka_return_val_if_fail(theme, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(name && *name, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(locale, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        ka_return_val_if_fail(profile, KA_ERROR_INVALID);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.

        if (!(key = build_key(theme, name, locale, profile, &klen)))
                return KA_ERROR_OOM;

        dlen = sizeof(uint32_t) + (fname ? strlen(fname) + 1 : 0);

        if (!(data = ka_malloc(dlen))) {
                ka_free(key);
                return KA_ERROR_OOM;
        }

        ka_assert_se(time(&now) != (time_t) -1);<--- %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed int'.
        *(uint32_t*) data = (uint32_t) now;

        if (fname)
                strcpy((char*) data + sizeof(uint32_t), fname);

        ret = db_store(key, klen, data, dlen);

        ka_free(key);
        ka_free(data);

        return ret;
}