]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
d6170d27 | 2 | |
13aca847 YW |
3 | #include <errno.h> |
4 | ||
b753e14a | 5 | #include "alloc-util.h" |
13aca847 | 6 | #include "device-private.h" |
27458ed6 | 7 | #include "path-util.h" |
d6170d27 ZJS |
8 | #include "udevadm-util.h" |
9 | ||
13aca847 | 10 | int find_device(const char *id, const char *prefix, sd_device **ret) { |
b753e14a YW |
11 | _cleanup_free_ char *buf = NULL; |
12 | ||
d6170d27 | 13 | assert(id); |
13aca847 | 14 | assert(ret); |
d6170d27 | 15 | |
b753e14a YW |
16 | if (prefix && !path_startswith(id, prefix)) { |
17 | buf = path_join(NULL, prefix, id); | |
18 | if (!buf) | |
19 | return -ENOMEM; | |
20 | id = buf; | |
21 | } | |
d6170d27 | 22 | |
13aca847 YW |
23 | if (path_startswith(id, "/sys/")) |
24 | return sd_device_new_from_syspath(ret, id); | |
25 | ||
27458ed6 | 26 | if (path_startswith(id, "/dev/")) { |
13aca847 YW |
27 | struct stat st; |
28 | ||
29 | if (stat(id, &st) < 0) | |
30 | return -errno; | |
31 | ||
32 | return device_new_from_stat_rdev(ret, &st); | |
33 | } | |
34 | ||
35 | return -EINVAL; | |
d6170d27 | 36 | } |