Blame SOURCES/0003-refactored-sysfs-entry-to-always-store-resolved-devi.patch

bf6610
From e1ddfbef8bb460dec235628bd106e9e1b3aa483a Mon Sep 17 00:00:00 2001
bf6610
From: Dan Callaghan <dcallagh@redhat.com>
bf6610
Date: Wed, 8 Jul 2015 21:00:55 +1000
bf6610
Subject: [PATCH 3/4] refactored sysfs::entry to always store resolved device
bf6610
 path
bf6610
bf6610
The sysfs::entry will always be pointing at the fully resolved path
bf6610
under /sys/devices, which makes other sysfs operations like finding the
bf6610
parent device and checking attributes simpler.
bf6610
---
bf6610
 src/core/network.cc |  2 +-
bf6610
 src/core/osutils.cc | 13 +++++++
bf6610
 src/core/osutils.h  |  1 +
bf6610
 src/core/scsi.cc    |  2 +-
bf6610
 src/core/sysfs.cc   | 98 +++++++++++++++--------------------------------------
bf6610
 src/core/sysfs.h    |  7 ++--
bf6610
 6 files changed, 49 insertions(+), 74 deletions(-)
bf6610
bf6610
diff --git a/src/core/network.cc b/src/core/network.cc
bf6610
index c636514..230edf2 100644
bf6610
--- a/src/core/network.cc
bf6610
+++ b/src/core/network.cc
bf6610
@@ -321,7 +321,7 @@ bool scan_network(hwNode & n)
bf6610
       interface.claim();
bf6610
       interface.addHint("icon", string("network"));
bf6610
 
bf6610
-      string businfo = sysfs_getbusinfo(sysfs::entry::byClass("net", interface.getLogicalName()));
bf6610
+      string businfo = sysfs::entry::byClass("net", interface.getLogicalName()).businfo();
bf6610
       interface.setBusInfo(businfo);
bf6610
 
bf6610
 //scan_mii(fd, interface);
bf6610
diff --git a/src/core/osutils.cc b/src/core/osutils.cc
bf6610
index c9543b9..504fffd 100644
bf6610
--- a/src/core/osutils.cc
bf6610
+++ b/src/core/osutils.cc
bf6610
@@ -10,6 +10,7 @@
bf6610
 #include <limits.h>
bf6610
 #include <stdlib.h>
bf6610
 #include <string.h>
bf6610
+#include <libgen.h>
bf6610
 #include <regex.h>
bf6610
 #include <ctype.h>
bf6610
 #include <stdio.h>
bf6610
@@ -413,6 +414,18 @@ string realpath(const string & path)
bf6610
 }
bf6610
 
bf6610
 
bf6610
+string dirname(const string & path)
bf6610
+{
bf6610
+  size_t len = path.length();
bf6610
+  char *buffer = new char[len + 1];
bf6610
+  path.copy(buffer, len);
bf6610
+  buffer[len] = '\0';
bf6610
+  string result = dirname(buffer);
bf6610
+  delete buffer;
bf6610
+  return result;
bf6610
+}
bf6610
+
bf6610
+
bf6610
 string spaces(unsigned int count, const string & space)
