2015-12-09 21:00:37 +00:00
|
|
|
{- Sqlite database of information about Keys
|
2015-12-07 17:42:03 +00:00
|
|
|
-
|
2016-01-01 19:09:42 +00:00
|
|
|
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2015-12-07 17:42:03 +00:00
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2016-01-12 17:31:13 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2015-12-07 17:42:03 +00:00
|
|
|
|
2015-12-09 21:00:37 +00:00
|
|
|
module Database.Keys (
|
2015-12-07 17:42:03 +00:00
|
|
|
DbHandle,
|
2016-05-16 18:49:12 +00:00
|
|
|
closeDb,
|
2015-12-09 21:00:37 +00:00
|
|
|
addAssociatedFile,
|
|
|
|
getAssociatedFiles,
|
2015-12-15 17:05:23 +00:00
|
|
|
getAssociatedKey,
|
2015-12-09 21:00:37 +00:00
|
|
|
removeAssociatedFile,
|
2015-12-09 21:47:05 +00:00
|
|
|
storeInodeCaches,
|
|
|
|
addInodeCaches,
|
|
|
|
getInodeCaches,
|
|
|
|
removeInodeCaches,
|
2016-10-17 18:58:33 +00:00
|
|
|
runWriter,
|
2015-12-07 17:42:03 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-11 19:52:11 +00:00
|
|
|
import qualified Database.Keys.SQL as SQL
|
2015-12-07 17:42:03 +00:00
|
|
|
import Database.Types
|
2015-12-23 22:34:51 +00:00
|
|
|
import Database.Keys.Handle
|
2015-12-23 18:59:58 +00:00
|
|
|
import qualified Database.Queue as H
|
2017-02-13 21:30:28 +00:00
|
|
|
import Database.Init
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Locations
|
|
|
|
import Annex.Common hiding (delete)
|
2016-07-19 16:12:19 +00:00
|
|
|
import Annex.Version (versionUsesKeysDatabase)
|
2016-01-01 19:50:59 +00:00
|
|
|
import qualified Annex
|
2015-12-07 17:42:03 +00:00
|
|
|
import Annex.LockFile
|
2015-12-09 21:00:37 +00:00
|
|
|
import Utility.InodeCache
|
2015-12-09 21:47:05 +00:00
|
|
|
import Annex.InodeSentinal
|
2016-01-01 19:09:42 +00:00
|
|
|
import Git.FilePath
|
2015-12-23 22:34:51 +00:00
|
|
|
|
|
|
|
{- Runs an action that reads from the database.
|
|
|
|
-
|
|
|
|
- If the database doesn't already exist, it's not created; mempty is
|
|
|
|
- returned instead. This way, when the keys database is not in use,
|
|
|
|
- there's minimal overhead in checking it.
|
|
|
|
-
|
|
|
|
- If the database is already open, any writes are flushed to it, to ensure
|
|
|
|
- consistency.
|
|
|
|
-
|
|
|
|
- Any queued writes will be flushed before the read.
|
|
|
|
-}
|
2016-01-11 19:52:11 +00:00
|
|
|
runReader :: Monoid v => (SQL.ReadHandle -> Annex v) -> Annex v
|
2016-07-19 18:02:49 +00:00
|
|
|
runReader a = do
|
|
|
|
h <- getDbHandle
|
|
|
|
withDbState h go
|
2015-12-23 22:34:51 +00:00
|
|
|
where
|
2016-02-12 18:15:28 +00:00
|
|
|
go DbUnavailable = return (mempty, DbUnavailable)
|
2015-12-23 22:34:51 +00:00
|
|
|
go st@(DbOpen qh) = do
|
|
|
|
liftIO $ H.flushDbQueue qh
|
2016-01-11 19:52:11 +00:00
|
|
|
v <- a (SQL.ReadHandle qh)
|
2015-12-23 22:34:51 +00:00
|
|
|
return (v, st)
|
|
|
|
go DbClosed = do
|
|
|
|
st' <- openDb False DbClosed
|
|
|
|
v <- case st' of
|
2016-01-11 19:52:11 +00:00
|
|
|
(DbOpen qh) -> a (SQL.ReadHandle qh)
|
2015-12-23 22:34:51 +00:00
|
|
|
_ -> return mempty
|
|
|
|
return (v, st')
|
|
|
|
|
2016-01-11 19:52:11 +00:00
|
|
|
runReaderIO :: Monoid v => (SQL.ReadHandle -> IO v) -> Annex v
|
|
|
|
runReaderIO a = runReader (liftIO . a)
|
2015-12-23 22:34:51 +00:00
|
|
|
|
|
|
|
{- Runs an action that writes to the database. Typically this is used to
|
|
|
|
- queue changes, which will be flushed at a later point.
|
|
|
|
-
|
|
|
|
- The database is created if it doesn't exist yet. -}
|
2016-01-11 19:52:11 +00:00
|
|
|
runWriter :: (SQL.WriteHandle -> Annex ()) -> Annex ()
|
2016-07-19 18:02:49 +00:00
|
|
|
runWriter a = do
|
2015-12-23 22:34:51 +00:00
|
|
|
h <- getDbHandle
|
|
|
|
withDbState h go
|
|
|
|
where
|
|
|
|
go st@(DbOpen qh) = do
|
2016-01-11 19:52:11 +00:00
|
|
|
v <- a (SQL.WriteHandle qh)
|
2015-12-23 22:34:51 +00:00
|
|
|
return (v, st)
|
|
|
|
go st = do
|
|
|
|
st' <- openDb True st
|
|
|
|
v <- case st' of
|
2016-01-11 19:52:11 +00:00
|
|
|
DbOpen qh -> a (SQL.WriteHandle qh)
|
2015-12-23 22:34:51 +00:00
|
|
|
_ -> error "internal"
|
2015-12-24 17:06:03 +00:00
|
|
|
return (v, st')
|
2015-12-23 22:34:51 +00:00
|
|
|
|
2016-01-11 19:52:11 +00:00
|
|
|
runWriterIO :: (SQL.WriteHandle -> IO ()) -> Annex ()
|
|
|
|
runWriterIO a = runWriter (liftIO . a)
|
2015-12-23 22:34:51 +00:00
|
|
|
|
|
|
|
{- Gets the handle cached in Annex state; creates a new one if it's not yet
|
|
|
|
- available, but doesn't open the database. -}
|
|
|
|
getDbHandle :: Annex DbHandle
|
2016-01-01 19:50:59 +00:00
|
|
|
getDbHandle = go =<< Annex.getState Annex.keysdbhandle
|
2015-12-23 22:34:51 +00:00
|
|
|
where
|
|
|
|
go (Just h) = pure h
|
|
|
|
go Nothing = do
|
2016-07-19 18:02:49 +00:00
|
|
|
h <- ifM versionUsesKeysDatabase
|
|
|
|
( liftIO newDbHandle
|
|
|
|
, liftIO unavailableDbHandle
|
|
|
|
)
|
2016-01-01 19:50:59 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.keysdbhandle = Just h }
|
2015-12-23 22:34:51 +00:00
|
|
|
return h
|
|
|
|
|
|
|
|
{- Opens the database, perhaps creating it if it doesn't exist yet.
|
2015-12-16 17:24:45 +00:00
|
|
|
-
|
|
|
|
- Multiple readers and writers can have the database open at the same
|
|
|
|
- time. Database.Handle deals with the concurrency issues.
|
|
|
|
- The lock is held while opening the database, so that when
|
|
|
|
- the database doesn't exist yet, one caller wins the lock and
|
|
|
|
- can create it undisturbed.
|
|
|
|
-}
|
2015-12-23 22:34:51 +00:00
|
|
|
openDb :: Bool -> DbState -> Annex DbState
|
|
|
|
openDb _ st@(DbOpen _) = return st
|
2016-02-12 18:15:28 +00:00
|
|
|
openDb False DbUnavailable = return DbUnavailable
|
|
|
|
openDb createdb _ = catchPermissionDenied permerr $ withExclusiveLock gitAnnexKeysDbLock $ do
|
2015-12-09 21:00:37 +00:00
|
|
|
dbdir <- fromRepo gitAnnexKeysDb
|
2015-12-07 17:42:03 +00:00
|
|
|
let db = dbdir </> "db"
|
2015-12-23 22:34:51 +00:00
|
|
|
dbexists <- liftIO $ doesFileExist db
|
|
|
|
case (dbexists, createdb) of
|
|
|
|
(True, _) -> open db
|
|
|
|
(False, True) -> do
|
2017-02-13 21:30:28 +00:00
|
|
|
initDb db SQL.createTables
|
2015-12-23 22:34:51 +00:00
|
|
|
open db
|
2016-02-12 18:15:28 +00:00
|
|
|
(False, False) -> return DbUnavailable
|
2015-12-23 22:34:51 +00:00
|
|
|
where
|
2017-09-06 21:07:49 +00:00
|
|
|
open db = liftIO $ DbOpen <$> H.openDbQueue H.MultiWriter db SQL.containedTable
|
2016-02-12 18:15:28 +00:00
|
|
|
-- If permissions don't allow opening the database, treat it as if
|
|
|
|
-- it does not exist.
|
|
|
|
permerr e = case createdb of
|
|
|
|
False -> return DbUnavailable
|
|
|
|
True -> throwM e
|
2015-12-09 18:55:47 +00:00
|
|
|
|
2016-05-16 18:49:12 +00:00
|
|
|
{- Closes the database if it was open. Any writes will be flushed to it.
|
|
|
|
-
|
|
|
|
- This does not normally need to be called; the database will auto-close
|
|
|
|
- when the handle is garbage collected. However, this can be used to
|
|
|
|
- force a re-read of the database, in case another process has written
|
|
|
|
- data to it.
|
|
|
|
-}
|
|
|
|
closeDb :: Annex ()
|
2016-07-19 18:02:49 +00:00
|
|
|
closeDb = liftIO . closeDbHandle =<< getDbHandle
|
2016-05-16 18:49:12 +00:00
|
|
|
|
2016-01-05 21:22:19 +00:00
|
|
|
addAssociatedFile :: Key -> TopFilePath -> Annex ()
|
2016-01-12 18:01:50 +00:00
|
|
|
addAssociatedFile k f = runWriterIO $ SQL.addAssociatedFile (toIKey k) f
|
2015-12-07 17:42:03 +00:00
|
|
|
|
2015-12-09 21:00:37 +00:00
|
|
|
{- Note that the files returned were once associated with the key, but
|
2015-12-07 17:42:03 +00:00
|
|
|
- some of them may not be any longer. -}
|
2016-01-05 21:22:19 +00:00
|
|
|
getAssociatedFiles :: Key -> Annex [TopFilePath]
|
2016-01-12 18:01:50 +00:00
|
|
|
getAssociatedFiles = runReaderIO . SQL.getAssociatedFiles . toIKey
|
2015-12-07 17:42:03 +00:00
|
|
|
|
2015-12-15 17:05:23 +00:00
|
|
|
{- Gets any keys that are on record as having a particular associated file.
|
|
|
|
- (Should be one or none but the database doesn't enforce that.) -}
|
2016-01-05 21:22:19 +00:00
|
|
|
getAssociatedKey :: TopFilePath -> Annex [Key]
|
2016-01-12 18:01:50 +00:00
|
|
|
getAssociatedKey = map fromIKey <$$> runReaderIO . SQL.getAssociatedKey
|
2015-12-15 17:05:23 +00:00
|
|
|
|
2016-01-05 21:22:19 +00:00
|
|
|
removeAssociatedFile :: Key -> TopFilePath -> Annex ()
|
2016-01-12 18:01:50 +00:00
|
|
|
removeAssociatedFile k = runWriterIO . SQL.removeAssociatedFile (toIKey k)
|
2015-12-23 22:34:51 +00:00
|
|
|
|
2015-12-09 21:47:05 +00:00
|
|
|
{- Stats the files, and stores their InodeCaches. -}
|
|
|
|
storeInodeCaches :: Key -> [FilePath] -> Annex ()
|
|
|
|
storeInodeCaches k fs = withTSDelta $ \d ->
|
|
|
|
addInodeCaches k . catMaybes =<< liftIO (mapM (`genInodeCache` d) fs)
|
|
|
|
|
|
|
|
addInodeCaches :: Key -> [InodeCache] -> Annex ()
|
2016-01-12 18:01:50 +00:00
|
|
|
addInodeCaches k is = runWriterIO $ SQL.addInodeCaches (toIKey k) is
|
2015-12-09 21:00:37 +00:00
|
|
|
|
2015-12-09 21:47:05 +00:00
|
|
|
{- A key may have multiple InodeCaches; one for the annex object, and one
|
|
|
|
- for each pointer file that is a copy of it. -}
|
|
|
|
getInodeCaches :: Key -> Annex [InodeCache]
|
2016-01-12 18:01:50 +00:00
|
|
|
getInodeCaches = runReaderIO . SQL.getInodeCaches . toIKey
|
2015-12-09 21:47:05 +00:00
|
|
|
|
|
|
|
removeInodeCaches :: Key -> Annex ()
|
2016-01-12 18:01:50 +00:00
|
|
|
removeInodeCaches = runWriterIO . SQL.removeInodeCaches . toIKey
|