diff -urN gio/kqueue/dep-list.c gio/kqueue/dep-list.c
--- gio/kqueue/dep-list.c	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/dep-list.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,521 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include <glib.h>
-
-#include <stdlib.h>  /* calloc */
-#include <stdio.h>   /* printf */
-#include <dirent.h>  /* opendir, readdir, closedir */
-#include <string.h>  /* strcmp */
-#include <assert.h>
-
-#include "dep-list.h"
-
-static gboolean kdl_debug_enabled = FALSE;
-#define perror_msg if (kdl_debug_enabled) g_warning
-
-
-/**
- * Print a list to stdout.
- *
- * @param[in] dl A pointer to a list.
- **/
-void
-dl_print (const dep_list *dl)
-{
-    while (dl != NULL) {
-        printf ("%lld:%s ", (long long int) dl->inode, dl->path);
-        dl = dl->next;
-    }
-    printf ("\n");
-}
-
-/**
- * Create a new list item.
- *
- * Create a new list item and initialize its fields.
- *
- * @param[in] path  A name of a file (the string is not copied!).
- * @param[in] inode A file's inode number.
- * @return A pointer to a new item or NULL in the case of error.
- **/
-dep_list* dl_create (char *path, ino_t inode)
-{
-    dep_list *dl = calloc (1, sizeof (dep_list));
-    if (dl == NULL) {
-        perror_msg ("Failed to create a new dep-list item");
-        return NULL;
-    }
-
-    dl->path = path;
-    dl->inode = inode;
-    return dl;
-}
-
-/**
- * Create a shallow copy of a list.
- *
- * A shallow copy is a copy of a structure, but not the copy of the
- * contents. All data pointers ('path' in our case) of a list and its
- * shallow copy will point to the same memory.
- *
- * @param[in] dl A pointer to list to make a copy. May be NULL.
- * @return A shallow copy of the list.
- **/ 
-dep_list*
-dl_shallow_copy (const dep_list *dl)
-{
-    if (dl == NULL) {
-        return NULL;
-    }
-
-    dep_list *head = calloc (1, sizeof (dep_list));
-    if (head == NULL) {
-        perror_msg ("Failed to allocate head during shallow copy");
-        return NULL;
-    }
-
-    dep_list *cp = head;
-    const dep_list *it = dl;
-
-    while (it != NULL) {
-        cp->path = it->path;
-        cp->inode = it->inode;
-        if (it->next) {
-            cp->next = calloc (1, sizeof (dep_list));
-            if (cp->next == NULL) {
-                perror_msg ("Failed to allocate a new element during shallow copy");
-                dl_shallow_free (head);
-                return NULL;
-            }
-            cp = cp->next;
-        }
-        it = it->next;
-    }
-
-    return head;
-}
-
-/**
- * Free the memory allocated for shallow copy.
- *
- * This function will free the memory used by a list structure, but
- * the list data will remain in the heap.
- *
- * @param[in] dl A pointer to a list. May be NULL.
- **/
-void
-dl_shallow_free (dep_list *dl)
-{
-    while (dl != NULL) {
-        dep_list *ptr = dl;
-        dl = dl->next;
-        free (ptr);
-    }
-}
-
-/**
- * Free the memory allocated for a list.
- *
- * This function will free all the memory used by a list: both
- * list structure and the list data.
- *
- * @param[in] dl A pointer to a list. May be NULL.
- **/
-void
-dl_free (dep_list *dl)
-{
-    while (dl != NULL) {
-        dep_list *ptr = dl;
-        dl = dl->next;
-
-        free (ptr->path);
-        free (ptr);
-    }
-}
-
-/**
- * Create a directory listing and return it as a list.
- *
- * @param[in] path A path to a directory.
- * @return A pointer to a list. May return NULL, check errno in this case.
- **/
-dep_list*
-dl_listing (const char *path)
-{
-    assert (path != NULL);
-
-    dep_list *head = NULL;
-    dep_list *prev = NULL;
-    DIR *dir = opendir (path);
-    if (dir != NULL) {
-        struct dirent *ent;
-
-        while ((ent = readdir (dir)) != NULL) {
-            if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, "..")) {
-                continue;
-            }
-
-            if (head == NULL) {
-                head = calloc (1, sizeof (dep_list));
-                if (head == NULL) {
-                    perror_msg ("Failed to allocate head during listing");
-                    goto error;
-                }
-            }
-
-            dep_list *iter = (prev == NULL) ? head : calloc (1, sizeof (dep_list));
-            if (iter == NULL) {
-                perror_msg ("Failed to allocate a new element during listing");
-                goto error;
-            }
-
-            iter->path = strdup (ent->d_name);
-            if (iter->path == NULL) {
-                perror_msg ("Failed to copy a string during listing");
-                goto error;
-            }
-
-            iter->inode = ent->d_ino;
-            iter->next = NULL;
-            if (prev) {
-                prev->next = iter;
-            }
-            prev = iter;
-        }
-
-        closedir (dir);
-    }
-    return head;
-
-error:
-    if (dir != NULL) {
-        closedir (dir);
-    }
-    dl_free (head);
-    return NULL;
-}
-
-/**
- * Perform a diff on lists.
- *
- * This function performs something like a set intersection. The same items
- * will be removed from the both lists. Items are comapred by a filename.
- * 
- * @param[in,out] before A pointer to a pointer to a list. Will contain items
- *     which were not found in the 'after' list.
- * @param[in,out] after  A pointer to a pointer to a list. Will containt items
- *     which were not found in the 'before' list.
- **/
-void
-dl_diff (dep_list **before, dep_list **after)
-{
-    assert (before != NULL);
-    assert (after != NULL);
-
-    if (*before == NULL || *after == NULL) {
-        return;
-    }
-
-    dep_list *before_iter = *before;
-    dep_list *before_prev = NULL;
-
-    while (before_iter != NULL) {
-        dep_list *after_iter = *after;
-        dep_list *after_prev = NULL;
-
-        int matched = 0;
-        while (after_iter != NULL) {
-            if (strcmp (before_iter->path, after_iter->path) == 0) {
-                matched = 1;
-                /* removing the entry from the both lists */
-                if (before_prev) {
-                    before_prev->next = before_iter->next;
-                } else {
-                    *before = before_iter->next;
-                }
-
-                if (after_prev) {
-                    after_prev->next = after_iter->next;
-                } else {
-                    *after = after_iter->next;
-                }
-                free (after_iter);
-                break;
-            }
-            after_prev = after_iter;
-            after_iter = after_iter->next;
-        }
-
-        dep_list *oldptr = before_iter;
-        before_iter = before_iter->next;
-        if (matched == 0) {
-            before_prev = oldptr;
-        } else {
-            free (oldptr);
-        }
-    }
-}
-
-
-/**
- * Traverses two lists. Compares items with a supplied expression
- * and performs the passed code on a match. Removes the matched entries
- * from the both lists.
- **/
-#define EXCLUDE_SIMILAR(removed_list, added_list, match_expr, matched_code) \
-    assert (removed_list != NULL);                                      \
-    assert (added_list != NULL);                                        \
-                                                                        \
-    dep_list *removed_list##_iter = *removed_list;                      \
-    dep_list *removed_list##_prev = NULL;                               \
-                                                                        \
-    int productive = 0;                                                 \
-                                                                        \
-    while (removed_list##_iter != NULL) {                               \
-        dep_list *added_list##_iter = *added_list;                      \
-        dep_list *added_list##_prev = NULL;                             \
-                                                                        \
-        int matched = 0;                                                \
-        while (added_list##_iter != NULL) {                             \
-            if (match_expr) {                                           \
-                matched = 1;                                            \
-                ++productive;                                           \
-                matched_code;                                           \
-                                                                        \
-                if (removed_list##_prev) {                              \
-                    removed_list##_prev->next = removed_list##_iter->next; \
-                } else {                                                \
-                    *removed_list = removed_list##_iter->next;          \
-                }                                                       \
-                if (added_list##_prev) {                                \
-                    added_list##_prev->next = added_list##_iter->next;  \
-                } else {                                                \
-                    *added_list = added_list##_iter->next;              \
-                }                                                       \
-                free (added_list##_iter);                               \
-                break;                                                  \
-            }                                                           \
-            added_list##_iter = added_list##_iter->next;                \
-        }                                                               \
-        dep_list *oldptr = removed_list##_iter;                         \
-        removed_list##_iter = removed_list##_iter->next;                \
-        if (matched == 0) {                                             \
-            removed_list##_prev = oldptr;                               \
-        } else {                                                        \
-            free (oldptr);                                              \
-        }                                                               \
-    }                                                                   \
-    return (productive > 0);
-
-
-#define cb_invoke(cbs, name, udata, ...) \
-    do { \
-        if (cbs->name) { \
-            (cbs->name) (udata, ## __VA_ARGS__); \
-        } \
-    } while (0)
-
-/**
- * Detect and notify about moves in the watched directory.
- *
- * A move is what happens when you rename a file in a directory, and
- * a new name is unique, i.e. you didnt overwrite any existing files
- * with this one.
- *
- * @param[in,out] removed  A list of the removed files in the directory.
- * @param[in,out] added    A list of the added files of the directory.
- * @param[in]     cbs      A pointer to #traverse_cbs, an user-defined set of 
- *     traverse callbacks.
- * @param[in]     udata    A pointer to the user-defined data.
- * @return 0 if no files were renamed, >0 otherwise.
-**/
-static int
-dl_detect_moves (dep_list           **removed, 
-                 dep_list           **added, 
-                 const traverse_cbs  *cbs, 
-                 void                *udata)
-{
-    assert (cbs != NULL);
-
-     EXCLUDE_SIMILAR
-        (removed, added,
-         (removed_iter->inode == added_iter->inode),
-         {
-             cb_invoke (cbs, moved, udata,
-                        removed_iter->path, removed_iter->inode,
-                        added_iter->path, added_iter->inode);
-         });
-}
-
-/**
- * Detect and notify about replacements in the watched directory.
- *
- * Consider you are watching a directory foo with the folloing files
- * insinde:
- *
- *    foo/bar
- *    foo/baz
- *
- * A replacement in a watched directory is what happens when you invoke
- *
- *    mv /foo/bar /foo/bar
- *
- * i.e. when you replace a file in a watched directory with another file
- * from the same directory.
- *
- * @param[in,out] removed  A list of the removed files in the directory.
- * @param[in,out] current  A list with the current contents of the directory.
- * @param[in]     cbs      A pointer to #traverse_cbs, an user-defined set of 
- *     traverse callbacks.
- * @param[in]     udata    A pointer to the user-defined data.
- * @return 0 if no files were renamed, >0 otherwise.
- **/
-static int
-dl_detect_replacements (dep_list           **removed,
-                        dep_list           **current,
-                        const traverse_cbs  *cbs,
-                        void                *udata)
-{
-    assert (cbs != NULL);
-
-    EXCLUDE_SIMILAR
-        (removed, current,
-         (removed_iter->inode == current_iter->inode),
-         {
-            cb_invoke (cbs, replaced, udata,
-                        removed_iter->path, removed_iter->inode,
-                        current_iter->path, current_iter->inode);
-         });
-}
-
-/**
- * Detect and notify about overwrites in the watched directory.
- *
- * Consider you are watching a directory foo with a file inside:
- *
- *    foo/bar
- *
- * And you also have a directory tmp with a file 1:
- * 
- *    tmp/1
- *
- * You do not watching directory tmp.
- *
- * An overwrite in a watched directory is what happens when you invoke
- *
- *    mv /tmp/1 /foo/bar
- *
- * i.e. when you overwrite a file in a watched directory with another file
- * from the another directory.
- *
- * @param[in,out] previous A list with the previous contents of the directory.
- * @param[in,out] current  A list with the current contents of the directory.
- * @param[in]     cbs      A pointer to #traverse_cbs, an user-defined set of 
- *     traverse callbacks.
- * @param[in]     udata    A pointer to the user-defined data.
- * @return 0 if no files were renamed, >0 otherwise.
- **/
-static int
-dl_detect_overwrites (dep_list           **previous,
-                      dep_list           **current,
-                      const traverse_cbs  *cbs,
-                      void                *udata)
-{
-    assert (cbs != NULL);
-
-    EXCLUDE_SIMILAR
-        (previous, current,
-         (strcmp (previous_iter->path, current_iter->path) == 0
-          && previous_iter->inode != current_iter->inode),
-         {
-             cb_invoke (cbs, overwritten, udata, current_iter->path, current_iter->inode);
-         });
-}
-
-
-/**
- * Traverse a list and invoke a callback for each item.
- * 
- * @param[in] list  A #dep_list.
- * @param[in] cb    A #single_entry_cb callback function.
- * @param[in] udata A pointer to the user-defined data.
- **/
-static void 
-dl_emit_single_cb_on (dep_list        *list,
-                      single_entry_cb  cb,
-                      void            *udata)
-{
-    while (cb && list != NULL) {
-        (cb) (udata, list->path, list->inode);
-        list = list->next;
-    }
-}
-
-
-/**
- * Recognize all the changes in the directory, invoke the appropriate callbacks.
- *
- * This is the core function of directory diffing submodule.
- *
- * @param[in] before The previous contents of the directory.
- * @param[in] after  The current contents of the directory.
- * @param[in] cbs    A pointer to user callbacks (#traverse_callbacks).
- * @param[in] udata  A pointer to user data.
- **/
-void
-dl_calculate (dep_list           *before,
-              dep_list           *after,
-              const traverse_cbs *cbs,
-              void               *udata)
-{
-    assert (cbs != NULL);
-
-    int need_update = 0;
-
-    dep_list *was = dl_shallow_copy (before);
-    dep_list *pre = dl_shallow_copy (before);
-    dep_list *now = dl_shallow_copy (after);
-    dep_list *lst = dl_shallow_copy (after);
-
-    dl_diff (&was, &now); 
-
-    need_update += dl_detect_moves (&was, &now, cbs, udata);
-    need_update += dl_detect_replacements (&was, &lst, cbs, udata);
-    dl_detect_overwrites (&pre, &lst, cbs, udata);
- 
-    if (need_update) {
-        cb_invoke (cbs, names_updated, udata);
-    }
-
-    dl_emit_single_cb_on (was, cbs->removed, udata);
-    dl_emit_single_cb_on (now, cbs->added, udata);
-
-    cb_invoke (cbs, many_added, udata, now);
-    cb_invoke (cbs, many_removed, udata, was);
-    
-    dl_shallow_free (lst);
-    dl_shallow_free (now);
-    dl_shallow_free (pre);
-    dl_shallow_free (was);
-}
-
diff -urN gio/kqueue/dep-list.h gio/kqueue/dep-list.h
--- gio/kqueue/dep-list.h	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/dep-list.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,69 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __DEP_LIST_H__
-#define __DEP_LIST_H__
-
-#include <sys/types.h> /* ino_t */
-
-typedef struct dep_list {
-    struct dep_list *next;
-
-    char *path;
-    ino_t inode;
-} dep_list;
-
-typedef void (* no_entry_cb)     (void *udata);
-typedef void (* single_entry_cb) (void *udata, const char *path, ino_t inode);
-typedef void (* dual_entry_cb)   (void *udata,
-                                  const char *from_path, ino_t from_inode,
-                                  const char *to_path,   ino_t to_inode);
-typedef void (* list_cb)         (void *udata, const dep_list *list);
-
-
-typedef struct traverse_cbs {
-    single_entry_cb  added;
-    single_entry_cb  removed;
-    dual_entry_cb    replaced;
-    single_entry_cb  overwritten;
-    dual_entry_cb    moved;
-    list_cb          many_added;
-    list_cb          many_removed;
-    no_entry_cb      names_updated;
-} traverse_cbs;
-
-dep_list* dl_create       (char *path, ino_t inode);
-void      dl_print        (const dep_list *dl);
-dep_list* dl_shallow_copy (const dep_list *dl);
-void      dl_shallow_free (dep_list *dl);
-void      dl_free         (dep_list *dl);
-dep_list* dl_listing      (const char *path);
-void      dl_diff         (dep_list **before, dep_list **after);
-
-void
-dl_calculate (dep_list            *before,
-              dep_list            *after,
-              const traverse_cbs  *cbs,
-              void                *udata);
-
-
-#endif /* __DEP_LIST_H__ */
diff -urN gio/kqueue/gkqueuefilemonitor.c gio/kqueue/gkqueuefilemonitor.c
--- gio/kqueue/gkqueuefilemonitor.c	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/gkqueuefilemonitor.c	2017-03-09 01:13:25.074624000 +0300
@@ -1,198 +1,203 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
+/*-
+ * Copyright (c) 2016 Rozhuk Ivan <rozhuk.im@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * Author: Rozhuk Ivan <rozhuk.im@gmail.com>
+ *
+ */
 
 #include "config.h"
 
-#include "gkqueuefilemonitor.h"
-#include "kqueue-helper.h"
-#include "kqueue-exclusions.h"
-#include <gio/gpollfilemonitor.h>
-#include <gio/gfile.h>
+#include <glib-object.h>
+#include <string.h>
+#include <gio/gfilemonitor.h>
+#include <gio/glocalfilemonitor.h>
 #include <gio/giomodule.h>
+#include "glib-private.h"
+#include <glib-unix.h>
+#include "kqueue_fnm.h"
 
 
-struct _GKqueueFileMonitor
-{
-  GLocalFileMonitor parent_instance;
+static GMutex			kqueue_lock;
+static GSource			*kqueue_source = NULL;
+static volatile kqueue_fnm_p	kqueue_fnm = NULL;
 
-  kqueue_sub *sub;
+#define G_TYPE_KQUEUE_FILE_MONITOR      (g_kqueue_file_monitor_get_type())
+#define G_KQUEUE_FILE_MONITOR(inst)     (G_TYPE_CHECK_INSTANCE_CAST((inst), \
+					 G_TYPE_KQUEUE_FILE_MONITOR, GKqueueFileMonitor))
 
-  GFileMonitor *fallback;
-  GFile *fbfile;
-};
+typedef GLocalFileMonitorClass	GKqueueFileMonitorClass;
 
