chiark / gitweb /
[PATCH] rework the logging code so that each program logs with the proper name in...
[elogind.git] / libsysfs / sysfs_dir.c
index ac2ecfcbe14bd056fa565b5f3cccf82125a63fb9..ce01669754160dfa652e29f5e7dd6462cae845ae 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;
@@ -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;       
                }
@@ -260,7 +260,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
 {
        unsigned char *fbuf = NULL;
        unsigned char *vbuf = NULL;
-       size_t length = 0;
+       ssize_t length = 0;
        long pgsize = 0;
        int fd;
 
@@ -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");
@@ -441,13 +438,15 @@ int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
                return -1;
        }
        if (sysdir->subdirs == NULL) 
-               if ((sysfs_read_dir_subdirs(sysdir) != 0) 
-                   || sysdir->subdirs == NULL)
+               if ((sysfs_read_dir_subdirs(sysdir)) != 0) 
                        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) {
+               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;
 }
@@ -466,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);
@@ -476,7 +482,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;
 }
@@ -511,44 +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);
-                       }
-               }
-       }
-       if (attrlist->count == 0) {
-               dprintf("No attributes in the list, destroying list now\n");
-               dlist_destroy(attrlist);
-               attrlist = NULL;
-               return 1;
-       }
-       return 0;
-}
-
 /**
  * add_attribute: open and add attribute at path to given directory
  * @sysdir: directory to add attribute to
@@ -636,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,14 +622,10 @@ 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, sizeof(file_path));
-               strncat(file_path, "/", sizeof(file_path));
-               strncat(file_path, dirent->d_name, sizeof(file_path));
-               if ((lstat(file_path, &astats)) != 0) {
-                       dprintf("stat failed\n");
-                       continue;
-               }
-               if (S_ISREG(astats.st_mode)) 
+               strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
+               strcat(file_path, "/");
+               strcat(file_path, dirent->d_name);
+               if ((sysfs_path_is_file(file_path)) == 0)
                        retval = add_attribute(sysdir, file_path);
        }
        closedir(dir);
@@ -678,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,14 +659,10 @@ 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, sizeof(file_path));
-               strncat(file_path, "/", sizeof(file_path));
-               strncat(file_path, dirent->d_name, sizeof(file_path));
-               if ((lstat(file_path, &astats)) != 0) {
-                       dprintf("stat failed\n");
-                       continue;
-               }
-               if (S_ISLNK(astats.st_mode)) {
+               strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
+               strcat(file_path, "/");
+               strcat(file_path, dirent->d_name);
+               if ((sysfs_path_is_link(file_path)) == 0) {
                        retval = add_link(sysdir, file_path);
                        if (retval != 0)
                                break;
@@ -723,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,14 +699,10 @@ 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, sizeof(file_path));
-               strncat(file_path, "/", sizeof(file_path));
-               strncat(file_path, dirent->d_name, sizeof(file_path));
-               if ((lstat(file_path, &astats)) != 0) {
-                       dprintf("stat failed\n");
-                       continue;
-               }
-               if (S_ISDIR(astats.st_mode))
+               strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
+               strcat(file_path, "/");
+               strcat(file_path, dirent->d_name);
+               if ((sysfs_path_is_dir(file_path)) == 0)
                        retval = add_subdirectory(sysdir, file_path);
        }
        closedir(dir);
@@ -784,9 +737,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, 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;
@@ -804,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
@@ -830,25 +867,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) {
-               /*
-                * don't read here since we would have read the attribute in 
-                * in the routine that called this routine
-                */ 
-               return attr;
+               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) {
-                               attr = (struct sysfs_attribute *)dlist_find_custom
-                                       (dir->attributes, attrname, dir_attribute_name_equal);
+                       if ((add_attribute(dir, new_path)) == 0) {
+                               attr = (struct sysfs_attribute *)
+                                       dlist_find_custom(dir->attributes,
+                                       attrname, dir_attribute_name_equal);
                        }
-                       return attr;
                }
        }
-       return NULL;
+               
+       return attr;
 }
 
 /**
@@ -864,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);
@@ -949,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);
+}