run_inotify.cxx 1.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "db/update/InotifySource.hxx"
23
#include "event/Loop.hxx"
24
#include "Log.hxx"
25

26 27
#include <exception>

28 29
#include <sys/inotify.h>

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

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

int main(int argc, char **argv)
45
try {
46 47 48
	const char *path;

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

	path = argv[1];

55
	EventLoop event_loop;
56
	const ShutdownHandler shutdown_handler(event_loop);
57

58 59
	InotifySource source(event_loop, my_inotify_callback, nullptr);
	source.Add(path, IN_MASK);
60

61
	event_loop.Run();
62

63
	return EXIT_SUCCESS;
64 65
} catch (...) {
	LogError(std::current_exception());
66
	return EXIT_FAILURE;
67
}