fs_events.c 2.22 KB
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

#include <scot/socket.h>
#include <scot/socket_in.h>
#include <scot/fs_watcher.h>
#include <scot/event.h>
#include <scot/event_listener.h>
#include <scot/exception.h>
#include <scot/memory.h>

#include <scot_common.h>


struct scot_fs_watcher * fsw;

void
on_exit_func (void)
{
	scot_event_listener_stop ((struct scot_event_listener *) fsw);
	scot_fs_watcher_free (fsw);

	exc_end ();
}

void
exit_func (int inum)
{
	exit (0);
}

unsigned short
fs_cb (struct scot_event * _e)
{
	struct scot_fs_watcher_event * fswe;
	struct scot_event * e;
	char * e_s;

	printf ("serialized %d bytes\n", scot_event_serialize (_e, &e_s));
	e    = scot_event_deserialize (e, e_s);
	printf ("deserialized event\n");
	fswe = (struct scot_fs_watcher_event *) e;
	SCOT_MEM_FREE (e_s);

	switch (e->event)
	{
		case (SCOT_EVENT_FS_WATCHER_CREATE):
			printf ("%s/%s created\n", fswe->path, fswe->name);
			break;
		case (SCOT_EVENT_FS_WATCHER_DELETE):
			printf ("%s/%s deleted\n", fswe->path, fswe->name);
			break;
		case (SCOT_EVENT_FS_WATCHER_RENAME):
			printf ("%s/%s renamed to %s/%s\n", 
					fswe->path, fswe->oldname, 
					fswe->path, fswe->name);
	}

	scot_event_free (e);

	return SCOT_EVENT_END;
}

int
main (int argc, char * argv [])
{
	excenv_t *           ee;

	signal (SIGINT, exit_func);
	atexit (on_exit_func);

	TRY
	{
		fsw = scot_fs_watcher_new ();

		scot_event_listener_register_cb (
				(struct scot_event_listener *) fsw,
				SCOT_EVENT_FS_WATCHER_CREATE,
				fs_cb, NULL);

		scot_event_listener_start ((struct scot_event_listener *) fsw);
	}
	CATCH (ee)
	{
		print_all_exceptions (ee);
		exit (EXIT_FAILURE);
	}


	TRY
	{
		char buffer [1024];

		fgets (buffer, 1024, stdin);
		*strchr (buffer, '\n') = '\0';

		while (strcmp ("quit", buffer) != 0)
		{
			scot_fs_watcher_add (
					fsw, buffer, 0 | SCOT_EVENT_FS_WATCHER_CREATE |
					             SCOT_EVENT_FS_WATCHER_DELETE |
					             SCOT_EVENT_FS_WATCHER_RENAME);

			fgets (buffer, 1024, stdin);
			*strchr (buffer, '\n') = '\0';
		}
	}
	CATCH (ee)
	{
		exception_t *e;

		while (e = retrive_exception (ee))
		{
			print_exception (e);
			free_exception (e);
		}

      free_catched (ee);
	}

	return 0;
}