git-annex/Assistant/Threads/MountWatcher.hs

196 lines
5.9 KiB
Haskell
Raw Normal View History

{- 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
import Assistant.Common
import Assistant.DaemonStatus
2012-08-22 18:32:17 +00:00
import Assistant.Sync
2012-07-22 19:06:18 +00:00
import qualified Annex
import qualified Git
import Utility.ThreadScheduler
import Utility.Mounts
2012-07-22 19:06:18 +00:00
import Remote.List
import qualified Types.Remote as Remote
import qualified Data.Set as S
#if WITH_DBUS
import Utility.DBus
import DBus.Client
import DBus
import Data.Word (Word32)
import Control.Concurrent
import qualified Control.Exception as E
#else
#warning Building without dbus support; will use mtab polling
#endif
thisThread :: ThreadName
thisThread = "MountWatcher"
mountWatcherThread :: NamedThread
mountWatcherThread = NamedThread "MountWatcher" $
#if WITH_DBUS
dbusThread
#else
pollingThread
#endif
#if WITH_DBUS
dbusThread :: Assistant ()
dbusThread = do
runclient <- asIO1 go
r <- liftIO $ E.try $ runClient getSessionAddress runclient
either onerr (const noop) r
where
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 <- liftIO $ newMVar =<< currentMountPoints
handleevent <- asIO1 $ \_event -> do
nowmounted <- liftIO $ currentMountPoints
wasmounted <- liftIO $ swapMVar mvar nowmounted
handleMounts wasmounted nowmounted
liftIO $ forM_ mountChanged $ \matcher ->
listen client matcher handleevent
, do
liftAnnex $
warning "No known volume monitor available through dbus; falling back to mtab polling"
pollingThread
)
onerr :: E.SomeException -> Assistant ()
onerr e = do
{- 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. -}
liftAnnex $
warning $ "dbus failed; falling back to mtab polling (" ++ show e ++ ")"
pollingThread
{- Examine the list of services connected to dbus, to see if there
- are any we can use to monitor mounts. If not, will attempt to start one. -}
checkMountMonitor :: Client -> Assistant Bool
checkMountMonitor client = do
running <- filter (`elem` usableservices)
<$> liftIO (listServiceNames client)
2012-08-05 19:08:58 +00:00
case running of
2012-10-29 18:30:10 +00:00
[] -> startOneService client startableservices
2012-08-05 19:08:58 +00:00
(service:_) -> do
debug [ "Using running DBUS service"
2012-08-05 19:08:58 +00:00
, service
, "to monitor mount events."
]
return True
where
startableservices = [gvfs, gvfsgdu]
usableservices = startableservices ++ [kde]
2013-01-10 20:08:40 +00:00
gvfs = "org.gtk.Private.UDisks2VolumeMonitor"
gvfsgdu = "org.gtk.Private.GduVolumeMonitor"
kde = "org.kde.DeviceNotifications"
2012-10-29 18:30:10 +00:00
startOneService :: Client -> [ServiceName] -> Assistant Bool
startOneService _ [] = return False
startOneService client (x:xs) = do
2013-01-14 17:22:19 +00:00
_ <- liftIO $ tryNonAsync $ callDBus client "StartServiceByName"
[toVariant x, toVariant (0 :: Word32)]
2012-10-29 18:30:10 +00:00
ifM (liftIO $ elem x <$> listServiceNames client)
( do
2012-10-29 18:30:10 +00:00
debug
[ "Started DBUS service", x
, "to monitor mount events."
]
return True
, startOneService client xs
)
2012-08-23 22:58:54 +00:00
{- Filter matching events recieved when drives are mounted and unmounted. -}
mountChanged :: [MatchRule]
2013-01-10 20:43:22 +00:00
mountChanged = [gvfs True, gvfs False, kde, kdefallback]
2012-10-31 06:34:03 +00:00
where
2013-01-10 20:18:26 +00:00
{- gvfs reliably generates this event whenever a
2012-10-31 06:34:03 +00:00
- drive is mounted/unmounted, whether automatically, or manually -}
2013-01-10 20:43:22 +00:00
gvfs mount = matchAny
2013-01-10 20:18:26 +00:00
{ matchInterface = Just "org.gtk.Private.RemoteVolumeMonitor"
2012-10-31 06:34:03 +00:00
, matchMember = Just $ if mount then "MountAdded" else "MountRemoved"
}
{- 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 -}
kde = matchAny
{ matchInterface = Just "org.kde.Solid.Device"
, matchMember = Just "setupDone"
}
{- 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"
}
#endif
pollingThread :: Assistant ()
pollingThread = go =<< liftIO currentMountPoints
2012-10-31 06:34:03 +00:00
where
go wasmounted = do
liftIO $ threadDelaySeconds (Seconds 10)
nowmounted <- liftIO currentMountPoints
handleMounts wasmounted nowmounted
go nowmounted
handleMounts :: MountPoints -> MountPoints -> Assistant ()
handleMounts wasmounted nowmounted =
mapM_ (handleMount . mnt_dir) $
S.toList $ newMountPoints wasmounted nowmounted
handleMount :: FilePath -> Assistant ()
handleMount dir = do
debug ["detected mount of", dir]
rs <- filter (Git.repoIsLocal . Remote.repo) <$> remotesUnder dir
reconnectRemotes True rs
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..)
-}
remotesUnder :: FilePath -> Assistant [Remote]
remotesUnder dir = do
repotop <- liftAnnex $ fromRepo Git.repoPath
rs <- liftAnnex remoteList
pairs <- liftAnnex $ mapM (checkremote repotop) rs
2012-07-22 19:06:18 +00:00
let (waschanged, rs') = unzip pairs
when (any id waschanged) $ do
liftAnnex $ Annex.changeState $ \s -> s { Annex.remotes = rs' }
updateSyncRemotes
2012-07-22 19:06:18 +00:00
return $ map snd $ filter fst pairs
2012-10-31 06:34:03 +00:00
where
checkremote repotop r = case Remote.localpath r of
Just p | dirContains dir (absPathFrom repotop p) ->
(,) <$> pure True <*> updateRemote r
_ -> return (False, r)
type MountPoints = S.Set Mntent
currentMountPoints :: IO MountPoints
currentMountPoints = S.fromList <$> getMounts
newMountPoints :: MountPoints -> MountPoints -> MountPoints
newMountPoints old new = S.difference new old