2015-01-28 20:51:40 +00:00
|
|
|
{- git-annex file locations
|
|
|
|
-
|
2017-05-15 22:10:13 +00:00
|
|
|
- Copyright 2010-2017 Joey Hess <id@joeyh.name>
|
2015-01-28 20:51:40 +00:00
|
|
|
-
|
2019-08-17 18:08:07 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-01-28 20:51:40 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.DirHashes (
|
|
|
|
Hasher,
|
|
|
|
HashLevels(..),
|
|
|
|
objectHashLevels,
|
2015-01-28 21:17:26 +00:00
|
|
|
branchHashLevels,
|
|
|
|
branchHashDir,
|
2015-01-28 20:51:40 +00:00
|
|
|
dirHashes,
|
|
|
|
hashDirMixed,
|
|
|
|
hashDirLower,
|
2016-09-29 15:33:42 +00:00
|
|
|
display_32bits_as_dir
|
2015-01-28 20:51:40 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Default
|
2019-07-29 16:41:45 +00:00
|
|
|
import Data.Bits
|
2017-05-15 22:10:13 +00:00
|
|
|
import qualified Data.ByteArray
|
2015-01-28 20:51:40 +00:00
|
|
|
|
|
|
|
import Common
|
2017-02-24 17:42:30 +00:00
|
|
|
import Key
|
2015-01-28 20:51:40 +00:00
|
|
|
import Types.GitConfig
|
|
|
|
import Types.Difference
|
2017-05-15 22:10:13 +00:00
|
|
|
import Utility.Hash
|
2019-07-28 18:27:33 +00:00
|
|
|
import Utility.MD5
|
2015-01-28 20:51:40 +00:00
|
|
|
|
|
|
|
type Hasher = Key -> FilePath
|
|
|
|
|
|
|
|
-- Number of hash levels to use. 2 is the default.
|
|
|
|
newtype HashLevels = HashLevels Int
|
|
|
|
|
|
|
|
instance Default HashLevels where
|
|
|
|
def = HashLevels 2
|
|
|
|
|
|
|
|
objectHashLevels :: GitConfig -> HashLevels
|
2015-01-28 21:17:26 +00:00
|
|
|
objectHashLevels = configHashLevels OneLevelObjectHash
|
|
|
|
|
|
|
|
branchHashLevels :: GitConfig -> HashLevels
|
|
|
|
branchHashLevels = configHashLevels OneLevelBranchHash
|
|
|
|
|
|
|
|
configHashLevels :: Difference -> GitConfig -> HashLevels
|
|
|
|
configHashLevels d config
|
2015-01-28 22:17:10 +00:00
|
|
|
| hasDifference d (annexDifferences config) = HashLevels 1
|
2015-01-28 20:51:40 +00:00
|
|
|
| otherwise = def
|
|
|
|
|
2015-01-28 21:17:26 +00:00
|
|
|
branchHashDir :: GitConfig -> Key -> String
|
2015-04-11 04:10:34 +00:00
|
|
|
branchHashDir = hashDirLower . branchHashLevels
|
2015-01-28 21:17:26 +00:00
|
|
|
|
2015-01-28 20:51:40 +00:00
|
|
|
{- Two different directory hashes may be used. The mixed case hash
|
|
|
|
- came first, and is fine, except for the problem of case-strict
|
|
|
|
- filesystems such as Linux VFAT (mounted with shortname=mixed),
|
|
|
|
- which do not allow using a directory "XX" when "xx" already exists.
|
2018-11-30 16:37:45 +00:00
|
|
|
- To support that, some git-annex repositories use the lower case-hash.
|
|
|
|
- All special remotes use the lower-case hash for new data, but old data
|
|
|
|
- may still used the mixed case hash. -}
|
2015-01-28 20:51:40 +00:00
|
|
|
dirHashes :: [HashLevels -> Hasher]
|
|
|
|
dirHashes = [hashDirLower, hashDirMixed]
|
|
|
|
|
|
|
|
hashDirs :: HashLevels -> Int -> String -> FilePath
|
|
|
|
hashDirs (HashLevels 1) sz s = addTrailingPathSeparator $ take sz s
|
|
|
|
hashDirs _ sz s = addTrailingPathSeparator $ take sz s </> drop sz s
|
|
|
|
|
2017-05-15 22:10:13 +00:00
|
|
|
hashDirLower :: HashLevels -> Hasher
|
2019-01-16 20:09:53 +00:00
|
|
|
hashDirLower n k = hashDirs n 3 $ take 6 $ show $ md5 $ serializeKey' $ nonChunkKey k
|
2017-05-15 22:10:13 +00:00
|
|
|
|
|
|
|
{- This was originally using Data.Hash.MD5 from MissingH. This new version
|
|
|
|
- is faster, but ugly as it has to replicate the 4 Word32's that produced. -}
|
2015-01-28 20:51:40 +00:00
|
|
|
hashDirMixed :: HashLevels -> Hasher
|
2017-05-15 22:10:13 +00:00
|
|
|
hashDirMixed n k = hashDirs n 2 $ take 4 $ concatMap display_32bits_as_dir $
|
|
|
|
encodeWord32 $ map fromIntegral $ Data.ByteArray.unpack $
|
2019-01-16 20:09:53 +00:00
|
|
|
Utility.Hash.md5 $ serializeKey' $ nonChunkKey k
|
2015-01-28 20:51:40 +00:00
|
|
|
where
|
2017-05-15 22:10:13 +00:00
|
|
|
encodeWord32 (b1:b2:b3:b4:rest) =
|
|
|
|
(shiftL b4 24 .|. shiftL b3 16 .|. shiftL b2 8 .|. b1)
|
|
|
|
: encodeWord32 rest
|
|
|
|
encodeWord32 _ = []
|