2011-01-16 16:05:05 -04:00
|
|
|
|
{- git-annex file content managing
|
2010-10-27 16:53:54 -04:00
|
|
|
|
-
|
2012-12-07 17:28:23 -04:00
|
|
|
|
- Copyright 2010,2012 Joey Hess <joey@kitenet.net>
|
2010-10-27 16:53:54 -04:00
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
2010-10-14 03:40:26 -04:00
|
|
|
|
|
2011-10-04 00:40:47 -04:00
|
|
|
|
module Annex.Content (
|
2011-01-16 16:05:05 -04:00
|
|
|
|
inAnnex,
|
2011-11-09 18:33:15 -04:00
|
|
|
|
inAnnexSafe,
|
|
|
|
|
lockContent,
|
2011-01-16 16:05:05 -04:00
|
|
|
|
getViaTmp,
|
2013-01-10 11:45:44 -04:00
|
|
|
|
getViaTmpChecked,
|
2011-03-22 17:27:04 -04:00
|
|
|
|
getViaTmpUnchecked,
|
2011-04-27 20:06:07 -04:00
|
|
|
|
withTmp,
|
2011-03-22 17:27:04 -04:00
|
|
|
|
checkDiskSpace,
|
2011-01-16 16:05:05 -04:00
|
|
|
|
moveAnnex,
|
2012-12-08 17:03:39 -04:00
|
|
|
|
sendAnnex,
|
2013-01-10 11:45:44 -04:00
|
|
|
|
prepSendAnnex,
|
2011-01-16 16:05:05 -04:00
|
|
|
|
removeAnnex,
|
|
|
|
|
fromAnnex,
|
|
|
|
|
moveBad,
|
2011-06-22 21:19:52 -04:00
|
|
|
|
getKeysPresent,
|
2012-01-02 14:20:20 -04:00
|
|
|
|
saveState,
|
|
|
|
|
downloadUrl,
|
2012-01-19 17:05:39 -04:00
|
|
|
|
preseedTmp,
|
2012-04-21 14:06:36 -04:00
|
|
|
|
freezeContent,
|
|
|
|
|
thawContent,
|
2012-12-13 15:44:56 -04:00
|
|
|
|
replaceFile,
|
2013-02-15 17:58:49 -04:00
|
|
|
|
cleanObjectLoc,
|
2011-01-16 16:05:05 -04:00
|
|
|
|
) where
|
2010-10-14 03:40:26 -04:00
|
|
|
|
|
2012-03-11 18:04:58 -04:00
|
|
|
|
import System.IO.Unsafe (unsafeInterleaveIO)
|
2011-11-09 21:45:03 -04:00
|
|
|
|
|
2011-10-05 16:02:51 -04:00
|
|
|
|
import Common.Annex
|
2011-10-15 16:21:08 -04:00
|
|
|
|
import Logs.Location
|
2011-06-30 13:16:57 -04:00
|
|
|
|
import qualified Git
|
2010-10-14 03:40:26 -04:00
|
|
|
|
import qualified Annex
|
2011-10-04 00:40:47 -04:00
|
|
|
|
import qualified Annex.Queue
|
|
|
|
|
import qualified Annex.Branch
|
2012-03-22 17:09:54 -04:00
|
|
|
|
import Utility.DiskFree
|
2011-09-23 18:13:24 -04:00
|
|
|
|
import Utility.FileMode
|
2012-01-02 14:20:20 -04:00
|
|
|
|
import qualified Utility.Url as Url
|
2011-06-01 21:56:04 -04:00
|
|
|
|
import Types.Key
|
2011-07-05 20:36:43 -04:00
|
|
|
|
import Utility.DataUnits
|
2012-01-19 17:05:39 -04:00
|
|
|
|
import Utility.CopyFile
|
2011-03-27 21:43:25 -04:00
|
|
|
|
import Config
|
2011-11-09 21:45:03 -04:00
|
|
|
|
import Annex.Exception
|
2012-04-21 14:06:36 -04:00
|
|
|
|
import Git.SharedRepository
|
2012-04-21 16:59:49 -04:00
|
|
|
|
import Annex.Perms
|
2013-04-02 13:13:42 -04:00
|
|
|
|
import Annex.Link
|
2012-12-07 17:28:23 -04:00
|
|
|
|
import Annex.Content.Direct
|
|
|
|
|
|
2011-11-09 18:33:15 -04:00
|
|
|
|
{- Checks if a given key's content is currently present. -}
|
2010-10-14 19:36:11 -04:00
|
|
|
|
inAnnex :: Key -> Annex Bool
|
2012-12-07 17:28:23 -04:00
|
|
|
|
inAnnex = inAnnex' id False $ liftIO . doesFileExist
|
|
|
|
|
|
|
|
|
|
{- Generic inAnnex, handling both indirect and direct mode.
|
|
|
|
|
-
|
|
|
|
|
- In direct mode, at least one of the associated files must pass the
|
|
|
|
|
- check. Additionally, the file must be unmodified.
|
|
|
|
|
-}
|
|
|
|
|
inAnnex' :: (a -> Bool) -> a -> (FilePath -> Annex a) -> Key -> Annex a
|
|
|
|
|
inAnnex' isgood bad check key = withObjectLoc key checkindirect checkdirect
|
|
|
|
|
where
|
|
|
|
|
checkindirect loc = do
|
|
|
|
|
whenM (fromRepo Git.repoIsUrl) $
|
|
|
|
|
error "inAnnex cannot check remote repo"
|
|
|
|
|
check loc
|
|
|
|
|
checkdirect [] = return bad
|
|
|
|
|
checkdirect (loc:locs) = do
|
|
|
|
|
r <- check loc
|
|
|
|
|
if isgood r
|
2012-12-08 17:03:39 -04:00
|
|
|
|
then ifM (goodContent key loc)
|
2012-12-07 17:28:23 -04:00
|
|
|
|
( return r
|
|
|
|
|
, checkdirect locs
|
|
|
|
|
)
|
|
|
|
|
else checkdirect locs
|
2011-11-09 18:33:15 -04:00
|
|
|
|
|
|
|
|
|
{- A safer check; the key's content must not only be present, but
|
|
|
|
|
- is not in the process of being removed. -}
|
|
|
|
|
inAnnexSafe :: Key -> Annex (Maybe Bool)
|
2013-02-18 02:39:40 -04:00
|
|
|
|
inAnnexSafe = inAnnex' (fromMaybe False) (Just False) go
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
2012-12-13 00:29:06 -04:00
|
|
|
|
go f = liftIO $ openforlock f >>= check
|
2012-12-13 00:24:19 -04:00
|
|
|
|
openforlock f = catchMaybeIO $
|
|
|
|
|
openFd f ReadOnly Nothing defaultFileFlags
|
|
|
|
|
check Nothing = return is_missing
|
|
|
|
|
check (Just h) = do
|
|
|
|
|
v <- getLock h (ReadLock, AbsoluteSeek, 0, 0)
|
|
|
|
|
closeFd h
|
|
|
|
|
return $ case v of
|
|
|
|
|
Just _ -> is_locked
|
|
|
|
|
Nothing -> is_unlocked
|
|
|
|
|
is_locked = Nothing
|
|
|
|
|
is_unlocked = Just True
|
|
|
|
|
is_missing = Just False
|
2010-10-16 13:59:48 -04:00
|
|
|
|
|
2011-11-09 22:15:33 -04:00
|
|
|
|
{- Content is exclusively locked while running an action that might remove
|
|
|
|
|
- it. (If the content is not present, no locking is done.) -}
|
2011-11-09 18:33:15 -04:00
|
|
|
|
lockContent :: Key -> Annex a -> Annex a
|
2011-11-09 21:45:03 -04:00
|
|
|
|
lockContent key a = do
|
2013-04-04 15:46:33 -04:00
|
|
|
|
file <- calcRepo $ gitAnnexLocation key
|
2012-04-21 14:06:36 -04:00
|
|
|
|
bracketIO (openforlock file >>= lock) unlock a
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
{- Since files are stored with the write bit disabled, have
|
|
|
|
|
- to fiddle with permissions to open for an exclusive lock. -}
|
|
|
|
|
openforlock f = catchMaybeIO $ ifM (doesFileExist f)
|
|
|
|
|
( withModifiedFileMode f
|
|
|
|
|
(`unionFileModes` ownerWriteMode)
|
|
|
|
|
open
|
|
|
|
|
, open
|
|
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
open = openFd f ReadWrite Nothing defaultFileFlags
|
|
|
|
|
lock Nothing = return Nothing
|
|
|
|
|
lock (Just fd) = do
|
|
|
|
|
v <- tryIO $ setLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
|
case v of
|
|
|
|
|
Left _ -> error "content is locked"
|
|
|
|
|
Right _ -> return $ Just fd
|
|
|
|
|
unlock Nothing = noop
|
|
|
|
|
unlock (Just l) = closeFd l
|
2011-11-09 21:45:03 -04:00
|
|
|
|
|
support .git/annex on a different disk than the rest of the repo
The only fully supported thing is to have the main repository on one disk,
and .git/annex on another. Only commands that move data in/out of the annex
will need to copy it across devices.
There is only partial support for putting arbitrary subdirectories of
.git/annex on different devices. For one thing, but this can require more
copies to be done. For example, when .git/annex/tmp is on one device, and
.git/annex/journal on another, every journal write involves a call to
mv(1). Also, there are a few places that make hard links between various
subdirectories of .git/annex with createLink, that are not handled.
In the common case without cross-device, the new moveFile is actually
faster than renameFile, avoiding an unncessary stat to check that a file
(not a directory) is being moved. Of course if a cross-device move is
needed, it is as slow as mv(1) of the data.
2011-11-28 15:26:27 -04:00
|
|
|
|
{- Runs an action, passing it a temporary filename to get,
|
2010-10-23 14:26:38 -04:00
|
|
|
|
- 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
|
2013-01-10 11:45:44 -04:00
|
|
|
|
getViaTmp = getViaTmpChecked (return True)
|
|
|
|
|
|
|
|
|
|
{- 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 = finishGetViaTmp (return True)
|
|
|
|
|
|
|
|
|
|
getViaTmpChecked :: Annex Bool -> Key -> (FilePath -> Annex Bool) -> Annex Bool
|
|
|
|
|
getViaTmpChecked check key action = do
|
2011-11-08 15:34:10 -04:00
|
|
|
|
tmp <- fromRepo $ gitAnnexTmpLocation 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
|
2012-04-20 14:57:57 -04:00
|
|
|
|
alreadythere <- if e
|
|
|
|
|
then fromIntegral . fileSize <$> liftIO (getFileStatus tmp)
|
|
|
|
|
else return 0
|
|
|
|
|
ifM (checkDiskSpace Nothing key alreadythere)
|
|
|
|
|
( do
|
2012-04-21 14:06:36 -04:00
|
|
|
|
when e $ thawContent tmp
|
2013-01-10 11:45:44 -04:00
|
|
|
|
finishGetViaTmp check key action
|
2012-04-20 14:57:57 -04:00
|
|
|
|
, return False
|
|
|
|
|
)
|
2011-03-22 17:27:04 -04:00
|
|
|
|
|
2013-01-10 11:45:44 -04:00
|
|
|
|
finishGetViaTmp :: Annex Bool -> Key -> (FilePath -> Annex Bool) -> Annex Bool
|
|
|
|
|
finishGetViaTmp check key action = do
|
2012-12-08 13:13:36 -04:00
|
|
|
|
tmpfile <- prepTmp key
|
2013-01-10 11:45:44 -04:00
|
|
|
|
ifM (action tmpfile <&&> check)
|
2012-03-14 17:43:34 -04:00
|
|
|
|
( do
|
2012-12-08 13:13:36 -04:00
|
|
|
|
moveAnnex key tmpfile
|
2011-07-01 15:24:07 -04:00
|
|
|
|
logStatus key InfoPresent
|
2010-10-25 14:10:38 -04:00
|
|
|
|
return True
|
2012-03-14 17:43:34 -04:00
|
|
|
|
, 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
|
2012-03-14 17:43:34 -04:00
|
|
|
|
)
|
2010-10-23 14:26:38 -04:00
|
|
|
|
|
2013-01-10 11:45:44 -04:00
|
|
|
|
prepTmp :: Key -> Annex FilePath
|
|
|
|
|
prepTmp key = do
|
|
|
|
|
tmp <- fromRepo $ gitAnnexTmpLocation key
|
|
|
|
|
createAnnexDirectory (parentDir tmp)
|
|
|
|
|
return tmp
|
|
|
|
|
|
2011-04-27 20:06:07 -04:00
|
|
|
|
{- Creates a temp file, runs an action on it, and cleans up the temp file. -}
|
|
|
|
|
withTmp :: Key -> (FilePath -> Annex a) -> Annex a
|
|
|
|
|
withTmp key action = do
|
2011-07-15 03:12:05 -04:00
|
|
|
|
tmp <- prepTmp key
|
2011-04-27 20:06:07 -04:00
|
|
|
|
res <- action tmp
|
2012-06-06 13:13:13 -04:00
|
|
|
|
liftIO $ nukeFile tmp
|
2011-04-27 20:06:07 -04:00
|
|
|
|
return res
|
|
|
|
|
|
2011-03-22 17:27:04 -04:00
|
|
|
|
{- Checks that there is disk space available to store a given key,
|
2012-04-20 14:57:57 -04:00
|
|
|
|
- in a destination (or the annex) printing a warning if not. -}
|
|
|
|
|
checkDiskSpace :: Maybe FilePath -> Key -> Integer -> Annex Bool
|
|
|
|
|
checkDiskSpace destination key alreadythere = do
|
2013-01-01 13:52:47 -04:00
|
|
|
|
reserve <- annexDiskReserve <$> Annex.getGitConfig
|
2012-04-20 14:57:57 -04:00
|
|
|
|
free <- liftIO . getDiskFree =<< dir
|
|
|
|
|
force <- Annex.getState Annex.force
|
2012-03-22 17:09:54 -04:00
|
|
|
|
case (free, keySize key) of
|
2012-04-20 14:57:57 -04:00
|
|
|
|
(Just have, Just need) -> do
|
2012-04-20 16:16:13 -04:00
|
|
|
|
let ok = (need + reserve <= have + alreadythere) || force
|
|
|
|
|
unless ok $ do
|
|
|
|
|
liftIO $ print (need, reserve, have, alreadythere)
|
2012-04-20 14:57:57 -04:00
|
|
|
|
needmorespace (need + reserve - have - alreadythere)
|
|
|
|
|
return ok
|
|
|
|
|
_ -> return True
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
dir = maybe (fromRepo gitAnnexDir) return destination
|
|
|
|
|
needmorespace n =
|
|
|
|
|
warning $ "not enough free space, need " ++
|
|
|
|
|
roughSize storageUnits True n ++
|
|
|
|
|
" more" ++ forcemsg
|
|
|
|
|
forcemsg = " (use --force to override this check or adjust annex.diskreserve)"
|
2011-03-22 17:27:04 -04:00
|
|
|
|
|
2012-12-07 14:40:31 -04:00
|
|
|
|
{- Moves a key's content into .git/annex/objects/
|
2012-12-07 17:28:23 -04:00
|
|
|
|
-
|
2012-12-07 14:40:31 -04:00
|
|
|
|
- In direct mode, moves it to the associated file, or files.
|
2011-04-28 20:41:40 -04:00
|
|
|
|
-
|
|
|
|
|
- What if the key there already has content? This could happen for
|
|
|
|
|
- various reasons; perhaps the same content is being annexed again.
|
|
|
|
|
- Perhaps there has been a hash collision generating the keys.
|
|
|
|
|
-
|
|
|
|
|
- The current strategy is to assume that in this case it's safe to delete
|
|
|
|
|
- one of the two copies of the content; and the one already in the annex
|
|
|
|
|
- is left there, assuming it's the original, canonical copy.
|
|
|
|
|
-
|
|
|
|
|
- I considered being more paranoid, and checking that both files had
|
|
|
|
|
- the same content. Decided against it because A) users explicitly choose
|
|
|
|
|
- a backend based on its hashing properties and so if they're dealing
|
|
|
|
|
- with colliding files it's their own fault and B) adding such a check
|
|
|
|
|
- would not catch all cases of colliding keys. For example, perhaps
|
|
|
|
|
- a remote has a key; if it's then added again with different content then
|
|
|
|
|
- the overall system now has two different peices of content for that
|
|
|
|
|
- key, and one of them will probably get deleted later. So, adding the
|
|
|
|
|
- check here would only raise expectations that git-annex cannot truely
|
|
|
|
|
- meet.
|
|
|
|
|
-}
|
2010-11-08 19:26:37 -04:00
|
|
|
|
moveAnnex :: Key -> FilePath -> Annex ()
|
2012-12-07 17:28:23 -04:00
|
|
|
|
moveAnnex key src = withObjectLoc key storeobject storedirect
|
2012-12-07 14:40:31 -04:00
|
|
|
|
where
|
2013-02-18 02:39:40 -04:00
|
|
|
|
storeobject dest = ifM (liftIO $ doesFileExist dest)
|
|
|
|
|
( liftIO $ removeFile src
|
|
|
|
|
, do
|
|
|
|
|
createContentDir dest
|
|
|
|
|
liftIO $ moveFile src dest
|
|
|
|
|
freezeContent dest
|
|
|
|
|
freezeContentDir dest
|
|
|
|
|
)
|
2013-02-15 16:02:35 -04:00
|
|
|
|
storedirect fs = storedirect' =<< filterM validsymlink fs
|
|
|
|
|
validsymlink f = (==) (Just key) <$> isAnnexLink f
|
2013-02-05 16:48:00 -04:00
|
|
|
|
|
2013-04-04 15:46:33 -04:00
|
|
|
|
storedirect' [] = storeobject =<< calcRepo (gitAnnexLocation key)
|
2013-02-05 16:48:00 -04:00
|
|
|
|
storedirect' (dest:fs) = do
|
2013-02-14 16:17:40 -04:00
|
|
|
|
updateInodeCache key src
|
2012-12-07 14:40:31 -04:00
|
|
|
|
thawContent src
|
2013-02-15 16:02:35 -04:00
|
|
|
|
replaceFile dest $ liftIO . moveFile src
|
2013-04-02 13:13:42 -04:00
|
|
|
|
{- Copy to any other locations. -}
|
2013-02-15 16:02:35 -04:00
|
|
|
|
forM_ fs $ \f -> replaceFile f $
|
2013-04-02 13:13:42 -04:00
|
|
|
|
liftIO . void . copyFileExternal dest
|
2012-12-07 14:40:31 -04:00
|
|
|
|
|
2013-04-02 13:13:42 -04:00
|
|
|
|
{- Replaces a possibly already existing file with a new version,
|
|
|
|
|
- atomically, by running an action.
|
|
|
|
|
|
|
|
|
|
- The action is passed a temp file, which it can write to, and once
|
|
|
|
|
- done the temp file is moved into place.
|
|
|
|
|
-}
|
2013-02-15 16:02:35 -04:00
|
|
|
|
replaceFile :: FilePath -> (FilePath -> Annex ()) -> Annex ()
|
2012-12-07 17:28:23 -04:00
|
|
|
|
replaceFile file a = do
|
2013-04-02 13:13:42 -04:00
|
|
|
|
tmpdir <- fromRepo gitAnnexTmpDir
|
|
|
|
|
createAnnexDirectory tmpdir
|
|
|
|
|
tmpfile <- liftIO $ do
|
|
|
|
|
(tmpfile, h) <- openTempFileWithDefaultPermissions tmpdir $
|
|
|
|
|
takeFileName file
|
|
|
|
|
hClose h
|
|
|
|
|
return tmpfile
|
|
|
|
|
a tmpfile
|
2013-02-15 16:02:35 -04:00
|
|
|
|
liftIO $ do
|
2013-04-02 13:13:42 -04:00
|
|
|
|
r <- tryIO $ rename tmpfile file
|
2013-02-15 16:02:35 -04:00
|
|
|
|
case r of
|
2013-04-02 13:13:42 -04:00
|
|
|
|
Left _ -> do
|
|
|
|
|
createDirectoryIfMissing True $ parentDir file
|
|
|
|
|
rename tmpfile file
|
2013-02-15 16:02:35 -04:00
|
|
|
|
_ -> noop
|
2011-07-15 03:12:05 -04:00
|
|
|
|
|
2012-12-08 17:03:39 -04:00
|
|
|
|
{- Runs an action to transfer an object's content.
|
|
|
|
|
-
|
|
|
|
|
- In direct mode, it's possible for the file to change as it's being sent.
|
2013-01-09 18:42:29 -04:00
|
|
|
|
- If this happens, runs the rollback action and returns False. The
|
2013-01-10 11:45:44 -04:00
|
|
|
|
- rollback action should remove the data that was transferred.
|
2012-12-08 17:03:39 -04:00
|
|
|
|
-}
|
2013-02-18 02:39:40 -04:00
|
|
|
|
sendAnnex :: Key -> Annex () -> (FilePath -> Annex Bool) -> Annex Bool
|
2013-01-10 11:45:44 -04:00
|
|
|
|
sendAnnex key rollback sendobject = go =<< prepSendAnnex key
|
|
|
|
|
where
|
|
|
|
|
go Nothing = return False
|
|
|
|
|
go (Just (f, checksuccess)) = do
|
|
|
|
|
r <- sendobject f
|
|
|
|
|
ifM checksuccess
|
|
|
|
|
( return r
|
|
|
|
|
, do
|
|
|
|
|
rollback
|
|
|
|
|
return False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
{- Returns a file that contains an object's content,
|
|
|
|
|
- and an check to run after the transfer is complete.
|
|
|
|
|
-
|
|
|
|
|
- In direct mode, it's possible for the file to change as it's being sent,
|
|
|
|
|
- and the check detects this case and returns False.
|
2013-03-18 11:18:04 -04:00
|
|
|
|
-
|
|
|
|
|
- Note that the returned check action is, in some cases, run in the
|
|
|
|
|
- Annex monad of the remote that is receiving the object, rather than
|
2013-03-18 13:17:43 -04:00
|
|
|
|
- the sender. So it cannot rely on Annex state.
|
2013-01-10 11:45:44 -04:00
|
|
|
|
-}
|
|
|
|
|
prepSendAnnex :: Key -> Annex (Maybe (FilePath, Annex Bool))
|
|
|
|
|
prepSendAnnex key = withObjectLoc key indirect direct
|
2012-12-08 17:03:39 -04:00
|
|
|
|
where
|
2013-01-10 11:45:44 -04:00
|
|
|
|
indirect f = return $ Just (f, return True)
|
|
|
|
|
direct [] = return Nothing
|
|
|
|
|
direct (f:fs) = do
|
2013-02-14 16:17:40 -04:00
|
|
|
|
cache <- recordedInodeCache key
|
2012-12-08 17:03:39 -04:00
|
|
|
|
-- check that we have a good file
|
2013-02-19 16:26:07 -04:00
|
|
|
|
ifM (sameInodeCache f cache)
|
|
|
|
|
( return $ Just (f, sameInodeCache f cache)
|
2013-01-10 11:45:44 -04:00
|
|
|
|
, direct fs
|
2012-12-08 17:03:39 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
{- Performs an action, passing it the location to use for a key's content.
|
|
|
|
|
-
|
|
|
|
|
- In direct mode, the associated files will be passed. But, if there are
|
|
|
|
|
- no associated files for a key, the indirect mode action will be
|
|
|
|
|
- performed instead. -}
|
|
|
|
|
withObjectLoc :: Key -> (FilePath -> Annex a) -> ([FilePath] -> Annex a) -> Annex a
|
|
|
|
|
withObjectLoc key indirect direct = ifM isDirect
|
|
|
|
|
( do
|
|
|
|
|
fs <- associatedFiles key
|
|
|
|
|
if null fs
|
|
|
|
|
then goindirect
|
|
|
|
|
else direct fs
|
|
|
|
|
, goindirect
|
|
|
|
|
)
|
|
|
|
|
where
|
2013-04-04 15:46:33 -04:00
|
|
|
|
goindirect = indirect =<< calcRepo (gitAnnexLocation key)
|
2012-12-08 17:03:39 -04:00
|
|
|
|
|
2012-02-24 16:30:47 -04:00
|
|
|
|
cleanObjectLoc :: Key -> Annex ()
|
|
|
|
|
cleanObjectLoc key = do
|
2013-04-04 15:46:33 -04:00
|
|
|
|
file <- calcRepo $ gitAnnexLocation key
|
2013-02-14 14:10:36 -04:00
|
|
|
|
unlessM crippledFileSystem $
|
2013-02-15 16:37:57 -04:00
|
|
|
|
void $ liftIO $ catchMaybeIO $ allowWrite $ parentDir file
|
|
|
|
|
liftIO $ removeparents file (3 :: Int)
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
removeparents _ 0 = noop
|
|
|
|
|
removeparents file n = do
|
|
|
|
|
let dir = parentDir file
|
|
|
|
|
maybe noop (const $ removeparents dir (n-1))
|
|
|
|
|
<=< catchMaybeIO $ removeDirectory dir
|
2012-02-24 16:30:47 -04:00
|
|
|
|
|
2012-12-07 17:28:23 -04:00
|
|
|
|
{- Removes a key's file from .git/annex/objects/
|
|
|
|
|
-
|
|
|
|
|
- In direct mode, deletes the associated files or files, and replaces
|
|
|
|
|
- them with symlinks. -}
|
2011-07-15 03:12:05 -04:00
|
|
|
|
removeAnnex :: Key -> Annex ()
|
2012-12-07 17:28:23 -04:00
|
|
|
|
removeAnnex key = withObjectLoc key remove removedirect
|
|
|
|
|
where
|
|
|
|
|
remove file = do
|
2013-02-14 14:10:36 -04:00
|
|
|
|
unlessM crippledFileSystem $
|
|
|
|
|
liftIO $ allowWrite $ parentDir file
|
2013-02-15 17:58:49 -04:00
|
|
|
|
liftIO $ nukeFile file
|
|
|
|
|
removeInodeCache key
|
2012-12-07 17:28:23 -04:00
|
|
|
|
cleanObjectLoc key
|
2012-12-08 13:13:36 -04:00
|
|
|
|
removedirect fs = do
|
2013-02-14 16:17:40 -04:00
|
|
|
|
cache <- recordedInodeCache key
|
2013-02-15 16:37:57 -04:00
|
|
|
|
removeInodeCache key
|
2013-02-05 16:48:00 -04:00
|
|
|
|
mapM_ (resetfile cache) fs
|
2013-02-19 16:26:07 -04:00
|
|
|
|
resetfile cache f = whenM (sameInodeCache f cache) $ do
|
2013-04-04 15:46:33 -04:00
|
|
|
|
l <- inRepo $ gitAnnexLink f key
|
2012-12-07 17:28:23 -04:00
|
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
|
cwd <- liftIO getCurrentDirectory
|
|
|
|
|
let top' = fromMaybe top $ absNormPath cwd top
|
|
|
|
|
let l' = relPathDirToFile top' (fromMaybe l $ absNormPath top' l)
|
2013-04-02 13:13:42 -04:00
|
|
|
|
replaceFile f $ makeAnnexLink l'
|
2010-11-08 19:26:37 -04:00
|
|
|
|
|
|
|
|
|
{- Moves a key's file out of .git/annex/objects/ -}
|
|
|
|
|
fromAnnex :: Key -> FilePath -> Annex ()
|
2012-12-07 17:28:23 -04:00
|
|
|
|
fromAnnex key dest = do
|
2013-04-04 15:46:33 -04:00
|
|
|
|
file <- calcRepo $ gitAnnexLocation key
|
2013-02-14 14:10:36 -04:00
|
|
|
|
unlessM crippledFileSystem $
|
|
|
|
|
liftIO $ allowWrite $ parentDir file
|
2012-04-21 14:06:36 -04:00
|
|
|
|
thawContent file
|
|
|
|
|
liftIO $ moveFile file dest
|
2012-02-24 16:30:47 -04:00
|
|
|
|
cleanObjectLoc key
|
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
|
2013-04-04 15:46:33 -04:00
|
|
|
|
src <- calcRepo $ gitAnnexLocation key
|
2011-11-11 01:52:58 -04:00
|
|
|
|
bad <- fromRepo gitAnnexBadDir
|
2011-11-08 15:34:10 -04:00
|
|
|
|
let dest = bad </> takeFileName src
|
2012-04-21 16:59:49 -04:00
|
|
|
|
createAnnexDirectory (parentDir dest)
|
2013-02-14 14:10:36 -04:00
|
|
|
|
unlessM crippledFileSystem $
|
|
|
|
|
liftIO $ allowWrite (parentDir src)
|
|
|
|
|
liftIO $ moveFile src dest
|
2012-02-24 16:30:47 -04:00
|
|
|
|
cleanObjectLoc key
|
2011-07-01 15:24:07 -04:00
|
|
|
|
logStatus key InfoMissing
|
2010-11-13 14:59:27 -04:00
|
|
|
|
return dest
|
|
|
|
|
|
2013-02-15 17:58:49 -04:00
|
|
|
|
{- List of keys whose content exists in the annex. -}
|
2010-11-08 15:15:21 -04:00
|
|
|
|
getKeysPresent :: Annex [Key]
|
2013-02-15 17:58:49 -04:00
|
|
|
|
getKeysPresent = do
|
|
|
|
|
direct <- isDirect
|
|
|
|
|
dir <- fromRepo gitAnnexObjectDir
|
|
|
|
|
liftIO $ traverse direct (2 :: Int) dir
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
2013-02-15 17:58:49 -04:00
|
|
|
|
traverse direct depth dir = do
|
2012-12-13 00:24:19 -04:00
|
|
|
|
contents <- catchDefaultIO [] (dirContents dir)
|
|
|
|
|
if depth == 0
|
2013-02-15 17:58:49 -04:00
|
|
|
|
then do
|
|
|
|
|
contents' <- filterM (present direct) contents
|
|
|
|
|
let keys = mapMaybe (fileKey . takeFileName) contents'
|
|
|
|
|
continue keys []
|
2012-12-13 00:24:19 -04:00
|
|
|
|
else do
|
2013-02-15 17:58:49 -04:00
|
|
|
|
let deeper = traverse direct (depth - 1)
|
2012-12-13 00:24:19 -04:00
|
|
|
|
continue [] (map deeper contents)
|
|
|
|
|
continue keys [] = return keys
|
|
|
|
|
continue keys (a:as) = do
|
|
|
|
|
{- Force lazy traversal with unsafeInterleaveIO. -}
|
|
|
|
|
morekeys <- unsafeInterleaveIO a
|
|
|
|
|
continue (morekeys++keys) as
|
2011-06-22 21:19:52 -04:00
|
|
|
|
|
2013-02-15 17:58:49 -04:00
|
|
|
|
{- In indirect mode, look for the key. In direct mode,
|
|
|
|
|
- the inode cache file is only present when a key's content
|
|
|
|
|
- is present. -}
|
|
|
|
|
present False d = doesFileExist $ contentfile d
|
|
|
|
|
present True d = doesFileExist $ contentfile d ++ ".cache"
|
|
|
|
|
contentfile d = d </> takeFileName d
|
|
|
|
|
|
2012-01-28 15:41:52 -04:00
|
|
|
|
{- Things to do to record changes to content when shutting down.
|
|
|
|
|
-
|
|
|
|
|
- It's acceptable to avoid committing changes to the branch,
|
|
|
|
|
- especially if performing a short-lived action.
|
|
|
|
|
-}
|
|
|
|
|
saveState :: Bool -> Annex ()
|
2012-09-15 20:46:38 -04:00
|
|
|
|
saveState nocommit = doSideAction $ do
|
2012-04-27 13:23:52 -04:00
|
|
|
|
Annex.Queue.flush
|
2012-09-15 20:46:38 -04:00
|
|
|
|
unless nocommit $
|
2013-01-01 13:52:47 -04:00
|
|
|
|
whenM (annexAlwaysCommit <$> Annex.getGitConfig) $
|
2012-09-15 19:47:23 -04:00
|
|
|
|
Annex.Branch.commit "update"
|
2012-01-02 14:20:20 -04:00
|
|
|
|
|
|
|
|
|
{- Downloads content from any of a list of urls. -}
|
|
|
|
|
downloadUrl :: [Url.URLString] -> FilePath -> Annex Bool
|
2013-04-08 23:34:05 -04:00
|
|
|
|
downloadUrl urls file = go =<< annexWebDownloadCommand <$> Annex.getGitConfig
|
|
|
|
|
where
|
|
|
|
|
go Nothing = do
|
|
|
|
|
opts <- map Param . annexWebOptions <$> Annex.getGitConfig
|
|
|
|
|
headers <- getHttpHeaders
|
|
|
|
|
liftIO $ anyM (\u -> Url.download u headers opts file) urls
|
|
|
|
|
go (Just basecmd) = liftIO $ anyM (downloadcmd basecmd) urls
|
|
|
|
|
downloadcmd basecmd url =
|
2013-04-08 23:56:37 -04:00
|
|
|
|
boolSystem "sh" [Param "-c", Param $ gencmd url basecmd]
|
2013-04-08 23:34:05 -04:00
|
|
|
|
<&&> doesFileExist file
|
2013-04-08 23:56:37 -04:00
|
|
|
|
gencmd url = massReplace
|
|
|
|
|
[ ("%file", shellEscape file)
|
|
|
|
|
, ("%url", shellEscape url)
|
|
|
|
|
]
|
2012-01-19 17:05:39 -04:00
|
|
|
|
|
|
|
|
|
{- Copies a key's content, when present, to a temp file.
|
|
|
|
|
- This is used to speed up some rsyncs. -}
|
2012-01-20 13:23:11 -04:00
|
|
|
|
preseedTmp :: Key -> FilePath -> Annex Bool
|
|
|
|
|
preseedTmp key file = go =<< inAnnex key
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
go False = return False
|
|
|
|
|
go True = do
|
|
|
|
|
ok <- copy
|
|
|
|
|
when ok $ thawContent file
|
|
|
|
|
return ok
|
|
|
|
|
copy = ifM (liftIO $ doesFileExist file)
|
2013-02-15 17:58:49 -04:00
|
|
|
|
( return True
|
|
|
|
|
, do
|
2013-04-04 15:46:33 -04:00
|
|
|
|
s <- calcRepo $ gitAnnexLocation key
|
2013-02-15 17:58:49 -04:00
|
|
|
|
liftIO $ copyFileExternal s file
|
|
|
|
|
)
|
2012-04-21 14:06:36 -04:00
|
|
|
|
|
|
|
|
|
{- Blocks writing to an annexed file. The file is made unwritable
|
|
|
|
|
- to avoid accidental edits. core.sharedRepository may change
|
|
|
|
|
- who can read it. -}
|
|
|
|
|
freezeContent :: FilePath -> Annex ()
|
2013-02-14 14:10:36 -04:00
|
|
|
|
freezeContent file = unlessM crippledFileSystem $
|
|
|
|
|
liftIO . go =<< fromRepo getSharedRepository
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
go GroupShared = modifyFileMode file $
|
|
|
|
|
removeModes writeModes .
|
|
|
|
|
addModes [ownerReadMode, groupReadMode]
|
|
|
|
|
go AllShared = modifyFileMode file $
|
|
|
|
|
removeModes writeModes .
|
|
|
|
|
addModes readModes
|
|
|
|
|
go _ = preventWrite file
|
2012-04-21 14:06:36 -04:00
|
|
|
|
|
|
|
|
|
{- Allows writing to an annexed file that freezeContent was called on
|
|
|
|
|
- before. -}
|
|
|
|
|
thawContent :: FilePath -> Annex ()
|
2013-02-14 14:10:36 -04:00
|
|
|
|
thawContent file = unlessM crippledFileSystem $
|
|
|
|
|
liftIO . go =<< fromRepo getSharedRepository
|
2012-12-13 00:24:19 -04:00
|
|
|
|
where
|
|
|
|
|
go GroupShared = groupWriteRead file
|
|
|
|
|
go AllShared = groupWriteRead file
|
|
|
|
|
go _ = allowWrite file
|