2010-11-09 19:59:49 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-06-09 18:45:00 +00:00
|
|
|
- Copyright 2010-2016 Joey Hess <id@joeyh.name>
|
2010-11-09 19:59:49 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Unlock where
|
|
|
|
|
|
|
|
import Command
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2016-03-09 17:43:22 +00:00
|
|
|
import Annex.Perms
|
2015-01-02 17:48:56 +00:00
|
|
|
import Annex.CatFile
|
2015-12-10 20:12:05 +00:00
|
|
|
import Annex.Version
|
|
|
|
import Annex.Link
|
|
|
|
import Annex.ReplaceFile
|
2011-07-06 00:24:10 +00:00
|
|
|
import Utility.CopyFile
|
2016-04-14 18:30:15 +00:00
|
|
|
import Utility.FileMode
|
2016-06-09 18:45:00 +00:00
|
|
|
import Git.FilePath
|
|
|
|
import qualified Database.Keys
|
2010-11-09 19:59:49 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = mkcmd "unlock" "unlock files for modification"
|
|
|
|
|
|
|
|
editcmd :: Command
|
|
|
|
editcmd = mkcmd "edit" "same as unlock"
|
|
|
|
|
|
|
|
mkcmd :: String -> String -> Command
|
2015-07-10 17:18:46 +00:00
|
|
|
mkcmd n d = notDirect $ withGlobalOptions annexedMatchingOptions $
|
2015-07-08 19:08:02 +00:00
|
|
|
command n SectionCommon d paramPaths (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withFilesInGit $ whenAnnexed start
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2015-12-10 20:12:05 +00:00
|
|
|
{- Before v6, the unlock subcommand replaces the symlink with a copy of
|
|
|
|
- the file's content. In v6 and above, it converts the file from a symlink
|
|
|
|
- to a pointer. -}
|
2014-04-17 22:03:39 +00:00
|
|
|
start :: FilePath -> Key -> CommandStart
|
2015-12-10 20:12:05 +00:00
|
|
|
start file key = ifM (isJust <$> isAnnexLink file)
|
|
|
|
( do
|
|
|
|
showStart "unlock" file
|
2016-06-09 18:40:44 +00:00
|
|
|
ifM versionSupportsUnlockedPointers
|
|
|
|
( next $ performNew file key
|
|
|
|
, startOld file key
|
2015-12-10 20:12:05 +00:00
|
|
|
)
|
|
|
|
, stop
|
|
|
|
)
|
|
|
|
|
|
|
|
performNew :: FilePath -> Key -> CommandPerform
|
|
|
|
performNew dest key = do
|
2016-04-14 18:30:15 +00:00
|
|
|
destmode <- liftIO $ catchMaybeIO $ fileMode <$> getFileStatus dest
|
2016-06-09 18:40:44 +00:00
|
|
|
replaceFile dest $ \tmp ->
|
|
|
|
ifM (inAnnex key)
|
|
|
|
( do
|
|
|
|
r <- linkFromAnnex key tmp destmode
|
|
|
|
case r of
|
|
|
|
LinkAnnexOk -> return ()
|
|
|
|
LinkAnnexNoop -> return ()
|
|
|
|
_ -> error "unlock failed"
|
|
|
|
, liftIO $ writePointerFile tmp key destmode
|
|
|
|
)
|
2016-04-14 18:30:15 +00:00
|
|
|
next $ cleanupNew dest key destmode
|
2015-12-10 20:12:05 +00:00
|
|
|
|
2016-04-14 18:30:15 +00:00
|
|
|
cleanupNew :: FilePath -> Key -> Maybe FileMode -> CommandCleanup
|
|
|
|
cleanupNew dest key destmode = do
|
|
|
|
stagePointerFile dest destmode =<< hashPointerFile key
|
2016-06-09 18:45:00 +00:00
|
|
|
Database.Keys.addAssociatedFile key =<< inRepo (toTopFilePath dest)
|
2015-12-10 20:12:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
startOld :: FilePath -> Key -> CommandStart
|
|
|
|
startOld file key =
|
2014-08-10 19:16:24 +00:00
|
|
|
ifM (inAnnex key)
|
2015-01-02 17:48:56 +00:00
|
|
|
( ifM (isJust <$> catKeyFileHEAD file)
|
2015-12-10 20:12:05 +00:00
|
|
|
( next $ performOld file key
|
2014-08-10 19:16:24 +00:00
|
|
|
, do
|
2015-01-02 17:48:56 +00:00
|
|
|
warning "this has not yet been committed to git; cannot unlock it"
|
2014-08-10 19:16:24 +00:00
|
|
|
next $ next $ return False
|
|
|
|
)
|
2014-08-09 15:09:54 +00:00
|
|
|
, do
|
2014-08-10 19:16:24 +00:00
|
|
|
warning "content not present; cannot unlock"
|
2014-08-09 15:09:54 +00:00
|
|
|
next $ next $ return False
|
|
|
|
)
|
2010-11-09 19:59:49 +00:00
|
|
|
|
2015-12-10 20:12:05 +00:00
|
|
|
performOld :: FilePath -> Key -> CommandPerform
|
|
|
|
performOld dest key = ifM (checkDiskSpace Nothing key 0 True)
|
2015-01-02 17:48:56 +00:00
|
|
|
( do
|
|
|
|
src <- calcRepo $ gitAnnexLocation key
|
|
|
|
tmpdest <- fromRepo $ gitAnnexTmpObjectLocation key
|
2015-01-09 17:11:56 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir tmpdest)
|
2015-01-02 17:48:56 +00:00
|
|
|
showAction "copying"
|
|
|
|
ifM (liftIO $ copyFileExternal CopyAllMetaData src tmpdest)
|
|
|
|
( do
|
|
|
|
liftIO $ do
|
|
|
|
removeFile dest
|
|
|
|
moveFile tmpdest dest
|
|
|
|
thawContent dest
|
|
|
|
next $ return True
|
|
|
|
, do
|
|
|
|
warning "copy failed!"
|
|
|
|
next $ return False
|
|
|
|
)
|
|
|
|
, do
|
|
|
|
warning "not enough disk space to copy file"
|
|
|
|
next $ return False
|
|
|
|
)
|