1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-09-01 21:59:38 +02:00
Files
build
debian
docs
gio
fam
fen
gdbus-codegen
gvdb
inotify
Makefile.am
ginotifydirectorymonitor.c
ginotifydirectorymonitor.h
ginotifyfilemonitor.c
ginotifyfilemonitor.h
inotify-diag.c
inotify-diag.h
inotify-helper.c
inotify-helper.h
inotify-kernel.c
inotify-kernel.h
inotify-missing.c
inotify-missing.h
inotify-path.c
inotify-path.h
inotify-sub.c
inotify-sub.h
libasyncns
tests
win32
xdgmime
.gitignore
ChangeLog
Makefile.am
abicheck.sh
gaction.c
gaction.h
gactiongroup.c
gactiongroup.h
gappinfo.c
gappinfo.h
gapplication.c
gapplication.h
gapplicationcommandline.c
gapplicationcommandline.h
gapplicationimpl-dbus.c
gapplicationimpl.h
gasynchelper.c
gasynchelper.h
gasyncinitable.c
gasyncinitable.h
gasyncresult.c
gasyncresult.h
gbufferedinputstream.c
gbufferedinputstream.h
gbufferedoutputstream.c
gbufferedoutputstream.h
gcancellable.c
gcancellable.h
gcharsetconverter.c
gcharsetconverter.h
gcontenttype.c
gcontenttype.h
gcontenttypeprivate.h
gconverter.c
gconverter.h
gconverterinputstream.c
gconverterinputstream.h
gconverteroutputstream.c
gconverteroutputstream.h
gcredentials.c
gcredentials.h
gdatainputstream.c
gdatainputstream.h
gdataoutputstream.c
gdataoutputstream.h
gdbus-bash-completion.sh
gdbus-example-objectmanager.xml
gdbus-tool.c
gdbusaddress.c
gdbusaddress.h
gdbusauth.c
gdbusauth.h
gdbusauthmechanism.c
gdbusauthmechanism.h
gdbusauthmechanismanon.c
gdbusauthmechanismanon.h
gdbusauthmechanismexternal.c
gdbusauthmechanismexternal.h
gdbusauthmechanismsha1.c
gdbusauthmechanismsha1.h
gdbusauthobserver.c
gdbusauthobserver.h
gdbusconnection.c
gdbusconnection.h
gdbuserror.c
gdbuserror.h
gdbusinterface.c
gdbusinterface.h
gdbusinterfaceskeleton.c
gdbusinterfaceskeleton.h
gdbusintrospection.c
gdbusintrospection.h
gdbusmessage.c
gdbusmessage.h
gdbusmethodinvocation.c
gdbusmethodinvocation.h
gdbusnameowning.c
gdbusnameowning.h
gdbusnamewatching.c
gdbusnamewatching.h
gdbusobject.c
gdbusobject.h
gdbusobjectmanager.c
gdbusobjectmanager.h
gdbusobjectmanagerclient.c
gdbusobjectmanagerclient.h
gdbusobjectmanagerserver.c
gdbusobjectmanagerserver.h
gdbusobjectproxy.c
gdbusobjectproxy.h
gdbusobjectskeleton.c
gdbusobjectskeleton.h
gdbusprivate.c
gdbusprivate.h
gdbusproxy.c
gdbusproxy.h
gdbusserver.c
gdbusserver.h
gdbusutils.c
gdbusutils.h
gdelayedsettingsbackend.c
gdelayedsettingsbackend.h
gdesktopappinfo.c
gdesktopappinfo.h
gdrive.c
gdrive.h
gdummyfile.c
gdummyfile.h
gdummyproxyresolver.c
gdummyproxyresolver.h
gdummytlsbackend.c
gdummytlsbackend.h
gemblem.c
gemblem.h
gemblemedicon.c
gemblemedicon.h
gfile.c
gfile.h
gfileattribute-priv.h
gfileattribute.c
gfileattribute.h
gfiledescriptorbased.c
gfiledescriptorbased.h
gfileenumerator.c
gfileenumerator.h
gfileicon.c
gfileicon.h
gfileinfo-priv.h
gfileinfo.c
gfileinfo.h
gfileinputstream.c
gfileinputstream.h
gfileiostream.c
gfileiostream.h
gfilemonitor.c
gfilemonitor.h
gfilenamecompleter.c
gfilenamecompleter.h
gfileoutputstream.c
gfileoutputstream.h
gfilterinputstream.c
gfilterinputstream.h
gfilteroutputstream.c
gfilteroutputstream.h
gicon.c
gicon.h
ginetaddress.c
ginetaddress.h
ginetsocketaddress.c
ginetsocketaddress.h
ginitable.c
ginitable.h
ginputstream.c
ginputstream.h
gio-marshal.list
gio-querymodules.c
gio.h
gio.rc.in
gio.symbols
gioenums.h
gioenumtypes.c.template
gioenumtypes.h.template
gioerror.c
gioerror.h
giomodule-priv.h
giomodule.c
giomodule.h
gioscheduler.c
gioscheduler.h
giostream.c
giostream.h
giotypes.h
gkeyfilesettingsbackend.c
glib-compile-schemas.c
gloadableicon.c
gloadableicon.h
glocaldirectorymonitor.c
glocaldirectorymonitor.h
glocalfile.c
glocalfile.h
glocalfileenumerator.c
glocalfileenumerator.h
glocalfileinfo.c
glocalfileinfo.h
glocalfileinputstream.c
glocalfileinputstream.h
glocalfileiostream.c
glocalfileiostream.h
glocalfilemonitor.c
glocalfilemonitor.h
glocalfileoutputstream.c
glocalfileoutputstream.h
glocalvfs.c
glocalvfs.h
gmemoryinputstream.c
gmemoryinputstream.h
gmemoryoutputstream.c
gmemoryoutputstream.h
gmemorysettingsbackend.c
gmount.c
gmount.h
gmountoperation.c
gmountoperation.h
gmountprivate.h
gnativevolumemonitor.c
gnativevolumemonitor.h
gnetworkaddress.c
gnetworkaddress.h
gnetworkingprivate.h
gnetworkservice.c
gnetworkservice.h
gnullsettingsbackend.c
goutputstream.c
goutputstream.h
gpermission.c
gpermission.h
gpollableinputstream.c
gpollableinputstream.h
gpollableoutputstream.c
gpollableoutputstream.h
gpollfilemonitor.c
gpollfilemonitor.h
gproxy.c
gproxy.h
gproxyaddress.c
gproxyaddress.h
gproxyaddressenumerator.c
gproxyaddressenumerator.h
gproxyresolver.c
gproxyresolver.h
gregistrysettingsbackend.c
gregistrysettingsbackend.h
gresolver.c
gresolver.h
gschema.dtd
gseekable.c
gseekable.h
gsettings-bash-completion.sh
gsettings-mapping.c
gsettings-mapping.h
gsettings-tool.c
gsettings.c
gsettings.h
gsettingsbackend.c
gsettingsbackend.h
gsettingsbackendinternal.h
gsettingsschema.c
gsettingsschema.h
gsimpleaction.c
gsimpleaction.h
gsimpleactiongroup.c
gsimpleactiongroup.h
gsimpleasyncresult.c
gsimpleasyncresult.h
gsimplepermission.c
gsimplepermission.h
gsocket.c
gsocket.h
gsocketaddress.c
gsocketaddress.h
gsocketaddressenumerator.c
gsocketaddressenumerator.h
gsocketclient.c
gsocketclient.h
gsocketconnectable.c
gsocketconnectable.h
gsocketconnection.c
gsocketconnection.h
gsocketcontrolmessage.c
gsocketcontrolmessage.h
gsocketinputstream.c
gsocketinputstream.h
gsocketlistener.c
gsocketlistener.h
gsocketoutputstream.c
gsocketoutputstream.h
gsocketservice.c
gsocketservice.h
gsocks4aproxy.c
gsocks4aproxy.h
gsocks4proxy.c
gsocks4proxy.h
gsocks5proxy.c
gsocks5proxy.h
gsrvtarget.c
gsrvtarget.h
gtcpconnection.c
gtcpconnection.h
gtcpwrapperconnection.c
gtcpwrapperconnection.h
gthemedicon.c
gthemedicon.h
gthreadedresolver.c
gthreadedresolver.h
gthreadedsocketservice.c
gthreadedsocketservice.h
gtimezonemonitor.c
gtimezonemonitor.h
gtlsbackend.c
gtlsbackend.h
gtlscertificate.c
gtlscertificate.h
gtlsclientconnection.c
gtlsclientconnection.h
gtlsconnection.c
gtlsconnection.h
gtlsserverconnection.c
gtlsserverconnection.h
gunionvolumemonitor.c
gunionvolumemonitor.h
gunixconnection.c
gunixconnection.h
gunixcredentialsmessage.c
gunixcredentialsmessage.h
gunixfdlist.c
gunixfdlist.h
gunixfdmessage.c
gunixfdmessage.h
gunixinputstream.c
gunixinputstream.h
gunixmount.c
gunixmount.h
gunixmounts.c
gunixmounts.h
gunixoutputstream.c
gunixoutputstream.h
gunixresolver.c
gunixresolver.h
gunixsocketaddress.c
gunixsocketaddress.h
gunixvolume.c
gunixvolume.h
gunixvolumemonitor.c
gunixvolumemonitor.h
gvfs.c
gvfs.h
gvolume.c
gvolume.h
gvolumemonitor.c
gvolumemonitor.h
gwin32appinfo.c
gwin32appinfo.h
gwin32inputstream.c
gwin32inputstream.h
gwin32mount.c
gwin32mount.h
gwin32outputstream.c
gwin32outputstream.h
gwin32resolver.c
gwin32resolver.h
gwin32volumemonitor.c
gwin32volumemonitor.h
gzlibcompressor.c
gzlibcompressor.h
gzlibdecompressor.c
gzlibdecompressor.h
makefile.msc
strinfo.c
glib
gmodule
gobject
gthread
m4macros
po
tests
.gitignore
AUTHORS
COPYING
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-18
ChangeLog.pre-2-2
ChangeLog.pre-2-20
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL.in
MAINTAINERS
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README.commits
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.ac
gio-2.0-uninstalled.pc.in
gio-2.0.pc.in
gio-unix-2.0-uninstalled.pc.in
gio-unix-2.0.pc.in
gio-windows-2.0.pc.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
glib/gio/inotify/inotify-path.c

