File: file_change_notifier.cpp

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (237 lines) | stat: -rw-r--r-- 5,397 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// K-3D
// Copyright (c) 1995-2008, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
	\author Bart Janssens (bart.janssens@lid.kviv.be)
	\author Timothy M. Shead (tshead@k-3d.com)
*/

#include "inotify-cxx.h"

#include <k3d-i18n-config.h>
#include <k3dsdk/application_plugin_factory.h>
#include <k3dsdk/ifile_change_notifier.h>
#include <k3dsdk/log.h>
#include <k3dsdk/path.h>
#include <k3dsdk/result.h>

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include <stdexcept>

namespace module
{

namespace inotify
{

class file_change_notifier :
	public k3d::ifile_change_notifier
{
public:
	file_change_notifier()
	{
		try
		{
			inotify.reset(new Inotify());
			inotify->SetNonBlock(true);
		}
		catch(InotifyException& e)
		{
			inotify.reset();
			k3d::log() << error << e.GetMessage() << " - Inotify-based change notification disabled." << std::endl;
		}
	}
	
	k3d::uint_t watch_file(const k3d::filesystem::path& Path, const sigc::slot<void>& Slot)
	{
		try
		{
			if(!inotify)
				throw std::runtime_error("Inotify-based change notification disabled.");

			if(Path.empty())
				throw std::runtime_error("Cannot watch empty path.");

			boost::shared_ptr<InotifyWatch> watch;
			if(paths.count(Path))
			{
				watch = paths[Path];
			}
			else
			{
				watch.reset(new InotifyWatch(Path.native_filesystem_string(), IN_CLOSE_WRITE));
				inotify->Add(*watch);
				paths[Path] = watch;
			}

			for(k3d::uint_t i = 0; i != watches.size(); ++i)
			{
				if(!watches[i])
				{
					watches[i] = watch.get();
					slots[i] = Slot;
					return i + 1;
				}
			}

			watches.push_back(watch.get());
			slots.push_back(Slot);
			return watches.size();
		}
		catch(InotifyException& e)
		{
			k3d::log() << error << e.GetMessage() << " code: " << e.GetErrorNumber() << std::endl;
			return 0;
		}
		catch(std::exception& e)
		{
			k3d::log() << error << e.what() << std::endl;
			return 0;
		}
	}
	
	void unwatch_file(const k3d::uint_t WatchID)
	{
		if(!inotify)
			return;

		if(WatchID == 0)
			return;

		try
		{
			const k3d::uint_t watch_index = WatchID - 1;

			if(watch_index >= watches.size())
				throw std::runtime_error("Watch ID out-of-range.");

			if(!watches[watch_index])
				throw std::runtime_error("Invalid Watch ID.");

			InotifyWatch* const watch = watches[watch_index];
			watches[watch_index] = 0;
			slots[watch_index] = sigc::slot<void>();

			if(0 == std::count(watches.begin(), watches.end(), watch))
			{
				for(paths_t::iterator path = paths.begin(); path != paths.end(); ++path)
				{
					if(path->second.get() == watch)
					{	
						inotify->Remove(*watch);
						paths.erase(path);
						return;
					}
				}
			}

		}
		catch(InotifyException& e)
		{
			k3d::log() << error << e.GetMessage() << " code: " << e.GetErrorNumber() << std::endl;
		}
		catch(std::exception& e)
		{
			k3d::log() << error << e.what() << std::endl;
		}
	}

	void wait_for_changes()
	{
		if(!inotify)
			return;
		
		// In the blocking case, we check for events in a manner that does not require holding a lock on the inotify object
		inotify->SetNonBlock(false);
		fd_set read_descriptors;
		FD_ZERO(&read_descriptors);
		FD_SET(inotify->GetDescriptor(), &read_descriptors);
		select(inotify->GetDescriptor() + 1, &read_descriptors, NULL, NULL, NULL);
	}
	
	const k3d::uint_t change_count()
	{
		if(!inotify)
			return 0;
		
		inotify->SetNonBlock(true);
		inotify->WaitForEvents();
		return inotify->GetEventCount();
	}
	
	void signal_change()
	{
		if(!inotify)
			return;

		if(!inotify->GetEventCount())
			return;
		
		InotifyEvent event;
		if(!inotify->GetEvent(&event))
		{
			return;
		}

		InotifyWatch* watch = event.GetWatch();
		for(k3d::uint_t i = 0; i != watches.size(); ++i)
		{
			if(watches[i] == watch)
				slots[i]();
		}
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::application_plugin_factory<file_change_notifier,
			k3d::interface_list<k3d::ifile_change_notifier> > factory(
				k3d::uuid(0x37e5155b, 0xed45e9aa, 0xd1f0ed88, 0xb582ea76),
				"InotifyFileChangeNotifier",
				_("Monitors files for changes, using the Linux inotify system"),
				"Desktop",
				k3d::iplugin_factory::STABLE);

		return factory;
	}
	
private:
	boost::scoped_ptr<Inotify> inotify;

	typedef std::map<k3d::filesystem::path, boost::shared_ptr<InotifyWatch> > paths_t;
	paths_t paths;

	std::vector<InotifyWatch*> watches;
	std::vector<sigc::slot<void> > slots;
};


/////////////////////////////////////////////////////////////////////////////
// file_change_notifier_factory

k3d::iplugin_factory& file_change_notifier_factory()
{
	return file_change_notifier::get_factory();
}

} // namespace inotify

} // namespace module