From 318a6e3f85f960893433115df0806d7f0a622bfb Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Tue, 12 May 2015 18:00:00 -0500
Subject: [PATCH 53/54] Pass Event instances to event handlers and
 _notifyDevice.

This allows the StorageEventSynchronizer.validate method to exclude
events that arrived before whatever operation is being synchronized
was initiated.
---
 blivet/handler.py | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/blivet/handler.py b/blivet/handler.py
index 81e7edb..304777e 100644
--- a/blivet/handler.py
+++ b/blivet/handler.py
@@ -57,22 +57,23 @@ class EventHandler(object):
         currentThread().name = "Event%d" % event.id
         log.debug("event: %s", event)
         if event.action == "add":
-            self.deviceAddedCB(event.info)
+            self.deviceAddedCB(event)
         elif event.action == "remove":
-            self.deviceRemovedCB(event.info)
+            self.deviceRemovedCB(event)
         elif event.action == "change":
-            self.deviceChangedCB(event.info)
+            self.deviceChangedCB(event)
         else:
             log.info("unknown event: %s", event)
 
         if flags.console_debug:
             print "<- %s" % event
 
-    def deviceAddedCB(self, info, force=False):
+    def deviceAddedCB(self, event, force=False):
         """ Handle an "add" uevent on a block device.
 
             The device could be newly created or newly activated.
         """
+        info = event.info
         sysfs_path = udev.device_get_sysfs_path(info)
         log.debug("device added: %s", sysfs_path)
         if info.subsystem != "block":
@@ -96,9 +97,9 @@ class EventHandler(object):
 
         # If _notifyDevice returns True, this uevent is related to processing
         # of an action. It may or may not be in the tree.
-        if self._notifyDevice("add", info):
+        if self._notifyDevice(event):
             # This will update size, uuid, &c for new devices.
-            return self.deviceChangedCB(info, expected=True)
+            return self.deviceChangedCB(event, expected=True)
 
         if self.devicetree.actions.processing:
             log.debug("ignoring unexpected event during action processing")
@@ -259,8 +260,9 @@ class EventHandler(object):
         # MD TODO: raid level, spares
         # BTRFS TODO: check for changes to subvol list IFF the volume is mounted
 
-    def deviceChangedCB(self, info, expected=False):
+    def deviceChangedCB(self, event, expected=False):
         """ Handle a "changed" uevent on a block device. """
+        info = event.info
         sysfs_path = udev.device_get_sysfs_path(info)
         log.debug("device changed: %s", sysfs_path)
         if info.subsystem != "block":
@@ -292,11 +294,11 @@ class EventHandler(object):
              (udev.device_is_dm(info) and "DM_NAME" in info))):
             # md and dm devices aren't really added until you get a change
             # event
-            return self.deviceAddedCB(info, force=True)
+            return self.deviceAddedCB(event, force=True)
 
         if not expected:
             # See if this event was triggered a blivet action.
-            expected = self._notifyDevice("change", info)
+            expected = self._notifyDevice(event)
 
         if not expected and self.devicetree.actions.processing:
             log.debug("ignoring unexpected event during action processing")
@@ -416,7 +418,7 @@ class EventHandler(object):
 
             self._memberChangeHandler(info, device)
 
-    def deviceRemovedCB(self, info):
+    def deviceRemovedCB(self, event):
         """ Handle a "remove" uevent on a block device.
 
             This is generally going to be interpreted as a deactivation as
@@ -426,11 +428,12 @@ class EventHandler(object):
             It seems sensible to interpret remove events as deactivations and
             handle destruction via change events on parent devices.
         """
+        info = event.info
         log.debug("device removed: %s", udev.device_get_sysfs_path(info))
         if info.subsystem != "block":
             return
 
-        if self._notifyDevice("remove", info):
+        if self._notifyDevice(event):
             return
 
         if self.devicetree.actions.processing:
@@ -449,7 +452,7 @@ class EventHandler(object):
                     fmt._mountpoint = None
                     fmt._mounted_read_only = False
 
-    def _notifyDevice(self, action, info):
+    def _notifyDevice(self, event):
         """ Notify condition variables as appropriate.
 
             :param str action: an action string, eg: 'add', 'remove'
@@ -465,6 +468,8 @@ class EventHandler(object):
             operations. Flags within the manager are used to indicate which, if
             any, of these methods is under way.
         """
+        action = event.action
+        info = event.info
         ret = False
         name = udev.device_get_name(info)
         sysfs_path = udev.device_get_sysfs_path(info)
@@ -489,7 +494,7 @@ class EventHandler(object):
 
         if action in ("add", "change") and device and \
            device.modifySync.creating and not device.delegateModifyEvents and \
-           device.modifySync.validate(info):
+           device.modifySync.validate(event):
             log.debug("* create %s", device.name)
             ## device create without event delegation (eg: partition)
             event_sync = device.modifySync
@@ -521,13 +526,13 @@ class EventHandler(object):
                 event_sync = device.modifySync
             elif action == "change" and device and \
                  device.controlSync.changing and \
-                 device.controlSync.validate(info):
+                 device.controlSync.validate(event):
                 ## device change (eg: event on pv for vg or lv creation)
                 log.debug("* change %s", device.name)
                 event_sync = device.controlSync
             elif action == "change" and device and \
                  device.format.eventSync.active and \
-                 device.format.eventSync.validate(info):
+                 device.format.eventSync.validate(event):
                 ## any change to a format
                 log.debug("* change %s format", device.name)
                 event_sync = device.format.eventSync
@@ -551,7 +556,7 @@ class EventHandler(object):
                     for action in _actions:
                         if action.device.name == name and \
                            action.format.eventSync.active and \
-                           action.format.eventSync.validate(info):
+                           action.format.eventSync.validate(event):
                             ## any change to a format scheduled for destruction
                             ## on a device not scheduled for destruction
                             log.debug("*** change %s format", action.device.name)
@@ -574,7 +579,7 @@ class EventHandler(object):
                         event_sync = current_action.device.controlSync
                     elif action == "change" and \
                          current_action.device.modifySync.destroying and \
-                         current_action.device.modifySync.validate(info) and \
+                         current_action.device.modifySync.validate(event) and \
                          current_action.device.delegateModifyEvents:
                         # change to pv when removing lv or vg
                         name = current_action.device.name
@@ -582,7 +587,7 @@ class EventHandler(object):
                         event_sync = current_action.device.modifySync
                     elif action == "change" and \
                          current_action.format.eventSync.active and \
-                         current_action.format.eventSync.validate(info):
+                         current_action.format.eventSync.validate(event):
                         ## any change to a format on a device scheduled for
                         ## destruction
                         name = current_action.device.name
-- 
2.1.0

