2015-02-16 19:08:29 +00:00
|
|
|
{- types for SQL databases
|
|
|
|
-
|
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
|
|
|
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
|
2015-02-16 19:08:29 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
|
|
module Database.Types where
|
|
|
|
|
|
|
|
import Database.Persist.TH
|
|
|
|
import Data.Maybe
|
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
|
|
|
import Data.Char
|
2015-02-16 19:08:29 +00:00
|
|
|
|
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
|
|
|
import Utility.PartialPrelude
|
2017-02-24 17:42:30 +00:00
|
|
|
import Key
|
2015-12-09 21:00:37 +00:00
|
|
|
import Utility.InodeCache
|
2015-02-16 19:08:29 +00:00
|
|
|
|
|
|
|
-- A serialized Key
|
|
|
|
newtype SKey = SKey String
|
|
|
|
deriving (Show, Read)
|
|
|
|
|
|
|
|
toSKey :: Key -> SKey
|
|
|
|
toSKey = SKey . key2file
|
|
|
|
|
|
|
|
fromSKey :: SKey -> Key
|
2016-11-16 01:29:54 +00:00
|
|
|
fromSKey (SKey s) = fromMaybe (error $ "bad serialized Key " ++ s) (file2key s)
|
2015-02-16 19:08:29 +00:00
|
|
|
|
|
|
|
derivePersistField "SKey"
|
2015-12-09 21:00:37 +00:00
|
|
|
|
2016-01-12 18:01:50 +00:00
|
|
|
-- A Key index. More efficient than SKey, but its Read instance does not
|
|
|
|
-- work when it's used in any kind of complex data structure.
|
|
|
|
newtype IKey = IKey String
|
|
|
|
|
|
|
|
instance Read IKey where
|
|
|
|
readsPrec _ s = [(IKey s, "")]
|
|
|
|
|
|
|
|
instance Show IKey where
|
|
|
|
show (IKey s) = s
|
|
|
|
|
|
|
|
toIKey :: Key -> IKey
|
|
|
|
toIKey = IKey . key2file
|
|
|
|
|
|
|
|
fromIKey :: IKey -> Key
|
2016-11-16 01:29:54 +00:00
|
|
|
fromIKey (IKey s) = fromMaybe (error $ "bad serialized Key " ++ s) (file2key s)
|
2016-01-12 18:01:50 +00:00
|
|
|
|
|
|
|
derivePersistField "IKey"
|
|
|
|
|
2015-12-09 21:00:37 +00:00
|
|
|
-- A serialized InodeCache
|
|
|
|
newtype SInodeCache = I String
|
|
|
|
deriving (Show, Read)
|
|
|
|
|
|
|
|
toSInodeCache :: InodeCache -> SInodeCache
|
|
|
|
toSInodeCache = I . showInodeCache
|
|
|
|
|
|
|
|
fromSInodeCache :: SInodeCache -> InodeCache
|
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
|
|
|
fromSInodeCache (I s) = fromMaybe (error $ "bad serialized InodeCache " ++ s) (readInodeCache s)
|
2015-12-09 21:00:37 +00:00
|
|
|
|
|
|
|
derivePersistField "SInodeCache"
|
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
|
|
|
|
|
|
|
-- A serialized FilePath.
|
|
|
|
--
|
|
|
|
-- Not all unicode characters round-trip through sqlite. In particular,
|
|
|
|
-- surrigate code points do not. So, escape the FilePath. But, only when
|
|
|
|
-- it contains such characters.
|
|
|
|
newtype SFilePath = SFilePath String
|
|
|
|
|
|
|
|
-- Note that Read instance does not work when used in any kind of complex
|
|
|
|
-- data structure.
|
|
|
|
instance Read SFilePath where
|
|
|
|
readsPrec _ s = [(SFilePath s, "")]
|
|
|
|
|
|
|
|
instance Show SFilePath where
|
|
|
|
show (SFilePath s) = s
|
|
|
|
|
|
|
|
toSFilePath :: FilePath -> SFilePath
|
|
|
|
toSFilePath s@('"':_) = SFilePath (show s)
|
|
|
|
toSFilePath s
|
|
|
|
| any needsescape s = SFilePath (show s)
|
|
|
|
| otherwise = SFilePath s
|
|
|
|
where
|
|
|
|
needsescape c = case generalCategory c of
|
|
|
|
Surrogate -> True
|
|
|
|
PrivateUse -> True
|
|
|
|
NotAssigned -> True
|
|
|
|
_ -> False
|
|
|
|
|
|
|
|
fromSFilePath :: SFilePath -> FilePath
|
|
|
|
fromSFilePath (SFilePath s@('"':_)) =
|
|
|
|
fromMaybe (error "bad serialized SFilePath " ++ s) (readish s)
|
|
|
|
fromSFilePath (SFilePath s) = s
|
|
|
|
|
|
|
|
derivePersistField "SFilePath"
|
|
|
|
|