run_inotify.cxx 1.94 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 <sys/inotify.h>

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

static void
37 38
my_inotify_callback(gcc_unused int wd, unsigned mask,
		    const char *name, gcc_unused void *ctx)
39
{
40
	printf("mask=0x%x name='%s'\n", mask, name);
41 42 43 44 45 46 47
}

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

	if (argc != 2) {
48 49
		fprintf(stderr, "Usage: run_inotify PATH\n");
		return EXIT_FAILURE;
50 51 52 53
	}

	path = argv[1];

54 55
	EventLoop event_loop((EventLoop::Default()));
	const ShutdownHandler shutdown_handler(event_loop);
56

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

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

73
	event_loop.Run();
74

75
	delete source;
76
	return EXIT_SUCCESS;
77
}