2011-01-16 16:05:05 -04:00
|
|
|
{- git-annex file content managing
|
2010-10-27 16:53:54 -04:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
2010-10-14 03:40:26 -04:00
|
|
|
|
2011-01-16 16:05:05 -04:00
|
|
|
module Content (
|
|
|
|
inAnnex,
|
|
|
|
calcGitLink,
|
|
|
|
logStatus,
|
|
|
|
getViaTmp,
|
2011-03-22 17:27:04 -04:00
|
|
|
getViaTmpUnchecked,
|
|
|
|
checkDiskSpace,
|
2011-01-16 16:05:05 -04:00
|
|
|
preventWrite,
|
|
|
|
allowWrite,
|
|
|
|
moveAnnex,
|
|
|
|
removeAnnex,
|
|
|
|
fromAnnex,
|
|
|
|
moveBad,
|
|
|
|
getKeysPresent
|
|
|
|
) where
|
2010-10-14 03:40:26 -04:00
|
|
|
|
2010-11-22 17:51:55 -04:00
|
|
|
import System.IO.Error (try)
|
2010-10-14 03:40:26 -04:00
|
|
|
import System.Directory
|
|
|
|
import Control.Monad.State (liftIO)
|
2010-10-16 21:03:25 -04:00
|
|
|
import System.Path
|
2011-03-03 15:22:53 -04:00
|
|
|
import Control.Monad (when, unless, filterM)
|
2010-11-08 15:15:21 -04:00
|
|
|
import System.Posix.Files
|
2010-11-13 14:59:27 -04:00
|
|
|
import System.FilePath
|
2011-03-15 21:34:13 -04:00
|
|
|
import Data.Maybe
|
2010-10-16 16:20:49 -04:00
|
|
|
|
2010-10-14 03:40:26 -04:00
|
|
|
import Types
|
|
|
|
import Locations
|
2010-10-17 12:08:59 -04:00
|
|
|
import LocationLog
|
2010-10-14 03:40:26 -04:00
|
|
|
import UUID
|
|
|
|
import qualified GitRepo as Git
|
|
|
|
import qualified Annex
|
2010-10-16 21:03:25 -04:00
|
|
|
import Utility
|
2011-03-22 17:27:04 -04:00
|
|
|
import StatFS
|
|
|
|
import Key
|
2011-03-23 01:06:14 -04:00
|
|
|
import DataUnits
|
2011-03-27 21:43:25 -04:00
|
|
|
import Config
|
2010-10-25 19:17:11 -04:00
|
|
|
|
2011-01-27 17:00:32 -04:00
|
|
|
{- Checks if a given key is currently present in the gitAnnexLocation. -}
|
2010-10-14 19:36:11 -04:00
|
|
|
inAnnex :: Key -> Annex Bool
|
|
|
|
inAnnex key = do
|
2010-10-14 14:38:29 -04:00
|
|
|
g <- Annex.gitRepo
|
2010-10-31 23:21:16 -04:00
|
|
|
when (Git.repoIsUrl g) $ error "inAnnex cannot check remote repo"
|
2011-01-27 17:00:32 -04:00
|
|
|
liftIO $ doesFileExist $ gitAnnexLocation g key
|
2010-10-16 13:59:48 -04:00
|
|
|
|
2010-10-16 21:03:25 -04:00
|
|
|
{- Calculates the relative path to use to link a file to a key. -}
|
|
|
|
calcGitLink :: FilePath -> Key -> Annex FilePath
|
|
|
|
calcGitLink file key = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
cwd <- liftIO $ getCurrentDirectory
|
2010-11-22 15:46:57 -04:00
|
|
|
let absfile = case absNormPath cwd file of
|
2010-10-16 21:03:25 -04:00
|
|
|
Just f -> f
|
2011-03-12 15:30:17 -04:00
|
|
|
Nothing -> error $ "unable to normalize " ++ file
|
2011-03-03 14:51:57 -04:00
|
|
|
return $ relPathDirToDir (parentDir absfile)
|
|
|
|
(Git.workTree g) </> ".git" </> annexLocation key
|
2010-10-17 12:08:59 -04:00
|
|
|
|
2011-03-03 15:22:53 -04:00
|
|
|
{- Updates the LocationLog when a key's presence changes.
|
|
|
|
-
|
|
|
|
- Note that the LocationLog is not updated in bare repositories.
|
|
|
|
- Operations that change a bare repository should be done from
|
|
|
|
- a non-bare repository, and the LocationLog in that repository be
|
|
|
|
- updated instead. -}
|
2010-10-17 12:08:59 -04:00
|
|
|
logStatus :: Key -> LogStatus -> Annex ()
|
|
|
|
logStatus key status = do
|
|
|
|
g <- Annex.gitRepo
|
2011-03-03 15:22:53 -04:00
|
|
|
unless (Git.repoIsLocalBare g) $ do
|
|
|
|
u <- getUUID g
|
|
|
|
logfile <- liftIO $ logChange g key u status
|
|
|
|
Annex.queue "add" [Param "--"] logfile
|
2010-10-17 12:08:59 -04:00
|
|
|
|
2010-10-23 14:26:38 -04:00
|
|
|
{- Runs an action, passing it a temporary filename to download,
|
|
|
|
- and if the action succeeds, moves the temp file into
|
|
|
|
- the annex as a key's content. -}
|
2010-10-25 14:10:38 -04:00
|
|
|
getViaTmp :: Key -> (FilePath -> Annex Bool) -> Annex Bool
|
2010-10-23 14:26:38 -04:00
|
|
|
getViaTmp key action = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-28 14:10:50 -04:00
|
|
|
let tmp = gitAnnexTmpLocation g key
|
2011-03-22 17:27:04 -04:00
|
|
|
|
|
|
|
-- Check that there is enough free disk space.
|
|
|
|
-- When the temp file already exists, count the space
|
|
|
|
-- it is using as free.
|
|
|
|
e <- liftIO $ doesFileExist tmp
|
|
|
|
if e
|
|
|
|
then do
|
|
|
|
stat <- liftIO $ getFileStatus tmp
|
|
|
|
checkDiskSpace' (fromIntegral $ fileSize stat) key
|
|
|
|
else checkDiskSpace key
|
|
|
|
|
|
|
|
getViaTmpUnchecked key action
|
|
|
|
|
|
|
|
{- Like getViaTmp, but does not check that there is enough disk space
|
|
|
|
- for the incoming key. For use when the key content is already on disk
|
|
|
|
- and not being copied into place. -}
|
|
|
|
getViaTmpUnchecked :: Key -> (FilePath -> Annex Bool) -> Annex Bool
|
|
|
|
getViaTmpUnchecked key action = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let tmp = gitAnnexTmpLocation g key
|
|
|
|
|
2010-10-23 14:26:38 -04:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir tmp)
|
|
|
|
success <- action tmp
|
2010-11-22 15:46:57 -04:00
|
|
|
if success
|
2010-10-23 14:26:38 -04:00
|
|
|
then do
|
2010-11-08 19:26:37 -04:00
|
|
|
moveAnnex key tmp
|
2010-10-23 14:26:38 -04:00
|
|
|
logStatus key ValuePresent
|
2010-10-25 14:10:38 -04:00
|
|
|
return True
|
2010-10-23 14:26:38 -04:00
|
|
|
else do
|
2010-10-25 15:44:27 -04:00
|
|
|
-- the tmp file is left behind, in case caller wants
|
|
|
|
-- to resume its transfer
|
2010-10-25 14:10:38 -04:00
|
|
|
return False
|
2010-10-23 14:26:38 -04:00
|
|
|
|
2011-03-22 17:27:04 -04:00
|
|
|
{- Checks that there is disk space available to store a given key,
|
|
|
|
- throwing an error if not. -}
|
|
|
|
checkDiskSpace :: Key -> Annex ()
|
|
|
|
checkDiskSpace = checkDiskSpace' 0
|
|
|
|
|
|
|
|
checkDiskSpace' :: Integer -> Key -> Annex ()
|
|
|
|
checkDiskSpace' adjustment key = do
|
|
|
|
g <- Annex.gitRepo
|
2011-03-27 21:43:25 -04:00
|
|
|
r <- getConfig g "diskreserve" ""
|
2011-03-26 14:37:39 -04:00
|
|
|
let reserve = case readSize dataUnits r of
|
|
|
|
Nothing -> megabyte
|
|
|
|
Just v -> v
|
2011-03-22 17:27:04 -04:00
|
|
|
stats <- liftIO $ getFileSystemStats (gitAnnexDir g)
|
|
|
|
case (stats, keySize key) of
|
|
|
|
(Nothing, _) -> return ()
|
|
|
|
(_, Nothing) -> return ()
|
|
|
|
(Just (FileSystemStats { fsStatBytesAvailable = have }), Just need) ->
|
2011-03-22 17:53:40 -04:00
|
|
|
if (need + reserve > have + adjustment)
|
2011-03-23 12:45:34 -04:00
|
|
|
then needmorespace (need + reserve - have - adjustment)
|
2011-03-22 17:27:04 -04:00
|
|
|
else return ()
|
|
|
|
where
|
2011-03-22 17:53:40 -04:00
|
|
|
megabyte :: Integer
|
2011-03-26 14:37:39 -04:00
|
|
|
megabyte = 1000000
|
2011-03-23 12:45:34 -04:00
|
|
|
needmorespace n = do
|
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
unless force $
|
|
|
|
error $ "not enough free space, need " ++
|
2011-03-26 14:37:39 -04:00
|
|
|
roughSize storageUnits True n ++
|
2011-03-23 12:45:34 -04:00
|
|
|
" more (use --force to override this check or adjust annex.diskreserve)"
|
2011-03-22 17:27:04 -04:00
|
|
|
|
2010-11-08 19:26:37 -04:00
|
|
|
{- Removes the write bits from a file. -}
|
|
|
|
preventWrite :: FilePath -> IO ()
|
|
|
|
preventWrite f = unsetFileMode f writebits
|
|
|
|
where
|
|
|
|
writebits = foldl unionFileModes ownerWriteMode
|
|
|
|
[groupWriteMode, otherWriteMode]
|
|
|
|
|
|
|
|
{- Turns a file's write bit back on. -}
|
|
|
|
allowWrite :: FilePath -> IO ()
|
|
|
|
allowWrite f = do
|
|
|
|
s <- getFileStatus f
|
2010-11-22 15:46:57 -04:00
|
|
|
setFileMode f $ fileMode s `unionFileModes` ownerWriteMode
|
2010-11-08 19:26:37 -04:00
|
|
|
|
2010-11-08 16:47:36 -04:00
|
|
|
{- Moves a file into .git/annex/objects/ -}
|
2010-11-08 19:26:37 -04:00
|
|
|
moveAnnex :: Key -> FilePath -> Annex ()
|
|
|
|
moveAnnex key src = do
|
2010-11-08 16:47:36 -04:00
|
|
|
g <- Annex.gitRepo
|
2011-01-27 17:00:32 -04:00
|
|
|
let dest = gitAnnexLocation g key
|
2010-11-08 19:26:37 -04:00
|
|
|
let dir = parentDir dest
|
|
|
|
liftIO $ do
|
|
|
|
createDirectoryIfMissing True dir
|
2010-11-10 13:08:29 -04:00
|
|
|
allowWrite dir -- in case the directory already exists
|
2010-11-08 19:26:37 -04:00
|
|
|
renameFile src dest
|
|
|
|
preventWrite dest
|
|
|
|
preventWrite dir
|
|
|
|
|
|
|
|
{- Removes a key's file from .git/annex/objects/ -}
|
|
|
|
removeAnnex :: Key -> Annex ()
|
|
|
|
removeAnnex key = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-27 17:00:32 -04:00
|
|
|
let file = gitAnnexLocation g key
|
2010-11-08 19:26:37 -04:00
|
|
|
let dir = parentDir file
|
|
|
|
liftIO $ do
|
|
|
|
allowWrite dir
|
|
|
|
removeFile file
|
|
|
|
removeDirectory dir
|
|
|
|
|
|
|
|
{- Moves a key's file out of .git/annex/objects/ -}
|
|
|
|
fromAnnex :: Key -> FilePath -> Annex ()
|
|
|
|
fromAnnex key dest = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-27 17:00:32 -04:00
|
|
|
let file = gitAnnexLocation g key
|
2010-11-08 19:26:37 -04:00
|
|
|
let dir = parentDir file
|
|
|
|
liftIO $ do
|
|
|
|
allowWrite dir
|
|
|
|
allowWrite file
|
|
|
|
renameFile file dest
|
|
|
|
removeDirectory dir
|
2010-11-08 16:47:36 -04:00
|
|
|
|
2010-11-13 14:59:27 -04:00
|
|
|
{- Moves a key out of .git/annex/objects/ into .git/annex/bad, and
|
2010-11-13 15:42:56 -04:00
|
|
|
- returns the file it was moved to. -}
|
2010-11-13 14:59:27 -04:00
|
|
|
moveBad :: Key -> Annex FilePath
|
|
|
|
moveBad key = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-27 17:00:32 -04:00
|
|
|
let src = gitAnnexLocation g key
|
|
|
|
let dest = gitAnnexBadDir g </> takeFileName src
|
2011-01-11 19:41:13 -04:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir dest)
|
|
|
|
liftIO $ allowWrite (parentDir src)
|
2010-11-13 15:42:56 -04:00
|
|
|
liftIO $ renameFile src dest
|
2010-11-13 15:40:12 -04:00
|
|
|
liftIO $ removeDirectory (parentDir src)
|
2011-03-03 21:34:30 -04:00
|
|
|
logStatus key ValueMissing
|
2010-11-13 14:59:27 -04:00
|
|
|
return dest
|
|
|
|
|
2010-11-08 15:15:21 -04:00
|
|
|
{- List of keys whose content exists in .git/annex/objects/ -}
|
|
|
|
getKeysPresent :: Annex [Key]
|
|
|
|
getKeysPresent = do
|
|
|
|
g <- Annex.gitRepo
|
2011-01-27 17:00:32 -04:00
|
|
|
getKeysPresent' $ gitAnnexObjectDir g
|
2010-11-08 16:47:36 -04:00
|
|
|
getKeysPresent' :: FilePath -> Annex [Key]
|
|
|
|
getKeysPresent' dir = do
|
2010-12-20 14:57:43 -04:00
|
|
|
exists <- liftIO $ doesDirectoryExist dir
|
|
|
|
if (not exists)
|
|
|
|
then return []
|
|
|
|
else do
|
2011-03-16 11:27:29 -04:00
|
|
|
-- 2 levels of hashing
|
2011-04-02 15:50:51 -04:00
|
|
|
levela <- liftIO $ dirContents dir
|
|
|
|
levelb <- liftIO $ mapM dirContents levela
|
|
|
|
contents <- liftIO $ mapM dirContents (concat levelb)
|
2011-03-16 11:27:29 -04:00
|
|
|
files <- liftIO $ filterM present (concat contents)
|
|
|
|
return $ catMaybes $ map (fileKey . takeFileName) files
|
2010-11-08 15:15:21 -04:00
|
|
|
where
|
2010-11-13 14:59:27 -04:00
|
|
|
present d = do
|
2010-12-02 21:26:37 -04:00
|
|
|
result <- try $
|
2011-03-16 11:27:29 -04:00
|
|
|
getFileStatus $ d </> takeFileName d
|
2010-12-02 21:26:37 -04:00
|
|
|
case result of
|
|
|
|
Right s -> return $ isRegularFile s
|
|
|
|
Left _ -> return False
|