2019-01-17 19:40:44 +00:00
|
|
|
{- git-annex tmp files
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2019-01-17 19:40:44 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Tmp where
|
|
|
|
|
2019-09-10 17:37:07 +00:00
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex
|
2019-01-17 19:40:44 +00:00
|
|
|
import Annex.LockFile
|
|
|
|
import Annex.Perms
|
|
|
|
import Types.CleanupActions
|
2020-11-24 16:38:12 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2019-01-17 19:40:44 +00:00
|
|
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
-- | For creation of tmp files, other than for key's contents.
|
|
|
|
--
|
|
|
|
-- The action should normally clean up whatever files it writes to the temp
|
|
|
|
-- directory that is passed to it. However, once the action is done,
|
|
|
|
-- any files left in that directory may be cleaned up by another process at
|
|
|
|
-- any time.
|
2020-10-29 16:02:46 +00:00
|
|
|
withOtherTmp :: (RawFilePath -> Annex a) -> Annex a
|
2019-01-17 19:40:44 +00:00
|
|
|
withOtherTmp a = do
|
2020-12-11 19:28:58 +00:00
|
|
|
Annex.addCleanupAction OtherTmpCleanup cleanupOtherTmp
|
2019-01-17 19:40:44 +00:00
|
|
|
tmpdir <- fromRepo gitAnnexTmpOtherDir
|
|
|
|
tmplck <- fromRepo gitAnnexTmpOtherLock
|
2019-02-02 17:56:31 +00:00
|
|
|
withSharedLock (const tmplck) $ do
|
|
|
|
void $ createAnnexDirectory tmpdir
|
|
|
|
a tmpdir
|
2019-01-17 19:40:44 +00:00
|
|
|
|
2019-09-10 17:37:07 +00:00
|
|
|
-- | This uses an alternate temp directory. The action should normally
|
|
|
|
-- clean up whatever files it writes there, but if it leaves files
|
|
|
|
-- there (perhaps due to being interrupted), the files will be eventually
|
|
|
|
-- cleaned up by another git-annex process (after they're a week old).
|
|
|
|
--
|
|
|
|
-- Unlike withOtherTmp, this does not rely on locking working.
|
|
|
|
-- Its main use is in situations where the state of lockfile is not
|
|
|
|
-- determined yet, eg during initialization.
|
2020-10-29 16:02:46 +00:00
|
|
|
withEventuallyCleanedOtherTmp :: (RawFilePath -> Annex a) -> Annex a
|
2019-09-10 17:37:07 +00:00
|
|
|
withEventuallyCleanedOtherTmp = bracket setup cleanup
|
|
|
|
where
|
|
|
|
setup = do
|
|
|
|
tmpdir <- fromRepo gitAnnexTmpOtherDirOld
|
|
|
|
void $ createAnnexDirectory tmpdir
|
|
|
|
return tmpdir
|
2020-10-29 16:02:46 +00:00
|
|
|
cleanup = liftIO . void . tryIO . removeDirectory . fromRawFilePath
|
2019-09-10 17:37:07 +00:00
|
|
|
|
2019-01-17 19:40:44 +00:00
|
|
|
-- | Cleans up any tmp files that were left by a previous
|
|
|
|
-- git-annex process that got interrupted or failed to clean up after
|
|
|
|
-- itself for some other reason.
|
|
|
|
--
|
|
|
|
-- Does not do anything if withOtherTmp is running.
|
|
|
|
cleanupOtherTmp :: Annex ()
|
|
|
|
cleanupOtherTmp = do
|
|
|
|
tmplck <- fromRepo gitAnnexTmpOtherLock
|
|
|
|
void $ tryIO $ tryExclusiveLock (const tmplck) $ do
|
2020-10-29 16:02:46 +00:00
|
|
|
tmpdir <- fromRawFilePath <$> fromRepo gitAnnexTmpOtherDir
|
2019-01-17 19:40:44 +00:00
|
|
|
void $ liftIO $ tryIO $ removeDirectoryRecursive tmpdir
|
2020-10-29 16:02:46 +00:00
|
|
|
oldtmp <- fromRawFilePath <$> fromRepo gitAnnexTmpOtherDirOld
|
2019-01-17 19:40:44 +00:00
|
|
|
liftIO $ mapM_ cleanold =<< dirContentsRecursive oldtmp
|
|
|
|
liftIO $ void $ tryIO $ removeDirectory oldtmp -- when empty
|
|
|
|
where
|
|
|
|
cleanold f = do
|
|
|
|
now <- liftIO getPOSIXTime
|
|
|
|
let oldenough = now - (60 * 60 * 24 * 7)
|
|
|
|
catchMaybeIO (modificationTime <$> getSymbolicLinkStatus f) >>= \case
|
|
|
|
Just mtime | realToFrac mtime <= oldenough ->
|
2020-11-24 16:38:12 +00:00
|
|
|
void $ tryIO $ removeWhenExistsWith R.removeLink (toRawFilePath f)
|
2019-01-17 19:40:44 +00:00
|
|
|
_ -> return ()
|