Added file locking to filesystem backend
Getty Ritter
9 years ago
| 5 | 5 | import qualified Data.ByteString.Char8 as BS |
| 6 | 6 | import Database.Tansu.Internal (Database(..)) |
| 7 | 7 | import System.Directory (createDirectoryIfMissing, doesFileExist) |
| 8 | import System.FileLock (SharedExclusive(Exclusive), withFileLock) | |
| 8 | 9 | import System.FilePath.Posix ((</>)) |
| 9 | 10 | |
| 10 | 11 | filePathSet :: FilePath -> ByteString -> ByteString -> IO () |
| 20 | 21 | then Just `fmap` BS.readFile keyPath |
| 21 | 22 | else return Nothing |
| 22 | 23 | |
| 24 | filePathLock :: FilePath -> IO a -> IO a | |
| 25 | filePathLock path comp = do | |
| 26 | withFileLock (path </> ".lock") Exclusive (const comp) | |
| 27 | ||
| 23 | 28 | withFilesystemDb :: FilePath -> (Database -> IO a) -> IO a |
| 24 | 29 | withFilesystemDb path comp = do |
| 25 | 30 | createDirectoryIfMissing True path |
| 26 | comp $ Database { dbSet = filePathSet path | |
| 27 | , dbGet = filePathGet path | |
| 28 |
|
|
| 31 | comp $ Database { dbSet = filePathSet path | |
| 32 | , dbGet = filePathGet path | |
| 33 | , dbRunTransaction = filePathLock path | |
| 29 | 34 | } |