1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2020 Igor V. Kovalenko <igor.v.kovalenko@gmail.com>
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <errno.h>
25 #include <dirent.h>
26
27 #include <pulse/xmalloc.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/log.h>
30
31 #include "database.h"
32 #include "core-error.h"
33
pa_database_open(const char * path,const char * fn,bool prependmid,bool for_write)34 pa_database* pa_database_open(const char *path, const char *fn, bool prependmid, bool for_write) {
35
36 const char *filename_suffix = pa_database_get_filename_suffix();
37
38 char *machine_id = NULL, *filename_prefix, *full_path;
39
40 DIR *database_dir = NULL;
41 struct dirent *de;
42
43 pa_database *f;
44
45 pa_assert(filename_suffix && filename_suffix[0]);
46
47 if (prependmid && !(machine_id = pa_machine_id())) {
48 return NULL;
49 }
50
51 /* Database file name starts with ${machine_id}-${fn} */
52 if (machine_id)
53 filename_prefix = pa_sprintf_malloc("%s-%s", machine_id, fn);
54 else
55 filename_prefix = pa_xstrdup(fn);
56
57 /* Search for existing database directory entry name matching architecture suffix and filename suffix. */
58 database_dir = opendir(path);
59
60 if (database_dir) {
61 for (;;) {
62 errno = 0;
63 de = readdir(database_dir);
64 if (!de) {
65 if (errno) {
66 pa_log_warn("Unable to search for existing database file, readdir() failed: %s", pa_cstrerror(errno));
67 /* can continue as if there is no matching database file candidate */
68 }
69
70 break;
71 }
72
73 if (pa_startswith(de->d_name, filename_prefix)
74 && de->d_name[strlen(filename_prefix)] == '.'
75 && pa_endswith(de->d_name + strlen(filename_prefix) + 1, filename_suffix)) {
76
77 /* candidate filename found, replace filename_prefix with this one */
78
79 pa_log_debug("Found existing database file '%s/%s', using it", path, de->d_name);
80 pa_xfree(filename_prefix);
81 filename_prefix = pa_xstrndup(de->d_name, strlen(de->d_name) - strlen(filename_suffix));
82 break;
83 }
84 }
85
86 closedir(database_dir);
87 } else {
88 pa_log_warn("Unable to search for existing database file, failed to open directory %s: %s", path, pa_cstrerror(errno));
89 }
90
91 full_path = pa_sprintf_malloc("%s" PA_PATH_SEP "%s%s", path, filename_prefix, filename_suffix);
92
93 f = pa_database_open_internal(full_path, for_write);
94
95 if (f)
96 pa_log_info("Successfully opened '%s' database file '%s'.", fn, full_path);
97 else
98 pa_log("Failed to open '%s' database file '%s': %s", fn, full_path, pa_cstrerror(errno));
99
100 pa_xfree(full_path);
101 pa_xfree(filename_prefix);
102
103 /* deallocate machine_id if it was used to construct file name */
104 pa_xfree(machine_id);
105
106 return f;
107 }
108