Skip to content

Commit d91afd3

Browse files
snoyberghvr
authored andcommitted
Use file instead of dir locking #187 (#203)
* Use file instead of dir locking #187 This commit simply imports the code from the filelock package verbatim into a subdirectory, filelock. Depending on filelock as an external package instead would be more straightforward, but I'm not sure what the rules for external dependencies are here. * Switch to upstream filelock Given that the extra dependency doesn't seem to be a problem, remove the inlined code. If in fact the dependency should be avoided, just ignore this commit and use the parent.
1 parent 71a24d6 commit d91afd3

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

hackage-security/hackage-security.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ library
103103
Cabal >= 1.14 && < 2.2,
104104
containers >= 0.4 && < 0.6,
105105
ed25519 >= 0.0 && < 0.1,
106+
filelock >= 0.1.1 && < 0.2,
106107
filepath >= 1.2 && < 1.5,
107108
mtl >= 2.2 && < 2.3,
108109
parsec >= 3.1 && < 3.2,

hackage-security/src/Hackage/Security/Util/IO.hs

+11-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Control.Exception
1111
import Data.Time
1212
import System.IO hiding (openTempFile, withFile)
1313
import System.IO.Error
14+
import qualified System.FileLock as FL
1415

1516
import Hackage.Security.Util.Path
1617

@@ -30,22 +31,19 @@ handleDoesNotExist act =
3031
then return Nothing
3132
else throwIO e
3233

33-
-- | Attempt to create a filesystem lock in the specified directory
34+
-- | Attempt to create a filesystem lock in the specified directory.
3435
--
35-
-- Given a file @/path/to@, we do this by attempting to create the directory
36-
-- @//path/to/hackage-security-lock@, and deleting the directory again
37-
-- afterwards. Creating a directory that already exists will throw an exception
38-
-- on most OSs (certainly Linux, OSX and Windows) and is a reasonably common way
39-
-- to implement a lock file.
36+
-- This will use OS-specific file locking primitives, and throw an
37+
-- exception if the lock is already present.
4038
withDirLock :: Path Absolute -> IO a -> IO a
41-
withDirLock dir = bracket_ takeLock releaseLock
39+
withDirLock dir act = do
40+
res <- FL.withTryFileLock lock FL.Exclusive (const act)
41+
case res of
42+
Just a -> return a
43+
Nothing -> error $ "withFileLock: lock already exists: " ++ lock
4244
where
43-
lock :: Path Absolute
44-
lock = dir </> fragment "hackage-security-lock"
45-
46-
takeLock, releaseLock :: IO ()
47-
takeLock = createDirectory lock
48-
releaseLock = removeDirectory lock
45+
lock :: FilePath
46+
lock = toFilePath $ dir </> fragment "hackage-security-lock"
4947

5048
{-------------------------------------------------------------------------------
5149
Debugging

stack.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ packages:
1010
- precompute-fileinfo
1111
extra-deps:
1212
- http-client-0.5.5
13+
- filelock-0.1.1.2

0 commit comments

Comments
 (0)