File 0001-Handle-write-only-attributes.patch of Package python-rtslib-fb

From 03c8c15983a21bc2b158c58140a2871bb1ed857b Mon Sep 17 00:00:00 2001
From: Lee Duncan <lduncan@suse.com>
Date: Wed, 6 Feb 2019 12:29:41 -0800
Subject: [PATCH 1/3] Handle write-only attributes.
Patch-mainline: In developer's queue

A recent kernel change (see commit 6baca7601bdee2e5) makes
the pi_prot_format protection information attribute write-only,
since it always returned 0 when being read. This commit is being
reverted, but it still brought up the prospect of write-only
attributes.

Currently, when doing a dump(), rtslib iterates through all
readable attributes to decide which ones to save as part of
our current state, but saving write-only attributes
makes no sense, since we cannot read them to capture their
value. Towards this end, enhande the _list_files() internal
method to allow filtering on whether the file is writable
as well as whether or not it is readable. Then modify
list_attributes() to allow this new parameter and modify
dump() to request only R/W attributes.
---
 rtslib/node.py | 53 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/rtslib/node.py b/rtslib/node.py
index 1d77cd1b81ba..1ab7d9b8c58b 100644
--- a/rtslib/node.py
+++ b/rtslib/node.py
@@ -81,31 +81,42 @@ class CFSNode(object):
             raise RTSLibNotInCFS("This %s does not exist in configFS"
                                  % self.__class__.__name__)
 
-    def _list_files(self, path, writable=None):
+    def _list_files(self, path, writable=None, readable=None):
         '''
         List files under a path depending on their owner's write permissions.
         @param path: The path under which the files are expected to be. If the
         path itself is not a directory, an empty list will be returned.
         @type path: str
-        @param writable: If None (default), returns all parameters, if True,
-        returns read-write parameters, if False, returns just the read-only
-        parameters.
+        @param writable: If None (default), return all files despite their
+        writability. If True, return only writable files. If False, return
+        only non-writable files.
         @type writable: bool or None
-        @return: List of file names filtered according to their write perms.
+        @param readable: If None (default), return all files despite their
+        readability. If True, return only readable files. If False, return
+        only non-readable files.
+        @type readable: bool or None
+        @return: List of file names filtered according to their
+        read/write perms.
         '''
         if not os.path.isdir(path):
             return []
 
-        if writable is None:
+        if writable is None and readable is None:
             names = os.listdir(path)
-        elif writable:
-            names = [name for name in os.listdir(path)
-                     if (os.stat("%s/%s" % (path, name))[stat.ST_MODE] \
-                         & stat.S_IWUSR)]
         else:
-            names = [os.path.basename(name) for name in os.listdir(path)
-                     if not (os.stat("%s/%s" % (path, name))[stat.ST_MODE] \
-                             & stat.S_IWUSR)]
+            names = []
+            for name in os.listdir(path):
+                sres = os.stat("%s/%s" % (path, name))
+                if writable is not None:
+                    if writable != ((sres[stat.ST_MODE] & stat.S_IWUSR) == \
+                            stat.S_IWUSR):
+                        continue
+                if readable is not None:
+                    if readable != ((sres[stat.ST_MODE] & stat.S_IRUSR) == \
+                            stat.S_IRUSR):
+                        continue
+                names.append(name)
+
         names.sort()
         return names
 
@@ -123,17 +134,21 @@ class CFSNode(object):
         path = "%s/param" % self.path
         return self._list_files(path, writable)
 
-    def list_attributes(self, writable=None):
+    def list_attributes(self, writable=None, readable=None):
         '''
-        @param writable: If None (default), returns all attributes, if True,
-        returns read-write attributes, if False, returns just the read-only
-        attributes.
+        @param writable: If None (default), return all files despite their
+        writability. If True, return only writable files. If False, return
+        only non-writable files.
         @type writable: bool or None
+        @param readable: If None (default), return all files despite their
+        readability. If True, return only readable files. If False, return
+        only non-readable files.
+        @type readable: bool or None
         @return: A list of existing attribute names as strings.
         '''
         self._check_self()
         path = "%s/attrib" % self.path
-        return self._list_files(path, writable)
+        return self._list_files(path, writable, readable)
 
     def set_attribute(self, attribute, value):
         '''
@@ -220,7 +235,7 @@ class CFSNode(object):
         d = {}
         attrs = {}
         params = {}
-        for item in self.list_attributes(writable=True):
+        for item in self.list_attributes(writable=True, readable=True):
             try:
                 attrs[item] = int(self.get_attribute(item))
             except ValueError:
-- 
2.16.4

openSUSE Build Service is sponsored by