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.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
|
2019-12-06 18:19:23 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
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
|
2019-08-26 19:52:19 +00:00
|
|
|
cmd = 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
|
2020-07-10 19:40:06 +00:00
|
|
|
seek ps = unlessM crippledFileSystem $
|
2020-07-13 21:04:02 +00:00
|
|
|
withFilesInGitAnnex ww seeker =<< workTreeItems ww ps
|
2020-05-28 19:55:17 +00:00
|
|
|
where
|
|
|
|
ww = WarnUnmatchLsFiles
|
2020-07-13 21:04:02 +00:00
|
|
|
seeker = AnnexedFileSeeker
|
2020-07-22 18:23:28 +00:00
|
|
|
{ startAction = start FixAll
|
2020-07-13 21:04:02 +00:00
|
|
|
, checkContentPresent = Nothing
|
|
|
|
, usesLocationLog = False
|
|
|
|
}
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2015-12-27 19:59:59 +00:00
|
|
|
data FixWhat = FixSymlinks | FixAll
|
|
|
|
|
2020-09-14 20:49:33 +00:00
|
|
|
start :: FixWhat -> SeekInput -> RawFilePath -> Key -> CommandStart
|
|
|
|
start fixwhat si file key = do
|
2019-12-06 18:19:23 +00:00
|
|
|
currlink <- liftIO $ catchMaybeIO $ R.readSymbolicLink file
|
2020-11-03 14:11:04 +00:00
|
|
|
wantlink <- calcRepo $ gitAnnexLink file key
|
2015-12-27 19:59:59 +00:00
|
|
|
case currlink of
|
|
|
|
Just l
|
2020-11-03 14:11:04 +00:00
|
|
|
| l /= wantlink -> fixby $ fixSymlink file wantlink
|
2015-12-27 19:59:59 +00:00
|
|
|
| otherwise -> stop
|
|
|
|
Nothing -> case fixwhat of
|
|
|
|
FixAll -> fixthin
|
|
|
|
FixSymlinks -> stop
|
|
|
|
where
|
2020-09-14 20:49:33 +00:00
|
|
|
fixby = starting "fix" (mkActionItem (key, file)) si
|
2015-12-27 19:59:59 +00:00
|
|
|
fixthin = do
|
2019-12-11 18:12:22 +00:00
|
|
|
obj <- calcRepo (gitAnnexLocation key)
|
|
|
|
stopUnless (isUnmodified key file <&&> isUnmodified key obj) $ do
|
2015-12-27 19:59:59 +00:00
|
|
|
thin <- annexThin <$> Annex.getGitConfig
|
2019-12-06 18:44:42 +00:00
|
|
|
fs <- liftIO $ catchMaybeIO $ R.getFileStatus file
|
2019-12-11 18:12:22 +00:00
|
|
|
os <- liftIO $ catchMaybeIO $ R.getFileStatus obj
|
2015-12-27 19:59:59 +00:00
|
|
|
case (linkCount <$> fs, linkCount <$> os, thin) of
|
|
|
|
(Just 1, Just 1, True) ->
|
2019-12-06 18:44:42 +00:00
|
|
|
fixby $ makeHardLink file key
|
2015-12-27 19:59:59 +00:00
|
|
|
(Just n, Just n', False) | n > 1 && n == n' ->
|
2019-12-06 18:44:42 +00:00
|
|
|
fixby $ breakHardLink file key obj
|
2015-12-27 19:59:59 +00:00
|
|
|
_ -> stop
|
|
|
|
|
2019-12-11 18:12:22 +00:00
|
|
|
breakHardLink :: RawFilePath -> Key -> RawFilePath -> CommandPerform
|
2015-12-27 19:59:59 +00:00
|
|
|
breakHardLink file key obj = do
|
2020-03-06 15:31:01 +00:00
|
|
|
replaceWorkTreeFile (fromRawFilePath file) $ \tmp -> do
|
2020-11-06 18:10:58 +00:00
|
|
|
let tmp' = toRawFilePath tmp
|
2019-12-06 18:44:42 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> R.getFileStatus file
|
2020-11-06 18:10:58 +00:00
|
|
|
unlessM (checkedCopyFile key obj tmp' mode) $
|
2015-12-27 19:59:59 +00:00
|
|
|
error "unable to break hard link"
|
2020-11-06 18:10:58 +00:00
|
|
|
thawContent tmp'
|
|
|
|
modifyContent obj $ freezeContent obj
|
2019-12-11 18:12:22 +00:00
|
|
|
Database.Keys.storeInodeCaches key [file]
|
2015-12-27 19:59:59 +00:00
|
|
|
next $ return True
|
|
|
|
|
2019-12-06 18:44:42 +00:00
|
|
|
makeHardLink :: RawFilePath -> Key -> CommandPerform
|
2015-12-27 19:59:59 +00:00
|
|
|
makeHardLink file key = do
|
2020-03-06 15:31:01 +00:00
|
|
|
replaceWorkTreeFile (fromRawFilePath file) $ \tmp -> do
|
2019-12-06 18:44:42 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> R.getFileStatus file
|
2020-11-03 14:11:04 +00:00
|
|
|
linkFromAnnex key (toRawFilePath 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
|
|
|
|
2020-11-03 14:11:04 +00:00
|
|
|
fixSymlink :: RawFilePath -> RawFilePath -> CommandPerform
|
2015-12-27 19:59:59 +00:00
|
|
|
fixSymlink file link = do
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2020-03-09 16:29:40 +00:00
|
|
|
-- preserve mtime of symlink
|
|
|
|
mtime <- liftIO $ catchMaybeIO $ modificationTimeHiRes
|
2020-11-03 14:11:04 +00:00
|
|
|
<$> R.getSymbolicLinkStatus file
|
2013-07-11 15:39:42 +00:00
|
|
|
#endif
|
2020-03-09 16:29:40 +00:00
|
|
|
createWorkTreeDirectory (parentDir file)
|
2020-11-03 14:11:04 +00:00
|
|
|
liftIO $ R.removeLink file
|
|
|
|
liftIO $ R.createSymbolicLink link file
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2020-03-09 16:29:40 +00:00
|
|
|
liftIO $ maybe noop (\t -> touch file t False) mtime
|
2013-07-11 15:39:42 +00:00
|
|
|
#endif
|
2020-11-03 14:11:04 +00:00
|
|
|
next $ cleanupSymlink (fromRawFilePath 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
|