fileneglect (empty) → 0.0.0
raw patch · 4 files changed
+103/−0 lines, 4 filesdep +basedep +hinotifydep +stmsetup-changed
Dependencies added: base, hinotify, stm
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- fileneglect.cabal +26/−0
- src/System/IDontNotify.hs +45/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2016, Athan Clark++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Athan Clark nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ fileneglect.cabal view
@@ -0,0 +1,26 @@+Name: fileneglect+Version: 0.0.0+Author: Athan Clark <athan.clark@gmail.com>+Maintainer: Athan Clark <athan.clark@gmail.com>+License: BSD3+License-File: LICENSE+Synopsis: Block thread until a file stops being modified+Description:+ See module for docs+Cabal-Version: >= 1.10+Build-Type: Simple+Category: Filesystem+++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: System.IDontNotify+ Build-Depends: base >= 4.8 && < 5+ , hinotify+ , stm++Source-Repository head+ Type: git+ Location: https://github.com/athanclark/fileneglect
+ src/System/IDontNotify.hs view
@@ -0,0 +1,45 @@+{-|+Module : System.IDontNotify+Copyright : (c) 2016 Athan Clark+License : BSD-style+Maintainer : athan.clark@gmail.com+Stability : experimental+Portability : GHC+-}++module System.IDontNotify where++import System.INotify+import Control.Concurrent (forkIO, threadDelay)+import Control.Concurrent.STM+import Control.Concurrent.STM.TSem+import Control.Monad (when, void)+++-- | Block execution until a file stops misbehaving:+--+-- > neglectFile "foo" 1000000+--+-- will wait until the modifications to file @foo@ happen in an interval greater+-- than one second.+neglectFile :: FilePath -- ^ File to watch+ -> Int -- ^ Decay threshold in picoseconds+ -> IO ()+neglectFile file time = do+ debouncer <- atomically $ newTSem 0+ notif <- initINotify+ expectation <- newTVarIO (0 :: Int)++ watcher <- addWatch notif [Modify] file $ \e -> void $ forkIO $ do+ e' <- atomically $ do+ modifyTVar expectation (+1)+ readTVar expectation+ print e+ threadDelay time+ e'' <- readTVarIO expectation+ when (e' == e'') $+ atomically $ signalTSem debouncer++ atomically $ waitTSem debouncer+ removeWatch watcher+ killINotify notif