From 3936599885e33552b2d9261ee88edc09f75cb6be Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 13 Jan 2022 13:24:50 -0400 Subject: [PATCH] move code from Command.Fsck Sponsored-by: Dartmouth College's Datalad project --- Annex/Content.hs | 41 +++++++++++++++++++++++++++++++++++++++++ Command/Fsck.hs | 36 ------------------------------------ Command/Migrate.hs | 2 +- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Annex/Content.hs b/Annex/Content.hs index d56d2f8824..b7c7f645d8 100644 --- a/Annex/Content.hs +++ b/Annex/Content.hs @@ -59,6 +59,10 @@ module Annex.Content ( Verification(..), unVerified, withTmpWorkDir, + KeyStatus(..), + isKeyUnlockedThin, + getKeyStatus, + getKeyFileStatus, ) where import System.IO.Unsafe (unsafeInterleaveIO) @@ -794,3 +798,40 @@ exclude [] _ = [] -- optimisation exclude smaller larger = S.toList $ remove larger $ S.fromList smaller where remove a b = foldl (flip S.delete) b a + +data KeyStatus + = KeyMissing + | KeyPresent + | KeyUnlockedThin + -- ^ An annex.thin worktree file is hard linked to the object. + | KeyLockedThin + -- ^ The object has hard links, but the file being fscked + -- is not the one that hard links to it. + deriving (Show) + +isKeyUnlockedThin :: KeyStatus -> Bool +isKeyUnlockedThin KeyUnlockedThin = True +isKeyUnlockedThin KeyLockedThin = False +isKeyUnlockedThin KeyPresent = False +isKeyUnlockedThin KeyMissing = False + +getKeyStatus :: Key -> Annex KeyStatus +getKeyStatus key = catchDefaultIO KeyMissing $ do + afs <- not . null <$> Database.Keys.getAssociatedFiles key + obj <- calcRepo (gitAnnexLocation key) + multilink <- ((> 1) . linkCount <$> liftIO (R.getFileStatus obj)) + return $ if multilink && afs + then KeyUnlockedThin + else KeyPresent + +getKeyFileStatus :: Key -> FilePath -> Annex KeyStatus +getKeyFileStatus key file = do + s <- getKeyStatus key + case s of + KeyUnlockedThin -> catchDefaultIO KeyUnlockedThin $ + ifM (isJust <$> isAnnexLink (toRawFilePath file)) + ( return KeyLockedThin + , return KeyUnlockedThin + ) + _ -> return s + diff --git a/Command/Fsck.hs b/Command/Fsck.hs index b34b3a12f4..6ab159e105 100644 --- a/Command/Fsck.hs +++ b/Command/Fsck.hs @@ -705,39 +705,3 @@ withFsckDb (StartIncremental h) a = a h withFsckDb NonIncremental _ = noop withFsckDb (ScheduleIncremental _ _ i) a = withFsckDb i a -data KeyStatus - = KeyMissing - | KeyPresent - | KeyUnlockedThin - -- ^ An annex.thin worktree file is hard linked to the object. - | KeyLockedThin - -- ^ The object has hard links, but the file being fscked - -- is not the one that hard links to it. - deriving (Show) - -isKeyUnlockedThin :: KeyStatus -> Bool -isKeyUnlockedThin KeyUnlockedThin = True -isKeyUnlockedThin KeyLockedThin = False -isKeyUnlockedThin KeyPresent = False -isKeyUnlockedThin KeyMissing = False - -getKeyStatus :: Key -> Annex KeyStatus -getKeyStatus key = catchDefaultIO KeyMissing $ do - afs <- not . null <$> Database.Keys.getAssociatedFiles key - obj <- calcRepo (gitAnnexLocation key) - multilink <- ((> 1) . linkCount <$> liftIO (R.getFileStatus obj)) - return $ if multilink && afs - then KeyUnlockedThin - else KeyPresent - -getKeyFileStatus :: Key -> FilePath -> Annex KeyStatus -getKeyFileStatus key file = do - s <- getKeyStatus key - case s of - KeyUnlockedThin -> catchDefaultIO KeyUnlockedThin $ - ifM (isJust <$> isAnnexLink (toRawFilePath file)) - ( return KeyLockedThin - , return KeyUnlockedThin - ) - _ -> return s - diff --git a/Command/Migrate.hs b/Command/Migrate.hs index 1844f9a63b..0a4b3c77d0 100644 --- a/Command/Migrate.hs +++ b/Command/Migrate.hs @@ -96,7 +96,7 @@ perform onlyremovesize o file oldkey oldbackend newbackend = go =<< genkey (fast | knowngoodcontent = finish (removesize newkey) | otherwise = stopUnless checkcontent $ finish (removesize newkey) - checkcontent = Command.Fsck.checkBackend oldbackend oldkey Command.Fsck.KeyPresent afile + checkcontent = Command.Fsck.checkBackend oldbackend oldkey KeyPresent afile finish newkey = ifM (Command.ReKey.linkKey file oldkey newkey) ( do _ <- copyMetaData oldkey newkey -- 2.39.5