2015-08-05 18:09:25 +00:00
|
|
|
{- git-annex-shell checks
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-08-05 18:09:25 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module CmdLine.GitAnnexShell.Checks where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2015-08-05 18:09:25 +00:00
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.Init
|
|
|
|
import Utility.UserInfo
|
|
|
|
import Utility.Env
|
|
|
|
|
2018-03-07 19:15:23 +00:00
|
|
|
limitedEnv :: String
|
|
|
|
limitedEnv = "GIT_ANNEX_SHELL_LIMITED"
|
|
|
|
|
2015-08-05 18:09:25 +00:00
|
|
|
checkNotLimited :: IO ()
|
2018-03-07 19:15:23 +00:00
|
|
|
checkNotLimited = checkEnv limitedEnv
|
|
|
|
|
|
|
|
readOnlyEnv :: String
|
|
|
|
readOnlyEnv = "GIT_ANNEX_SHELL_READONLY"
|
2015-08-05 18:09:25 +00:00
|
|
|
|
|
|
|
checkNotReadOnly :: IO ()
|
2018-03-07 19:15:23 +00:00
|
|
|
checkNotReadOnly = checkEnv readOnlyEnv
|
2015-08-05 18:09:25 +00:00
|
|
|
|
2018-05-25 17:17:56 +00:00
|
|
|
appendOnlyEnv :: String
|
|
|
|
appendOnlyEnv = "GIT_ANNEX_SHELL_APPENDONLY"
|
|
|
|
|
|
|
|
checkNotAppendOnly :: IO ()
|
|
|
|
checkNotAppendOnly = checkEnv appendOnlyEnv
|
|
|
|
|
2015-08-05 18:09:25 +00:00
|
|
|
checkEnv :: String -> IO ()
|
2018-03-07 19:15:23 +00:00
|
|
|
checkEnv var = checkEnvSet var >>= \case
|
|
|
|
False -> noop
|
|
|
|
True -> giveup $ "Action blocked by " ++ var
|
|
|
|
|
|
|
|
checkEnvSet :: String -> IO Bool
|
|
|
|
checkEnvSet var = getEnv var >>= return . \case
|
|
|
|
Nothing -> False
|
|
|
|
Just "" -> False
|
|
|
|
Just _ -> True
|
2015-08-05 18:09:25 +00:00
|
|
|
|
|
|
|
checkDirectory :: Maybe FilePath -> IO ()
|
|
|
|
checkDirectory mdir = do
|
|
|
|
v <- getEnv "GIT_ANNEX_SHELL_DIRECTORY"
|
|
|
|
case (v, mdir) of
|
|
|
|
(Nothing, _) -> noop
|
|
|
|
(Just d, Nothing) -> req d Nothing
|
|
|
|
(Just d, Just dir)
|
|
|
|
| d `equalFilePath` dir -> noop
|
|
|
|
| otherwise -> do
|
|
|
|
home <- myHomeDir
|
|
|
|
d' <- canondir home d
|
|
|
|
dir' <- canondir home dir
|
|
|
|
if d' `equalFilePath` dir'
|
|
|
|
then noop
|
|
|
|
else req d' (Just dir')
|
|
|
|
where
|
2016-11-16 01:29:54 +00:00
|
|
|
req d mdir' = giveup $ unwords
|
2015-08-05 18:09:25 +00:00
|
|
|
[ "Only allowed to access"
|
|
|
|
, d
|
|
|
|
, maybe "and could not determine directory from command line" ("not " ++) mdir'
|
|
|
|
]
|
|
|
|
|
|
|
|
{- A directory may start with ~/ or in some cases, even /~/,
|
|
|
|
- or could just be relative to home, or of course could
|
|
|
|
- be absolute. -}
|
|
|
|
canondir home d
|
|
|
|
| "~/" `isPrefixOf` d = return d
|
|
|
|
| "/~/" `isPrefixOf` d = return $ drop 1 d
|
2020-11-04 18:20:37 +00:00
|
|
|
| otherwise = relHome $ fromRawFilePath $ absPathFrom
|
2020-11-03 22:34:27 +00:00
|
|
|
(toRawFilePath home)
|
|
|
|
(toRawFilePath d)
|
2015-08-05 18:09:25 +00:00
|
|
|
|
|
|
|
{- Modifies a Command to check that it is run in either a git-annex
|
|
|
|
- repository, or a repository with a gcrypt-id set. -}
|
|
|
|
gitAnnexShellCheck :: Command -> Command
|
2023-05-11 17:36:59 +00:00
|
|
|
gitAnnexShellCheck = addCheck GitAnnexShellOk okforshell . dontCheck repoExists
|
2015-08-05 18:09:25 +00:00
|
|
|
where
|
|
|
|
okforshell = unlessM (isInitialized <||> isJust . gcryptId <$> Annex.getGitConfig) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup "Not a git-annex or gcrypt repository."
|