util-lib: add a new skip_dev_prefix() helper
This new helper removes a leading /dev if there is one. We have code doing this all over the place, let's unify this, and correct it while we are at it, by using path_startswith() rather than startswith() to drop the prefix.
This commit is contained in:
parent
b3f5897f6e
commit
a119ec7c82
|
@ -143,3 +143,13 @@ bool is_deviceallow_pattern(const char *path);
|
||||||
int systemd_installation_has_version(const char *root, unsigned minimal_version);
|
int systemd_installation_has_version(const char *root, unsigned minimal_version);
|
||||||
|
|
||||||
bool dot_or_dot_dot(const char *path);
|
bool dot_or_dot_dot(const char *path);
|
||||||
|
|
||||||
|
static inline const char *skip_dev_prefix(const char *p) {
|
||||||
|
const char *e;
|
||||||
|
|
||||||
|
/* Drop any /dev prefix if there is any */
|
||||||
|
|
||||||
|
e = path_startswith(p, "/dev/");
|
||||||
|
|
||||||
|
return e ?: p;
|
||||||
|
}
|
||||||
|
|
|
@ -649,10 +649,7 @@ bool tty_is_vc(const char *tty) {
|
||||||
bool tty_is_console(const char *tty) {
|
bool tty_is_console(const char *tty) {
|
||||||
assert(tty);
|
assert(tty);
|
||||||
|
|
||||||
if (startswith(tty, "/dev/"))
|
return streq(skip_dev_prefix(tty), "console");
|
||||||
tty += 5;
|
|
||||||
|
|
||||||
return streq(tty, "console");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int vtnr_from_tty(const char *tty) {
|
int vtnr_from_tty(const char *tty) {
|
||||||
|
@ -660,8 +657,7 @@ int vtnr_from_tty(const char *tty) {
|
||||||
|
|
||||||
assert(tty);
|
assert(tty);
|
||||||
|
|
||||||
if (startswith(tty, "/dev/"))
|
tty = skip_dev_prefix(tty);
|
||||||
tty += 5;
|
|
||||||
|
|
||||||
if (!startswith(tty, "tty") )
|
if (!startswith(tty, "tty") )
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -775,8 +771,7 @@ bool tty_is_vc_resolve(const char *tty) {
|
||||||
|
|
||||||
assert(tty);
|
assert(tty);
|
||||||
|
|
||||||
if (startswith(tty, "/dev/"))
|
tty = skip_dev_prefix(tty);
|
||||||
tty += 5;
|
|
||||||
|
|
||||||
if (streq(tty, "console")) {
|
if (streq(tty, "console")) {
|
||||||
tty = resolve_dev_console(&active);
|
tty = resolve_dev_console(&active);
|
||||||
|
@ -918,11 +913,9 @@ int getttyname_malloc(int fd, char **ret) {
|
||||||
|
|
||||||
r = ttyname_r(fd, path, sizeof(path));
|
r = ttyname_r(fd, path, sizeof(path));
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
const char *p;
|
|
||||||
char *c;
|
char *c;
|
||||||
|
|
||||||
p = startswith(path, "/dev/");
|
c = strdup(skip_dev_prefix(path));
|
||||||
c = strdup(p ?: path);
|
|
||||||
if (!c)
|
if (!c)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
|
|
@ -3400,8 +3400,7 @@ static bool tty_may_match_dev_console(const char *tty) {
|
||||||
if (!tty)
|
if (!tty)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (startswith(tty, "/dev/"))
|
tty = skip_dev_prefix(tty);
|
||||||
tty += 5;
|
|
||||||
|
|
||||||
/* trivial identity? */
|
/* trivial identity? */
|
||||||
if (streq(tty, "console"))
|
if (streq(tty, "console"))
|
||||||
|
|
|
@ -588,6 +588,21 @@ static void test_systemd_installation_has_version(const char *path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_skip_dev_prefix(void) {
|
||||||
|
|
||||||
|
assert_se(streq(skip_dev_prefix("/"), "/"));
|
||||||
|
assert_se(streq(skip_dev_prefix("/dev"), ""));
|
||||||
|
assert_se(streq(skip_dev_prefix("/dev/"), ""));
|
||||||
|
assert_se(streq(skip_dev_prefix("/dev/foo"), "foo"));
|
||||||
|
assert_se(streq(skip_dev_prefix("/dev/foo/bar"), "foo/bar"));
|
||||||
|
assert_se(streq(skip_dev_prefix("//dev"), ""));
|
||||||
|
assert_se(streq(skip_dev_prefix("//dev//"), ""));
|
||||||
|
assert_se(streq(skip_dev_prefix("/dev///foo"), "foo"));
|
||||||
|
assert_se(streq(skip_dev_prefix("///dev///foo///bar"), "foo///bar"));
|
||||||
|
assert_se(streq(skip_dev_prefix("//foo"), "//foo"));
|
||||||
|
assert_se(streq(skip_dev_prefix("foo"), "foo"));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
log_set_max_level(LOG_DEBUG);
|
log_set_max_level(LOG_DEBUG);
|
||||||
log_parse_environment();
|
log_parse_environment();
|
||||||
|
@ -607,6 +622,7 @@ int main(int argc, char **argv) {
|
||||||
test_file_in_same_dir();
|
test_file_in_same_dir();
|
||||||
test_filename_is_valid();
|
test_filename_is_valid();
|
||||||
test_hidden_or_backup_file();
|
test_hidden_or_backup_file();
|
||||||
|
test_skip_dev_prefix();
|
||||||
|
|
||||||
test_systemd_installation_has_version(argv[1]); /* NULL is OK */
|
test_systemd_installation_has_version(argv[1]); /* NULL is OK */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue