X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libsysfs%2Fsysfs_dir.c;h=be54a1cf8689fe884dda70c59c45ab3db416dd0b;hp=a83c81f493bef500b55edbc4aee4aa18146d90d5;hb=d1fb871d99db38c7704d8e583ff5e0a00e713837;hpb=070f54223f66490e9d25719c495e074f77da8985 diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c index a83c81f49..be54a1cf8 100644 --- a/libsysfs/sysfs_dir.c +++ b/libsysfs/sysfs_dir.c @@ -1,9 +1,9 @@ /* - * syfs_dir.c + * sysfs_dir.c * * Directory utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,9 +20,84 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ -#include "libsysfs.h" +#include "sysfs/libsysfs.h" #include "sysfs.h" +/** + * sysfs_del_attribute: routine for dlist integration + */ +static void sysfs_del_attribute(void *attr) +{ + sysfs_close_attribute((struct sysfs_attribute *)attr); +} + +/** + * sysfs_del_link: routine for dlist integration + */ +static void sysfs_del_link(void *ln) +{ + sysfs_close_link((struct sysfs_link *)ln); +} + +/** + * sysfs_del_dir: routine for dlist integration + */ +static void sysfs_del_directory(void *dir) +{ + sysfs_close_directory((struct sysfs_directory *)dir); +} + +/** + * dir_attribute_name_equal: compares dir attributes by name + * @a: attribute name for comparison + * @b: sysfs_attribute to be compared. + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_attribute_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((char *)a), ((struct sysfs_attribute *)b)->name) == 0) + return 1; + + return 0; +} + +/** + * dir_link_name_equal: compares dir links by name + * @a: link name for comparison + * @b: sysfs_link to be compared. + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_link_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((char *)a), ((struct sysfs_link *)b)->name) == 0) + return 1; + + return 0; +} + +/** + * dir_subdir_name_equal: compares subdirs by name + * @a: name of subdirectory to compare + * @b: sysfs_directory subdirectory to be compared + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_subdir_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((char *)a), ((struct sysfs_directory *)b)->name) == 0) + return 1; + + return 0; +} + /** * sysfs_close_attribute: closes and cleans up attribute * @sysattr: attribute to close. @@ -62,13 +137,21 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path) } sysattr = alloc_attribute(); if (sysattr == NULL) { - dprintf(stderr, "Error allocating attribute at %s\n", path); + dprintf("Error allocating attribute at %s\n", path); return NULL; } - strncpy(sysattr->path, path, sizeof(sysattr->path)); + if (sysfs_get_name_from_path(path, sysattr->name, + SYSFS_NAME_LEN) != 0) { + dprintf("Error retrieving attrib name from path: %s\n", path); + sysfs_close_attribute(sysattr); + return NULL; + } + safestrcpy(sysattr->path, path); if ((stat(sysattr->path, &fileinfo)) != 0) { - perror("stat"); + dprintf("Stat failed: No such attribute?\n"); sysattr->method = 0; + free(sysattr); + sysattr = NULL; } else { if (fileinfo.st_mode & S_IRUSR) sysattr->method |= SYSFS_METHOD_SHOW; @@ -79,6 +162,94 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path) return sysattr; } +/** + * sysfs_write_attribute: write value to the attribute + * @sysattr: attribute to write + * @new_value: value to write + * @len: length of "new_value" + * returns 0 with success and -1 with error. + */ +int sysfs_write_attribute(struct sysfs_attribute *sysattr, + const char *new_value, size_t len) +{ + int fd; + int length; + + if (sysattr == NULL || new_value == NULL || len == 0) { + errno = EINVAL; + return -1; + } + + if (!(sysattr->method & SYSFS_METHOD_STORE)) { + dprintf ("Store method not supported for attribute %s\n", + sysattr->path); + errno = EACCES; + return -1; + } + if (sysattr->method & SYSFS_METHOD_SHOW) { + /* + * read attribute again to see if we can get an updated value + */ + if ((sysfs_read_attribute(sysattr)) != 0) { + dprintf("Error reading attribute\n"); + return -1; + } + if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { + dprintf("Attr %s already has the requested value %s\n", + sysattr->name, new_value); + return 0; + } + } + /* + * open O_WRONLY since some attributes have no "read" but only + * "write" permission + */ + if ((fd = open(sysattr->path, O_WRONLY)) < 0) { + dprintf("Error reading attribute %s\n", sysattr->path); + return -1; + } + + length = write(fd, new_value, len); + if (length < 0) { + dprintf("Error writing to the attribute %s - invalid value?\n", + sysattr->name); + close(fd); + return -1; + } else if ((unsigned int)length != len) { + dprintf("Could not write %d bytes to attribute %s\n", + len, sysattr->name); + /* + * since we could not write user supplied number of bytes, + * restore the old value if one available + */ + if (sysattr->method & SYSFS_METHOD_SHOW) { + length = write(fd, sysattr->value, sysattr->len); + close(fd); + return -1; + } + } + + /* + * Validate length that has been copied. Alloc appropriate area + * in sysfs_attribute. Verify first if the attribute supports reading + * (show method). If it does not, do not bother + */ + if (sysattr->method & SYSFS_METHOD_SHOW) { + if (length != sysattr->len) { + sysattr->value = (char *)realloc + (sysattr->value, length); + sysattr->len = length; + safestrncpy(sysattr->value, new_value, length); + } else { + /*"length" of the new value is same as old one */ + safestrncpy(sysattr->value, new_value, length); + } + } + + close(fd); + return 0; +} + /** * sysfs_read_attribute: reads value from attribute * @sysattr: attribute to read @@ -88,8 +259,8 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) { char *fbuf = NULL; char *vbuf = NULL; - size_t length = 0; - int pgsize = 0; + ssize_t length = 0; + long pgsize = 0; int fd; if (sysattr == NULL) { @@ -97,34 +268,43 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) return -1; } if (!(sysattr->method & SYSFS_METHOD_SHOW)) { - dprintf (stderr, "Show method not supported for attribute %s\n", + dprintf("Show method not supported for attribute %s\n", sysattr->path); + errno = EACCES; return -1; } pgsize = getpagesize(); fbuf = (char *)calloc(1, pgsize+1); if (fbuf == NULL) { - perror("calloc"); + dprintf("calloc failed\n"); return -1; } if ((fd = open(sysattr->path, O_RDONLY)) < 0) { - dprintf (stderr, "Error reading attribute %s\n", sysattr->path); + dprintf("Error reading attribute %s\n", sysattr->path); free(fbuf); return -1; } length = read(fd, fbuf, pgsize); if (length < 0) { - dprintf (stderr, "Error reading from attribute %s\n", - sysattr->path); + dprintf("Error reading from attribute %s\n", sysattr->path); close(fd); free(fbuf); return -1; } + if (sysattr->len > 0) { + if ((sysattr->len == length) && + (!(strncmp(sysattr->value, fbuf, length)))) { + close(fd); + free(fbuf); + return 0; + } + free(sysattr->value); + } sysattr->len = length; close(fd); vbuf = (char *)realloc(fbuf, length+1); if (vbuf == NULL) { - perror("realloc"); + dprintf("realloc failed\n"); free(fbuf); return -1; } @@ -142,33 +322,33 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) * @vsize: size of value buffer * returns 0 with success and -1 with error. */ -int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize) +int sysfs_read_attribute_value(const char *attrpath, + char *value, size_t vsize) { struct sysfs_attribute *attr = NULL; size_t length = 0; - if (attrpath == NULL || value == NULL) { + if (attrpath == NULL || value == NULL || vsize == 0) { errno = EINVAL; return -1; } attr = sysfs_open_attribute(attrpath); if (attr == NULL) { - dprintf(stderr, "Invalid attribute path %s\n", attrpath); + dprintf("Invalid attribute path %s\n", attrpath); errno = EINVAL; return -1; } if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) { - dprintf(stderr, "Error reading from attribute %s\n", attrpath); + dprintf("Error reading from attribute %s\n", attrpath); sysfs_close_attribute(attr); return -1; } length = strlen(attr->value); if (length > vsize) - dprintf(stderr, - "Value length %d is larger than supplied buffer %d\n", + dprintf("Value length %d is larger than supplied buffer %d\n", length, vsize); - strncpy(value, attr->value, vsize); + safestrncpy(value, attr->value, vsize); sysfs_close_attribute(attr); return 0; @@ -181,85 +361,29 @@ int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize) * @name: name to look for * returns char * value - could be NULL */ -char *sysfs_get_value_from_attributes(struct sysfs_attribute *attr, - const char *name) +char *sysfs_get_value_from_attributes(struct dlist *attr, const char *name) { struct sysfs_attribute *cur = NULL; - char tmpname[SYSFS_NAME_LEN]; if (attr == NULL || name == NULL) { errno = EINVAL; return NULL; - } - cur = attr; - while (cur != NULL) { - memset(tmpname, 0, SYSFS_NAME_LEN); - if ((sysfs_get_name_from_path(cur->path, tmpname, - SYSFS_NAME_LEN)) != 0) { - cur = cur->next; - continue; - } - if (strcmp(tmpname, name) == 0) + } + dlist_for_each_data(attr, cur, struct sysfs_attribute) { + if (strcmp(cur->name, name) == 0) return cur->value; - cur = cur->next; } return NULL; } /** - * add_subdir_to_dir: adds subdirectory to directory's subdirs - * @sysdir: directory to add subdir to - * @subdir: subdirectory to add. - */ -static void add_subdir_to_dir(struct sysfs_directory *sysdir, - struct sysfs_directory *subdir) -{ - if (sysdir != NULL && subdir != NULL) { - subdir->next = sysdir->subdirs; - sysdir->subdirs = subdir; - } -} - -/** - * add_attr_to_dir: adds attribute to directory's attributes - * @sysdir: directory to add attribute to - * @sysattr: attribute to add. - */ -static void add_attr_to_dir(struct sysfs_directory *sysdir, - struct sysfs_attribute *sysattr) -{ - if (sysdir != NULL && sysattr != NULL) { - sysattr->next = sysdir->attributes; - sysdir->attributes = sysattr; - } -} - -/** - * sysfs_close_dlink: closes and cleans up directory link. - * @dlink: directory link to close. - */ -void sysfs_close_dlink(struct sysfs_dlink *dlink) -{ - if (dlink != NULL) { - dlink->next = NULL; - if (dlink->target != NULL) - sysfs_close_directory(dlink->target); - free(dlink); - } -} - -/** - * add_dlink_to_dir: adds directory link to directory's links list. - * @sysdir: directory to add it to. - * @dlink: link to add. + * sysfs_close_link: closes and cleans up link. + * @ln: link to close. */ -static void add_dlink_to_dir(struct sysfs_directory *sysdir, - struct sysfs_dlink *dlink) +void sysfs_close_link(struct sysfs_link *ln) { - if (sysdir != NULL && dlink != NULL) { - dlink->next = sysdir->links; - sysdir->links = dlink; - } + if (ln != NULL) + free(ln); } /** @@ -268,36 +392,15 @@ static void add_dlink_to_dir(struct sysfs_directory *sysdir, */ void sysfs_close_directory(struct sysfs_directory *sysdir) { - struct sysfs_directory *sdir = NULL, *dnext = NULL; - struct sysfs_dlink *dlink = NULL, *nextl = NULL; - struct sysfs_attribute *attr = NULL, *anext = NULL; - if (sysdir != NULL) { - if (sysdir->subdirs != NULL) { - for (sdir = sysdir->subdirs; sdir != NULL; - sdir = dnext) { - dnext = sdir->next; - sysfs_close_directory(sdir); - } - } - if (sysdir->links != NULL) { - for (dlink = sysdir->links; dlink != NULL; - dlink = nextl) { - nextl = dlink->next; - sysfs_close_dlink(dlink); - } - } - if (sysdir->attributes != NULL) { - for (attr = sysdir->attributes; attr != NULL; - attr = anext) { - anext = attr->next; - /* sysfs_close_attribute(attr); */ - if (attr->value != NULL) - free(attr->value); - free(attr); - } - } + if (sysdir->subdirs != NULL) + dlist_destroy(sysdir->subdirs); + if (sysdir->links != NULL) + dlist_destroy(sysdir->links); + if (sysdir->attributes != NULL) + dlist_destroy(sysdir->attributes); free(sysdir); + sysdir = NULL; } } @@ -312,12 +415,44 @@ static struct sysfs_directory *alloc_directory(void) } /** - * alloc_dlink: allocates and initializes directory link structure - * returns struct sysfs_dlink with success or NULL with error. + * alloc_link: allocates and initializes link structure + * returns struct sysfs_link with success or NULL with error. + */ +static struct sysfs_link *alloc_link(void) +{ + return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link)); +} + +/** + * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs + * @sysdir: directory whose subdirs need reading. + * returns 0 with success and -1 with error. */ -static struct sysfs_dlink *alloc_dlink(void) +int sysfs_read_all_subdirs(struct sysfs_directory *sysdir) { - return (struct sysfs_dlink *)calloc(1, sizeof(struct sysfs_dlink)); + struct sysfs_directory *cursub = NULL; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + if (sysdir->subdirs == NULL) + if ((sysfs_read_dir_subdirs(sysdir)) != 0) + return 0; + if (sysdir->subdirs != NULL) { + dlist_for_each_data(sysdir->subdirs, cursub, + struct sysfs_directory) { + if ((sysfs_read_dir_subdirs(cursub)) != 0) { + dprintf ("Error reading subdirectory %s\n", + cursub->name); + retval = -1; + } + } + } + if (!retval) + errno = 0; + return retval; } /** @@ -334,57 +469,252 @@ struct sysfs_directory *sysfs_open_directory(const char *path) errno = EINVAL; return NULL; } + + if (sysfs_path_is_dir(path) != 0) { + dprintf("Invalid path to directory %s\n", path); + errno = EINVAL; + return NULL; + } + sdir = alloc_directory(); if (sdir == NULL) { - dprintf(stderr, "Error allocating directory %s\n", path); + dprintf("Error allocating directory %s\n", path); return NULL; } - strncpy(sdir->path, path, sizeof(sdir->path)); + if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) { + dprintf("Error getting directory name from path: %s\n", path); + sysfs_close_directory(sdir); + return NULL; + } + safestrcpy(sdir->path, path); return sdir; } /** - * sysfs_open_dlink: opens a sysfs directory link, creates struct, and returns + * sysfs_open_link: opens a sysfs link, creates struct, and returns * @path: path of link to open. - * returns: struct sysfs_dlink * with success and NULL on error. + * returns: struct sysfs_link * with success and NULL on error. */ -struct sysfs_dlink *sysfs_open_dlink(const char *linkpath) +struct sysfs_link *sysfs_open_link(const char *linkpath) { - struct sysfs_dlink *dlink = NULL; - struct sysfs_directory *tdir = NULL; - char name[SYSFS_NAME_LEN]; - char target[SYSFS_PATH_MAX]; + struct sysfs_link *ln = NULL; - if (linkpath == NULL) { + if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) { errno = EINVAL; return NULL; } - memset(name, 0, SYSFS_NAME_LEN); - memset(target, 0, SYSFS_PATH_MAX); - if ((sysfs_get_name_from_path(linkpath, name, SYSFS_NAME_LEN)) != 0 - || (sysfs_get_link(linkpath, target, SYSFS_PATH_MAX)) != 0) { - errno = EINVAL; - dprintf(stderr, "Invalid link path %s\n", linkpath); + ln = alloc_link(); + if (ln == NULL) { + dprintf("Error allocating link %s\n", linkpath); return NULL; } - dlink = alloc_dlink(); - if (dlink == NULL) { - dprintf(stderr, - "Error allocating directory link %s\n", linkpath); + safestrcpy(ln->path, linkpath); + if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0 + || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) { + errno = EINVAL; + dprintf("Invalid link path %s\n", linkpath); return NULL; } - strcpy(dlink->name, name); - tdir = sysfs_open_directory(target); - if (tdir == NULL) { - dprintf(stderr, "Invalid directory link target %s\n", target); - sysfs_close_dlink(dlink); - return NULL; - } - dlink->target = tdir; - return dlink; + return ln; +} + +/** + * add_attribute: open and add attribute at path to given directory + * @sysdir: directory to add attribute to + * @path: path to attribute + * returns 0 with success and -1 with error. + */ +static int add_attribute(struct sysfs_directory *sysdir, const char *path) +{ + struct sysfs_attribute *attr = NULL; + + attr = sysfs_open_attribute(path); + if (attr == NULL) { + dprintf("Error opening attribute %s\n", path); + return -1; + } + if (attr->method & SYSFS_METHOD_SHOW) { + if ((sysfs_read_attribute(attr)) != 0) { + dprintf("Error reading attribute %s\n", path); + sysfs_close_attribute(attr); + return 0; + } + } + + if (sysdir->attributes == NULL) { + sysdir->attributes = dlist_new_with_delete + (sizeof(struct sysfs_attribute), sysfs_del_attribute); + } + dlist_unshift_sorted(sysdir->attributes, attr, sort_list); + + return 0; +} + +/** + * add_subdirectory: open and add subdirectory at path to given directory + * @sysdir: directory to add subdir to + * @path: path to subdirectory + * returns 0 with success and -1 with error. + */ +static int add_subdirectory(struct sysfs_directory *sysdir, const char *path) +{ + struct sysfs_directory *subdir = NULL; + + subdir = sysfs_open_directory(path); + if (subdir == NULL) { + dprintf("Error opening directory %s\n", path); + return -1; + } + if (sysdir->subdirs == NULL) + sysdir->subdirs = dlist_new_with_delete + (sizeof(struct sysfs_directory), sysfs_del_directory); + dlist_unshift_sorted(sysdir->subdirs, subdir, sort_list); + return 0; +} + +/** + * add_link: open and add link at path to given directory + * @sysdir: directory to add link to + * @path: path to link + * returns 0 with success and -1 with error. + */ +static int add_link(struct sysfs_directory *sysdir, const char *path) +{ + struct sysfs_link *ln = NULL; + + ln = sysfs_open_link(path); + if (ln == NULL) { + dprintf("Error opening link %s\n", path); + return -1; + } + if (sysdir->links == NULL) + sysdir->links = dlist_new_with_delete + (sizeof(struct sysfs_link), sysfs_del_link); + dlist_unshift_sorted(sysdir->links, ln, sort_list); + return 0; +} + +/** + * sysfs_read_dir_attributes: grabs attributes for the given directory + * @sysdir: sysfs directory to open + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_attributes(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + safestrcpy(file_path, sysdir->path); + safestrcat(file_path, "/"); + safestrcat(file_path, dirent->d_name); + if ((sysfs_path_is_file(file_path)) == 0) + retval = add_attribute(sysdir, file_path); + } + closedir(dir); + if (!retval) + errno = 0; + return(retval); +} + +/** + * sysfs_read_dir_links: grabs links in a specific directory + * @sysdir: sysfs directory to read links + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_links(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + safestrcpy(file_path, sysdir->path); + safestrcat(file_path, "/"); + safestrcat(file_path, dirent->d_name); + if ((sysfs_path_is_link(file_path)) == 0) { + retval = add_link(sysdir, file_path); + if (retval != 0) + break; + } + } + closedir(dir); + if (!retval) + errno = 0; + return(retval); +} + +/** + * sysfs_read_dir_subdirs: grabs subdirs in a specific directory + * @sysdir: sysfs directory to read links + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + safestrcpy(file_path, sysdir->path); + safestrcat(file_path, "/"); + safestrcat(file_path, dirent->d_name); + if ((sysfs_path_is_dir(file_path)) == 0) + retval = add_subdirectory(sysdir, file_path); + } + closedir(dir); + if (!retval) + errno = 0; + return(retval); } /** @@ -397,9 +727,6 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) DIR *dir = NULL; struct dirent *dirent = NULL; struct stat astats; - struct sysfs_attribute *attr = NULL; - struct sysfs_directory *subdir = NULL; - struct sysfs_dlink *dlink = NULL; char file_path[SYSFS_PATH_MAX]; int retval = 0; @@ -409,7 +736,7 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) } dir = opendir(sysdir->path); if (dir == NULL) { - perror("opendir"); + dprintf("Error opening directory %s\n", sysdir->path); return -1; } while(((dirent = readdir(dir)) != NULL) && retval == 0) { @@ -418,79 +745,325 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) if (0 == strcmp(dirent->d_name, "..")) continue; memset(file_path, 0, SYSFS_PATH_MAX); - strncpy(file_path, sysdir->path, sizeof(file_path)); - strncat(file_path, "/", sizeof(file_path)); - strncat(file_path, dirent->d_name, sizeof(file_path)); + safestrcpy(file_path, sysdir->path); + safestrcat(file_path, "/"); + safestrcat(file_path, dirent->d_name); if ((lstat(file_path, &astats)) != 0) { - perror("stat"); + dprintf("stat failed\n"); continue; } - if (S_ISREG(astats.st_mode)) { - attr = sysfs_open_attribute(file_path); - if (attr == NULL) { - dprintf (stderr, "Error opening attribute %s\n", - file_path); - retval = -1; - break; + if (S_ISDIR(astats.st_mode)) + retval = add_subdirectory(sysdir, file_path); + + else if (S_ISLNK(astats.st_mode)) + retval = add_link(sysdir, file_path); + + else if (S_ISREG(astats.st_mode)) + retval = add_attribute(sysdir, file_path); + } + closedir(dir); + if (!retval) + errno = 0; + 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; + } + errno = 0; + 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; + } + errno = 0; + 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; + } + errno = 0; + return 0; +} + +/** + * sysfs_get_directory_attribute: retrieves attribute attrname from current + * directory only + * @dir: directory to retrieve attribute from + * @attrname: name of attribute to look for + * + * NOTE: Since we know the attribute to look for, this routine looks for the + * attribute if it was created _after_ the attrlist was read initially. + * + * returns sysfs_attribute if found and NULL if not found + */ +struct sysfs_attribute *sysfs_get_directory_attribute + (struct sysfs_directory *dir, char *attrname) +{ + struct sysfs_attribute *attr = NULL; + char new_path[SYSFS_PATH_MAX]; + + if (dir == NULL || attrname == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->attributes == NULL) + if ((sysfs_read_dir_attributes(dir) != 0) + || (dir->attributes == NULL)) + return NULL; + + attr = (struct sysfs_attribute *)dlist_find_custom + (dir->attributes, attrname, dir_attribute_name_equal); + 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); + safestrcpy(new_path, dir->path); + safestrcat(new_path, "/"); + safestrcat(new_path, attrname); + if ((sysfs_path_is_file(new_path)) == 0) { + if ((add_attribute(dir, new_path)) == 0) { + attr = (struct sysfs_attribute *) + dlist_find_custom(dir->attributes, + attrname, dir_attribute_name_equal); } - if (attr->method & SYSFS_METHOD_SHOW) { - if ((sysfs_read_attribute(attr)) != 0) { - dprintf (stderr, - "Error reading attribute %s\n", - file_path); - sysfs_close_attribute(attr); + } + } + + return attr; +} + +/** + * sysfs_get_directory_link: retrieves link from one directory list + * @dir: directory to retrieve link from + * @linkname: name of link to look for + * returns reference to sysfs_link if found and NULL if not found + */ +struct sysfs_link *sysfs_get_directory_link + (struct sysfs_directory *dir, char *linkname) +{ + if (dir == NULL || linkname == NULL) { + errno = EINVAL; + return 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); +} + +/** + * sysfs_get_subdirectory: retrieves subdirectory by name. + * @dir: directory to search for subdirectory. + * @subname: subdirectory name to get. + * returns reference to subdirectory or NULL if not found + */ +struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir, + char *subname) +{ + struct sysfs_directory *sub = NULL, *cursub = NULL; + + if (dir == NULL || subname == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->subdirs == NULL) + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; + + sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs, + subname, dir_subdir_name_equal); + if (sub != NULL) + return sub; + + if (dir->subdirs != NULL) { + dlist_for_each_data(dir->subdirs, cursub, + struct sysfs_directory) { + if (cursub->subdirs == NULL) { + if (sysfs_read_dir_subdirs(cursub) != 0) + continue; + if (cursub->subdirs == NULL) continue; - } - } - add_attr_to_dir(sysdir, attr); - } else if (S_ISDIR(astats.st_mode)) { - subdir = sysfs_open_directory(file_path); - if (subdir == NULL) { - dprintf (stderr, "Error opening directory %s\n", - file_path); - retval = -1; - break; - } - add_subdir_to_dir(sysdir, subdir); - } else if (S_ISLNK(astats.st_mode)) { - dlink = sysfs_open_dlink(file_path); - if (dlink == NULL) { - dprintf(stderr, "Error opening link %s\n", - file_path); - retval = -1; - break; } - add_dlink_to_dir(sysdir, dlink); + sub = sysfs_get_subdirectory(cursub, subname); + if (sub != NULL) + return sub; } } - closedir(dir); - return(retval); + return NULL; } /** - * sysfs_read_dlinks: reads a directory link's target directory. Can - * supply a linked list of links. - * @dlink: directory link to read. - * returns 0 with success or -1 with error. + * sysfs_get_subdirectory_link: looks through all subdirs for specific link. + * @dir: directory and subdirectories to search for link. + * @linkname: link name to get. + * returns reference to link or NULL if not found */ -int sysfs_read_dlinks(struct sysfs_dlink *dlink) +struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, + char *linkname) { - struct sysfs_dlink *cur = NULL; + struct sysfs_directory *cursub = NULL; + struct sysfs_link *ln = NULL; - if (dlink == NULL || dlink->target == NULL) { + if (dir == NULL || linkname == NULL) { errno = EINVAL; - return -1; + return NULL; } - cur = dlink; - while (cur != NULL) { - if ((sysfs_read_directory(cur->target)) != 0) { - dprintf(stderr, - "Error reading directory link target %s\n", - dlink->name); - return -1; + + ln = sysfs_get_directory_link(dir, linkname); + if (ln != NULL) + return ln; + + if (dir->subdirs == NULL) + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; + + if (dir->subdirs != NULL) { + dlist_for_each_data(dir->subdirs, cursub, + struct sysfs_directory) { + ln = sysfs_get_subdirectory_link(cursub, linkname); + if (ln != NULL) + return ln; } - cur = cur->next; } - - return 0; + 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); }