handle newlines on keys
This commit is contained in:
parent
603e01e96c
commit
570899ed0c
1 changed files with 27 additions and 16 deletions
43
Backend.hs
43
Backend.hs
|
@ -23,18 +23,12 @@ module Backend (
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import System.Directory
|
import System.Directory
|
||||||
|
import Data.String.Utils
|
||||||
import Locations
|
import Locations
|
||||||
import GitRepo
|
import GitRepo
|
||||||
import Utility
|
import Utility
|
||||||
import Types
|
import Types
|
||||||
|
|
||||||
{- Name of state file that holds the key for an annexed file,
|
|
||||||
- using a given backend. -}
|
|
||||||
backendFile :: State -> Backend -> FilePath -> String
|
|
||||||
backendFile state backend file =
|
|
||||||
gitStateDir (repo state) ++ (gitRelative (repo state) file) ++
|
|
||||||
"." ++ (name backend)
|
|
||||||
|
|
||||||
{- Attempts to store a file in one of the backends, and returns
|
{- Attempts to store a file in one of the backends, and returns
|
||||||
- its key. -}
|
- its key. -}
|
||||||
storeFile :: State -> FilePath -> IO (Maybe Key)
|
storeFile :: State -> FilePath -> IO (Maybe Key)
|
||||||
|
@ -49,14 +43,10 @@ storeFile' (b:bs) state file = do
|
||||||
if (not stored)
|
if (not stored)
|
||||||
then nextbackend
|
then nextbackend
|
||||||
else do
|
else do
|
||||||
bookkeeping key
|
recordKey state b file key
|
||||||
return $ Just key
|
return $ Just key
|
||||||
where
|
where
|
||||||
nextbackend = storeFile' bs state file
|
nextbackend = storeFile' bs state file
|
||||||
backendfile = backendFile state b file
|
|
||||||
bookkeeping key = do
|
|
||||||
createDirectoryIfMissing True (parentDir backendfile)
|
|
||||||
writeFile backendfile key
|
|
||||||
|
|
||||||
{- Attempts to retrieve an file from one of the backends, saving it to
|
{- Attempts to retrieve an file from one of the backends, saving it to
|
||||||
- a specified location. -}
|
- a specified location. -}
|
||||||
|
@ -81,10 +71,6 @@ dropFile state file = do
|
||||||
removeFile $ backendFile state b file
|
removeFile $ backendFile state b file
|
||||||
return $ Just key
|
return $ Just key
|
||||||
|
|
||||||
{- Looks up the key a backend uses for an already annexed file. -}
|
|
||||||
lookupKey :: Backend -> State -> FilePath -> IO Key
|
|
||||||
lookupKey backend state file = readFile (backendFile state backend file)
|
|
||||||
|
|
||||||
{- Looks up the backend used for an already annexed file. -}
|
{- Looks up the backend used for an already annexed file. -}
|
||||||
lookupBackend :: State -> FilePath -> IO (Maybe Backend)
|
lookupBackend :: State -> FilePath -> IO (Maybe Backend)
|
||||||
lookupBackend state file = lookupBackend' (backends state) state file
|
lookupBackend state file = lookupBackend' (backends state) state file
|
||||||
|
@ -97,6 +83,31 @@ lookupBackend' (b:bs) state file = do
|
||||||
else
|
else
|
||||||
lookupBackend' bs state file
|
lookupBackend' bs state file
|
||||||
|
|
||||||
|
{- Name of state file that holds the key for an annexed file,
|
||||||
|
- using a given backend. -}
|
||||||
|
backendFile :: State -> Backend -> FilePath -> String
|
||||||
|
backendFile state backend file =
|
||||||
|
gitStateDir (repo state) ++ (gitRelative (repo state) file) ++
|
||||||
|
"." ++ (name backend)
|
||||||
|
|
||||||
{- Checks if a file is available via a given backend. -}
|
{- Checks if a file is available via a given backend. -}
|
||||||
checkBackend :: Backend -> State -> FilePath -> IO (Bool)
|
checkBackend :: Backend -> State -> FilePath -> IO (Bool)
|
||||||
checkBackend backend state file = doesFileExist $ backendFile state backend file
|
checkBackend backend state file = doesFileExist $ backendFile state backend file
|
||||||
|
|
||||||
|
{- Looks up the key a backend uses for an already annexed file. -}
|
||||||
|
lookupKey :: Backend -> State -> FilePath -> IO Key
|
||||||
|
lookupKey backend state file = do
|
||||||
|
k <- readFile (backendFile state backend file)
|
||||||
|
return $ chomp k
|
||||||
|
where
|
||||||
|
chomp s = if (endswith s "\n")
|
||||||
|
then (reverse . (drop 1) . reverse) s
|
||||||
|
else s
|
||||||
|
|
||||||
|
{- Records the key a backend uses for an annexed file. -}
|
||||||
|
recordKey :: State -> Backend -> FilePath -> Key -> IO ()
|
||||||
|
recordKey state backend file key = do
|
||||||
|
createDirectoryIfMissing True (parentDir record)
|
||||||
|
writeFile record $ key ++ "\n"
|
||||||
|
where
|
||||||
|
record = backendFile state backend file
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue