2012-07-07 17:47:36 +00:00
|
|
|
{- git-annex transfer information files and lock files
|
2012-07-01 18:29:00 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Transfer where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Annex.Perms
|
|
|
|
import Annex.Exception
|
|
|
|
import qualified Git
|
2012-07-01 20:59:54 +00:00
|
|
|
import Types.Remote
|
2012-07-02 12:35:15 +00:00
|
|
|
import qualified Fields
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
import System.Posix.Types
|
|
|
|
import Data.Time.Clock
|
2012-07-18 22:42:41 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time
|
|
|
|
import System.Locale
|
|
|
|
import Control.Concurrent
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
{- Enough information to uniquely identify a transfer, used as the filename
|
|
|
|
- of the transfer information file. -}
|
2012-07-01 20:10:00 +00:00
|
|
|
data Transfer = Transfer
|
|
|
|
{ transferDirection :: Direction
|
2012-07-05 20:34:20 +00:00
|
|
|
, transferUUID :: UUID
|
2012-07-01 20:10:00 +00:00
|
|
|
, transferKey :: Key
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Ord)
|
2012-07-01 18:29:00 +00:00
|
|
|
|
2012-07-02 17:49:27 +00:00
|
|
|
{- Information about a Transfer, stored in the transfer information file.
|
|
|
|
-
|
|
|
|
- Note that the associatedFile may not correspond to a file in the local
|
|
|
|
- git repository. It's some file, possibly relative to some directory,
|
|
|
|
- of some repository, that was acted on to initiate the transfer.
|
|
|
|
-}
|
2012-07-01 18:29:00 +00:00
|
|
|
data TransferInfo = TransferInfo
|
2012-07-18 22:42:41 +00:00
|
|
|
{ startedTime :: Maybe POSIXTime
|
2012-07-01 20:10:00 +00:00
|
|
|
, transferPid :: Maybe ProcessID
|
2012-07-18 22:42:41 +00:00
|
|
|
, transferTid :: Maybe ThreadId
|
2012-07-05 20:34:20 +00:00
|
|
|
, transferRemote :: Maybe Remote
|
2012-07-01 18:29:00 +00:00
|
|
|
, bytesComplete :: Maybe Integer
|
|
|
|
, associatedFile :: Maybe FilePath
|
|
|
|
}
|
2012-07-01 20:10:00 +00:00
|
|
|
deriving (Show, Eq, Ord)
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
data Direction = Upload | Download
|
2012-07-01 20:10:00 +00:00
|
|
|
deriving (Eq, Ord)
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
instance Show Direction where
|
|
|
|
show Upload = "upload"
|
|
|
|
show Download = "download"
|
|
|
|
|
|
|
|
readDirection :: String -> Maybe Direction
|
|
|
|
readDirection "upload" = Just Upload
|
|
|
|
readDirection "download" = Just Download
|
|
|
|
readDirection _ = Nothing
|
|
|
|
|
2012-07-01 20:59:54 +00:00
|
|
|
upload :: UUID -> Key -> AssociatedFile -> Annex a -> Annex a
|
|
|
|
upload u key file a = transfer (Transfer Upload u key) file a
|
2012-07-01 19:04:29 +00:00
|
|
|
|
2012-07-01 20:59:54 +00:00
|
|
|
download :: UUID -> Key -> AssociatedFile -> Annex a -> Annex a
|
|
|
|
download u key file a = transfer (Transfer Download u key) file a
|
2012-07-01 19:04:29 +00:00
|
|
|
|
2012-07-02 05:31:10 +00:00
|
|
|
fieldTransfer :: Direction -> Key -> Annex a -> Annex a
|
|
|
|
fieldTransfer direction key a = do
|
2012-07-02 12:35:15 +00:00
|
|
|
afile <- Fields.getField Fields.associatedFile
|
2012-07-02 05:31:10 +00:00
|
|
|
maybe a (\u -> transfer (Transfer direction (toUUID u) key) afile a)
|
2012-07-02 12:35:15 +00:00
|
|
|
=<< Fields.getField Fields.remoteUUID
|
2012-07-02 05:31:10 +00:00
|
|
|
|
2012-07-07 17:47:36 +00:00
|
|
|
{- Runs a transfer action. Creates and locks the lock file while the
|
2012-07-17 21:16:30 +00:00
|
|
|
- action is running, and stores info in the transfer information
|
2012-07-07 17:47:36 +00:00
|
|
|
- file. Will throw an error if the transfer is already in progress.
|
2012-07-01 18:29:00 +00:00
|
|
|
-}
|
|
|
|
transfer :: Transfer -> Maybe FilePath -> Annex a -> Annex a
|
2012-07-01 19:04:29 +00:00
|
|
|
transfer t file a = do
|
|
|
|
tfile <- fromRepo $ transferFile t
|
|
|
|
createAnnexDirectory $ takeDirectory tfile
|
2012-07-01 18:29:00 +00:00
|
|
|
mode <- annexFileMode
|
|
|
|
info <- liftIO $ TransferInfo
|
2012-07-18 22:42:41 +00:00
|
|
|
<$> (Just . utcTimeToPOSIXSeconds <$> getCurrentTime)
|
2012-07-01 20:10:00 +00:00
|
|
|
<*> pure Nothing -- pid not stored in file, so omitted for speed
|
2012-07-18 22:42:41 +00:00
|
|
|
<*> pure Nothing -- tid ditto
|
2012-07-01 18:29:00 +00:00
|
|
|
<*> pure Nothing -- not 0; transfer may be resuming
|
2012-07-05 20:34:20 +00:00
|
|
|
<*> pure Nothing
|
2012-07-01 18:29:00 +00:00
|
|
|
<*> pure file
|
2012-07-01 19:04:29 +00:00
|
|
|
bracketIO (prep tfile mode info) (cleanup tfile) a
|
2012-07-01 18:29:00 +00:00
|
|
|
where
|
2012-07-01 19:04:29 +00:00
|
|
|
prep tfile mode info = do
|
2012-07-07 17:47:36 +00:00
|
|
|
fd <- openFd (transferLockFile tfile) ReadWrite (Just mode)
|
2012-07-01 18:29:00 +00:00
|
|
|
defaultFileFlags { trunc = True }
|
|
|
|
locked <- catchMaybeIO $
|
|
|
|
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
when (locked == Nothing) $
|
|
|
|
error $ "transfer already in progress"
|
2012-07-07 17:47:36 +00:00
|
|
|
writeFile tfile $ writeTransferInfo info
|
|
|
|
return fd
|
|
|
|
cleanup tfile fd = do
|
2012-07-01 18:29:00 +00:00
|
|
|
removeFile tfile
|
2012-07-07 17:47:36 +00:00
|
|
|
removeFile $ transferLockFile tfile
|
|
|
|
closeFd fd
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
{- If a transfer is still running, returns its TransferInfo. -}
|
|
|
|
checkTransfer :: Transfer -> Annex (Maybe TransferInfo)
|
2012-07-01 19:04:29 +00:00
|
|
|
checkTransfer t = do
|
2012-07-01 18:29:00 +00:00
|
|
|
mode <- annexFileMode
|
2012-07-01 19:04:29 +00:00
|
|
|
tfile <- fromRepo $ transferFile t
|
2012-07-01 18:29:00 +00:00
|
|
|
mfd <- liftIO $ catchMaybeIO $
|
2012-07-07 17:47:36 +00:00
|
|
|
openFd (transferLockFile tfile) ReadOnly (Just mode) defaultFileFlags
|
2012-07-01 18:29:00 +00:00
|
|
|
case mfd of
|
|
|
|
Nothing -> return Nothing -- failed to open file; not running
|
|
|
|
Just fd -> do
|
|
|
|
locked <- liftIO $
|
|
|
|
getLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
2012-07-07 17:47:36 +00:00
|
|
|
liftIO $ closeFd fd
|
2012-07-01 18:29:00 +00:00
|
|
|
case locked of
|
2012-07-07 17:47:36 +00:00
|
|
|
Nothing -> return Nothing
|
|
|
|
Just (pid, _) -> liftIO $
|
|
|
|
flip catchDefaultIO Nothing $
|
|
|
|
readTransferInfo pid
|
|
|
|
<$> readFile tfile
|
2012-07-01 18:29:00 +00:00
|
|
|
|
|
|
|
{- Gets all currently running transfers. -}
|
|
|
|
getTransfers :: Annex [(Transfer, TransferInfo)]
|
|
|
|
getTransfers = do
|
2012-07-01 20:59:54 +00:00
|
|
|
transfers <- catMaybes . map parseTransferFile <$> findfiles
|
2012-07-01 18:29:00 +00:00
|
|
|
infos <- mapM checkTransfer transfers
|
|
|
|
return $ map (\(t, Just i) -> (t, i)) $
|
|
|
|
filter running $ zip transfers infos
|
|
|
|
where
|
|
|
|
findfiles = liftIO . dirContentsRecursive
|
|
|
|
=<< fromRepo gitAnnexTransferDir
|
|
|
|
running (_, i) = isJust i
|
|
|
|
|
|
|
|
{- The transfer information file to use for a given Transfer. -}
|
|
|
|
transferFile :: Transfer -> Git.Repo -> FilePath
|
2012-07-01 20:59:54 +00:00
|
|
|
transferFile (Transfer direction u key) r = gitAnnexTransferDir r
|
2012-07-01 19:04:29 +00:00
|
|
|
</> show direction
|
2012-07-01 20:59:54 +00:00
|
|
|
</> fromUUID u
|
2012-07-01 19:04:29 +00:00
|
|
|
</> keyFile key
|
2012-07-01 18:29:00 +00:00
|
|
|
|
2012-07-07 17:47:36 +00:00
|
|
|
{- The transfer lock file corresponding to a given transfer info file. -}
|
|
|
|
transferLockFile :: FilePath -> FilePath
|
2012-07-17 21:22:00 +00:00
|
|
|
transferLockFile infofile = let (d,f) = splitFileName infofile in
|
|
|
|
combine d ("lck." ++ f)
|
2012-07-07 17:47:36 +00:00
|
|
|
|
2012-07-01 18:29:00 +00:00
|
|
|
{- Parses a transfer information filename to a Transfer. -}
|
2012-07-01 20:59:54 +00:00
|
|
|
parseTransferFile :: FilePath -> Maybe Transfer
|
2012-07-17 21:26:53 +00:00
|
|
|
parseTransferFile file
|
|
|
|
| "lck." `isPrefixOf` (takeFileName file) = Nothing
|
|
|
|
| otherwise = case drop (length bits - 3) bits of
|
2012-07-01 19:04:29 +00:00
|
|
|
[direction, u, key] -> Transfer
|
2012-07-01 18:29:00 +00:00
|
|
|
<$> readDirection direction
|
2012-07-01 20:59:54 +00:00
|
|
|
<*> pure (toUUID u)
|
2012-07-01 18:29:00 +00:00
|
|
|
<*> fileKey key
|
|
|
|
_ -> Nothing
|
|
|
|
where
|
|
|
|
bits = splitDirectories file
|
|
|
|
|
|
|
|
writeTransferInfo :: TransferInfo -> String
|
2012-07-01 19:04:29 +00:00
|
|
|
writeTransferInfo info = unlines
|
2012-07-01 18:29:00 +00:00
|
|
|
-- transferPid is not included; instead obtained by looking at
|
|
|
|
-- the process that locks the file.
|
|
|
|
[ show $ startedTime info
|
|
|
|
-- bytesComplete is not included; changes too fast
|
2012-07-01 19:04:29 +00:00
|
|
|
, fromMaybe "" $ associatedFile info -- comes last; arbitrary content
|
2012-07-01 18:29:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
readTransferInfo :: ProcessID -> String -> Maybe TransferInfo
|
|
|
|
readTransferInfo pid s =
|
|
|
|
case bits of
|
|
|
|
[time] -> TransferInfo
|
2012-07-18 22:42:41 +00:00
|
|
|
<$> parsetime time
|
2012-07-01 20:10:00 +00:00
|
|
|
<*> pure (Just pid)
|
2012-07-01 18:29:00 +00:00
|
|
|
<*> pure Nothing
|
|
|
|
<*> pure Nothing
|
2012-07-18 22:42:41 +00:00
|
|
|
<*> pure Nothing
|
2012-07-02 15:02:47 +00:00
|
|
|
<*> pure (if null filename then Nothing else Just filename)
|
2012-07-01 18:29:00 +00:00
|
|
|
_ -> Nothing
|
|
|
|
where
|
2012-07-01 19:04:29 +00:00
|
|
|
(bits, filebits) = splitAt 1 $ lines s
|
2012-07-02 15:02:47 +00:00
|
|
|
filename = join "\n" filebits
|
2012-07-18 22:42:41 +00:00
|
|
|
parsetime t = Just . utcTimeToPOSIXSeconds
|
|
|
|
<$> parseTime defaultTimeLocale "%s%Qs" t
|