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