2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2014-11-26 01:39:19 +01:00
|
|
|
|
2018-08-29 10:28:05 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2018-10-22 06:46:35 +02:00
|
|
|
#include "alloc-util.h"
|
2018-08-29 10:28:05 +02:00
|
|
|
#include "device-private.h"
|
2017-08-09 19:03:39 +02:00
|
|
|
#include "path-util.h"
|
2014-11-26 01:39:19 +01:00
|
|
|
#include "udevadm-util.h"
|
|
|
|
|
2018-08-29 10:28:05 +02:00
|
|
|
int find_device(const char *id, const char *prefix, sd_device **ret) {
|
2018-10-22 06:46:35 +02:00
|
|
|
_cleanup_free_ char *buf = NULL;
|
|
|
|
|
2014-11-26 01:39:19 +01:00
|
|
|
assert(id);
|
2018-08-29 10:28:05 +02:00
|
|
|
assert(ret);
|
2014-11-26 01:39:19 +01:00
|
|
|
|
2018-10-22 06:46:35 +02:00
|
|
|
if (prefix && !path_startswith(id, prefix)) {
|
2018-11-30 10:43:57 +01:00
|
|
|
buf = path_join(prefix, id);
|
2018-10-22 06:46:35 +02:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
id = buf;
|
|
|
|
}
|
2014-11-26 01:39:19 +01:00
|
|
|
|
2018-08-29 10:28:05 +02:00
|
|
|
if (path_startswith(id, "/sys/"))
|
|
|
|
return sd_device_new_from_syspath(ret, id);
|
|
|
|
|
2017-08-09 19:03:39 +02:00
|
|
|
if (path_startswith(id, "/dev/")) {
|
2018-08-29 10:28:05 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(id, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return device_new_from_stat_rdev(ret, &st);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
2014-11-26 01:39:19 +01:00
|
|
|
}
|