1{-# LANGUAGE CPP, NamedFieldPuns #-}
2--cross-platform file locking utilizing POSIX file locking on Unix/Linux and Windows file locking
3--hackage's System.FileLock doesn't support POSIX advisory locks nor locking file based on file descriptors, hence this needless rewrite
4module ProjectM36.FileLock where
7#if defined(mingw32_HOST_OS)
8import System.Win32.Types
9import Foreign.Marshal.Alloc
10import System.Win32.File
11import System.Win32.Mem
14#if defined(i386_HOST_ARCH)
15# define WINDOWS_CCONV stdcall
16#elif defined(x86_64_HOST_ARCH)
17# define WINDOWS_CCONV ccall
19# error Unknown mingw32 arch
21foreign import WINDOWS_CCONV "LockFileEx" c_lockFileEx :: HANDLE -> DWORD -> DWORD -> DWORD -> DWORD -> LPOVERLAPPED -> IO BOOL
23foreign import WINDOWS_CCONV "UnlockFileEx" c_unlockFileEx :: HANDLE -> DWORD -> DWORD -> DWORD -> LPOVERLAPPED -> IO BOOL
27openLockFile :: FilePath -> IO LockFile
28openLockFile path = createFile path
29 (gENERIC_READ .|. gENERIC_WRITE)
30 (fILE_SHARE_READ .|. fILE_SHARE_WRITE)
36closeLockFile :: LockFile -> IO ()
37closeLockFile file = do
41--swiped from System.FileLock package
42lockFile :: HANDLE -> LockType -> IO ()
43lockFile winHandle lock = do
44 let exFlag = case lock of
47 blockFlag = 0 --always block
48 sizeof_OVERLAPPED = 32
50 allocaBytes sizeof_OVERLAPPED $ \op -> do
51 zeroMemory op $ fromIntegral sizeof_OVERLAPPED
52 res <- c_lockFileEx winHandle (exFlag .|. blockFlag) 0 1 0 op
56 error "failed to wait for database lock"
58unlockFile :: HANDLE -> IO ()
59unlockFile winHandle = do
60 let sizeof_OVERLAPPED = 32
61 allocaBytes sizeof_OVERLAPPED $ \op -> do
62 zeroMemory op $ fromIntegral sizeof_OVERLAPPED
63 res <- c_unlockFileEx winHandle 0 1 0 op
67 error ("failed to unlock database lock: " ++ show res)
70--all of this complicated nonsense is fixed if we switch to GHC 8.2 which includes native flock support on handles
71import qualified System.Posix.IO as P
72import System.Posix.Types
73import System.Posix.Files
76lockStruct :: P.LockRequest -> P.FileLock
77lockStruct req = (req, AbsoluteSeek, 0, 0)
79newtype LockFile = LockFile Fd
81--we cannot use openFile from System.IO because it implements complicated locking which prevents opening the same file twice in write mode in the same process with no way to bypass the check.
82openLockFile :: FilePath -> IO LockFile
84 LockFile <$> P.createFile path ownerWriteMode
86closeLockFile :: LockFile -> IO ()
87closeLockFile l@(LockFile fd) = do
91--blocks on lock, if necessary
92lockFile :: LockFile -> LockType -> IO ()
93lockFile (LockFile fd) lock = do
94 let lockt = case lock of
95 WriteLock -> P.WriteLock
96 ReadLock -> P.ReadLock
97 P.waitToSetLock fd (lockStruct lockt)
99unlockFile :: LockFile -> IO ()
100unlockFile (LockFile fd) =
101 P.waitToSetLock fd (lockStruct P.Unlock)
104data LockType = ReadLock | WriteLock
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list