]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: use 'stat' to lookup nvme controller
authorHannes Reinecke <hare@suse.de>
Sat, 26 Jun 2021 11:05:10 +0000 (13:05 +0200)
committerHannes Reinecke <hare@suse.de>
Sat, 26 Jun 2021 11:05:10 +0000 (13:05 +0200)
When looking up nvme subsystems for individual controllers we should
be using 'stat()' instead of 'opendir()'; this is less heavy-handed
and provides better error checking.

Signed-off-by: Hannes Reinecke <hare@suse.de>
src/nvme/tree.c

index a0631ccb9039dd7985ca3a8741a96d5af8a33472..25b9b586aa1f360e30a7a0d293169d717be2057b 100644 (file)
@@ -17,6 +17,7 @@
 #include <unistd.h>
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 
@@ -988,7 +989,6 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_ctrl_t c)
 {
        struct dirent **subsys;
        char *subsys_name = NULL;
-       DIR *d;
        int ret, i;
        char path[PATH_MAX];
 
@@ -996,13 +996,14 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_ctrl_t c)
        if (ret < 0)
                return NULL;
        for (i = 0; i < ret; i++) {
+               struct stat st;
+
                sprintf(path, "%s/%s/%s", nvme_subsys_sysfs_dir,
                        subsys[i]->d_name, c->name);
-               d = opendir(path);
-               if (!d)
+               nvme_msg(LOG_DEBUG, "lookup subsystem %s\n", path);
+               if (stat(path, &st) < 0)
                        continue;
                subsys_name = strdup(subsys[i]->d_name);
-               closedir(d);
                break;
        }
        nvme_free_dirents(subsys, i);