2011-10-31 16:33:41 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-05-08 19:43:37 +00:00
|
|
|
- Copyright 2011-2023 Joey Hess <id@joeyh.name>
|
2011-10-31 16:33:41 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-10-31 16:33:41 +00:00
|
|
|
-}
|
|
|
|
|
2023-04-10 16:56:45 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2011-10-31 19:18:41 +00:00
|
|
|
module Command.Reinject where
|
2011-10-31 16:33:41 +00:00
|
|
|
|
|
|
|
import Command
|
|
|
|
import Logs.Location
|
|
|
|
import Annex.Content
|
2016-04-22 17:49:32 +00:00
|
|
|
import Backend
|
|
|
|
import Types.KeySource
|
2019-06-25 15:37:52 +00:00
|
|
|
import Utility.Metered
|
2022-10-26 17:58:20 +00:00
|
|
|
import Annex.WorkTree
|
2020-01-06 18:22:22 +00:00
|
|
|
import qualified Git
|
2023-04-10 16:56:45 +00:00
|
|
|
import qualified Annex
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd :: Command
|
2023-05-08 19:43:37 +00:00
|
|
|
cmd = withAnnexOptions [backendOption, jsonOptions] $
|
2022-06-29 17:28:08 +00:00
|
|
|
command "reinject" SectionUtility
|
|
|
|
"inject content of file back into annex"
|
|
|
|
(paramRepeating (paramPair "SRC" "DEST"))
|
|
|
|
(seek <$$> optParser)
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
data ReinjectOptions = ReinjectOptions
|
|
|
|
{ params :: CmdParams
|
|
|
|
, knownOpt :: Bool
|
2023-06-26 18:05:31 +00:00
|
|
|
, guessKeysOpt :: Bool
|
2016-04-22 17:49:32 +00:00
|
|
|
}
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser ReinjectOptions
|
|
|
|
optParser desc = ReinjectOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> switch
|
|
|
|
( long "known"
|
|
|
|
<> help "inject all known files"
|
|
|
|
<> hidden
|
|
|
|
)
|
2023-06-26 18:05:31 +00:00
|
|
|
<*> switch
|
|
|
|
( long "guesskeys"
|
|
|
|
<> help "inject files that are named like keys"
|
|
|
|
<> hidden
|
|
|
|
)
|
2016-04-22 17:49:32 +00:00
|
|
|
|
|
|
|
seek :: ReinjectOptions -> CommandSeek
|
|
|
|
seek os
|
2023-06-26 18:05:31 +00:00
|
|
|
| guessKeysOpt os && knownOpt os = giveup "Cannot combine --known with --guesskeys"
|
|
|
|
| guessKeysOpt os = withStrings (commandAction . startGuessKeys) (params os)
|
2018-10-01 18:12:06 +00:00
|
|
|
| knownOpt os = withStrings (commandAction . startKnown) (params os)
|
2023-05-08 19:43:37 +00:00
|
|
|
| otherwise = withPairs (commandAction . startSrcDest) (params os)
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-05-08 19:43:37 +00:00
|
|
|
startSrcDest :: (SeekInput, (String, String)) -> CommandStart
|
|
|
|
startSrcDest (si, (src, dest))
|
2011-10-31 20:46:51 +00:00
|
|
|
| src == dest = stop
|
2023-05-08 19:43:37 +00:00
|
|
|
| otherwise = starting "reinject" ai si $ notAnnexed src' $
|
2022-10-26 17:58:20 +00:00
|
|
|
lookupKey (toRawFilePath dest) >>= \case
|
2023-05-08 19:43:37 +00:00
|
|
|
Just key -> ifM (verifyKeyContent key src')
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " does not have expected content of "
|
|
|
|
<> QuotedPath (toRawFilePath dest)
|
|
|
|
)
|
2023-04-10 16:56:45 +00:00
|
|
|
Nothing -> do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " is not an annexed file"
|
2017-02-09 19:40:44 +00:00
|
|
|
where
|
2020-11-02 20:31:28 +00:00
|
|
|
src' = toRawFilePath src
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-06-26 18:05:31 +00:00
|
|
|
startGuessKeys :: FilePath -> CommandStart
|
|
|
|
startGuessKeys src = starting "reinject" ai si $ notAnnexed src' $
|
|
|
|
case fileKey (toRawFilePath (takeFileName src)) of
|
|
|
|
Just key -> ifM (verifyKeyContent key src')
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $ QuotedPath src'
|
|
|
|
<> " does not have expected content"
|
|
|
|
)
|
|
|
|
Nothing -> do
|
|
|
|
warning "Not named like an object file; skipping"
|
|
|
|
next $ return True
|
|
|
|
where
|
|
|
|
src' = toRawFilePath src
|
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
|
|
|
si = SeekInput [src]
|
|
|
|
|
2016-04-22 17:49:32 +00:00
|
|
|
startKnown :: FilePath -> CommandStart
|
2023-05-08 19:43:37 +00:00
|
|
|
startKnown src = starting "reinject" ai si $ notAnnexed src' $ do
|
|
|
|
(key, _) <- genKey ks nullMeterUpdate =<< defaultBackend
|
|
|
|
ifM (isKnownKey key)
|
|
|
|
( perform src' key
|
|
|
|
, do
|
|
|
|
warning "Not known content; skipping"
|
|
|
|
next $ return True
|
|
|
|
)
|
2020-02-21 13:34:59 +00:00
|
|
|
where
|
|
|
|
src' = toRawFilePath src
|
2020-11-02 20:31:28 +00:00
|
|
|
ks = KeySource src' src' Nothing
|
2023-04-08 19:48:32 +00:00
|
|
|
ai = ActionItemOther (Just (QuotedPath src'))
|
2020-09-14 20:49:33 +00:00
|
|
|
si = SeekInput [src]
|
2016-04-22 17:49:32 +00:00
|
|
|
|
2023-05-08 19:43:37 +00:00
|
|
|
notAnnexed :: RawFilePath -> CommandPerform -> CommandPerform
|
2020-01-06 18:22:22 +00:00
|
|
|
notAnnexed src a =
|
|
|
|
ifM (fromRepo Git.repoIsLocalBare)
|
|
|
|
( a
|
2022-10-26 17:58:20 +00:00
|
|
|
, lookupKey src >>= \case
|
2023-04-10 16:56:45 +00:00
|
|
|
Just _ -> do
|
|
|
|
qp <- coreQuotePath <$> Annex.getGitConfig
|
|
|
|
giveup $ decodeBS $ quote qp $
|
|
|
|
"cannot used annexed file as src: "
|
|
|
|
<> QuotedPath src
|
2022-10-26 17:58:20 +00:00
|
|
|
Nothing -> a
|
2020-01-06 18:22:22 +00:00
|
|
|
)
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2020-11-02 20:31:28 +00:00
|
|
|
perform :: RawFilePath -> Key -> CommandPerform
|
2023-05-08 19:43:37 +00:00
|
|
|
perform src key = do
|
2023-05-08 20:03:34 +00:00
|
|
|
maybeAddJSONField "key" (serializeKey key)
|
2023-05-08 19:43:37 +00:00
|
|
|
ifM move
|
|
|
|
( next $ cleanup key
|
|
|
|
, giveup "failed"
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
annex.securehashesonly
Cryptographically secure hashes can be forced to be used in a repository,
by setting annex.securehashesonly. This does not prevent the git repository
from containing files with insecure hashes, but it does prevent the content
of such files from being pulled into .git/annex/objects from another
repository.
We want to make sure that at no point does git-annex accept content into
.git/annex/objects that is hashed with an insecure key. Here's how it
was done:
* .git/annex/objects/xx/yy/KEY/ is kept frozen, so nothing can be
written to it normally
* So every place that writes content must call, thawContent or modifyContent.
We can audit for these, and be sure we've considered all cases.
* The main functions are moveAnnex, and linkToAnnex; these were made to
check annex.securehashesonly, and are the main security boundary
for annex.securehashesonly.
* Most other calls to modifyContent deal with other files in the KEY
directory (inode cache etc). The other ones that mess with the content
are:
- Annex.Direct.toDirectGen, in which content already in the
annex directory is moved to the direct mode file, so not relevant.
- fix and lock, which don't add new content
- Command.ReKey.linkKey, which manually unlocks it to make a
copy.
* All other calls to thawContent appear safe.
Made moveAnnex return a Bool, so checked all callsites and made them
deal with a failure in appropriate ways.
linkToAnnex simply returns LinkAnnexFailed; all callsites already deal
with it failing in appropriate ways.
This commit was sponsored by Riku Voipio.
2017-02-27 17:01:32 +00:00
|
|
|
move = checkDiskSpaceToGet key False $
|
2020-11-16 18:09:55 +00:00
|
|
|
moveAnnex key (AssociatedFile Nothing) src
|
2011-10-31 16:33:41 +00:00
|
|
|
|
2012-09-16 05:17:48 +00:00
|
|
|
cleanup :: Key -> CommandCleanup
|
|
|
|
cleanup key = do
|
2011-10-31 16:33:41 +00:00
|
|
|
logStatus key InfoPresent
|
2012-09-16 05:17:48 +00:00
|
|
|
return True
|