chiark / gitweb /
[PATCH] Libsysfs updates
[elogind.git] / libsysfs / sysfs_dir.c
index c159db7af8738de2047d369997438218effc855e..be54a1cf8689fe884dda70c59c45ab3db416dd0b 100644 (file)
@@ -20,7 +20,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  */
-#include "libsysfs.h"
+#include "sysfs/libsysfs.h"
 #include "sysfs.h"
 
 /**
@@ -58,9 +58,9 @@ static int dir_attribute_name_equal(void *a, void *b)
        if (a == NULL || b == NULL)
                return 0;
 
-       if (strcmp(((unsigned char *)a), ((struct sysfs_attribute *)b)->name) 
-           == 0)
+       if (strcmp(((char *)a), ((struct sysfs_attribute *)b)->name) == 0)
                return 1;
+
        return 0;
 }
 
@@ -75,9 +75,9 @@ static int dir_link_name_equal(void *a, void *b)
        if (a == NULL || b == NULL)
                return 0;
 
-       if (strcmp(((unsigned char *)a), ((struct sysfs_link *)b)->name) 
-           == 0)
+       if (strcmp(((char *)a), ((struct sysfs_link *)b)->name) == 0)
                return 1;
+
        return 0;
 }
 
@@ -92,9 +92,9 @@ static int dir_subdir_name_equal(void *a, void *b)
        if (a == NULL || b == NULL)
                return 0;
 
-       if (strcmp(((unsigned char *)a), ((struct sysfs_directory *)b)->name)
-           == 0)
+       if (strcmp(((char *)a), ((struct sysfs_directory *)b)->name) == 0)
                return 1;
+
        return 0;
 }
 
@@ -126,7 +126,7 @@ static struct sysfs_attribute *alloc_attribute(void)
  * @path: path to attribute.
  * returns sysfs_attribute struct with success and NULL with error.
  */
-struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
+struct sysfs_attribute *sysfs_open_attribute(const char *path)
 {
        struct sysfs_attribute *sysattr = NULL;
        struct stat fileinfo;
@@ -140,14 +140,13 @@ struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
                dprintf("Error allocating attribute at %s\n", path);
                return NULL;
        }
-       if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN) 
-           != 0) {
-               dprintf("Error retrieving attribute name from path: %s\n", 
-                       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;
        }
-       strncpy(sysattr->path, path, SYSFS_PATH_MAX);
+       safestrcpy(sysattr->path, path);
        if ((stat(sysattr->path, &fileinfo)) != 0) {
                dprintf("Stat failed: No such attribute?\n");
                sysattr->method = 0;
@@ -171,7 +170,7 @@ struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
  * returns 0 with success and -1 with error.
  */
 int sysfs_write_attribute(struct sysfs_attribute *sysattr,
-               const unsigned char *new_value, size_t len)
+               const char *new_value, size_t len)
 {
        int fd;
        int length;
@@ -184,6 +183,7 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
        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) {
@@ -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;       
                }
@@ -215,7 +215,7 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
                        sysattr->name);
                close(fd);
                return -1;
-       } else if (length != len) {
+       } else if ((unsigned int)length != len) {
                dprintf("Could not write %d bytes to attribute %s\n", 
                                        len, sysattr->name);
                /* 
@@ -236,13 +236,13 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
         */ 
        if (sysattr->method & SYSFS_METHOD_SHOW) {
                if (length != sysattr->len) {
-                       sysattr->value = (char *)realloc(sysattr->value, 
-                                                               length);
+                       sysattr->value = (char *)realloc
+                               (sysattr->value, length);
                        sysattr->len = length;
-                       strncpy(sysattr->value, new_value, length);
+                       safestrncpy(sysattr->value, new_value, length);
                } else {
                        /*"length" of the new value is same as old one */ 
-                       strncpy(sysattr->value, new_value, length);
+                       safestrncpy(sysattr->value, new_value, length);
                }
        }
                        
@@ -250,7 +250,6 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
        return 0;
 }
 
-
 /**
  * sysfs_read_attribute: reads value from attribute
  * @sysattr: attribute to read
@@ -258,8 +257,8 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
  */
 int sysfs_read_attribute(struct sysfs_attribute *sysattr)
 {
-       unsigned char *fbuf = NULL;
-       unsigned char *vbuf = NULL;
+       char *fbuf = NULL;
+       char *vbuf = NULL;
        ssize_t length = 0;
        long pgsize = 0;
        int fd;
@@ -271,14 +270,11 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
        if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
                dprintf("Show method not supported for attribute %s\n",
                        sysattr->path);
+               errno = EACCES;
                return -1;
        }
