]> www.infradead.org Git - users/hch/nvmetcli.git/commitdiff
nvmet: improve enable semantics
authorChristoph Hellwig <hch@lst.de>
Sat, 26 Mar 2016 10:35:20 +0000 (11:35 +0100)
committerChristoph Hellwig <hch@lst.de>
Sat, 26 Mar 2016 10:36:09 +0000 (11:36 +0100)
Allow for a None state of the _enable variable, and use that fact to
automatically set up the enabled state in the primary Node class.

Signed-off-by: Christoph Hellwig <hch@lst.de>
nvmet/nvme.py

index 89fc65f11a4fe5a50483420d713477c7a85fee1a..8e5fb574cab989b1d482214b0c7ae94aba39aa29 100644 (file)
@@ -50,7 +50,7 @@ class CFSNode(object):
 
     def __init__(self):
         self._path = self.configfs_dir
-        self._enable = 0
+        self._enable = None
         self.attr_groups = []
 
     def __eq__(self, other):
@@ -85,6 +85,7 @@ class CFSNode(object):
             except:
                 raise CFSError("Could not create %s in configFS" %
                                self.__class__.__name__)
+        self.get_enable()
 
     def _exists(self):
         return os.path.isdir(self.path)
@@ -138,7 +139,7 @@ class CFSNode(object):
         if not os.path.isfile(path):
             raise CFSError("Cannot find attribute: %s" % path)
 
-        if self._enable > 0:
+        if self._enable:
             raise CFSError("Cannot set attribute while %s is enabled" %
                            self.__class__.__name__)
 
@@ -167,7 +168,7 @@ class CFSNode(object):
         self._check_self()
         path = "%s/enable" % self.path
         if not os.path.isfile(path):
-            return False
+            return None
 
         with open(path, 'r') as file_fd:
             self._enable = int(file_fd.read().strip())
@@ -177,14 +178,14 @@ class CFSNode(object):
         self._check_self()
         path = "%s/enable" % self.path
 
-        if not os.path.isfile(path):
+        if not os.path.isfile(path) or self._enable is None:
             raise CFSError("Cannot enable %s" % self.path)
 
         try:
             with open(path, 'w') as file_fd:
                 file_fd.write(str(value))
         except Exception as e:
-            raise CFSError("Cannot enable attribute %s: %s (%s)" %
+            raise CFSError("Cannot enable %s: %s (%s)" %
                            (self.path, e, value))
         self._enable = value
 
@@ -479,7 +480,6 @@ class Namespace(CFSNode):
         self._nsid = nsid
         self._path = "%s/namespaces/%d" % (self.subsystem.path, self.nsid)
         self._create_in_cfs(mode)
-        self.get_enable()  # XXX should move to baseclass
 
     def _get_subsystem(self):
         return self._subsystem