Skip to content

Commit

Permalink
Use lukko for file-locking
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Nov 13, 2019
1 parent dc2114a commit 7f15d36
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 214 deletions.
201 changes: 0 additions & 201 deletions cabal-install/Distribution/Client/Compat/FileLock.hsc

This file was deleted.

37 changes: 32 additions & 5 deletions cabal-install/Distribution/Client/Store.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE RecordWildCards, NamedFieldPuns #-}
{-# LANGUAGE CPP, RecordWildCards, NamedFieldPuns #-}


-- | Management for the installed package store.
Expand All @@ -23,7 +23,6 @@ module Distribution.Client.Store (

import Prelude ()
import Distribution.Client.Compat.Prelude
import Distribution.Client.Compat.FileLock

import Distribution.Client.DistDirLayout
import Distribution.Client.RebuildMonad
Expand All @@ -41,8 +40,16 @@ import Control.Exception
import Control.Monad (forM_)
import System.FilePath
import System.Directory
import System.IO

#ifdef MIN_VERSION_lukko
import Lukko
#else
import System.IO (openFile, IOMode(ReadWriteMode), hClose)
import GHC.IO.Handle.Lock (hLock, hTryLock, LockMode(ExclusiveLock))
#if MIN_VERSION_base(4,11,0)
import GHC.IO.Handle.Lock (hUnlock)
#endif
#endif

-- $concurrency
--
Expand Down Expand Up @@ -235,6 +242,26 @@ withIncomingUnitIdLock verbosity StoreDirLayout{storeIncomingLock}
compid unitid action =
bracket takeLock releaseLock (\_hnd -> action)
where
#ifdef MIN_VERSION_lukko
takeLock
| fileLockingSupported = do
fd <- fdOpen (storeIncomingLock compid unitid)
gotLock <- fdTryLock fd ExclusiveLock
unless gotLock $ do
info verbosity $ "Waiting for file lock on store entry "
++ display compid </> display unitid
fdLock fd ExclusiveLock
return fd

-- if there's no locking, do nothing. Be careful on AIX.
| otherwise = return undefined -- :(

releaseLock fd
| fileLockingSupported = do
fdUnlock fd
fdClose fd
| otherwise = return ()
#else
takeLock = do
h <- openFile (storeIncomingLock compid unitid) ReadWriteMode
-- First try non-blocking, but if we would have to wait then
Expand All @@ -246,5 +273,5 @@ withIncomingUnitIdLock verbosity StoreDirLayout{storeIncomingLock}
hLock h ExclusiveLock
return h

releaseLock = hClose

releaseLock h = hUnlock h >> hClose h
#endif
7 changes: 3 additions & 4 deletions cabal-install/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ TAR_VER="0.5.1.0"; TAR_VER_REGEXP="0\.5\.([1-9]|1[0-9]|0\.[3-9]|0\.1[0-9])\.
# >= 0.5.0.3 && < 0.6
DIGEST_VER="0.0.1.2"; DIGEST_REGEXP="0\.0\.(1\.[2-9]|[2-9]\.?)"
# >= 0.0.1.2 && < 0.1
ZIP_ARCHIVE_VER="0.3.3"; ZIP_ARCHIVE_REGEXP="0\.3\.[3-9]"
# >= 0.3.3 && < 0.4
LUKKO_VER="0.1"; LUKKO_VER_REGEXP="0\.1\.?"

HACKAGE_URL="https://hackage.haskell.org/package"

Expand Down Expand Up @@ -471,7 +470,7 @@ info_pkg "edit-distance" ${EDIT_DISTANCE_VER} ${EDIT_DISTANCE_VER_REGEXP}
info_pkg "ed25519" ${ED25519_VER} ${ED25519_VER_REGEXP}
info_pkg "tar" ${TAR_VER} ${TAR_VER_REGEXP}
info_pkg "digest" ${DIGEST_VER} ${DIGEST_REGEXP}
info_pkg "zip-archive" ${ZIP_ARCHIVE_VER} ${ZIP_ARCHIVE_REGEXP}
info_pkg "lukko" ${LUKKO_VER} ${LUKKO_REGEXP}
info_pkg "hackage-security" ${HACKAGE_SECURITY_VER} \
${HACKAGE_SECURITY_VER_REGEXP}

Expand Down Expand Up @@ -509,7 +508,7 @@ do_pkg "edit-distance" ${EDIT_DISTANCE_VER} ${EDIT_DISTANCE_VER_REGEXP}
do_pkg "ed25519" ${ED25519_VER} ${ED25519_VER_REGEXP}
do_pkg "tar" ${TAR_VER} ${TAR_VER_REGEXP}
do_pkg "digest" ${DIGEST_VER} ${DIGEST_REGEXP}
do_pkg "zip-archive" ${ZIP_ARCHIVE_VER} ${ZIP_ARCHIVE_REGEXP}
do_pkg "lukko" ${LUKKO_VER} ${LUKKO_REGEXP}
do_pkg "hackage-security" ${HACKAGE_SECURITY_VER} \
${HACKAGE_SECURITY_VER_REGEXP}

Expand Down
12 changes: 10 additions & 2 deletions cabal-install/cabal-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Category: Distribution
Build-type: Custom
Extra-Source-Files:
README.md bash-completion/cabal bootstrap.sh changelog
tests/README.md

-- Generated with 'make gen-extra-source-files'
-- Do NOT edit this section manually; instead, run the script.
Expand Down Expand Up @@ -122,6 +121,11 @@ Flag debug-tracetree
default: False
manual: True

Flag lukko
description: Use @lukko@ for file-locking
default: True
manual: True

custom-setup
setup-depends:
Cabal >= 2.2,
Expand Down Expand Up @@ -176,7 +180,6 @@ executable cabal
Distribution.Client.CmdSdist
Distribution.Client.Compat.Directory
Distribution.Client.Compat.ExecutablePath
Distribution.Client.Compat.FileLock
Distribution.Client.Compat.FilePerms
Distribution.Client.Compat.Prelude
Distribution.Client.Compat.Process
Expand Down Expand Up @@ -352,6 +355,11 @@ executable cabal
else
build-depends: unix >= 2.5 && < 2.9

if flag(lukko)
build-depends: lukko >= 0.1 && <0.2
else
build-depends: base >= 4.10

if flag(debug-expensive-assertions)
cpp-options: -DDEBUG_EXPENSIVE_ASSERTIONS

Expand Down
12 changes: 10 additions & 2 deletions cabal-install/cabal-install.cabal.pp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
build-depends: Win32 >= 2 && < 3
else
build-depends: unix >= 2.5 && < 2.9

if flag(lukko)
build-depends: lukko >= 0.1 && <0.2
else
build-depends: base >= 4.10
%enddef
%def CABAL_COMPONENTCOMMON
default-language: Haskell2010
Expand Down Expand Up @@ -106,7 +111,6 @@
Distribution.Client.CmdSdist
Distribution.Client.Compat.Directory
Distribution.Client.Compat.ExecutablePath
Distribution.Client.Compat.FileLock
Distribution.Client.Compat.FilePerms
Distribution.Client.Compat.Prelude
Distribution.Client.Compat.Process
Expand Down Expand Up @@ -275,7 +279,6 @@
%endif
Extra-Source-Files:
README.md bash-completion/cabal bootstrap.sh changelog
tests/README.md
-- Generated with 'make gen-extra-source-files'
-- Do NOT edit this section manually; instead, run the script.
Expand Down Expand Up @@ -376,6 +379,11 @@
default: False
manual: True
Flag lukko
description: Use @lukko@ for file-locking
default: True
manual: True
%if CABAL_FLAG_LIB
%else
custom-setup
Expand Down

0 comments on commit 7f15d36

Please sign in to comment.