Byron Clark avatar Byron Clark committed 47d6ffe

Only attempt to mount the device when media is inserted.

NTFS devices mounted using ntfs-3g (fuse) were being immediately mounted after
unmount. This change makes udiskie more careful about saving device state. It
also limits the automount algorithm to only mount a changed device if it has
had media inserted.

addresses #11

Comments (0)

Files changed (2)

udiskie/device.py

     def is_filesystem(self):
         return self._get_property('IdUsage') == 'filesystem'
 
+    def has_media(self):
+        return self._get_property('DeviceIsMediaAvailable')
+
     def id_type(self):
         return self._get_property('IdType')
 
 import udiskie.device
 
 class DeviceState:
-    def __init__(self, mounted):
+    def __init__(self, mounted, has_media):
         self.mounted = mounted
+        self.has_media = has_media
 
 
 class AutoMounter:
                                                         mount_paths),
                                   'drive-removable-media').show()
 
+        self._store_device_state(device)
+
+    def _store_device_state(self, device):
+        state = DeviceState(device.is_mounted(),
+                            device.has_media())
+        self.last_device_state[device.device_path] = state
+
+    def _remove_device_state(self, device):
+        if device.device_path in self.last_device_state:
+            del self.last_device_state[device.device_path]
+
+    def _get_device_state(self, device):
+        return self.last_device_state.get(device.device_path)
+
     def mount_present_devices(self):
         """Mount handleable devices that are already present."""
         for device in udiskie.device.get_all(self.bus):
 
     def device_added(self, device):
         self.log.debug('device added: %s' % (device,))
+        udiskie_device = udiskie.device.Device(self.bus, device)
         # Since the device just appeared we don't want the old state.
-        if device in self.last_device_state:
-            del self.last_device_state[device]
-        self._mount_device(udiskie.device.Device(self.bus, device))
+        self._remove_device_state(udiskie_device)
+        self._mount_device(udiskie_device)
 
     def device_removed(self, device):
         self.log.debug('device removed: %s' % (device,))
-        if device in self.last_device_state:
-            del self.last_device_state[device]
+        self._remove_device_state(udiskie.device.Device(self.bus, device))
 
     def device_changed(self, device):
         self.log.debug('device changed: %s' % (device,))
-        last_state = self.last_device_state.get(device)
+
         udiskie_device = udiskie.device.Device(self.bus, device)
-        if (not last_state) or (not last_state.mounted):
+        last_state = self._get_device_state(udiskie_device)
+
+        if not last_state:
+            # First time we saw the device, try to mount it.
             self._mount_device(udiskie_device)
-        self.last_device_state[device] = DeviceState(udiskie_device.is_mounted())
+        else:
+            media_added = False
+            if udiskie_device.has_media() and not last_state.has_media:
+                media_added = True
+
+            if media_added and not last_state.mounted:
+                # Wasn't mounted before, but it has new media now.
+                self._mount_device(udiskie_device)
+
+        self._store_device_state(udiskie_device)
 
 
 def cli(args):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.