-#ifdef __KLIBC__
-       pgsize = 0x1000;                
-#else
-       pgsize = sysconf(_SC_PAGESIZE);
-#endif
-       fbuf = (unsigned char *)calloc(1, pgsize+1);
+       pgsize = getpagesize();
+       fbuf = (char *)calloc(1, pgsize+1);
        if (fbuf == NULL) {
                dprintf("calloc failed\n");
                return -1;
@@ -299,13 +295,14 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
                if ((sysattr->len == length) && 
                                (!(strncmp(sysattr->value, fbuf, length)))) {
                        close(fd);
+                       free(fbuf);
                        return 0;
                }
                free(sysattr->value);
        }
        sysattr->len = length;
        close(fd);
-       vbuf = (unsigned char *)realloc(fbuf, length+1);
+       vbuf = (char *)realloc(fbuf, length+1);
        if (vbuf == NULL) {
                dprintf("realloc failed\n");
                free(fbuf);
@@ -325,13 +322,13 @@ 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 unsigned char *attrpath, 
-                                       unsigned 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;
        }
@@ -351,7 +348,7 @@ int sysfs_read_attribute_value(const unsigned char *attrpath,
        if (length > vsize) 
                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;
@@ -362,10 +359,9 @@ int sysfs_read_attribute_value(const unsigned char *attrpath,
  *     attribute name, return its value
  * @attr: attribute to search
  * @name: name to look for
- * returns unsigned char * value - could be NULL
+ * returns char * value - could be NULL
  */
-unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, 
-                                       const unsigned char *name)
+char *sysfs_get_value_from_attributes(struct dlist *attr, const char *name)
 {      
        struct sysfs_attribute *cur = NULL;
        
@@ -435,6 +431,7 @@ static struct sysfs_link *alloc_link(void)
 int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
 {
        struct sysfs_directory *cursub = NULL;
+       int retval = 0;
 
        if (sysdir == NULL) {
                errno = EINVAL;
@@ -446,12 +443,16 @@ int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
        if (sysdir->subdirs != NULL) {
                dlist_for_each_data(sysdir->subdirs, cursub, 
                                                struct sysfs_directory) {
-                       if ((sysfs_read_dir_subdirs(cursub)) != 0) 
+                       if ((sysfs_read_dir_subdirs(cursub)) != 0) {
                                dprintf ("Error reading subdirectory %s\n",
                                                cursub->name);
+                               retval = -1;
+                       }
                }
        }
-       return 0;
+       if (!retval)
+               errno = 0;
+       return retval;
 }
 
 /**
@@ -460,7 +461,7 @@ int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
  * @path: path of directory to open.
  * returns: struct sysfs_directory * with success and NULL on error.
  */
-struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
+struct sysfs_directory *sysfs_open_directory(const char *path)
 {
        struct sysfs_directory *sdir = NULL;
 
@@ -468,6 +469,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 to directory %s\n", path);
+               errno = EINVAL;
+               return NULL;
+       }
+
        sdir = alloc_directory();
        if (sdir == NULL) {
                dprintf("Error allocating directory %s\n", path);
@@ -478,7 +486,7 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
                sysfs_close_directory(sdir);
                return NULL;
        }
-       strncpy(sdir->path, path, SYSFS_PATH_MAX);
+       safestrcpy(sdir->path, path);
 
        return sdir;
 }
@@ -488,7 +496,7 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
  * @path: path of link to open.
  * returns: struct sysfs_link * with success and NULL on error.
  */
-struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
+struct sysfs_link *sysfs_open_link(const char *linkpath)
 {
        struct sysfs_link *ln = NULL;
 
@@ -502,7 +510,7 @@ struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
                dprintf("Error allocating link %s\n", linkpath);
                return NULL;
        }
-       strcpy(ln->path, 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;
@@ -513,47 +521,13 @@ 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
  * @path: path to attribute
  * returns 0 with success and -1 with error.
  */
-static int add_attribute(struct sysfs_directory *sysdir, 
-                                       const unsigned char *path)
+static int add_attribute(struct sysfs_directory *sysdir, const char *path)
 {
        struct sysfs_attribute *attr = NULL;
 
@@ -574,7 +548,7 @@ static int add_attribute(struct sysfs_directory *sysdir,
                sysdir->attributes = dlist_new_with_delete
                        (sizeof(struct sysfs_attribute), sysfs_del_attribute);
        }
-       dlist_unshift(sysdir->attributes, attr);
+       dlist_unshift_sorted(sysdir->attributes, attr, sort_list);
 
        return 0;
 }
@@ -585,8 +559,7 @@ static int add_attribute(struct sysfs_directory *sysdir,
  * @path: path to subdirectory
  * returns 0 with success and -1 with error.
  */
-static int add_subdirectory(struct sysfs_directory *sysdir, 
-                                       const unsigned char *path)
+static int add_subdirectory(struct sysfs_directory *sysdir, const char *path)
 {
        struct sysfs_directory *subdir = NULL;
 
@@ -598,7 +571,7 @@ static int add_subdirectory(struct sysfs_directory *sysdir,
        if (sysdir->subdirs == NULL)
                sysdir->subdirs = dlist_new_with_delete
                        (sizeof(struct sysfs_directory), sysfs_del_directory);
-       dlist_unshift(sysdir->subdirs, subdir);
+       dlist_unshift_sorted(sysdir->subdirs, subdir, sort_list);
        return 0;
 }
 
@@ -608,7 +581,7 @@ static int add_subdirectory(struct sysfs_directory *sysdir,
  * @path: path to link
  * returns 0 with success and -1 with error.
  */
-static int add_link(struct sysfs_directory *sysdir, const unsigned char *path)
+static int add_link(struct sysfs_directory *sysdir, const char *path)
 {
        struct sysfs_link *ln = NULL;
 
@@ -620,7 +593,7 @@ static int add_link(struct sysfs_directory *sysdir, const unsigned char *path)
        if (sysdir->links == NULL)
                sysdir->links = dlist_new_with_delete
                                (sizeof(struct sysfs_link), sysfs_del_link);
-       dlist_unshift(sysdir->links, ln);
+       dlist_unshift_sorted(sysdir->links, ln, sort_list);
        return 0;
 }
 
@@ -633,8 +606,7 @@ 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];
+       char file_path[SYSFS_PATH_MAX];
        int retval = 0;
 
        if (sysdir == NULL) {
@@ -652,17 +624,15 @@ int sysfs_read_dir_attributes(struct sysfs_directory *sysdir)
                if (0 == strcmp(dirent->d_name, ".."))
                        continue;
                memset(file_path, 0, SYSFS_PATH_MAX);
-               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)) 
+               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);
 }
 
@@ -675,8 +645,7 @@ 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];
+       char file_path[SYSFS_PATH_MAX];
        int retval = 0;
 
        if (sysdir == NULL) {
@@ -694,20 +663,18 @@ int sysfs_read_dir_links(struct sysfs_directory *sysdir)
                if (0 == strcmp(dirent->d_name, ".."))
                        continue;
                memset(file_path, 0, SYSFS_PATH_MAX);
-               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)) {
+               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);
 }
 
