chiark / gitweb /
[PATCH] udev - drop all methods :)
[elogind.git] / libsysfs / sysfs_dir.c
index ff2edf461588458b68201051b5a709059b80b4fc..c159db7af8738de2047d369997438218effc855e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * syfs_dir.c
+ * sysfs_dir.c
  *
  * Directory utility functions for libsysfs
  *
@@ -147,7 +147,7 @@ struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
                sysfs_close_attribute(sysattr);
                return NULL;
        }
-       strncpy(sysattr->path, path, sizeof(sysattr->path));
+       strncpy(sysattr->path, path, SYSFS_PATH_MAX);
        if ((stat(sysattr->path, &fileinfo)) != 0) {
                dprintf("Stat failed: No such attribute?\n");
                sysattr->method = 0;
@@ -187,6 +187,13 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr,
                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("Attribute %s already has the requested value %s\n",
                                        sysattr->name, new_value);
@@ -253,8 +260,8 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
 {
        unsigned char *fbuf = NULL;
        unsigned char *vbuf = NULL;
-       size_t length = 0;
-       int pgsize = 0;
+       ssize_t length = 0;
+       long pgsize = 0;
        int fd;
 
        if (sysattr == NULL) {
@@ -266,7 +273,11 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
                        sysattr->path);
                return -1;
        }
-       pgsize = getpagesize();
+#ifdef __KLIBC__
+       pgsize = 0x1000;                
+#else
+       pgsize = sysconf(_SC_PAGESIZE);
+#endif
        fbuf = (unsigned char *)calloc(1, pgsize+1);
        if (fbuf == NULL) {
                dprintf("calloc failed\n");
@@ -284,6 +295,14 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
                free(fbuf);
                return -1;
        }
+       if (sysattr->len > 0) {
+               if ((sysattr->len == length) && 
+                               (!(strncmp(sysattr->value, fbuf, length)))) {
+                       close(fd);
+                       return 0;
+               }
+               free(sysattr->value);
+       }
        sysattr->len = length;
        close(fd);
        vbuf = (unsigned char *)realloc(fbuf, length+1);
@@ -385,6 +404,7 @@ void sysfs_close_directory(struct sysfs_directory *sysdir)
                if (sysdir->attributes != NULL) 
                        dlist_destroy(sysdir->attributes);
                free(sysdir);
+               sysdir = NULL;
        }
 }
 
@@ -420,12 +440,16 @@ int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
                errno = EINVAL;
                return -1;
        }
-       if (sysdir->subdirs == NULL)
-               return 0;
-       dlist_for_each_data(sysdir->subdirs, cursub, struct sysfs_directory) {
-               if (sysfs_read_directory(cursub) != 0) 
-                       dprintf ("Error reading subdirectory %s\n",
-                               cursub->name);
+       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);
+               }
        }
        return 0;
 }
@@ -454,7 +478,7 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
                sysfs_close_directory(sdir);
                return NULL;
        }
-       strncpy(sdir->path, path, sizeof(sdir->path));
+       strncpy(sdir->path, path, SYSFS_PATH_MAX);
 
        return sdir;
 }
