glib/gio/kqueue/kqueue-missing.c

180 lines
5.0 KiB
C
Raw Normal View History

/*******************************************************************************
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>
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
#include "glib-private.h"
#include "kqueue-helper.h"
#define SCAN_MISSING_TIME 4 /* 1/4 Hz */
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 gboolean scan_missing_running = FALSE; /* must be accessed under @missing_lock */
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
static gboolean
_km_scan_missing_cb (gpointer user_data)
{
return _km_scan_missing (NULL);
}
/**
* _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);
if (!scan_missing_running)
{
GSource *source;
scan_missing_running = TRUE;
source = g_timeout_source_new_seconds (SCAN_MISSING_TIME);
g_source_set_callback (source, _km_scan_missing_cb, NULL, NULL);
g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
g_source_unref (source);
}
G_UNLOCK (missing_lock);
}
/**
* _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.
**/
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
static void
_kh_file_appeared_cb (kqueue_sub *sub)
{
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
gint64 now = g_get_monotonic_time ();
g_assert (sub != NULL);
g_assert (sub->filename);
if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
return;
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CREATED,
sub->basename, NULL, NULL, now);
g_file_monitor_source_handle_event (sub->source, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT,
sub->basename, NULL, NULL, now);
}
/**
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
* _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.
**/
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
gboolean
_km_scan_missing (kqueue_sub *check_this_sub_only)
{
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);
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
if (check_this_sub_only != NULL && sub != check_this_sub_only)
continue;
if (_kqsub_start_watching (sub))
{
KM_W ("file %s now exists, starting watching", sub->filename);
kqueue: Make it possible to pass file monitor tests Previously, kqueue file monitor only add event sources for directories regardless of the type of the file being monitored. Doing so may be possible on inotify, but it is not sufficient on kqueue. Watching a directory on kqueue doesn't report changes made to files under it, and we must watch files themselves to get notified. This problem is fixed by adding a second watch for non-directory file monitors, and the result is that we are now able to receive 'CHANGED' and 'ATTRIBUTE_CHANGED' events for non-directory files. Since having two watches on one file monitor requires many code changes to work properly, this commit also changes the following things: - NOTE_ALL macro is now replaced by note_all inline function. Since the kqueue backend is shared by all BSD operating systems, there are a few difference between these systems. It is easier to do '#ifdef' check in a function than in a macro. - Both g_kqueue_file_monitor_callback and g_kqueue_file_monitor_cancel now holds a lock before accessing kqueue_sub structs. This fixes a crash when these two functions are called from different threads, causing g_kqueue_file_monitor_callback to access freed memory. - 'mask' variable in g_kqueue_file_monitor_callback is now removed. The usage of 'mask' was wrong because of the 'mask > 0' check. 'CHANGED' event has value 0 so the 'mask > 0' check made it impossible to emit 'CHANGED' events. - kqueue-missing scans can now be triggered from the kqueue event callback instead of always waiting for 4 seconds. - Don't remove a file from kqueue on unlink unless its hard link count has dropped to zero. - Don't use 'else if' in the check of 'fflags'. It is possible for a kevent to have multiple flags set. - Don't use g_file_monitor_emit_event directly. Always use g_file_monitor_source_handle_event to report events. Events submitted to g_file_monitor_emit_event are delivered immediately, but events sent to g_file_monitor_source_handle_event are scheduled by GLocalFileMonitor. If we mix the two, the order of events will be wrong and tests will fail. - Report 'CHANGES_DONE_HINT' immediately after 'CREATED' if the file created is not a regular file. This is copied from ih_event_callback.
2018-06-03 13:35:48 +02:00
if (check_this_sub_only == NULL)
_kh_file_appeared_cb (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);
}