flesh out kqueue library
Have not tried to build this yet. But barring minor mistakes, I think it's good.
This commit is contained in:
parent
89fcee03d0
commit
90d565149a
3 changed files with 81 additions and 26 deletions
|
@ -25,6 +25,8 @@ import qualified Data.Map as M
|
||||||
|
|
||||||
type DirMap = M.Map Fd FilePath
|
type DirMap = M.Map Fd FilePath
|
||||||
|
|
||||||
|
data Kqueue = Kqueue Fd DirMap
|
||||||
|
|
||||||
{- Builds a map of directories in a tree, possibly pruning some.
|
{- Builds a map of directories in a tree, possibly pruning some.
|
||||||
- Opens each directory in the tree. -}
|
- Opens each directory in the tree. -}
|
||||||
scanRecursive :: FilePath -> (FilePath -> Bool) -> IO DirMap
|
scanRecursive :: FilePath -> (FilePath -> Bool) -> IO DirMap
|
||||||
|
@ -43,25 +45,33 @@ addSubDir dirmap dir prune = M.union dirmap <$> scanRecursive dir prune
|
||||||
removeSubDir :: FilePath -> DirMap -> DirMap
|
removeSubDir :: FilePath -> DirMap -> DirMap
|
||||||
removeSubDir dir = M.filter (not . dirContains dir)
|
removeSubDir dir = M.filter (not . dirContains dir)
|
||||||
|
|
||||||
foreign import ccall unsafe "libkqueue.h waitchange" c_waitchange
|
foreign import ccall unsafe "libkqueue.h init_kqueue" c_init_kqueue
|
||||||
:: Ptr Fd -> IO Fd
|
:: CInt -> Ptr Fd -> IO Fd
|
||||||
|
foreign import ccall unsafe "libkqueue.h waitchange_kqueue" c_waitchange_kqueue
|
||||||
|
:: Fd -> IO Fd
|
||||||
|
|
||||||
{- Waits for a change in a map of directories, and returns the directory
|
{- Initializes a Kqueue to watch a map of directories. -}
|
||||||
- where the change took place.
|
initKqueue :: DirMap -> IO Kqueue
|
||||||
|
initKqueue dirmap = withArrayLen (M.keys dirmap) $ \fdcnt c_fds ->
|
||||||
|
h <- c_init_kqueue (fromIntegral fdcnt) c_fds
|
||||||
|
return $ Kqueue h dirmap
|
||||||
|
|
||||||
|
{- Stops a Kqueue. Note: Does not directly close the Fds in the dirmap,
|
||||||
|
- so it can be reused. -}
|
||||||
|
stopKqueue :: Kqueue -> IO
|
||||||
|
stopKqueue (Kqueue h _) = closeFd h
|
||||||
|
|
||||||
|
{- Waits for a change on a Kqueue, and returns the directory
|
||||||
|
- or directories where a change took place.
|
||||||
-
|
-
|
||||||
- The kqueue interface does not tell what type of change took place in
|
- The kqueue interface does not tell what type of change took place in
|
||||||
- the directory; it could be an added file, a deleted file, a renamed
|
- the directory; it could be an added file, a deleted file, a renamed
|
||||||
- file, a new subdirectory, or a deleted subdirectory, or a moved
|
- file, a new subdirectory, or a deleted subdirectory, or a moved
|
||||||
- subdirectory.
|
- subdirectory.
|
||||||
-
|
-
|
||||||
- Note that if subdirectories have changed, the caller will want to
|
- Note that if subdirectories have changed, the caller should re-run
|
||||||
- update the map before calling this again. -}
|
- initKqueue to get them watched. -}
|
||||||
waitChange :: DirMap -> IO (Maybe FilePath)
|
waitChange :: Kqueue -> IO [FilePath]
|
||||||
waitChange dirmap = withArray (M.keys dirmap) $ \c_fds -> do
|
waitChange (Kqueue h dirmap) = do
|
||||||
changed <- c_waitchange c_fds
|
changed <- c_waitchange_kqueue h
|
||||||
ifM (safeErrno <$> getErrno)
|
return $ M.lookup changed dirmap
|
||||||
( return $ M.lookup changed dirmap
|
|
||||||
, return Nothing
|
|
||||||
)
|
|
||||||
where
|
|
||||||
safeErrno (Errno v) = v == 0
|
|
||||||
|
|
|
@ -5,18 +5,62 @@
|
||||||
* Licensed under the GNU GPL version 3 or higher.
|
* Licensed under the GNU GPL version 3 or higher.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/event.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
/* Waits for a change event on one of the array of directory fds,
|
/* Initializes a kqueue, with a list of fds to watch for changes.
|
||||||
* and returns the one that changed. */
|
* Returns the kqueue's handle. */
|
||||||
int waitchange(const int *fds) {
|
int init_kqueue(const int fdcnt, const int *fdlist) {
|
||||||
// if (kqueue(blah, &fds) != 0)
|
struct nodelay = {0, 0};
|
||||||
// return 0; /* errno is set */
|
int kq;
|
||||||
// else
|
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
printf("in waitchange!, %i %i\n", fds[0], fds[1]);
|
if ((kq = kqueue()) == -1) {
|
||||||
|
perror("kqueue");
|
||||||
return fds[0];
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prime the pump with the list of fds, but don't wait for any
|
||||||
|
* change events. */
|
||||||
|
helper(kq, fdcnt, fdlist, &nodelay);
|
||||||
|
|
||||||
|
return kq;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Waits for a change event on a kqueue.
|
||||||
|
*
|
||||||
|
* Returns the fd that changed, or -1 on error.
|
||||||
|
*/
|
||||||
|
signed int waitchange_kqueue(const int kq) {
|
||||||
|
helper(kq, 0, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The specified fds are added to the set of fds being watched for changes.
|
||||||
|
* Fds passed to prior calls still take effect, so it's most efficient to
|
||||||
|
* not pass the same fds repeatedly.
|
||||||
|
*/
|
||||||
|
signed int helper(const int kq, const int fdcnt, const int *fdlist, cont struct *timeout) {
|
||||||
|
int i, nev;
|
||||||
|
struct kevent evlist[1];
|
||||||
|
struct kevent chlist[fdcnt];
|
||||||
|
|
||||||
|
for (i = 0; i < fdcnt; i++) {
|
||||||
|
EV_SET(&chlist[i], fdlist[i], EVFILT_VNODE,
|
||||||
|
EV_ADD | EV_ENABLE | EV_CLEAR,
|
||||||
|
NOTE_WRITE,
|
||||||
|
1,
|
||||||
|
timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
nev = kevent(info->kq, info->chlist, info->cnt, info->evlist,
|
||||||
|
1, NULL);
|
||||||
|
|
||||||
|
if (nev == 1)
|
||||||
|
return evlist[0].ident;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
int waitchange(const int *fds);
|
int init_kqueue(const int fdcnt, const int *fdlist);
|
||||||
|
signed int waitchange_kqueue(const int kq);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue