2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-12-27 19:59:59 +00:00
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-11-02 23:04:24 +00:00
|
|
|
-}
|
|
|
|
|
2013-07-11 15:39:42 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
module Command.Fix where
|
|
|
|
|
|
|
|
import Command
|
2015-12-27 19:59:59 +00:00
|
|
|
import Config
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.Version
|
|
|
|
import Annex.ReplaceFile
|
|
|
|
import Annex.Content
|
2015-12-27 20:12:48 +00:00
|
|
|
import Annex.Perms
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Queue
|
2015-12-27 19:59:59 +00:00
|
|
|
import qualified Database.Keys
|
2018-10-30 04:40:17 +00:00
|
|
|
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2013-07-11 15:39:42 +00:00
|
|
|
import Utility.Touch
|
2018-10-30 04:40:17 +00:00
|
|
|
import System.Posix.Files
|
2016-05-05 19:49:56 +00:00
|
|
|
#endif
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2018-02-19 18:28:17 +00:00
|
|
|
cmd = notDirect $ noCommit $ withGlobalOptions [annexedMatchingOptions] $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "fix" SectionMaintenance
|
2015-12-27 19:59:59 +00:00
|
|
|
"fix up links to annexed content"
|
2015-07-08 19:08:02 +00:00
|
|
|
paramPaths (withParams seek)
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2015-12-27 19:59:59 +00:00
|
|
|
seek ps = unlessM crippledFileSystem $ do
|
|
|
|
fixwhat <- ifM versionSupportsUnlockedPointers
|
|
|
|
( return FixAll
|
|
|
|
, return FixSymlinks
|
|
|
|
)
|
2018-10-01 18:12:06 +00:00
|
|
|
withFilesInGit
|
|
|
|
(commandAction . (whenAnnexed $ start fixwhat))
|
|
|
|
=<< workTreeItems ps
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2015-12-27 19:59:59 +00:00
|
|
|
data FixWhat = FixSymlinks | FixAll
|
|
|
|
|
|
|
|
start :: FixWhat -> FilePath -> Key -> CommandStart
|
|
|
|
start fixwhat file key = do
|
|
|
|
currlink <- liftIO $ catchMaybeIO $ readSymbolicLink file
|
|
|
|
wantlink <- calcRepo $ gitAnnexLink file key
|
|
|
|
case currlink of
|
|
|
|
Just l
|
|
|
|
| l /= wantlink -> fixby $ fixSymlink file wantlink
|
|
|
|
| otherwise -> stop
|
|
|
|
Nothing -> case fixwhat of
|
|
|
|
FixAll -> fixthin
|
|
|
|
FixSymlinks -> stop
|
|
|
|
where
|
|
|
|
fixby a = do
|
2011-12-09 16:23:45 +00:00
|
|
|
showStart "fix" file
|
2015-12-27 19:59:59 +00:00
|
|
|
next a
|
|
|
|
fixthin = do
|
|
|
|
obj <- calcRepo $ gitAnnexLocation key
|
|
|
|
stopUnless (isUnmodified key file <&&> isUnmodified key obj) $ do
|
|
|
|
thin <- annexThin <$> Annex.getGitConfig
|
|
|
|
fs <- liftIO $ catchMaybeIO $ getFileStatus file
|
|
|
|
os <- liftIO $ catchMaybeIO $ getFileStatus obj
|
|
|
|
case (linkCount <$> fs, linkCount <$> os, thin) of
|
|
|
|
(Just 1, Just 1, True) ->
|
|
|
|
fixby $ makeHardLink file key
|
|
|
|
(Just n, Just n', False) | n > 1 && n == n' ->
|
|
|
|
fixby $ breakHardLink file key obj
|
|
|
|
_ -> stop
|
|
|
|
|
|
|
|
breakHardLink :: FilePath -> Key -> FilePath -> CommandPerform
|
|
|
|
breakHardLink file key obj = do
|
2015-12-27 20:12:48 +00:00
|
|
|
replaceFile file $ \tmp -> do
|
2016-04-14 18:30:15 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> getFileStatus file
|
|
|
|
unlessM (checkedCopyFile key obj tmp mode) $
|
2015-12-27 19:59:59 +00:00
|
|
|
error "unable to break hard link"
|
2015-12-27 20:12:48 +00:00
|
|
|
thawContent tmp
|
|
|
|
modifyContent obj $ freezeContent obj
|
2015-12-27 19:59:59 +00:00
|
|
|
Database.Keys.storeInodeCaches key [file]
|
|
|
|
next $ return True
|
|
|
|
|
|
|
|
makeHardLink :: FilePath -> Key -> CommandPerform
|
|
|
|
makeHardLink file key = do
|
|
|
|
replaceFile file $ \tmp -> do
|
2016-04-14 18:30:15 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> getFileStatus file
|
2017-12-05 19:00:50 +00:00
|
|
|
linkFromAnnex key tmp mode >>= \case
|
2015-12-27 19:59:59 +00:00
|
|
|
LinkAnnexFailed -> error "unable to make hard link"
|
|
|
|
_ -> noop
|
|
|
|
next $ return True
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-12-27 19:59:59 +00:00
|
|
|
fixSymlink :: FilePath -> FilePath -> CommandPerform
|
|
|
|
fixSymlink file link = do
|
2013-07-11 15:39:42 +00:00
|
|
|
liftIO $ do
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2013-07-11 15:39:42 +00:00
|
|
|
-- preserve mtime of symlink
|
2018-10-30 04:40:17 +00:00
|
|
|
mtime <- catchMaybeIO $ modificationTimeHiRes
|
2013-07-11 15:39:42 +00:00
|
|
|
<$> getSymbolicLinkStatus file
|
|
|
|
#endif
|
2015-01-09 17:11:56 +00:00
|
|
|
createDirectoryIfMissing True (parentDir file)
|
2013-07-11 15:39:42 +00:00
|
|
|
removeFile file
|
|
|
|
createSymbolicLink link file
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2013-07-11 15:39:42 +00:00
|
|
|
maybe noop (\t -> touch file t False) mtime
|
|
|
|
#endif
|
2015-12-27 19:59:59 +00:00
|
|
|
next $ cleanupSymlink file
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2015-12-27 19:59:59 +00:00
|
|
|
cleanupSymlink :: FilePath -> CommandCleanup
|
|
|
|
cleanupSymlink file = do
|
2012-06-07 19:19:44 +00:00
|
|
|
Annex.Queue.addCommand "add" [Param "--force", Param "--"] [file]
|
2010-11-02 23:04:24 +00:00
|
|
|
return True
|