From: Karel Zak Date: Thu, 28 May 2015 10:09:41 +0000 (+0200) Subject: lib/sysfs: rename devname functions, cleanup X-Git-Tag: v2.27-rc1~185 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c62abc42bb1c2aad60d0da1ce3f7498ac48cd1b;p=thirdparty%2Futil-linux.git lib/sysfs: rename devname functions, cleanup Well, I don't have mental power to use function names like sysfs_devname_to_dev_name() so this patch renames to sysfs_devname_sys_to_dev() sysfs_devname_dev_to_sys() It also cleanups usage of the functions. We have to be sure that sysfs.c code returns regular devnames. The existence of the sysfs devnames (with '!') should be completely hidden in sysfs specific code. Signed-off-by: Karel Zak --- diff --git a/include/sysfs.h b/include/sysfs.h index 6b08bbed5e..55213780cd 100644 --- a/include/sysfs.h +++ b/include/sysfs.h @@ -92,7 +92,7 @@ extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr); extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern); /** - * sysfs_devname_to_dev_name: + * sysfs_devname_sys_to_dev: * @name: devname to be converted in place * * Linux kernel linux/drivers/base/core.c: device_get_devnode() @@ -100,7 +100,7 @@ extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern); * /dev device name. This helper replaces all ocurrences of '!' in * @name by '/' to convert from /sys to /dev. */ -static inline void sysfs_devname_to_dev_name (char *name) +static inline void sysfs_devname_sys_to_dev(char *name) { char *c; @@ -110,15 +110,12 @@ static inline void sysfs_devname_to_dev_name (char *name) } /** - * sysfs_dev_name_to_devname: + * sysfs_devname_dev_to_sys: * @name: devname to be converted in place * - * Linux kernel linux/drivers/base/core.c: device_get_devnode() - * defines a replacement of '!' in the /sys device name by '/' in the - * /dev device name. This helper replaces all ocurrences of '/' in - * @name by '!' to convert from /dev to /sys. + * See sysfs_devname_sys_to_dev(). */ -static inline void sysfs_dev_name_to_devname (char *name) +static inline void sysfs_devname_dev_to_sys(char *name) { char *c; diff --git a/lib/sysfs.c b/lib/sysfs.c index 34a520758b..1ea2e779a1 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -66,8 +66,19 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent) /* * Create path to /sys/block///dev */ - int len = snprintf(buf, sizeof(buf), - _PATH_SYS_BLOCK "/%s/%s/dev", parent, name); + char *_name = strdup(name), *_parent = strdup(parent); + int len; + + if (!_name || !_parent) + return 0; + + sysfs_devname_dev_to_sys(_name); + sysfs_devname_dev_to_sys(_parent); + + len = snprintf(buf, sizeof(buf), + _PATH_SYS_BLOCK "/%s/%s/dev", _parent, _name); + free(_name); + free(_parent); if (len < 0 || (size_t) len + 1 > sizeof(buf)) return 0; path = buf; @@ -76,12 +87,16 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent) /* * Create path to /sys/block//dev */ - char sysname[PATH_MAX]; + char *_name = strdup(name); + int len; - strncpy(sysname, name, sizeof(sysname)); - sysfs_dev_name_to_devname(sysname); - int len = snprintf(buf, sizeof(buf), - _PATH_SYS_BLOCK "/%s/dev", sysname); + if (!_name) + return 0; + + sysfs_devname_dev_to_sys(_name); + len = snprintf(buf, sizeof(buf), + _PATH_SYS_BLOCK "/%s/dev", _name); + free(_name); if (len < 0 || (size_t) len + 1 > sizeof(buf)) return 0; path = buf; @@ -135,7 +150,6 @@ char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz) return NULL; /* create the final "/dev/" string */ - sysfs_devname_to_dev_name(name); memmove(buf + 5, name, sz + 1); memcpy(buf, "/dev/", 5); @@ -550,6 +564,8 @@ char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz) sz = strlen(name); memmove(buf, name, sz + 1); + sysfs_devname_sys_to_dev(buf); + return buf; } @@ -794,7 +810,7 @@ int sysfs_devno_to_wholedisk(dev_t dev, char *diskname, if (!name) goto err; - sysfs_devname_to_dev_name(name); + sysfs_devname_sys_to_dev(name); if (diskname && len) { strncpy(diskname, name, len); diskname[len - 1] = '\0'; diff --git a/libblkid/src/devno.c b/libblkid/src/devno.c index 3c082271be..f4a36e4f56 100644 --- a/libblkid/src/devno.c +++ b/libblkid/src/devno.c @@ -208,7 +208,6 @@ static char *scandev_devno_to_devpath(dev_t devno) new_list = NULL; } } - sysfs_devname_to_dev_name(devname); free_dirlist(&list); free_dirlist(&new_list); diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index d826c778f2..c98e28a0e8 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -421,7 +421,6 @@ static char *get_device_path(struct blkdev_cxt *cxt) return canonicalize_dm_name(cxt->name); snprintf(path, sizeof(path), "/dev/%s", cxt->name); - sysfs_devname_to_dev_name(path); return xstrdup(path); } @@ -1153,30 +1152,33 @@ static int set_cxt(struct blkdev_cxt *cxt, cxt->name = xstrdup(name); cxt->partition = wholedisk != NULL; + /* make sure that the name is usable in paths */ + sysfs_devname_sys_to_dev(cxt->name); + cxt->filename = get_device_path(cxt); if (!cxt->filename) { - warnx(_("%s: failed to get device path"), name); + warnx(_("%s: failed to get device path"), cxt->name); return -1; } DBG(CXT, ul_debugobj(cxt, "%s: filename=%s", cxt->name, cxt->filename)); - devno = sysfs_devname_to_devno(name, wholedisk ? wholedisk->name : NULL); + devno = sysfs_devname_to_devno(cxt->name, wholedisk ? wholedisk->name : NULL); if (!devno) { - warnx(_("%s: unknown device name"), name); + warnx(_("%s: unknown device name"), cxt->name); return -1; } if (lsblk->inverse) { if (sysfs_init(&cxt->sysfs, devno, wholedisk ? &wholedisk->sysfs : NULL)) { - warnx(_("%s: failed to initialize sysfs handler"), name); + warnx(_("%s: failed to initialize sysfs handler"), cxt->name); return -1; } if (parent) parent->sysfs.parent = &cxt->sysfs; } else { if (sysfs_init(&cxt->sysfs, devno, parent ? &parent->sysfs : NULL)) { - warnx(_("%s: failed to initialize sysfs handler"), name); + warnx(_("%s: failed to initialize sysfs handler"), cxt->name); return -1; } } @@ -1197,14 +1199,15 @@ static int set_cxt(struct blkdev_cxt *cxt, DBG(CXT, ul_debugobj(cxt, "zero size device -- ignore")); return -1; } - if (is_dm(name)) { + if (is_dm(cxt->name)) { cxt->dm_name = sysfs_strdup(&cxt->sysfs, "dm/name"); if (!cxt->dm_name) { - warnx(_("%s: failed to get dm name"), name); + warnx(_("%s: failed to get dm name"), cxt->name); return -1; } } + /* use "name" (sysfs-like name) here */ cxt->npartitions = sysfs_count_partitions(&cxt->sysfs, name); cxt->nholders = sysfs_count_dirents(&cxt->sysfs, "holders"); cxt->nslaves = sysfs_count_dirents(&cxt->sysfs, "slaves");