-static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
+typedef struct {
+	GLocalFileMonitor	parent_instance;
+	kqueue_file_mon_data_p	fmd;
+} GKqueueFileMonitor;
 
+GType g_kqueue_file_monitor_get_type(void);
 G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
-       g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
+       g_io_extension_point_implement(G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
                g_define_type_id,
                "kqueue",
-               20))
+               10))
 
 
 static void
-_fallback_callback (GFileMonitor      *unused,
-                    GFile             *first,
-                    GFile             *second,
-                    GFileMonitorEvent  event,
-                    gpointer           udata)
-{
-  GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
-  GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
-  g_assert (kq_mon != NULL);
-  g_assert (mon != NULL);
-  (void) unused;
-
-  if (event == G_FILE_MONITOR_EVENT_CHANGED)
-    {
-      GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
-
-      _kh_dir_diff (kq_mon->sub, local_monitor->source);
-    }
-  else
-    g_file_monitor_emit_event (mon, first, second, event);
-}
-
-
-static void
-g_kqueue_file_monitor_finalize (GObject *object)
-{
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
-
-  if (kqueue_monitor->sub)
-    {
-      _kh_cancel_sub (kqueue_monitor->sub);
-      _kh_sub_free (kqueue_monitor->sub);
-      kqueue_monitor->sub = NULL;
-    }
-
-  if (kqueue_monitor->fallback)
-    g_object_unref (kqueue_monitor->fallback);
-
-  if (kqueue_monitor->fbfile)
-    g_object_unref (kqueue_monitor->fbfile);
-
-  if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
-    (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
-}
-
-static void
-g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor,
-                             const gchar *dirname,
-                             const gchar *basename,
-                             const gchar *filename,
-                             GFileMonitorSource *source)
-{
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
-  GObject *obj;
-  GKqueueFileMonitorClass *klass;
-  GObjectClass *parent_class;
-  kqueue_sub *sub = NULL;
-  gboolean ret_kh_startup = FALSE;
-  const gchar *path = NULL; 
-
-
-  ret_kh_startup = _kh_startup ();
-  g_assert (ret_kh_startup);
-
-  path = filename;
-  if (!path)
-    path = dirname;
-
-  /* For a directory monitor, create a subscription object anyway.
-   * It will be used for directory diff calculation routines. 
-   * Wait, directory diff in a GKqueueFileMonitor?
-   * Yes, it is. When a file monitor is started on an non-existent
-   * file, GIO uses a GKqueueFileMonitor object for that. If a directory
-   * will be created under that path, GKqueueFileMonitor will have to
-   * handle the directory notifications. */
-
-  sub = _kh_sub_new (path, TRUE, source);
-
-  /* FIXME: what to do about errors here? we can't return NULL or another
-   * kind of error and an assertion is probably too hard (same issue as in
-   * the inotify backend) */
-  g_assert (sub != NULL);
-  kqueue_monitor->sub = sub;
-
-  if (!_ke_is_excluded (path))
-    _kh_add_sub (sub);
-  else
-    {
-      GFile *file = g_file_new_for_path (path);
-      kqueue_monitor->fbfile = file;
-      kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
-      g_signal_connect (kqueue_monitor->fallback,
-                        "changed",
-                        G_CALLBACK (_fallback_callback),
-                        kqueue_monitor);
-    }
+kqueue_event_handler(kqueue_fnm_p kfnm,
+    kqueue_file_mon_data_p fmd, void *udata, uint32_t event,
+    const char *base, const char *filename, const char *new_filename) {
+	static const uint32_t kfnm_to_glib_map[] = {
+		0,				/* KF_EVENT_NOT_CHANGED */
+		G_FILE_MONITOR_EVENT_CREATED,	/* KF_EVENT_CREATED */
+		G_FILE_MONITOR_EVENT_DELETED,	/* KF_EVENT_DELETED */
+		G_FILE_MONITOR_EVENT_RENAMED,	/* KF_EVENT_RENAMED */
+		G_FILE_MONITOR_EVENT_CHANGED	/* KF_EVENT_CHANGED */
+	};
+
+	if (NULL == kfnm || NULL == filename ||
+	    KF_EVENT_CREATED > event ||
+	    KF_EVENT_CHANGED < event)
+		return;
+	g_file_monitor_source_handle_event(udata,
+	    kfnm_to_glib_map[event],
+	    filename, new_filename, NULL,
+	    g_source_get_time(kqueue_source));
+}
+
+static gboolean
+g_kqueue_file_monitor_callback(gint fd, GIOCondition condition, gpointer user_data) {
+
+	/* Only one thread process events. */
+	g_mutex_lock(&kqueue_lock);
+	kqueue_fnm_proccess_events(kqueue_fnm, kqueue_event_handler);
+	g_mutex_unlock(&kqueue_lock);
+
+	return (TRUE);
+}
+
+static gboolean
+g_kqueue_file_monitor_is_supported(void) {
+
+	if (NULL != kqueue_fnm)
+		return (TRUE);
+	/* Init only once. */
+	g_mutex_lock(&kqueue_lock);
+	if (NULL != kqueue_fnm) {
+		g_mutex_unlock(&kqueue_lock);
+		return (TRUE); /* Initialized while wait lock. */
+	}
+	kqueue_fnm = kqueue_fnm_create();
+	if (NULL == kqueue_fnm) {
+		g_mutex_unlock(&kqueue_lock);
+		return (FALSE); /* Init fail. */
+	}
+	kqueue_source = g_unix_fd_source_new(kqueue_fnm_get_ev_recv_fd(kqueue_fnm), G_IO_IN);
+	g_source_set_callback(kqueue_source, (GSourceFunc)g_kqueue_file_monitor_callback, NULL, NULL);
+	g_source_attach(kqueue_source, GLIB_PRIVATE_CALL(g_get_worker_context)());
+	g_mutex_unlock(&kqueue_lock);
+
+	return (TRUE);
 }
 
 static gboolean
-g_kqueue_file_monitor_is_supported (void)
-{
-  return _kh_startup ();
+g_kqueue_file_monitor_cancel(GFileMonitor *monitor) {
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR(monitor);
+
+	kqueue_fnm_del(kqueue_fnm, gffm->fmd);
+	gffm->fmd = NULL;
+
+	return (TRUE);
 }
 
 static void
-g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
-{
-  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
-  GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
+g_kqueue_file_monitor_finalize(GObject *object) {
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR(object);
 
-  gobject_class->finalize = g_kqueue_file_monitor_finalize;
-  file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
+	kqueue_fnm_del(kqueue_fnm, gffm->fmd);
+	gffm->fmd = NULL;
+}
 
-  local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
-  local_file_monitor_class->start = g_kqueue_file_monitor_start;
-  local_file_monitor_class->mount_notify = TRUE; /* TODO: ??? */
+static void
+g_kqueue_file_monitor_start(GLocalFileMonitor *local_monitor,
+    const gchar *dirname, const gchar *basename,
+    const gchar *filename, GFileMonitorSource *source) {
+	GKqueueFileMonitor *gffm = G_KQUEUE_FILE_MONITOR (local_monitor);
+
+	g_assert(NULL != kqueue_fnm);
+	g_source_ref((GSource*)source);
+
+	if (NULL == filename) {
+		filename = dirname;
+	}
+	gffm->fmd = kqueue_fnm_add(kqueue_fnm, filename, source);
 }
 
 static void
-g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
-{
+g_kqueue_file_monitor_init(GKqueueFileMonitor *monitor) {
+	
 }
 
-static gboolean
-g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
-{
-  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
-
-  if (kqueue_monitor->sub)
-    {
-      _kh_cancel_sub (kqueue_monitor->sub);
-      _kh_sub_free (kqueue_monitor->sub);
-      kqueue_monitor->sub = NULL;
-    }
-  else if (kqueue_monitor->fallback)
-    {
-      g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor);
-      g_file_monitor_cancel (kqueue_monitor->fallback);
-    }
+static void
+g_kqueue_file_monitor_class_init(GKqueueFileMonitorClass *class) {
+	GObjectClass *gobject_class = G_OBJECT_CLASS(class);
+	GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS(class);
+
+	class->is_supported = g_kqueue_file_monitor_is_supported;
+	class->start = g_kqueue_file_monitor_start;
+	class->mount_notify = TRUE; /* TODO: ??? */
+	file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
+	gobject_class->finalize = g_kqueue_file_monitor_finalize;
+}
+
+static void
+g_kqueue_file_monitor_class_finalize(GKqueueFileMonitorClass *class) {
+	
+}
+
+void
+g_io_module_load(GIOModule *module) {
+
+	g_type_module_use(G_TYPE_MODULE(module));
+
+	g_io_extension_point_implement(G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
+	    G_TYPE_KQUEUE_FILE_MONITOR, "kqueue", 10);
+	g_io_extension_point_implement(G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
+	    G_TYPE_KQUEUE_FILE_MONITOR, "kqueue", 10);
+}
+
+void
+g_io_module_unload(GIOModule *module) {
+	
+	g_assert_not_reached();
+}
 
-  if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
-    (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);
+char **
+g_io_module_query(void) {
+	char *eps[] = {
+		G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
+		G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
+		NULL
+	};
 
-  return TRUE;
+	return (g_strdupv(eps));
 }
diff -urN gio/kqueue/gkqueuefilemonitor.h gio/kqueue/gkqueuefilemonitor.h
--- gio/kqueue/gkqueuefilemonitor.h	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/gkqueuefilemonitor.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,51 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __G_KQUEUE_FILE_MONITOR_H__
-#define __G_KQUEUE_FILE_MONITOR_H__
-
-#include <glib-object.h>
-#include <string.h>
-#include <gio/gfilemonitor.h>
-#include <gio/glocalfilemonitor.h>
-#include <gio/giomodule.h>
-
-G_BEGIN_DECLS
-
-#define G_TYPE_KQUEUE_FILE_MONITOR        (g_kqueue_file_monitor_get_type ())
-#define G_KQUEUE_FILE_MONITOR(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_KQUEUE_FILE_MONITOR, GKqueueFileMonitor))
-#define G_KQUEUE_FILE_MONITOR_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST ((k), G_TYPE_KQUEUE_FILE_MONITOR, GKqueueFileMonitorClass))
-#define G_IS_KQUEUE_FILE_MONITOR(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_KQUEUE_FILE_MONITOR))
-#define G_IS_KQUEUE_FILE_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_KQUEUE_FILE_MONITOR))
-
-typedef struct _GKqueueFileMonitor      GKqueueFileMonitor;
-typedef struct _GKqueueFileMonitorClass GKqueueFileMonitorClass;
-
-struct _GKqueueFileMonitorClass {
-  GLocalFileMonitorClass parent_class;
-};
-
-GType g_kqueue_file_monitor_get_type (void);
-
-G_END_DECLS
-
-#endif /* __G_KQUEUE_FILE_MONITOR_H__ */
diff -urN gio/kqueue/kqueue_fnm.c gio/kqueue/kqueue_fnm.c
--- gio/kqueue/kqueue_fnm.c	1970-01-01 03:00:00.000000000 +0300
+++ gio/kqueue/kqueue_fnm.c	2017-03-09 01:13:25.483544000 +0300
@@ -0,0 +1,710 @@
+/*-
+ * Copyright (c) 2016 - 2017 Rozhuk Ivan <rozhuk.im@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * Author: Rozhuk Ivan <rozhuk.im@gmail.com>
+ *
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/stat.h>
+#include <sys/fcntl.h> /* open, fcntl */
+
+#include <inttypes.h>
+#include <stdlib.h> /* malloc, exit */
+#include <unistd.h> /* close, write, sysconf */
+#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */
+#include <errno.h>
+#include <dirent.h> // opendir, readdir
+
+#include "kqueue_fnm.h"
+
+
+/* Preallocate items count. */
+#ifndef FILES_ALLOC_BLK_SIZE
+#	define FILES_ALLOC_BLK_SIZE	8
+#endif
+
+typedef struct file_info_s { /* Directory file. */
+	struct dirent 	de;		/* d_reclen used for action. */
+	struct stat	sb;
+} file_info_t, *file_info_p;
+
+
+typedef struct readdir_data_s {
+	int		fd;
+	uint8_t		*buf;
+	size_t		buf_size;
+	size_t		buf_used;
+	size_t		buf_pos;
+	long		basep;		/* For getdirentries(). */
+} readdir_data_t, *readdir_data_p;
+
+
+typedef struct kqueue_file_mon_data_s {
+	int		fd;		/* fd for notify kqueue(). */
+	int		is_dir;
+	char		path[(PATH_MAX + 2)];
+	size_t		path_size;
+	size_t		name_offset;	/* Parent path size. */
+	void		*udata;
+	kqueue_fnm_p	kfnm;
+	/* For dir. */
+	file_info_p	files;
+	volatile size_t	files_count;
+	size_t		files_allocated;
+} kqueue_file_mon_data_t;
+
+
+typedef struct kqueue_file_nonify_monitor_s {
+	int		fd;		/* kqueue() fd. */
+	int		pfd[2];		/* pipe queue specific. */
+	uint8_t		*tmpbuf;
+	size_t		tmpbuf_size;
+} kqueue_fnm_t;
+
+
+typedef void (*kq_msg_cb)(kqueue_file_mon_data_p fmd);
+
+typedef struct kqueue_file_mon_msg_pkt_s {
+	size_t		magic;
+	kq_msg_cb	msg_cb;
+	kqueue_file_mon_data_p fmd;
+	size_t		chk_sum;
+} kqueue_fnm_msg_pkt_t, *kqueue_fnm_msg_pkt_p;
+
+#define KF_MSG_PKT_MAGIC	0xffddaa00
+#define KF_MSG_PKT_CHK_SUM_SET(__msg_pkt)				\
+    (__msg_pkt)->chk_sum = (((size_t)(__msg_pkt)->msg_cb) ^ ((size_t)(__msg_pkt)->fmd))
+#define KF_MSG_PKT_IS_VALID(__msg_pkt)					\
+    (KF_MSG_PKT_MAGIC == (__msg_pkt)->magic &&				\
+     (((size_t)(__msg_pkt)->msg_cb) ^ ((size_t)(__msg_pkt)->fmd)) == (__msg_pkt)->chk_sum)
+
+
+#ifndef O_NOATIME
+#	define O_NOATIME	0
+#endif
+#ifndef O_EVTONLY
+#	define O_EVTONLY	O_RDONLY
+#endif
+#define OPEN_FILE_FLAGS		(O_EVTONLY | O_NONBLOCK | O_NOFOLLOW | O_NOATIME | O_CLOEXEC)
+
+#define EVFILT_VNODE_FLAGS_ALL	(NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE)
+
+
+
+#define IS_NAME_DOTS(__name)	('.' == (__name)[0] &&			\
+				 ('\0' == (__name)[1] || 		\
+				  ('.' == (__name)[1] && '\0' == (__name)[2])))
+#define IS_DE_NAME_EQ(__de1, __de2)  (0 == mem_cmpn((__de1)->d_name,	\
+						    (__de1)->d_namlen,	\
+						    (__de2)->d_name,	\
+						    (__de2)->d_namlen))
+#define zalloc(__size)		calloc(1, (__size))
+
+
+static inline int
+mem_cmpn(const void *buf1, const size_t buf1_size,
+    const void *buf2, const size_t buf2_size) {
+
+	if (buf1_size != buf2_size)
+		return (((buf1_size > buf2_size) ? 127 : -127));
+	if (0 == buf1_size || buf1 == buf2)
+		return (0);
+	if (NULL == buf1)
+		return (-127);
+	if (NULL == buf2)
+		return (127);
+	return (memcmp(buf1, buf2, buf1_size));
+}
+
+/* For old BSD systems. */
+#if !defined(__FreeBSD_version) || __FreeBSD_version < 1100000
+static inline void *
+reallocarray(void *buf, const size_t nmemb, const size_t size) {
+	size_t nmemb_size;
+
+	nmemb_size = (nmemb * size);
+	if (0 == nmemb_size) {
+		if (0 != nmemb &&
+		    0 != size) { /* Overflow. */
+			errno = ENOMEM;
+			return (NULL);
+		}
+		nmemb_size ++;
+	} else if (((nmemb | size) & (SIZE_T_MAX << (sizeof(size_t) * 4))) &&
+	    (nmemb_size / size) != nmemb) { /* size_t overflow. */
+		errno = ENOMEM;
+		return (NULL);
+	}
+	return (realloc(buf, nmemb_size));
+}
+#endif
+
+static inline int
+realloc_items(void **items, const size_t item_size,
+    size_t *allocated, const size_t alloc_blk_cnt, const size_t count) {
+	size_t allocated_prev, allocated_new;
+	uint8_t *items_new;
+
+	if (NULL == items || NULL == allocated || 0 == alloc_blk_cnt)
+		return (EINVAL);
+	allocated_prev = (*allocated);
+	if (NULL != (*items) &&
+	    allocated_prev > count &&
+	    allocated_prev <= (count + alloc_blk_cnt))
+		return (0);
+	allocated_new = (((count / alloc_blk_cnt) + 1) * alloc_blk_cnt);
+	items_new = (uint8_t*)reallocarray((*items), item_size, allocated_new);
+	if (NULL == items_new) /* Realloc fail! */
+		return (ENOMEM);
+	if (allocated_new > allocated_prev) { /* Init new mem. */
+		bzero((items_new + (allocated_prev * item_size)),
+		    ((allocated_new - allocated_prev) * item_size));
+	}
+	(*items) = items_new;
+	(*allocated) = allocated_new;
+
+	return (0);
+}
+
+
+static int
+readdir_data_start(int fd, uint8_t *buf, size_t buf_size, readdir_data_p rddd) {
+
+	if (-1 == fd || NULL == buf || 0 == buf_size || NULL == rddd)
+		return (EINVAL);
+	if (-1 == lseek(fd, 0, SEEK_SET))
+		return (errno);
+	bzero(rddd, sizeof(readdir_data_t));
+	rddd->fd = fd;
+	rddd->buf = buf;
+	rddd->buf_size = buf_size;
+
+	return (0);
+}
+
+static int
+readdir_data_next(readdir_data_p rddd, struct dirent *de) {
+	int ios;
+	struct dirent *de_cur;
+
+	if (NULL == rddd || NULL == de)
+		return (EINVAL);
+
+retry:
+	if (rddd->buf_used <= rddd->buf_pos) {
+		ios = getdirentries(rddd->fd, (char*)rddd->buf,
+		    rddd->buf_size, &rddd->basep);
+		if (-1 == ios)
+			return (errno);
+		rddd->buf_used = (size_t)ios;
+		rddd->buf_pos = 0;
+	}
+	de_cur = ((struct dirent*)(rddd->buf + rddd->buf_pos));
+	if (rddd->buf_used <= rddd->buf_pos ||
+	    0 == de_cur->d_reclen)
+		return (ESPIPE); /* EOF. */
+	rddd->buf_pos += de_cur->d_reclen;
+	if (DT_WHT == de_cur->d_type ||
+	    IS_NAME_DOTS(de_cur->d_name))
+		goto retry;
+	memcpy(de, de_cur, de_cur->d_reclen);
+
+	return (0);
+}
+
+static void
+kqueue_file_mon_data_clean(kqueue_file_mon_data_p fmd) {
+
+	if (NULL == fmd)
+		return;
+	close(fmd->fd);
+	fmd->fd = -1;
+	fmd->files_count = 0;
+	realloc_items((void**)&fmd->files, sizeof(file_info_t),
+	    &fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count);
+}
+
+static void
+kqueue_file_mon_data_free(kqueue_file_mon_data_p fmd) {
+
+	if (NULL == fmd)
+		return;
+	close(fmd->fd);
+	free(fmd->files);
+	free(fmd);
+}
+
+static kqueue_file_mon_data_p
+kqueue_file_mon_data_alloc(kqueue_fnm_p kfnm, const char *path, void *udata) {
+	kqueue_file_mon_data_p fmd;
+
+	if (NULL == kfnm || NULL == path)
+		return (NULL);
+	fmd = zalloc(sizeof(kqueue_file_mon_data_t));
+	if (NULL == fmd)
+		return (NULL);
+	/* Remember args. */
+	fmd->path_size = strlcpy(fmd->path, path, sizeof(fmd->path));
+	fmd->name_offset = fmd->path_size;
+	fmd->udata = udata;
+	fmd->kfnm = kfnm;
+
+	return (fmd);
+}
+
+static int
+kqueue_file_mon_data_find_de_fileno(kqueue_file_mon_data_p fmd,
+    struct dirent *de, size_t *idx) {
+	size_t i;
+
+	if (NULL == fmd || NULL == de || NULL == idx)
+		return (0);
+	for (i = 0; i < fmd->files_count; i ++) {
+		if (de->d_fileno != fmd->files[i].de.d_fileno ||
+		    de->d_type != fmd->files[i].de.d_type)
+			continue;
+		(*idx) = i;
+		return (1);
+	}
+	(*idx) = fmd->files_count;
+	return (0);
+}
+
+
+static void
+kqueue_file_mon_data_init(kqueue_file_mon_data_p fmd) {
+	struct dirent *de;
+	struct stat sb;
+	struct kevent kev;
+	struct timespec ke_timeout;
+	uint8_t *tmpbuf;
+	readdir_data_t rddd;
+
+	if (NULL == fmd)
+		return;
+	fmd->fd = open(fmd->path, OPEN_FILE_FLAGS);
+	if (-1 == fmd->fd)
+		return;
+	if (0 != fstat(fmd->fd, &sb))
+		goto err_out;
+	/* Get parent folder name. */
+	if (S_ISDIR(sb.st_mode)) {
+		fmd->is_dir = 1;
+		/* Be sure that folder contain trailing '/'. */
+		if ('/' != fmd->path[(fmd->path_size - 1)]) {
+			fmd->path[fmd->path_size] = '/';
+			fmd->path[(fmd->path_size + 1)] = 0;
+			fmd->path_size ++;
+		}
+		/* Skip last '/' for parent dir search. */
+		fmd->name_offset = (fmd->path_size - 1);
+	}
+	/* Find parent dir path size. */
+	while (0 < fmd->name_offset && '/' != fmd->path[(fmd->name_offset - 1)]) {
+		fmd->name_offset --;
+	}
+
+	/* Dir special processing. */
+	if (0 != fmd->is_dir) {
+		/* Read and remember dir content. */
+		/* Get temp buf. */
+		/* Do this once per dir handle. */
+		if ((size_t)sb.st_blksize > fmd->kfnm->tmpbuf_size) {
+			tmpbuf = realloc(fmd->kfnm->tmpbuf, sb.st_blksize);
+			if (NULL == tmpbuf)
+				goto err_out;
+			fmd->kfnm->tmpbuf = tmpbuf;
+			fmd->kfnm->tmpbuf_size = (size_t)sb.st_blksize;
+		} else {
+			tmpbuf = fmd->kfnm->tmpbuf;
+		}
+		if (0 != readdir_data_start(fmd->fd, tmpbuf, sb.st_blksize, &rddd))
+			goto err_out;
+
+		for (;;) {
+			if (0 != realloc_items((void**)&fmd->files,
+			    sizeof(file_info_t), &fmd->files_allocated,
+			    FILES_ALLOC_BLK_SIZE, fmd->files_count))
+				goto err_out;
+			de = &fmd->files[fmd->files_count].de; /* Use short name. */
+			/* Get file name from folder. */
+			if (0 != readdir_data_next(&rddd, de))
+				break;
+			/* Get file attrs. */
+			if (0 != fstatat(fmd->fd, de->d_name,
+			    &fmd->files[fmd->files_count].sb,
+			    AT_SYMLINK_NOFOLLOW)) {
+				bzero(&fmd->files[fmd->files_count].sb,
+				    sizeof(struct stat));
+			}
+			fmd->files_count ++;
+		}
+	}
+	/* Add to kqueue. */
+	kev.ident = (uintptr_t)fmd->fd;
+	kev.filter = EVFILT_VNODE;
+	kev.flags = (EV_ADD | EV_CLEAR);
+	kev.fflags = EVFILT_VNODE_FLAGS_ALL;
+	kev.udata = (void*)fmd;
+	bzero(&ke_timeout, sizeof(ke_timeout));
+	if (-1 == kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, &ke_timeout))
+		goto err_out;
+	return; /* OK. */
+
+err_out:
+	kqueue_file_mon_data_clean(fmd);
+}
+
+
+static void
+kqueue_handle_changes(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd,
+    kfnm_event_handler_cb cb_func) {
+	file_info_t fi;
+	size_t i, j, k;
+	readdir_data_t rddd;
+
+	if (NULL == kfnm || NULL == fmd || NULL == cb_func)
+		return;
+	if (0 != fstat(fmd->fd, &fi.sb) ||
+	    0 == fi.sb.st_nlink) {
+		kqueue_file_mon_data_clean(fmd);
+		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
+		    fmd->path, "", NULL);
+		return;
+	}
+	if (0 == fmd->is_dir) {
+		fmd->path[fmd->name_offset] = 0;
+		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, fmd->path,
+		    (fmd->path + fmd->name_offset), NULL);
+		fmd->path[fmd->name_offset] = '/';
+		return;
+	}
+	/* Dir processing. */
+	if (0 != readdir_data_start(fmd->fd, fmd->kfnm->tmpbuf,
+	    fi.sb.st_blksize, &rddd))
+		return;
+	/* Mark all as removed. */
+	for (i = 0; i < fmd->files_count; i ++) {
+		fmd->files[i].de.d_reclen = KF_EVENT_DELETED;
+	}
+	/* Update dir. */
+	while (0 == readdir_data_next(&rddd, &fi.de)) {
+		/* Get file stat. */
+		if (0 != fstatat(fmd->fd, fi.de.d_name, &fi.sb, AT_SYMLINK_NOFOLLOW)) {
+			bzero(&fi.sb, sizeof(struct stat)); /* Fail, set to zero. */
+		}
+		/* Is new file/folder? */
+		if (0 == kqueue_file_mon_data_find_de_fileno(fmd, &fi.de, &i)) { /* Add new. */
+			if (0 != realloc_items((void**)&fmd->files, sizeof(file_info_t),
+			    &fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count))
+				goto err_out;
+			memcpy(&fmd->files[fmd->files_count], &fi, sizeof(file_info_t));
+			fmd->files[fmd->files_count].de.d_reclen = KF_EVENT_CREATED;
+			fmd->files_count ++;
+			/* Notify. */
+			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CREATED,
+			    fmd->path, fi.de.d_name, NULL);
+			continue;
+		}
+		/* Is renamed? */
+		if (0 == IS_DE_NAME_EQ(&fi.de, &fmd->files[i].de)) {
+			/* Notify. */
+			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED,
+			    fmd->path, fmd->files[i].de.d_name, fi.de.d_name);
+			/* Update. */
+			memcpy(&fmd->files[i], &fi, sizeof(file_info_t));
+			fmd->files[i].de.d_reclen = KF_EVENT_RENAMED;
+			continue;
+		}
+		/* Is modified? */
+		if (0 != memcmp(&fmd->files[i].sb, &fi.sb, sizeof(struct stat))) {
+			memcpy(&fmd->files[i].sb, &fi.sb, sizeof(struct stat)); /* Update stat. */
+			fmd->files[i].de.d_reclen = KF_EVENT_CHANGED;
+			/* Notify. */
+			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED,
+			    fmd->path, fi.de.d_name, NULL);
+			continue;
+		}
+		/* Not changed. */
+		fmd->files[i].de.d_reclen = KF_EVENT_NOT_CHANGED;
+	}
+	/* Remove marked as removed. */
+	for (i = 0; i < fmd->files_count; i ++) {
+		if (KF_EVENT_DELETED != fmd->files[i].de.d_reclen)
+			continue;
+		/* Look for next non removed item + notify. */
+		for (j = (i + 1); j < fmd->files_count; j ++) {
+			if (KF_EVENT_DELETED != fmd->files[j].de.d_reclen)
+				break;
+		}
+		/* Notify. */
+		for (k = i; k < j; k ++) {
+			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
+			    fmd->path, fmd->files[k].de.d_name, NULL);
+		}
+		memmove(&fmd->files[i], &fmd->files[j],
+		    (sizeof(file_info_t) * (fmd->files_count - j)));
+		fmd->files_count -= (j - i);
+	}
+
+err_out:
+	return;
+}
+
+static void
+kqueue_handle_rename(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd,
+    kfnm_event_handler_cb cb_func) {
+	int up_dir_fd, found = 0;
+	readdir_data_t rddd;
+	struct dirent de;
+	struct stat sb;
+	char old_filename[(MAXNAMLEN + 2)];
+	size_t old_filename_size;
+
+	if (NULL == kfnm || NULL == fmd || NULL == cb_func)
+		return;
+	if (0 != fstat(fmd->fd, &sb) ||
+	    0 == sb.st_nlink) {
+notify_removed:
+		kqueue_file_mon_data_clean(fmd);
+		cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
+		    fmd->path, "", NULL);
+		return;
+	}
+	/* Save old file name. */
+	old_filename_size = (fmd->path_size - fmd->name_offset - fmd->is_dir);
+	memcpy(old_filename,
+	    (fmd->path + fmd->name_offset),
+	    old_filename_size);
+	old_filename[old_filename_size] = 0;
+
+	/* Get parent folder name. */
+	fmd->path[fmd->name_offset] = 0;
+	/* Try to open. */
+	up_dir_fd = open(fmd->path, (OPEN_FILE_FLAGS | O_DIRECTORY));
+	/* Restore '/' after parent folder. */
+	fmd->path[fmd->name_offset] = '/';
+	if (-1 == up_dir_fd ||
+	    0 != fstat(up_dir_fd, &sb) ||
+	    0 != readdir_data_start(up_dir_fd, fmd->kfnm->tmpbuf, sb.st_blksize, &rddd)) {
+		close(up_dir_fd);
+		return;
+	}
+	/* Find new name by inode. */
+	while (0 == readdir_data_next(&rddd, &de)) {
+		if (de.d_fileno == sb.st_ino) {
+			found ++;
+			break;
+		}
+	}
+	close(up_dir_fd);
+	if (0 == found)
+		goto notify_removed; /* Not found. */
+	/* Update name. */
+	if ((sizeof(fmd->path) - 2) <= (fmd->name_offset + de.d_namlen))
+		return; /* Too long. */
+	memcpy((fmd->path + fmd->name_offset), de.d_name, de.d_namlen);
+	fmd->path_size = (fmd->name_offset + de.d_namlen);
+	/* Add last '/' for dir. */
+	fmd->path[fmd->path_size] = '/';
+	fmd->path_size += fmd->is_dir;
+	fmd->path[fmd->path_size] = 0;
+	/* Notify. */
+	cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED,
+	    fmd->path, old_filename, de.d_name);
+}
+
+
+static void
+kqueue_fnm_delay_call_process(kqueue_fnm_p kfnm, kq_msg_cb forced_msg_cb) {
+	ssize_t rd;
+	kqueue_fnm_msg_pkt_t msg;
+
+	for (;;) {
+		rd = read(kfnm->pfd[0], &msg, sizeof(msg));
+		if (0 >= rd)
+			return;
+		if (0 == KF_MSG_PKT_IS_VALID(&msg))
+			continue;
+		if (NULL != forced_msg_cb) {
+			forced_msg_cb(msg.fmd);
+			continue;
+		}
+		if (NULL == msg.msg_cb)
+			continue;
+		msg.msg_cb(msg.fmd);
+	}
+}
+
+static int
+kqueue_fnm_delay_call(kqueue_fnm_p kfnm, kq_msg_cb msg_cb,
+    kqueue_file_mon_data_p fmd) {
+	kqueue_fnm_msg_pkt_t msg;
+
+	if (NULL == kfnm || NULL == fmd)
+		return (EINVAL);
+	msg.magic = KF_MSG_PKT_MAGIC;
+	msg.msg_cb = msg_cb;
+	msg.fmd = fmd;
+	KF_MSG_PKT_CHK_SUM_SET(&msg);
+	if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg)))
+		return (0);
+	return (errno);
+}
+
+
+void
+kqueue_fnm_free(kqueue_fnm_p kfnm) {
+
+	if (NULL == kfnm)
+		return;
+	close(kfnm->fd);
+	kfnm->fd = -1;
+	/* Free all in delay calls queue. */
+	kqueue_fnm_delay_call_process(kfnm, kqueue_file_mon_data_free);
+	close(kfnm->pfd[0]);
+	close(kfnm->pfd[1]);
+	kfnm->pfd[0] = -1;
+	kfnm->pfd[1] = -1;
+	kfnm->tmpbuf_size = 0;
+	free(kfnm->tmpbuf);
+	free(kfnm);
+}
+
+kqueue_fnm_p
+kqueue_fnm_create(void) {
+	kqueue_fnm_p kfnm;
+	struct kevent kev;
+	struct timespec ke_timeout;
+
+	kfnm = zalloc(sizeof(kqueue_fnm_t));
+	if (NULL == kfnm)
+		return (NULL);
+	kfnm->fd = kqueue();
+	if (-1 == kfnm->fd)
+		goto err_out;
+	if (-1 == pipe2(kfnm->pfd, O_NONBLOCK))
+		goto err_out;
+	kev.ident = kfnm->pfd[0];
+	kev.filter = EVFILT_READ;
+	kev.flags = (EV_ADD | EV_ENABLE);
+	kev.fflags = 0;
+	kev.udata = NULL;
+	bzero(&ke_timeout, sizeof(ke_timeout));
+	if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, &ke_timeout))
+		goto err_out;
+	return (kfnm);
+
+err_out:
+	kqueue_fnm_free(kfnm);
+	return (NULL);
+}
+
+kqueue_file_mon_data_p
+kqueue_fnm_add(kqueue_fnm_p kfnm, const char *path, void *udata) {
+	int error;
+	kqueue_file_mon_data_p fmd;
+	
+	if (NULL == kfnm || NULL == path)
+		return (NULL);
+	fmd = (void*)kqueue_file_mon_data_alloc(kfnm, path, udata);
+	if (NULL == fmd)
+		return (NULL);
+	/* Shedule delay call to init. */
+	error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_init, fmd);
+	if (0 != error) { /* Error, init directly. */
+		//kqueue_file_mon_data_init(fmd);
+		kqueue_file_mon_data_free(fmd);
+		return (NULL);
+	}
+	return (fmd);
+}
+
+void
+kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) {
+	int error;
+
+	if (NULL == kfnm || NULL == fmd)
+		return;
+	/* Cancel notifications. */
+	close(fmd->fd);
+	fmd->fd = -1;
+	/* Shedule delay call to free. */
+	error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_free, fmd);
+	if (0 == error)
+		return;
+	/* Error, free directly. */
+	kqueue_file_mon_data_free(fmd);
+}
+
+
+int
+kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm) {
+
+	if (NULL == kfnm)
+		return (-1);
+	return (kfnm->fd);
+}
+
+void
+kqueue_fnm_proccess_events(kqueue_fnm_p kfnm, kfnm_event_handler_cb cb_func) {
+	struct kevent kev;
+	struct timespec ke_timeout;
+	kqueue_file_mon_data_p fmd;
+
+	if (NULL == kfnm || NULL == cb_func)
+		return;
+	/* Get and proccess events. */
+	bzero(&ke_timeout, sizeof(ke_timeout));
+	while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, &ke_timeout)) {
+		if (kev.ident == (uintptr_t)kfnm->pfd[0] &&
+		    kev.filter == EVFILT_READ) { /* Handle delay calls. */
+			kqueue_fnm_delay_call_process(kfnm, NULL);
+			continue;
+		}
+		if (EVFILT_VNODE != kev.filter ||
+		    0 == kev.udata)
+			continue; /* Unknown event or no associated data, skip. */
+		fmd = (kqueue_file_mon_data_p)kev.udata;
+
+		if (EV_ERROR & kev.flags) {
+			kev.flags |= NOTE_REVOKE; /* Treat error as unmount. */
+		}
+		if (NOTE_RENAME & kev.fflags) {
+			kqueue_handle_rename(kfnm, fmd, cb_func);
+		}
+		if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB) & kev.fflags) {
+			kqueue_handle_changes(kfnm, fmd, cb_func);
+		}
+		if ((NOTE_DELETE | NOTE_REVOKE) & kev.fflags) {
+			kqueue_file_mon_data_clean(fmd);
+			cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED,
+			    fmd->path, "", NULL);
+		}
+	}
+}
diff -urN gio/kqueue/kqueue_fnm.h gio/kqueue/kqueue_fnm.h
--- gio/kqueue/kqueue_fnm.h	1970-01-01 03:00:00.000000000 +0300
+++ gio/kqueue/kqueue_fnm.h	2017-03-09 01:13:25.484588000 +0300
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2016 Rozhuk Ivan <rozhuk.im@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * Author: Rozhuk Ivan <rozhuk.im@gmail.com>
+ *
+ */
+
+
+ 
+#ifndef __KQUEUE_FILE_NOTIFY_MONITOR_H__
+#define __KQUEUE_FILE_NOTIFY_MONITOR_H__
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <inttypes.h>
+
+
+typedef struct kqueue_file_nonify_monitor_s	*kqueue_fnm_p;
+typedef struct kqueue_file_mon_data_s		*kqueue_file_mon_data_p;
+
+typedef void (*kfnm_event_handler_cb)(kqueue_fnm_p kfnm,
+			       kqueue_file_mon_data_p fmd, void *udata,
+			       uint32_t event,
+			       const char *base,
+			       const char *filename,
+			       const char *new_filename);
+#define KF_EVENT_NOT_CHANGED	0 /* Internal use. */
+#define KF_EVENT_CREATED	1
+#define KF_EVENT_DELETED	2
+#define KF_EVENT_RENAMED	3
+#define KF_EVENT_CHANGED	4
+
+
+kqueue_fnm_p	kqueue_fnm_create(void);
+void		kqueue_fnm_free(kqueue_fnm_p kfnm);
+
+kqueue_file_mon_data_p	kqueue_fnm_add(kqueue_fnm_p kfnm,
+			    const char *path, void *udata);
+void		kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd);
+
+int		kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm);
+void		kqueue_fnm_proccess_events(kqueue_fnm_p kfnm,
+		    kfnm_event_handler_cb cb_func);
+
+
+#endif /* __KQUEUE_FILE_NOTIFY_MONITOR_H__ */
diff -urN gio/kqueue/kqueue-exclusions.c gio/kqueue/kqueue-exclusions.c
--- gio/kqueue/kqueue-exclusions.c	2015-10-14 14:39:37.000000000 +0300
+++ gio/kqueue/kqueue-exclusions.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,65 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-  Copyright (c) 2012 Antoine Jacoutot <ajacoutot@openbsd.org>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include <fcntl.h>
-#include <glib.h>
-#include <gio/gio.h>
-#include "kqueue-exclusions.h"
-
-static gboolean ke_debug_enabled = FALSE;
-#define KE_W if (ke_debug_enabled) g_warning
-
-/*
- * _ke_is_excluded:
- * @full_path - a path to file to check.
- *
- * Returns: TRUE if the file should be excluded from the kqueue-powered
- *      monitoring, FALSE otherwise.
- **/
-gboolean
-_ke_is_excluded (const char *full_path)
-{
-#if defined (O_EVTONLY)
-  return FALSE;
-#else
-  GFile *f = NULL;
-  GMount *mount = NULL;
-
-  f = g_file_new_for_path (full_path);
-
-  if (f != NULL) {
-    mount = g_file_find_enclosing_mount (f, NULL, NULL);
-    g_object_unref (f);
-  }
-
-  if ((mount != NULL && (g_mount_can_unmount (mount))) || g_str_has_prefix (full_path, "/mnt/"))
-  {
-    KE_W ("Excluding %s from kernel notification, falling back to poll", full_path);
-    if (mount)
-      g_object_unref (mount);
-    return TRUE;
-  }
-  else
-    return FALSE;
-#endif
-}
diff -urN gio/kqueue/kqueue-exclusions.h gio/kqueue/kqueue-exclusions.h
--- gio/kqueue/kqueue-exclusions.h	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/kqueue-exclusions.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,28 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __KQUEUE_EXCLUSIONS_H
-#define __KQUEUE_EXCLUSIONS_H
-
-gboolean _ke_is_excluded  (const char *full_path);
-
-#endif /* __KQUEUE_EXCLUDES_H */
diff -urN gio/kqueue/kqueue-helper.c gio/kqueue/kqueue-helper.c
--- gio/kqueue/kqueue-helper.c	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/kqueue-helper.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,558 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include "config.h"
-#include <sys/types.h>
-#include <sys/event.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <gio/glocalfile.h>
-#include <gio/glocalfilemonitor.h>
-#include <gio/gfile.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <pthread.h>
-#include "kqueue-helper.h"
-#include "kqueue-utils.h"
-#include "kqueue-thread.h"
-#include "kqueue-missing.h"
-#include "kqueue-exclusions.h"
-
-static gboolean kh_debug_enabled = FALSE;
-#define KH_W if (kh_debug_enabled) g_warning
-
-static GHashTable *subs_hash_table = NULL;
-G_LOCK_DEFINE_STATIC (hash_lock);
-
-static int kqueue_descriptor = -1;
-static int kqueue_socket_pair[] = {-1, -1};
-static pthread_t kqueue_thread;
-
-
-void _kh_file_appeared_cb (kqueue_sub *sub);
-
-/**
- * accessor function for kqueue_descriptor
- **/
-int
-get_kqueue_descriptor()
-{
-  return kqueue_descriptor;
-}
-
-/**
- * convert_kqueue_events_to_gio:
- * @flags: a set of kqueue filter flags
- * @done: a pointer to #gboolean indicating that the
- *      conversion has been done (out)
- *
- * Translates kqueue filter flags into GIO event flags.
- *
- * Returns: a #GFileMonitorEvent
- **/
-static GFileMonitorEvent
-convert_kqueue_events_to_gio (uint32_t flags, gboolean *done)
-{
-  g_assert (done != NULL);
-  *done = FALSE;
-
-  /* TODO: The following notifications should be emulated, if possible:
-   * - G_FILE_MONITOR_EVENT_PRE_UNMOUNT
-   */
-  if (flags & NOTE_DELETE)
-    {    
-      *done = TRUE;
-      return G_FILE_MONITOR_EVENT_DELETED;
-    }
-  if (flags & NOTE_ATTRIB)
-    {
-      *done = TRUE;
-      return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
-    }
-  if (flags & (NOTE_WRITE | NOTE_EXTEND))
-    {
-      *done = TRUE;
-      return G_FILE_MONITOR_EVENT_CHANGED;
-    }
-  if (flags & NOTE_RENAME)
-    {
-      *done = TRUE;
-      return G_FILE_MONITOR_EVENT_MOVED;
-    }
-  if (flags & NOTE_REVOKE)
-    {
-      *done = TRUE;
-      return G_FILE_MONITOR_EVENT_UNMOUNTED;
-    }
-
-  /* done is FALSE */
-  return 0;
-}
-
-typedef struct {
-  kqueue_sub *sub;
-  GFileMonitorSource *source;
-} handle_ctx;
-
-/**
- * handle_created: 
- * @udata: a pointer to user data (#handle_context).
- * @path: file name of a new file.
- * @inode: inode number of a new file.
- *
- * A callback function for the directory diff calculation routine,
- * produces G_FILE_MONITOR_EVENT_CREATED event for a created file.
- **/
-static void
-handle_created (void *udata, const char *path, ino_t inode)
-{
-  handle_ctx *ctx = NULL;
-
-  (void) inode;
-  ctx = (handle_ctx *) udata;
-  g_assert (udata != NULL);
-  g_assert (ctx->sub != NULL);
-  g_assert (ctx->source != NULL);
-
-  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED, path,
-                                      NULL, NULL, g_get_monotonic_time ());
-}
-
-/**
- * handle_deleted:
- * @udata: a pointer to user data (#handle_context).
- * @path: file name of the removed file.
- * @inode: inode number of the removed file.
- *
- * A callback function for the directory diff calculation routine,
- * produces G_FILE_MONITOR_EVENT_DELETED event for a deleted file.
- **/
-static void
-handle_deleted (void *udata, const char *path, ino_t inode)
-{
-  handle_ctx *ctx = NULL;
-
-  (void) inode;
-  ctx = (handle_ctx *) udata;
-  g_assert (udata != NULL);
-  g_assert (ctx->sub != NULL);
-  g_assert (ctx->source != NULL);
-
-  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED, path,
-                                      NULL, NULL, g_get_monotonic_time ());
-}
-
-/**
- * handle_moved:
- * @udata: a pointer to user data (#handle_context).
- * @from_path: file name of the source file.
- * @from_inode: inode number of the source file.
- * @to_path: file name of the replaced file.
- * @to_inode: inode number of the replaced file.
- *
- * A callback function for the directory diff calculation routine,
- * produces G_FILE_MONITOR_EVENT_RENAMED event on a move.
- **/
-static void
-handle_moved (void       *udata,
-              const char *from_path,
-              ino_t       from_inode,
-              const char *to_path,
-              ino_t       to_inode)
-{
-  handle_ctx *ctx = NULL;
-
-  (void) from_inode;
-  (void) to_inode;
-
-  ctx = (handle_ctx *) udata;
-  g_assert (udata != NULL);
-  g_assert (ctx->sub != NULL);
-  g_assert (ctx->source != NULL);
-
-  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_RENAMED,
-                                      from_path, to_path, NULL, g_get_monotonic_time ());
-}
-
-/**
- * handle_overwritten:
- * @data: a pointer to user data (#handle_context).
- * @path: file name of the overwritten file.
- * @node: inode number of the overwritten file.
- *
- * A callback function for the directory diff calculation routine,
- * produces G_FILE_MONITOR_EVENT_DELETED/CREATED event pair when
- * an overwrite occurs in the directory (see dep-list for details).
- **/
-static void
-handle_overwritten (void *udata, const char *path, ino_t inode)
-{
-  handle_ctx *ctx = NULL;
-
-  (void) inode;
-  ctx = (handle_ctx *) udata;
-  g_assert (udata != NULL);
-  g_assert (ctx->sub != NULL);
-  g_assert (ctx->source != NULL);
-
-  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED,
-                                      path, NULL, NULL, g_get_monotonic_time ());
-
-  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED,
-                                      path, NULL, NULL, g_get_monotonic_time ());
-}
-
-static const traverse_cbs cbs = {
-  handle_created,
-  handle_deleted,
-  handle_moved,
-  handle_overwritten,
-  handle_moved,
-  NULL, /* many added */
-  NULL, /* many removed */
-  NULL, /* names updated */
-};
-
-
-void
-_kh_dir_diff (kqueue_sub *sub, GFileMonitorSource *source)
-{
-  dep_list *was;
-  handle_ctx ctx;
-
-  g_assert (sub != NULL);
-  g_assert (source != NULL);
-
-  memset (&ctx, 0, sizeof (handle_ctx));
-  ctx.sub = sub;
-  ctx.source = source;
-
-  was = sub->deps;
-  sub->deps = dl_listing (sub->filename);
- 
-  dl_calculate (was, sub->deps, &cbs, &ctx);
-
-  dl_free (was);
-}
-
-
-/**
- * process_kqueue_notifications:
- * @gioc: unused.
- * @cond: unused.
- * @data: unused.
- *
- * Processes notifications, coming from the kqueue thread.
- *
- * Reads notifications from the command file descriptor, emits the
- * "changed" event on the appropriate monitor.
- *
- * A typical GIO Channel callback function.
- *
- * Returns: %TRUE
- **/
-static gboolean
-process_kqueue_notifications (GIOChannel   *gioc,
-                              GIOCondition  cond,
-                              gpointer      data)
-{
-  struct kqueue_notification n;
-  kqueue_sub *sub = NULL;
-  GFileMonitorSource *source = NULL;
-  GFileMonitorEvent mask = 0;
-  
-  g_assert (kqueue_socket_pair[0] != -1);
-  if (!_ku_read (kqueue_socket_pair[0], &n, sizeof (struct kqueue_notification)))
-    {
-      KH_W ("Failed to read a kqueue notification, error %d", errno);
-      return TRUE;
-    }
-
-  G_LOCK (hash_lock);
-  sub = (kqueue_sub *) g_hash_table_lookup (subs_hash_table, GINT_TO_POINTER (n.fd));
-  G_UNLOCK (hash_lock);
-
-  if (sub == NULL)
-    {
-      KH_W ("Got a notification for a deleted or non-existing subscription %d",
-             n.fd);
-      return TRUE;
-    }
-
-  source = sub->user_data;
-  g_assert (source != NULL);
-
-  if (n.flags & (NOTE_DELETE | NOTE_REVOKE))
-    {
-      if (sub->deps)
-        {
-          dl_free (sub->deps);
-          sub->deps = NULL;  
-        }  
-      _km_add_missing (sub);
-
-      if (!(n.flags & NOTE_REVOKE))
-        {
-          /* Note that NOTE_REVOKE is issued by the kqueue thread
-           * on EV_ERROR kevent. In this case, a file descriptor is
-           * already closed from the kqueue thread, no need to close
-           * it manually */ 
-          _kh_cancel_sub (sub);
-        }
-    }
-
-  if (sub->is_dir && n.flags & (NOTE_WRITE | NOTE_EXTEND))
-    {
-      _kh_dir_diff (sub, source);
-      n.flags &= ~(NOTE_WRITE | NOTE_EXTEND);
-    }
-
-  if (n.flags)
-    {
-      gboolean done = FALSE;
-      mask = convert_kqueue_events_to_gio (n.flags, &done);
-      if (done == TRUE)
-        g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, g_get_monotonic_time ());
-    }
-
-  return TRUE;
-}
-
-
-/*
- * _kh_startup_impl:
- * @unused: unused
- *
- * Kqueue backend startup code. Should be called only once.
- *
- * Returns: %TRUE on success, %FALSE otherwise.
- **/
-static gpointer
-_kh_startup_impl (gpointer unused)
-{
-  GIOChannel *channel = NULL;
-  gboolean result = FALSE;
-
-  kqueue_descriptor = kqueue ();
-  result = (kqueue_descriptor != -1);
-  if (!result)
-    {
-      KH_W ("Failed to initialize kqueue\n!");
-      return GINT_TO_POINTER (FALSE);
-    }
-
-  result = socketpair (AF_UNIX, SOCK_STREAM, 0, kqueue_socket_pair);
-  if (result != 0)
-    {
-      KH_W ("Failed to create socket pair\n!");
-      return GINT_TO_POINTER (FALSE) ;
-    }
-
-  result = pthread_create (&kqueue_thread,
-                           NULL,
-                           _kqueue_thread_func,
-                           &kqueue_socket_pair[1]);
-  if (result != 0)
-    {
-      KH_W ("Failed to run kqueue thread\n!");
-      return GINT_TO_POINTER (FALSE);
-    }
-
-  _km_init (_kh_file_appeared_cb);
-
-  channel = g_io_channel_unix_new (kqueue_socket_pair[0]);
-  g_io_add_watch (channel, G_IO_IN, process_kqueue_notifications, NULL);
-
-  subs_hash_table = g_hash_table_new (g_direct_hash, g_direct_equal);
-
-  KH_W ("started gio kqueue backend\n");
-  return GINT_TO_POINTER (TRUE);
-}
-
-
-/*
- * _kh_startup:
- * Kqueue backend initialization.
- *
- * Returns: %TRUE on success, %FALSE otherwise.
- **/
-gboolean
-_kh_startup (void)
-{
-  static GOnce init_once = G_ONCE_INIT;
-  g_once (&init_once, _kh_startup_impl, NULL);
-  return GPOINTER_TO_INT (init_once.retval);
-}
-
-
-/**
- * _kh_start_watching:
- * @sub: a #kqueue_sub
- *
- * Starts watching on a subscription.
- *
- * Returns: %TRUE on success, %FALSE otherwise.
- **/
-gboolean
-_kh_start_watching (kqueue_sub *sub)
-{
-  g_assert (kqueue_socket_pair[0] != -1);
-  g_assert (sub != NULL);
-  g_assert (sub->filename != NULL);
-
-  /* kqueue requires a file descriptor to monitor. Sad but true */
-#if defined (O_EVTONLY)
-  sub->fd = open (sub->filename, O_EVTONLY);
-#else
-  sub->fd = open (sub->filename, O_RDONLY);
-#endif
-
-  if (sub->fd == -1)
-    {
-      KH_W ("failed to open file %s (error %d)", sub->filename, errno);
-      return FALSE;
-    }
-
-  _ku_file_information (sub->fd, &sub->is_dir, NULL);
-  if (sub->is_dir)
-    {
-      /* I know, it is very bad to make such decisions in this way and here.
-       * We already do have an user_data at the #kqueue_sub, and it may point to
-       * GKqueueFileMonitor or GKqueueDirectoryMonitor. For a directory case,
-       * we need to scan in contents for the further diffs. Ideally this process
-       * should be delegated to the GKqueueDirectoryMonitor, but for now I will
-       * do it in a dirty way right here. */
-      if (sub->deps)
-        dl_free (sub->deps);
-
-      sub->deps = dl_listing (sub->filename);  
-    }
-
-  G_LOCK (hash_lock);
-  g_hash_table_insert (subs_hash_table, GINT_TO_POINTER (sub->fd), sub);
-  G_UNLOCK (hash_lock);
-
-  _kqueue_thread_push_fd (sub->fd);
-  
-  /* Bump the kqueue thread. It will pick up a new sub entry to monitor */
-  if (!_ku_write (kqueue_socket_pair[0], "A", 1))
-    KH_W ("Failed to bump the kqueue thread (add fd, error %d)", errno);
-  return TRUE;
-}
-
-
-/**
- * _kh_add_sub:
- * @sub: a #kqueue_sub
- *
- * Adds a subscription for monitoring.
- *
- * This funciton tries to start watching a subscription with
- * _kh_start_watching(). On failure, i.e. when a file does not exist yet,
- * the subscription will be added to a list of missing files to continue
- * watching when the file will appear.
- *
- * Returns: %TRUE
- **/
-gboolean
-_kh_add_sub (kqueue_sub *sub)
-{
-  g_assert (sub != NULL);
-
-  if (!_kh_start_watching (sub))
-    _km_add_missing (sub);
-
-  return TRUE;
-}
-
-
-/**
- * _kh_cancel_sub:
- * @sub a #kqueue_sub
- *
- * Stops monitoring on a subscription.
- *
- * Returns: %TRUE
- **/
-gboolean
-_kh_cancel_sub (kqueue_sub *sub)
-{
-  gboolean missing = FALSE;
-  g_assert (kqueue_socket_pair[0] != -1);
-  g_assert (sub != NULL);
-
-  G_LOCK (hash_lock);
-  missing = !g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
-  G_UNLOCK (hash_lock);
-
-  if (missing)
-    {
-      /* If there were no fd for this subscription, file is still
-       * missing. */
-      KH_W ("Removing subscription from missing");
-      _km_remove (sub);
-    }
-  else
-    {
-      /* fd will be closed in the kqueue thread */
-      _kqueue_thread_remove_fd (sub->fd);
-
-      /* Bump the kqueue thread. It will pick up a new sub entry to remove*/
-      if (!_ku_write (kqueue_socket_pair[0], "R", 1))
-        KH_W ("Failed to bump the kqueue thread (remove fd, error %d)", errno);
-    }
-
-  return TRUE;
-}
-
-
-/**
- * _kh_file_appeared_cb:
- * @sub: a #kqueue_sub
- *
- * A callback function for kqueue-missing subsystem.
- *
- * Signals that a missing file has finally appeared in the filesystem.
- * Emits %G_FILE_MONITOR_EVENT_CREATED.
- **/
-void
-_kh_file_appeared_cb (kqueue_sub *sub)
-{
-  GFile* child;
-
-  g_assert (sub != NULL);
-  g_assert (sub->filename);
-
-  if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
-    return;
-
-  child = g_file_new_for_path (sub->filename);
-
-  g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
-                             child,
-                             NULL,
-                             G_FILE_MONITOR_EVENT_CREATED);
-
-  g_object_unref (child);
-}
diff -urN gio/kqueue/kqueue-helper.h gio/kqueue/kqueue-helper.h
--- gio/kqueue/kqueue-helper.h	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/kqueue-helper.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,38 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __KQUEUE_HELPER_H
-#define __KQUEUE_HELPER_H
-
-#include "kqueue-sub.h"
-#include <gio/glocalfilemonitor.h>
-#include <gio/gfilemonitor.h>
-
-gboolean _kh_startup        (void);
-gboolean _kh_add_sub        (kqueue_sub *sub);
-gboolean _kh_cancel_sub     (kqueue_sub *sub);
-
-gboolean _kh_start_watching (kqueue_sub *sub);
-
-void     _kh_dir_diff       (kqueue_sub *sub, GFileMonitorSource *source);
-
-#endif /* __KQUEUE_HELPER_H */
diff -urN gio/kqueue/kqueue-missing.c gio/kqueue/kqueue-missing.c
--- gio/kqueue/kqueue-missing.c	2014-07-05 03:59:10.000000000 +0400
+++ gio/kqueue/kqueue-missing.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,158 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include <glib.h>
-
-#include "kqueue-helper.h"
-#include "kqueue-sub.h"
-#include "kqueue-missing.h"
-
-
-#define SCAN_MISSING_TIME 4 /* 1/4 Hz */
-
-static gboolean km_scan_missing (gpointer user_data);
-
-static gboolean km_debug_enabled = FALSE;
-#define KM_W if (km_debug_enabled) g_warning
-
-static GSList *missing_subs_list = NULL;
-G_LOCK_DEFINE_STATIC (missing_lock);
-
-static volatile gboolean scan_missing_running = FALSE;
-static on_create_cb file_appeared_callback;
-
-
-/**
- * _km_init:
- * @cb: a callback function. It will be called when a watched file
- *     will appear.
- *
- * Initialize the kqueue-missing module (optional).
- **/
-void
-_km_init (on_create_cb cb)
-{
-  file_appeared_callback = cb;
-}
-
-
-/**
- * _km_add_missing:
- * @sub: a #kqueue_sub
- *
- * Adds a subscription to the missing files list.
- **/
-void
-_km_add_missing (kqueue_sub *sub)
-{
-  G_LOCK (missing_lock);
-  if (g_slist_find (missing_subs_list, sub))
-    {
-      KM_W ("asked to add %s to missing list but it's already on the list!\n", sub->filename);
-      G_UNLOCK (missing_lock);
-      return;
-    }
-
-  KM_W ("adding %s to missing list\n", sub->filename);
-  missing_subs_list = g_slist_prepend (missing_subs_list, sub);
-  G_UNLOCK (missing_lock);
-
-  if (!scan_missing_running)
-    {
-      scan_missing_running = TRUE;
-      g_timeout_add_seconds (SCAN_MISSING_TIME, km_scan_missing, NULL);
-    }
-}
-
-
-/**
- * km_scan_missing:
- * @user_data: unused
- *
- * The core missing files watching routine.
- *
- * Traverses through a list of missing files, tries to start watching each with
- * kqueue, removes the appropriate entry and invokes a user callback if the file
- * has appeared.
- *
- * Returns: %FALSE if no missing files left, %TRUE otherwise.
- **/
-static gboolean
-km_scan_missing (gpointer user_data)
-{
-  GSList *head;
-  GSList *not_missing = NULL;
-  gboolean retval = FALSE;
-  
-  G_LOCK (missing_lock);
-
-  if (missing_subs_list)
-    KM_W ("we have a job");
-
-  for (head = missing_subs_list; head; head = head->next)
-    {
-      kqueue_sub *sub = (kqueue_sub *) head->data;
-      g_assert (sub != NULL);
-      g_assert (sub->filename != NULL);
-
-      if (_kh_start_watching (sub))
-        {
-          KM_W ("file %s now exists, starting watching", sub->filename);
-          if (file_appeared_callback)
-            file_appeared_callback (sub);
-          not_missing = g_slist_prepend (not_missing, head);
-        }
-    }
-
-  for (head = not_missing; head; head = head->next)
-    {
-      GSList *link = (GSList *) head->data;
-      missing_subs_list = g_slist_remove_link (missing_subs_list, link);
-    }
-  g_slist_free (not_missing);
-
-  if (missing_subs_list == NULL)
-    {
-      scan_missing_running = FALSE;
-      retval = FALSE;
-    }
-  else
-    retval = TRUE;
-
-  G_UNLOCK (missing_lock);
-  return retval;
-}
-
-
-/**
- * _km_remove:
- * @sub: a #kqueue_sub
- *
- * Removes a subscription from a list of missing files.
- **/
-void
-_km_remove (kqueue_sub *sub)
-{
-  G_LOCK (missing_lock);
-  missing_subs_list = g_slist_remove (missing_subs_list, sub);
-  G_UNLOCK (missing_lock);
-}
diff -urN gio/kqueue/kqueue-missing.h gio/kqueue/kqueue-missing.h
--- gio/kqueue/kqueue-missing.h	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/kqueue-missing.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,32 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __G_KQUEUE_MISSING_H
-#define __G_KQUEUE_MISSING_H
-
-typedef void (*on_create_cb) (kqueue_sub *);
-
-void _km_init        (on_create_cb cb);
-void _km_add_missing (kqueue_sub *sub);
-void _km_remove      (kqueue_sub *sub);
-
-#endif /* __G_KQUEUE_MISSING_H */
diff -urN gio/kqueue/kqueue-sub.c gio/kqueue/kqueue-sub.c
--- gio/kqueue/kqueue-sub.c	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/kqueue-sub.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,79 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include <glib.h>
-
-#include "kqueue-sub.h"
-
-static gboolean ks_debug_enabled = FALSE;
-#define KS_W if (ks_debug_enabled) g_warning
-
-/**
- * _kh_sub_new:
- * @filename: a file path to monitor (will be copied)
- * @pair_moves: pair moves flag. Refer to #GFileMonitorFlags documentation.
- * @user_data: user-supplied poiner.
- *
- * Creates a new subscription object.
- *
- * Returns: a pointer to a created subscription object.
- **/
-kqueue_sub*
-_kh_sub_new (const gchar    *filename,
-             gboolean        pair_moves,
-             gpointer        user_data)
-{
-  kqueue_sub *sub = g_slice_new (kqueue_sub);
-  g_assert (sub != NULL);
-  
-  sub->filename = g_strdup (filename);
-  sub->pair_moves = pair_moves;
-  sub->user_data = user_data;
-  sub->fd = -1;
-  sub->deps = NULL;
-  /* I think that having such flag in the subscription is not good */
-  sub->is_dir = 0;
-
-  KS_W ("new subscription for %s being setup\n", sub->filename);
-  
-  return sub;
-}
-
-
-/**
- * _kh_sub_free:
- * @sub: a #kqueue_sub
- *
- * Frees a subscription object and all its associated memory.
- **/
-void
-_kh_sub_free (kqueue_sub *sub)
-{
-  if (sub->deps)
-    {
-      dl_free (sub->deps);
-      sub->deps = NULL;
-    }
-
-  g_free (sub->filename);
-  g_slice_free (kqueue_sub, sub);
-}
diff -urN gio/kqueue/kqueue-sub.h gio/kqueue/kqueue-sub.h
--- gio/kqueue/kqueue-sub.h	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/kqueue-sub.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,50 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __KQUEUE_SUB_H
-#define __KQUEUE_SUB_H
-
-#include "dep-list.h"
-
-/**
- * kqueue_sub:
- * @filename: a name of the file to monitor
- * @user_data: the pointer to user data
- * @pair_moves: unused (currently not implemented)
- * @fd: the associated file descriptor (used by kqueue)
- *
- * Represents a subscription on a file or directory.
- */
-typedef struct
-{
-  gchar*    filename;
-  gpointer  user_data;
-  gboolean  pair_moves;
-  int       fd;
-  dep_list* deps;
-  int       is_dir;
-} kqueue_sub;
-
-kqueue_sub* _kh_sub_new  (const gchar* filename, gboolean pair_moves, gpointer user_data);
-void        _kh_sub_free (kqueue_sub* sub);
-
-#endif /* __KQUEUE_SUB_H */
diff -urN gio/kqueue/kqueue-thread.c gio/kqueue/kqueue-thread.c
--- gio/kqueue/kqueue-thread.c	2015-03-01 04:50:06.000000000 +0300
+++ gio/kqueue/kqueue-thread.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,304 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include "config.h"
-#include <sys/types.h>
-#include <sys/event.h>
-#include <sys/time.h>
-#include <unistd.h>
-#include <errno.h>
-#include <glib.h>
-
-#include "kqueue-thread.h"
-#include "kqueue-sub.h"
-#include "kqueue-utils.h"
-
-static gboolean kt_debug_enabled = FALSE;
-#define KT_W if (kt_debug_enabled) g_warning
-
-static GQueue pick_up_fds_queue = G_QUEUE_INIT;
-G_LOCK_DEFINE_STATIC (pick_up_lock);
-
-static GSList *remove_fds_list = NULL;
-G_LOCK_DEFINE_STATIC (remove_lock);
-
-/* GIO does not have analogues for NOTE_LINK and(?) NOTE_REVOKE, so
- * we do not ask kqueue() to watch for these events for now. */
-const uint32_t KQUEUE_VNODE_FLAGS =
-  NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME;
-
-extern int get_kqueue_descriptor(void);
-
-/**
- * _kqueue_thread_collect_fds:
- * @events: a #kevents - the list of events to monitor. Will be extended
- *     with new items.
- *
- * Picks up new file descriptors for monitoring from a global queue.
- *
- * To add new items to the list, use _kqueue_thread_push_fd().
- */
-static void
-_kqueue_thread_collect_fds (kevents *events)
-{
-  g_assert (events != NULL);
-  gint length = 0;
-
-  G_LOCK (pick_up_lock);
-  if ((length = g_queue_get_length (&pick_up_fds_queue)) != 0)
-    {
-      gpointer fdp = NULL;
-      kevents_extend_sz (events, length);
-
-      while ((fdp = g_queue_pop_head (&pick_up_fds_queue)) != NULL)
-        {
-          struct kevent *pevent = &events->memory[events->kq_size++];
-          EV_SET (pevent,
-                  GPOINTER_TO_INT (fdp),
-                  EVFILT_VNODE,
-                  EV_ADD | EV_ENABLE | EV_ONESHOT,
-                  KQUEUE_VNODE_FLAGS,
-                  0,
-                  0);
-        }
-    }
-  G_UNLOCK (pick_up_lock);
-}
-
-
-/**
- * _kqueue_thread_cleanup_fds:
- * @events: a #kevents -- list of events to monitor. Cancelled
- *     subscriptions will be removed from it, and its size
- *     probably will be reduced.
- *
- * Removes file descriptors from monitoring.
- *
- * This function will pick up file descriptors from a global list
- * to cancel monitoring on them. The list will be freed then.
- *
- * To add new items to the list, use _kqueue_thread_remove_fd().
- */
-static void
-_kqueue_thread_cleanup_fds (kevents *events)
-{
-  g_assert (events != NULL);
-
-  G_LOCK (remove_lock);
-  if (remove_fds_list)
-    {
-      size_t oldsize = events->kq_size;
-      int i, j;
-
-      for (i = 1, j = 1; i < oldsize; i++)
-        {
-          int fd = events->memory[i].ident;
-          GSList *elem = g_slist_find (remove_fds_list, GINT_TO_POINTER (fd));
-          if (elem == NULL)
-            {
-              if (i != j)
-                events->memory[j] = events->memory[i];
-              ++j;
-            }
-          else if (close (fd) == -1)
-            KT_W ("Failed to close fd %d, error %d", fd, errno);
-        }
-
-      KT_W ("FD Clean up complete, kq_size now %d\n", j);
-      events->kq_size = j;
-      kevents_reduce (events);
-      g_slist_free (remove_fds_list);
-      remove_fds_list = NULL;
-    }
-  G_UNLOCK (remove_lock);
-}
-
-
-/**
- * _kqueue_thread_drop_fd:
- * @events: a #kevents -- list of events to monitor. Cancelled
- *     subscriptions will be removed from it, and its size
- *     probably will be reduced.
- *
- * Removes a concrete file descriptor from monitoring.
- */
-static void
-_kqueue_thread_drop_fd (kevents *events, int fd)
-{
-  g_assert (events != NULL);
-
-  int i;
-  for (i = 1; i < events->kq_size; i++)
-    {
-      if (events->memory[i].ident == fd)
-        {
-          if (close (fd) == -1)
-            KT_W ("Failed to close fd %d, error %d", fd, errno);
-
-          events->memory[i] = events->memory[--events->kq_size];
-          return;
-        }
-    } 
-}
-
-/**
- * _kqueue_thread_func:
- * @arg: a pointer to int -- control file descriptor.
- *
- * The thread communicates with the outside world through a so-called
- * command file descriptor. The thread reads control commands from it
- * and writes the notifications into it.
- *
- * Control commands are single-byte characters:
- * - 'A' - pick up new file descriptors to monitor
- * - 'R' - remove some descriptors from monitoring.
- *
- * For details, see _kqueue_thread_collect_fds() and
- * _kqueue_thread_cleanup_fds().
- *
- * Notifications, that thread writes into the command file descriptor,
- * are represented with #kqueue_notification objects.
- *
- * Returns: %NULL
- */
-void*
-_kqueue_thread_func (void *arg)
-{
-  int fd, kqueue_descriptor;
-  kevents waiting;
-
-  g_assert (arg != NULL);
-  kevents_init_sz (&waiting, 1);
-
-  fd = *(int *) arg;
-
-  kqueue_descriptor = get_kqueue_descriptor();
-  if (kqueue_descriptor == -1)
-    {
-      KT_W ("fatal: kqueue is not initialized!\n");
-      return NULL;
-    }
-
-  EV_SET (&waiting.memory[0],
-          fd,
-          EVFILT_READ,
-          EV_ADD | EV_ENABLE | EV_ONESHOT,
-          NOTE_LOWAT,
-          1,
-          0);
-  waiting.kq_size = 1;
-
-  for (;;)
-    {
-      /* TODO: Provide more items in the 'eventlist' to kqueue(2).
-       * Currently the backend takes notifications from the kernel one
-       * by one, i.e. there will be a lot of system calls and context
-       * switches when the application will monitor a lot of files with
-       * high filesystem activity on each. */
-     
-      struct kevent received;
-      KT_W ("Watching for %zi items", waiting.kq_size);
-      int ret = kevent (kqueue_descriptor, waiting.memory, waiting.kq_size, &received, 1, NULL);
-      int kevent_errno = errno;
-      KT_W ("Awoken.");
-
-      if (ret == -1)
-        {
-          KT_W ("kevent failed: %d", kevent_errno);
-          if (kevent_errno == EINTR)
-            continue;
-          else
-            return NULL;
-        }
-
-      if (received.ident == fd)
-        {
-          char c;
-            if (!_ku_read (fd, &c, 1))
-              {
-                KT_W ("Failed to read command, error %d", errno);
-                continue;
-              }
-          if (c == 'A')
-            _kqueue_thread_collect_fds (&waiting);
-          else if (c == 'R')
-            _kqueue_thread_cleanup_fds (&waiting);
-        }
-      else 
-        {
-          struct kqueue_notification kn;
-          kn.fd = received.ident;
-
-          if (received.flags & EV_ERROR)
-            {
-              kn.flags = NOTE_REVOKE;
-              _kqueue_thread_drop_fd (&waiting, received.ident);
-            }
-          else
-            kn.flags = (received.fflags & ~NOTE_REVOKE);
-
-          if (!_ku_write (fd, &kn, sizeof (struct kqueue_notification)))
-            KT_W ("Failed to write a kqueue notification, error %d", errno);
-        }
-    }
-  kevents_free (&waiting);
-  return NULL;
-}
-
-
-/**
- * _kqueue_thread_push_fd:
- * @fd: a file descriptor
- *
- * Puts a new file descriptor into the pick up list for monitroing.
- *
- * The kqueue thread will not start monitoring on it immediately, it
- * should be bumped via its command file descriptor manually.
- * See kqueue_thread() and _kqueue_thread_collect_fds() for details.
- */
-void
-_kqueue_thread_push_fd (int fd)
-{
-  G_LOCK (pick_up_lock);
-  g_queue_push_tail (&pick_up_fds_queue, GINT_TO_POINTER (fd));
-  G_UNLOCK (pick_up_lock);
-}
-
-
-/**
- * _kqueue_thread_remove_fd:
- * @fd: a file descriptor
- *
- * Puts a new file descriptor into the remove list to cancel monitoring
- * on it.
- *
- * The kqueue thread will not stop monitoring on it immediately, it
- * should be bumped via its command file descriptor manually.
- * See kqueue_thread() and _kqueue_thread_collect_fds() for details.
- */
-void
-_kqueue_thread_remove_fd (int fd)
-{
-  G_LOCK (remove_lock);
-  remove_fds_list = g_slist_prepend (remove_fds_list, GINT_TO_POINTER (fd));
-  G_UNLOCK (remove_lock);
-}
diff -urN gio/kqueue/kqueue-thread.h gio/kqueue/kqueue-thread.h
--- gio/kqueue/kqueue-thread.h	2014-06-09 22:17:59.000000000 +0400
+++ gio/kqueue/kqueue-thread.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,45 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __KQUEUE_THREAD_H
-#define __KQUEUE_THREAD_H
-
-/**
- * kqueue_notification:
- * @fd: file descriptor, on which an activity has occured.
- * @flags: kqueue event flags, see man kevent(2).
- *
- * Represents an event occured on a file descriptor. Used for marshalling from
- * kqueue thread to its subscribers.
- */
-struct kqueue_notification {
-  /*< public >*/
-  int fd;
-  uint32_t flags;
-};
-
-
-void* _kqueue_thread_func      (void *arg);
-void  _kqueue_thread_push_fd   (int fd);
-void  _kqueue_thread_remove_fd (int fd);
-
-#endif /* __KQUEUE_SUB_H */
diff -urN gio/kqueue/kqueue-utils.c gio/kqueue/kqueue-utils.c
--- gio/kqueue/kqueue-utils.c	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/kqueue-utils.c	1970-01-01 03:00:00.000000000 +0300
@@ -1,210 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#include <sys/types.h>
-#include <sys/event.h>
-#include <string.h>
-#include <glib.h>
-#include <unistd.h>
-#include <sys/stat.h> 
-#include <errno.h>
-#include "kqueue-utils.h"
-
-static gboolean ku_debug_enabled = FALSE;
-#define KU_W if (ku_debug_enabled) g_warning
-
-
-
-#define KEVENTS_EXTEND_COUNT 10
-
-
-/**
- * kevents_init_sz:
- * @kv: a #kevents
- * @n_initial: the initial preallocated memory size. If it is less than
- *      %KEVENTS_EXTEND_COUNT, this value will be used instead.
- *
- * Initializes a #kevents object.
- **/
-void
-kevents_init_sz (kevents *kv, gsize n_initial)
-{
-  g_assert (kv != NULL);
-
-  memset (kv, 0, sizeof (kevents));
-
-  if (n_initial < KEVENTS_EXTEND_COUNT)
-    n_initial = KEVENTS_EXTEND_COUNT;
-
-  kv->memory = g_new0 (struct kevent, n_initial);
-  kv->kq_allocated = n_initial;
-}
-
-
-/**
- * kevents_extend_sz:
- * @kv: a #kevents
- * @n_new: the number of new objects to be added
- *
- * Extends the allocated memory, if needed.
- **/
-void
-kevents_extend_sz (kevents *kv, gsize n_new)
-{
-  g_assert (kv != NULL);
-
-  if (kv->kq_size + n_new <= kv->kq_allocated)
-    return;
-
-  kv->kq_allocated += (n_new + KEVENTS_EXTEND_COUNT);
-  kv->memory = g_renew (struct kevent, kv->memory, kv->kq_allocated);
-}
-
-
-/**
- * kevents_reduce:
- * @kv: a #kevents
- *
- * Reduces the allocated heap size, if needed.
- *
- * If the allocated heap size is >= 3*used
- * and 2*used >= %KEVENTS_EXTEND_COUNT, reduce it to 2*used.
- **/
-void
-kevents_reduce (kevents *kv)
-{
-  g_assert (kv != NULL);
-  gsize candidate_sz;
-
-  if (kv->kq_size == 0 || kv->kq_allocated == 0 || kv->memory == NULL)
-    return;
-
-  candidate_sz = 2 * kv->kq_size;
-
-  if (((double) kv->kq_allocated / kv->kq_size) >= 3 &&
-      candidate_sz >= KEVENTS_EXTEND_COUNT)
-    {
-      kv->kq_allocated = candidate_sz;
-      kv->memory = g_renew (struct kevent, kv->memory, kv->kq_allocated);
-    }
-}
-
-
-/**
- * kevents_free:
- * @kv: a #kevents
- *
- * Resets the kevents object and frees all the associated memory.
- **/
-void
-kevents_free (kevents *kv)
-{
-  g_assert (kv != NULL);
-
-  g_free (kv->memory);
-  memset (kv, 0, sizeof (kevents));
-}
-
-
-#define SAFE_GENERIC_OP(fcn, fd, data, size) \
-  while (size > 0) \
-    { \
-      gsize retval = fcn (fd, data, size); \
-      if (retval == -1) \
-        { \
-          if (errno == EINTR) \
-            continue; \
-          else \
-            return FALSE; \
-        } \
-      size -= retval; \
-      data += retval; \
-    } \
-  return TRUE;
-
-
-/**
- * _ku_read:
- * @fd: a file descriptor
- * @data: the destination buffer
- * @size: how many bytes to read
- *
- * A ready-to-EINTR version of read().
- *
- * This function expects to work with a blocking socket.
- *
- * Returns: %TRUE on success, %FALSE otherwise
- **/
-gboolean
-_ku_read (int fd, gpointer data, gsize size)
-{
-  SAFE_GENERIC_OP (read, fd, data, size);
-}
-
-
-/**
- * _ku_write:
- * @fd: a file descriptor
- * @data: the buffer to write
- * @size: how many bytes to write
- *
- * A ready-to-EINTR version of write().
- *
- * This function expects to work with a blocking socket.
- *
- * Returns: %TRUE on success, %FALSE otherwise
- **/
-gboolean
-_ku_write (int fd, gconstpointer data, gsize size)
-{
-  SAFE_GENERIC_OP (write, fd, data, size);
-}
-
-
-/**
- * Get some file information by its file descriptor.
- *
- * @param[in]  fd      A file descriptor.
- * @param[out] is_dir  A flag indicating directory.
- * @param[out] inode   A file's inode number.
- **/
-void
-_ku_file_information (int fd, int *is_dir, ino_t *inode)
-{
-  g_assert (fd != -1);
-
-  struct stat st;
-  memset (&st, 0, sizeof (struct stat));
-
-  if (fstat (fd, &st) == -1)
-    {
-      KU_W ("fstat failed, assuming it is just a file");
-      is_dir = NULL;
-      return;
-    }
-
-  if (is_dir != NULL)
-      *is_dir = ((st.st_mode & S_IFDIR) == S_IFDIR) ? 1 : 0;
-
-  if (inode != NULL)
-      *inode = st.st_ino;
-}
diff -urN gio/kqueue/kqueue-utils.h gio/kqueue/kqueue-utils.h
--- gio/kqueue/kqueue-utils.h	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/kqueue-utils.h	1970-01-01 03:00:00.000000000 +0300
@@ -1,53 +0,0 @@
-/*******************************************************************************
-  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*******************************************************************************/
-
-#ifndef __KQUEUE_UTILS_H
-#define __KQUEUE_UTILS_H
-
-#include <sys/types.h> /* ino_t */
-
-/**
- * kqueue_notification:
- * @memory: a pointer to the allocated memory
- * @kq_size: the number of used items
- * @kq_allocated: the number of allocated items
- *
- * Represents a pool of (struct kevent) objects.
- */
-typedef struct {
-  struct kevent *memory;
-  gsize kq_size;
-  gsize kq_allocated;
-} kevents;
-
-void kevents_init_sz   (kevents *kv, gsize n_initial);
-void kevents_extend_sz (kevents *kv, gsize n_new);
-void kevents_reduce    (kevents *kv);
-void kevents_free      (kevents *kv);
-
-
-gboolean _ku_read             (int fd, gpointer data, gsize size);
-gboolean _ku_write            (int fd, gconstpointer data, gsize size);
-
-void     _ku_file_information (int fd, int *is_dir, ino_t *inode);
-
-#endif /* __KQUEUE_UTILS_H */
diff -urN gio/kqueue/Makefile.am gio/kqueue/Makefile.am
--- gio/kqueue/Makefile.am	2015-10-14 14:41:16.000000000 +0300
+++ gio/kqueue/Makefile.am	2017-03-09 01:13:25.063425000 +0300
@@ -4,21 +4,8 @@
 
 libkqueue_la_SOURCES = \
        gkqueuefilemonitor.c \
-       gkqueuefilemonitor.h \
-       kqueue-helper.c \
-       kqueue-helper.h \
-       kqueue-thread.c \
-       kqueue-thread.h \
-       kqueue-sub.c \
-       kqueue-sub.h \
-       kqueue-missing.c \
-       kqueue-missing.h \
-       kqueue-utils.c \
-       kqueue-utils.h \
-       kqueue-exclusions.c \
-       kqueue-exclusions.h \
-       dep-list.c \
-       dep-list.h \
+       kqueue_fnm.c \
+       kqueue_fnm.h \
        $(NULL)
 
 libkqueue_la_CFLAGS = \