bf6610
 {
bf6610
   string result = "";
bf6610
diff --git a/src/core/osutils.h b/src/core/osutils.h
bf6610
index 1eb59c0..549258e 100644
bf6610
--- a/src/core/osutils.h
bf6610
+++ b/src/core/osutils.h
bf6610
@@ -14,6 +14,7 @@ bool exists(const std::string & path);
bf6610
 bool samefile(const std::string & path1, const std::string & path2);
bf6610
 std::string readlink(const std::string & path);
bf6610
 std::string realpath(const std::string & path);
bf6610
+std::string dirname(const std::string & path);
bf6610
 bool loadfile(const std::string & file, std::vector < std::string > &lines);
bf6610
 
bf6610
 size_t splitlines(const std::string & s,
bf6610
diff --git a/src/core/scsi.cc b/src/core/scsi.cc
bf6610
index d85fcc8..4aaec81 100644
bf6610
--- a/src/core/scsi.cc
bf6610
+++ b/src/core/scsi.cc
bf6610
@@ -841,7 +841,7 @@ static bool scan_hosts(hwNode & node)
bf6610
 
bf6610
           if (!controller)
bf6610
           {
bf6610
-            string parentbusinfo = sysfs_getbusinfo(sysfs::entry::byClass("scsi_host", host_kname(number)));
bf6610
+            string parentbusinfo = sysfs::entry::byClass("scsi_host", host_kname(number)).businfo();
bf6610
 
bf6610
             controller = node.findChildByBusInfo(parentbusinfo);
bf6610
           }
bf6610
diff --git a/src/core/sysfs.cc b/src/core/sysfs.cc
bf6610
index 0c14a51..79c664e 100644
bf6610
--- a/src/core/sysfs.cc
bf6610
+++ b/src/core/sysfs.cc
bf6610
@@ -13,7 +13,6 @@
bf6610
 #include <stdio.h>
bf6610
 #include <string.h>
bf6610
 #include <dirent.h>
bf6610
-#include <libgen.h>
bf6610
 #include <sys/stat.h>
bf6610
 #include <sys/types.h>
bf6610
 #include <sys/mount.h>
bf6610
@@ -24,9 +23,7 @@ using namespace sysfs;
bf6610
 
bf6610
 struct sysfs::entry_i
bf6610
 {
bf6610
-  string devclass;
bf6610
-  string devbus;
bf6610
-  string devname;
bf6610
+  string devpath;
bf6610
 };
bf6610
 
bf6610
 struct sysfs_t
bf6610
@@ -102,7 +99,7 @@ static string sysfs_getbustype(const string & path)
bf6610
   {
bf6610
     devname =
bf6610
       string(fs.path + "/bus/") + string(namelist[i]->d_name) +
bf6610
-      "/devices/" + basename((char *) path.c_str());
bf6610
+      "/devices/" + basename(path.c_str());
bf6610
 
bf6610
     if (samefile(devname, path))
bf6610
       return string(namelist[i]->d_name);
bf6610
@@ -144,48 +141,15 @@ static string sysfstobusinfo(const string & path)
bf6610
 }
bf6610
 
bf6610
 
bf6610
-static string sysfs_getbusinfo_byclass(const string & devclass, const string & devname)
bf6610
+string entry::businfo() const
bf6610
 {
bf6610
-  string device =
bf6610
-    fs.path + string("/class/") + devclass + string("/") + devname + "/device";
bf6610
-  string result = "";
bf6610
-  int i = 0;
bf6610
-
bf6610
-  while((result == "") && (i<2))                  // device may point to /businfo or /businfo/devname
bf6610
-  {
bf6610
-    if(!exists(device)) return "";
bf6610
-    result = sysfstobusinfo(realpath(device));
bf6610
-    device += "/../" + devname + "/..";
bf6610
-    i++;
bf6610
-  }
bf6610
-
bf6610
+  string result = sysfstobusinfo(This->devpath);
bf6610
+  if (result.empty())
bf6610
+    result = sysfstobusinfo(dirname(This->devpath));
bf6610
   return result;
bf6610
 }
bf6610
 
bf6610
 
bf6610
-static string sysfs_getbusinfo_bybus(const string & devbus, const string & devname)
bf6610
-{
bf6610
-  string device =
bf6610
-    fs.path + string("/bus/") + devbus + string("/devices/") + devname;
bf6610
-  char buffer[PATH_MAX + 1];
bf6610
-
bf6610
-  if (!realpath(device.c_str(), buffer))
bf6610
-    return "";
bf6610
-
bf6610
-  return sysfstobusinfo(hw::strip(buffer));
bf6610
-}
bf6610
-
bf6610
-
bf6610
-string sysfs_getbusinfo(const entry & e)
bf6610
-{
bf6610
-  if(e.This->devclass != "")
bf6610
-    return sysfs_getbusinfo_byclass(e.This->devclass, e.This->devname);
bf6610
-  if(e.This->devbus != "")
bf6610
-    return sysfs_getbusinfo_bybus(e.This->devbus, e.This->devname);
bf6610
-  return "";
bf6610
-}
bf6610
-
bf6610
-
bf6610
 static string finddevice(const string & name, const string & root = "")
bf6610
 {
bf6610
   struct dirent **namelist;
bf6610
@@ -229,47 +193,33 @@ string sysfs_finddevice(const string & name)
bf6610
 }
bf6610
 
bf6610
 
bf6610
-string sysfs_getdriver(const string & devclass,
bf6610
-const string & devname)
bf6610
+string entry::driver() const
bf6610
 {
bf6610
-  string driverpath =
bf6610
-    fs.path + string("/class/") + devclass + string("/") + devname + "/";
bf6610
-  string driver = driverpath + "/driver";
bf6610
-  char buffer[PATH_MAX + 1];
bf6610
-  int namelen = 0;
bf6610
-
bf6610
-  if ((namelen = readlink(driver.c_str(), buffer, sizeof(buffer))) < 0)
bf6610
+  string driverlink = This->devpath + "/driver";
bf6610
+  if (!exists(driverlink))
bf6610
     return "";
bf6610
-
bf6610
-  return string(basename(buffer));
bf6610
+  return basename(readlink(driverlink).c_str());
bf6610
 }
bf6610
 
bf6610
 
bf6610
 entry entry::byBus(string devbus, string devname)
bf6610
 {
bf6610
-  entry e;
bf6610
-
bf6610
-  e.This->devbus = devbus;
bf6610
-  e.This->devname = devname;
bf6610
-
bf6610
+  entry e(fs.path + "/bus/" + devbus + "/devices/" + devname);
bf6610
   return e;
bf6610
 }
bf6610
 
bf6610
 
bf6610
 entry entry::byClass(string devclass, string devname)
bf6610
 {
bf6610
-  entry e;
bf6610
-
bf6610
-  e.This->devclass = devclass;
bf6610
-  e.This->devname = devname;
bf6610
-
bf6610
+  entry e(fs.path + "/class/" + devclass + "/" + devname + "/device");
bf6610
   return e;
bf6610
 }
bf6610
 
bf6610
 
bf6610
-entry::entry()
bf6610
+entry::entry(const string & devpath)
bf6610
 {
bf6610
   This = new entry_i;
bf6610
+  This->devpath = realpath(devpath);
bf6610
 }
bf6610
 
bf6610
 
bf6610
@@ -296,15 +246,23 @@ entry::~entry()
bf6610
 
bf6610
 bool entry::hassubdir(const string & s)
bf6610
 {
bf6610
-  if(This->devclass != "")
bf6610
-    return exists(fs.path + string("/class/") + This->devclass + string("/") + This->devname + "/" + s);
bf6610
-  
bf6610
-  if(This->devbus != "")
bf6610
-    return exists(fs.path + string("/bus/") + This->devbus + string("/devices/") + This->devname + string("/") + s);
bf6610
+  return exists(This->devpath + "/" + s);
bf6610
+}
bf6610
 
bf6610
-  return false;
bf6610
+
bf6610
+string entry::name() const
bf6610
+{
bf6610
+  return basename(This->devpath.c_str());
bf6610
+}
bf6610
+
bf6610
+
bf6610
+entry entry::parent() const
bf6610
+{
bf6610
+  entry e(dirname(This->devpath));
bf6610
+  return e;
bf6610
 }
bf6610
 
bf6610
+
bf6610
 bool scan_sysfs(hwNode & n)
bf6610
 {
bf6610
   return false;
bf6610
diff --git a/src/core/sysfs.h b/src/core/sysfs.h
bf6610
index 08d2bc3..31946a2 100644
bf6610
--- a/src/core/sysfs.h
bf6610
+++ b/src/core/sysfs.h
bf6610
@@ -21,11 +21,15 @@ namespace sysfs
bf6610
       ~entry();
bf6610
 
bf6610
       bool hassubdir(const string &);
bf6610
+      string name() const;
bf6610
+      string businfo() const;
bf6610
+      string driver() const;
bf6610
+      entry parent() const;
bf6610
 
bf6610
       struct entry_i * This;
bf6610
 
bf6610
     private:
bf6610
-      entry();
bf6610
+      entry(const string &);
bf6610
 
bf6610
   };
bf6610
 
bf6610
@@ -34,6 +38,5 @@ namespace sysfs
bf6610
 
bf6610
 bool scan_sysfs(hwNode & n);
bf6610
 
bf6610
-std::string sysfs_getbusinfo(const sysfs::entry &);
bf6610
 std::string sysfs_finddevice(const string &name);
bf6610
 #endif
bf6610
-- 
bf6610
2.7.3
bf6610