441 lines
10 KiB
C
Raw Normal View History

/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/* inotify-path.c - GVFS Directory Monitor based on inotify.
Copyright (C) 2006 John McCutchan
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors:
John McCutchan <john@johnmccutchan.com>
*/
#include "config.h"
/* Don't put conflicting kernel types in the global namespace: */
#define __KERNEL_STRICT_NAMES
#include <sys/inotify.h>
#include <string.h>
#include <glib.h>
#include "inotify-kernel.h"
#include "inotify-path.h"
#include "inotify-missing.h"
#define IP_INOTIFY_MASK (IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_DELETE|IN_CREATE|IN_DELETE_SELF|IN_UNMOUNT|IN_MOVE_SELF|IN_CLOSE_WRITE)
/* Older libcs don't have this */
#ifndef IN_ONLYDIR
#define IN_ONLYDIR 0
#endif
typedef struct ip_watched_dir_s {
char *path;
/* TODO: We need to maintain a tree of watched directories
* so that we can deliver move/delete events to sub folders.
* Or the application could do it...
*/
struct ip_watched_dir_s* parent;
GList* children;
/* Inotify state */
gint32 wd;
/* List of inotify subscriptions */
GList *subs;
} ip_watched_dir_t;
static gboolean ip_debug_enabled = FALSE;
#define IP_W if (ip_debug_enabled) g_warning
/* path -> ip_watched_dir */
static GHashTable * path_dir_hash = NULL;
/* inotify_sub * -> ip_watched_dir *
*
* Each subscription is attached to a watched directory or it is on
* the missing list
*/
static GHashTable * sub_dir_hash = NULL;
/* This hash holds GLists of ip_watched_dir_t *'s
* We need to hold a list because symbolic links can share
* the same wd
*/
static GHashTable * wd_dir_hash = NULL;
static ip_watched_dir_t *ip_watched_dir_new (const char *path,
int wd);
static void ip_watched_dir_free (ip_watched_dir_t *dir);
static void ip_event_callback (ik_event_t *event);
static void (*event_callback)(ik_event_t *event, inotify_sub *sub);
gboolean
_ip_startup (void (*cb)(ik_event_t *event, inotify_sub *sub))
{
static gboolean initialized = FALSE;
static gboolean result = FALSE;
if (initialized == TRUE)
return result;
event_callback = cb;
result = _ik_startup (ip_event_callback);
if (!result)
return FALSE;
path_dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
sub_dir_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
wd_dir_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
initialized = TRUE;
return TRUE;
}
static void
ip_map_path_dir (const char *path,
ip_watched_dir_t *dir)
{
g_assert (path && dir);
g_hash_table_insert (path_dir_hash, dir->path, dir);
}
static void
ip_map_sub_dir (inotify_sub *sub,
ip_watched_dir_t *dir)
{
/* Associate subscription and directory */
g_assert (dir && sub);
g_hash_table_insert (sub_dir_hash, sub, dir);
dir->subs = g_list_prepend (dir->subs, sub);
}
static void
ip_map_wd_dir (gint32 wd,
ip_watched_dir_t *dir)
{
GList *dir_list;
g_assert (wd >= 0 && dir);
dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
dir_list = g_list_prepend (dir_list, dir);
g_hash_table_replace (wd_dir_hash, GINT_TO_POINTER (dir->wd), dir_list);
}
gboolean
_ip_start_watching (inotify_sub *sub)
{
gint32 wd;
int err;
ip_watched_dir_t *dir;
g_assert (sub);
g_assert (!sub->cancelled);
g_assert (sub->dirname);
IP_W ("Starting to watch %s\n", sub->dirname);
dir = g_hash_table_lookup (path_dir_hash, sub->dirname);
if (dir)
{
IP_W ("Already watching\n");
goto out;
}
IP_W ("Trying to add inotify watch ");
wd = _ik_watch (sub->dirname, IP_INOTIFY_MASK|IN_ONLYDIR, &err);
if (wd < 0)
{
IP_W ("Failed\n");
return FALSE;
}
else
{
/* Create new watched directory and associate it with the
* wd hash and path hash
*/
IP_W ("Success\n");
dir = ip_watched_dir_new (sub->dirname, wd);
ip_map_wd_dir (wd, dir);
ip_map_path_dir (sub->dirname, dir);
}
out:
ip_map_sub_dir (sub, dir);
return TRUE;
}
static void
ip_unmap_path_dir (const char *path,
ip_watched_dir_t *dir)
{
g_assert (path && dir);
g_hash_table_remove (path_dir_hash, dir->path);
}
static void
ip_unmap_wd_dir (gint32 wd,
ip_watched_dir_t *dir)
{
GList *dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
if (!dir_list)
return;
g_assert (wd >= 0 && dir);
dir_list = g_list_remove (dir_list, dir);
if (dir_list == NULL)
g_hash_table_remove (wd_dir_hash, GINT_TO_POINTER (dir->wd));
else
g_hash_table_replace (wd_dir_hash, GINT_TO_POINTER (dir->wd), dir_list);
}
static void
ip_unmap_wd (gint32 wd)
{
GList *dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
if (!dir_list)
return;
g_assert (wd >= 0);
g_hash_table_remove (wd_dir_hash, GINT_TO_POINTER (wd));
g_list_free (dir_list);
}
static void
ip_unmap_sub_dir (inotify_sub *sub,
ip_watched_dir_t *dir)
{
g_assert (sub && dir);
g_hash_table_remove (sub_dir_hash, sub);
dir->subs = g_list_remove (dir->subs, sub);
}
static void
ip_unmap_all_subs (ip_watched_dir_t *dir)
{
GList *l = NULL;
for (l = dir->subs; l; l = l->next)
{
inotify_sub *sub = l->data;
g_hash_table_remove (sub_dir_hash, sub);
}
g_list_free (dir->subs);
dir->subs = NULL;
}
gboolean
_ip_stop_watching (inotify_sub *sub)
{
ip_watched_dir_t *dir = NULL;
dir = g_hash_table_lookup (sub_dir_hash, sub);
if (!dir)
return TRUE;
ip_unmap_sub_dir (sub, dir);
/* No one is subscribing to this directory any more */
if (dir->subs == NULL)
{
_ik_ignore (dir->path, dir->wd);
ip_unmap_wd_dir (dir->wd, dir);
ip_unmap_path_dir (dir->path, dir);
ip_watched_dir_free (dir);
}
return TRUE;
}
static ip_watched_dir_t *
ip_watched_dir_new (const char *path,
gint32 wd)
{
ip_watched_dir_t *dir = g_new0 (ip_watched_dir_t, 1);
dir->path = g_strdup (path);
dir->wd = wd;
return dir;
}
static void
ip_watched_dir_free (ip_watched_dir_t *dir)
{
g_assert (dir->subs == NULL);
g_free (dir->path);
g_free (dir);
}
static void
ip_wd_delete (gpointer data,
gpointer user_data)
{
ip_watched_dir_t *dir = data;
GList *l = NULL;
for (l = dir->subs; l; l = l->next)
{
inotify_sub *sub = l->data;
/* Add subscription to missing list */
_im_add (sub);
}
ip_unmap_all_subs (dir);
/* Unassociate the path and the directory */
ip_unmap_path_dir (dir->path, dir);
ip_watched_dir_free (dir);
}
static void
ip_event_dispatch (GList *dir_list,
GList *pair_dir_list,
ik_event_t *event)
{
GList *dirl;
if (!event)
return;
for (dirl = dir_list; dirl; dirl = dirl->next)
{
GList *subl;
ip_watched_dir_t *dir = dirl->data;
for (subl = dir->subs; subl; subl = subl->next)
{
inotify_sub *sub = subl->data;
/* If the subscription and the event
* contain a filename and they don't
* match, we don't deliver this event.
*/
if (sub->filename &&
event->name &&
strcmp (sub->filename, event->name))
continue;
/* If the subscription has a filename
* but this event doesn't, we don't
* deliever this event.
*/
if (sub->filename && !event->name)
continue;
/* FIXME: We might need to synthesize
* DELETE/UNMOUNT events when
* the filename doesn't match
*/
event_callback (event, sub);
}
}
if (!event->pair)
return;
for (dirl = pair_dir_list; dirl; dirl = dirl->next)
{
GList *subl;
ip_watched_dir_t *dir = dirl->data;
for (subl = dir->subs; subl; subl = subl->next)
{
inotify_sub *sub = subl->data;
/* If the subscription and the event
* contain a filename and they don't
* match, we don't deliver this event.
*/
if (sub->filename &&
event->pair->name &&
strcmp (sub->filename, event->pair->name))
continue;
/* If the subscription has a filename
* but this event doesn't, we don't
* deliever this event.
*/
if (sub->filename && !event->pair->name)
continue;
/* FIXME: We might need to synthesize
* DELETE/UNMOUNT events when
* the filename doesn't match
*/
event_callback (event->pair, sub);
}
}
}
static void
ip_event_callback (ik_event_t *event)
{
GList* dir_list = NULL;
GList* pair_dir_list = NULL;
dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (event->wd));
/* We can ignore the IGNORED events */
if (event->mask & IN_IGNORED)
{
_ik_event_free (event);
return;
}
if (event->pair)
pair_dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (event->pair->wd));
if (event->mask & IP_INOTIFY_MASK)
ip_event_dispatch (dir_list, pair_dir_list, event);
/* We have to manage the missing list
* when we get an event that means the
* file has been deleted/moved/unmounted.
*/
if (event->mask & IN_DELETE_SELF ||
event->mask & IN_MOVE_SELF ||
event->mask & IN_UNMOUNT)
{
/* Add all subscriptions to missing list */
g_list_foreach (dir_list, ip_wd_delete, NULL);
/* Unmap all directories attached to this wd */
ip_unmap_wd (event->wd);
}
_ik_event_free (event);
}
const char *
_ip_get_path_for_wd (gint32 wd)
{
GList *dir_list;
ip_watched_dir_t *dir;
g_assert (wd >= 0);
dir_list = g_hash_table_lookup (wd_dir_hash, GINT_TO_POINTER (wd));
if (dir_list)
{
dir = dir_list->data;
if (dir)
return dir->path;
}
return NULL;
}