Skip to content

Commit

Permalink
Make get_sysfs_attr() use our path joiners and add sysfs_readlink()
Browse files Browse the repository at this point in the history
Because seriously, code shouldn't have to care about if we do
get_sysfs_attr("/sys/foo/","bar") or get_sysfs_attr("/sys/foo","/bar")

This also makes these functions able to deal with an alternate root
directory separately from the sysfs path.

Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela committed Nov 6, 2015
1 parent df67eed commit 52f6c29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
12 changes: 6 additions & 6 deletions blivet/devicelibs/edd.py
Expand Up @@ -80,7 +80,7 @@ def __init__(self, sysfspath):
that this is a particular device. Used for logging later.
"""

self.version = util.get_sysfs_attr(self.sysfspath, "version")
self.version = util.get_sysfs_attr(self.sysfspath, "version", root=fsroot)
""" The edd version this entry claims conformance with, from
/sys/firmware/edd/int13_devXX/version """

Expand Down Expand Up @@ -239,7 +239,7 @@ def __repr__(self):
return "<EddEntry%s>" % (self._fmt(' ', ''),)

def load(self):
interface = util.get_sysfs_attr(self.sysfspath, "interface")
interface = util.get_sysfs_attr(self.sysfspath, "interface", root=fsroot)
# save this so we can log it from the matcher.
self.interface = interface
if interface:
Expand Down Expand Up @@ -302,11 +302,11 @@ def load(self):
else:
raise e

self.mbr_sig = util.get_sysfs_attr(self.sysfspath, "mbr_signature")
sectors = util.get_sysfs_attr(self.sysfspath, "sectors")
self.mbr_sig = util.get_sysfs_attr(self.sysfspath, "mbr_signature", root=fsroot)
sectors = util.get_sysfs_attr(self.sysfspath, "sectors", root=fsroot)
if sectors:
self.sectors = int(sectors)
hbus = util.get_sysfs_attr(self.sysfspath, "host_bus")
hbus = util.get_sysfs_attr(self.sysfspath, "host_bus", root=fsroot)
if hbus:
match = re_host_bus_pci.match(hbus)
if match:
Expand Down Expand Up @@ -372,7 +372,7 @@ def devname_from_ata_pci_dev(self):
ata_port_idx = int(components[5][3:])

fn = components[0:6] + ['ata_port', ata_port]
port_no = int(util.get_sysfs_attr(os.path.join(*fn), 'port_no'))
port_no = int(util.get_sysfs_attr(os.path.join(*fn), 'port_no', root=fsroot))

if self.edd.type == "ATA":
# On ATA, port_no is kernel's ata_port->local_port_no, which
Expand Down
32 changes: 27 additions & 5 deletions blivet/util.py
Expand Up @@ -339,25 +339,47 @@ def join_paths(*paths):
return join_paths(*paths[0])
return normalize_path_slashes('/'.join(paths))

def get_sysfs_attr(path, attr):
def get_sysfs_attr(path, attr, root=None):
if not attr:
log.debug("get_sysfs_attr() called with attr=None")
return None
if not isinstance(path, Path):
path = Path(path=path, root=root)
elif root != None:
path.newroot(root)

attribute = "%s/%s" % (path, attr)
attribute = os.path.realpath(attribute)
attribute = path + attr
fullattr = os.path.realpath(attribute.ondisk)

if not os.path.isfile(attribute) and not os.path.islink(attribute):
if not os.path.isfile(fullattr) and not os.path.islink(fullattr):
log.warning("%s is not a valid attribute", attr)
return None

f = open(attribute, "r")
f = open(fullattr, "r")
data = f.read()
f.close()
sdata = "".join(["%02x" % (ord(x),) for x in data])
testdata_log.debug("sysfs attr %s: %s", attribute, sdata)
return data.strip()

def sysfs_readlink(path, link, root=None):
if not link:
log.debug("sysfs_readlink() called with link=None")
if isinstance(path, Path):
linkpath = path + link
else:
linkpath = Path(path, root=root) + link

linkpath = Path(os.path.normpath(linkpath), root=linkpath.root)
fullpath = os.path.normpath(linkpath.ondisk)

if not os.path.islink(fullpath):
log.warning("%s is not a valid symlink", linkpath)
return None

output = os.readlink(fullpath)
testdata_log.debug("new sysfs link: \"%s\" -> \"%s\"", linkpath, output)
return output

def get_sysfs_path_by_name(dev_node, class_name="block"):
""" Return sysfs path for a given device.
Expand Down

0 comments on commit 52f6c29

Please sign in to comment.