aboutsummaryrefslogtreecommitdiff
path: root/Command/Fsck.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-07-09 12:26:25 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-07-09 12:26:25 -0400
commit97545540a3e00bb696142c022894ac216961dc23 (patch)
tree50e102151444a8a45ff513aa789b48ed154d1782 /Command/Fsck.hs
parent6ddffe0f0e64a87fd924f736941fa242e0d740a6 (diff)
use Alternative when parsing mutually exclusive fsck options
Diffstat (limited to 'Command/Fsck.hs')
-rw-r--r--Command/Fsck.hs91
1 files changed, 45 insertions, 46 deletions
diff --git a/Command/Fsck.hs b/Command/Fsck.hs
index 5350a63eb..9ca859d95 100644
--- a/Command/Fsck.hs
+++ b/Command/Fsck.hs
@@ -50,12 +50,15 @@ cmd = command "fsck" SectionMaintenance
data FsckOptions = FsckOptions
{ fsckFiles :: CmdParams
, fsckFromOption :: Maybe RemoteName
- , startIncrementalOption :: Bool
- , moreIncrementalOption :: Bool
- , incrementalScheduleOption :: Maybe Duration
+ , incrementalOpt :: Maybe IncrementalOpt
, keyOptions :: KeyOptions
}
+data IncrementalOpt
+ = StartIncrementalO
+ | MoreIncrementalO
+ | ScheduleIncrementalO Duration
+
optParser :: CmdParamsDesc -> Parser FsckOptions
optParser desc = FsckOptions
<$> cmdParams desc
@@ -63,19 +66,22 @@ optParser desc = FsckOptions
( long "from" <> short 'f' <> metavar paramRemote
<> help "check remote"
))
- <*> switch
- ( long "incremental" <> short 'S'
- <> help "start an incremental fsck"
- )
- <*> switch
- ( long "more" <> short 'm'
- <> help "continue an incremental fsck"
- )
- <*> optional (option (str >>= parseDuration)
- ( long "incremental-schedule" <> metavar paramTime
- <> help "schedule incremental fscking"
- ))
+ <*> optional parseincremental
<*> parseKeyOptions False
+ where
+ parseincremental =
+ flag' StartIncrementalO
+ ( long "incremental" <> short 'S'
+ <> help "start an incremental fsck"
+ )
+ <|> flag' MoreIncrementalO
+ ( long "more" <> short 'm'
+ <> help "continue an incremental fsck"
+ )
+ <|> (ScheduleIncrementalO <$> option (str >>= parseDuration)
+ ( long "incremental-schedule" <> metavar paramTime
+ <> help "schedule incremental fscking"
+ ))
-- TODO: annexedMatchingOptions
@@ -83,7 +89,7 @@ seek :: FsckOptions -> CommandSeek
seek o = do
from <- Remote.byNameWithUUID (fsckFromOption o)
u <- maybe getUUID (pure . Remote.uuid) from
- i <- getIncremental u o
+ i <- prepIncremental u (incrementalOpt o)
withKeyOptions (keyOptions o) False
(\k -> startKey i k =<< getNumCopies)
(withFilesInGit $ whenAnnexed $ start from i)
@@ -511,33 +517,26 @@ getStartTime u = do
data Incremental = StartIncremental FsckDb.FsckHandle | ContIncremental FsckDb.FsckHandle | NonIncremental
-getIncremental :: UUID -> FsckOptions -> Annex Incremental
-getIncremental u o = do
- i <- maybe (return False) checkschedule (incrementalScheduleOption o)
- case (i, startIncrementalOption o, moreIncrementalOption o) of
- (False, False, False) -> return NonIncremental
- (False, True, False) -> startIncremental
- (False ,False, True) -> contIncremental
- (True, False, False) ->
- maybe startIncremental (const contIncremental)
- =<< getStartTime u
- _ -> error "Specify only one of --incremental, --more, or --incremental-schedule"
- where
- startIncremental = do
- recordStartTime u
- ifM (FsckDb.newPass u)
- ( StartIncremental <$> FsckDb.openDb u
- , error "Cannot start a new --incremental fsck pass; another fsck process is already running."
- )
- contIncremental = ContIncremental <$> FsckDb.openDb u
-
- checkschedule delta = do
- Annex.addCleanup FsckCleanup $ do
- v <- getStartTime u
- case v of
- Nothing -> noop
- Just started -> do
- now <- liftIO getPOSIXTime
- when (now - realToFrac started >= durationToPOSIXTime delta) $
- resetStartTime u
- return True
+prepIncremental :: UUID -> Maybe IncrementalOpt -> Annex Incremental
+prepIncremental _ Nothing = pure NonIncremental
+prepIncremental u (Just StartIncrementalO) = do
+ recordStartTime u
+ ifM (FsckDb.newPass u)
+ ( StartIncremental <$> FsckDb.openDb u
+ , error "Cannot start a new --incremental fsck pass; another fsck process is already running."
+ )
+prepIncremental u (Just MoreIncrementalO) =
+ ContIncremental <$> FsckDb.openDb u
+prepIncremental u (Just (ScheduleIncrementalO delta)) = do
+ Annex.addCleanup FsckCleanup $ do
+ v <- getStartTime u
+ case v of
+ Nothing -> noop
+ Just started -> do
+ now <- liftIO getPOSIXTime
+ when (now - realToFrac started >= durationToPOSIXTime delta) $
+ resetStartTime u
+ started <- getStartTime u
+ prepIncremental u $ Just $ case started of
+ Nothing -> StartIncrementalO
+ Just _ -> MoreIncrementalO