1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-25 23:21:33 +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:
Yu Watanabe 2022-04-16 14:33:58 +09:00 committed by GitHub
commit 747bee2662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 43 deletions

View File

@ -156,24 +156,16 @@ int readlink_malloc(const char *p, char **ret) {
int readlink_value(const char *p, char **ret) {
_cleanup_free_ char *link = NULL;
char *value;
int r;
assert(p);
assert(ret);
r = readlink_malloc(p, &link);
if (r < 0)
return r;
value = basename(link);
if (!value)
return -ENOENT;
value = strdup(value);
if (!value)
return -ENOMEM;
*ret = value;
return 0;
return path_extract_filename(link, ret);
}
int readlink_and_make_absolute(const char *p, char **r) {

View File

@ -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) {
_cleanup_free_ char *sysname = NULL;
const char *sysnum = NULL;
const char *pos;
size_t len = 0;
char *p;
int r;
if (!device->devpath)
return -EINVAL;
assert(device);
pos = strrchr(device->devpath, '/');
if (!pos)
return -EINVAL;
pos++;
/* devpath is not a root directory */
if (*pos == '\0' || pos <= device->devpath)
return -EINVAL;
sysname = strdup(pos);
if (!sysname)
return -ENOMEM;
r = path_extract_filename(device->devpath, &sysname);
if (r < 0)
return r;
/* some devices have '!' in their name, change that to '/' */
while (sysname[len] != '\0') {
if (sysname[len] == '!')
sysname[len] = '/';
for (p = strchrnul(sysname, '!'); *p != '\0'; p = strchrnul(p, '!'))
*p = '/';
len++;
}
/* trailing number (refuse number only sysname)*/
for (; p > sysname && isdigit(p[-1]); p--)
;
/* trailing number */
while (len > 0 && isdigit(sysname[--len]))
sysnum = &sysname[len];
if (len == 0)
sysnum = NULL;
device->sysnum = sysnum;
device->sysnum = p > sysname && *p != '\0' ? p : NULL;
return free_and_replace(device->sysname, sysname);
}

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <ctype.h>
#include <fcntl.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);
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);
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));