@@ -720,8 +687,7 @@ 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];
+       char file_path[SYSFS_PATH_MAX];
        int retval = 0;
 
        if (sysdir == NULL) {
@@ -739,17 +705,15 @@ int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir)
                if (0 == strcmp(dirent->d_name, ".."))
                        continue;
                memset(file_path, 0, SYSFS_PATH_MAX);
-               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))
+               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);
 }
 
@@ -763,7 +727,7 @@ int sysfs_read_directory(struct sysfs_directory *sysdir)
        DIR *dir = NULL;
        struct dirent *dirent = NULL;
        struct stat astats;
-       unsigned char file_path[SYSFS_PATH_MAX];
+       char file_path[SYSFS_PATH_MAX];
        int retval = 0;
 
        if (sysdir == NULL) {
@@ -781,9 +745,9 @@ 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, SYSFS_PATH_MAX);
-               strcat(file_path, "/");
-               strcat(file_path, dirent->d_name);
+               safestrcpy(file_path, sysdir->path);
+               safestrcat(file_path, "/");
+               safestrcat(file_path, dirent->d_name);
                if ((lstat(file_path, &astats)) != 0) {
                        dprintf("stat failed\n");
                        continue;
@@ -798,21 +762,114 @@ int sysfs_read_directory(struct sysfs_directory *sysdir)
                        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, unsigned char *attrname)
+                       (struct sysfs_directory *dir, char *attrname)
 {
        struct sysfs_attribute *attr = NULL;
-       unsigned char new_path[SYSFS_PATH_MAX];
+       char new_path[SYSFS_PATH_MAX];
        
        if (dir == NULL || attrname == NULL) {
                errno = EINVAL;
@@ -826,19 +883,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);
+               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) {
+                       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;
 }
 
@@ -849,15 +912,19 @@ struct sysfs_attribute *sysfs_get_directory_attribute
  * returns reference to sysfs_link if found and NULL if not found
  */
 struct sysfs_link *sysfs_get_directory_link
-                       (struct sysfs_directory *dir, unsigned char *linkname)
+                       (struct sysfs_directory *dir, char *linkname)
 {
        if (dir == NULL || linkname == NULL) {
                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);
@@ -870,7 +937,7 @@ struct sysfs_link *sysfs_get_directory_link
  * returns reference to subdirectory or NULL if not found
  */
 struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
-                                               unsigned char *subname)
+                                               char *subname)
 {
        struct sysfs_directory *sub = NULL, *cursub = NULL;
 
@@ -912,7 +979,7 @@ struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
  * returns reference to link or NULL if not found
  */
 struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
-                                               unsigned char *linkname)
+                                               char *linkname)
 {
        struct sysfs_directory *cursub = NULL;
        struct sysfs_link *ln = NULL;
@@ -940,3 +1007,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);
+}