git-annex/Locations.hs

54 lines
1.7 KiB
Haskell
Raw Normal View History

{- git-annex file locations
-}
2010-10-11 21:52:46 +00:00
module Locations (
gitStateDir,
2010-10-13 00:04:36 +00:00
stateLoc,
keyFile,
2010-10-13 07:41:12 +00:00
fileKey,
2010-10-13 04:58:59 +00:00
annexLocation,
annexLocationRelative
2010-10-11 21:52:46 +00:00
) where
2010-10-13 00:04:36 +00:00
import Data.String.Utils
import Types
2010-10-14 06:36:41 +00:00
import qualified GitRepo as Git
2010-10-11 04:23:49 +00:00
{- Long-term, cross-repo state is stored in files inside the .git-annex
2010-10-12 04:53:42 +00:00
- directory, in the git repository's working tree. -}
stateLoc = ".git-annex"
2010-10-14 06:36:41 +00:00
gitStateDir :: Git.Repo -> FilePath
gitStateDir repo = (Git.workTree repo) ++ "/" ++ stateLoc ++ "/"
2010-10-13 00:04:36 +00:00
{- An annexed file's content is stored in
2010-10-13 20:21:50 +00:00
- /path/to/repo/.git/annex/<backend>/<key>
-
- (That allows deriving the key and backend by looking at the symlink to it.)
-}
2010-10-14 06:36:41 +00:00
annexLocation :: Git.Repo -> Backend -> Key -> FilePath
2010-10-13 20:21:50 +00:00
annexLocation r backend key =
2010-10-14 06:36:41 +00:00
(Git.workTree r) ++ "/" ++ (annexLocationRelative r backend key)
2010-10-13 05:04:06 +00:00
2010-10-13 05:36:20 +00:00
{- Annexed file's location relative to the gitWorkTree -}
2010-10-14 06:36:41 +00:00
annexLocationRelative :: Git.Repo -> Backend -> Key -> FilePath
2010-10-13 20:21:50 +00:00
annexLocationRelative r backend key =
2010-10-14 06:36:41 +00:00
Git.dir r ++ "/annex/" ++ (name backend) ++ "/" ++ (keyFile key)
2010-10-13 07:41:12 +00:00
{- Converts a key into a filename fragment.
-
- Escape "/" in the key name, to keep a flat tree of files and avoid
- issues with keys containing "/../" or ending with "/" etc.
-
- "/" is escaped to "%" because it's short and rarely used, and resembles
- a slash
- "%" is escaped to "&s", and "&" to "&a"; this ensures that the mapping
- is one to one.
- -}
keyFile :: Key -> FilePath
keyFile key = replace "/" "%" $ replace "%" "&s" $ replace "&" "&a" $ show key
2010-10-14 03:31:08 +00:00
{- Reverses keyFile, converting a filename fragment (ie, the basename of
- the symlink target) into a key. -}
2010-10-13 07:41:12 +00:00
fileKey :: FilePath -> Key
fileKey file = Key $ replace "&a" "&" $ replace "&s" "%" $ replace "%" "/" file