avoid over-long filenames for side lock files
This commit is contained in:
parent
c8cf30d81d
commit
85345abe8b
1 changed files with 10 additions and 3 deletions
|
@ -34,6 +34,7 @@ import Data.List
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import Network.BSD
|
import Network.BSD
|
||||||
import System.FilePath
|
import System.FilePath
|
||||||
|
import Data.Hash.MD5
|
||||||
|
|
||||||
type LockFile = FilePath
|
type LockFile = FilePath
|
||||||
|
|
||||||
|
@ -59,9 +60,7 @@ readPidLock lockfile = (readish =<<) <$> catchMaybeIO (readFile lockfile)
|
||||||
-- root filesystem doesn't support posix locks.
|
-- root filesystem doesn't support posix locks.
|
||||||
trySideLock :: LockFile -> (Maybe Posix.LockHandle -> IO a) -> IO a
|
trySideLock :: LockFile -> (Maybe Posix.LockHandle -> IO a) -> IO a
|
||||||
trySideLock lockfile a = do
|
trySideLock lockfile a = do
|
||||||
f <- absPath lockfile
|
sidelock <- sideLockFile lockfile
|
||||||
let sidelock = "/dev/shm" </>
|
|
||||||
intercalate "_" (splitDirectories (makeRelative "/" f)) ++ ".lck"
|
|
||||||
mlck <- catchDefaultIO Nothing $
|
mlck <- catchDefaultIO Nothing $
|
||||||
withUmask nullFileMode $
|
withUmask nullFileMode $
|
||||||
Posix.tryLockExclusive (Just mode) sidelock
|
Posix.tryLockExclusive (Just mode) sidelock
|
||||||
|
@ -73,6 +72,14 @@ trySideLock lockfile a = do
|
||||||
-- lock file there, so could not delete a stale lock.
|
-- lock file there, so could not delete a stale lock.
|
||||||
mode = combineModes (readModes ++ writeModes)
|
mode = combineModes (readModes ++ writeModes)
|
||||||
|
|
||||||
|
sideLockFile :: LockFile -> IO LockFile
|
||||||
|
sideLockFile lockfile = do
|
||||||
|
f <- absPath lockfile
|
||||||
|
let base = intercalate "_" (splitDirectories (makeRelative "/" f))
|
||||||
|
let shortbase = reverse $ take 32 $ reverse base
|
||||||
|
let md5 = if base == shortbase then "" else md5s (Str base)
|
||||||
|
return $ "/dev/shm" </> md5 ++ shortbase ++ ".lck"
|
||||||
|
|
||||||
-- | Tries to take a lock; does not block when the lock is already held.
|
-- | Tries to take a lock; does not block when the lock is already held.
|
||||||
--
|
--
|
||||||
-- The method used is atomic even on NFS without needing O_EXCL support.
|
-- The method used is atomic even on NFS without needing O_EXCL support.
|
||||||
|
|
Loading…
Add table
Reference in a new issue