@@ -490,18 +514,126 @@ struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
 }
 
 /**
- * sysfs_read_directory: grabs attributes, links, and subdirectories
+ * 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)
+{
+       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(sysdir->attributes, attr);
+
+       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 unsigned 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(sysdir->subdirs, subdir);
+       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 unsigned 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(sysdir->links, ln);
+       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_directory(struct sysfs_directory *sysdir)
+int sysfs_read_dir_attributes(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_link *ln = NULL;
        unsigned char file_path[SYSFS_PATH_MAX];
        int retval = 0;
 
@@ -520,61 +652,59 @@ 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));
+               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)) {  
-                       attr = sysfs_open_attribute(file_path);
-                       if (attr == NULL) {
-                               dprintf("Error opening attribute %s\n",
-                                       file_path);
-                               retval = -1;
-                               break;
-                       }
-                       if (attr->method & SYSFS_METHOD_SHOW) {
-                               if ((sysfs_read_attribute(attr)) != 0) {
-                                       dprintf("Error reading attribute %s\n",
-                                               file_path);
-                                       sysfs_close_attribute(attr);
-                                       continue;
-                               }
-                       }
-                                               
-                       if (sysdir->attributes == NULL) {
-                               sysdir->attributes = dlist_new_with_delete
-                                       (sizeof(struct sysfs_attribute),
-                                                       sysfs_del_attribute);
-                       }
-                       dlist_unshift(sysdir->attributes, attr);
-               } else if (S_ISDIR(astats.st_mode)) {
-                       subdir = sysfs_open_directory(file_path);
-                       if (subdir == NULL) {
-                               dprintf("Error opening directory %s\n",
-                                       file_path);
-                               retval = -1;
-                               break;
-                       }
-                       if (sysdir->subdirs == NULL)
-                               sysdir->subdirs = dlist_new_with_delete
-                                       (sizeof(struct sysfs_directory),
-                                                       sysfs_del_directory);
-                       dlist_unshift(sysdir->subdirs, subdir);
-               } else if (S_ISLNK(astats.st_mode)) {
-                       ln = sysfs_open_link(file_path);
-                       if (ln == NULL) {
-                               dprintf("Error opening link %s\n", file_path);
-                               retval = -1;
+               if (S_ISREG(astats.st_mode)) 
+                       retval = add_attribute(sysdir, file_path);
+       }
+       closedir(dir);
+       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;
+       struct stat astats;
+       unsigned 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);
+               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)) {
+                       retval = add_link(sysdir, file_path);
+                       if (retval != 0)
                                break;
-                       }
-                       if (sysdir->links == NULL)
-                               sysdir->links = dlist_new_with_delete
-                                               (sizeof(struct sysfs_link),
-                                                               sysfs_del_link);
-                       dlist_unshift(sysdir->links, ln);
                }
        }
        closedir(dir);
@@ -582,7 +712,98 @@ int sysfs_read_directory(struct sysfs_directory *sysdir)
 }
 
 /**
- * sysfs_get_directory_attribute: retrieves attribute attrname
+ * 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;
+       struct stat astats;
+       unsigned 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);
+               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))
+                       retval = add_subdirectory(sysdir, file_path);
+       }
+       closedir(dir);
+       return(retval);
+}
+
+/**
+ * sysfs_read_directory: grabs attributes, links, and subdirectories
+ * @sysdir: sysfs directory to open
+ * returns 0 with success and -1 with error.
+ */
+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];
+       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);
+               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)) 
+                       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);
+       return(retval);
+}
+
+/**
+ * sysfs_get_directory_attribute: retrieves attribute attrname from current
+ *     directory only
  * @dir: directory to retrieve attribute from
  * @attrname: name of attribute to look for
  * returns sysfs_attribute if found and NULL if not found
@@ -590,30 +811,35 @@ int sysfs_read_directory(struct sysfs_directory *sysdir)
 struct sysfs_attribute *sysfs_get_directory_attribute
                        (struct sysfs_directory *dir, unsigned char *attrname)
 {
-       struct sysfs_directory *sdir = NULL;
        struct sysfs_attribute *attr = NULL;
+       unsigned char new_path[SYSFS_PATH_MAX];
        
        if (dir == NULL || attrname == NULL) {
                errno = EINVAL;
                return NULL;
        }
-       
-       attr = (struct sysfs_attribute *)dlist_find_custom(dir->attributes,
-               attrname, dir_attribute_name_equal);
-       if (attr != NULL)
-               return attr;
-       
-       if (dir->subdirs != NULL) {
-               dlist_for_each_data(dir->subdirs, sdir, 
-                                       struct sysfs_directory) {
-                       if (sdir->attributes == NULL)
-                               continue;
-                       attr = sysfs_get_directory_attribute(sdir, attrname);
-                       if (attr != NULL)
-                               return attr;
+
+       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) {
+               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) {
+                               attr = (struct sysfs_attribute *)
+                                       dlist_find_custom(dir->attributes, 
+                                       attrname, dir_attribute_name_equal);
+                       }
                }
        }
-       return NULL;
+       return attr;
 }
 
 /**
@@ -629,6 +855,10 @@ struct sysfs_link *sysfs_get_directory_link
                errno = EINVAL;
                return NULL;
        }
+       if (dir->links == NULL)
+               if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL))
+                       return NULL;
+
        return (struct sysfs_link *)dlist_find_custom(dir->links,
                linkname, dir_link_name_equal);
 }
@@ -644,10 +874,15 @@ struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
 {
        struct sysfs_directory *sub = NULL, *cursub = NULL;
 
-       if (dir == NULL || dir->subdirs == NULL || subname == 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) 
@@ -656,8 +891,12 @@ struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
        if (dir->subdirs != NULL) {
                dlist_for_each_data(dir->subdirs, cursub, 
                                        struct sysfs_directory) {
-                       if (cursub->subdirs == NULL)
-                               continue;
+                       if (cursub->subdirs == NULL) {
+                               if (sysfs_read_dir_subdirs(cursub) != 0)
+                                       continue;
+                               if (cursub->subdirs == NULL)
+                                       continue;
+                       }
                        sub = sysfs_get_subdirectory(cursub, subname);
                        if (sub != NULL)
                                return sub;
@@ -678,7 +917,7 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
        struct sysfs_directory *cursub = NULL;
        struct sysfs_link *ln = NULL;
 
-       if (dir == NULL || dir->links == NULL || linkname == NULL) {
+       if (dir == NULL || linkname == NULL) {
                errno = EINVAL;
                return NULL;
        }
@@ -687,14 +926,13 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
        if (ln != NULL)
                return ln;
 
-       if (dir->subdirs == NULL)
-               return NULL;
+       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) {
-                       if (cursub->subdirs == NULL)
-                               continue;
                        ln = sysfs_get_subdirectory_link(cursub, linkname);
                        if (ln != NULL)
                                return ln;