2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2014-11-26 01:39:19 +01:00
|
|
|
/*
|
2018-06-12 17:15:23 +02:00
|
|
|
* Copyright © 2008-2009 Kay Sievers <kay@vrfy.org>
|
2014-11-26 01:39:19 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
struct udev_device *find_device(struct udev *udev,
|
|
|
|
const char *id,
|
|
|
|
const char *prefix) {
|
|
|
|
|
|
|
|
assert(udev);
|
|
|
|
assert(id);
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-08-09 19:03:39 +02:00
|
|
|
if (path_startswith(id, "/dev/")) {
|
2014-11-26 01:39:19 +01:00
|
|
|
struct stat statbuf;
|
|
|
|
char type;
|
|
|
|
|
|
|
|
if (stat(id, &statbuf) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (S_ISBLK(statbuf.st_mode))
|
|
|
|
type = 'b';
|
|
|
|
else if (S_ISCHR(statbuf.st_mode))
|
|
|
|
type = 'c';
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
|
2017-08-09 19:03:39 +02:00
|
|
|
} else if (path_startswith(id, "/sys/"))
|
2014-11-26 01:39:19 +01:00
|
|
|
return udev_device_new_from_syspath(udev, id);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|