File: Win32Notify.hs

package info (click to toggle)
git-annex 10.20251029-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,300 kB
  • sloc: haskell: 91,492; javascript: 9,103; sh: 1,593; makefile: 216; perl: 137; ansic: 44
file content (68 lines) | stat: -rw-r--r-- 2,031 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{- Win32-notify interface
 -
 - Copyright 2013 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

module Utility.DirWatcher.Win32Notify (watchDir) where

import Common
import Utility.DirWatcher.Types
import qualified Utility.RawFilePath as R

import System.Win32.Notify
import System.PosixCompat.Files (isRegularFile)

watchDir :: OsPath -> (OsPath -> Bool) -> Bool -> WatchHooks -> IO WatchManager
watchDir dir ignored scanevents hooks = do
	scan dir
	wm <- initWatchManager
	void $ watchDirectory wm (fromOsPath dir) True [Create, Delete, Modify, Move] dispatch
	return wm
  where
	dispatch evt
		| ignoredPath ignored (toOsPath (filePath evt)) = noop
		| otherwise = case evt of
			(Deleted _ _)
				| isDirectory evt -> runhook delDirHook Nothing
				| otherwise -> runhook delHook Nothing
			(Created _ _)
				| isDirectory evt -> noop
				| otherwise -> runhook addHook Nothing
			(Modified _ _)
				| isDirectory evt -> noop
				{- Add hooks are run when a file is modified for 
				 - compatibility with INotify, which calls the add
				 - hook when a file is closed, and so tends to call
				 - both add and modify for file modifications. -}
				| otherwise -> do
					runhook addHook Nothing
					runhook modifyHook Nothing
	  where
		runhook h s = maybe noop (\a -> a (toOsPath (filePath evt)) s) (h hooks)

	scan d = unless (ignoredPath ignored d) $
		mapM_ go =<< emptyWhenDoesNotExist
			(dirContentsRecursiveSkipping (const False) False d)
	  where		
		go f
			| ignoredPath ignored f = noop
			| otherwise = do
				ms <- getstatus f
				case ms of
					Nothing -> noop
					Just s
						| isRegularFile s ->
							when scanevents $
								runhook addHook ms
						| otherwise ->
							noop
		  where
			runhook h s = maybe noop (\a -> a f s) (h hooks)
		
	getstatus = catchMaybeIO . R.getFileStatus . fromOsPath

{- Check each component of the path to see if it's ignored. -}
ignoredPath :: (OsPath -> Bool) -> OsPath -> Bool
ignoredPath ignored = any ignored . map dropTrailingPathSeparator . splitPath