]> dgit.raspbian.org Git - git-annex.git/commitdiff
avoid partial functions
authorJoey Hess <joeyh@joeyh.name>
Tue, 30 Jul 2024 15:27:20 +0000 (11:27 -0400)
committerJoey Hess <joeyh@joeyh.name>
Tue, 30 Jul 2024 15:28:44 +0000 (11:28 -0400)
This is horrible old code and ghc has started to warn about head and
tail. Rewrote it to avoid all partial functions except !! and guarded
uses of !! with length checks.

Upgrade/V1.hs

index 0c5666c2ae454f2df570ef84875c1e457c07ed40..f4a7ccd83d44a53d2d2ed2147f760435cd877dfd 100644 (file)
@@ -1,6 +1,6 @@
 {- git-annex v1 -> v2 upgrade support
  -
- - Copyright 2011 Joey Hess <id@joeyh.name>
+ - Copyright 2011-2024 Joey Hess <id@joeyh.name>
  -
  - Licensed under the GNU AGPL version 3 or higher.
  -}
@@ -18,6 +18,7 @@ import qualified Data.ByteString.Short as S (toShort, fromShort)
 import qualified Data.ByteString.Lazy as L
 import qualified System.FilePath.ByteString as P
 import System.PosixCompat.Files (isRegularFile)
+import Text.Read
 
 import Annex.Common
 import Types.Upgrade
@@ -149,13 +150,16 @@ oldlog2key l
 -- WORM backend keys: "WORM:mtime:size:filename"
 -- all the rest: "backend:key"
 --
--- If the file looks like "WORM:XXX-...", then it was created by mixing
+-- If the file looks like "WORM:FOO-...", then it was created by mixing
 -- v2 and v1; that infelicity is worked around by treating the value
 -- as the v2 key that it is.
 readKey1 :: String -> Key
-readKey1 v
-       | mixup = fromJust $ deserializeKey $ intercalate ":" $ Prelude.tail bits
-       | otherwise = mkKey $ \d -> d
+readKey1 = fromMaybe (giveup "unable to parse v0 key") . readKey1'
+
+readKey1' :: String -> Maybe Key
+readKey1' v
+       | mixup = deserializeKey $ intercalate ":" $ drop 1 bits
+       | otherwise = Just $ mkKey $ \d -> d
                { keyName = S.toShort (encodeBS n)
                , keyVariety = parseKeyVariety (encodeBS b)
                , keySize = s
@@ -166,13 +170,13 @@ readKey1 v
        b = Prelude.head bits
        n = intercalate ":" $ drop (if wormy then 3 else 1) bits
        t = if wormy
-               then Just (Prelude.read (bits !! 1) :: EpochTime)
+               then readMaybe (bits !! 1) :: Maybe EpochTime
                else Nothing
-       s = if wormy
-               then Just (Prelude.read (bits !! 2) :: Integer)
+       s = if wormy && length bits > 2
+               then readMaybe (bits !! 2) :: Maybe Integer
                else Nothing
-       wormy = Prelude.head bits == "WORM"
-       mixup = wormy && isUpper (Prelude.head $ bits !! 1)
+       wormy = length bits > 1 && headMaybe bits == Just "WORM"
+       mixup = wormy && fromMaybe False (isUpper <$> (headMaybe $ bits !! 1))
 
 showKey1 :: Key -> String
 showKey1 k = intercalate ":" $ filter (not . null)