2012-08-31 19:17:12 +00:00
|
|
|
{- git-annex assistant webapp configurators for making local repositories
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2012-08-31 19:17:12 +00:00
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-08-31 19:17:12 +00:00
|
|
|
-}
|
|
|
|
|
2015-05-10 19:56:07 +00:00
|
|
|
{-# LANGUAGE CPP, QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2015-05-10 19:54:58 +00:00
|
|
|
{-# LANGUAGE RankNTypes, KindSignatures, TypeFamilies, FlexibleContexts #-}
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Local where
|
|
|
|
|
2012-11-25 04:26:46 +00:00
|
|
|
import Assistant.WebApp.Common
|
2013-09-18 00:02:42 +00:00
|
|
|
import Assistant.WebApp.Gpg
|
2013-10-28 15:33:14 +00:00
|
|
|
import Assistant.WebApp.MakeRemote
|
2013-04-08 19:36:09 +00:00
|
|
|
import Assistant.Sync
|
2013-11-23 19:50:17 +00:00
|
|
|
import Assistant.Restart
|
2019-08-26 16:32:45 +00:00
|
|
|
import Assistant.MakeRepo
|
2015-09-09 22:06:49 +00:00
|
|
|
import qualified Annex
|
2012-08-31 19:17:12 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Config
|
2012-10-30 23:12:05 +00:00
|
|
|
import qualified Git.Command
|
2020-11-04 18:20:37 +00:00
|
|
|
import Config.Files.AutoStart
|
2012-08-31 19:17:12 +00:00
|
|
|
import Utility.FreeDesktop
|
2013-12-10 05:26:52 +00:00
|
|
|
import Utility.DiskFree
|
2016-05-05 19:53:31 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-08-31 19:17:12 +00:00
|
|
|
import Utility.Mounts
|
2016-05-05 19:53:31 +00:00
|
|
|
#endif
|
2012-08-31 19:17:12 +00:00
|
|
|
import Utility.DataUnits
|
2013-04-03 21:01:40 +00:00
|
|
|
import Remote (prettyUUID)
|
2012-10-09 18:24:17 +00:00
|
|
|
import Annex.UUID
|
2018-10-19 19:17:48 +00:00
|
|
|
import Annex.CurrentBranch
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-10 19:35:10 +00:00
|
|
|
import Logs.PreferredContent
|
2013-03-20 20:46:35 +00:00
|
|
|
import Logs.UUID
|
2012-10-25 22:17:32 +00:00
|
|
|
import Utility.UserInfo
|
2013-01-01 18:01:47 +00:00
|
|
|
import Config
|
2013-09-16 20:07:27 +00:00
|
|
|
import Utility.Gpg
|
|
|
|
import qualified Remote.GCrypt as GCrypt
|
|
|
|
import qualified Types.Remote
|
2018-04-25 18:38:42 +00:00
|
|
|
import Utility.Android
|
2020-01-10 18:10:20 +00:00
|
|
|
import Types.ProposedAccepted
|
2023-03-01 19:55:58 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
2013-03-20 20:46:35 +00:00
|
|
|
import qualified Data.Map as M
|
2012-08-31 19:17:12 +00:00
|
|
|
import Data.Char
|
2013-09-16 20:07:27 +00:00
|
|
|
import Data.Ord
|
2022-06-28 19:17:41 +00:00
|
|
|
import Data.Kind
|
2013-06-27 05:15:28 +00:00
|
|
|
import qualified Text.Hamlet as Hamlet
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
data RepositoryPath = RepositoryPath Text
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
{- Custom field display for a RepositoryPath, with an icon etc.
|
|
|
|
-
|
|
|
|
- Validates that the path entered is not empty, and is a safe value
|
|
|
|
- to use as a repository. -}
|
2022-06-28 19:17:41 +00:00
|
|
|
repositoryPathField :: forall (m :: Type -> Type). (MonadIO m, HandlerSite m ~ WebApp) => Bool -> Field m Text
|
2012-11-18 17:21:19 +00:00
|
|
|
repositoryPathField autofocus = Field
|
|
|
|
{ fieldParse = \l _ -> parse l
|
|
|
|
, fieldEnctype = UrlEncoded
|
2013-03-10 19:43:10 +00:00
|
|
|
, fieldView = view
|
2012-11-18 17:21:19 +00:00
|
|
|
}
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
view idAttr nameAttr attrs val isReq =
|
|
|
|
[whamlet|<input type="text" *{attrs} id="#{idAttr}" name="#{nameAttr}" :isReq:required :autofocus:autofocus value="#{either id id val}">|]
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2012-10-31 06:34:03 +00:00
|
|
|
parse [path]
|
|
|
|
| T.null path = nopath
|
|
|
|
| otherwise = liftIO $ checkRepositoryPath path
|
|
|
|
parse [] = return $ Right Nothing
|
|
|
|
parse _ = nopath
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2012-10-31 06:34:03 +00:00
|
|
|
nopath = return $ Left "Enter a location for the repository"
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
{- As well as checking the path for a lot of silly things, tilde is
|
|
|
|
- expanded in the returned path. -}
|
|
|
|
checkRepositoryPath :: Text -> IO (Either (SomeMessage WebApp) (Maybe Text))
|
|
|
|
checkRepositoryPath p = do
|
|
|
|
home <- myHomeDir
|
|
|
|
let basepath = expandTilde home $ T.unpack p
|
2020-11-04 18:20:37 +00:00
|
|
|
path <- fromRawFilePath <$> absPath (toRawFilePath basepath)
|
|
|
|
let parent = fromRawFilePath $ parentDir (toRawFilePath path)
|
2012-08-31 19:17:12 +00:00
|
|
|
problems <- catMaybes <$> mapM runcheck
|
|
|
|
[ (return $ path == "/", "Enter the full path to use for the repository.")
|
|
|
|
, (return $ all isSpace basepath, "A blank path? Seems unlikely.")
|
|
|
|
, (doesFileExist path, "A file already exists with that name.")
|
|
|
|
, (return $ path == home, "Sorry, using git-annex for your whole home directory is not currently supported.")
|
|
|
|
, (not <$> doesDirectoryExist parent, "Parent directory does not exist.")
|
|
|
|
, (not <$> canWrite path, "Cannot write a repository there.")
|
|
|
|
]
|
|
|
|
return $
|
|
|
|
case headMaybe problems of
|
|
|
|
Nothing -> Right $ Just $ T.pack basepath
|
|
|
|
Just prob -> Left prob
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-10-02 04:42:27 +00:00
|
|
|
runcheck (chk, msg) = ifM chk ( return $ Just msg, return Nothing )
|
2012-10-31 06:34:03 +00:00
|
|
|
expandTilde home ('~':'/':path) = home </> path
|
|
|
|
expandTilde _ path = path
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
{- On first run, if run in the home directory, default to putting it in
|
|
|
|
- ~/Desktop/annex, when a Desktop directory exists, and ~/annex otherwise.
|
|
|
|
-
|
2018-04-25 18:17:52 +00:00
|
|
|
- When on Android, default to ~/storage/shared/annex, which termux sets up
|
|
|
|
- as a link to the sdcard.
|
|
|
|
-
|
2012-09-28 20:04:49 +00:00
|
|
|
- If run in another directory, that the user can write to,
|
2013-04-20 22:53:04 +00:00
|
|
|
- the user probably wants to put it there. Unless that directory
|
|
|
|
- contains a git-annex file, in which case the user has probably
|
|
|
|
- browsed to a directory with git-annex and run it from there. -}
|
2012-08-31 19:17:12 +00:00
|
|
|
defaultRepositoryPath :: Bool -> IO FilePath
|
|
|
|
defaultRepositoryPath firstrun = do
|
2013-12-09 20:39:23 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-02-25 18:09:39 +00:00
|
|
|
home <- myHomeDir
|
2014-06-10 23:20:14 +00:00
|
|
|
currdir <- liftIO getCurrentDirectory
|
|
|
|
if home == currdir && firstrun
|
2012-09-28 20:04:49 +00:00
|
|
|
then inhome
|
2014-06-10 23:20:14 +00:00
|
|
|
else ifM (legit currdir <&&> canWrite currdir)
|
|
|
|
( return currdir
|
2013-04-20 22:53:04 +00:00
|
|
|
, inhome
|
|
|
|
)
|
2013-12-09 20:39:23 +00:00
|
|
|
#else
|
2013-12-24 21:04:07 +00:00
|
|
|
-- On Windows, always default to ~/Desktop/annex or ~/annex,
|
|
|
|
-- no cwd handling because the user might be able to write
|
|
|
|
-- to the entire drive.
|
2014-02-25 18:09:39 +00:00
|
|
|
if firstrun then inhome else inhome
|
2013-12-09 20:39:23 +00:00
|
|
|
#endif
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2018-04-25 18:17:52 +00:00
|
|
|
inhome = ifM osAndroid
|
|
|
|
( do
|
|
|
|
home <- myHomeDir
|
|
|
|
let storageshared = home </> "storage" </> "shared"
|
|
|
|
ifM (doesDirectoryExist storageshared)
|
|
|
|
( relHome $ storageshared </> gitAnnexAssistantDefaultDir
|
|
|
|
, return $ "~" </> gitAnnexAssistantDefaultDir
|
|
|
|
)
|
|
|
|
, do
|
|
|
|
desktop <- userDesktopDir
|
|
|
|
ifM (doesDirectoryExist desktop <&&> canWrite desktop)
|
|
|
|
( relHome $ desktop </> gitAnnexAssistantDefaultDir
|
|
|
|
, return $ "~" </> gitAnnexAssistantDefaultDir
|
|
|
|
)
|
|
|
|
)
|
2014-02-25 18:09:39 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-12-24 21:04:07 +00:00
|
|
|
-- Avoid using eg, standalone build's git-annex.linux/ directory
|
|
|
|
-- when run from there.
|
2013-04-20 22:53:04 +00:00
|
|
|
legit d = not <$> doesFileExist (d </> "git-annex")
|
2014-02-25 18:09:39 +00:00
|
|
|
#endif
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
newRepositoryForm :: FilePath -> Hamlet.Html -> MkMForm RepositoryPath
|
2012-09-18 21:50:07 +00:00
|
|
|
newRepositoryForm defpath msg = do
|
2014-04-18 00:07:09 +00:00
|
|
|
(pathRes, pathView) <- mreq (repositoryPathField True) (bfs "")
|
2012-09-18 21:50:07 +00:00
|
|
|
(Just $ T.pack $ addTrailingPathSeparator defpath)
|
2012-08-31 19:17:12 +00:00
|
|
|
let (err, errmsg) = case pathRes of
|
|
|
|
FormMissing -> (False, "")
|
2013-10-02 04:42:27 +00:00
|
|
|
FormFailure l -> (True, concatMap T.unpack l)
|
2012-08-31 19:17:12 +00:00
|
|
|
FormSuccess _ -> (False, "")
|
|
|
|
let form = do
|
|
|
|
webAppFormAuthToken
|
2012-09-18 21:50:07 +00:00
|
|
|
$(widgetFile "configurators/newrepository/form")
|
2012-08-31 19:17:12 +00:00
|
|
|
return (RepositoryPath <$> pathRes, form)
|
|
|
|
|
|
|
|
{- Making the first repository, when starting the webapp for the first time. -}
|
2013-06-27 05:15:28 +00:00
|
|
|
getFirstRepositoryR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
getFirstRepositoryR = postFirstRepositoryR
|
2013-06-27 05:15:28 +00:00
|
|
|
postFirstRepositoryR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
postFirstRepositoryR = page "Getting started" (Just Configuration) $ do
|
2021-02-02 23:01:45 +00:00
|
|
|
unlessM (liftIO $ inSearchPath "git") $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup "You need to install git in order to use git-annex!"
|
2018-04-25 18:17:52 +00:00
|
|
|
androidspecial <- liftIO osAndroid
|
2013-06-03 17:51:54 +00:00
|
|
|
path <- liftIO . defaultRepositoryPath =<< liftH inFirstRun
|
2013-10-14 16:19:11 +00:00
|
|
|
((res, form), enctype) <- liftH $ runFormPostNoToken $ newRepositoryForm path
|
2012-08-31 19:17:12 +00:00
|
|
|
case res of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess (RepositoryPath p) -> liftH $
|
2013-06-10 20:41:02 +00:00
|
|
|
startFullAssistant (T.unpack p) ClientGroup Nothing
|
2012-09-18 21:50:07 +00:00
|
|
|
_ -> $(widgetFile "configurators/newrepository/first")
|
|
|
|
|
2013-05-03 19:21:31 +00:00
|
|
|
getAndroidCameraRepositoryR :: Handler ()
|
2018-04-25 18:17:52 +00:00
|
|
|
getAndroidCameraRepositoryR = do
|
|
|
|
home <- liftIO myHomeDir
|
|
|
|
let dcim = home </> "storage" </> "dcim"
|
|
|
|
startFullAssistant dcim SourceGroup $ Just addignore
|
2013-06-10 20:41:02 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
addignore = do
|
2013-06-10 20:41:02 +00:00
|
|
|
liftIO $ unlessM (doesFileExist ".gitignore") $
|
2013-11-16 22:48:16 +00:00
|
|
|
writeFile ".gitignore" ".thumbnails"
|
2013-06-10 20:41:02 +00:00
|
|
|
void $ inRepo $
|
|
|
|
Git.Command.runBool [Param "add", File ".gitignore"]
|
2013-05-03 19:21:31 +00:00
|
|
|
|
2013-02-19 00:37:26 +00:00
|
|
|
{- Adding a new local repository, which may be entirely separate, or may
|
|
|
|
- be connected to the current repository. -}
|
2013-06-27 05:15:28 +00:00
|
|
|
getNewRepositoryR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
getNewRepositoryR = postNewRepositoryR
|
2013-06-27 05:15:28 +00:00
|
|
|
postNewRepositoryR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
postNewRepositoryR = page "Add another repository" (Just Configuration) $ do
|
2012-09-18 21:50:07 +00:00
|
|
|
home <- liftIO myHomeDir
|
2013-10-14 16:19:11 +00:00
|
|
|
((res, form), enctype) <- liftH $ runFormPostNoToken $ newRepositoryForm home
|
2012-09-18 21:50:07 +00:00
|
|
|
case res of
|
2013-02-19 00:37:26 +00:00
|
|
|
FormSuccess (RepositoryPath p) -> do
|
2012-09-18 21:50:07 +00:00
|
|
|
let path = T.unpack p
|
2013-03-12 12:09:31 +00:00
|
|
|
isnew <- liftIO $ makeRepo path False
|
2014-05-30 00:12:17 +00:00
|
|
|
u <- liftIO $ initRepo isnew True path Nothing (Just ClientGroup)
|
2013-03-03 21:07:27 +00:00
|
|
|
liftIO $ addAutoStartFile path
|
2013-02-19 00:37:26 +00:00
|
|
|
liftIO $ startAssistant path
|
|
|
|
askcombine u path
|
2012-09-18 21:50:07 +00:00
|
|
|
_ -> $(widgetFile "configurators/newrepository")
|
2013-02-19 00:37:26 +00:00
|
|
|
where
|
|
|
|
askcombine newrepouuid newrepopath = do
|
|
|
|
newrepo <- liftIO $ relHome newrepopath
|
2013-06-03 17:51:54 +00:00
|
|
|
mainrepo <- fromJust . relDir <$> liftH getYesod
|
2013-02-19 00:37:26 +00:00
|
|
|
$(widgetFile "configurators/newrepository/combine")
|
|
|
|
|
2014-05-30 16:48:01 +00:00
|
|
|
{- Ensure that a remote's description, group, etc are available by
|
|
|
|
- immediately pulling from it. Also spawns a sync to push to it as well. -}
|
|
|
|
immediateSyncRemote :: Remote -> Assistant ()
|
|
|
|
immediateSyncRemote r = do
|
2018-10-19 19:17:48 +00:00
|
|
|
currentbranch <- liftAnnex $ getCurrentBranch
|
2014-05-30 16:48:01 +00:00
|
|
|
void $ manualPull currentbranch [r]
|
|
|
|
syncRemote r
|
|
|
|
|
2013-09-27 04:31:12 +00:00
|
|
|
getCombineRepositoryR :: FilePath -> UUID -> Handler Html
|
|
|
|
getCombineRepositoryR newrepopath newrepouuid = do
|
2014-05-30 16:48:01 +00:00
|
|
|
liftAssistant . immediateSyncRemote =<< combineRepos newrepopath remotename
|
2013-11-07 22:02:00 +00:00
|
|
|
redirect $ EditRepositoryR $ RepoUUID newrepouuid
|
2013-02-19 00:37:26 +00:00
|
|
|
where
|
|
|
|
remotename = takeFileName newrepopath
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
selectDriveForm :: [RemovableDrive] -> Hamlet.Html -> MkMForm RemovableDrive
|
2014-04-18 00:07:09 +00:00
|
|
|
selectDriveForm drives = renderBootstrap3 bootstrapFormLayout $ RemovableDrive
|
2012-08-31 19:17:12 +00:00
|
|
|
<$> pure Nothing
|
2014-04-18 00:07:09 +00:00
|
|
|
<*> areq (selectFieldList pairs `withNote` onlywritable) (bfs "Select drive:") Nothing
|
|
|
|
<*> areq textField (bfs "Use this directory on the drive:")
|
2013-03-20 20:46:35 +00:00
|
|
|
(Just $ T.pack gitAnnexAssistantDefaultDir)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
pairs = zip (map describe drives) (map mountPoint drives)
|
|
|
|
describe drive = case diskFree drive of
|
|
|
|
Nothing -> mountPoint drive
|
|
|
|
Just free ->
|
|
|
|
let sz = roughSize storageUnits True free
|
|
|
|
in T.unwords
|
|
|
|
[ mountPoint drive
|
|
|
|
, T.concat ["(", T.pack sz]
|
|
|
|
, "free)"
|
|
|
|
]
|
2013-12-12 18:25:50 +00:00
|
|
|
onlywritable = [whamlet|This list only includes drives you can write to.|]
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2013-03-20 20:46:35 +00:00
|
|
|
removableDriveRepository :: RemovableDrive -> FilePath
|
|
|
|
removableDriveRepository drive =
|
|
|
|
T.unpack (mountPoint drive) </> T.unpack (driveRepoPath drive)
|
|
|
|
|
|
|
|
{- Adding a removable drive. -}
|
2013-06-27 05:15:28 +00:00
|
|
|
getAddDriveR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
getAddDriveR = postAddDriveR
|
2013-06-27 05:15:28 +00:00
|
|
|
postAddDriveR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
postAddDriveR = page "Add a removable drive" (Just Configuration) $ do
|
2013-10-02 04:42:27 +00:00
|
|
|
removabledrives <- liftIO driveList
|
2012-08-31 19:17:12 +00:00
|
|
|
writabledrives <- liftIO $
|
|
|
|
filterM (canWrite . T.unpack . mountPoint) removabledrives
|
2013-10-14 16:19:11 +00:00
|
|
|
((res, form), enctype) <- liftH $ runFormPostNoToken $
|
2013-03-20 20:46:35 +00:00
|
|
|
selectDriveForm (sort writabledrives)
|
2012-08-31 19:17:12 +00:00
|
|
|
case res of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess drive -> liftH $ redirect $ ConfirmAddDriveR drive
|
2012-11-25 04:38:11 +00:00
|
|
|
_ -> $(widgetFile "configurators/adddrive")
|
2013-03-20 20:46:35 +00:00
|
|
|
|
|
|
|
{- The repo may already exist, when adding removable media
|
|
|
|
- that has already been used elsewhere. If so, check
|
|
|
|
- the UUID of the repo and see if it's one we know. If not,
|
2013-09-16 20:07:27 +00:00
|
|
|
- the user must confirm the repository merge.
|
|
|
|
-
|
|
|
|
- If the repo does not already exist on the drive, prompt about
|
|
|
|
- encryption. -}
|
2013-06-27 05:15:28 +00:00
|
|
|
getConfirmAddDriveR :: RemovableDrive -> Handler Html
|
2013-09-18 19:30:53 +00:00
|
|
|
getConfirmAddDriveR drive = ifM (liftIO $ probeRepoExists dir)
|
2013-09-16 20:07:27 +00:00
|
|
|
( do
|
2013-09-18 19:30:53 +00:00
|
|
|
mu <- liftIO $ probeUUID dir
|
2013-09-16 20:07:27 +00:00
|
|
|
case mu of
|
2013-09-18 19:30:53 +00:00
|
|
|
Nothing -> maybe askcombine isknownuuid
|
2013-09-27 20:21:56 +00:00
|
|
|
=<< liftAnnex (probeGCryptRemoteUUID dir)
|
2013-09-18 19:30:53 +00:00
|
|
|
Just driveuuid -> isknownuuid driveuuid
|
2013-09-16 20:07:27 +00:00
|
|
|
, newrepo
|
|
|
|
)
|
2013-03-20 20:46:35 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
dir = removableDriveRepository drive
|
|
|
|
newrepo = do
|
2015-09-09 22:06:49 +00:00
|
|
|
cmd <- liftAnnex $ gpgCmd <$> Annex.getGitConfig
|
2013-09-16 20:07:27 +00:00
|
|
|
secretkeys <- sortBy (comparing snd) . M.toList
|
2015-09-09 22:06:49 +00:00
|
|
|
<$> liftIO (secretKeys cmd)
|
2013-09-16 20:07:27 +00:00
|
|
|
page "Encrypt repository?" (Just Configuration) $
|
|
|
|
$(widgetFile "configurators/adddrive/encrypt")
|
2013-09-17 19:36:15 +00:00
|
|
|
knownrepo = getFinishAddDriveR drive NoRepoKey
|
2013-09-16 20:07:27 +00:00
|
|
|
askcombine = page "Combine repositories?" (Just Configuration) $
|
|
|
|
$(widgetFile "configurators/adddrive/combine")
|
2013-09-18 19:30:53 +00:00
|
|
|
isknownuuid driveuuid =
|
2019-01-01 19:39:45 +00:00
|
|
|
ifM (M.member driveuuid <$> liftAnnex uuidDescMap)
|
2013-09-18 19:30:53 +00:00
|
|
|
( knownrepo
|
|
|
|
, askcombine
|
|
|
|
)
|
2013-03-20 20:46:35 +00:00
|
|
|
|
2013-09-16 20:46:00 +00:00
|
|
|
setupDriveModal :: Widget
|
|
|
|
setupDriveModal = $(widgetFile "configurators/adddrive/setupmodal")
|
2013-03-20 20:46:35 +00:00
|
|
|
|
2013-09-17 19:36:15 +00:00
|
|
|
getGenKeyForDriveR :: RemovableDrive -> Handler Html
|
2013-10-02 04:42:27 +00:00
|
|
|
getGenKeyForDriveR drive = withNewSecretKey $ \keyid ->
|
2013-09-18 00:02:42 +00:00
|
|
|
{- Generating a key takes a long time, and
|
|
|
|
- the removable drive may have been disconnected
|
|
|
|
- in the meantime. Check that it is still mounted
|
|
|
|
- before finishing. -}
|
|
|
|
ifM (liftIO $ any (\d -> mountPoint d == mountPoint drive) <$> driveList)
|
2013-09-26 20:09:45 +00:00
|
|
|
( getFinishAddDriveR drive (RepoKey keyid)
|
2013-09-18 00:02:42 +00:00
|
|
|
, getAddDriveR
|
2013-09-17 19:36:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
getFinishAddDriveR :: RemovableDrive -> RepoKey -> Handler Html
|
|
|
|
getFinishAddDriveR drive = go
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-09-26 16:40:19 +00:00
|
|
|
go (RepoKey keyid) = whenGcryptInstalled $ makewith $ const $ do
|
2013-09-26 20:09:45 +00:00
|
|
|
r <- liftAnnex $ addRemote $
|
|
|
|
makeGCryptRemote remotename dir keyid
|
2013-09-26 16:40:19 +00:00
|
|
|
return (Types.Remote.uuid r, r)
|
2013-10-22 17:32:10 +00:00
|
|
|
go NoRepoKey = checkGCryptRepoEncryption dir makeunencrypted makeunencrypted $ do
|
|
|
|
mu <- liftAnnex $ probeGCryptRemoteUUID dir
|
|
|
|
case mu of
|
|
|
|
Just u -> enableexistinggcryptremote u
|
2016-11-16 01:29:54 +00:00
|
|
|
Nothing -> giveup "The drive contains a gcrypt repository that is not a git-annex special remote. This is not supported."
|
2013-09-27 20:21:56 +00:00
|
|
|
enableexistinggcryptremote u = do
|
|
|
|
remotename' <- liftAnnex $ getGCryptRemoteName u dir
|
|
|
|
makewith $ const $ do
|
|
|
|
r <- liftAnnex $ addRemote $
|
2014-02-11 18:06:50 +00:00
|
|
|
enableSpecialRemote remotename' GCrypt.remote Nothing $ M.fromList
|
2020-01-10 18:10:20 +00:00
|
|
|
[(Proposed "gitrepo", Proposed dir)]
|
2013-09-27 20:21:56 +00:00
|
|
|
return (u, r)
|
2013-09-18 19:30:53 +00:00
|
|
|
{- Making a new unencrypted repo, or combining with an existing one. -}
|
|
|
|
makeunencrypted = makewith $ \isnew -> (,)
|
2014-05-30 00:12:17 +00:00
|
|
|
<$> liftIO (initRepo isnew False dir (Just remotename) Nothing)
|
2013-09-18 19:30:53 +00:00
|
|
|
<*> combineRepos dir remotename
|
2013-09-16 20:35:27 +00:00
|
|
|
makewith a = do
|
2013-03-12 12:09:31 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True dir
|
|
|
|
isnew <- liftIO $ makeRepo dir True
|
2013-07-23 17:38:05 +00:00
|
|
|
{- Removable drives are not reliable media, so enable fsync. -}
|
|
|
|
liftIO $ inDir dir $
|
2019-12-05 19:10:23 +00:00
|
|
|
setConfig "core.fsyncobjectfiles"
|
2013-07-23 17:40:27 +00:00
|
|
|
(Git.Config.boolConfig True)
|
2013-09-16 20:35:27 +00:00
|
|
|
(u, r) <- a isnew
|
2014-05-30 16:31:54 +00:00
|
|
|
when isnew $
|
2014-05-30 18:03:04 +00:00
|
|
|
liftAnnex $ defaultStandardGroup u TransferGroup
|
2014-05-30 16:48:01 +00:00
|
|
|
liftAssistant $ immediateSyncRemote r
|
2013-09-16 20:35:27 +00:00
|
|
|
redirect $ EditNewRepositoryR u
|
2014-10-09 18:53:13 +00:00
|
|
|
mountpoint = T.unpack (mountPoint drive)
|
2013-03-20 20:46:35 +00:00
|
|
|
dir = removableDriveRepository drive
|
|
|
|
remotename = takeFileName mountpoint
|
2013-02-19 00:37:26 +00:00
|
|
|
|
|
|
|
{- Each repository is made a remote of the other.
|
|
|
|
- Next call syncRemote to get them in sync. -}
|
|
|
|
combineRepos :: FilePath -> String -> Handler Remote
|
2013-03-04 20:36:38 +00:00
|
|
|
combineRepos dir name = liftAnnex $ do
|
2013-10-02 04:42:27 +00:00
|
|
|
hostname <- fromMaybe "host" <$> liftIO getHostname
|
2015-04-09 18:59:08 +00:00
|
|
|
mylocation <- fromRepo Git.repoLocation
|
2020-11-04 18:20:37 +00:00
|
|
|
mypath <- liftIO $ fromRawFilePath <$> relPathDirToFile
|
|
|
|
(toRawFilePath dir)
|
|
|
|
(toRawFilePath mylocation)
|
2015-04-09 18:59:08 +00:00
|
|
|
liftIO $ inDir dir $ void $ makeGitRemote hostname mypath
|
2013-02-19 00:37:26 +00:00
|
|
|
addRemote $ makeGitRemote name dir
|
2012-09-02 19:06:27 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getEnableDirectoryR :: UUID -> Handler Html
|
2012-12-30 03:10:18 +00:00
|
|
|
getEnableDirectoryR uuid = page "Enable a repository" (Just Configuration) $ do
|
2013-04-03 21:01:40 +00:00
|
|
|
description <- liftAnnex $ T.pack <$> prettyUUID uuid
|
2012-09-13 20:47:44 +00:00
|
|
|
$(widgetFile "configurators/enabledirectory")
|
|
|
|
|
2012-08-31 19:17:12 +00:00
|
|
|
{- List of removable drives. -}
|
|
|
|
driveList :: IO [RemovableDrive]
|
2013-12-09 20:56:52 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-12-09 20:59:16 +00:00
|
|
|
-- Just enumerate all likely drive letters for Windows.
|
|
|
|
-- Could use wmic, but it only works for administrators.
|
2013-12-10 05:18:04 +00:00
|
|
|
driveList = mapM (\d -> genRemovableDrive $ d:":\\") ['A'..'Z']
|
2013-12-09 20:56:52 +00:00
|
|
|
#else
|
2013-12-10 05:18:04 +00:00
|
|
|
driveList = mapM (genRemovableDrive . mnt_dir) =<< filter sane <$> getMounts
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
-- filter out some things that are surely not removable drives
|
|
|
|
sane Mntent { mnt_dir = dir, mnt_fsname = dev }
|
|
|
|
{- We want real disks like /dev/foo, not
|
|
|
|
- dummy mount points like proc or tmpfs or
|
|
|
|
- gvfs-fuse-daemon. -}
|
|
|
|
| not ('/' `elem` dev) = False
|
|
|
|
{- Just in case: These mount points are surely not
|
|
|
|
- removable disks. -}
|
|
|
|
| dir == "/" = False
|
|
|
|
| dir == "/tmp" = False
|
|
|
|
| dir == "/run/shm" = False
|
|
|
|
| dir == "/run/lock" = False
|
|
|
|
| otherwise = True
|
2013-12-09 20:56:52 +00:00
|
|
|
#endif
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2013-12-10 05:18:04 +00:00
|
|
|
genRemovableDrive :: FilePath -> IO RemovableDrive
|
|
|
|
genRemovableDrive dir = RemovableDrive
|
|
|
|
<$> getDiskFree dir
|
|
|
|
<*> pure (T.pack dir)
|
|
|
|
<*> pure (T.pack gitAnnexAssistantDefaultDir)
|
|
|
|
|
2012-08-31 19:17:12 +00:00
|
|
|
{- Bootstraps from first run mode to a fully running assistant in a
|
|
|
|
- repository, by running the postFirstRun callback, which returns the
|
|
|
|
- url to the new webapp. -}
|
2013-06-10 20:41:02 +00:00
|
|
|
startFullAssistant :: FilePath -> StandardGroup -> Maybe (Annex ())-> Handler ()
|
|
|
|
startFullAssistant path repogroup setup = do
|
2012-08-31 19:17:12 +00:00
|
|
|
webapp <- getYesod
|
|
|
|
url <- liftIO $ do
|
2013-03-12 12:09:31 +00:00
|
|
|
isnew <- makeRepo path False
|
2014-05-30 00:12:17 +00:00
|
|
|
void $ initRepo isnew True path Nothing (Just repogroup)
|
|
|
|
inDir path $ fromMaybe noop setup
|
2013-03-03 21:07:27 +00:00
|
|
|
addAutoStartFile path
|
2013-05-11 23:14:30 +00:00
|
|
|
setCurrentDirectory path
|
2012-08-31 19:17:12 +00:00
|
|
|
fromJust $ postFirstRun webapp
|
|
|
|
redirect $ T.pack url
|
|
|
|
|
|
|
|
{- Checks if the user can write to a directory.
|
|
|
|
-
|
|
|
|
- The directory may be in the process of being created; if so
|
|
|
|
- the parent directory is checked instead. -}
|
|
|
|
canWrite :: FilePath -> IO Bool
|
|
|
|
canWrite dir = do
|
|
|
|
tocheck <- ifM (doesDirectoryExist dir)
|
2020-11-04 18:20:37 +00:00
|
|
|
( return dir
|
|
|
|
, return $ fromRawFilePath $ parentDir $ toRawFilePath dir
|
|
|
|
)
|
2023-03-01 19:55:58 +00:00
|
|
|
catchBoolIO $ R.fileAccess (toRawFilePath tocheck) False True False
|
2013-09-18 19:30:53 +00:00
|
|
|
|
|
|
|
{- Gets the UUID of the git repo at a location, which may not exist, or
|
|
|
|
- not be a git-annex repo. -}
|
|
|
|
probeUUID :: FilePath -> IO (Maybe UUID)
|
|
|
|
probeUUID dir = catchDefaultIO Nothing $ inDir dir $ do
|
|
|
|
u <- getUUID
|
|
|
|
return $ if u == NoUUID then Nothing else Just u
|