[vhffs-dev] [1529] added slave check support (quick'n'dirty compile-time option)

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1529
Author:   gradator
Date:     2010-01-09 18:29:44 +0100 (Sat, 09 Jan 2010)
Log Message:
-----------
added slave check support (quick'n'dirty compile-time option)

Modified Paths:
--------------
    trunk/vhffs-fssync/HOWTOCOMPILEANDRUN
    trunk/vhffs-fssync/vhffsfssync_slave.c

Modified: trunk/vhffs-fssync/HOWTOCOMPILEANDRUN
===================================================================
--- trunk/vhffs-fssync/HOWTOCOMPILEANDRUN	2009-12-21 15:36:18 UTC (rev 1528)
+++ trunk/vhffs-fssync/HOWTOCOMPILEANDRUN	2010-01-09 17:29:44 UTC (rev 1529)
@@ -1,3 +1,4 @@
-gcc -ggdb -Wall -Werror -o vhffsfssync_master `pkg-config --cflags --libs glib-2.0` vhffsfssync_master.c && ./vhffsfssync_master src
-gcc -ggdb -Wall -Werror -o vhffsfssync_slave `pkg-config --cflags --libs glib-2.0` vhffsfssync_slave.c && ./vhffsfssync_slave dst0 127.0.0.1 4567
+gcc -ggdb -Wall -Werror -o vhffsfssync_master `pkg-config --cflags --libs glib-2.0` vhffsfssync_master.c
+gcc -ggdb -Wall -Werror -o vhffsfssync_slave `pkg-config --cflags --libs glib-2.0` vhffsfssync_slave.c
+gcc -DCHECK -ggdb -Wall -Werror -o vhffsfssync_check `pkg-config --cflags --libs glib-2.0` vhffsfssync_slave.c
 

Modified: trunk/vhffs-fssync/vhffsfssync_slave.c
===================================================================
--- trunk/vhffs-fssync/vhffsfssync_slave.c	2009-12-21 15:36:18 UTC (rev 1528)
+++ trunk/vhffs-fssync/vhffsfssync_slave.c	2010-01-09 17:29:44 UTC (rev 1529)
@@ -209,7 +209,7 @@
 	return 0;
 }
 
-
+#ifndef CHECK
 /* ---------------------------------------- */
 int vhffsfssync_remove(char *pathname)  {
 	struct stat st;
@@ -289,6 +289,7 @@
 	if(i != argc) return -1;
 	return 0;
 }
+#endif
 
 
 int vhffsfssync_checkfile(vhffsfssync_conn *conn, char *path, char *type, int long long size, int long long mtime) {
@@ -312,7 +313,9 @@
 			/* we don't need other file types (chr, block, fifo, socket, ...) */
 
 			if(strcmp(type, type_)) {
+#ifndef CHECK
 				vhffsfssync_remove(path);
+#endif
 				fetch = TRUE;
 			}
 			else if(st.st_size != size) {
@@ -332,15 +335,20 @@
 			return -1;
 		}
 	}
-
+#ifndef CHECK
 	if(fetch) {
 		vhffsfssync_net_send_event(conn, g_strdup_printf("get%c%s%c", '\0', path, '\0') );
 	}
+#else
+	if(fetch) {
+		printf("%s is not synched\n", path );
+	}
+#endif
 
 	return 0;
 }
 
-
+#ifndef CHECK
 int vhffsfssync_event(vhffsfssync_conn *conn, char *event)  {
 	char *cur, **args = NULL;
 	int argalloc = 0, argc = 0;
@@ -537,8 +545,126 @@
 	free(args);
 	return ret;
 }
+#endif
 
 
+#ifdef CHECK
+int vhffsfssync_event(vhffsfssync_conn *conn, char *event)  {
+	char *cur, **args = NULL;
+	int argalloc = 0, argc = 0;
+	int ret = 0;
+
+	do {
+		for(cur = event ; *cur++ != '\0' ; );
+
+		if(argc >= argalloc)  {
+			argalloc = ( (argc >>8) +1) <<8;
+			args = realloc( args, argalloc * sizeof(char*) );
+		}
+
+		args[argc++] = event;
+		event = cur;
+	} while(*event);
+	if(!argc) return -1;
+
+#if DEBUG_EVENTS
+	int i;
+	for(i = 0 ; i < argc ; i++) {
+		printf("%s ", args[i]);
+	}
+	printf("\n");
+#endif
+
+	if(!strcmp(args[0], "remove")) {
+	}
+	else if(!strcmp(args[0], "create")) {
+	}
+	else if(!strcmp(args[0], "open")) {
+	}
+	else if(!strcmp(args[0], "close")) {
+	}
+	else if(!strcmp(args[0], "mkdir")) {
+	}
+	else if(!strcmp(args[0], "symlink")) {
+	}
+	else if(!strcmp(args[0], "move")) {
+	}
+	else if(!strcmp(args[0], "write")) {
+	}
+	else if(!strcmp(args[0], "ls")) {
+		char *root, *root_;
+		struct stat st;
+
+		root = args[1];
+		root_ = strdup(root);
+
+		if( lstat(root, &st) ) {
+			printf("%s not found on slave\n", root );
+		}
+		else {
+			int i;
+			GHashTable *filesindex;
+			DIR *d;
+
+			// build an index with all files
+			filesindex = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
+
+			// check each file
+			for( i = 2 ; i < argc ; i+=4 ) {
+				char *path;
+				g_hash_table_insert(filesindex, args[i], args[i]);
+
+				path = g_strdup_printf("%s/%s", root_, args[i]);
+				vhffsfssync_checkfile(conn, path, args[i+1], atoll(args[i+2]), atoll(args[i+3]) );
+				free(path);
+			}
+
+			// check for removed files
+			d = opendir(root_);
+			if(d) {
+				struct dirent *dir;
+				while( (dir = readdir(d)) )  {
+					if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") )  {
+						if(! g_hash_table_lookup(filesindex, dir->d_name) )  {
+							char *path = g_strdup_printf("%s/%s", root_, dir->d_name);
+							printf("%s found on slave, not present on master\n", path );
+							free(path);
+						}
+					}
+				}
+				closedir(d);
+			}
+			else {
+				printf("%s is not a directory\n", root );
+			}
+			g_hash_table_destroy(filesindex);
+		}
+		free(root_);
+	}
+	else if(!strcmp(args[0], "time")) {
+		time_t sec;
+		struct timeval tv;
+		gettimeofday(&tv, NULL);
+		sec = atol(args[1]);
+		if( abs(tv.tv_sec - sec) > 10 ) {
+			fprintf(stderr, "The slave timestamp is not synchronous with the master timestamp\n");
+			ret = -1;
+		}
+	}
+	else if(!strcmp(args[0], "hello")) {
+		// nice to meet you
+	}
+	else {
+		fprintf(stderr, "Received unhandled event: %s\n", args[0]);
+		ret = -1;
+	}
+
+	free(args);
+	return ret;
+}
+#endif
+
+
 int vhffsfssync_parse(vhffsfssync_conn *conn)  {
 	char *cur, *end;
 
@@ -658,6 +784,10 @@
 	char *root = NULL;
 	int limitrate = 0;
 
+#ifdef CHECK
+	foreground = 1;
+#endif
+
 	struct option long_options[] = {
 		{ "foreground", no_argument, NULL, 'f' },
 		{ "limit-rate", required_argument, NULL, 'r' },


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/