From 59d34ee47b14c728ebd6399fb89c409383a1aa2e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 11 Jul 2008 00:04:58 +0200 Subject: Initial import --- Makefile | 21 +++++++++++ direct-append.c | 71 +++++++++++++++++++++++++++++++++++ inotify-watch.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 Makefile create mode 100644 direct-append.c create mode 100644 inotify-watch.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0212a22 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +CC := gcc +CFLAGS := $(CFLAGS) -I../inotail -W -Wall -pipe -D_USE_SOURCE \ + -Wstrict-prototypes -Wsign-compare -Wshadow -Wchar-subscripts \ + -Wmissing-declarations -Wpointer-arith -Wcast-align -Wmissing-prototypes + +# Compile with 'make DEBUG=true' to enable debugging +DEBUG = false +ifeq ($(strip $(DEBUG)),true) + CFLAGS += -g -DDEBUG -fmudflap + LDFLAGS += -lmudflap +endif + +all: inotify-watch direct-append +inotify-watch: inotify-watch.o +direct-append: direct-append.o + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f inotify-watch direct-append *.o diff --git a/direct-append.c b/direct-append.c new file mode 100644 index 0000000..bf18c72 --- /dev/null +++ b/direct-append.c @@ -0,0 +1,71 @@ +/* + * Append some random characters to a file + * + * Just a small tool to debug inotify + */ + +#include +#include +#include + +#include +#include + +#define DEFAULT_N_CHARS 1024 + +static char get_random_printable_char(void) +{ + int fd; + char ret; + + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + do { + read(fd, &ret, sizeof(ret)); + } while (ret < 0x20 || ret > 0x7e); + + close(fd); + + return ret; +} + +int main(int argc, char *argv[]) +{ + int fd, n_chars, i; + char *str; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + /* Open the file, create it in case it doesn't exist */ + if ((fd = open(argv[1], O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + if (lseek(fd, 0, SEEK_END) < 0) { + perror("lseek"); + exit(EXIT_FAILURE); + } + + n_chars = strtoul(argv[2], NULL, 0); + if (n_chars <= 0) + n_chars = DEFAULT_N_CHARS; + + str = malloc(n_chars * sizeof(char)); + + for (i = 0; i < n_chars; i++) + str[i] = get_random_printable_char(); + + write(fd, str, n_chars); + free(str); + close(fd); + + exit(EXIT_SUCCESS); +} diff --git a/inotify-watch.c b/inotify-watch.c new file mode 100644 index 0000000..7ccf9c4 --- /dev/null +++ b/inotify-watch.c @@ -0,0 +1,113 @@ +/* + * inotify-watchdir.c + * + * Watch a file or directory for changes using inotify + * + * Copyright (c) Tobias Klauser + * All rights reserved + */ + +#include +#include +#include +#include +#include +#include + +#include "inotify.h" +#include "inotify-syscalls.h" + +struct mask2string { + uint32_t mask; + const char *desc; +}; + +static struct mask2string events[] = { + { IN_ACCESS, "ACCESS" }, + { IN_MODIFY, "MODIFY" }, + { IN_ATTRIB, "ATTRIB" }, + { IN_CLOSE_WRITE, "CLOSE_WRITE" }, + { IN_CLOSE_NOWRITE, "CLOSE_NOWRITE" }, + { IN_OPEN, "OPEN" }, + { IN_MOVED_FROM, "MOVED_FROM" }, + { IN_MOVED_TO, "MOVED_TO" }, + { IN_CREATE, "CREATE" }, + { IN_DELETE, "DELETE" }, + { IN_DELETE_SELF, "DELETE_SELF" }, + { IN_MOVE_SELF, "MOVE_SELF" }, + { IN_UNMOUNT, "UNMOUNT" }, + { IN_Q_OVERFLOW, "Q_OVERFLOW" }, + { IN_IGNORED, "IGNORED" }, + /* special flags */ + { IN_ONLYDIR, "ONLYDIR" }, + { IN_DONT_FOLLOW, "DONT_FOLLOW" }, + { IN_MASK_ADD, "MASK_ADD" }, + { IN_ONESHOT, "ONESHOT" }, + /* end of array */ + { -1, NULL } +}; + +static void inotify_print_event(struct inotify_event *inev) +{ + int i; + + /* stat? */ + printf("(%s) wd=%04x, cookie=%04x, len=%04x, name=\"%s\" :", + inev->mask & IN_ISDIR ? "dir" : "file", inev->wd, inev->cookie, inev->len, inev->name); + + for (i = 0; events[i].desc; i++) + if (inev->mask & events[i].mask) + printf(" %s", events[i].desc); + + printf("\n"); +} + +int main(int argc, char *argv[]) +{ + int fd, len; + int *watches; + struct inotify_event *inev; + char buf[1024]; + + if (argc < 2) { + printf("Usage: %s ...\n", *argv); + exit(EXIT_FAILURE); + } + + ++argv; + + fd = inotify_init(); + if (fd < 0) { + perror("inotify_init"); + exit(EXIT_FAILURE); + } + + watches = malloc(argc * sizeof(int)); + if (!watches) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + while (*argv) { + *watches = inotify_add_watch(fd, *argv, IN_ALL_EVENTS|IN_UNMOUNT); + if (*watches < 0) { + perror("inotify_add_watch"); + exit(EXIT_FAILURE); + } + ++watches; + ++argv; + } + + while (1) { + len = read(fd, buf, sizeof(buf)); + inev = (struct inotify_event *) &buf; + while (len > 0) { + inotify_print_event(inev); + + len -= sizeof(struct inotify_event) + inev->len; + inev = (struct inotify_event *) ((char *) inev + sizeof(struct inotify_event) + inev->len); + } + } + + return 0; +} -- cgit v1.2.3-54-g00ecf