Use SHA library for files less than 50 kb in size, at which point it's faster than forking the more optimised external program.
This commit is contained in:
parent
d3be479093
commit
40729e7fa2
2 changed files with 33 additions and 23 deletions
|
@ -42,28 +42,16 @@ genBackendE size = do
|
||||||
, getKey = keyValueE size
|
, getKey = keyValueE size
|
||||||
}
|
}
|
||||||
|
|
||||||
shaCommand :: SHASize -> Either (L.ByteString -> String) String
|
|
||||||
shaCommand sz
|
|
||||||
| sz == 1 = use SysConfig.sha1 sha1
|
|
||||||
| sz == 256 = use SysConfig.sha256 sha256
|
|
||||||
| sz == 224 = use SysConfig.sha224 sha224
|
|
||||||
| sz == 384 = use SysConfig.sha384 sha384
|
|
||||||
| sz == 512 = use SysConfig.sha512 sha512
|
|
||||||
| otherwise = error $ "bad sha size " ++ show sz
|
|
||||||
where
|
|
||||||
use Nothing sha = Left $ showDigest . sha
|
|
||||||
use (Just c) _ = Right c
|
|
||||||
|
|
||||||
shaName :: SHASize -> String
|
shaName :: SHASize -> String
|
||||||
shaName size = "SHA" ++ show size
|
shaName size = "SHA" ++ show size
|
||||||
|
|
||||||
shaNameE :: SHASize -> String
|
shaNameE :: SHASize -> String
|
||||||
shaNameE size = shaName size ++ "E"
|
shaNameE size = shaName size ++ "E"
|
||||||
|
|
||||||
shaN :: SHASize -> FilePath -> Annex String
|
shaN :: SHASize -> FilePath -> Integer -> Annex String
|
||||||
shaN size file = do
|
shaN shasize file filesize = do
|
||||||
showAction "checksum"
|
showAction "checksum"
|
||||||
case shaCommand size of
|
case shaCommand shasize filesize of
|
||||||
Left sha -> liftIO $ sha <$> L.readFile file
|
Left sha -> liftIO $ sha <$> L.readFile file
|
||||||
Right command -> liftIO $ runcommand command
|
Right command -> liftIO $ runcommand command
|
||||||
where
|
where
|
||||||
|
@ -74,16 +62,34 @@ shaN size file = do
|
||||||
then error $ command ++ " parse error"
|
then error $ command ++ " parse error"
|
||||||
else return sha
|
else return sha
|
||||||
|
|
||||||
|
shaCommand :: SHASize -> Integer -> Either (L.ByteString -> String) String
|
||||||
|
shaCommand shasize filesize
|
||||||
|
| shasize == 1 = use SysConfig.sha1 sha1
|
||||||
|
| shasize == 256 = use SysConfig.sha256 sha256
|
||||||
|
| shasize == 224 = use SysConfig.sha224 sha224
|
||||||
|
| shasize == 384 = use SysConfig.sha384 sha384
|
||||||
|
| shasize == 512 = use SysConfig.sha512 sha512
|
||||||
|
| otherwise = error $ "bad sha size " ++ show shasize
|
||||||
|
where
|
||||||
|
use Nothing sha = Left $ showDigest . sha
|
||||||
|
use (Just c) sha
|
||||||
|
-- use builtin, but slower sha for small files
|
||||||
|
-- benchmarking indicates it's faster up to
|
||||||
|
-- and slightly beyond 50 kb files
|
||||||
|
| filesize < 51200 = use Nothing sha
|
||||||
|
| otherwise = Right c
|
||||||
|
|
||||||
{- A key is a checksum of its contents. -}
|
{- A key is a checksum of its contents. -}
|
||||||
keyValue :: SHASize -> KeySource -> Annex (Maybe Key)
|
keyValue :: SHASize -> KeySource -> Annex (Maybe Key)
|
||||||
keyValue size source = do
|
keyValue shasize source = do
|
||||||
let file = contentLocation source
|
let file = contentLocation source
|
||||||
s <- shaN size file
|
|
||||||
stat <- liftIO $ getFileStatus file
|
stat <- liftIO $ getFileStatus file
|
||||||
|
let filesize = fromIntegral $ fileSize stat
|
||||||
|
s <- shaN shasize file filesize
|
||||||
return $ Just $ stubKey
|
return $ Just $ stubKey
|
||||||
{ keyName = s
|
{ keyName = s
|
||||||
, keyBackendName = shaName size
|
, keyBackendName = shaName shasize
|
||||||
, keySize = Just $ fromIntegral $ fileSize stat
|
, keySize = Just filesize
|
||||||
}
|
}
|
||||||
|
|
||||||
{- Extension preserving keys. -}
|
{- Extension preserving keys. -}
|
||||||
|
@ -106,10 +112,12 @@ keyValueE size source = keyValue size source >>= maybe (return Nothing) addE
|
||||||
checkKeyChecksum :: SHASize -> Key -> FilePath -> Annex Bool
|
checkKeyChecksum :: SHASize -> Key -> FilePath -> Annex Bool
|
||||||
checkKeyChecksum size key file = do
|
checkKeyChecksum size key file = do
|
||||||
fast <- Annex.getState Annex.fast
|
fast <- Annex.getState Annex.fast
|
||||||
present <- liftIO $ doesFileExist file
|
mstat <- liftIO $ catchMaybeIO $ getFileStatus file
|
||||||
if not present || fast
|
case (mstat, fast) of
|
||||||
then return True
|
(Just stat, False) -> do
|
||||||
else check <$> shaN size file
|
let filesize = fromIntegral $ fileSize stat
|
||||||
|
check <$> shaN size file filesize
|
||||||
|
_ -> return True
|
||||||
where
|
where
|
||||||
check s
|
check s
|
||||||
| s == dropExtension (keyName key) = True
|
| s == dropExtension (keyName key) = True
|
||||||
|
|
2
debian/changelog
vendored
2
debian/changelog
vendored
|
@ -7,6 +7,8 @@ git-annex (3.20120630) UNRELEASED; urgency=low
|
||||||
* When shaNsum commands cannot be found, use the Haskell SHA library
|
* When shaNsum commands cannot be found, use the Haskell SHA library
|
||||||
(already a dependency) to do the checksumming. This may be slower,
|
(already a dependency) to do the checksumming. This may be slower,
|
||||||
but avoids portability problems.
|
but avoids portability problems.
|
||||||
|
* Use SHA library for files less than 50 kb in size, at which point it's
|
||||||
|
faster than forking the more optimised external program.
|
||||||
|
|
||||||
-- Joey Hess <joeyh@debian.org> Sun, 01 Jul 2012 15:04:37 -0400
|
-- Joey Hess <joeyh@debian.org> Sun, 01 Jul 2012 15:04:37 -0400
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue