From 2f9432b33bf5c8f9c89d8c8d3c255466fc3eb361 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 11 Apr 2014 22:18:20 -0400 Subject: reorganized lockFiles --- kiki.hs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'kiki.hs') diff --git a/kiki.hs b/kiki.hs index 414d185..e4c0dc4 100644 --- a/kiki.hs +++ b/kiki.hs @@ -723,17 +723,18 @@ readPacketsFromFile fname = do return $ decode input #endif +-- | Attempts to lock each file in the list. +-- Returns a list of locks and a list of filenames +-- that could not be locked. +lockFiles :: [FilePath] -> IO ( [(DotLock,FilePath)], [FilePath] ) lockFiles fs = do - let dolock f = do + ls <- forM fs $ \f -> do lk <- dotlock_create f 0 - let fail = return Nothing - dotake lk = do + v <- flip (maybe $ return Nothing) lk $ \lk -> do e <- dotlock_take lk (-1) - if e==0 then return (Just lk) - else fail - v <- maybe fail dotake lk + return $ if e==0 then Just lk + else Nothing return (v,f) - ls <- mapM dolock fs let (lks, fails) = partition (isJust . fst) ls return (map (\(lk,f)->(fromJust lk,f)) lks, map snd fails) -- cgit v1.2.3