| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "base-dirs.h" |
| 10 |
#include "log.h" |
| 11 |
#include "wpversion.h" |
| 12 |
#include "wpbuildbasedirs.h" |
| 13 |
|
| 14 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-base-dirs") |
| 15 |
|
| 16 |
/*! |
| 17 |
* \defgroup wpbasedirs Base Directories File Lookup |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Returns /basedir/subdir/filename, with filename treated as a module |
| 21 |
* if WP_BASE_DIRS_FLAG_MODULE is set. |
| 22 |
* The basedir is assumed to be either an absolute path or NULL. |
| 23 |
* The subdir is assumed to be a path relative to basedir or NULL. |
| 24 |
*/ |
| 25 |
static gchar * |
| 26 |
make_path (guint flags, const gchar *basedir, const gchar *subdir, |
| 27 |
const gchar *filename) |
| 28 |
{ |
| 29 |
g_autofree gchar *full_basedir = NULL; |
| 30 |
g_autofree gchar *full_filename = NULL; |
| 31 |
|
| 32 |
/* merge subdir into basedir, if necessary */ |
| 33 |
if (subdir) { |
| 34 |
full_basedir = g_canonicalize_filename (subdir, basedir); |
| 35 |
basedir = full_basedir; |
| 36 |
} |
| 37 |
|
| 38 |
if (flags & WP_BASE_DIRS_FLAG_MODULE) { |
| 39 |
g_autofree gchar *basename = g_path_get_basename (filename); |
| 40 |
g_autofree gchar *dirname = g_path_get_dirname (filename); |
| 41 |
const gchar *prefix = ""; |
| 42 |
const gchar *suffix = ""; |
| 43 |
if (!g_str_has_prefix (basename, "lib")) |
| 44 |
prefix = "lib"; |
| 45 |
if (!g_str_has_suffix (basename, ".so")) |
| 46 |
suffix = ".so"; |
| 47 |
full_filename = g_strconcat (dirname, G_DIR_SEPARATOR_S, |
| 48 |
prefix, basename, suffix, NULL); |
| 49 |
filename = full_filename; |
| 50 |
} |
| 51 |
|
| 52 |
return g_canonicalize_filename (filename, basedir); |
| 53 |
} |
| 54 |
|
| 55 |
static GPtrArray * |
| 56 |
lookup_dirs (guint flags, gboolean is_absolute) |
| 57 |
{ |
| 58 |
g_autoptr(GPtrArray) dirs = g_ptr_array_new_with_free_func (g_free); |
| 59 |
const gchar *dir; |
| 60 |
const gchar *subdir = |
| 61 |
(flags & WP_BASE_DIRS_FLAG_SUBDIR_WIREPLUMBER) ? "wireplumber" : "."; |
| 62 |
|
| 63 |
/* Compile the list of lookup directories in priority order */ |
| 64 |
if (is_absolute) { |
| 65 |
g_ptr_array_add (dirs, NULL); |
| 66 |
} |
| 67 |
else if ((flags & WP_BASE_DIRS_ENV_CONFIG) && |
| 68 |
(dir = g_getenv ("WIREPLUMBER_CONFIG_DIR"))) { |
| 69 |
g_auto (GStrv) env_dirs = g_strsplit (dir, G_SEARCHPATH_SEPARATOR_S, 0); |
| 70 |
for (guint i = 0; env_dirs[i]; i++) { |
| 71 |
g_ptr_array_add (dirs, g_canonicalize_filename (env_dirs[i], NULL)); |
| 72 |
} |
| 73 |
} |
| 74 |
else if ((flags & WP_BASE_DIRS_ENV_DATA) && |
| 75 |
(dir = g_getenv ("WIREPLUMBER_DATA_DIR"))) { |
| 76 |
g_auto (GStrv) env_dirs = g_strsplit (dir, G_SEARCHPATH_SEPARATOR_S, 0); |
| 77 |
for (guint i = 0; env_dirs[i]; i++) { |
| 78 |
g_ptr_array_add (dirs, g_canonicalize_filename (env_dirs[i], NULL)); |
| 79 |
} |
| 80 |
} |
| 81 |
else if ((flags & WP_BASE_DIRS_ENV_MODULE) && |
| 82 |
(dir = g_getenv ("WIREPLUMBER_MODULE_DIR"))) { |
| 83 |
g_auto (GStrv) env_dirs = g_strsplit (dir, G_SEARCHPATH_SEPARATOR_S, 0); |
| 84 |
for (guint i = 0; env_dirs[i]; i++) { |
| 85 |
g_ptr_array_add (dirs, g_canonicalize_filename (env_dirs[i], NULL)); |
| 86 |
} |
| 87 |
} |
| 88 |
else { |
| 89 |
if (flags & WP_BASE_DIRS_XDG_CONFIG_HOME) { |
| 90 |
dir = g_get_user_config_dir (); |
| 91 |
if (G_LIKELY (g_path_is_absolute (dir))) |
| 92 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, dir)); |
| 93 |
} |
| 94 |
if (flags & WP_BASE_DIRS_XDG_DATA_HOME) { |
| 95 |
dir = g_get_user_data_dir (); |
| 96 |
if (G_LIKELY (g_path_is_absolute (dir))) |
| 97 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, dir)); |
| 98 |
} |
| 99 |
if (flags & WP_BASE_DIRS_XDG_CONFIG_DIRS) { |
| 100 |
const gchar * const *xdg_dirs = g_get_system_config_dirs (); |
| 101 |
for (guint i = 0; xdg_dirs[i]; i++) { |
| 102 |
if (G_LIKELY (g_path_is_absolute (xdg_dirs[i]))) |
| 103 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, xdg_dirs[i])); |
| 104 |
} |
| 105 |
} |
| 106 |
if (flags & WP_BASE_DIRS_BUILD_SYSCONFDIR) { |
| 107 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, BUILD_SYSCONFDIR)); |
| 108 |
} |
| 109 |
if (flags & WP_BASE_DIRS_XDG_DATA_DIRS) { |
| 110 |
const gchar * const *xdg_dirs = g_get_system_data_dirs (); |
| 111 |
for (guint i = 0; xdg_dirs[i]; i++) { |
| 112 |
if (G_LIKELY (g_path_is_absolute (xdg_dirs[i]))) |
| 113 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, xdg_dirs[i])); |
| 114 |
} |
| 115 |
} |
| 116 |
if (flags & WP_BASE_DIRS_BUILD_DATADIR) { |
| 117 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, BUILD_DATADIR)); |
| 118 |
} |
| 119 |
if (flags & WP_BASE_DIRS_BUILD_LIBDIR) { |
| 120 |
subdir = (flags & WP_BASE_DIRS_FLAG_SUBDIR_WIREPLUMBER) ? |
| 121 |
"wireplumber-" WIREPLUMBER_API_VERSION : "."; |
| 122 |
g_ptr_array_add (dirs, g_canonicalize_filename (subdir, BUILD_LIBDIR)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return g_steal_pointer (&dirs); |
| 127 |
} |
| 128 |
|
| 129 |
/*! |
| 130 |
* \brief Searches for \a filename in the hierarchy of directories specified |
| 131 |
* by the \a flags parameter |
| 132 |
* |
| 133 |
* Returns the highest priority file found in the hierarchy of directories |
| 134 |
* specified by the \a flags parameter. The \a subdir parameter is the name |
| 135 |
* of the subdirectory to search in, inside the specified directories. If |
| 136 |
* \a subdir is NULL, the base path of each directory is used. |
| 137 |
* |
| 138 |
* The \a filename parameter is the name of the file to search for. If the |
| 139 |
* file is found, its full path is returned. If the file is not found, NULL |
| 140 |
* is returned. The file is considered found if it is a regular file. |
| 141 |
* |
| 142 |
* If the \a filename is an absolute path, it is tested for existence and |
| 143 |
* returned as is, ignoring the lookup directories in \a flags as well as |
| 144 |
* the \a subdir parameter. |
| 145 |
* |
| 146 |
* \ingroup wpbasedirs |
| 147 |
* \param flags flags to specify the directories to look into and other |
| 148 |
* options specific to the kind of file being looked up |
| 149 |
* \param subdir (nullable): the name of the subdirectory to search in, |
| 150 |
* inside the specified directories |
| 151 |
* \param filename the name of the file to search for |
| 152 |
* \returns (transfer full) (nullable): A newly allocated string with the |
| 153 |
* absolute, canonicalized file path, or NULL if the file was not found. |
| 154 |
* \since 0.5.0 |
| 155 |
*/ |
| 156 |
gchar * |
| 157 |
wp_base_dirs_find_file (WpBaseDirsFlags flags, const gchar * subdir, |
| 158 |
const gchar * filename) |
| 159 |
{ |
| 160 |
gboolean is_absolute = g_path_is_absolute (filename); |
| 161 |
g_autoptr (GPtrArray) dir_paths = lookup_dirs (flags, is_absolute); |
| 162 |
gchar *ret = NULL; |
| 163 |
|
| 164 |
/* ignore the subdir if filename is absolute */ |
| 165 |
if (is_absolute) |
| 166 |
subdir = NULL; |
| 167 |
|
| 168 |
for (guint i = 0; i < dir_paths->len; i++) { |
| 169 |
g_autofree gchar *path = make_path (flags, g_ptr_array_index (dir_paths, i), |
| 170 |
subdir, filename); |
| 171 |
wp_trace ("test file: %s", path); |
| 172 |
if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) { |
| 173 |
ret = g_steal_pointer (&path); |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
wp_debug ("lookup '%s', return: %s", filename, ret); |
| 179 |
return ret; |
| 180 |
} |
| 181 |
|
| 182 |
struct conffile_iterator_item |
| 183 |
{ |
| 184 |
gchar *filename; |
| 185 |
gchar *path; |
| 186 |
}; |
| 187 |
|
| 188 |
static void |
| 189 |
conffile_iterator_item_clear (struct conffile_iterator_item *item) |
| 190 |
{ |
| 191 |
g_free (item->filename); |
| 192 |
g_free (item->path); |
| 193 |
} |
| 194 |
|
| 195 |
struct conffile_iterator_data |
| 196 |
{ |
| 197 |
GArray *items; |
| 198 |
guint idx; |
| 199 |
}; |
| 200 |
|
| 201 |
static void |
| 202 |
conffile_iterator_reset (WpIterator *it) |
| 203 |
{ |
| 204 |
struct conffile_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 205 |
it_data->idx = 0; |
| 206 |
} |
| 207 |
|
| 208 |
static gboolean |
| 209 |
conffile_iterator_next (WpIterator *it, GValue *item) |
| 210 |
{ |
| 211 |
struct conffile_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 212 |
|
| 213 |
if (it_data->idx < it_data->items->len) { |
| 214 |
const gchar *path = g_array_index (it_data->items, |
| 215 |
struct conffile_iterator_item, it_data->idx).path; |
| 216 |
it_data->idx++; |
| 217 |
g_value_init (item, G_TYPE_STRING); |
| 218 |
g_value_set_string (item, path); |
| 219 |
return TRUE; |
| 220 |
} |
| 221 |
return FALSE; |
| 222 |
} |
| 223 |
|
| 224 |
static gboolean |
| 225 |
conffile_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret, |
| 226 |
gpointer data) |
| 227 |
{ |
| 228 |
struct conffile_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 229 |
|
| 230 |
for (guint i = 0; i < it_data->items->len; i++) { |
| 231 |
g_auto (GValue) item = G_VALUE_INIT; |
| 232 |
const gchar *path = g_array_index (it_data->items, |
| 233 |
struct conffile_iterator_item, i).path; |
| 234 |
g_value_init (&item, G_TYPE_STRING); |
| 235 |
g_value_set_string (&item, path); |
| 236 |
if (!func (&item, ret, data)) |
| 237 |
return FALSE; |
| 238 |
} |
| 239 |
return TRUE; |
| 240 |
} |
| 241 |
|
| 242 |
static void |
| 243 |
conffile_iterator_finalize (WpIterator *it) |
| 244 |
{ |
| 245 |
struct conffile_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 246 |
g_clear_pointer (&it_data->items, g_array_unref); |
| 247 |
} |
| 248 |
|
| 249 |
static const WpIteratorMethods conffile_iterator_methods = { |
| 250 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 251 |
.reset = conffile_iterator_reset, |
| 252 |
.next = conffile_iterator_next, |
| 253 |
.fold = conffile_iterator_fold, |
| 254 |
.finalize = conffile_iterator_finalize, |
| 255 |
}; |
| 256 |
|
| 257 |
static gint |
| 258 |
conffile_iterator_item_compare (const struct conffile_iterator_item *a, |
| 259 |
const struct conffile_iterator_item *b) |
| 260 |
{ |
| 261 |
return g_strcmp0 (a->filename, b->filename); |
| 262 |
} |
| 263 |
|
| 264 |
/*! |
| 265 |
* \brief Creates an iterator to iterate over all files that match \a suffix |
| 266 |
* within the \a subdir of the directories specified in \a flags |
| 267 |
* |
| 268 |
* The \a subdir parameter is the name of the subdirectory to search in, |
| 269 |
* inside the directories specified by \a flags. If \a subdir is NULL, |
| 270 |
* the base path of each directory is used. If \a subdir is an absolute path, |
| 271 |
* files are only looked up in that directory and the directories in \a flags |
| 272 |
* are ignored. |
| 273 |
* |
| 274 |
* The \a suffix parameter is the filename suffix to match. If \a suffix is |
| 275 |
* NULL, all files are matched. |
| 276 |
* |
| 277 |
* The iterator will iterate over the absolute paths of all the files |
| 278 |
* files found, in the order of priority of the directories, starting from |
| 279 |
* the lowest priority directory (e.g. /usr/share/wireplumber) and ending |
| 280 |
* with the highest priority directory (e.g. $XDG_CONFIG_HOME/wireplumber). |
| 281 |
* Files within each directory are also sorted by filename. |
| 282 |
* |
| 283 |
* \ingroup wpbasedirs |
| 284 |
* \param flags flags to specify the directories to look into and other |
| 285 |
* options specific to the kind of file being looked up |
| 286 |
* \param subdir (nullable): the name of the subdirectory to search in, |
| 287 |
* inside the configuration directories |
| 288 |
* \param suffix (nullable): The filename suffix, NULL matches all entries |
| 289 |
* \returns (transfer full): a new iterator iterating over strings which are |
| 290 |
* absolute & canonicalized paths to the files found |
| 291 |
* \since 0.5.0 |
| 292 |
*/ |
| 293 |
WpIterator * |
| 294 |
wp_base_dirs_new_files_iterator (WpBaseDirsFlags flags, |
| 295 |
const gchar * subdir, const gchar * suffix) |
| 296 |
{ |
| 297 |
g_autoptr (GArray) items = |
| 298 |
g_array_new (FALSE, FALSE, sizeof (struct conffile_iterator_item)); |
| 299 |
g_autoptr (GPtrArray) dir_paths = NULL; |
| 300 |
|
| 301 |
g_array_set_clear_func (items, (GDestroyNotify) conffile_iterator_item_clear); |
| 302 |
|
| 303 |
if (subdir == NULL) |
| 304 |
subdir = "."; |
| 305 |
|
| 306 |
/* Note: this list is highest-priority first */ |
| 307 |
dir_paths = lookup_dirs (flags, g_path_is_absolute (subdir)); |
| 308 |
|
| 309 |
/* Run backwards through the list to get files in lowest-priority-first order */ |
| 310 |
for (guint i = dir_paths->len; i > 0; i--) { |
| 311 |
g_autofree gchar *dirpath = |
| 312 |
g_canonicalize_filename (subdir, g_ptr_array_index (dir_paths, i - 1)); |
| 313 |
g_autoptr (GDir) dir = g_dir_open (dirpath, 0, NULL); |
| 314 |
|
| 315 |
if (dir) { |
| 316 |
g_autoptr (GArray) dir_items = g_array_new (FALSE, FALSE, |
| 317 |
sizeof (struct conffile_iterator_item)); |
| 318 |
|
| 319 |
wp_trace ("searching dir: %s", dirpath); |
| 320 |
|
| 321 |
/* Store all filenames with their full path in the local array */ |
| 322 |
const gchar *filename; |
| 323 |
while ((filename = g_dir_read_name (dir))) { |
| 324 |
if (filename[0] == '.') |
| 325 |
continue; |
| 326 |
|
| 327 |
if (suffix && !g_str_has_suffix (filename, suffix)) |
| 328 |
continue; |
| 329 |
|
| 330 |
/* verify the file is regular and canonicalize the path */ |
| 331 |
g_autofree gchar *path = make_path (flags, dirpath, NULL, filename); |
| 332 |
if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) |
| 333 |
continue; |
| 334 |
|
| 335 |
/* remove item with the same filename from the global items array, |
| 336 |
so that lower priority files can be shadowed */ |
| 337 |
for (guint j = 0; j < items->len; j++) { |
| 338 |
struct conffile_iterator_item *item = &g_array_index (items, |
| 339 |
struct conffile_iterator_item, j); |
| 340 |
if (g_strcmp0 (item->filename, filename) == 0) { |
| 341 |
g_array_remove_index (items, j); |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/* append in the local array */ |
| 347 |
g_array_append_val (dir_items, ((struct conffile_iterator_item) { |
| 348 |
.filename = g_strdup (filename), |
| 349 |
.path = g_steal_pointer (&path), |
| 350 |
})); |
| 351 |
} |
| 352 |
|
| 353 |
/* Sort files of the current dir by filename */ |
| 354 |
g_array_sort (dir_items, (GCompareFunc) conffile_iterator_item_compare); |
| 355 |
|
| 356 |
/* Append the sorted files to the global array */ |
| 357 |
g_array_append_vals (items, dir_items->data, dir_items->len); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/* Construct iterator */ |
| 362 |
WpIterator *it = wp_iterator_new (&conffile_iterator_methods, |
| 363 |
sizeof (struct conffile_iterator_data)); |
| 364 |
struct conffile_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 365 |
it_data->items = g_steal_pointer (&items); |
| 366 |
it_data->idx = 0; |
| 367 |
return g_steal_pointer (&it); |
| 368 |
} |