svn commit: linux-cifs-client r41 - in branches/SOC/linux-2.6bk-dnotify/fs/cifs: . examples

asser at samba.org asser at samba.org
Sun Aug 28 21:23:49 GMT 2005


Author: asser
Date: 2005-08-28 21:23:49 +0000 (Sun, 28 Aug 2005)
New Revision: 41

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=linux-cifs-client&rev=41

Log:
Added a few simple examples on how to use dnotify and inotify

Added:
   branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/
   branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/inotify.c
   branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/notify.c


Changeset:
Added: branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/inotify.c
===================================================================
--- branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/inotify.c	2005-08-28 14:59:17 UTC (rev 40)
+++ branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/inotify.c	2005-08-28 21:23:49 UTC (rev 41)
@@ -0,0 +1,47 @@
+#include <stdio.h>
+#include <linux/inotify.h>
+#include <linux/inotify-syscalls.h>
+#include <linux/kernel.h>
+#include <sys/poll.h>
+#include <string.h>
+#include <errno.h>
+
+int main(int argc, char *argv[]) {
+	int fd, wd, bytes_read;
+	struct pollfd pfd;
+	struct inotify_event event;
+
+	if(0 > (fd = inotify_init())) {
+		printf("Error on notify init: %d\n", fd);
+		exit(-1);
+	}
+	
+	if(argc < 2) {
+		printf("Usage: %s <file>\n", argv[0]);
+		exit(-1);
+	}
+
+	if(0 > (wd = inotify_add_watch(fd, argv[1], IN_MODIFY))) {
+		printf("Error on add watch: %d\n", wd);
+		exit(-1);
+	}
+
+	pfd.fd = fd;
+	pfd.events = POLLIN | POLLERR | POLLNVAL;
+
+	while(1) {
+		if(0 > poll(&pfd, 1, -1)) {
+			printf("Error on poll: %s\n", strerror(errno));
+			exit(-1);
+		}
+
+		if(pfd.revents & POLLIN) {
+			bytes_read = read(fd, &event, sizeof(struct inotify_event));
+			if(bytes_read < 0) {
+				printf("Error reading some bytes and stuff: %s\n", strerror(bytes_read));
+			} else {
+				printf("Event on %ld mask %lx\n", event.wd, event.mask);
+			}
+		}
+	}
+}

Added: branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/notify.c
===================================================================
--- branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/notify.c	2005-08-28 14:59:17 UTC (rev 40)
+++ branches/SOC/linux-2.6bk-dnotify/fs/cifs/examples/notify.c	2005-08-28 21:23:49 UTC (rev 41)
@@ -0,0 +1,115 @@
+#define _GNU_SOURCE
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <stdio.h>
+#include <errno.h>
+#include <signal.h>
+
+int requiredEvent = 0;
+
+void
+eventHandler( int sig )
+{
+  requiredEvent = 1;
+}
+
+
+int
+main( int argc, char *argv[] )
+{
+  struct stat      statBuf;
+  struct sigaction action;
+  int              fd;
+  char            *dir;
+  char            *slashPtr;
+  char             splitFilename[128];
+  unsigned long	   mask;
+
+  if( argc != 2 ) {
+    printf( "Usage: %s filename", argv[0] );
+    exit( -1 );
+  }
+
+  if( lstat( argv[1], &statBuf ) < 0 ) {
+    printf( "Cannot lstat %s : %s\n", argv[1], strerror(errno) );
+    exit( -1 );
+  }
+
+  if( ! S_ISREG( statBuf.st_mode ) ) {
+    printf( "%s is not a regular file\n", argv[1] );
+    exit( -1 );
+  }
+
+  /* Find directory the file is in, or assume the current directory */
+  strncpy( splitFilename, argv[1], 127 );
+  if( (slashPtr = rindex( splitFilename, '/' )) == NULL ) {
+    dir       = ".";
+  } else {
+    *slashPtr = 0;
+    dir       = splitFilename;
+  }
+
+  /* Set up the signal handler */
+  action.sa_handler = eventHandler;
+  sigemptyset( &action.sa_mask );
+  action.sa_flags = SA_RESTART;
+  sigaction( SIGRTMIN, &action, NULL );
+
+  /* Open the directory the file is in */
+  if( (fd = open( dir, O_RDONLY )) < 0 ) {
+    printf( "Cannot open %s for reading : %s\n", dir, strerror(errno) );
+    exit( -1 );
+  }
+
+  /* Choose the signal I want to receive when the directory content changes */
+  if( fcntl( fd, F_SETSIG, SIGRTMIN) < 0 ) {
+    printf( "Cannot set signal : %s\n", strerror(errno) );
+    exit( -1 );
+  }
+
+  mask = DN_MODIFY|DN_MULTISHOT;
+  printf("Asking for notification with mask %lx\n", mask);
+  /* Ask for notification when a modification is made in the directory */
+  if( fcntl( fd, F_NOTIFY, mask ) < 0 ) {
+    printf( "Cannot fcntl %s : %s\n", dir, strerror(errno) );
+    exit( -1 );
+  }
+
+  /* Infinite loop - a real program would be doing stuff */
+  while( 1 ) {
+    time_t modifiedTime;
+
+    /* This demo has nothing to do, so just wait for a signal */
+    pause();
+
+    /* Check the flag which indicates the right signal has been received */
+    if( requiredEvent ) {
+
+      /* Something has been modified in the directory - stat our file */
+      modifiedTime = statBuf.st_mtime;
+
+      if( lstat( argv[1], &statBuf ) < 0 ) {
+	printf( "Cannot lstat %s : %s\n", argv[1], strerror(errno) );
+	exit( -1 );
+      }
+
+      /* If the modification time has changed, the file has been altered */
+      if( modifiedTime != statBuf.st_mtime ) {
+	printf( "File %s has been modified\n", argv[1] );
+
+	sleep(15);
+	printf("Cancelling notification\n");
+	if(fcntl(fd, F_NOTIFY, 0) < 0)
+		printf("Cannot fcntl: %s\n", strerror(errno));
+	
+	sleep(15);
+	exit(0);
+      }
+
+      requiredEvent = 0;
+    }
+  }
+
+}
+



More information about the samba-cvs mailing list