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>
|
|
|
|
|
|
|
|
#include "device-private.h"
|
2017-08-09 19:03:39 +02:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-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) {
|
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
|
|
|
|
|
|
|
if (prefix && !startswith(id, prefix))
|
2015-02-03 02:05:59 +01:00
|
|
|
id = strjoina(prefix, id);
|
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
|
|
|
}
|