use NonEmpty for dirHashes

This avoids 4 uses of head.
This commit is contained in:
Joey Hess 2024-09-26 18:15:00 -04:00
parent 43f31121a5
commit 10216b44d2
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
8 changed files with 34 additions and 22 deletions

View file

@ -307,7 +307,7 @@ bup2GitRemote r
| otherwise = Git.Construct.fromUrl $ "ssh://" ++ host ++ slash dir
where
bits = splitc ':' r
host = Prelude.head bits
host = fromMaybe "" $ headMaybe bits
dir = intercalate ":" $ drop 1 bits
-- "host:~user/dir" is not supported specially by bup;
-- "host:dir" is relative to the home directory;

View file

@ -17,6 +17,7 @@ module Remote.Directory (
import qualified Data.ByteString.Lazy as L
import qualified Data.Map as M
import qualified Data.List.NonEmpty as NE
import qualified System.FilePath.ByteString as P
import Data.Default
import System.PosixCompat.Files (isRegularFile, deviceID)
@ -166,8 +167,11 @@ directorySetup _ mu _ c gc = do
{- Locations to try to access a given Key in the directory.
- We try more than one since we used to write to different hash
- directories. -}
locations :: RawFilePath -> Key -> [RawFilePath]
locations d k = map (d P.</>) (keyPaths k)
locations :: RawFilePath -> Key -> NE.NonEmpty RawFilePath
locations d k = NE.map (d P.</>) (keyPaths k)
locations' :: RawFilePath -> Key -> [RawFilePath]
locations' d k = NE.toList (locations d k)
{- Returns the location off a Key in the directory. If the key is
- present, returns the location that is actually used, otherwise
@ -175,8 +179,9 @@ locations d k = map (d P.</>) (keyPaths k)
getLocation :: RawFilePath -> Key -> IO RawFilePath
getLocation d k = do
let locs = locations d k
fromMaybe (Prelude.head locs)
<$> firstM (doesFileExist . fromRawFilePath) locs
fromMaybe (NE.head locs)
<$> firstM (doesFileExist . fromRawFilePath)
(NE.toList locs)
{- Directory where the file(s) for a key are stored. -}
storeDir :: RawFilePath -> Key -> RawFilePath
@ -246,7 +251,7 @@ finalizeStoreGeneric d tmp dest = do
dest' = fromRawFilePath dest
retrieveKeyFileM :: RawFilePath -> ChunkConfig -> CopyCoWTried -> Retriever
retrieveKeyFileM d (LegacyChunks _) _ = Legacy.retrieve locations d
retrieveKeyFileM d (LegacyChunks _) _ = Legacy.retrieve locations' d
retrieveKeyFileM d NoChunks cow = fileRetriever' $ \dest k p iv -> do
src <- liftIO $ fromRawFilePath <$> getLocation d k
void $ liftIO $ fileCopier cow src (fromRawFilePath dest) p iv
@ -311,8 +316,8 @@ removeDirGeneric removeemptyparents topdir dir = do
goparents (upFrom subdir) =<< tryIO (removeDirectory d)
checkPresentM :: RawFilePath -> ChunkConfig -> CheckPresent
checkPresentM d (LegacyChunks _) k = Legacy.checkKey d locations k
checkPresentM d _ k = checkPresentGeneric d (locations d k)
checkPresentM d (LegacyChunks _) k = Legacy.checkKey d locations' k
checkPresentM d _ k = checkPresentGeneric d (locations' d k)
checkPresentGeneric :: RawFilePath -> [RawFilePath] -> Annex Bool
checkPresentGeneric d ps = checkPresentGeneric' d $

View file

@ -51,6 +51,7 @@ import Annex.Verify
import qualified Utility.RawFilePath as R
import qualified Data.Map as M
import qualified Data.List.NonEmpty as NE
remote :: RemoteType
remote = specialRemoteType $ RemoteType
@ -222,7 +223,7 @@ rsyncSetup _ mu _ c gc = do
store :: RsyncOpts -> Key -> FilePath -> MeterUpdate -> Annex ()
store o k src meterupdate = storeGeneric o meterupdate basedest populatedest
where
basedest = fromRawFilePath $ Prelude.head (keyPaths k)
basedest = fromRawFilePath $ NE.head (keyPaths k)
populatedest dest = liftIO $ if canrename
then do
R.rename (toRawFilePath src) (toRawFilePath dest)

View file

@ -22,6 +22,7 @@ import Utility.Split
import Data.Default
import System.FilePath.Posix
import qualified Data.List.NonEmpty as NE
type RsyncUrl = String
@ -42,7 +43,7 @@ mkRsyncUrl :: RsyncOpts -> FilePath -> RsyncUrl
mkRsyncUrl o f = rsyncUrl o </> rsyncEscape o f
rsyncUrls :: RsyncOpts -> Key -> [RsyncUrl]
rsyncUrls o k = map use dirHashes
rsyncUrls o k = map use (NE.toList dirHashes)
where
use h = rsyncUrl o </> hash h </> rsyncEscape o (f </> f)
f = fromRawFilePath (keyFile k)