run_inotify.cxx 1.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
21
#include "ShutdownHandler.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "InotifySource.hxx"
23
#include "event/Loop.hxx"
24
#include "util/Error.hxx"
25
#include "Log.hxx"
26

27 28
#include <glib.h>

29 30
#include <sys/inotify.h>

31
static constexpr unsigned IN_MASK =
32
#ifdef IN_ONLYDIR
33
	IN_ONLYDIR|
34
#endif
35 36
	IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
	|IN_MOVE|IN_MOVE_SELF;
37 38

static void
39 40
my_inotify_callback(gcc_unused int wd, unsigned mask,
		    const char *name, gcc_unused void *ctx)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
{
	g_print("mask=0x%x name='%s'\n", mask, name);
}

int main(int argc, char **argv)
{
	const char *path;

	if (argc != 2) {
		g_printerr("Usage: run_inotify PATH\n");
		return 1;
	}

	path = argv[1];

56 57
	EventLoop event_loop((EventLoop::Default()));
	const ShutdownHandler shutdown_handler(event_loop);
58

59
	Error error;
60
	InotifySource *source = InotifySource::Create(event_loop,
61
						      my_inotify_callback,
62
						      nullptr, error);
63
	if (source == NULL) {
64
		LogError(error);
65 66 67
		return 2;
	}

68
	int descriptor = source->Add(path, IN_MASK, error);
69
	if (descriptor < 0) {
70
		delete source;
71
		LogError(error);
72 73 74
		return 2;
	}

75
	event_loop.Run();
76

77
	delete source;
78
}