Skip to content

Commit

Permalink
basic/path-util: do not say that /dev and /sys are device paths
Browse files Browse the repository at this point in the history
"/dev" or "/dev/" is the mount point, not a device path. In particular,
'systemctl status /dev' clearly does not refer to a device, so let's tweak
the code a bit to say that those are not device paths.

(Treating "/../dev" same as "/dev" would be also be reasonable, but that
requires chase(), which requires disk access, which we don't want to do from
this lightweight function.)

(cherry picked from commit 8f1998b)
(cherry picked from commit fc13a26)
  • Loading branch information
keszybz authored and bluca committed Nov 9, 2023
1 parent 7cf55c8 commit 3cbdcfd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/basic/path-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 14 additions & 14 deletions src/test/test-path-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -125,19 +125,19 @@ 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"));
assert_se(!is_device_path("/./sys./foo"));
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"));
Expand Down

0 comments on commit 3cbdcfd

Please sign in to comment.