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
|
2022-06-14 18:19:58 +00:00
|
|
|
import Annex.Link
|
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
|
|
|
|
2023-03-01 19:55:58 +00:00
|
|
|
import System.PosixCompat.Files (fileMode, linkCount)
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2021-10-18 20:25:28 +00:00
|
|
|
import qualified System.Posix.Files as Posix
|
2023-03-01 19:55:58 +00:00
|
|
|
import Utility.Touch
|
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
|
2023-05-04 18:28:21 +00:00
|
|
|
cmd = noCommit $ withAnnexOptions [annexedMatchingOptions, jsonOptions] $
|
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
|
2023-04-25 23:26:20 +00:00
|
|
|
ww = WarnUnmatchLsFiles "fix"
|
2020-07-13 21:04:02 +00:00
|
|
|
seeker = AnnexedFileSeeker
|
2023-12-06 17:04:32 +00:00
|
|
|
{ startAction = const $ 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
|
2019-12-06 18:44:42 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> R.getFileStatus file
|
2023-10-26 17:36:49 +00:00
|
|
|
unlessM (checkedCopyFile key obj tmp mode) $
|
2023-04-10 17:38:14 +00:00
|
|
|
giveup "unable to break hard link"
|
2023-10-26 17:36:49 +00:00
|
|
|
thawContent tmp
|
|
|
|
Database.Keys.storeInodeCaches key [tmp]
|
2022-05-16 16:34:56 +00:00
|
|
|
modifyContentDir obj $ freezeContent obj
|
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
|
2023-10-26 17:36:49 +00:00
|
|
|
linkFromAnnex' key tmp mode >>= \case
|
2023-04-10 17:38:14 +00:00
|
|
|
LinkAnnexFailed -> giveup "unable to make hard link"
|
2015-12-27 19:59:59 +00:00
|
|
|
_ -> 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
|
2021-10-18 20:25:28 +00:00
|
|
|
mtime <- liftIO $ catchMaybeIO $ Posix.modificationTimeHiRes
|
2020-11-03 14:11:04 +00:00
|
|
|
<$> R.getSymbolicLinkStatus file
|
2013-07-11 15:39:42 +00:00
|
|
|
#endif
|
2022-06-22 17:40:14 +00:00
|
|
|
replaceWorkTreeFile (fromRawFilePath file) $ \tmpfile -> do
|
2023-10-26 17:36:49 +00:00
|
|
|
liftIO $ R.createSymbolicLink link tmpfile
|
2018-10-13 05:36:06 +00:00
|
|
|
#if ! defined(mingw32_HOST_OS)
|
2023-10-26 17:36:49 +00:00
|
|
|
liftIO $ maybe noop (\t -> touch tmpfile t False) mtime
|
2013-07-11 15:39:42 +00:00
|
|
|
#endif
|
2022-06-14 18:19:58 +00:00
|
|
|
stageSymlink file =<< hashSymlink link
|
|
|
|
next $ return True
|