diff options
Diffstat (limited to 'libsysfs/sysfs_dir.c')
-rw-r--r-- | libsysfs/sysfs_dir.c | 227 |
1 files changed, 170 insertions, 57 deletions
diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c index c159db7af8..a60410ed28 100644 --- a/libsysfs/sysfs_dir.c +++ b/libsysfs/sysfs_dir.c @@ -195,7 +195,7 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr, return -1; } if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { - dprintf("Attribute %s already has the requested value %s\n", + dprintf("Attr %s already has the requested value %s\n", sysattr->name, new_value); return 0; } @@ -274,7 +274,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) return -1; } #ifdef __KLIBC__ - pgsize = 0x1000; + pgsize = 0x1000; #else pgsize = sysconf(_SC_PAGESIZE); #endif @@ -468,6 +468,13 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path) errno = EINVAL; return NULL; } + + if (sysfs_path_is_dir(path) != 0) { + dprintf("Invalid path directory %s\n", path); + errno = EINVAL; + return NULL; + } + sdir = alloc_directory(); if (sdir == NULL) { dprintf("Error allocating directory %s\n", path); @@ -514,39 +521,6 @@ struct sysfs_link *sysfs_open_link(const unsigned char *linkpath) } /** - * sysfs_refresh_attributes: Refresh attributes list - * @attrlist: list of attributes to refresh - * Returns 0 on success, 1 on failure - */ -int sysfs_refresh_attributes(struct dlist *attrlist) -{ - struct sysfs_attribute *attr = NULL; - - if (attrlist == NULL) { - errno = EINVAL; - return 1; - } - dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { - if (attr->method & SYSFS_METHOD_SHOW) { - if ((sysfs_read_attribute(attr)) != 0) { - dprintf("Error reading attribute %s\n", - attr->path); - if ((sysfs_path_is_file(attr->path)) != 0) { - dprintf("Attr %s no longer exists\n", - attr->name); - } - } - } else { - if ((sysfs_path_is_file(attr->path)) != 0) { - dprintf("Attr %s no longer exists\n", - attr->name); - } - } - } - return 0; -} - -/** * add_attribute: open and add attribute at path to given directory * @sysdir: directory to add attribute to * @path: path to attribute @@ -633,7 +607,6 @@ int sysfs_read_dir_attributes(struct sysfs_directory *sysdir) { DIR *dir = NULL; struct dirent *dirent = NULL; - struct stat astats; unsigned char file_path[SYSFS_PATH_MAX]; int retval = 0; @@ -655,11 +628,7 @@ int sysfs_read_dir_attributes(struct sysfs_directory *sysdir) strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); strcat(file_path, "/"); strcat(file_path, dirent->d_name); - if ((lstat(file_path, &astats)) != 0) { - dprintf("stat failed\n"); - continue; - } - if (S_ISREG(astats.st_mode)) + if ((sysfs_path_is_file(file_path)) == 0) retval = add_attribute(sysdir, file_path); } closedir(dir); @@ -675,7 +644,6 @@ int sysfs_read_dir_links(struct sysfs_directory *sysdir) { DIR *dir = NULL; struct dirent *dirent = NULL; - struct stat astats; unsigned char file_path[SYSFS_PATH_MAX]; int retval = 0; @@ -697,11 +665,7 @@ int sysfs_read_dir_links(struct sysfs_directory *sysdir) strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); strcat(file_path, "/"); strcat(file_path, dirent->d_name); - if ((lstat(file_path, &astats)) != 0) { - dprintf("stat failed\n"); - continue; - } - if (S_ISLNK(astats.st_mode)) { + if ((sysfs_path_is_link(file_path)) == 0) { retval = add_link(sysdir, file_path); if (retval != 0) break; @@ -720,7 +684,6 @@ int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir) { DIR *dir = NULL; struct dirent *dirent = NULL; - struct stat astats; unsigned char file_path[SYSFS_PATH_MAX]; int retval = 0; @@ -742,11 +705,7 @@ int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir) strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); strcat(file_path, "/"); strcat(file_path, dirent->d_name); - if ((lstat(file_path, &astats)) != 0) { - dprintf("stat failed\n"); - continue; - } - if (S_ISDIR(astats.st_mode)) + if ((sysfs_path_is_dir(file_path)) == 0) retval = add_subdirectory(sysdir, file_path); } closedir(dir); @@ -802,6 +761,90 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) } /** + * sysfs_refresh_dir_attributes: Refresh attributes list + * @sysdir: directory whose list of attributes to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_attributes(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->attributes != NULL) { + dlist_destroy(sysdir->attributes); + sysdir->attributes = NULL; + } + if ((sysfs_read_dir_attributes(sysdir)) != 0) { + dprintf("Error refreshing attributes for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** + * sysfs_refresh_dir_links: Refresh links list + * @sysdir: directory whose list of links to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_links(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->links != NULL) { + dlist_destroy(sysdir->links); + sysdir->links = NULL; + } + if ((sysfs_read_dir_links(sysdir)) != 0) { + dprintf("Error refreshing links for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** + * sysfs_refresh_dir_subdirs: Refresh subdirs list + * @sysdir: directory whose list of subdirs to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_subdirs(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->subdirs != NULL) { + dlist_destroy(sysdir->subdirs); + sysdir->subdirs = NULL; + } + if ((sysfs_read_dir_subdirs(sysdir)) != 0) { + dprintf("Error refreshing subdirs for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** * sysfs_get_directory_attribute: retrieves attribute attrname from current * directory only * @dir: directory to retrieve attribute from @@ -826,19 +869,25 @@ struct sysfs_attribute *sysfs_get_directory_attribute attr = (struct sysfs_attribute *)dlist_find_custom (dir->attributes, attrname, dir_attribute_name_equal); - if (attr == NULL) { + if (attr != NULL) { + if ((sysfs_read_attribute(attr)) != 0) { + dprintf("Error reading attribute %s\n", attr->name); + return NULL; + } + } else { memset(new_path, 0, SYSFS_PATH_MAX); strcpy(new_path, dir->path); strcat(new_path, "/"); strcat(new_path, attrname); if ((sysfs_path_is_file(new_path)) == 0) { - if ((add_attribute(dir, new_path)) == 0) { + if ((add_attribute(dir, new_path)) == 0) { attr = (struct sysfs_attribute *) - dlist_find_custom(dir->attributes, + dlist_find_custom(dir->attributes, attrname, dir_attribute_name_equal); } } } + return attr; } @@ -855,9 +904,13 @@ struct sysfs_link *sysfs_get_directory_link errno = EINVAL; return NULL; } - if (dir->links == NULL) + if (dir->links == NULL) { if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL)) return NULL; + } else { + if ((sysfs_refresh_dir_links(dir)) != 0) + return NULL; + } return (struct sysfs_link *)dlist_find_custom(dir->links, linkname, dir_link_name_equal); @@ -940,3 +993,63 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, } return NULL; } + +/** + * sysfs_get_dir_attributes: returns dlist of directory attributes + * @dir: directory to retrieve attributes from + * returns dlist of attributes or NULL + */ +struct dlist *sysfs_get_dir_attributes(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->attributes == NULL) { + if (sysfs_read_dir_attributes(dir) != 0) + return NULL; + } + + return (dir->attributes); +} + +/** + * sysfs_get_dir_links: returns dlist of directory links + * @dir: directory to return links for + * returns dlist of links or NULL + */ +struct dlist *sysfs_get_dir_links(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->links == NULL) { + if (sysfs_read_dir_links(dir) != 0) + return NULL; + } + + return (dir->links); +} + +/** + * sysfs_get_dir_subdirs: returns dlist of directory subdirectories + * @dir: directory to return subdirs for + * returns dlist of subdirs or NULL + */ +struct dlist *sysfs_get_dir_subdirs(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->subdirs == NULL) { + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; + } + + return (dir->subdirs); +} |