mirror of
https://github.com/systemd/systemd.git
synced 2025-03-09 12:58:26 +03:00
Merge pull request #23089 from yuwata/sd-device-use-path_extract_filename
sd-device: use path_extract_filename()
This commit is contained in:
commit
747bee2662
@ -156,24 +156,16 @@ int readlink_malloc(const char *p, char **ret) {
|
|||||||
|
|
||||||
int readlink_value(const char *p, char **ret) {
|
int readlink_value(const char *p, char **ret) {
|
||||||
_cleanup_free_ char *link = NULL;
|
_cleanup_free_ char *link = NULL;
|
||||||
char *value;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
assert(p);
|
||||||
|
assert(ret);
|
||||||
|
|
||||||
r = readlink_malloc(p, &link);
|
r = readlink_malloc(p, &link);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
value = basename(link);
|
return path_extract_filename(link, ret);
|
||||||
if (!value)
|
|
||||||
return -ENOENT;
|
|
||||||
|
|
||||||
value = strdup(value);
|
|
||||||
if (!value)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
*ret = value;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int readlink_and_make_absolute(const char *p, char **r) {
|
int readlink_and_make_absolute(const char *p, char **r) {
|
||||||
|
@ -1170,42 +1170,24 @@ _public_ int sd_device_get_devname(sd_device *device, const char **devname) {
|
|||||||
|
|
||||||
static int device_set_sysname_and_sysnum(sd_device *device) {
|
static int device_set_sysname_and_sysnum(sd_device *device) {
|
||||||
_cleanup_free_ char *sysname = NULL;
|
_cleanup_free_ char *sysname = NULL;
|
||||||
const char *sysnum = NULL;
|
char *p;
|
||||||
const char *pos;
|
int r;
|
||||||
size_t len = 0;
|
|
||||||
|
|
||||||
if (!device->devpath)
|
assert(device);
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
pos = strrchr(device->devpath, '/');
|
r = path_extract_filename(device->devpath, &sysname);
|
||||||
if (!pos)
|
if (r < 0)
|
||||||
return -EINVAL;
|
return r;
|
||||||
pos++;
|
|
||||||
|
|
||||||
/* devpath is not a root directory */
|
|
||||||
if (*pos == '\0' || pos <= device->devpath)
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
sysname = strdup(pos);
|
|
||||||
if (!sysname)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
/* some devices have '!' in their name, change that to '/' */
|
/* some devices have '!' in their name, change that to '/' */
|
||||||
while (sysname[len] != '\0') {
|
for (p = strchrnul(sysname, '!'); *p != '\0'; p = strchrnul(p, '!'))
|
||||||
if (sysname[len] == '!')
|
*p = '/';
|
||||||
sysname[len] = '/';
|
|
||||||
|
|
||||||
len++;
|
/* trailing number (refuse number only sysname)*/
|
||||||
}
|
for (; p > sysname && isdigit(p[-1]); p--)
|
||||||
|
;
|
||||||
|
|
||||||
/* trailing number */
|
device->sysnum = p > sysname && *p != '\0' ? p : NULL;
|
||||||
while (len > 0 && isdigit(sysname[--len]))
|
|
||||||
sysnum = &sysname[len];
|
|
||||||
|
|
||||||
if (len == 0)
|
|
||||||
sysnum = NULL;
|
|
||||||
|
|
||||||
device->sysnum = sysnum;
|
|
||||||
return free_and_replace(device->sysname, sysname);
|
return free_and_replace(device->sysname, sysname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "device-enumerator-private.h"
|
#include "device-enumerator-private.h"
|
||||||
@ -163,7 +164,13 @@ static void test_sd_device_one(sd_device *d) {
|
|||||||
assert_se(r >= 0 || r == -ENOENT);
|
assert_se(r >= 0 || r == -ENOENT);
|
||||||
|
|
||||||
r = sd_device_get_sysnum(d, &val);
|
r = sd_device_get_sysnum(d, &val);
|
||||||
assert_se(r >= 0 || r == -ENOENT);
|
if (r >= 0) {
|
||||||
|
assert_se(val > sysname);
|
||||||
|
assert_se(val < sysname + strlen(sysname));
|
||||||
|
assert_se(in_charset(val, DIGITS));
|
||||||
|
assert_se(!isdigit(val[-1]));
|
||||||
|
} else
|
||||||
|
assert_se(r == -ENOENT);
|
||||||
|
|
||||||
r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
|
r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
|
||||||
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
|
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user