unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob f6fa91dad0c0b43925202e323a88e919ea9d64f4 12300 bytes (raw)
name: gnu/packages/patches/eudev-hwdb-bin-path.patch 	 # note: path name is non-authoritative(*)

  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
 
From 097290727d6e16aa2e5859e3cd6f0c5ea4c47101 Mon Sep 17 00:00:00 2001
Message-ID: <097290727d6e16aa2e5859e3cd6f0c5ea4c47101.1696060300.git.vivien@planete-kraus.eu>
From: Vivien Kraus <vivien@planete-kraus.eu>
Date: Tue, 26 Sep 2023 18:17:34 +0200
Subject: [PATCH] Improvements for Guix

---
This is the combination of:

  - https://github.com/eudev-project/eudev/pull/262
  - https://github.com/eudev-project/eudev/pull/263
  - https://github.com/eudev-project/eudev/pull/264

 src/libudev/libudev-hwdb.c |  37 +++++++++--
 src/libudev/libudev.h      |   4 +-
 src/udev/udevadm-hwdb.c    | 127 +++++++++++++++++++++++++++++++++----
 3 files changed, 149 insertions(+), 19 deletions(-)

diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c
index 62029e2b2..f7212c519 100644
--- a/src/libudev/libudev-hwdb.c
+++ b/src/libudev/libudev-hwdb.c
@@ -47,6 +47,7 @@ struct udev_hwdb {
         struct udev *udev;
         int refcount;
 
+        char *bin_paths;
         FILE *f;
         struct stat st;
         union {
@@ -256,10 +257,28 @@ static int trie_search_f(struct udev_hwdb *hwdb, const char *search) {
         return 0;
 }
 
-static const char hwdb_bin_paths[] =
-    "/etc/udev/hwdb.bin\0"
-    UDEV_LIBEXEC_DIR "/hwdb.bin\0";
-
+static char *get_hwdb_bin_paths (void) {
+        static const char default_locations[] =
+          "/etc/udev/hwdb.bin\0"
+          UDEV_LIBEXEC_DIR "/hwdb.bin\0";
+        const char *by_env = getenv("UDEV_HWDB_BIN");
+        if (by_env != NULL) {
+                char *path = malloc(strlen(by_env) + 1
+                                    + sizeof (default_locations));
+                if (path != NULL) {
+                        memcpy(path, by_env, strlen(by_env) + 1);
+                        memcpy(path + strlen(by_env) + 1,
+                               default_locations,
+                               sizeof (default_locations));
+                }
+                return path;
+        }
+        char *path = malloc(sizeof (default_locations));
+        if (path != NULL) {
+                memcpy(path, default_locations, sizeof (default_locations));
+        }
+        return path;
+}
 
 /**
  * udev_hwdb_new:
@@ -282,7 +301,12 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
         udev_list_init(udev, &hwdb->properties_list, true);
 
         /* find hwdb.bin in hwdb_bin_paths */
-        NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
+        hwdb->bin_paths = get_hwdb_bin_paths();
+        if (hwdb->bin_paths == NULL) {
+                udev_hwdb_unref(hwdb);
+                return NULL;
+        }
+        NULSTR_FOREACH(hwdb_bin_path, hwdb->bin_paths) {
                 hwdb->f = fopen(hwdb_bin_path, "re");
                 if (hwdb->f)
                         break;
@@ -363,6 +387,7 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
                 return NULL;
         if (hwdb->map)
                 munmap((void *)hwdb->map, hwdb->st.st_size);
+        free(hwdb->bin_paths);
         if (hwdb->f)
                 fclose(hwdb->f);
         udev_list_cleanup(&hwdb->properties_list);
@@ -381,7 +406,7 @@ bool udev_hwdb_validate(struct udev_hwdb *hwdb) {
                 return false;
 
         /* if hwdb.bin doesn't exist anywhere, we need to update */
-        NULSTR_FOREACH(p, hwdb_bin_paths) {
+        NULSTR_FOREACH(p, hwdb->bin_paths) {
                 if (stat(p, &st) >= 0) {
                         found = true;
                         break;
diff --git a/src/libudev/libudev.h b/src/libudev/libudev.h
index 8491d2b81..e4bc18970 100644
--- a/src/libudev/libudev.h
+++ b/src/libudev/libudev.h
@@ -185,7 +185,9 @@ struct udev_list_entry *udev_queue_get_queued_list_entry(struct udev_queue *udev
 /*
  *  udev_hwdb
  *
- *  access to the static hardware properties database
+ *  access to the static hardware properties database; the database to
+ *  use can be overriden by setting the UDEV_HWDB_BIN environment
+ *  variable to its file name
  */
 struct udev_hwdb;
 struct udev_hwdb *udev_hwdb_new(struct udev *udev);
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index d14a3434f..c2214dc91 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -35,11 +35,69 @@
  * Uses a Patricia/radix trie to index all matches for efficient lookup.
  */
 
-static const char * const conf_file_dirs[] = {
-        UDEV_HWDB_DIR,
-        UDEV_LIBEXEC_DIR "/hwdb.d",
-        NULL
-};
+static ssize_t follow_path (const char *path_variable, char **destination, size_t max) {
+        size_t n = 0;
+        if (path_variable != NULL) {
+                while (path_variable[0] == ':') {
+                      path_variable++;
+                }
+                while (path_variable[0] != '\0') {
+                        const char *end = strchr(path_variable, ':');
+                        if (end == NULL) {
+                                end = path_variable + strlen(path_variable);
+                        }
+                        if (n < max) {
+                                destination[n] = strndup(path_variable, end - path_variable);
+                                if (destination[n] == NULL) {
+                                        return -ENOMEM;
+                                }
+                        }
+                        n++;
+                        path_variable = end;
+                        while (path_variable[0] == ':') {
+                                path_variable++;
+                        }
+                }
+        }
+        if (n < max) {
+                destination[n] = NULL;
+        }
+        return n;
+}
+
+static char ** list_conf_file_dirs (void) {
+        static const char *main_hwdb_dir = UDEV_HWDB_DIR;
+        static const char *libexec_dir = UDEV_LIBEXEC_DIR "/hwdb.d";
+        const char *path_variable = getenv ("UDEV_HWDB_PATH");
+        ssize_t path_length = 0;
+        if (path_variable != NULL) {
+                path_length = follow_path(path_variable, NULL, 0);
+                if (path_length < 0) {
+                      return NULL;
+                }
+        }
+        char **all_dirs = malloc((path_length + 2 + 1) * sizeof (char *));
+        if (all_dirs == NULL) {
+                return NULL;
+        }
+        for (ssize_t i = 0; i < path_length + 2 + 1; i++) {
+                all_dirs[i] = NULL;
+        }
+        ssize_t error = follow_path(path_variable, all_dirs, path_length + 1);
+        all_dirs[path_length] = strdup(main_hwdb_dir);
+        all_dirs[path_length + 1] = strdup(libexec_dir);
+        all_dirs[path_length + 2] = NULL;
+        if (error < 0
+            || all_dirs[path_length] == NULL
+            || all_dirs[path_length + 1] == NULL) {
+                for (ssize_t i = 0; i < path_length + 2 + 1; i++) {
+                        free(all_dirs[i]);
+                }
+                free(all_dirs);
+                all_dirs = NULL;
+        }
+        return all_dirs;
+}
 
 /* in-memory trie objects */
 struct trie {
@@ -564,10 +622,15 @@ static int import_file(struct udev *udev, struct trie *trie, const char *filenam
 static void help(void) {
         printf("Usage: udevadm hwdb OPTIONS\n"
                "  -u,--update          update the hardware database\n"
+               "  -o,--output=.../hwdb.bin generate in .../hwdb.bin instead of /etc/udev/hwdb.bin\n"
                "  --usr                generate in " UDEV_LIBEXEC_DIR " instead of /etc/udev\n"
                "  -t,--test=MODALIAS   query database and print result\n"
                "  -r,--root=PATH       alternative root path in the filesystem\n"
-               "  -h,--help\n\n");
+               "  -h,--help\n"
+               "\n"
+               "The HWDB is searched in the UDEV_HWDB_PATH search path, "
+               UDEV_HWDB_DIR ", and " UDEV_LIBEXEC_DIR "/hwdb.d.\n"
+               "\n");
 }
 
 static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
@@ -578,6 +641,7 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
         static const struct option options[] = {
                 { "update", no_argument,       NULL, 'u' },
                 { "usr",    no_argument,       NULL, ARG_USR },
+                { "output", required_argument, NULL, 'o' },
                 { "test",   required_argument, NULL, 't' },
                 { "root",   required_argument, NULL, 'r' },
                 { "help",   no_argument,       NULL, 'h' },
@@ -585,19 +649,37 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
         };
         const char *test = NULL;
         const char *root = "";
-        const char *hwdb_bin_dir = "/etc/udev";
         bool update = false;
         struct trie *trie = NULL;
         int err, c;
         int rc = EXIT_SUCCESS;
 
-        while ((c = getopt_long(argc, argv, "ut:r:h", options, NULL)) >= 0)
+        _cleanup_free_ char *hwdb_bin = strdup("/etc/udev/hwdb.bin");
+        if (hwdb_bin == NULL) {
+                rc = EXIT_FAILURE;
+                goto out;
+        }
+
+        while ((c = getopt_long(argc, argv, "uo:t:r:h", options, NULL)) >= 0)
                 switch(c) {
                 case 'u':
                         update = true;
                         break;
                 case ARG_USR:
-                        hwdb_bin_dir = UDEV_LIBEXEC_DIR;
+                        free(hwdb_bin);
+                        hwdb_bin = strdup(UDEV_LIBEXEC_DIR "/hwdb.bin");
+                        if (hwdb_bin == NULL) {
+                                rc = EXIT_FAILURE;
+                                goto out;
+                        }
+                        break;
+                case 'o':
+                        free(hwdb_bin);
+                        hwdb_bin = strdup(optarg);
+                        if (hwdb_bin == NULL) {
+                                rc = EXIT_FAILURE;
+                                goto out;
+                        }
                         break;
                 case 't':
                         test = optarg;
@@ -619,9 +701,18 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
+        if (root) {
+                char *full_hwdb_bin = strjoin(root, "/", hwdb_bin, NULL);
+                if (full_hwdb_bin == NULL) {
+                        rc = EXIT_FAILURE;
+                        goto out;
+                }
+                free (hwdb_bin);
+                hwdb_bin = full_hwdb_bin;
+        }
+
         if (update) {
                 char **files, **f;
-                _cleanup_free_ char *hwdb_bin = NULL;
 
                 trie = new0(struct trie, 1);
                 if (!trie) {
@@ -644,7 +735,20 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
                 }
                 trie->nodes_count++;
 
-                err = conf_files_list_strv(&files, ".hwdb", root, conf_file_dirs);
+                char **conf_file_dirs = list_conf_file_dirs ();
+                if (conf_file_dirs == NULL) {
+                        rc = EXIT_FAILURE;
+                        goto out;
+                }
+                err = conf_files_list_strv(&files, ".hwdb", root,
+                                           (const char * const*) conf_file_dirs);
+                size_t dir_to_cleanup = 0;
+                for (dir_to_cleanup = 0;
+                     conf_file_dirs[dir_to_cleanup] != NULL;
+                     dir_to_cleanup++) {
+                        free (conf_file_dirs[dir_to_cleanup]);
+                }
+                free (conf_file_dirs);
                 if (err < 0) {
                         log_error_errno(err, "failed to enumerate hwdb files: %m");
                         rc = EXIT_FAILURE;
@@ -672,7 +776,6 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
                 log_debug("strings dedup'ed: %8zu bytes (%8zu)",
                           trie->strings->dedup_len, trie->strings->dedup_count);
 
-                hwdb_bin = strjoin(root, "/", hwdb_bin_dir, "/hwdb.bin", NULL);
                 if (!hwdb_bin) {
                         rc = EXIT_FAILURE;
                         goto out;

base-commit: 0d86dd9a2a57d5d0809de6395dc36abe515818b5
-- 
2.41.0


debug log:

solving f6fa91dad0 ...
found f6fa91dad0 in https://yhetil.org/guix-patches/ab54b51365fb18c06f235fedf451e64bad0c88bb.1696083614.git.vivien@planete-kraus.eu/ ||
	https://yhetil.org/guix-patches/18ff7cf952823194b52adcd0e6a13171d701ab94.1696070591.git.vivien@planete-kraus.eu/

applying [1/1] https://yhetil.org/guix-patches/ab54b51365fb18c06f235fedf451e64bad0c88bb.1696083614.git.vivien@planete-kraus.eu/
diff --git a/gnu/packages/patches/eudev-hwdb-bin-path.patch b/gnu/packages/patches/eudev-hwdb-bin-path.patch
new file mode 100644
index 0000000000..f6fa91dad0

1:32: trailing whitespace.
 
1:40: trailing whitespace.
 
1:67: trailing whitespace.
 
1:72: trailing whitespace.
 
1:94: trailing whitespace.
 
Checking patch gnu/packages/patches/eudev-hwdb-bin-path.patch...
Applied patch gnu/packages/patches/eudev-hwdb-bin-path.patch cleanly.
warning: squelched 10 whitespace errors
warning: 15 lines add whitespace errors.

skipping https://yhetil.org/guix-patches/18ff7cf952823194b52adcd0e6a13171d701ab94.1696070591.git.vivien@planete-kraus.eu/ for f6fa91dad0
index at:
100644 f6fa91dad0c0b43925202e323a88e919ea9d64f4	gnu/packages/patches/eudev-hwdb-bin-path.patch

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).