X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libsysfs%2Fsysfs_dir.c;h=ce01669754160dfa652e29f5e7dd6462cae845ae;hp=7dbee8aba85c2f74f0b1dd66a0d80a1b46ff0802;hb=95a6f4c8acafe7031087667aa556a50a6a091c93;hpb=bcbe2d8e7d4ccd975e79d0c7defbe1d64d1b129c diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c index 7dbee8aba..ce0166975 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; } @@ -273,11 +273,8 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) sysattr->path); return -1; } -#ifdef __KLIBC__ - pgsize = 0x4000; -#else - pgsize = sysconf(_SC_PAGESIZE); -#endif + + pgsize = getpagesize(); fbuf = (unsigned char *)calloc(1, pgsize+1); if (fbuf == NULL) { dprintf("calloc failed\n"); @@ -468,6 +465,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); @@ -513,39 +517,6 @@ struct sysfs_link *sysfs_open_link(const unsigned char *linkpath) return ln; } -/** - * 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 @@ -633,7 +604,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 +625,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 +641,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 +662,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 +681,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 +702,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); @@ -801,6 +757,90 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) return(retval); } +/** + * 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 @@ -826,19 +866,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 +901,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 +990,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); +}