diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2021-01-07 14:45:59 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2021-01-29 20:31:56 +1000 |
commit | bb1cc1d0ff15806345335f6b2ae968ca922ec72a (patch) | |
tree | c0003d126aa87017776fb42c527abb30719a65a1 /libwacom | |
parent | c379aba3ac473245053ae62f73a9ff580eeea9f2 (diff) | |
download | libwacom-bb1cc1d0ff15806345335f6b2ae968ca922ec72a.tar.gz |
database: switch file loading to use opendir/readdir
Makes cleanup much easier and we're only going through the files one-by-one
anyway, there is never a need to have the list of entries available.
Same for the dbverify test. We leave the scandir usage in
libwacom-list-local-devices because it provides us with the convenient sort
mechanism that we'd otherwise need to do ourselves.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'libwacom')
-rw-r--r-- | libwacom/libwacom-database.c | 128 |
1 files changed, 61 insertions, 67 deletions
diff --git a/libwacom/libwacom-database.c b/libwacom/libwacom-database.c index 62b942e..4582912 100644 --- a/libwacom/libwacom-database.c +++ b/libwacom/libwacom-database.c @@ -823,13 +823,13 @@ has_suffix(const char *name, const char *suffix) } static int -scandir_tablet_filter(const struct dirent *entry) +is_tablet_file(const struct dirent *entry) { return has_suffix(entry->d_name, TABLET_SUFFIX); } static int -scandir_stylus_filter(const struct dirent *entry) +is_stylus_file(const struct dirent *entry) { return has_suffix(entry->d_name, STYLUS_SUFFIX); } @@ -837,54 +837,52 @@ scandir_stylus_filter(const struct dirent *entry) static bool load_tablet_files(WacomDeviceDatabase *db, const char *datadir) { - int n, nfiles; - struct dirent **files; - bool success = false; - - n = scandir(datadir, &files, scandir_tablet_filter, alphasort); - if (n < 0) - return false; - - nfiles = n; - while(n--) { - WacomDevice *d; - const WacomMatch **matches, **match; - - d = libwacom_parse_tablet_keyfile(db, datadir, files[n]->d_name); - - if (!d) - continue; - - matches = libwacom_get_matches(d); - if (!matches || !*matches) { - g_critical("Device '%s' has no matches defined\n", - libwacom_get_name(d)); - goto out; - } - - for (match = matches; *match; match++) { - const char *matchstr; - matchstr = libwacom_match_get_match_string(*match); - /* no duplicate matches allowed */ - if (g_hash_table_lookup(db->device_ht, matchstr) != NULL) { - g_critical("Duplicate match of '%s' on device '%s'.", - matchstr, libwacom_get_name(d)); - goto out; - } - g_hash_table_insert (db->device_ht, g_strdup (matchstr), d); - libwacom_ref(d); - } - libwacom_unref(d); - } - - success = true; + DIR *dir; + struct dirent *file; + bool success = false; -out: - while(nfiles--) - free(files[nfiles]); - free(files); + dir = opendir(datadir); + if (!dir) + return false; + + while ((file = readdir(dir))) { + WacomDevice *d; + const WacomMatch **matches, **match; + + if (!is_tablet_file(file)) + continue; + + d = libwacom_parse_tablet_keyfile(db, datadir, file->d_name); + if (!d) + continue; + + matches = libwacom_get_matches(d); + if (!matches || !*matches) { + g_critical("Device '%s' has no matches defined\n", + libwacom_get_name(d)); + goto out; + } + + for (match = matches; *match; match++) { + const char *matchstr; + matchstr = libwacom_match_get_match_string(*match); + /* no duplicate matches allowed */ + if (g_hash_table_lookup(db->device_ht, matchstr) != NULL) { + g_critical("Duplicate match of '%s' on device '%s'.", + matchstr, libwacom_get_name(d)); + goto out; + } + g_hash_table_insert (db->device_ht, g_strdup (matchstr), d); + libwacom_ref(d); + } + libwacom_unref(d); + } - return success; + success = true; + +out: + closedir(dir); + return success; } static void @@ -896,31 +894,27 @@ stylus_destroy(void *data) static bool load_stylus_files(WacomDeviceDatabase *db, const char *datadir) { - int n, nfiles; - struct dirent **files; - bool success = false; - - n = scandir(datadir, &files, scandir_stylus_filter, alphasort); - if (n < 0) - return false; + DIR *dir; + struct dirent *file; - nfiles = n; - while(n--) { - char *path; + dir = opendir(datadir); + if (!dir) + return false; - path = g_build_filename (datadir, files[n]->d_name, NULL); - libwacom_parse_stylus_keyfile(db, path); - g_free(path); - } + while ((file = readdir(dir))) { + char *path; - success = true; + if (!is_stylus_file(file)) + continue; - while(nfiles--) - free(files[nfiles]); - free(files); + path = g_build_filename (datadir, file->d_name, NULL); + libwacom_parse_stylus_keyfile(db, path); + g_free(path); + } + closedir(dir); - return success; + return true; } static bool |