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,
|
2016-01-01 19:09:42 +00:00
|
|
|
scanAssociatedFiles,
|
2015-12-09 21:47:05 +00:00
|
|
|
storeInodeCaches,
|
|
|
|
addInodeCaches,
|
|
|
|
getInodeCaches,
|
|
|
|
removeInodeCaches,
|
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
|
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.Perms
|
|
|
|
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 qualified Git.Types
|
|
|
|
import qualified Git.LsTree
|
2016-01-01 19:16:16 +00:00
|
|
|
import qualified Git.Branch
|
2016-01-01 19:09:42 +00:00
|
|
|
import Git.Ref
|
|
|
|
import Git.FilePath
|
|
|
|
import Annex.CatFile
|
2015-12-07 17:42:03 +00:00
|
|
|
|
|
|
|
import Database.Esqueleto hiding (Key)
|
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
|
|
|
|
liftIO $ do
|
|
|
|
createDirectoryIfMissing True dbdir
|
2016-01-12 17:01:44 +00:00
|
|
|
H.initDb db SQL.createTables
|
2015-12-23 22:34:51 +00:00
|
|
|
setAnnexDirPerm dbdir
|
|
|
|
setAnnexFilePerm db
|
|
|
|
open db
|
2016-02-12 18:15:28 +00:00
|
|
|
(False, False) -> return DbUnavailable
|
2015-12-23 22:34:51 +00:00
|
|
|
where
|
2016-01-12 17:01:44 +00:00
|
|
|
open db = liftIO $ DbOpen <$> H.openDbQueue 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
|
|
|
|
2016-01-01 19:09:42 +00:00
|
|
|
{- Find all unlocked associated files. This is expensive, and so normally
|
|
|
|
- the associated files are updated incrementally when changes are noticed. -}
|
|
|
|
scanAssociatedFiles :: Annex ()
|
2016-01-01 19:16:16 +00:00
|
|
|
scanAssociatedFiles = whenM (isJust <$> inRepo Git.Branch.current) $
|
|
|
|
runWriter $ \h -> do
|
|
|
|
showSideAction "scanning for unlocked files"
|
|
|
|
dropallassociated h
|
2016-01-01 19:50:59 +00:00
|
|
|
(l, cleanup) <- inRepo $ Git.LsTree.lsTree headRef
|
2016-01-01 19:16:16 +00:00
|
|
|
forM_ l $ \i ->
|
|
|
|
when (isregfile i) $
|
|
|
|
maybe noop (add h i)
|
2016-01-01 19:56:24 +00:00
|
|
|
=<< catKey (Git.LsTree.sha i)
|
2016-01-01 19:50:59 +00:00
|
|
|
liftIO $ void cleanup
|
2016-01-01 19:09:42 +00:00
|
|
|
where
|
2016-01-11 19:52:11 +00:00
|
|
|
dropallassociated h = liftIO $ flip SQL.queueDb h $
|
|
|
|
delete $ from $ \(_r :: SqlExpr (Entity SQL.Associated)) ->
|
2016-01-01 19:09:42 +00:00
|
|
|
return ()
|
2016-04-14 17:07:46 +00:00
|
|
|
isregfile i = case Git.Types.toBlobType (Git.LsTree.mode i) of
|
|
|
|
Just Git.Types.FileBlob -> True
|
|
|
|
Just Git.Types.ExecutableBlob -> True
|
|
|
|
_ -> False
|
2016-01-11 19:52:11 +00:00
|
|
|
add h i k = liftIO $ flip SQL.queueDb h $
|
|
|
|
void $ insertUnique $ SQL.Associated
|
2016-01-12 18:01:50 +00:00
|
|
|
(toIKey k)
|
Fix storing of filenames of v6 unlocked files when the filename is not representable in the current locale.
This is a mostly backwards compatable change. I broke backwards
compatability in the case where a filename starts with double-quote.
That seems likely to be very rare, and v6 unlocked files are a new feature
anyway, and fsck needs to fix missing associated file mappings anyway. So,
I decided that is good enough.
The encoding used is to just show the String when it contains a problem
character. While that adds some overhead to addAssociatedFile and
removeAssociatedFile, those are not called very often. This approach has
minimal decode overhead, because most filenames won't be encoded that way,
and it only has to look for the leading double-quote to skip the expensive
read. So, getAssociatedFiles remains fast.
I did consider using ByteString instead, but getting a FilePath converted
with all chars intact, even surrigates, is difficult, and it looks like
instance PersistField ByteString uses Text, which I don't trust for problem
encoded data. It would probably be slower too, and it would make the
database less easy to inspect manually.
2016-02-14 20:37:25 +00:00
|
|
|
(toSFilePath $ getTopFilePath $ Git.LsTree.file i)
|
2015-12-09 21:00:37 +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
|