run_inotify.cxx 1.71 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 <sys/inotify.h>

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

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

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

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

	path = argv[1];

53
	EventLoop event_loop;
54
	const ShutdownHandler shutdown_handler(event_loop);
55

56 57
	InotifySource source(event_loop, my_inotify_callback, nullptr);
	source.Add(path, IN_MASK);
58

59
	event_loop.Run();
60

61
	return EXIT_SUCCESS;
62 63 64
} catch (const std::runtime_error &e) {
	LogError(e);
	return EXIT_FAILURE;
65
}