2012-07-19 17:01:41 +00:00
|
|
|
{- git-annex assistant mount watcher, using either dbus or mtab polling
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Assistant.Threads.MountWatcher where
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
import Assistant.Common
|
2012-07-19 17:01:41 +00:00
|
|
|
import Assistant.ThreadedMonad
|
|
|
|
import Assistant.DaemonStatus
|
2012-07-23 03:16:56 +00:00
|
|
|
import Assistant.ScanRemotes
|
2012-08-22 18:32:17 +00:00
|
|
|
import Assistant.Sync
|
2012-10-24 17:35:43 +00:00
|
|
|
import Assistant.Pushes
|
2012-07-22 19:06:18 +00:00
|
|
|
import qualified Annex
|
|
|
|
import qualified Git
|
2012-07-19 17:01:41 +00:00
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import Utility.Mounts
|
2012-07-22 19:06:18 +00:00
|
|
|
import Remote.List
|
|
|
|
import qualified Types.Remote as Remote
|
2012-07-19 17:01:41 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
#if WITH_DBUS
|
2012-08-21 23:58:53 +00:00
|
|
|
import Utility.DBus
|
2012-07-19 17:01:41 +00:00
|
|
|
import DBus.Client
|
2012-07-20 03:34:33 +00:00
|
|
|
import DBus
|
2012-07-20 05:59:21 +00:00
|
|
|
import Data.Word (Word32)
|
2012-09-29 20:09:07 +00:00
|
|
|
import Control.Concurrent
|
|
|
|
import qualified Control.Exception as E
|
2012-07-19 17:01:41 +00:00
|
|
|
#else
|
|
|
|
#warning Building without dbus support; will use mtab polling
|
|
|
|
#endif
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
thisThread :: ThreadName
|
|
|
|
thisThread = "MountWatcher"
|
|
|
|
|
2012-10-24 17:35:43 +00:00
|
|
|
mountWatcherThread :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> PushNotifier -> NamedThread
|
2012-10-29 06:21:04 +00:00
|
|
|
mountWatcherThread st handle scanremotes pushnotifier = thread $ liftIO $
|
2012-07-19 17:01:41 +00:00
|
|
|
#if WITH_DBUS
|
2012-10-24 17:35:43 +00:00
|
|
|
dbusThread st handle scanremotes pushnotifier
|
2012-07-19 17:01:41 +00:00
|
|
|
#else
|
2012-10-24 17:35:43 +00:00
|
|
|
pollingThread st handle scanremotes pushnotifier
|
2012-07-19 17:01:41 +00:00
|
|
|
#endif
|
2012-09-06 18:56:04 +00:00
|
|
|
where
|
|
|
|
thread = NamedThread thisThread
|
2012-07-19 17:01:41 +00:00
|
|
|
|
|
|
|
#if WITH_DBUS
|
2012-07-20 03:34:33 +00:00
|
|
|
|
2012-10-24 17:35:43 +00:00
|
|
|
dbusThread :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> PushNotifier -> IO ()
|
2012-10-26 04:10:41 +00:00
|
|
|
dbusThread st dstatus scanremotes pushnotifier =
|
|
|
|
E.catch (runClient getSessionAddress go) onerr
|
2012-07-19 17:01:41 +00:00
|
|
|
where
|
2012-07-20 03:34:33 +00:00
|
|
|
go client = ifM (checkMountMonitor client)
|
|
|
|
( do
|
|
|
|
{- Store the current mount points in an mvar,
|
|
|
|
- to be compared later. We could in theory
|
|
|
|
- work out the mount point from the dbus
|
|
|
|
- message, but this is easier. -}
|
|
|
|
mvar <- newMVar =<< currentMountPoints
|
2012-08-23 22:58:54 +00:00
|
|
|
forM_ mountChanged $ \matcher ->
|
2012-07-20 22:14:57 +00:00
|
|
|
listen client matcher $ \_event -> do
|
|
|
|
nowmounted <- currentMountPoints
|
|
|
|
wasmounted <- swapMVar mvar nowmounted
|
2012-10-24 17:35:43 +00:00
|
|
|
handleMounts st dstatus scanremotes pushnotifier wasmounted nowmounted
|
2012-07-20 03:34:33 +00:00
|
|
|
, do
|
|
|
|
runThreadState st $
|
|
|
|
warning "No known volume monitor available through dbus; falling back to mtab polling"
|
|
|
|
pollinstead
|
|
|
|
)
|
2012-07-20 06:16:09 +00:00
|
|
|
onerr :: E.SomeException -> IO ()
|
2012-07-20 03:34:33 +00:00
|
|
|
onerr e = do
|
2012-10-26 23:38:27 +00:00
|
|
|
{- If the session dbus fails, the user probably
|
|
|
|
- logged out of their desktop. Even if they log
|
|
|
|
- back in, we won't have access to the dbus
|
|
|
|
- session key, so polling is the best that can be
|
|
|
|
- done in this situation. -}
|
2012-07-20 03:34:33 +00:00
|
|
|
runThreadState st $
|
2012-10-26 04:02:03 +00:00
|
|
|
warning $ "dbus failed; falling back to mtab polling (" ++ show e ++ ")"
|
2012-07-20 03:34:33 +00:00
|
|
|
pollinstead
|
2012-10-24 17:35:43 +00:00
|
|
|
pollinstead = pollingThread st dstatus scanremotes pushnotifier
|
2012-07-20 03:34:33 +00:00
|
|
|
|
2012-07-20 22:14:57 +00:00
|
|
|
{- Examine the list of services connected to dbus, to see if there
|
2012-07-20 05:59:21 +00:00
|
|
|
- are any we can use to monitor mounts. If not, will attempt to start one. -}
|
2012-07-20 03:34:33 +00:00
|
|
|
checkMountMonitor :: Client -> IO Bool
|
2012-07-20 22:14:57 +00:00
|
|
|
checkMountMonitor client = do
|
|
|
|
running <- filter (`elem` usableservices)
|
|
|
|
<$> listServiceNames client
|
2012-08-05 19:08:58 +00:00
|
|
|
case running of
|
|
|
|
[] -> startOneService client startableservices
|
|
|
|
(service:_) -> do
|
2012-10-29 06:21:04 +00:00
|
|
|
brokendebug thisThread [ "Using running DBUS service"
|
2012-08-05 19:08:58 +00:00
|
|
|
, service
|
2012-07-20 22:14:57 +00:00
|
|
|
, "to monitor mount events."
|
|
|
|
]
|
|
|
|
return True
|
2012-07-20 03:34:33 +00:00
|
|
|
where
|
2012-07-20 22:14:57 +00:00
|
|
|
startableservices = [gvfs]
|
|
|
|
usableservices = startableservices ++ [kde]
|
|
|
|
gvfs = "org.gtk.Private.GduVolumeMonitor"
|
|
|
|
kde = "org.kde.DeviceNotifications"
|
|
|
|
|
|
|
|
startOneService :: Client -> [ServiceName] -> IO Bool
|
|
|
|
startOneService _ [] = return False
|
|
|
|
startOneService client (x:xs) = do
|
|
|
|
_ <- callDBus client "StartServiceByName"
|
|
|
|
[toVariant x, toVariant (0 :: Word32)]
|
|
|
|
ifM (elem x <$> listServiceNames client)
|
|
|
|
( do
|
2012-10-29 06:21:04 +00:00
|
|
|
brokendebug thisThread [ "Started DBUS service"
|
2012-07-20 22:14:57 +00:00
|
|
|
, x
|
|
|
|
, "to monitor mount events."
|
|
|
|
]
|
|
|
|
return True
|
|
|
|
, startOneService client xs
|
|
|
|
)
|
2012-07-20 03:34:33 +00:00
|
|
|
|
2012-08-23 22:58:54 +00:00
|
|
|
{- Filter matching events recieved when drives are mounted and unmounted. -}
|
|
|
|
mountChanged :: [MatchRule]
|
|
|
|
mountChanged = [gvfs True, gvfs False, kde, kdefallback]
|
2012-07-20 22:14:57 +00:00
|
|
|
where
|
2012-08-23 22:58:54 +00:00
|
|
|
{- gvfs reliably generates this event whenever a drive is mounted/unmounted,
|
|
|
|
- whether automatically, or manually -}
|
|
|
|
gvfs mount = matchAny
|
2012-07-20 22:14:57 +00:00
|
|
|
{ matchInterface = Just "org.gtk.Private.RemoteVolumeMonitor"
|
2012-08-23 22:58:54 +00:00
|
|
|
, matchMember = Just $ if mount then "MountAdded" else "MountRemoved"
|
2012-07-20 22:14:57 +00:00
|
|
|
}
|
2012-08-23 22:58:54 +00:00
|
|
|
{- This event fires when KDE prompts the user what to do with a drive,
|
|
|
|
- but maybe not at other times. And it's not received -}
|
2012-07-20 22:14:57 +00:00
|
|
|
kde = matchAny
|
|
|
|
{ matchInterface = Just "org.kde.Solid.Device"
|
|
|
|
, matchMember = Just "setupDone"
|
|
|
|
}
|
2012-08-23 22:58:54 +00:00
|
|
|
{- This event may not be closely related to mounting a drive, but it's
|
|
|
|
- observed reliably when a drive gets mounted or unmounted. -}
|
|
|
|
kdefallback = matchAny
|
|
|
|
{ matchInterface = Just "org.kde.KDirNotify"
|
|
|
|
, matchMember = Just "enteredDirectory"
|
|
|
|
}
|
2012-07-19 17:01:41 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 17:35:43 +00:00
|
|
|
pollingThread :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> PushNotifier -> IO ()
|
|
|
|
pollingThread st dstatus scanremotes pushnotifier = go =<< currentMountPoints
|
2012-07-19 17:01:41 +00:00
|
|
|
where
|
|
|
|
go wasmounted = do
|
|
|
|
threadDelaySeconds (Seconds 10)
|
|
|
|
nowmounted <- currentMountPoints
|
2012-10-24 17:35:43 +00:00
|
|
|
handleMounts st dstatus scanremotes pushnotifier wasmounted nowmounted
|
2012-07-19 17:01:41 +00:00
|
|
|
go nowmounted
|
|
|
|
|
2012-10-24 17:35:43 +00:00
|
|
|
handleMounts :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> PushNotifier -> MountPoints -> MountPoints -> IO ()
|
|
|
|
handleMounts st dstatus scanremotes pushnotifier wasmounted nowmounted =
|
|
|
|
mapM_ (handleMount st dstatus scanremotes pushnotifier . mnt_dir) $
|
2012-08-05 01:18:57 +00:00
|
|
|
S.toList $ newMountPoints wasmounted nowmounted
|
2012-07-19 17:01:41 +00:00
|
|
|
|
2012-10-24 17:35:43 +00:00
|
|
|
handleMount :: ThreadState -> DaemonStatusHandle -> ScanRemoteMap -> PushNotifier -> FilePath -> IO ()
|
|
|
|
handleMount st dstatus scanremotes pushnotifier dir = do
|
2012-10-29 06:21:04 +00:00
|
|
|
brokendebug thisThread ["detected mount of", dir]
|
2012-10-24 17:35:43 +00:00
|
|
|
reconnectRemotes thisThread st dstatus scanremotes (Just pushnotifier)
|
2012-08-22 18:32:17 +00:00
|
|
|
=<< filter (Git.repoIsLocal . Remote.repo)
|
|
|
|
<$> remotesUnder st dstatus dir
|
2012-07-22 19:06:18 +00:00
|
|
|
|
|
|
|
{- Finds remotes located underneath the mount point.
|
|
|
|
-
|
|
|
|
- Updates state to include the remotes.
|
|
|
|
-
|
|
|
|
- The config of git remotes is re-read, as it may not have been available
|
|
|
|
- at startup time, or may have changed (it could even be a different
|
|
|
|
- repository at the same remote location..)
|
|
|
|
-}
|
2012-08-05 01:18:57 +00:00
|
|
|
remotesUnder :: ThreadState -> DaemonStatusHandle -> FilePath -> IO [Remote]
|
|
|
|
remotesUnder st dstatus dir = runThreadState st $ do
|
2012-07-22 19:06:18 +00:00
|
|
|
repotop <- fromRepo Git.repoPath
|
|
|
|
rs <- remoteList
|
|
|
|
pairs <- mapM (checkremote repotop) rs
|
|
|
|
let (waschanged, rs') = unzip pairs
|
|
|
|
when (any id waschanged) $ do
|
|
|
|
Annex.changeState $ \s -> s { Annex.remotes = rs' }
|
2012-10-14 18:47:01 +00:00
|
|
|
updateSyncRemotes dstatus
|
2012-07-22 19:06:18 +00:00
|
|
|
return $ map snd $ filter fst pairs
|
|
|
|
where
|
2012-08-26 18:26:43 +00:00
|
|
|
checkremote repotop r = case Remote.localpath r of
|
2012-08-05 01:18:57 +00:00
|
|
|
Just p | dirContains dir (absPathFrom repotop p) ->
|
2012-08-05 18:49:47 +00:00
|
|
|
(,) <$> pure True <*> updateRemote r
|
2012-07-22 19:06:18 +00:00
|
|
|
_ -> return (False, r)
|
2012-07-19 17:01:41 +00:00
|
|
|
|
2012-07-20 01:25:26 +00:00
|
|
|
type MountPoints = S.Set Mntent
|
2012-07-19 17:01:41 +00:00
|
|
|
|
|
|
|
currentMountPoints :: IO MountPoints
|
2012-07-20 01:25:26 +00:00
|
|
|
currentMountPoints = S.fromList <$> getMounts
|
2012-07-19 17:01:41 +00:00
|
|
|
|
|
|
|
newMountPoints :: MountPoints -> MountPoints -> MountPoints
|
|
|
|
newMountPoints old new = S.difference new old
|