2015-08-05 18:09:25 +00:00
|
|
|
{- git-annex-shell checks
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
checkNotLimited :: IO ()
|
|
|
|
checkNotLimited = checkEnv "GIT_ANNEX_SHELL_LIMITED"
|
|
|
|
|
|
|
|
checkNotReadOnly :: IO ()
|
|
|
|
checkNotReadOnly = checkEnv "GIT_ANNEX_SHELL_READONLY"
|
|
|
|
|
|
|
|
checkEnv :: String -> IO ()
|
|
|
|
checkEnv var = do
|
|
|
|
v <- getEnv var
|
|
|
|
case v of
|
|
|
|
Nothing -> noop
|
|
|
|
Just "" -> noop
|
2016-11-16 01:29:54 +00:00
|
|
|
Just _ -> giveup $ "Action blocked by " ++ var
|
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
|
|
|
|
| otherwise = relHome $ absPathFrom home d
|
|
|
|
|
|
|
|
{- 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
|
|
|
|
gitAnnexShellCheck = addCheck okforshell . dontCheck repoExists
|
|
|
|
where
|
|
|
|
okforshell = unlessM (isInitialized <||> isJust . gcryptId <$> Annex.getGitConfig) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup "Not a git-annex or gcrypt repository."
|