diff --git a/src/basic/path-util.c b/src/basic/path-util.c index 5c1e954cbd2..a08fd7fcadc 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -1252,9 +1252,16 @@ bool hidden_or_backup_file(const char *filename) { bool is_device_path(const char *path) { /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in - * /dev. */ + * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix. + * It does not actually resolve the path. + * + * Examples: + * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes. + * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no. + */ - return PATH_STARTSWITH_SET(path, "/dev/", "/sys/"); + const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/"); + return !isempty(p); } bool valid_device_node_path(const char *path) { diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 6346b0fa83c..142abb42dfe 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -104,18 +104,18 @@ TEST(is_device_path) { assert_se(!is_device_path("")); assert_se(!is_device_path("..")); - assert_se( is_device_path("/dev")); - assert_se( is_device_path("/./dev")); - assert_se( is_device_path("/./dev/.")); + assert_se(!is_device_path("/dev")); + assert_se(!is_device_path("/./dev")); + assert_se(!is_device_path("/./dev/.")); assert_se(!is_device_path("/./dev.")); assert_se( is_device_path("/./dev/foo")); assert_se( is_device_path("/./dev/./foo")); assert_se(!is_device_path("/./dev./foo")); - assert_se( is_device_path("//dev")); - assert_se( is_device_path("///dev")); - assert_se( is_device_path("/dev/")); - assert_se( is_device_path("///dev/")); - assert_se( is_device_path("/./dev/")); + assert_se(!is_device_path("//dev")); + assert_se(!is_device_path("///dev")); + assert_se(!is_device_path("/dev/")); + assert_se(!is_device_path("///dev/")); + assert_se(!is_device_path("/./dev/")); assert_se(!is_device_path("/../dev/")); assert_se( is_device_path("/dev/sda")); assert_se( is_device_path("/dev/sda5")); @@ -125,10 +125,10 @@ TEST(is_device_path) { assert_se(!is_device_path("/../../dev/sda5")); assert_se(!is_device_path("/../../../dev/sda5b3")); assert_se(!is_device_path("/.././.././dev/sda5b3/idontexit")); - assert_se( is_device_path("/sys")); - assert_se( is_device_path("/sys/")); - assert_se( is_device_path("/./sys")); - assert_se( is_device_path("/./sys/.")); + assert_se(!is_device_path("/sys")); + assert_se(!is_device_path("/sys/")); + assert_se(!is_device_path("/./sys")); + assert_se(!is_device_path("/./sys/.")); assert_se(!is_device_path("/./sys.")); assert_se( is_device_path("/./sys/foo")); assert_se( is_device_path("/./sys/./foo")); @@ -136,8 +136,8 @@ TEST(is_device_path) { assert_se( is_device_path("/sys/what")); assert_se( is_device_path("/sys/something/..")); assert_se( is_device_path("/sys/something/../")); - assert_se( is_device_path("/sys////")); - assert_se( is_device_path("/sys////.")); + assert_se(!is_device_path("/sys////")); + assert_se(!is_device_path("/sys////.")); assert_se( is_device_path("/sys/..")); assert_se( is_device_path("/sys/../")); assert_se(!is_device_path("/usr/../dev/sda"));