Move helper class from watcher to utils.io
authorMichael Hanselmann <hansmi@google.com>
Thu, 5 Jan 2012 20:22:45 +0000 (21:22 +0100)
committerMichael Hanselmann <hansmi@google.com>
Fri, 6 Jan 2012 11:34:48 +0000 (12:34 +0100)
“FileStatHelper” can be used together with “ReadFile” to a file's status
while it's opened. This avoids certain race conditions.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/utils/io.py
lib/watcher/__init__.py

index 8c785f7..ed55f92 100644 (file)
@@ -72,6 +72,25 @@ def ErrnoOrStr(err):
   return detail
 
 
+class FileStatHelper:
+  """Helper to store file handle's C{fstat}.
+
+  Useful in combination with L{ReadFile}'s C{preread} parameter.
+
+  """
+  def __init__(self):
+    """Initializes this class.
+
+    """
+    self.st = None
+
+  def __call__(self, fh):
+    """Calls C{fstat} on file handle.
+
+    """
+    self.st = os.fstat(fh.fileno())
+
+
 def ReadFile(file_name, size=-1, preread=None):
   """Reads a file.
 
index 915dea0..0325cce 100644 (file)
@@ -410,23 +410,6 @@ def _UpdateInstanceStatus(filename, instances):
                                   for inst in instances])
 
 
-class _StatCb:
-  """Helper to store file handle's C{fstat}.
-
-  """
-  def __init__(self):
-    """Initializes this class.
-
-    """
-    self.st = None
-
-  def __call__(self, fh):
-    """Calls C{fstat} on file handle.
-
-    """
-    self.st = os.fstat(fh.fileno())
-
-
 def _ReadInstanceStatus(filename):
   """Reads an instance status file.
 
@@ -440,7 +423,7 @@ def _ReadInstanceStatus(filename):
   """
   logging.debug("Reading per-group instance status from '%s'", filename)
 
-  statcb = _StatCb()
+  statcb = utils.FileStatHelper()
   try:
     content = utils.ReadFile(filename, preread=statcb)
   except EnvironmentError, err: