From edcd336477880368915245a4b7ddd1bca5940b30 Mon Sep 17 00:00:00 2001 From: "dsteklof@us.ibm.com" Date: Tue, 2 Dec 2003 00:48:01 -0800 Subject: [PATCH] [PATCH] another patch for path problem The quick patch I sent you yesterday fixes it in one location, but there are other points in the library that calls sysfs_get_mnt_path. We need to address all the areas in the library where paths are used. The following patch is a band-aid until we can get a proper path management in the library. --- libsysfs/libsysfs.h | 1 + libsysfs/sysfs_bus.c | 20 ++++++++++++++------ libsysfs/sysfs_class.c | 13 +++++++++---- libsysfs/sysfs_device.c | 13 ++++++++++--- libsysfs/sysfs_driver.c | 4 +++- libsysfs/sysfs_utils.c | 23 ++++++++++++++++++++++- 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/libsysfs/libsysfs.h b/libsysfs/libsysfs.h index f7e989e2..6d8e58de 100644 --- a/libsysfs/libsysfs.h +++ b/libsysfs/libsysfs.h @@ -142,6 +142,7 @@ extern "C" { /* * Function Prototypes */ +extern int sysfs_trailing_slash(unsigned char *path); extern int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len); extern int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name, size_t len); diff --git a/libsysfs/sysfs_bus.c b/libsysfs/sysfs_bus.c index 3111154c..639acef4 100644 --- a/libsysfs/sysfs_bus.c +++ b/libsysfs/sysfs_bus.c @@ -112,7 +112,10 @@ static struct sysfs_directory *open_bus_dir(const unsigned char *name) return NULL; } - strcat(buspath, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(buspath) == 0) + strcat(buspath, "/"); + + strcat(buspath, SYSFS_BUS_NAME); strcat(buspath, "/"); strcat(buspath, name); busdir = sysfs_open_directory(buspath); @@ -406,10 +409,13 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, return NULL; } - strcat(path, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(path) == 0) + strcat(path, "/"); + strcat(path, SYSFS_BUS_NAME); strcat(path, "/"); strcat(path, busname); - strcat(path, SYSFS_DEVICES_DIR); + strcat(path, "/"); + strcat(path, SYSFS_DEVICES_NAME); strcat(path, "/"); strcat(path, dev_id); @@ -442,15 +448,17 @@ int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname, } memset(subsys, 0, SYSFS_PATH_MAX); - strcpy(subsys, SYSFS_BUS_DIR); + strcpy(subsys, SYSFS_BUS_NAME); buslist = sysfs_open_subsystem_list(subsys); if (buslist != NULL) { dlist_for_each_data(buslist, bus, char) { memset(subsys, 0, SYSFS_PATH_MAX); - strcpy(subsys, SYSFS_BUS_DIR); + strcat(subsys, "/"); + strcpy(subsys, SYSFS_BUS_NAME); strcat(subsys, "/"); strcat(subsys, bus); - strcat(subsys, SYSFS_DRIVERS_DIR); + strcat(subsys, "/"); + strcat(subsys, SYSFS_DRIVERS_NAME); drivers = sysfs_open_subsystem_list(subsys); if (drivers != NULL) { dlist_for_each_data(drivers, curdrv, char) { diff --git a/libsysfs/sysfs_class.c b/libsysfs/sysfs_class.c index a0273565..54f22eee 100644 --- a/libsysfs/sysfs_class.c +++ b/libsysfs/sysfs_class.c @@ -116,14 +116,16 @@ static struct sysfs_directory *open_class_dir(const unsigned char *name) return NULL; } + if (sysfs_trailing_slash(classpath) == 0) + strcat(classpath, "/"); /* * We shall now treat "block" also as a class. Hence, check here * if "name" is "block" and proceed accordingly */ if (strcmp(name, SYSFS_BLOCK_NAME) == 0) { - strcat(classpath, SYSFS_BLOCK_DIR); + strcat(classpath, SYSFS_BLOCK_NAME); } else { - strcat(classpath, SYSFS_CLASS_DIR); + strcat(classpath, SYSFS_CLASS_NAME); strcat(classpath, "/"); strcat(classpath, name); } @@ -365,10 +367,13 @@ static int get_classdev_path(const unsigned char *classname, dprintf("Error getting sysfs mount path\n"); return -1; } + if (sysfs_trailing_slash(path) == 0) + strcat(path, "/"); + if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) { - strcat(path, SYSFS_BLOCK_DIR); + strcat(path, SYSFS_BLOCK_NAME); } else { - strcat(path, SYSFS_CLASS_DIR); + strcat(path, SYSFS_CLASS_NAME); strcat(path, "/"); strcat(path, classname); } diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c index fbd046f2..323a43da 100644 --- a/libsysfs/sysfs_device.c +++ b/libsysfs/sysfs_device.c @@ -36,7 +36,9 @@ static int confirm_device_bus(struct sysfs_device *dev, if (sysfs_get_mnt_path(devpath, SYSFS_PATH_MAX) != 0) return -1; - strcat(devpath, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(devpath) == 0) + strcat(devpath, "/"); + strcat(devpath, SYSFS_BUS_NAME); strcat(devpath, "/"); strcat(devpath, busname); strcat(devpath, SYSFS_DEVICES_DIR); @@ -306,7 +308,10 @@ static struct sysfs_directory *open_root_device_dir(const unsigned char *name) return NULL; } - strcat(rootpath, SYSFS_DEVICES_DIR); + if (sysfs_trailing_slash(rootpath) == 0) + strcat(rootpath, "/"); + + strcat(rootpath, SYSFS_DEVICES_NAME); strcat(rootpath, "/"); strcat(rootpath, name); rdir = sysfs_open_directory(rootpath); @@ -435,7 +440,9 @@ static int get_device_absolute_path(const unsigned char *device, dprintf ("Sysfs not supported on this system\n"); return -1; } - strcat(bus_path, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(bus_path) == 0) + strcat(bus_path, "/"); + strcat(bus_path, SYSFS_BUS_NAME); strcat(bus_path, "/"); strcat(bus_path, bus); strcat(bus_path, SYSFS_DEVICES_DIR); diff --git a/libsysfs/sysfs_driver.c b/libsysfs/sysfs_driver.c index 0011177e..1877dbce 100644 --- a/libsysfs/sysfs_driver.c +++ b/libsysfs/sysfs_driver.c @@ -182,7 +182,9 @@ static int get_driver_path(const unsigned char *bus, const unsigned char *drv, dprintf("Error getting sysfs mount path\n"); return -1; } - strcat(path, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(path) == 0) + strcat(path, "/"); + strcat(path, SYSFS_BUS_NAME); strcat(path, "/"); strcat(path, bus); strcat(path, SYSFS_DRIVERS_DIR); diff --git a/libsysfs/sysfs_utils.c b/libsysfs/sysfs_utils.c index 4e96051c..627e618d 100644 --- a/libsysfs/sysfs_utils.c +++ b/libsysfs/sysfs_utils.c @@ -76,6 +76,23 @@ static int sysfs_get_fs_mnt_path(const unsigned char *fs_type, #endif } +/* + * sysfs_trailing_slash: checks if there's a trailing slash to path + * @path: path to check + * returns 1 if true and 0 if not + */ +int sysfs_trailing_slash(unsigned char *path) +{ + unsigned char *s = NULL; + + if (path == NULL) + return 0; + s = &path[strlen(path)-1]; + if (strncmp(s, "/", 1) == 0) + return 1; + return 0; +} + /* * sysfs_get_mnt_path: Gets the sysfs mount point. * @mnt_path: place to put "sysfs" mount point @@ -226,6 +243,8 @@ struct dlist *sysfs_open_subsystem_list(unsigned char *name) return NULL; } + if (sysfs_trailing_slash(sysfs_path) == 0) + strcat(sysfs_path, "/"); strcat(sysfs_path, name); dir = sysfs_open_directory(sysfs_path); if (dir == NULL) { @@ -301,7 +320,9 @@ struct dlist *sysfs_open_bus_devices_list(unsigned char *name) return NULL; } - strcat(sysfs_path, SYSFS_BUS_DIR); + if (sysfs_trailing_slash(sysfs_path) == 0) + strcat(sysfs_path, "/"); + strcat(sysfs_path, SYSFS_BUS_NAME); strcat(sysfs_path, "/"); strcat(sysfs_path, name); strcat(sysfs_path, SYSFS_DEVICES_DIR); -- 2.39.5