2014-01-28 17:33:47 +00:00
|
|
|
{- Windows lock files
|
|
|
|
-
|
2021-01-13 18:38:35 +00:00
|
|
|
- Copyright 2014,2021 Joey Hess <id@joeyh.name>
|
2014-01-28 17:33:47 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2014-01-28 17:33:47 +00:00
|
|
|
-}
|
|
|
|
|
2021-01-13 18:38:35 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2014-08-20 20:45:58 +00:00
|
|
|
module Utility.LockFile.Windows (
|
2014-01-28 17:33:47 +00:00
|
|
|
lockShared,
|
|
|
|
lockExclusive,
|
|
|
|
dropLock,
|
2014-01-28 18:17:14 +00:00
|
|
|
waitToLock,
|
|
|
|
LockHandle
|
2014-01-28 17:33:47 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import System.Win32.Types
|
|
|
|
import System.Win32.File
|
|
|
|
import Control.Concurrent
|
2021-01-13 18:38:35 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import qualified System.FilePath.Windows.ByteString as P
|
2014-01-28 17:33:47 +00:00
|
|
|
|
2020-10-29 14:33:12 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2021-01-13 18:38:35 +00:00
|
|
|
import Utility.Split
|
|
|
|
import Utility.Path.AbsRel
|
2020-10-29 14:33:12 +00:00
|
|
|
|
|
|
|
type LockFile = RawFilePath
|
2014-01-28 17:33:47 +00:00
|
|
|
|
|
|
|
type LockHandle = HANDLE
|
|
|
|
|
|
|
|
{- Tries to lock a file with a shared lock, which allows other processes to
|
2015-05-18 18:16:49 +00:00
|
|
|
- also lock it shared. Fails if the file is exclusively locked. -}
|
2014-01-28 17:33:47 +00:00
|
|
|
lockShared :: LockFile -> IO (Maybe LockHandle)
|
2020-11-13 17:34:28 +00:00
|
|
|
lockShared = openLock fILE_SHARE_READ
|
2014-01-28 17:33:47 +00:00
|
|
|
|
|
|
|
{- Tries to take an exclusive lock on a file. Fails if another process has
|
2014-08-20 20:45:58 +00:00
|
|
|
- a shared or exclusive lock.
|
|
|
|
-
|
|
|
|
- Note that exclusive locking also prevents the file from being opened for
|
2015-05-17 18:22:14 +00:00
|
|
|
- read or write by any other process. So for advisory locking of a file's
|
|
|
|
- content, a separate LockFile should be used. -}
|
2014-01-28 17:33:47 +00:00
|
|
|
lockExclusive :: LockFile -> IO (Maybe LockHandle)
|
2020-11-13 17:34:28 +00:00
|
|
|
lockExclusive = openLock fILE_SHARE_NONE
|
2014-01-28 17:33:47 +00:00
|
|
|
|
|
|
|
{- Windows considers just opening a file enough to lock it. This will
|
|
|
|
- create the LockFile if it does not already exist.
|
|
|
|
-
|
2017-02-11 09:38:49 +00:00
|
|
|
- Will fail if the file is already open with an incompatible ShareMode.
|
2014-01-28 17:33:47 +00:00
|
|
|
- Note that this may happen if an unrelated process, such as a virus
|
|
|
|
- scanner, even looks at the file. See http://support.microsoft.com/kb/316609
|
|
|
|
-
|
|
|
|
- Note that createFile busy-waits to try to avoid failing when some other
|
|
|
|
- process briefly has a file open. But that would make checking locks
|
|
|
|
- much more expensive, so is not done here. Thus, the use of c_CreateFile.
|
2014-08-20 15:25:07 +00:00
|
|
|
-
|
|
|
|
- Also, passing Nothing for SECURITY_ATTRIBUTES ensures that the lock file
|
2015-05-17 18:22:14 +00:00
|
|
|
- is not inherited by any child process.
|
2014-01-28 17:33:47 +00:00
|
|
|
-}
|
|
|
|
openLock :: ShareMode -> LockFile -> IO (Maybe LockHandle)
|
|
|
|
openLock sharemode f = do
|
2021-01-13 18:38:35 +00:00
|
|
|
f' <- convertToNativeNamespace f
|
|
|
|
h <- withTString (fromRawFilePath f') $ \c_f ->
|
2014-08-20 15:25:07 +00:00
|
|
|
c_CreateFile c_f gENERIC_READ sharemode security_attributes
|
2014-01-28 17:33:47 +00:00
|
|
|
oPEN_ALWAYS fILE_ATTRIBUTE_NORMAL (maybePtr Nothing)
|
|
|
|
return $ if h == iNVALID_HANDLE_VALUE
|
|
|
|
then Nothing
|
|
|
|
else Just h
|
2014-08-20 15:25:07 +00:00
|
|
|
where
|
|
|
|
security_attributes = maybePtr Nothing
|
2014-01-28 17:33:47 +00:00
|
|
|
|
2021-01-13 18:38:35 +00:00
|
|
|
{- Convert a filepath to use Windows's native namespace.
|
|
|
|
- This avoids filesystem length limits.
|
|
|
|
-
|
|
|
|
- This is similar to the way base converts filenames on windows,
|
|
|
|
- but as that is implemented in C (create_device_name) and not
|
|
|
|
- exported, it cannot be used here. Several edge cases are not handled,
|
|
|
|
- including network shares and dos short paths.
|
|
|
|
-}
|
|
|
|
convertToNativeNamespace :: RawFilePath -> IO RawFilePath
|
|
|
|
convertToNativeNamespace f
|
|
|
|
| win32_dev_namespace `B.isPrefixOf` f = return f
|
|
|
|
| win32_file_namespace `B.isPrefixOf` f = return f
|
|
|
|
| nt_device_namespace `B.isPrefixOf` f = return f
|
|
|
|
| otherwise = do
|
|
|
|
-- Make absolute because any '.' and '..' in the path
|
|
|
|
-- will not be resolved once it's converted.
|
|
|
|
p <- absPath f
|
|
|
|
-- Normalize slashes.
|
|
|
|
let p' = P.normalise p
|
|
|
|
return (win32_file_namespace <> p')
|
|
|
|
where
|
|
|
|
|
|
|
|
win32_dev_namespace = "\\\\.\\"
|
|
|
|
win32_file_namespace = "\\\\?\\"
|
|
|
|
nt_device_namespace = "\\Device\\"
|
|
|
|
|
2014-01-28 17:33:47 +00:00
|
|
|
dropLock :: LockHandle -> IO ()
|
|
|
|
dropLock = closeHandle
|
|
|
|
|
|
|
|
{- If the initial lock fails, this is a BUSY wait, and does not
|
|
|
|
- guarentee FIFO order of waiters. In other news, Windows is a POS. -}
|
2015-05-22 17:50:37 +00:00
|
|
|
waitToLock :: IO (Maybe lockhandle) -> IO lockhandle
|
2014-01-28 17:33:47 +00:00
|
|
|
waitToLock locker = takelock
|
|
|
|
where
|
|
|
|
takelock = go =<< locker
|
|
|
|
go (Just lck) = return lck
|
|
|
|
go Nothing = do
|
|
|
|
threadDelay (500000) -- half a second
|
|
|
|
takelock
|