File: inotify-sub.c

package info (click to toggle)
gnome-vfs 1:2.24.4-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,120 kB
  • ctags: 10,397
  • sloc: ansic: 78,516; sh: 10,341; makefile: 902; perl: 99
file content (148 lines) | stat: -rw-r--r-- 3,652 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* inotify-helper.c - Gnome VFS 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"
#include <string.h>
#include <glib.h>
#include <libgnomevfs/gnome-vfs-module-shared.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include "inotify-sub.h"

static gboolean     is_debug_enabled = FALSE;
#define IS_W if (is_debug_enabled) g_warning

static void ih_sub_setup (ih_sub_t *sub);

ih_sub_t *
ih_sub_new (GnomeVFSURI *uri, GnomeVFSMonitorType mon_type)
{
	ih_sub_t *sub = NULL;

	sub = g_new0 (ih_sub_t, 1);
	sub->type = mon_type;
	sub->uri = uri;
	gnome_vfs_uri_ref (uri);
	sub->pathname = gnome_vfs_unescape_string (gnome_vfs_uri_get_path (uri), "/");
	/* gnome_vfs_unescape_string returns NULL when
	 * the uri path passed to it is invalid.
	 */
	if (!sub->pathname)
	{
		IS_W("new subscription for %s failed because of invalid characters.\n", gnome_vfs_uri_get_path (uri));
		g_free (sub);
		gnome_vfs_uri_unref (uri);
		return NULL;
	}
/* TODO: WAITING for flag to be implemented
	if (mon_type & GNOME_VFS_DONT_FOLLOW_SYMLINK)
	{
		sub->extra_flags |= IN_DONT_FOLLOW;
	}
*/

	IS_W("new subscription for %s being setup\n", sub->pathname);

	ih_sub_setup (sub);
	return sub;
}

void
ih_sub_free (ih_sub_t *sub)
{
	if (sub->filename)
		g_free (sub->filename);
	if (sub->dirname)
	    g_free (sub->dirname);
	g_free (sub->pathname);
	gnome_vfs_uri_unref (sub->uri);
	g_free (sub);
}

static
gchar *ih_sub_get_uri_dirname (GnomeVFSURI *uri)
{
	gchar *t, *out;
	t = gnome_vfs_uri_extract_dirname (uri);
	out = gnome_vfs_unescape_string (t, "/");
	g_free (t);

	return out;
}

static
gchar *ih_sub_get_uri_filename (GnomeVFSURI *uri)
{
	gchar *t, *out;
	t = gnome_vfs_uri_extract_short_name (uri);
	out = gnome_vfs_unescape_string (t, "/");
	g_free (t);

	return out;
}

static 
void ih_sub_fix_dirname (ih_sub_t *sub)
{
	size_t len = 0;
	
	g_assert (sub->dirname);

	len = strlen (sub->dirname);

	/* We need to strip a trailing slash
	 * to get the correct behaviour
	 * out of the kernel
	 */
	if (sub->dirname[len] == '/')
		sub->dirname[len] = '\0';
}

/*
 * XXX: Currently we just follow the gnome vfs monitor type flags when
 * deciding how to treat the path. In the future we could try
 * and determine whether the path points to a directory or a file but
 * that is racey.
 */
static void
ih_sub_setup (ih_sub_t *sub)
{
	if (sub->type & GNOME_VFS_MONITOR_DIRECTORY)
	{
		sub->dirname = g_strdup (sub->pathname);
		sub->filename = NULL;
	} else {
		sub->dirname = ih_sub_get_uri_dirname (sub->uri);
		sub->filename = ih_sub_get_uri_filename (sub->uri);
	}

	ih_sub_fix_dirname (sub);

	IS_W("sub->dirname = %s\n", sub->dirname);
	if (sub->filename)
	{
		IS_W("sub->filename = %s\n", sub->filename);
	}
}