2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
UNIX Filesystem Driver
|
|
|
|
-}
|
2019-09-10 23:14:43 +03:00
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
module Urbit.Vere.Clay
|
|
|
|
( clay
|
|
|
|
, clay'
|
|
|
|
)
|
|
|
|
where
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-11-03 22:03:58 +03:00
|
|
|
import Urbit.Arvo
|
2020-06-10 22:22:45 +03:00
|
|
|
import Urbit.King.App
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude
|
|
|
|
import Urbit.Vere.Pier.Types
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
import Conduit
|
|
|
|
import RIO.Directory
|
|
|
|
import RIO.FilePath
|
|
|
|
|
|
|
|
import qualified Data.Conduit.Combinators as CC
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-09-10 23:14:43 +03:00
|
|
|
data ClayDrv = ClayDrv
|
2019-09-13 03:08:04 +03:00
|
|
|
{ cdMountPoints :: TVar (Map Desk (Map FilePath Int))
|
2019-09-10 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
deskToPath :: Desk -> FilePath
|
|
|
|
deskToPath (Desk (Cord t)) = unpack t
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
-- | The hard coded mime type of every file.
|
2020-11-03 22:03:58 +03:00
|
|
|
textPlain :: Path
|
2019-09-10 23:14:43 +03:00
|
|
|
textPlain = Path [(MkKnot "text"), (MkKnot "plain")]
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
-- | Filter for dotfiles, tempfiles and backup files.
|
2019-09-10 23:14:43 +03:00
|
|
|
validClaySyncPath :: FilePath -> Bool
|
|
|
|
validClaySyncPath fp = hasPeriod && notTildeFile && notDotHash && notDoubleHash
|
|
|
|
where
|
|
|
|
fileName = takeFileName fp
|
|
|
|
hasPeriod = elem '.' fileName
|
|
|
|
notTildeFile = not $ "~" `isSuffixOf` fileName
|
|
|
|
notDotHash = not $ ".#" `isPrefixOf` fileName
|
|
|
|
notDoubleHash =
|
|
|
|
not $ ("#" `isPrefixOf` fileName) && ("#" `isSuffixOf` fileName)
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Returns a list of the result of running a function on each valid
|
|
|
|
file in the directory fp. Runnable in IO.
|
|
|
|
-}
|
2019-09-10 23:14:43 +03:00
|
|
|
foreachFileIn :: (MonadUnliftIO m)
|
|
|
|
=> FilePath -> (FilePath -> (ResourceT m) a) -> m [a]
|
|
|
|
foreachFileIn fp fun =
|
|
|
|
runConduitRes $ (sourceDirectoryDeep False fp)
|
|
|
|
.| filterC validClaySyncPath
|
|
|
|
.| CC.mapM fun
|
|
|
|
.| sinkList
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Note: Vere just reuses +mug, but since the actual hash function is
|
|
|
|
an implementation detail which doesn't leave the io driver, we just
|
|
|
|
use the standard hash.
|
|
|
|
-}
|
2019-09-10 23:14:43 +03:00
|
|
|
getHashOfFile :: (MonadIO m) => FilePath -> m (FilePath, Int)
|
|
|
|
getHashOfFile fp = do
|
|
|
|
bs <- readFile fp
|
|
|
|
let !h = hash bs
|
|
|
|
pure (fp, h)
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Takes an initial snapshot of the filesystem, recording what files exist and
|
|
|
|
what their hashes are.
|
|
|
|
-}
|
2019-09-13 03:08:04 +03:00
|
|
|
takeFilesystemSnapshot :: FilePath -> RIO e (Map FilePath Int)
|
2019-09-10 23:14:43 +03:00
|
|
|
takeFilesystemSnapshot fp = do
|
|
|
|
exists <- doesDirectoryExist fp
|
|
|
|
if not exists then
|
|
|
|
pure M.empty
|
|
|
|
else
|
|
|
|
M.fromList <$> foreachFileIn fp getHashOfFile
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Check an existing filepath against a snapshot of files that existed on disk
|
|
|
|
the last time we checked. Returns Either (unchanged) (new file data).
|
|
|
|
-}
|
2019-09-10 23:14:43 +03:00
|
|
|
checkFileForUpdates :: (MonadIO m)
|
2019-09-13 03:08:04 +03:00
|
|
|
=> Map FilePath Int -> FilePath
|
2019-09-10 23:14:43 +03:00
|
|
|
-> m (Either FilePath (FilePath, Mime, Int))
|
|
|
|
checkFileForUpdates snapshot fp = do
|
|
|
|
bs <- readFile fp
|
|
|
|
let !newHash = hash bs
|
2019-09-13 03:08:04 +03:00
|
|
|
pure $ case lookup fp snapshot of
|
2019-09-10 23:14:43 +03:00
|
|
|
-- text/plain is the hardcoded mime type of every file sent to clay.
|
|
|
|
Nothing -> Right (fp, (Mime textPlain (File (Octs bs))), newHash)
|
|
|
|
Just i -> if i == newHash then Left fp
|
|
|
|
else Right (fp, (Mime textPlain (File (Octs bs))), newHash)
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Given a previous snapshot of the filesystem, produces a list of changes
|
|
|
|
-}
|
2019-09-13 03:08:04 +03:00
|
|
|
buildActionListFromDifferences :: FilePath -> Map FilePath Int
|
2019-09-10 23:14:43 +03:00
|
|
|
-> RIO e [(FilePath, Maybe (Mime, Int))]
|
|
|
|
buildActionListFromDifferences fp snapshot = do
|
|
|
|
checks <- foreachFileIn fp (checkFileForUpdates snapshot)
|
|
|
|
|
2019-09-13 03:08:04 +03:00
|
|
|
let changedItems = rights checks <&> \(fp, m, i) -> (fp, Just (m, i))
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
let existsSet = S.fromList $ flip map checks $ \case
|
|
|
|
Left fp -> fp
|
|
|
|
Right (fp, _, _) -> fp
|
|
|
|
let deletedSet = S.difference (M.keysSet snapshot) existsSet
|
2019-09-13 03:08:04 +03:00
|
|
|
let deletedItems = (toList deletedSet) <&> \x -> (x, Nothing)
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
pure $ sort (deletedItems ++ changedItems)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
_boatFailed :: e -> WorkError -> IO ()
|
|
|
|
_boatFailed env _ = runRIO env $ do
|
2020-06-02 23:48:07 +03:00
|
|
|
pure () -- TODO What can we do?
|
|
|
|
|
2020-06-10 22:22:45 +03:00
|
|
|
clay'
|
|
|
|
:: HasPierEnv e
|
|
|
|
=> RIO e ([Ev], RAcquire e (DriverApi SyncEf))
|
|
|
|
clay' = do
|
|
|
|
ventQ :: TQueue EvErr <- newTQueueIO
|
|
|
|
env <- ask
|
|
|
|
|
|
|
|
let (bornEvs, startDriver) = clay env (writeTQueue ventQ)
|
|
|
|
|
|
|
|
let runDriver = do
|
|
|
|
diOnEffect <- startDriver
|
|
|
|
let diEventSource = fmap RRWork <$> tryReadTQueue ventQ
|
|
|
|
pure (DriverApi {..})
|
|
|
|
|
|
|
|
pure (bornEvs, runDriver)
|
|
|
|
|
2020-06-02 23:48:07 +03:00
|
|
|
clay
|
|
|
|
:: forall e
|
|
|
|
. (HasPierConfig e, HasLogFunc e, HasKingId e)
|
|
|
|
=> e
|
|
|
|
-> (EvErr -> STM ())
|
2020-06-10 22:22:45 +03:00
|
|
|
-> ([Ev], RAcquire e (SyncEf -> IO ()))
|
2020-06-02 23:48:07 +03:00
|
|
|
clay env plan =
|
2019-09-10 23:14:43 +03:00
|
|
|
(initialEvents, runSync)
|
|
|
|
where
|
2020-05-13 22:35:57 +03:00
|
|
|
king = fromIntegral (env ^. kingIdL)
|
|
|
|
|
2020-06-02 23:48:07 +03:00
|
|
|
boatEv = EvBlip $ BlipEvBoat $ BoatEvBoat () ()
|
|
|
|
|
|
|
|
-- TODO: In the case of -A, we need to read all the data from the
|
|
|
|
-- specified directory and shove it into an %into event.
|
2020-06-10 22:22:45 +03:00
|
|
|
initialEvents = [boatEv]
|
2019-09-10 23:14:43 +03:00
|
|
|
|
2020-06-07 02:34:27 +03:00
|
|
|
runSync :: RAcquire e (SyncEf -> IO ())
|
2019-09-13 23:06:13 +03:00
|
|
|
runSync = handleEffect <$> mkRAcquire start stop
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
start :: RIO e ClayDrv
|
2019-09-13 03:08:04 +03:00
|
|
|
start = ClayDrv <$> newTVarIO mempty
|
2019-09-10 23:14:43 +03:00
|
|
|
stop c = pure ()
|
|
|
|
|
2020-06-07 02:34:27 +03:00
|
|
|
handleEffect :: ClayDrv -> SyncEf -> IO ()
|
|
|
|
handleEffect cd = runRIO env . \case
|
2019-09-10 23:14:43 +03:00
|
|
|
SyncEfHill _ mountPoints -> do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) known mount points:", mountPoints)
|
2019-12-17 17:31:50 +03:00
|
|
|
pierPath <- view pierPathL
|
2019-09-10 23:14:43 +03:00
|
|
|
mountPairs <- flip mapM mountPoints $ \desk -> do
|
|
|
|
ss <- takeFilesystemSnapshot (pierPath </> (deskToPath desk))
|
|
|
|
pure (desk, ss)
|
|
|
|
atomically $ writeTVar (cdMountPoints cd) (M.fromList mountPairs)
|
|
|
|
|
|
|
|
SyncEfDirk p desk -> do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) dirk:", p, desk)
|
2019-09-10 23:14:43 +03:00
|
|
|
m <- atomically $ readTVar (cdMountPoints cd)
|
|
|
|
let snapshot = M.findWithDefault M.empty desk m
|
2019-12-17 17:31:50 +03:00
|
|
|
pierPath <- view pierPathL
|
2019-09-10 23:14:43 +03:00
|
|
|
let dir = pierPath </> deskToPath desk
|
|
|
|
actions <- buildActionListFromDifferences dir snapshot
|
|
|
|
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) dirk actions: ", actions)
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
let !intoList = map (actionsToInto dir) actions
|
2020-06-02 23:48:07 +03:00
|
|
|
|
|
|
|
let syncEv = EvBlip
|
|
|
|
$ BlipEvSync
|
|
|
|
$ SyncEvInto (Some (king, ())) desk False intoList
|
|
|
|
|
|
|
|
let syncFailed _ = pure ()
|
|
|
|
|
|
|
|
atomically $ plan (EvErr syncEv syncFailed)
|
|
|
|
|
2019-09-10 23:14:43 +03:00
|
|
|
|
2020-09-23 17:51:59 +03:00
|
|
|
atomically $ modifyTVar'
|
2019-09-10 23:14:43 +03:00
|
|
|
(cdMountPoints cd)
|
|
|
|
(applyActionsToMountPoints desk actions)
|
|
|
|
|
|
|
|
SyncEfErgo p desk actions -> do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) ergo:", p, desk, actions)
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
m <- atomically $ readTVar (cdMountPoints cd)
|
|
|
|
let mountPoint = M.findWithDefault M.empty desk m
|
|
|
|
|
2019-12-17 17:31:50 +03:00
|
|
|
pierPath <- view pierPathL
|
2019-09-10 23:14:43 +03:00
|
|
|
let dir = pierPath </> deskToPath desk
|
|
|
|
let hashedActions = map (calculateActionHash dir) actions
|
|
|
|
for_ hashedActions (performAction mountPoint)
|
|
|
|
|
2020-09-23 17:51:59 +03:00
|
|
|
atomically $ modifyTVar'
|
2019-09-10 23:14:43 +03:00
|
|
|
(cdMountPoints cd)
|
|
|
|
(applyActionsToMountPoints desk hashedActions)
|
|
|
|
|
|
|
|
SyncEfOgre p desk -> do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) ogre:", p, desk)
|
2019-12-17 17:31:50 +03:00
|
|
|
pierPath <- view pierPathL
|
2019-09-10 23:14:43 +03:00
|
|
|
removeDirectoryRecursive $ pierPath </> deskToPath desk
|
2020-09-23 17:51:59 +03:00
|
|
|
atomically $ modifyTVar' (cdMountPoints cd) (M.delete desk)
|
2019-09-10 23:14:43 +03:00
|
|
|
|
|
|
|
|
2019-09-13 21:02:41 +03:00
|
|
|
-- Change the structures off of the event into something we can work with
|
|
|
|
-- in Unix.
|
|
|
|
calculateActionHash :: FilePath -> (Path, Maybe Mime)
|
|
|
|
-> (FilePath, Maybe (Mime, Int))
|
2019-09-10 23:14:43 +03:00
|
|
|
calculateActionHash base (p, Nothing) = (base </> pathToFilePath p, Nothing)
|
|
|
|
calculateActionHash base (p, Just (Mime t f)) =
|
|
|
|
(base </> pathToFilePath p, Just ((Mime t f), (hash $ unOcts $ unFile f)))
|
|
|
|
|
|
|
|
-- Performs the actions on the actual filesystem
|
2019-09-13 21:02:41 +03:00
|
|
|
performAction :: (Map FilePath Int) -> (FilePath, Maybe (Mime, Int))
|
|
|
|
-> RIO e ()
|
2019-09-10 23:14:43 +03:00
|
|
|
performAction m (fp, Nothing) = do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) deleting file ", fp)
|
2019-09-10 23:14:43 +03:00
|
|
|
removeFile fp
|
|
|
|
performAction m (fp, Just ((Mime _ (File (Octs bs)), hash)))
|
2020-09-10 04:20:21 +03:00
|
|
|
| skip = logInfo $
|
2019-09-13 21:02:41 +03:00
|
|
|
displayShow ("(clay) skipping unchanged file update " , fp)
|
2019-09-10 23:14:43 +03:00
|
|
|
| otherwise = do
|
2020-09-10 04:20:21 +03:00
|
|
|
logInfo $ displayShow ("(clay) updating file " , fp)
|
2019-09-10 23:14:43 +03:00
|
|
|
createDirectoryIfMissing True $ takeDirectory fp
|
|
|
|
writeFile fp bs
|
|
|
|
where
|
|
|
|
skip = case M.lookup fp m of
|
|
|
|
Nothing -> False
|
|
|
|
Just i -> i == hash
|
|
|
|
|
|
|
|
-- Apply the actions to our internal snapshots
|
|
|
|
applyActionsToMountPoints :: Desk
|
|
|
|
-> [(FilePath, Maybe (Mime, Int))]
|
2019-09-13 03:08:04 +03:00
|
|
|
-> (Map Desk (Map FilePath Int))
|
|
|
|
-> (Map Desk (Map FilePath Int))
|
2019-09-10 23:14:43 +03:00
|
|
|
applyActionsToMountPoints desk actions m = M.alter change desk m
|
|
|
|
where
|
|
|
|
change (Just fileMap) = Just (foldl' applySyncAction fileMap actions)
|
|
|
|
change Nothing = change (Just M.empty)
|
|
|
|
|
|
|
|
-- Applies the sync mutations specified.
|
2019-09-13 03:08:04 +03:00
|
|
|
applySyncAction :: (Map FilePath Int)
|
2019-09-10 23:14:43 +03:00
|
|
|
-> (FilePath, Maybe (Mime, Int))
|
2019-09-13 03:08:04 +03:00
|
|
|
-> (Map FilePath Int)
|
2019-09-10 23:14:43 +03:00
|
|
|
applySyncAction m (fp, Nothing) = M.delete fp m
|
|
|
|
applySyncAction m (fp, (Just (_, h))) = M.insert fp h m
|
|
|
|
|
|
|
|
-- Changes an action list item into a form injectable into Urbit
|
2019-09-13 21:02:41 +03:00
|
|
|
actionsToInto :: FilePath -> (FilePath, Maybe (Mime, Int))
|
|
|
|
-> (Path, Maybe Mime)
|
2019-09-10 23:14:43 +03:00
|
|
|
actionsToInto prefix (fp, mybData) = (p, mybOutData)
|
|
|
|
where
|
|
|
|
p = filePathToPath strippedFp
|
|
|
|
strippedFp = case stripPrefix prefix fp of
|
|
|
|
Nothing -> error "Impossible missing prefix"
|
|
|
|
Just x -> x
|
|
|
|
mybOutData = case mybData of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just (m, i) -> Just m
|