database: extract common method to handle machine id and architecture

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/425>
This commit is contained in:
Igor V. Kovalenko 2020-11-29 11:21:29 +03:00 committed by PulseAudio Marge Bot
parent 0efae0488c
commit 0ac6b16787
12 changed files with 154 additions and 65 deletions

View file

@ -59,17 +59,24 @@ void pa_datum_free(pa_datum *d) {
pa_zero(d);
}
pa_database* pa_database_open(const char *fn, bool for_write) {
GDBM_FILE f;
int gdbm_cache_size;
char *path;
pa_assert(fn);
const char* pa_database_get_arch_suffix(void) {
/* We include the host identifier in the file name because gdbm
* files are CPU dependent, and we don't want things to go wrong
* if we are on a multiarch system. */
path = pa_sprintf_malloc("%s."CANONICAL_HOST".gdbm", fn);
return CANONICAL_HOST;
}
const char* pa_database_get_filename_suffix(void) {
return ".gdbm";
}
pa_database* pa_database_open_internal(const char *path, bool for_write) {
GDBM_FILE f;
int gdbm_cache_size;
pa_assert(path);
errno = 0;
/* We need to set the block size explicitly here, since otherwise
@ -80,8 +87,6 @@ pa_database* pa_database_open(const char *fn, bool for_write) {
if (f)
pa_log_debug("Opened GDBM database '%s'", path);
pa_xfree(path);
if (!f) {
if (errno == 0)
errno = EIO;

View file

@ -222,14 +222,21 @@ static int fill_data(simple_data *db, FILE *f) {
return pa_hashmap_size(db->map);
}
pa_database* pa_database_open(const char *fn, bool for_write) {
const char* pa_database_get_arch_suffix(void) {
/* Simple database binary file format is CPU dependent. */
return CANONICAL_HOST;
}
const char* pa_database_get_filename_suffix(void) {
return ".simple";
}
pa_database* pa_database_open_internal(const char *path, bool for_write) {
FILE *f;
char *path;
simple_data *db;
pa_assert(fn);
pa_assert(path);
path = pa_sprintf_malloc("%s."CANONICAL_HOST".simple", fn);
errno = 0;
f = pa_fopen_cloexec(path, "r");
@ -251,8 +258,6 @@ pa_database* pa_database_open(const char *fn, bool for_write) {
db = NULL;
}
pa_xfree(path);
return (pa_database*) db;
}

View file

@ -97,18 +97,23 @@ finish:
return c;
}
pa_database* pa_database_open(const char *fn, bool for_write) {
const char* pa_database_get_arch_suffix(void) {
/* TDB binary file format is not dependent on system architecture */
return NULL;
}
const char* pa_database_get_filename_suffix(void) {
return ".tdb";
}
pa_database* pa_database_open_internal(const char *path, bool for_write) {
struct tdb_context *c;
char *path;
pa_assert(fn);
pa_assert(path);
path = pa_sprintf_malloc("%s.tdb", fn);
if ((c = tdb_open_cloexec(path, 0, TDB_NOSYNC|TDB_NOLOCK, (for_write ? O_RDWR|O_CREAT : O_RDONLY), 0644)))
pa_log_debug("Opened TDB database '%s'", path);
pa_xfree(path);
if (!c) {
if (errno == 0)
errno = EIO;

72
src/pulsecore/database.c Normal file
View file

@ -0,0 +1,72 @@
/***
This file is part of PulseAudio.
Copyright 2020 Igor V. Kovalenko <igor.v.kovalenko@gmail.com>
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, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <pulse/xmalloc.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include "database.h"
#include "core-error.h"
pa_database* pa_database_open(const char *path, const char *fn, bool prependmid, bool for_write) {
const char *arch_suffix = pa_database_get_arch_suffix();
const char *filename_suffix = pa_database_get_filename_suffix();
char *machine_id = NULL, *filename_prefix, *full_path;
pa_database *f;
pa_assert(!arch_suffix || arch_suffix[0]);
pa_assert(filename_suffix && filename_suffix[0]);
if (prependmid && !(machine_id = pa_machine_id())) {
return NULL;
}
/* We include the host identifier in the file name because some database files are
* CPU dependent, and we don't want things to go wrong if we are on a multiarch system. */
filename_prefix = pa_sprintf_malloc("%s%s%s%s%s",
machine_id?:"", machine_id?"-":"",
fn,
arch_suffix?".":"", arch_suffix?:"");
full_path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s%s", path, filename_prefix, filename_suffix);
f = pa_database_open_internal(full_path, for_write);
if (f)
pa_log_info("Successfully opened '%s' database file '%s'.", fn, full_path);
else
pa_log("Failed to open '%s' database file '%s': %s", fn, full_path, pa_cstrerror(errno));
pa_xfree(full_path);
pa_xfree(filename_prefix);
/* deallocate machine_id if it was used to construct file name */
pa_xfree(machine_id);
return f;
}

View file

@ -38,8 +38,17 @@ typedef struct pa_datum {
void pa_datum_free(pa_datum *d);
/* This will append a suffix to the filename */
pa_database* pa_database_open(const char *fn, bool for_write);
/* Database implementation; returns non-empty system architecture name string if database file format depends on system architecture, or NULL otherwise. */
const char* pa_database_get_arch_suffix(void);
/* Database implementation; returns non-empty database filename extension string */
const char* pa_database_get_filename_suffix(void);
/* This will attempt opening database file matching compiled CANONICAL_HOST identifier.
* If prependmid is true, file name is augmented with machine id prefix. */
pa_database* pa_database_open(const char *path, const char *fn, bool prependmid, bool for_write);
/* Database implementation; opens specified database file using provided path. */
pa_database* pa_database_open_internal(const char *path, bool for_write);
void pa_database_close(pa_database *db);
pa_datum* pa_database_get(pa_database *db, const pa_datum *key, pa_datum* data);

View file

@ -14,6 +14,7 @@ libpulsecore_sources = [
'cpu-orc.c',
'cpu-x86.c',
'device-port.c',
'database.c',
'ffmpeg/resample2.c',
'filter/biquad.c',
'filter/crossover.c',