Dmitry Yu Okunev лет назад: 7
Родитель
Сommit
2278d2bf9b
56 измененных файлов с 8226 добавлено и 7647 удалено
  1. 22 25
      calc.c
  2. 11 12
      calc.h
  3. 30 37
      cgroup.c
  4. 3 3
      cgroup.h
  5. 13 13
      clsync.h
  6. 644 693
      cluster.c
  7. 11 11
      cluster.h
  8. 4 4
      common.h
  9. 105 101
      control.c
  10. 6 6
      control.h
  11. 63 63
      ctx.h
  12. 173 163
      error.c
  13. 11 11
      error.h
  14. 93 88
      fileutils.c
  15. 9 9
      fileutils.h
  16. 23 23
      gencompilerflags.c
  17. 24 30
      glibex.c
  18. 7 7
      glibex.h
  19. 4 4
      indexes.c
  20. 78 76
      indexes.h
  21. 47 42
      libclsync.c
  22. 7 7
      libclsync.h
  23. 8 8
      macros.h
  24. 1651 1376
      main.c
  25. 16 16
      main.h
  26. 85 82
      malloc.c
  27. 11 11
      malloc.h
  28. 376 346
      mon_bsm.c
  29. 9 9
      mon_bsm.h
  30. 102 91
      mon_dtracepipe.c
  31. 9 9
      mon_dtracepipe.h
  32. 35 26
      mon_fanotify.c
  33. 5 5
      mon_fanotify.h
  34. 173 174
      mon_gio.c
  35. 9 9
      mon_gio.h
  36. 74 79
      mon_inotify.c
  37. 8 8
      mon_inotify.h
  38. 350 342
      mon_kqueue.c
  39. 8 8
      mon_kqueue.h
  40. 21 22
      port-hacks.h
  41. 33 27
      posix-hacks.c
  42. 6 6
      posix-hacks.h
  43. 1016 982
      privileged.c
  44. 47 47
      privileged.h
  45. 32 29
      pthreadex.c
  46. 9 9
      pthreadex.h
  47. 163 146
      rules.c
  48. 7 7
      rules.h
  49. 362 353
      socket.c
  50. 26 26
      socket.h
  51. 69 53
      stringex.c
  52. 1 1
      stringex.h
  53. 2073 1870
      sync.c
  54. 24 24
      sync.h
  55. 5 5
      syscalls.c
  56. 15 13
      syscalls.h

+ 22 - 25
calc.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -29,38 +29,35 @@
 #ifndef HAVE_MHASH
 /**
  * @brief 			Calculated Adler32 value for char array
- * 
+ *
  * @param[in]	date		Pointer to data
  * @param[in]	len		Length of the data
- * 
+ *
  * @retval	uint32_t	Adler32 value of data
- * 
+ *
  */
 
 // Copied from http://en.wikipedia.org/wiki/Adler-32
-uint32_t adler32_calc(const unsigned char *const data, uint32_t len) { // where data is the location of the data in physical
-                                                                       // memory and len is the length of the data in bytes
-
-/*
-	if (len&3)
-		warning("len [%i] & 3 == %i != 0. Wrong length (not a multiple of 4).", len, len&3);
-*/
-
-	debug(70, "%p, %i", data, len);
-
+uint32_t adler32_calc ( const unsigned char *const data, uint32_t len ) // where data is the location of the data in physical
+{
+	// memory and len is the length of the data in bytes
+	/*
+		if (len&3)
+			warning("len [%i] & 3 == %i != 0. Wrong length (not a multiple of 4).", len, len&3);
+	*/
+	debug ( 70, "%p, %i", data, len );
 	const int MOD_ADLER = 65521;
 	uint32_t a = 1, b = 0;
 	int32_t index;
-	
+
 	// Process each byte of the data in order
-	for (index = 0; index < len; ++index)
-	{
-		debug(80, "%5i: %02x %02x %02x", index, data[index], a, b);
-		a = (a + data[index]) % MOD_ADLER;
-		b = (b + a) % MOD_ADLER;
+	for ( index = 0; index < len; ++index ) {
+		debug ( 80, "%5i: %02x %02x %02x", index, data[index], a, b );
+		a = ( a + data[index] ) % MOD_ADLER;
+		b = ( b + a ) % MOD_ADLER;
 	}
-	
-	return (b << 16) | a;
+
+	return ( b << 16 ) | a;
 }
 #endif
 

+ 11 - 12
calc.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -24,16 +24,15 @@
 
 #include "error.h"
 
-static inline uint32_t adler32_calc(const unsigned char *const data, uint32_t len) {
+static inline uint32_t adler32_calc ( const unsigned char *const data, uint32_t len )
+{
 	uint32_t adler32;
-	debug(70, "%p, %i -> mhash", data, len);
-
-	MHASH td = mhash_init(MHASH_ADLER32);
-	mhash(td, data, len);
-	mhash_deinit(td, &adler32);
-
+	debug ( 70, "%p, %i -> mhash", data, len );
+	MHASH td = mhash_init ( MHASH_ADLER32 );
+	mhash ( td, data, len );
+	mhash_deinit ( td, &adler32 );
 	return adler32;
 }
 #else
-extern uint32_t adler32_calc(const unsigned char *const data, uint32_t len);
+extern uint32_t adler32_calc ( const unsigned char *const data, uint32_t len );
 #endif

+ 30 - 37
cgroup.c

@@ -23,72 +23,65 @@
 
 static struct cgroup *cgroup = NULL;
 
-int clsync_cgroup_init(ctx_t *ctx_p) {
-	debug(2, "cgroup_name == \"%s\"", ctx_p->cg_groupname);
-
-	SAFE( cgroup_init(),							return -1; );
-	SAFE( (cgroup = cgroup_new_cgroup(ctx_p->cg_groupname)) == NULL,	return -1; );
-
+int clsync_cgroup_init ( ctx_t *ctx_p )
+{
+	debug ( 2, "cgroup_name == \"%s\"", ctx_p->cg_groupname );
+	SAFE ( cgroup_init(),							return -1; );
+	SAFE ( ( cgroup = cgroup_new_cgroup ( ctx_p->cg_groupname ) ) == NULL,	return -1; );
 	return 0;
 }
 
-__extension__ int clsync_cgroup_forbid_extra_devices() {
+__extension__ int clsync_cgroup_forbid_extra_devices()
+{
 	int rc;
 	char *allowed_devices[] = CG_ALLOWED_DEVICES, **allowed_device_i;
-
 	/*
 	 * Unfortunately, libcgroup doesn't allow multiple values for one key, and cgroups doesn't allow multiple devices for one set. So I was been have to write this hack. It adds character '/' to start of "devices.allow" for every new entry. So libclsync thinks that it's different keys, "/sys/fs/cgroup/devices/clsync/123/devices.allow" == "/sys/fs/cgroup/devices/clsync/123//devices.allow".
 	 */
-
-	char control_name_buf[BUFSIZ+BUFSIZ]={[0 ... BUFSIZ-1] = '/', 'd', 'e', 'v', 'i', 'c', 'e', 's', '.', 'a', 'l', 'l', 'o', 'w'}, *control_name = &control_name_buf[BUFSIZ];
-	debug(2, "");
-
+	char control_name_buf[BUFSIZ + BUFSIZ] = {[0 ... BUFSIZ - 1] = '/', 'd', 'e', 'v', 'i', 'c', 'e', 's', '.', 'a', 'l', 'l', 'o', 'w'}, *control_name = &control_name_buf[BUFSIZ];
+	debug ( 2, "" );
 	struct cgroup_controller *cgc;
-
-	SAFE( (cgc = cgroup_add_controller(cgroup, "devices")) == NULL,	return -1; );
-
-	debug(8, "Deny device: \"a\"");
-	SAFE( cgroup_add_value_string(cgc, "devices.deny", "a"),	return -1; );
+	SAFE ( ( cgc = cgroup_add_controller ( cgroup, "devices" ) ) == NULL,	return -1; );
+	debug ( 8, "Deny device: \"a\"" );
+	SAFE ( cgroup_add_value_string ( cgc, "devices.deny", "a" ),	return -1; );
 	allowed_device_i = allowed_devices;
-	while (*allowed_device_i != NULL) {
-
-		critical_on (control_name < control_name_buf);
 
-		debug(8, "Allow device: \"%s\" (\"%s\" = \"%s\")", *allowed_device_i, control_name, *allowed_device_i);
-		SAFE( cgroup_add_value_string(cgc, control_name, *allowed_device_i),return -1; );
+	while ( *allowed_device_i != NULL ) {
+		critical_on ( control_name < control_name_buf );
+		debug ( 8, "Allow device: \"%s\" (\"%s\" = \"%s\")", *allowed_device_i, control_name, *allowed_device_i );
+		SAFE ( cgroup_add_value_string ( cgc, control_name, *allowed_device_i ), return -1; );
 		control_name--;
 		allowed_device_i++;
 	}
 
-	if ((rc=cgroup_create_cgroup(cgroup, 1))) {
-		error("Got error while cgroup_create_cgroup(): %s", cgroup_strerror(rc));
+	if ( ( rc = cgroup_create_cgroup ( cgroup, 1 ) ) ) {
+		error ( "Got error while cgroup_create_cgroup(): %s", cgroup_strerror ( rc ) );
 		return -1;
 	}
 
 	return 0;
 }
 
-int clsync_cgroup_attach(ctx_t *ctx_p) {
+int clsync_cgroup_attach ( ctx_t *ctx_p )
+{
 	int rc;
-	debug(2, "");
+	debug ( 2, "" );
 
-	if ((rc=cgroup_attach_task_pid(cgroup, ctx_p->pid))) {
-		error("Got error while cgroup_attach_task_pid(): %s", cgroup_strerror(rc));
+	if ( ( rc = cgroup_attach_task_pid ( cgroup, ctx_p->pid ) ) ) {
+		error ( "Got error while cgroup_attach_task_pid(): %s", cgroup_strerror ( rc ) );
 		return -1;
 	}
 
 	return 0;
 }
 
-int clsync_cgroup_deinit(ctx_t *ctx_p) {
-	(void) ctx_p;
-
-	debug(2, "");
-
-	error_on(cgroup_delete_cgroup_ext(cgroup, CGFLAG_DELETE_IGNORE_MIGRATION | CGFLAG_DELETE_RECURSIVE));
-	cgroup_free(&cgroup);
-
-	debug(15, "end");
+int clsync_cgroup_deinit ( ctx_t *ctx_p )
+{
+	( void ) ctx_p;
+	debug ( 2, "" );
+	error_on ( cgroup_delete_cgroup_ext ( cgroup, CGFLAG_DELETE_IGNORE_MIGRATION | CGFLAG_DELETE_RECURSIVE ) );
+	cgroup_free ( &cgroup );
+	debug ( 15, "end" );
 	return 0;
 }
 

+ 3 - 3
cgroup.h

@@ -17,8 +17,8 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-extern int clsync_cgroup_init(struct ctx *ctx_p);
+extern int clsync_cgroup_init ( struct ctx *ctx_p );
 extern int clsync_cgroup_forbid_extra_devices();
-extern int clsync_cgroup_attach(struct ctx *ctx_p);
-extern int clsync_cgroup_deinit(struct ctx *ctx_p);
+extern int clsync_cgroup_attach ( struct ctx *ctx_p );
+extern int clsync_cgroup_deinit ( struct ctx *ctx_p );
 

+ 13 - 13
clsync.h

@@ -48,10 +48,10 @@ typedef struct api_eventinfo api_eventinfo_t;
 
 struct ctx;
 struct indexes;
-typedef int(*api_funct_init)  (struct ctx *, struct indexes *);
-typedef int(*api_funct_sync)  (int n, api_eventinfo_t *);
-typedef int(*api_funct_rsync) (const char *inclist, const char *exclist);
-typedef int(*api_funct_deinit)();
+typedef int ( *api_funct_init )  ( struct ctx *, struct indexes * );
+typedef int ( *api_funct_sync )  ( int n, api_eventinfo_t * );
+typedef int ( *api_funct_rsync ) ( const char *inclist, const char *exclist );
+typedef int ( *api_funct_deinit ) ();
 
 enum eventinfo_flags {
 	EVIF_NONE		= 0x00000000,	// No modifier
@@ -62,23 +62,23 @@ typedef enum eventinfo_flags eventinfo_flags_t;
 
 /**
  * @brief 			Writes the list to list-file for "--include-from" option of rsync using array of api_eventinfo_t
- * 
+ *
  * @param[in]	indexes_p	Pointer to "indexes"
  * @param[in]	listfile	File identifier to write to
  * @param[in]	n		Number of records in apievinfo
  * @param[in]	apievinfo	Pointer to api_eventinfo_t records
- * 
+ *
  * @retval	zero		Successful
  * @retval	non-zero	If got error while deleting the message. The error-code is placed into returned value.
- * 
+ *
  */
-extern int apievinfo2rsynclist(struct indexes *indexes_p, FILE *listfile, int n, api_eventinfo_t *apievinfo); // Not tested, yet
+extern int apievinfo2rsynclist ( struct indexes *indexes_p, FILE *listfile, int n, api_eventinfo_t *apievinfo ); // Not tested, yet
 
 /**
  * @brief 			Returns currect API version
- * 
+ *
  * @retval	api_version	Version of clsync's API
- * 
+ *
  */
 extern int clsyncapi_getapiversion();
 
@@ -86,13 +86,13 @@ extern int clsyncapi_getapiversion();
  * @brief 			clsync's wrapper for function "fork()". Should be used instead of "fork()" directly, to notify clsync about child's pid.
  *
  * @param[in]	ctx_p	Pointer to "ctx"
- * 
+ *
  * @retval	-1		If error (see "man 2 fork", added error code "ECANCELED" if too many children)
  * @retval	0		If child
  * @retval	pid		Pid of child of parent. (see "man 2 fork")
- * 
+ *
  */
-extern pid_t clsyncapi_fork(struct ctx *ctx_p);
+extern pid_t clsyncapi_fork ( struct ctx *ctx_p );
 
 #endif
 

Разница между файлами не показана из-за своего большого размера
+ 644 - 693
cluster.c


+ 11 - 11
cluster.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -32,11 +32,11 @@
 			error("CLUSTER_LOOP_EXPECTCMD()"); \
 			return errno; \
 		}\
-\
+		\
 		/* Is that the command we are expecting? Skipping if not. */\
 		if(clustercmd_p->h.cmd_id != clustercmd_id)\
 			continue;\
-}
+	}
 
 // Macros for writing messages
 
@@ -249,19 +249,19 @@ struct window {
 };
 typedef struct window window_t;
 
-typedef int (*cluster_recvproc_funct_t)(clustercmd_t *clustercmd_p);
+typedef int ( *cluster_recvproc_funct_t ) ( clustercmd_t *clustercmd_p );
 
 // Externs
 
-extern int cluster_init(struct ctx *ctx_p, struct indexes *indexes_p);
+extern int cluster_init ( struct ctx *ctx_p, struct indexes *indexes_p );
 extern int cluster_deinit();
 
-extern int cluster_lock(const char *fpath);
+extern int cluster_lock ( const char *fpath );
 extern int cluster_lock_byindexes();
 extern int cluster_unlock_all();
-extern int cluster_capture(const char *fpath);
+extern int cluster_capture ( const char *fpath );
 
-extern int cluster_modtime_update(const char *dirpath, short int dirlevel, mode_t st_mode);
+extern int cluster_modtime_update ( const char *dirpath, short int dirlevel, mode_t st_mode );
 extern int cluster_initialsync();
 
 #endif

+ 4 - 4
common.h

@@ -159,8 +159,8 @@ enum pthread_mutex_id {
 
 struct dosync_arg {
 	int evcount;
-	char excf_path[PATH_MAX+1];
-	char outf_path[PATH_MAX+1];
+	char excf_path[PATH_MAX + 1];
+	char outf_path[PATH_MAX + 1];
 	FILE *outf;
 	ctx_t *ctx_p;
 	struct indexes *indexes_p;
@@ -168,10 +168,10 @@ struct dosync_arg {
 	unsigned int linescount;
 	api_eventinfo_t *api_ei;
 	int api_ei_count;
-	char buf[BUFSIZ+1];
+	char buf[BUFSIZ + 1];
 
 // for be read by sync_parameter_get():
-	const char *include_list[MAXARGUMENTS+2];
+	const char *include_list[MAXARGUMENTS + 2];
 	size_t      include_list_count;
 	const char *list_type_str;
 	const char *evmask_str;

+ 105 - 101
control.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -37,128 +37,130 @@
 static pthread_t pthread_control;
 
 
-static inline int control_error(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, const char *const funct, const char *const args) {
-	debug(3, "%s(%s): %u: %s", funct, args, errno, strerror(errno));
-	return socket_reply(clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_ECUSTOM, funct, args, errno, strerror(errno));
+static inline int control_error ( clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, const char *const funct, const char *const args )
+{
+	debug ( 3, "%s(%s): %u: %s", funct, args, errno, strerror ( errno ) );
+	return socket_reply ( clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_ECUSTOM, funct, args, errno, strerror ( errno ) );
 }
 
 
-int control_dump(ctx_t *ctx_p, clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p) {
+int control_dump ( ctx_t *ctx_p, clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p )
+{
 	sockcmd_dat_dump_t *dat		= sockcmd_p->data;
-
-	debug(3, "%s", dat->dir_path);
-
-	return (sync_dump(ctx_p, dat->dir_path)) ? 
-		control_error(clsyncsock_p, sockcmd_p, "sync_dump", dat->dir_path) :
-		socket_reply(clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_DUMP);
+	debug ( 3, "%s", dat->dir_path );
+	return ( sync_dump ( ctx_p, dat->dir_path ) ) ?
+	       control_error ( clsyncsock_p, sockcmd_p, "sync_dump", dat->dir_path ) :
+	       socket_reply ( clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_DUMP );
 }
 
-int control_procclsyncsock(socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p) {
+int control_procclsyncsock ( socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p )
+{
 	int rc;
 	clsyncsock_t	*clsyncsock_p =          arg->clsyncsock_p;
-	ctx_t		*ctx_p        = (ctx_t *)arg->arg;
+	ctx_t		*ctx_p        = ( ctx_t * ) arg->arg;
 
-	switch(sockcmd_p->cmd_id) {
+	switch ( sockcmd_p->cmd_id ) {
 		case SOCKCMD_REQUEST_DUMP:
-			rc = control_dump(ctx_p, clsyncsock_p, sockcmd_p);
+			rc = control_dump ( ctx_p, clsyncsock_p, sockcmd_p );
 			break;
+
 		case SOCKCMD_REQUEST_INFO:
-			rc = socket_reply(clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_INFO, ctx_p->config_block, ctx_p->label, ctx_p->flags, ctx_p->flags_set);
+			rc = socket_reply ( clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_INFO, ctx_p->config_block, ctx_p->label, ctx_p->flags, ctx_p->flags_set );
 			break;
+
 		case SOCKCMD_REQUEST_SET: {
-			sockcmd_dat_set_t *dat = sockcmd_p->data;
-			rc = ctx_set(ctx_p, dat->key, dat->value);
-			if (rc) {
-				control_error(clsyncsock_p, sockcmd_p, "ctx_set", dat->key);
+				sockcmd_dat_set_t *dat = sockcmd_p->data;
+				rc = ctx_set ( ctx_p, dat->key, dat->value );
+
+				if ( rc ) {
+					control_error ( clsyncsock_p, sockcmd_p, "ctx_set", dat->key );
+					break;
+				}
+
+				rc = socket_reply ( clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_SET );
 				break;
 			}
-			rc = socket_reply(clsyncsock_p, sockcmd_p, SOCKCMD_REPLY_SET);
-			break;
-		}
+
 		case SOCKCMD_REQUEST_DIE:
-			rc = sync_term(SIGTERM);
+			rc = sync_term ( SIGTERM );
 			break;
+
 		default:
 			return EINVAL;
 	}
 
-	debug(3, "rc == %u", rc);
+	debug ( 3, "rc == %u", rc );
 	return rc;
 }
 
-static inline void closecontrol(ctx_t *ctx_p) {
-	if(ctx_p->socket) {
-		close(ctx_p->socket);
+static inline void closecontrol ( ctx_t *ctx_p )
+{
+	if ( ctx_p->socket ) {
+		close ( ctx_p->socket );
 		ctx_p->socket = 0;
 	}
 }
 
-int control_loop(ctx_t *ctx_p) {
-
+int control_loop ( ctx_t *ctx_p )
+{
 	// Starting
-
-	debug(1, "started (ctx_p->socket == %u)", ctx_p->socket);
+	debug ( 1, "started (ctx_p->socket == %u)", ctx_p->socket );
 	int s;
 
-	while((s=ctx_p->socket)) {
-
+	while ( ( s = ctx_p->socket ) ) {
 		// Check if the socket is still alive
-		if(socket_check_bysock(s)) {
-			error("Control socket closed [case 0]");
-			closecontrol(ctx_p);
+		if ( socket_check_bysock ( s ) ) {
+			error ( "Control socket closed [case 0]" );
+			closecontrol ( ctx_p );
 			continue;
 		}
 
 		// Waiting for event
-		debug(3, "waiting for events on the socket");
+		debug ( 3, "waiting for events on the socket" );
 		fd_set rfds;
-
-		FD_ZERO(&rfds);
-		FD_SET(s, &rfds);
-
-		int count = select(s+1, &rfds, NULL, NULL, NULL);
-
+		FD_ZERO ( &rfds );
+		FD_SET ( s, &rfds );
+		int count = select ( s + 1, &rfds, NULL, NULL, NULL );
 		// Processing the events
-		debug(2, "got %i events with select()", count);
+		debug ( 2, "got %i events with select()", count );
 
 		// Processing the events: checks
-		if(count == 0) {
-			debug(2, "select() timed out.");
+		if ( count == 0 ) {
+			debug ( 2, "select() timed out." );
 			continue;
 		}
 
-		if(count < 0) {
-			debug(1, "Got negative events count. Closing the socket.");
-			closecontrol(ctx_p);
+		if ( count < 0 ) {
+			debug ( 1, "Got negative events count. Closing the socket." );
+			closecontrol ( ctx_p );
 			continue;
 		}
 
-		if(!FD_ISSET(s, &rfds)) {
-			error("Got event, but not on the control socket. Closing the socket (cannot use \"select()\").");
-			closecontrol(ctx_p);
+		if ( !FD_ISSET ( s, &rfds ) ) {
+			error ( "Got event, but not on the control socket. Closing the socket (cannot use \"select()\")." );
+			closecontrol ( ctx_p );
 			continue;
 		}
 
 		// Processing the events: accepting new clsyncsock
+		clsyncsock_t *clsyncsock_p = socket_accept ( s );
 
-		clsyncsock_t *clsyncsock_p = socket_accept(s);
-		if(clsyncsock_p == NULL) {
-
-			if(errno == EUSERS)	// Too many connections. Just ignoring the new one.
+		if ( clsyncsock_p == NULL ) {
+			if ( errno == EUSERS )	// Too many connections. Just ignoring the new one.
 				continue;
 
 			// Got unknown error. Closing control socket just in case.
-			error("Cannot socket_accept()");
-			closecontrol(ctx_p);
+			error ( "Cannot socket_accept()" );
+			closecontrol ( ctx_p );
 			continue;
 		}
 
-		debug(2, "Starting new thread for new connection.");
-		socket_sockthreaddata_t *threaddata_p = socket_thread_attach(clsyncsock_p);
+		debug ( 2, "Starting new thread for new connection." );
+		socket_sockthreaddata_t *threaddata_p = socket_thread_attach ( clsyncsock_p );
 
-		if (threaddata_p == NULL) {
-			error("Cannot create a thread for connection");
-			closecontrol(ctx_p);
+		if ( threaddata_p == NULL ) {
+			error ( "Cannot create a thread for connection" );
+			closecontrol ( ctx_p );
 			continue;
 		}
 
@@ -169,83 +171,85 @@ int control_loop(ctx_t *ctx_p) {
 		threaddata_p->authtype		=  ctx_p->flags[SOCKETAUTH];
 		threaddata_p->flags		=  0;
 
-		if (socket_thread_start(threaddata_p)) {
-			error("Cannot start a thread for connection");
-			closecontrol(ctx_p);
+		if ( socket_thread_start ( threaddata_p ) ) {
+			error ( "Cannot start a thread for connection" );
+			closecontrol ( ctx_p );
 			continue;
 		}
+
 #ifdef DEBUG
 		// To prevent too often connections
-		sleep(1);
+		sleep ( 1 );
 #endif
 	}
 
 	// Cleanup
-
-	debug(1, "control_loop() finished");
+	debug ( 1, "control_loop() finished" );
 	return 0;
 }
 
-int control_run(ctx_t *ctx_p) {
-	if(ctx_p->socketpath != NULL) {
+int control_run ( ctx_t *ctx_p )
+{
+	if ( ctx_p->socketpath != NULL ) {
 		int ret =  0;
 		int s   = -1;
 
 		// initializing clsync-socket subsystem
-		if ((ret = socket_init()))
-			error("Cannot init clsync-sockets subsystem.");
+		if ( ( ret = socket_init() ) )
+			error ( "Cannot init clsync-sockets subsystem." );
 
+		if ( !ret ) {
+			clsyncsock_t *clsyncsock = socket_listen_unix ( ctx_p->socketpath );
 
-		if (!ret) {
-			clsyncsock_t *clsyncsock = socket_listen_unix(ctx_p->socketpath);
-			if (clsyncsock == NULL) {
+			if ( clsyncsock == NULL ) {
 				ret = errno;
 			} else {
 				s = clsyncsock->sock;
-				socket_cleanup(clsyncsock);
+				socket_cleanup ( clsyncsock );
 			}
 		}
 
 		// fixing privileges
-		if (!ret) {
-			if(ctx_p->flags[SOCKETMOD])
-				if(chmod(ctx_p->socketpath, ctx_p->socketmod)) {
-					error("Error, Cannot chmod(\"%s\", %o)", 
-						ctx_p->socketpath, ctx_p->socketmod);
+		if ( !ret ) {
+			if ( ctx_p->flags[SOCKETMOD] )
+				if ( chmod ( ctx_p->socketpath, ctx_p->socketmod ) ) {
+					error ( "Error, Cannot chmod(\"%s\", %o)",
+					        ctx_p->socketpath, ctx_p->socketmod );
 					ret = errno;
 				}
-			if(ctx_p->flags[SOCKETOWN])
-				if(chown(ctx_p->socketpath, ctx_p->socketuid, ctx_p->socketgid)) {
-					error("Error, Cannot chown(\"%s\", %u, %u)", 
-						ctx_p->socketpath, ctx_p->socketuid, ctx_p->socketgid);
+
+			if ( ctx_p->flags[SOCKETOWN] )
+				if ( chown ( ctx_p->socketpath, ctx_p->socketuid, ctx_p->socketgid ) ) {
+					error ( "Error, Cannot chown(\"%s\", %u, %u)",
+					        ctx_p->socketpath, ctx_p->socketuid, ctx_p->socketgid );
 					ret = errno;
 				}
 		}
 
 		// finish
-		if (ret) {
-			close(s);
+		if ( ret ) {
+			close ( s );
 			return ret;
 		}
 
 		ctx_p->socket = s;
-
-		debug(2, "ctx_p->socket = %u", ctx_p->socket);
-
-		ret = pthread_create(&pthread_control, NULL, (void *(*)(void *))control_loop, ctx_p);
+		debug ( 2, "ctx_p->socket = %u", ctx_p->socket );
+		ret = pthread_create ( &pthread_control, NULL, ( void * ( * ) ( void * ) ) control_loop, ctx_p );
 	}
-	
+
 	return 0;
 }
 
-int control_cleanup(ctx_t *ctx_p) {
-	if(ctx_p->socketpath != NULL) {
-		unlink(ctx_p->socketpath);
-		closecontrol(ctx_p);
+int control_cleanup ( ctx_t *ctx_p )
+{
+	if ( ctx_p->socketpath != NULL ) {
+		unlink ( ctx_p->socketpath );
+		closecontrol ( ctx_p );
 		// TODO: kill pthread_control and join
 //		pthread_join(pthread_control, NULL);
 		socket_deinit();
 	}
+
 	return 0;
 }
 

+ 6 - 6
control.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -20,8 +20,8 @@
 #ifndef __CLSYNC_CONTROL_H
 #define __CLSYNC_CONTROL_H
 
-extern int control_run(struct ctx *ctx_p);
-extern int control_cleanup(struct ctx *ctx_p);
+extern int control_run ( struct ctx *ctx_p );
+extern int control_cleanup ( struct ctx *ctx_p );
 
 #endif
 

+ 63 - 63
ctx.h

@@ -29,10 +29,10 @@
 #define	MAX_BLOCKTHREADS	(1<<4)
 
 #define register_blockthread(thread) {\
-	critical_on (ctx_p->blockthread_count >= MAX_BLOCKTHREADS);\
-	ctx_p->blockthread[ctx_p->blockthread_count++] = pthread_self();\
-	debug(3, "register_blockthread(): ctx_p->blockthread_count -> %i", ctx_p->blockthread_count);\
-}
+		critical_on (ctx_p->blockthread_count >= MAX_BLOCKTHREADS);\
+		ctx_p->blockthread[ctx_p->blockthread_count++] = pthread_self();\
+		debug(3, "register_blockthread(): ctx_p->blockthread_count -> %i", ctx_p->blockthread_count);\
+	}
 
 #define OPTION_FLAGS		(1<<10)
 #define OPTION_LONGOPTONLY	(1<<9)
@@ -55,11 +55,11 @@ enum flags_enum {
 	THREADING	= 'p',
 	RETRIES		= 'r',
 	OUTPUT_METHOD	= 'Y',
-	EXCLUDEMOUNTPOINTS= 'X',
+	EXCLUDEMOUNTPOINTS = 'X',
 	PIDFILE		= 'z',
 	CLUSTERIFACE	= 'c',
-	CLUSTERMCASTIPADDR='m',
-	CLUSTERMCASTIPPORT='P',
+	CLUSTERMCASTIPADDR = 'm',
+	CLUSTERMCASTIPPORT = 'P',
 	CLUSTERTIMEOUT	= 'G',
 	CLUSTERNODENAME = 'n',
 	CLUSTERHDLMIN	= 'o',
@@ -81,55 +81,55 @@ enum flags_enum {
 	LABEL		= 'l',
 	SHOW_VERSION	= 'V',
 
-	HAVERECURSIVESYNC 	=  0|OPTION_LONGOPTONLY,
-	RSYNCINCLIMIT		=  1|OPTION_LONGOPTONLY,
-	RSYNCPREFERINCLUDE	=  2|OPTION_LONGOPTONLY,
-	SYNCLISTSIMPLIFY	=  3|OPTION_LONGOPTONLY,
-	ONEFILESYSTEM		=  4|OPTION_LONGOPTONLY,
-	STATUSFILE		=  5|OPTION_LONGOPTONLY,
-	SKIPINITSYNC		=  6|OPTION_LONGOPTONLY,
-	ONLYINITSYNC		=  7|OPTION_LONGOPTONLY,
-	EXITONNOEVENTS		=  8|OPTION_LONGOPTONLY,
-	STANDBYFILE		=  9|OPTION_LONGOPTONLY,
-	EXITHOOK		= 10|OPTION_LONGOPTONLY,
-	CLUSTERSDLMAX		= 11|OPTION_LONGOPTONLY,
-	PREEXITHOOK		= 12|OPTION_LONGOPTONLY,
-	SOCKETAUTH		= 13|OPTION_LONGOPTONLY,
-	SOCKETMOD		= 14|OPTION_LONGOPTONLY,
-	SOCKETOWN		= 15|OPTION_LONGOPTONLY,
-	MAXITERATIONS		= 16|OPTION_LONGOPTONLY,
-	IGNOREFAILURES		= 17|OPTION_LONGOPTONLY,
-	DUMPDIR			= 18|OPTION_LONGOPTONLY,
-	CONFIGBLOCKINHERITS	= 19|OPTION_LONGOPTONLY,
-	MONITOR			= 20|OPTION_LONGOPTONLY,
-	SYNCHANDLERARGS0	= 21|OPTION_LONGOPTONLY,
-	SYNCHANDLERARGS1	= 22|OPTION_LONGOPTONLY,
-	CUSTOMSIGNALS		= 23|OPTION_LONGOPTONLY,
-	CHROOT			= 24|OPTION_LONGOPTONLY,
-	MOUNTPOINTS		= 25|OPTION_LONGOPTONLY,
-	SPLITTING		= 26|OPTION_LONGOPTONLY,
-	SYNCHANDLERUID		= 27|OPTION_LONGOPTONLY,
-	SYNCHANDLERGID		= 28|OPTION_LONGOPTONLY,
-	CAPS_INHERIT		= 29|OPTION_LONGOPTONLY,
-	CHECK_EXECVP_ARGS	= 30|OPTION_LONGOPTONLY,
-	PIVOT_ROOT		= 31|OPTION_LONGOPTONLY,
-	DETACH_NETWORK		= 32|OPTION_LONGOPTONLY,
-	DETACH_MISCELLANEA	= 33|OPTION_LONGOPTONLY,
-	ADDPERMITTEDHOOKFILES	= 34|OPTION_LONGOPTONLY,
-	SECCOMP_FILTER		= 35|OPTION_LONGOPTONLY,
-	FORGET_PRIVTHREAD_INFO	= 36|OPTION_LONGOPTONLY,
-	SECURESPLITTING		= 37|OPTION_LONGOPTONLY,
-	FTS_EXPERIMENTAL_OPTIMIZATION = 38|OPTION_LONGOPTONLY,
-	FORBIDDEVICES		= 39|OPTION_LONGOPTONLY,
-	CG_GROUPNAME		= 40|OPTION_LONGOPTONLY,
-	PERMIT_MPROTECT		= 41|OPTION_LONGOPTONLY,
-	SHM_MPROTECT		= 42|OPTION_LONGOPTONLY,
-	MODSIGN			= 43|OPTION_LONGOPTONLY,
-	CANCEL_SYSCALLS		= 44|OPTION_LONGOPTONLY,
-	EXITONSYNCSKIP		= 45|OPTION_LONGOPTONLY,
-	DETACH_IPC		= 46|OPTION_LONGOPTONLY,
-	PRIVILEGEDUID		= 47|OPTION_LONGOPTONLY,
-	PRIVILEGEDGID		= 48|OPTION_LONGOPTONLY,
+	HAVERECURSIVESYNC 	=  0 | OPTION_LONGOPTONLY,
+	RSYNCINCLIMIT		=  1 | OPTION_LONGOPTONLY,
+	RSYNCPREFERINCLUDE	=  2 | OPTION_LONGOPTONLY,
+	SYNCLISTSIMPLIFY	=  3 | OPTION_LONGOPTONLY,
+	ONEFILESYSTEM		=  4 | OPTION_LONGOPTONLY,
+	STATUSFILE		=  5 | OPTION_LONGOPTONLY,
+	SKIPINITSYNC		=  6 | OPTION_LONGOPTONLY,
+	ONLYINITSYNC		=  7 | OPTION_LONGOPTONLY,
+	EXITONNOEVENTS		=  8 | OPTION_LONGOPTONLY,
+	STANDBYFILE		=  9 | OPTION_LONGOPTONLY,
+	EXITHOOK		= 10 | OPTION_LONGOPTONLY,
+	CLUSTERSDLMAX		= 11 | OPTION_LONGOPTONLY,
+	PREEXITHOOK		= 12 | OPTION_LONGOPTONLY,
+	SOCKETAUTH		= 13 | OPTION_LONGOPTONLY,
+	SOCKETMOD		= 14 | OPTION_LONGOPTONLY,
+	SOCKETOWN		= 15 | OPTION_LONGOPTONLY,
+	MAXITERATIONS		= 16 | OPTION_LONGOPTONLY,
+	IGNOREFAILURES		= 17 | OPTION_LONGOPTONLY,
+	DUMPDIR			= 18 | OPTION_LONGOPTONLY,
+	CONFIGBLOCKINHERITS	= 19 | OPTION_LONGOPTONLY,
+	MONITOR			= 20 | OPTION_LONGOPTONLY,
+	SYNCHANDLERARGS0	= 21 | OPTION_LONGOPTONLY,
+	SYNCHANDLERARGS1	= 22 | OPTION_LONGOPTONLY,
+	CUSTOMSIGNALS		= 23 | OPTION_LONGOPTONLY,
+	CHROOT			= 24 | OPTION_LONGOPTONLY,
+	MOUNTPOINTS		= 25 | OPTION_LONGOPTONLY,
+	SPLITTING		= 26 | OPTION_LONGOPTONLY,
+	SYNCHANDLERUID		= 27 | OPTION_LONGOPTONLY,
+	SYNCHANDLERGID		= 28 | OPTION_LONGOPTONLY,
+	CAPS_INHERIT		= 29 | OPTION_LONGOPTONLY,
+	CHECK_EXECVP_ARGS	= 30 | OPTION_LONGOPTONLY,
+	PIVOT_ROOT		= 31 | OPTION_LONGOPTONLY,
+	DETACH_NETWORK		= 32 | OPTION_LONGOPTONLY,
+	DETACH_MISCELLANEA	= 33 | OPTION_LONGOPTONLY,
+	ADDPERMITTEDHOOKFILES	= 34 | OPTION_LONGOPTONLY,
+	SECCOMP_FILTER		= 35 | OPTION_LONGOPTONLY,
+	FORGET_PRIVTHREAD_INFO	= 36 | OPTION_LONGOPTONLY,
+	SECURESPLITTING		= 37 | OPTION_LONGOPTONLY,
+	FTS_EXPERIMENTAL_OPTIMIZATION = 38 | OPTION_LONGOPTONLY,
+	FORBIDDEVICES		= 39 | OPTION_LONGOPTONLY,
+	CG_GROUPNAME		= 40 | OPTION_LONGOPTONLY,
+	PERMIT_MPROTECT		= 41 | OPTION_LONGOPTONLY,
+	SHM_MPROTECT		= 42 | OPTION_LONGOPTONLY,
+	MODSIGN			= 43 | OPTION_LONGOPTONLY,
+	CANCEL_SYSCALLS		= 44 | OPTION_LONGOPTONLY,
+	EXITONSYNCSKIP		= 45 | OPTION_LONGOPTONLY,
+	DETACH_IPC		= 46 | OPTION_LONGOPTONLY,
+	PRIVILEGEDUID		= 47 | OPTION_LONGOPTONLY,
+	PRIVILEGEDGID		= 48 | OPTION_LONGOPTONLY,
 };
 typedef enum flags_enum flags_t;
 
@@ -226,9 +226,9 @@ struct api_functs {
 typedef struct api_functs api_functs_t;
 
 struct notifyenginefuncts {
-	int (*wait)(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
-	int (*handle)(struct ctx *ctx_p, struct indexes *indexes_p);
-	int (*add_watch_dir)(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
+	int ( *wait ) ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p );
+	int ( *handle ) ( struct ctx *ctx_p, struct indexes *indexes_p );
+	int ( *add_watch_dir ) ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
 };
 
 enum shflags {
@@ -323,7 +323,7 @@ struct ctx {
 #ifndef LIBCLSYNC
 	char *config_path;
 	const char *config_block;
-	char *customsignal[MAXSIGNALNUM+1];
+	char *customsignal[MAXSIGNALNUM + 1];
 	char *label;
 	char *watchdir;
 	char *pidfile;
@@ -377,20 +377,20 @@ struct ctx {
 	time_t synctime;
 	unsigned int synctimeout;
 	sigset_t *sigset;
-	char isignoredexitcode[(1<<8)];
+	char isignoredexitcode[ ( 1 << 8 )];
 	pthread_t blockthread[MAX_BLOCKTHREADS];
 	size_t    blockthread_count;
 
 	char *chroot_dir;
 
 #ifdef CAPABILITIES_SUPPORT
-	char *permitted_hookfile[MAXPERMITTEDHOOKFILES+1];
+	char *permitted_hookfile[MAXPERMITTEDHOOKFILES + 1];
 	int   permitted_hookfiles;
 #endif
 
 #ifdef UNSHARE_SUPPORT
 # ifdef GETMNTENT_SUPPORT
-	char *mountpoint[MAXMOUNTPOINTS+1];
+	char *mountpoint[MAXMOUNTPOINTS + 1];
 	int   mountpoints;
 # endif
 #endif

+ 173 - 163
error.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -49,338 +49,348 @@ static int *verbose	 = &three;
 
 pthread_mutex_t *error_mutex_p = NULL;
 
-static int printf_stderr(const char *fmt, ...) {
+static int printf_stderr ( const char *fmt, ... )
+{
 	va_list args;
 	int rc;
-
-	va_start(args, fmt);
-	rc = vfprintf(stderr, fmt, args);
-	va_end(args);
-
+	va_start ( args, fmt );
+	rc = vfprintf ( stderr, fmt, args );
+	va_end ( args );
 	return rc;
 }
 
-static int printf_stdout(const char *fmt, ...) {
+static int printf_stdout ( const char *fmt, ... )
+{
 	va_list args;
 	int rc;
-
-	va_start(args, fmt);
-	rc = vfprintf(stdout, fmt, args);
-	va_end(args);
-
+	va_start ( args, fmt );
+	rc = vfprintf ( stdout, fmt, args );
+	va_end ( args );
 	return rc;
 }
 
-static int vprintf_stderr(const char *fmt, va_list args) {
-	return vfprintf(stderr, fmt, args);
+static int vprintf_stderr ( const char *fmt, va_list args )
+{
+	return vfprintf ( stderr, fmt, args );
 }
 
-static int vprintf_stdout(const char *fmt, va_list args) {
-	return vfprintf(stdout, fmt, args);
+static int vprintf_stdout ( const char *fmt, va_list args )
+{
+	return vfprintf ( stdout, fmt, args );
 }
 
 
-static void flush_stderr(int level) {
-	(void) level;
-
-	fprintf(stderr, "\n");
-	fflush(stderr);
+static void flush_stderr ( int level )
+{
+	( void ) level;
+	fprintf ( stderr, "\n" );
+	fflush ( stderr );
 }
 
-static void flush_stdout(int level) {
-	(void) level;
-
-	fprintf(stdout, "\n");
-	fflush(stdout);
+static void flush_stdout ( int level )
+{
+	( void ) level;
+	fprintf ( stdout, "\n" );
+	fflush ( stdout );
 }
 
 
-static char _syslog_buffer[SYSLOG_BUFSIZ+1] = {0};
+static char _syslog_buffer[SYSLOG_BUFSIZ + 1] = {0};
 size_t      _syslog_buffer_filled = 0;
 
-static int vsyslog_buf(const char *fmt, va_list args) {
+static int vsyslog_buf ( const char *fmt, va_list args )
+{
 	int len;
 	size_t size;
-
 	size = SYSLOG_BUFSIZ - _syslog_buffer_filled;
-
 #ifdef VERYPARANOID
+
 	if (
-		(			 size	> SYSLOG_BUFSIZ)	|| 
-		(_syslog_buffer_filled + size	> SYSLOG_BUFSIZ)	||
-		(_syslog_buffer_filled		> SYSLOG_BUFSIZ)
+	    (			 size	> SYSLOG_BUFSIZ )	||
+	    ( _syslog_buffer_filled + size	> SYSLOG_BUFSIZ )	||
+	    ( _syslog_buffer_filled		> SYSLOG_BUFSIZ )
 	) {
-		fprintf(stderr, "Security problem while vsyslog_buf(): "
-			"_syslog_buffer_filled == %lu; "
-			"size == %lu; "
-			"SYSLOG_BUFSIZ == "XTOSTR(SYSLOG_BUFSIZ)"\n",
-			_syslog_buffer_filled, size);
-		exit(ENOBUFS);
+		fprintf ( stderr, "Security problem while vsyslog_buf(): "
+		          "_syslog_buffer_filled == %lu; "
+		          "size == %lu; "
+		          "SYSLOG_BUFSIZ == "XTOSTR ( SYSLOG_BUFSIZ ) "\n",
+		          _syslog_buffer_filled, size );
+		exit ( ENOBUFS );
 	}
+
 #endif
-	if (!size)
+
+	if ( !size )
 		return 0;
 
 	len = vsnprintf (
-		&_syslog_buffer[_syslog_buffer_filled],
-		size,
-		fmt,
-		args
-	);
+	          &_syslog_buffer[_syslog_buffer_filled],
+	          size,
+	          fmt,
+	          args
+	      );
 
-	if (len>0) {
+	if ( len > 0 ) {
 		_syslog_buffer_filled += len;
-		if (_syslog_buffer_filled > SYSLOG_BUFSIZ)
+
+		if ( _syslog_buffer_filled > SYSLOG_BUFSIZ )
 			_syslog_buffer_filled = SYSLOG_BUFSIZ;
 	}
 
 	return 0;
 }
 
-static int syslog_buf(const char *fmt, ...) {
+static int syslog_buf ( const char *fmt, ... )
+{
 	va_list args;
 	int rc;
-
-	va_start(args, fmt);
-	rc = vsyslog_buf(fmt, args);
-	va_end(args);
-
+	va_start ( args, fmt );
+	rc = vsyslog_buf ( fmt, args );
+	va_end ( args );
 	return rc;
 }
 
-static void syslog_flush(int level) {
-	syslog(level, "%s", _syslog_buffer);
+static void syslog_flush ( int level )
+{
+	syslog ( level, "%s", _syslog_buffer );
 	_syslog_buffer_filled = 0;
 }
 
-typedef int  *(  *outfunct_t)(const char *format, ...);
-typedef int  *( *voutfunct_t)(const char *format, va_list ap);
-typedef void *(*flushfunct_t)(int level);
+typedef int  * (  *outfunct_t ) ( const char *format, ... );
+typedef int  * ( *voutfunct_t ) ( const char *format, va_list ap );
+typedef void * ( *flushfunct_t ) ( int level );
 
 static outfunct_t outfunct[] = {
-	[OM_STDERR]	= (outfunct_t)printf_stderr,
-	[OM_STDOUT]	= (outfunct_t)printf_stdout,
-	[OM_SYSLOG]	= (outfunct_t)syslog_buf,
+	[OM_STDERR]	= ( outfunct_t ) printf_stderr,
+	[OM_STDOUT]	= ( outfunct_t ) printf_stdout,
+	[OM_SYSLOG]	= ( outfunct_t ) syslog_buf,
 };
 
 static voutfunct_t voutfunct[] = {
-	[OM_STDERR]	= (voutfunct_t)vprintf_stderr,
-	[OM_STDOUT]	= (voutfunct_t)vprintf_stdout,
-	[OM_SYSLOG]	= (voutfunct_t)vsyslog_buf,
+	[OM_STDERR]	= ( voutfunct_t ) vprintf_stderr,
+	[OM_STDOUT]	= ( voutfunct_t ) vprintf_stdout,
+	[OM_SYSLOG]	= ( voutfunct_t ) vsyslog_buf,
 };
 
 static flushfunct_t flushfunct[] = {
-	[OM_STDERR]	= (flushfunct_t)flush_stderr,
-	[OM_STDOUT]	= (flushfunct_t)flush_stdout,
-	[OM_SYSLOG]	= (flushfunct_t)syslog_flush,
+	[OM_STDERR]	= ( flushfunct_t ) flush_stderr,
+	[OM_STDOUT]	= ( flushfunct_t ) flush_stdout,
+	[OM_SYSLOG]	= ( flushfunct_t ) syslog_flush,
 };
 
-void _critical(const char *const function_name, const char *fmt, ...) {
-	if (*quiet)
+void _critical ( const char *const function_name, const char *fmt, ... )
+{
+	if ( *quiet )
 		return;
 
 	struct timespec abs_time;
-	clock_gettime(CLOCK_REALTIME , &abs_time);
+	clock_gettime ( CLOCK_REALTIME , &abs_time );
 	abs_time.tv_sec += 1;
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_timedlock(error_mutex_p, &abs_time);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_timedlock ( error_mutex_p, &abs_time );
 
 	outputmethod_t method = *outputmethod;
-
 	{
 		va_list args;
 		pthread_t thread = pthread_self();
 		pid_t pid = getpid();
-
-		outfunct[method]("Critical (pid: %u; thread: %p): %s(): ", pid, thread, function_name);
-		va_start(args, fmt);
-		voutfunct[method](fmt, args);
-		va_end(args);
-		outfunct[method](" (current errno %i: %s)", errno, strerror(errno));
-		flushfunct[method](LOG_CRIT);
+		outfunct[method] ( "Critical (pid: %u; thread: %p): %s(): ", pid, thread, function_name );
+		va_start ( args, fmt );
+		voutfunct[method] ( fmt, args );
+		va_end ( args );
+		outfunct[method] ( " (current errno %i: %s)", errno, strerror ( errno ) );
+		flushfunct[method] ( LOG_CRIT );
 	}
-
 #ifdef BACKTRACE_SUPPORT
 	{
 		void  *buf[BACKTRACE_LENGTH];
 		char **strings;
-		int backtrace_len = backtrace((void **)buf, BACKTRACE_LENGTH);
-
-		strings = backtrace_symbols(buf, backtrace_len);
-		if (strings == NULL) {
-			outfunct[method]("_critical(): Got error, but cannot print the backtrace. Current errno: %u: %s\n",
-				errno, strerror(errno));
-			flushfunct[method](LOG_CRIT);
-			pthread_mutex_unlock(error_mutex_p);
-			exit(EXIT_FAILURE);
+		int backtrace_len = backtrace ( ( void ** ) buf, BACKTRACE_LENGTH );
+		strings = backtrace_symbols ( buf, backtrace_len );
+
+		if ( strings == NULL ) {
+			outfunct[method] ( "_critical(): Got error, but cannot print the backtrace. Current errno: %u: %s\n",
+			                   errno, strerror ( errno ) );
+			flushfunct[method] ( LOG_CRIT );
+			pthread_mutex_unlock ( error_mutex_p );
+			exit ( EXIT_FAILURE );
 		}
 
-		for (int j = 1; j < backtrace_len; j++) {
-			outfunct[method]("        %s", strings[j]);
-			flushfunct[method](LOG_CRIT);
+		for ( int j = 1; j < backtrace_len; j++ ) {
+			outfunct[method] ( "        %s", strings[j] );
+			flushfunct[method] ( LOG_CRIT );
 		}
 	}
 #endif
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_unlock(error_mutex_p);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_unlock ( error_mutex_p );
 
 	error_deinit();
-	exit(errno);
-
+	exit ( errno );
 	return;
 }
 
-void _error(const char *const function_name, const char *fmt, ...) {
+void _error ( const char *const function_name, const char *fmt, ... )
+{
 	va_list args;
 
-	if (*quiet)
+	if ( *quiet )
 		return;
 
-	if (*verbose < 1)
+	if ( *verbose < 1 )
 		return;
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_reltimedlock(error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_reltimedlock ( error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT );
 
 	pthread_t thread = pthread_self();
 	pid_t pid = getpid();
 	outputmethod_t method = *outputmethod;
+	outfunct[method] ( *debug ? "Error (pid: %u; thread: %p): %s(): " : "Error: ", pid, thread, function_name );
+	va_start ( args, fmt );
+	voutfunct[method] ( fmt, args );
+	va_end ( args );
 
-	outfunct[method](*debug ? "Error (pid: %u; thread: %p): %s(): " : "Error: ", pid, thread, function_name);
-	va_start(args, fmt);
-	voutfunct[method](fmt, args);
-	va_end(args);
-	if (errno)
-		outfunct[method](" (%i: %s)", errno, strerror(errno));
-	flushfunct[method](LOG_ERR);
+	if ( errno )
+		outfunct[method] ( " (%i: %s)", errno, strerror ( errno ) );
+
+	flushfunct[method] ( LOG_ERR );
+
+	if ( error_mutex_p != NULL )
+		pthread_mutex_unlock ( error_mutex_p );
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_unlock(error_mutex_p);
 	return;
 }
 
-void _info(const char *const function_name, const char *fmt, ...) {
+void _info ( const char *const function_name, const char *fmt, ... )
+{
 	va_list args;
 
-	if (*quiet)
+	if ( *quiet )
 		return;
 
-	if (*verbose < 3)
+	if ( *verbose < 3 )
 		return;
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_reltimedlock(error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_reltimedlock ( error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT );
 
 	pthread_t thread = pthread_self();
 	pid_t pid = getpid();
 	outputmethod_t method = *outputmethod;
+	outfunct[method] ( *debug ? "Info (pid: %u; thread: %p): %s(): " : "Info: ", pid, thread, function_name );
+	va_start ( args, fmt );
+	voutfunct[method] ( fmt, args );
+	va_end ( args );
+	flushfunct[method] ( LOG_INFO );
 
-	outfunct[method](*debug ? "Info (pid: %u; thread: %p): %s(): " : "Info: ", pid, thread, function_name);
-	va_start(args, fmt);
-	voutfunct[method](fmt, args);
-	va_end(args);
-	flushfunct[method](LOG_INFO);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_unlock ( error_mutex_p );
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_unlock(error_mutex_p);
 	return;
 }
 
-void _warning(const char *const function_name, const char *fmt, ...) {
+void _warning ( const char *const function_name, const char *fmt, ... )
+{
 	va_list args;
 
-	if (*quiet)
+	if ( *quiet )
 		return;
 
-	if (*verbose < 2)
+	if ( *verbose < 2 )
 		return;
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_reltimedlock(error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_reltimedlock ( error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT );
 
 	pthread_t thread = pthread_self();
 	pid_t pid = getpid();
 	outputmethod_t method = *outputmethod;
+	outfunct[method] ( *debug ? "Warning (pid: %u; thread: %p): %s(): " : "Warning: ", pid, thread, function_name );
+	va_start ( args, fmt );
+	voutfunct[method] ( fmt, args );
+	va_end ( args );
+	flushfunct[method] ( LOG_WARNING );
 
-	outfunct[method](*debug ? "Warning (pid: %u; thread: %p): %s(): " : "Warning: ", pid, thread, function_name);
-	va_start(args, fmt);
-	voutfunct[method](fmt, args);
-	va_end(args);
-	flushfunct[method](LOG_WARNING);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_unlock ( error_mutex_p );
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_unlock(error_mutex_p);
 	return;
 }
 
 #ifdef _DEBUG_SUPPORT
-void _debug(int debug_level, const char *const function_name, const char *fmt, ...) {
+void _debug ( int debug_level, const char *const function_name, const char *fmt, ... )
+{
 	va_list args;
 
-	if (*quiet)
+	if ( *quiet )
 		return;
 
-	if (debug_level > *debug)
+	if ( debug_level > *debug )
 		return;
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_reltimedlock(error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_reltimedlock ( error_mutex_p, 0, OUTPUT_LOCK_TIMEOUT );
 
 	pthread_t thread = pthread_self();
 	pid_t pid = getpid();
 	outputmethod_t method = *outputmethod;
+	outfunct[method] ( "Debug%u (pid: %u; thread: %p): %s(): ", debug_level, pid, thread, function_name );
+	va_start ( args, fmt );
+	voutfunct[method] ( fmt, args );
+	va_end ( args );
+	flushfunct[method] ( LOG_DEBUG );
 
-	outfunct[method]("Debug%u (pid: %u; thread: %p): %s(): ", debug_level, pid, thread, function_name);
-	va_start(args, fmt);
-	voutfunct[method](fmt, args);
-	va_end(args);
-	flushfunct[method](LOG_DEBUG);
+	if ( error_mutex_p != NULL )
+		pthread_mutex_unlock ( error_mutex_p );
 
-	if (error_mutex_p != NULL)
-		pthread_mutex_unlock(error_mutex_p);
 	return;
 }
 #endif
 
-void error_init(void *_outputmethod, int *_quiet, int *_verbose, int *_debug) {
+void error_init ( void *_outputmethod, int *_quiet, int *_verbose, int *_debug )
+{
 	outputmethod 	= _outputmethod;
 	quiet		= _quiet;
 	verbose		= _verbose;
 	debug		= _debug;
-
-	openlog(NULL, SYSLOG_FLAGS, SYSLOG_FACILITY);
-
+	openlog ( NULL, SYSLOG_FLAGS, SYSLOG_FACILITY );
 	return;
 }
 
 ipc_type_t ipc_type;
-void error_init_ipc(ipc_type_t _ipc_type) {
+void error_init_ipc ( ipc_type_t _ipc_type )
+{
 	static pthread_mutex_t error_mutex = PTHREAD_MUTEX_INITIALIZER;
 	ipc_type = _ipc_type;
 
-	switch (ipc_type) {
+	switch ( ipc_type ) {
 		case IPCT_SHARED:
-			pthread_mutex_init_shared(&error_mutex_p);
+			pthread_mutex_init_shared ( &error_mutex_p );
 			break;
+
 		case IPCT_PRIVATE:
 			error_mutex_p = &error_mutex;
-			pthread_mutex_init(error_mutex_p, NULL);
+			pthread_mutex_init ( error_mutex_p, NULL );
 			break;
+
 		default:
-			critical ("Unknown ipc_type: %i", ipc_type);
+			critical ( "Unknown ipc_type: %i", ipc_type );
 	}
 
 	return;
 }
 
-void error_deinit() {
-	switch (ipc_type) {
+void error_deinit()
+{
+	switch ( ipc_type ) {
 		case IPCT_SHARED:
-			pthread_mutex_destroy_shared(error_mutex_p);
+			pthread_mutex_destroy_shared ( error_mutex_p );
 			error_mutex_p = NULL;
 			break;
+
 		case IPCT_PRIVATE:
 			break;
 	}

+ 11 - 11
error.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -28,22 +28,22 @@
 #	define DEBUGLEVEL_LIMIT 9
 #endif
 
-extern void _critical( const char *const function_name, const char *fmt, ...);
+extern void _critical ( const char *const function_name, const char *fmt, ... );
 #define critical(...) 				_critical(__FUNCTION__, __VA_ARGS__)
 #define critical_on(cond) {debug(30, "critical_on: checking: %s", TOSTR(cond)); if (unlikely(cond)) {critical("Assert: "TOSTR(cond));}}
 
-extern void _error(const char *const function_name, const char *fmt, ...);
+extern void _error ( const char *const function_name, const char *fmt, ... );
 #define error(...) 				_error(__FUNCTION__, __VA_ARGS__)
 #define error_on(cond)	  {if (unlikely(cond)) {error("Error: ("TOSTR(cond)") != 0");}}
 
-extern void _warning(const char *const function_name, const char *fmt, ...);
+extern void _warning ( const char *const function_name, const char *fmt, ... );
 #define warning(...) 				_warning(__FUNCTION__, __VA_ARGS__)
 
-extern void _info(const char *const function_name, const char *fmt, ...);
+extern void _info ( const char *const function_name, const char *fmt, ... );
 #define info(...) 				_info(__FUNCTION__, __VA_ARGS__)
 
 #ifdef _DEBUG_SUPPORT
-	extern void _debug(int debug_level, const char *const function_name, const char *fmt, ...);
+extern void _debug ( int debug_level, const char *const function_name, const char *fmt, ... );
 #	define debug(debug_level, ...)			{if (debug_level < DEBUGLEVEL_LIMIT) _debug(debug_level, __FUNCTION__, __VA_ARGS__);}
 #	define error_or_debug(debug_level, ...)		((debug_level)<0 ? _error(__FUNCTION__, __VA_ARGS__) : _debug(debug_level, __FUNCTION__, __VA_ARGS__))
 #else
@@ -62,8 +62,8 @@ enum ipc_type {
 };
 typedef enum ipc_type ipc_type_t;
 
-extern void error_init(void *_outputmethod, int *_quiet, int *_verbose, int *_debug);
-extern void error_init_ipc(ipc_type_t ipc_type);
+extern void error_init ( void *_outputmethod, int *_quiet, int *_verbose, int *_debug );
+extern void error_init_ipc ( ipc_type_t ipc_type );
 extern void error_deinit();
 
 enum outputmethod {

+ 93 - 88
fileutils.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -23,93 +23,99 @@
 #include "malloc.h"
 
 
-char *fd2fpath_malloc(int fd) {
+char *fd2fpath_malloc ( int fd )
+{
 #if __linux__
 	stat64_t st64;
 
-	if(fd <= 0) {
-		error("Invalid file descriptor supplied: fd2fpath_malloc(%i).", fd);
+	if ( fd <= 0 ) {
+		error ( "Invalid file descriptor supplied: fd2fpath_malloc(%i).", fd );
 		errno = EINVAL;
 		return NULL;
 	}
 
-	char *fpath = xmalloc((1<<8) + 2);
-	sprintf(fpath, "/proc/self/fd/%i", fd);
+	char *fpath = xmalloc ( ( 1 << 8 ) + 2 );
+	sprintf ( fpath, "/proc/self/fd/%i", fd );
 
-	if(lstat64(fpath, &st64)) {
-		error("Cannot lstat64(\"%s\", st64).", fpath);
+	if ( lstat64 ( fpath, &st64 ) ) {
+		error ( "Cannot lstat64(\"%s\", st64).", fpath );
 		return NULL;
 	}
 
 	ssize_t fpathlen = st64.st_size;
 
-	if(fpathlen > (1<<8))
-		fpath = xrealloc(fpath, fpathlen+2);
+	if ( fpathlen > ( 1 << 8 ) )
+		fpath = xrealloc ( fpath, fpathlen + 2 );
 
-	debug(3, "Getting file path from symlink \"%s\". Path length is: %i.", fpath, fpathlen);
-	if((fpathlen = readlink(fpath, fpath, fpathlen+1)) < 0) {
-		error("Cannot readlink(\"%s\", fpath, bufsize).", fpath);
+	debug ( 3, "Getting file path from symlink \"%s\". Path length is: %i.", fpath, fpathlen );
+
+	if ( ( fpathlen = readlink ( fpath, fpath, fpathlen + 1 ) ) < 0 ) {
+		error ( "Cannot readlink(\"%s\", fpath, bufsize).", fpath );
 		return NULL;
 	}
-	debug(3, "The path is: \"%s\"", fpath);
 
+	debug ( 3, "The path is: \"%s\"", fpath );
 	fpath[fpathlen] = 0;
 	return fpath;
 #else
-	critical("Function fd2fpath_malloc() is not supported in this OS");
+	critical ( "Function fd2fpath_malloc() is not supported in this OS" );
 	return NULL;
 #endif
 }
 
 /**
  * @brief 			Copies file
- * 
+ *
  * @param[in] 	path_from 	Source file path
  * @param[in] 	path_to		Destination file path
  *
  * @retval	zero 		Successfully copied
  * @retval	non-zero 	Got error, while copying
- * 
+ *
  */
 
-int fileutils_copy(const char *path_from, const char *path_to) {
+int fileutils_copy ( const char *path_from, const char *path_to )
+{
 	char buf[BUFSIZ];
 	FILE *from, *to;
+	from = fopen ( path_from, "r" );
 
-	from = fopen(path_from, "r");
-	if(from == NULL) {
-		error("fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for reading", 
-			path_from, path_to, path_from);
+	if ( from == NULL ) {
+		error ( "fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for reading",
+		        path_from, path_to, path_from );
 		return errno;
 	}
 
-	to   = fopen(path_to,   "w");
-	if(to == NULL) {
-		error("fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for writing", 
-			path_from, path_to, path_to);
+	to   = fopen ( path_to,   "w" );
+
+	if ( to == NULL ) {
+		error ( "fileutils_copy(\"%s\", \"%s\"): Cannot open file \"%s\" for writing",
+		        path_from, path_to, path_to );
 		return errno;
 	}
 
-	while(!feof(from)) {
+	while ( !feof ( from ) ) {
 		int err;
 		size_t r, w;
+		r =  fread ( buf, 1, BUFSIZ, from );
 
-		r =  fread(buf, 1, BUFSIZ, from);
-		if((err=ferror(from))) {
-			error("fileutils_copy(\"%s\", \"%s\"): Cannot read from file \"%s\"",
-				path_from, path_to, path_from);
+		if ( ( err = ferror ( from ) ) ) {
+			error ( "fileutils_copy(\"%s\", \"%s\"): Cannot read from file \"%s\"",
+			        path_from, path_to, path_from );
 			return errno;	// CHECK: Is the "errno" should be used in fread() case?
 		}
 
-		w = fwrite(buf, 1, r,      to);
-		if((err=ferror(to))) {
-			error("fileutils_copy(\"%s\", \"%s\"): Cannot write to file \"%s\"",
-				path_from, path_to, path_to);
+		w = fwrite ( buf, 1, r,      to );
+
+		if ( ( err = ferror ( to ) ) ) {
+			error ( "fileutils_copy(\"%s\", \"%s\"): Cannot write to file \"%s\"",
+			        path_from, path_to, path_to );
 			return errno;	// CHECK: is the "errno" should be used in fwrite() case?
 		}
-		if(r != w) {
-			error("fileutils_copy(\"%s\", \"%s\"): Got error while writing to file \"%s\" (%u != %u)",
-				path_from, path_to, path_to, r, w);
+
+		if ( r != w ) {
+			error ( "fileutils_copy(\"%s\", \"%s\"): Got error while writing to file \"%s\" (%u != %u)",
+			        path_from, path_to, path_to, r, w );
 			return errno;	// CHECK: is the "errno" should be used in case "r != w"?
 		}
 	}
@@ -120,38 +126,39 @@ int fileutils_copy(const char *path_from, const char *path_to) {
 
 /**
  * @brief 				Calculates directory level of a canonized path (actually it just counts "/"-s)
- * 
+ *
  * @param[in] 	path 			Canonized path (with realpath())
  *
  * @retval	zero or prositive	Direcory level
  * @retval	negative 		Got error, while calculation. Error-code is placed to errno.
- * 
+ *
  */
 
-short int fileutils_calcdirlevel(const char *path) {
+short int fileutils_calcdirlevel ( const char *path )
+{
 	short int dirlevel = 0;
 	const char *ptr = path;
 
-	if(path == NULL) {
-		error("path is NULL.");
-		errno=EINVAL;
+	if ( path == NULL ) {
+		error ( "path is NULL." );
+		errno = EINVAL;
 		return -1;
 	}
 
-	if(*path == 0) {
-		error("path has zero length.");
-		errno=EINVAL;
+	if ( *path == 0 ) {
+		error ( "path has zero length." );
+		errno = EINVAL;
 		return -2;
 	}
 
-	if(*path != '/') {
-		error("path \"%s\" is not canonized.", path);
-		errno=EINVAL;
+	if ( *path != '/' ) {
+		error ( "path \"%s\" is not canonized.", path );
+		errno = EINVAL;
 		return -3;
 	}
 
-	while(*(ptr++))
-		if(*ptr == '/')
+	while ( * ( ptr++ ) )
+		if ( *ptr == '/' )
 			dirlevel++;
 
 	return dirlevel;
@@ -159,25 +166,27 @@ short int fileutils_calcdirlevel(const char *path) {
 
 /**
  * @brief 			Combination of mkdirat() and openat()
- * 
+ *
  * @param[in]	dir_path	Path to directory to create and open
  @ @param[in]	dirfd_parent	File descriptor of directory for relative paths
  @ @param[in]	dir_mode	Modes for newly created directory (e.g. 750)
- * 
+ *
  * @retval	dirfd		File descriptor to newly created directory
  * @retval	NULL		On error
- * 
+ *
  */
-int mkdirat_open(const char *const dir_path, int dirfd_parent, mode_t dir_mode) {
+int mkdirat_open ( const char *const dir_path, int dirfd_parent, mode_t dir_mode )
+{
 	int dirfd;
+	debug ( 5, "mkdirat(%u, \"%s\", %o)", dirfd_parent, dir_path, dir_mode );
 
-	debug(5, "mkdirat(%u, \"%s\", %o)", dirfd_parent, dir_path, dir_mode);
-	if (mkdirat(dirfd_parent, dir_path, dir_mode))
+	if ( mkdirat ( dirfd_parent, dir_path, dir_mode ) )
 		return -1;
 
-	debug(5, "openat(%u, \"%s\", %x)", dirfd_parent, dir_path, O_RDWR|O_DIRECTORY|O_PATH);
-	dirfd = openat(dirfd_parent, dir_path, O_RDWR|O_DIRECTORY|O_PATH);
-	if (dirfd == -1)
+	debug ( 5, "openat(%u, \"%s\", %x)", dirfd_parent, dir_path, O_RDWR | O_DIRECTORY | O_PATH );
+	dirfd = openat ( dirfd_parent, dir_path, O_RDWR | O_DIRECTORY | O_PATH );
+
+	if ( dirfd == -1 )
 		return -1;
 
 	return dirfd;
@@ -185,13 +194,13 @@ int mkdirat_open(const char *const dir_path, int dirfd_parent, mode_t dir_mode)
 
 /**
  * @brief 			Opens a directory with open()
- * 
+ *
  * @param[out]	fd_p		Pointer to the result file descriptor
  @ @param[in]	dir_path	Path to the directory
- * 
+ *
  * @retval	*fd_p		On success
  * @retval	-1		On error
- * 
+ *
  * /
 int open_dir(int *fd_p, const char *const dir_path) {
 	int fd = open(dir_path, O_RDONLY|O_DIRECTORY|O_PATH);
@@ -206,35 +215,31 @@ int open_dir(int *fd_p, const char *const dir_path) {
 */
 
 
-uint32_t stat_diff(stat64_t *a, stat64_t *b) {
+uint32_t stat_diff ( stat64_t *a, stat64_t *b )
+{
 	uint32_t difference;
 #ifdef PARANOID
-	critical_on (a == NULL);
-	critical_on (b == NULL);
+	critical_on ( a == NULL );
+	critical_on ( b == NULL );
 #endif
-
 	difference = 0x0000;
-
 #define STAT_COMPARE(bit, field)	\
 	if (a->field != b->field)	\
 		difference |= bit;
-
-	STAT_COMPARE(STAT_FIELD_DEV,	st_dev);
-	STAT_COMPARE(STAT_FIELD_INO,	st_ino);
-	STAT_COMPARE(STAT_FIELD_MODE,	st_mode);
-	STAT_COMPARE(STAT_FIELD_NLINK,	st_nlink);
-	STAT_COMPARE(STAT_FIELD_UID,	st_uid);
-	STAT_COMPARE(STAT_FIELD_GID,	st_gid);
-	STAT_COMPARE(STAT_FIELD_RDEV,	st_rdev);
-	STAT_COMPARE(STAT_FIELD_SIZE,	st_size);
-	STAT_COMPARE(STAT_FIELD_BLKSIZE,st_blksize);
-	STAT_COMPARE(STAT_FIELD_BLOCKS,	st_blocks);
-	STAT_COMPARE(STAT_FIELD_ATIME,	st_atime);
-	STAT_COMPARE(STAT_FIELD_MTIME,	st_mtime);
-	STAT_COMPARE(STAT_FIELD_CTIME,	st_ctime);
-
+	STAT_COMPARE ( STAT_FIELD_DEV,	st_dev );
+	STAT_COMPARE ( STAT_FIELD_INO,	st_ino );
+	STAT_COMPARE ( STAT_FIELD_MODE,	st_mode );
+	STAT_COMPARE ( STAT_FIELD_NLINK,	st_nlink );
+	STAT_COMPARE ( STAT_FIELD_UID,	st_uid );
+	STAT_COMPARE ( STAT_FIELD_GID,	st_gid );
+	STAT_COMPARE ( STAT_FIELD_RDEV,	st_rdev );
+	STAT_COMPARE ( STAT_FIELD_SIZE,	st_size );
+	STAT_COMPARE ( STAT_FIELD_BLKSIZE, st_blksize );
+	STAT_COMPARE ( STAT_FIELD_BLOCKS,	st_blocks );
+	STAT_COMPARE ( STAT_FIELD_ATIME,	st_atime );
+	STAT_COMPARE ( STAT_FIELD_MTIME,	st_mtime );
+	STAT_COMPARE ( STAT_FIELD_CTIME,	st_ctime );
 #undef STAT_COMPARE
-
 	return difference;
 }
 

+ 9 - 9
fileutils.h

@@ -1,26 +1,26 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern char *fd2fpath_malloc(int fd);
+extern char *fd2fpath_malloc ( int fd );
 
-extern int fileutils_copy(const char *path_from, const char *path_to);
-extern short int fileutils_calcdirlevel(const char *path);
-extern int mkdirat_open(const char *const dir_path, int dirfd_parent, mode_t dir_mode);
-extern uint32_t stat_diff(stat64_t *a, stat64_t *b);
+extern int fileutils_copy ( const char *path_from, const char *path_to );
+extern short int fileutils_calcdirlevel ( const char *path );
+extern int mkdirat_open ( const char *const dir_path, int dirfd_parent, mode_t dir_mode );
+extern uint32_t stat_diff ( stat64_t *a, stat64_t *b );
 

+ 23 - 23
gencompilerflags.c

@@ -1,65 +1,65 @@
 #include <stdio.h>
 
-int main() {
-	printf("%s",
-
+int main()
+{
+	printf ( "%s",
 #ifdef _DEBUG_SUPPORT
-		"#define _DEBUG_SUPPORT\n"
+	         "#define _DEBUG_SUPPORT\n"
 #endif
 #ifdef _DEBUG_FORCE
-		"#define _DEBUG_FORCE\n"
+	         "#define _DEBUG_FORCE\n"
 #endif
 #ifdef KQUEUE_SUPPORT
-		"#define KQUEUE_SUPPORT\n"
+	         "#define KQUEUE_SUPPORT\n"
 #endif
 #ifdef INOTIFY_SUPPORT
-		"#define INOTIFY_SUPPORT\n"
+	         "#define INOTIFY_SUPPORT\n"
 #endif
 #ifdef INOTIFY_OLD
-		"#define INOTIFY_OLD\n"
+	         "#define INOTIFY_OLD\n"
 #endif
 #ifdef FANOTIFY_SUPPORT
-		"#define FANOTIFY_SUPPORT\n"
+	         "#define FANOTIFY_SUPPORT\n"
 #endif
 #ifdef BSM_SUPPORT
-		"#define BSM_SUPPORT\n"
+	         "#define BSM_SUPPORT\n"
 #endif
 #ifdef GIO_SUPPORT
-		"#define GIO_SUPPORT\n"
+	         "#define GIO_SUPPORT\n"
 #endif
 #ifdef DTRACEPIPE_SUPPORT
-		"#define DTRACEPIPE_SUPPORT\n"
+	         "#define DTRACEPIPE_SUPPORT\n"
 #endif
 #ifdef BACKTRACE_SUPPORT
-		"#define BACKTRACE_SUPPORT\n"
+	         "#define BACKTRACE_SUPPORT\n"
 #endif
 #ifdef CAPABILITIES_SUPPORT
-		"#define CAPABILITIES_SUPPORT\n"
+	         "#define CAPABILITIES_SUPPORT\n"
 #endif
 #ifdef SECCOMP_SUPPORT
-		"#define SECCOMP_SUPPORT\n"
+	         "#define SECCOMP_SUPPORT\n"
 #endif
 #ifdef GETMNTENT_SUPPORT
-		"#define GETMNTENT_SUPPORT\n"
+	         "#define GETMNTENT_SUPPORT\n"
 #endif
 #ifdef UNSHARE_SUPPORT
-		"#define UNSHARE_SUPPORT\n"
+	         "#define UNSHARE_SUPPORT\n"
 #endif
 #ifdef PIVOTROOT_OPT_SUPPORT
-		"#define PIVOTROOT_OPT_SUPPORT\n"
+	         "#define PIVOTROOT_OPT_SUPPORT\n"
 #endif
 #ifdef CGROUP_SUPPORT
-		"#define CGROUP_SUPPORT\n"
+	         "#define CGROUP_SUPPORT\n"
 #endif
 #ifdef TRE_SUPPORT
-		"#define TRE_SUPPORT\n"
+	         "#define TRE_SUPPORT\n"
 #endif
 #ifdef THREADING_SUPPORT
-		"#define THREADING_SUPPORT\n"
+	         "#define THREADING_SUPPORT\n"
 #endif
 #ifdef HL_LOCKS
-		"#define HL_LOCKS\n"
+	         "#define HL_LOCKS\n"
 #endif
-	);
+	       );
 	return 0;
 }

+ 24 - 30
glibex.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -30,49 +30,43 @@ __extension__ struct keyvalue_copy_arg {
 	GDupFunc v_dup_funct;
 };
 
-void g_hash_table_dup_item(gpointer k, gpointer v, gpointer arg_gp) {
-	GHashTable *ht_dst	= ((struct keyvalue_copy_arg *)arg_gp)->ht_dst;
-	GDupFunc k_dup_funct	= ((struct keyvalue_copy_arg *)arg_gp)->k_dup_funct;
-	GDupFunc v_dup_funct	= ((struct keyvalue_copy_arg *)arg_gp)->v_dup_funct;
-
-	g_hash_table_insert(ht_dst, k_dup_funct==NULL?NULL:k_dup_funct(k), v_dup_funct==NULL?NULL:v_dup_funct(v));
-
+void g_hash_table_dup_item ( gpointer k, gpointer v, gpointer arg_gp )
+{
+	GHashTable *ht_dst	= ( ( struct keyvalue_copy_arg * ) arg_gp )->ht_dst;
+	GDupFunc k_dup_funct	= ( ( struct keyvalue_copy_arg * ) arg_gp )->k_dup_funct;
+	GDupFunc v_dup_funct	= ( ( struct keyvalue_copy_arg * ) arg_gp )->v_dup_funct;
+	g_hash_table_insert ( ht_dst, k_dup_funct == NULL ? NULL : k_dup_funct ( k ), v_dup_funct == NULL ? NULL : v_dup_funct ( v ) );
 	return;
 }
 
-GHashTable *g_hash_table_dup(GHashTable *src, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct) {
-	GHashTable *dst = g_hash_table_new_full(hash_funct, key_equal_funct, key_destroy_funct, value_destroy_funct);
-
+GHashTable *g_hash_table_dup ( GHashTable *src, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct )
+{
+	GHashTable *dst = g_hash_table_new_full ( hash_funct, key_equal_funct, key_destroy_funct, value_destroy_funct );
 	struct keyvalue_copy_arg arg;
 	arg.ht_dst = dst;
 	arg.k_dup_funct =   key_dup_funct;
 	arg.v_dup_funct = value_dup_funct;
-
-	g_hash_table_foreach(src, g_hash_table_dup_item, &arg);
-
+	g_hash_table_foreach ( src, g_hash_table_dup_item, &arg );
 	return dst;
 }
 
-gboolean g_tree_dup_item(gpointer k, gpointer v, gpointer arg_gp) {
-	GTree *bt_dst		= ((struct keyvalue_copy_arg *)arg_gp)->bt_dst;
-	GDupFunc k_dup_funct	= ((struct keyvalue_copy_arg *)arg_gp)->k_dup_funct;
-	GDupFunc v_dup_funct	= ((struct keyvalue_copy_arg *)arg_gp)->v_dup_funct;
-
-	g_tree_replace(bt_dst, k_dup_funct==NULL?NULL:k_dup_funct(k), v_dup_funct==NULL?NULL:v_dup_funct(v));
-
+gboolean g_tree_dup_item ( gpointer k, gpointer v, gpointer arg_gp )
+{
+	GTree *bt_dst		= ( ( struct keyvalue_copy_arg * ) arg_gp )->bt_dst;
+	GDupFunc k_dup_funct	= ( ( struct keyvalue_copy_arg * ) arg_gp )->k_dup_funct;
+	GDupFunc v_dup_funct	= ( ( struct keyvalue_copy_arg * ) arg_gp )->v_dup_funct;
+	g_tree_replace ( bt_dst, k_dup_funct == NULL ? NULL : k_dup_funct ( k ), v_dup_funct == NULL ? NULL : v_dup_funct ( v ) );
 	return FALSE;
 }
 
-GTree *g_tree_dup(GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct) {
-	GTree *dst = g_tree_new_full(key_compare_func, key_compare_data, key_destroy_func, value_destroy_func);
-
+GTree *g_tree_dup ( GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct )
+{
+	GTree *dst = g_tree_new_full ( key_compare_func, key_compare_data, key_destroy_func, value_destroy_func );
 	struct keyvalue_copy_arg arg;
 	arg.bt_dst = dst;
 	arg.k_dup_funct =   key_dup_funct;
 	arg.v_dup_funct = value_dup_funct;
-
-	g_tree_foreach(src, g_tree_dup_item, &arg);
-
+	g_tree_foreach ( src, g_tree_dup_item, &arg );
 	return dst;
 }
 

+ 7 - 7
glibex.h

@@ -1,26 +1,26 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
 #include <glib.h>
 
-typedef gpointer(*GDupFunc)(gpointer data);
+typedef gpointer ( *GDupFunc ) ( gpointer data );
 
-extern GHashTable *g_hash_table_dup(GHashTable *ht, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct);
-extern GTree *g_tree_dup(GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct);
+extern GHashTable *g_hash_table_dup ( GHashTable *ht, GHashFunc hash_funct, GEqualFunc key_equal_funct, GDestroyNotify key_destroy_funct, GDestroyNotify value_destroy_funct, GDupFunc key_dup_funct, GDupFunc value_dup_funct );
+extern GTree *g_tree_dup ( GTree *src, GCompareDataFunc key_compare_func, gpointer key_compare_data, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, GDupFunc key_dup_funct, GDupFunc value_dup_funct );
 

+ 4 - 4
indexes.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */

+ 78 - 76
indexes.h

@@ -50,142 +50,144 @@ typedef struct indexes indexes_t;
 // Removes necessary rows from hash_tables if some watching descriptor closed
 // Return: 0 on success, non-zero on fail
 
-static inline int indexes_remove_bywd(indexes_t *indexes_p, int wd) {
-	int ret=0;
-
-	char *fpath = g_hash_table_lookup(indexes_p->wd2fpath_ht, GINT_TO_POINTER(wd));
-
-	ret |= g_hash_table_remove(indexes_p->wd2fpath_ht, GINT_TO_POINTER(wd));
-	if(fpath == NULL) {
-		error("Cannot remove from index \"fpath2wd\" by wd %i.", wd);
+static inline int indexes_remove_bywd ( indexes_t *indexes_p, int wd )
+{
+	int ret = 0;
+	char *fpath = g_hash_table_lookup ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
+	ret |= g_hash_table_remove ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
+
+	if ( fpath == NULL ) {
+		error ( "Cannot remove from index \"fpath2wd\" by wd %i.", wd );
 		return -1;
 	}
-	ret |= g_hash_table_remove(indexes_p->fpath2wd_ht, fpath);
 
+	ret |= g_hash_table_remove ( indexes_p->fpath2wd_ht, fpath );
 	return ret;
 }
 
 // Lookups file path by watching descriptor from hash_tables
 // Return: file path on success, NULL on fail
 
-static inline char *indexes_wd2fpath(indexes_t *indexes_p, int wd) {
-	return g_hash_table_lookup(indexes_p->wd2fpath_ht, GINT_TO_POINTER(wd));
+static inline char *indexes_wd2fpath ( indexes_t *indexes_p, int wd )
+{
+	return g_hash_table_lookup ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ) );
 }
 
 // Lookups watching descriptor by file path from hash_tables
 // Return: watching descriptor on success, -1 on fail
 
-static inline int indexes_fpath2wd(indexes_t *indexes_p, const char *fpath) {
-	gpointer gint_p = g_hash_table_lookup(indexes_p->fpath2wd_ht, fpath);
-	if(gint_p == NULL)
+static inline int indexes_fpath2wd ( indexes_t *indexes_p, const char *fpath )
+{
+	gpointer gint_p = g_hash_table_lookup ( indexes_p->fpath2wd_ht, fpath );
+
+	if ( gint_p == NULL )
 		return -1;
 
-	return GPOINTER_TO_INT(gint_p);
+	return GPOINTER_TO_INT ( gint_p );
 }
 
 // Adds necessary rows to hash_tables if some watching descriptor opened
 // Return: 0 on success, non-zero on fail
 
-static inline int indexes_add_wd(indexes_t *indexes_p, int wd, const char *fpath_const, size_t fpathlen) {
-	debug(4, "indexes_add_wd(indexes_p, %i, \"%s\", %i)", wd, fpath_const, fpathlen);
-
-	char *fpath = xmalloc(fpathlen+1);
-	memcpy(fpath, fpath_const, fpathlen+1);
-
-	g_hash_table_insert(indexes_p->wd2fpath_ht, GINT_TO_POINTER(wd), fpath);
-	g_hash_table_insert(indexes_p->fpath2wd_ht, fpath, GINT_TO_POINTER(wd));
-
+static inline int indexes_add_wd ( indexes_t *indexes_p, int wd, const char *fpath_const, size_t fpathlen )
+{
+	debug ( 4, "indexes_add_wd(indexes_p, %i, \"%s\", %i)", wd, fpath_const, fpathlen );
+	char *fpath = xmalloc ( fpathlen + 1 );
+	memcpy ( fpath, fpath_const, fpathlen + 1 );
+	g_hash_table_insert ( indexes_p->wd2fpath_ht, GINT_TO_POINTER ( wd ), fpath );
+	g_hash_table_insert ( indexes_p->fpath2wd_ht, fpath, GINT_TO_POINTER ( wd ) );
 	return 0;
 }
 
-static inline eventinfo_t *indexes_fpath2ei(indexes_t *indexes_p, const char *fpath) {
-	return (eventinfo_t *)g_hash_table_lookup(indexes_p->fpath2ei_ht, fpath);
+static inline eventinfo_t *indexes_fpath2ei ( indexes_t *indexes_p, const char *fpath )
+{
+	return ( eventinfo_t * ) g_hash_table_lookup ( indexes_p->fpath2ei_ht, fpath );
 }
 
-static inline int indexes_fpath2ei_add(indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo) {
-	debug(5, "\"%s\"", fpath);
-	g_hash_table_replace(indexes_p->fpath2ei_ht, fpath, evinfo);
-
+static inline int indexes_fpath2ei_add ( indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo )
+{
+	debug ( 5, "\"%s\"", fpath );
+	g_hash_table_replace ( indexes_p->fpath2ei_ht, fpath, evinfo );
 	return 0;
 }
 
-static inline int indexes_queueevent(indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo, queue_id_t queue_id) {
-
-	g_hash_table_replace(indexes_p->fpath2ei_coll_ht[queue_id], fpath, evinfo);
-
-	debug(3, "indexes_queueevent(indexes_p, \"%s\", evinfo, %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size(indexes_p->fpath2ei_coll_ht[queue_id]), queue_id);
+static inline int indexes_queueevent ( indexes_t *indexes_p, char *fpath, eventinfo_t *evinfo, queue_id_t queue_id )
+{
+	g_hash_table_replace ( indexes_p->fpath2ei_coll_ht[queue_id], fpath, evinfo );
+	debug ( 3, "indexes_queueevent(indexes_p, \"%s\", evinfo, %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] ), queue_id );
 	return 0;
 }
 
-static inline eventinfo_t *indexes_lookupinqueue(indexes_t *indexes_p, const char *fpath, queue_id_t queue_id) {
-	return (eventinfo_t *)g_hash_table_lookup(indexes_p->fpath2ei_coll_ht[queue_id], fpath);
+static inline eventinfo_t *indexes_lookupinqueue ( indexes_t *indexes_p, const char *fpath, queue_id_t queue_id )
+{
+	return ( eventinfo_t * ) g_hash_table_lookup ( indexes_p->fpath2ei_coll_ht[queue_id], fpath );
 }
 
-static inline int indexes_queuelen(indexes_t *indexes_p, queue_id_t queue_id) {
-	return g_hash_table_size(indexes_p->fpath2ei_coll_ht[queue_id]);
+static inline int indexes_queuelen ( indexes_t *indexes_p, queue_id_t queue_id )
+{
+	return g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] );
 }
 
-static inline int indexes_removefromqueue(indexes_t *indexes_p, char *fpath, queue_id_t queue_id) {
+static inline int indexes_removefromqueue ( indexes_t *indexes_p, char *fpath, queue_id_t queue_id )
+{
 //	debug(3, "indexes_removefromqueue(indexes_p, \"%s\", %i).", fpath, queue_id);
-
-	g_hash_table_remove(indexes_p->fpath2ei_coll_ht[queue_id], fpath);
-
-	debug(3, "indexes_removefromqueue(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size(indexes_p->fpath2ei_coll_ht[queue_id]), queue_id);
+	g_hash_table_remove ( indexes_p->fpath2ei_coll_ht[queue_id], fpath );
+	debug ( 3, "indexes_removefromqueue(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->fpath2ei_coll_ht[queue_id] ), queue_id );
 	return 0;
 }
 
-static inline int indexes_addexclude(indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags, queue_id_t queue_id) {
-	g_hash_table_replace(indexes_p->exc_fpath_coll_ht[queue_id], fpath, GINT_TO_POINTER(flags));
-
-	debug(3, "indexes_addexclude(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size(indexes_p->exc_fpath_coll_ht[queue_id]), queue_id);
+static inline int indexes_addexclude ( indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags, queue_id_t queue_id )
+{
+	g_hash_table_replace ( indexes_p->exc_fpath_coll_ht[queue_id], fpath, GINT_TO_POINTER ( flags ) );
+	debug ( 3, "indexes_addexclude(indexes_p, \"%s\", %i). It's now %i events collected in queue %i.", fpath, queue_id, g_hash_table_size ( indexes_p->exc_fpath_coll_ht[queue_id] ), queue_id );
 	return 0;
 }
 
-static inline int indexes_addexclude_aggr(indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags) {
-	debug(3, "indexes_addexclude_aggr(indexes_p, \"%s\", %u).", fpath, flags);
+static inline int indexes_addexclude_aggr ( indexes_t *indexes_p, char *fpath, eventinfo_flags_t flags )
+{
+	debug ( 3, "indexes_addexclude_aggr(indexes_p, \"%s\", %u).", fpath, flags );
+	gpointer flags_gp = g_hash_table_lookup ( indexes_p->exc_fpath_ht, fpath );
 
-	gpointer flags_gp = g_hash_table_lookup(indexes_p->exc_fpath_ht, fpath);
-	if(flags_gp != NULL)
-		flags |= GPOINTER_TO_INT(flags_gp);
+	if ( flags_gp != NULL )
+		flags |= GPOINTER_TO_INT ( flags_gp );
 
 	// Removing extra flags
-	if((flags&(EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY)) == (EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY))
+	if ( ( flags & ( EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY ) ) == ( EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY ) )
 		flags &= ~EVIF_CONTENTRECURSIVELY;
 
-	g_hash_table_replace(indexes_p->exc_fpath_ht, fpath, GINT_TO_POINTER(flags));
-
-	debug(3, "indexes_addexclude_aggr(indexes_p, \"%s\", flags): %u.", fpath, flags);
+	g_hash_table_replace ( indexes_p->exc_fpath_ht, fpath, GINT_TO_POINTER ( flags ) );
+	debug ( 3, "indexes_addexclude_aggr(indexes_p, \"%s\", flags): %u.", fpath, flags );
 	return 0;
 }
 
-static inline int indexes_outaggr_add(indexes_t *indexes_p, char *outline, eventinfo_flags_t flags) {
-	gpointer flags_gp = g_hash_table_lookup(indexes_p->out_lines_aggr_ht, outline);
-	if(flags_gp != NULL)
-		flags |= GPOINTER_TO_INT(flags_gp);
+static inline int indexes_outaggr_add ( indexes_t *indexes_p, char *outline, eventinfo_flags_t flags )
+{
+	gpointer flags_gp = g_hash_table_lookup ( indexes_p->out_lines_aggr_ht, outline );
+
+	if ( flags_gp != NULL )
+		flags |= GPOINTER_TO_INT ( flags_gp );
 
 	// Removing extra flags
-	if((flags&(EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY)) == (EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY))
+	if ( ( flags & ( EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY ) ) == ( EVIF_RECURSIVELY | EVIF_CONTENTRECURSIVELY ) )
 		flags &= ~EVIF_CONTENTRECURSIVELY;
 
-	g_hash_table_replace(indexes_p->out_lines_aggr_ht, outline, GINT_TO_POINTER(flags));
-
-	debug(3, "indexes_outaggr_aggr(indexes_p, \"%s\").", outline);
+	g_hash_table_replace ( indexes_p->out_lines_aggr_ht, outline, GINT_TO_POINTER ( flags ) );
+	debug ( 3, "indexes_outaggr_aggr(indexes_p, \"%s\").", outline );
 	return 0;
 }
 
-static inline fileinfo_t *indexes_fileinfo(indexes_t *indexes_p, const char *fpath) {
-	return (fileinfo_t *)g_hash_table_lookup(indexes_p->fileinfo_ht, fpath);
+static inline fileinfo_t *indexes_fileinfo ( indexes_t *indexes_p, const char *fpath )
+{
+	return ( fileinfo_t * ) g_hash_table_lookup ( indexes_p->fileinfo_ht, fpath );
 }
 
-static inline int indexes_fileinfo_add(indexes_t *indexes_p, const char *fpath_const, fileinfo_t *fi) {
-	size_t fpathlen = strlen(fpath_const);
-	debug(4, "indexes_add_wd(indexes_p, \"%s\", %p)", fpath_const, fpathlen);
-
-	char *fpath = xmalloc(fpathlen+1);
-	memcpy(fpath, fpath_const, fpathlen+1);
-
-	g_hash_table_insert(indexes_p->fileinfo_ht, fpath, fi);
-
+static inline int indexes_fileinfo_add ( indexes_t *indexes_p, const char *fpath_const, fileinfo_t *fi )
+{
+	size_t fpathlen = strlen ( fpath_const );
+	debug ( 4, "indexes_add_wd(indexes_p, \"%s\", %p)", fpath_const, fpathlen );
+	char *fpath = xmalloc ( fpathlen + 1 );
+	memcpy ( fpath, fpath_const, fpathlen + 1 );
+	g_hash_table_insert ( indexes_p->fileinfo_ht, fpath, fi );
 	return 0;
 }
 

+ 47 - 42
libclsync.c

@@ -1,18 +1,18 @@
 /*
     libclsyncmgr - clsync control socket API
-    
+
     Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -29,29 +29,32 @@
 #include "malloc.h"
 #include "socket.h"
 
-int libproc_procclsyncsock(socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p) {
+int libproc_procclsyncsock ( socket_sockthreaddata_t *arg, sockcmd_t *sockcmd_p )
+{
 	clsyncproc_t		*proc_p     = arg->arg;
 	clsyncsock_t		*clsyncsock_p = proc_p->sock_p;
 	clsyncsock_procfunct_t   procfunct    = proc_p->procfunct;
-
 #ifdef PARANOID
-	if (procfunct == NULL) {
-		error("procfunct == NULL");
+
+	if ( procfunct == NULL ) {
+		error ( "procfunct == NULL" );
 		return 0;
 	}
+
 #endif
 
-	if(procfunct(arg, sockcmd_p))
-		switch(sockcmd_p->cmd_id) {
+	if ( procfunct ( arg, sockcmd_p ) )
+		switch ( sockcmd_p->cmd_id ) {
 			default:
-				socket_sendinvalid(clsyncsock_p, sockcmd_p);
+				socket_sendinvalid ( clsyncsock_p, sockcmd_p );
 				break;
 		}
 
 	return 0;
 }
 
-static inline int _clsync_connect_setthreaddata(socket_sockthreaddata_t *threaddata_p, clsyncproc_t *proc_p, sockprocflags_t flags) {
+static inline int _clsync_connect_setthreaddata ( socket_sockthreaddata_t *threaddata_p, clsyncproc_t *proc_p, sockprocflags_t flags )
+{
 	threaddata_p->procfunct		=  libproc_procclsyncsock;
 	threaddata_p->clsyncsock_p	=  proc_p->sock_p;
 	threaddata_p->arg		=  proc_p;
@@ -59,60 +62,62 @@ static inline int _clsync_connect_setthreaddata(socket_sockthreaddata_t *threadd
 	threaddata_p->authtype		=  SOCKAUTH_NULL;
 	threaddata_p->flags		=  flags;
 	threaddata_p->freefunct_arg	=  free;
-
 	return 0;
 }
 
-static inline clsyncproc_t *_clsync_x_unix(
-	const char *const socket_path, 
-	clsyncsock_procfunct_t procfunct, 
-	sockprocflags_t flags, 
-	const char *const action, 
-	clsyncsock_t *(*socket_x_unix)(const char *const)
-) {
-	if (procfunct == NULL) {
+static inline clsyncproc_t *_clsync_x_unix (
+    const char *const socket_path,
+    clsyncsock_procfunct_t procfunct,
+    sockprocflags_t flags,
+    const char *const action,
+    clsyncsock_t * ( *socket_x_unix ) ( const char *const )
+)
+{
+	if ( procfunct == NULL ) {
 		errno = EINVAL;
 		return NULL;
 	}
 
-	clsyncproc_t *proc_p = xmalloc(sizeof(*proc_p));
-	memset(proc_p, 0, sizeof(*proc_p));
+	clsyncproc_t *proc_p = xmalloc ( sizeof ( *proc_p ) );
+	memset ( proc_p, 0, sizeof ( *proc_p ) );
+	proc_p->sock_p = socket_x_unix ( socket_path );
+
+	if ( proc_p->sock_p == NULL ) {
+		free ( proc_p );
 
-	proc_p->sock_p = socket_x_unix(socket_path);
-	if(proc_p->sock_p == NULL) {
-		free(proc_p);
-		if(errno == EUSERS) {
-			error("clsync_%s_unix(): Too many connections.", action);
+		if ( errno == EUSERS ) {
+			error ( "clsync_%s_unix(): Too many connections.", action );
 			return NULL;
 		}
 
-		error("clsync_%s_unix(): Cannot socket_%s_unix()",
-			action, action);
+		error ( "clsync_%s_unix(): Cannot socket_%s_unix()",
+		        action, action );
 		return NULL;
 	}
 
-	socket_sockthreaddata_t *threaddata_p = socket_thread_attach(proc_p->sock_p);
-	if (threaddata_p == NULL) {
-		socket_close(proc_p->sock_p);
-		free(proc_p);
+	socket_sockthreaddata_t *threaddata_p = socket_thread_attach ( proc_p->sock_p );
+
+	if ( threaddata_p == NULL ) {
+		socket_close ( proc_p->sock_p );
+		free ( proc_p );
 		return NULL;
 	}
 
-	_clsync_connect_setthreaddata(threaddata_p, proc_p, flags);
-
+	_clsync_connect_setthreaddata ( threaddata_p, proc_p, flags );
 	proc_p->procfunct = procfunct;
-	socket_thread_start(threaddata_p);
-
+	socket_thread_start ( threaddata_p );
 	return proc_p;
 }
 
-clsyncproc_t *clsync_listen_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags) {
-	return _clsync_x_unix(socket_path, procfunct, flags, "listen",  socket_listen_unix);
+clsyncproc_t *clsync_listen_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags )
+{
+	return _clsync_x_unix ( socket_path, procfunct, flags, "listen",  socket_listen_unix );
 }
 
 
-clsyncproc_t *clsync_connect_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags) {
-	return _clsync_x_unix(socket_path, procfunct, flags, "connect", socket_connect_unix);
+clsyncproc_t *clsync_connect_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags )
+{
+	return _clsync_x_unix ( socket_path, procfunct, flags, "connect", socket_connect_unix );
 }
 
 

+ 7 - 7
libclsync.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -26,7 +26,7 @@ struct clsyncproc {
 };
 typedef struct clsyncproc clsyncproc_t;
 
-extern int libclsync_init(int quite, int verbosity, int debug);
-extern clsyncproc_t *clsync_listen_unix (const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags);
-extern clsyncproc_t *clsync_connect_unix(const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags);
+extern int libclsync_init ( int quite, int verbosity, int debug );
+extern clsyncproc_t *clsync_listen_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags );
+extern clsyncproc_t *clsync_connect_unix ( const char *const socket_path, clsyncsock_procfunct_t procfunct, sockprocflags_t flags );
 

+ 8 - 8
macros.h

@@ -51,14 +51,14 @@
 #define require_strlen_le(str, limit) \
 	if (strlen(str) >= limit)\
 		critical("length of "TOSTR(str)" (\"%s\") >= "TOSTR(limit));\
-
+	 
 #define SAFE(code, onfail) __extension__({\
-	long _SAFE_rc;\
-	if ((_SAFE_rc = code)) {\
-		error("Got error while "TOSTR(code));\
-		onfail;\
-	} \
-	_SAFE_rc;\
-})
+		long _SAFE_rc;\
+		if ((_SAFE_rc = code)) {\
+			error("Got error while "TOSTR(code));\
+			onfail;\
+		} \
+		_SAFE_rc;\
+	})
 
 #endif

Разница между файлами не показана из-за своего большого размера
+ 1651 - 1376
main.c


+ 16 - 16
main.h

@@ -22,24 +22,24 @@ extern pid_t parent_pid;
 extern int argc;
 extern char *argv[];
 
-extern int main_rehash(ctx_t *ctx_p);
-extern int main_status_update(ctx_t *ctx_p);
-extern int ctx_set(ctx_t *ctx_p, const char *const parameter_name, const char *const parameter_value);
-extern int config_block_parse(ctx_t *ctx_p, const char *const config_block_name);
-extern int rules_count(ctx_t *ctx_p);
-extern char *parameter_expand(
-		ctx_t *ctx_p,
-		char *arg,
-		int exceptionflags,
-		int *macros_count_p,
-		int *expand_count_p,
-		const char *(*parameter_get)(const char *variable_name, void *arg),
-		void *parameter_get_arg
-	);
+extern int main_rehash ( ctx_t *ctx_p );
+extern int main_status_update ( ctx_t *ctx_p );
+extern int ctx_set ( ctx_t *ctx_p, const char *const parameter_name, const char *const parameter_value );
+extern int config_block_parse ( ctx_t *ctx_p, const char *const config_block_name );
+extern int rules_count ( ctx_t *ctx_p );
+extern char *parameter_expand (
+    ctx_t *ctx_p,
+    char *arg,
+    int exceptionflags,
+    int *macros_count_p,
+    int *expand_count_p,
+    const char * ( *parameter_get ) ( const char *variable_name, void *arg ),
+    void *parameter_get_arg
+);
 extern pid_t fork_helper();
 extern int parent_isalive();
-extern int sethandler_sigchld(void (*handler)());
-extern pid_t waitpid_timed(pid_t child_pid, int *status_p, long sec, long nsec);
+extern int sethandler_sigchld ( void ( *handler ) () );
+extern pid_t waitpid_timed ( pid_t child_pid, int *status_p, long sec, long nsec );
 
 #define exit_on(cond) { debug(30, "exit_on: checking: %s", TOSTR(cond)); if (unlikely(cond)) { debug(1, "Exiting due to: "TOSTR(cond)); exit(0); } }
 

+ 85 - 82
malloc.c

@@ -44,116 +44,117 @@ int  devzero_fd;
 # endif
 #endif
 
-void *xmalloc(size_t size) {
-	debug(20, "(%li)", size);
+void *xmalloc ( size_t size )
+{
+	debug ( 20, "(%li)", size );
 #ifdef PARANOID
 	size++;	// Just in case
 #endif
+	void *ret = malloc ( size );
 
-	void *ret = malloc(size);
-
-	if (ret == NULL)
-		critical("(%li): Cannot allocate memory.", size);
+	if ( ret == NULL )
+		critical ( "(%li): Cannot allocate memory.", size );
 
 #ifdef PARANOID
-	memset(ret, 0, size);
+	memset ( ret, 0, size );
 #endif
 	return ret;
 }
 
-void *xcalloc(size_t nmemb, size_t size) {
-	debug(20, "(%li, %li)", nmemb, size);
+void *xcalloc ( size_t nmemb, size_t size )
+{
+	debug ( 20, "(%li, %li)", nmemb, size );
 #ifdef PARANOID
 	nmemb++; // Just in case
 	size++;	 // Just in case
 #endif
+	void *ret = calloc ( nmemb, size );
 
-	void *ret = calloc(nmemb, size);
-
-	if (ret == NULL)
-		critical("(%li): Cannot allocate memory.", size);
+	if ( ret == NULL )
+		critical ( "(%li): Cannot allocate memory.", size );
 
 //	memset(ret, 0, nmemb*size);	// Just in case
 	return ret;
 }
 
-void *xrealloc(void *oldptr, size_t size) {
-	debug(20, "(%p, %li)", oldptr, size);
+void *xrealloc ( void *oldptr, size_t size )
+{
+	debug ( 20, "(%p, %li)", oldptr, size );
 #ifdef PARANOID
 	size++;	// Just in case
 #endif
+	void *ret = realloc ( oldptr, size );
 
-	void *ret = realloc(oldptr, size);
-
-	if (ret == NULL)
-		critical("(%p, %li): Cannot reallocate memory.", oldptr, size);
+	if ( ret == NULL )
+		critical ( "(%p, %li): Cannot reallocate memory.", oldptr, size );
 
 	return ret;
 }
 
 #ifdef CAPABILITIES_SUPPORT
-void *malloc_align(size_t size) {
+void *malloc_align ( size_t size )
+{
 	size_t total_size;
 	void *ret = NULL;
-	debug(20, "(%li)", size);
+	debug ( 20, "(%li)", size );
 # ifdef PARANOID
 	size++;	 // Just in case
 # endif
-
 	total_size  = size;
-
 	// Rounding total_size up to a number of times pagesize
-	total_size += pagesize-1;
+	total_size += pagesize - 1;
 	total_size /= pagesize;
 	total_size *= pagesize;
 
-	if (posix_memalign(&ret, pagesize, total_size))
-		critical("(%li): Cannot allocate memory.", size);
+	if ( posix_memalign ( &ret, pagesize, total_size ) )
+		critical ( "(%li): Cannot allocate memory.", size );
 
 # ifdef PARANOID
-	if (ret == NULL)
-		critical("(%li): ptr == NULL.", size);
-# endif
 
+	if ( ret == NULL )
+		critical ( "(%li): ptr == NULL.", size );
+
+# endif
 //	memset(ret, 0, nmemb*size);	// Just in case
 	return ret;
 }
 
-void *calloc_align(size_t nmemb, size_t size) {
+void *calloc_align ( size_t nmemb, size_t size )
+{
 	size_t total_size;
 	void *ret;
-	debug(20, "(%li, %li)", nmemb, size);
+	debug ( 20, "(%li, %li)", nmemb, size );
 # ifdef PARANOID
 	nmemb++; // Just in case
 	size++;	 // Just in case
 # endif
-
-	total_size = nmemb*size;
-	ret = malloc_align(total_size);
-	memset(ret, 0, total_size);
-
+	total_size = nmemb * size;
+	ret = malloc_align ( total_size );
+	memset ( ret, 0, total_size );
 	return ret;
 }
 
-char *strdup_protect(const char *src, int prot) {
-	size_t len = strlen(src)+1;
-	char *dst  = malloc_align(len);
-	strcpy(dst, src);
-	if (mprotect(dst, len, prot))
-		critical("(%p, 0x%o): Got error from mprotect(%p, %lu, 0x%o)", src, prot, dst, len, prot);
+char *strdup_protect ( const char *src, int prot )
+{
+	size_t len = strlen ( src ) + 1;
+	char *dst  = malloc_align ( len );
+	strcpy ( dst, src );
+
+	if ( mprotect ( dst, len, prot ) )
+		critical ( "(%p, 0x%o): Got error from mprotect(%p, %lu, 0x%o)", src, prot, dst, len, prot );
 
 	return dst;
 }
 
 # ifdef SECCOMP_SUPPORT
-int is_protected(void *addr) {
+int is_protected ( void *addr )
+{
 	char *_addr = addr, t;
 	int is_protected;
 	t = *_addr;
+	is_protected = ( read ( devzero_fd, addr, 1 ) == -1 );
 
-	is_protected = (read(devzero_fd, addr, 1) == -1);
-
-	if (!is_protected)
+	if ( !is_protected )
 		*_addr = t;
 
 	return is_protected;
@@ -162,81 +163,83 @@ int is_protected(void *addr) {
 
 #endif
 
-int memory_init() {
+int memory_init()
+{
 #ifdef CAPABILITIES_SUPPORT
-	pagesize   = sysconf(_SC_PAGE_SIZE);
+	pagesize   = sysconf ( _SC_PAGE_SIZE );
 
-	if (pagesize == -1)
-		critical("Got error from sysconf(_SC_PAGE_SIZE)");
+	if ( pagesize == -1 )
+		critical ( "Got error from sysconf(_SC_PAGE_SIZE)" );
 
 # ifdef SECCOMP_SUPPORT
-	devzero_fd = open(DEVZERO, O_RDONLY);
+	devzero_fd = open ( DEVZERO, O_RDONLY );
+
+	if ( devzero_fd == -1 )
+		critical ( "Got error while open(\""DEVZERO"\", O_RDONLY)" );
 
-	if (devzero_fd == -1)
-		critical("Got error while open(\""DEVZERO"\", O_RDONLY)");
 # endif
 #endif
-
 	return 0;
 }
 
-void *shm_malloc_try(size_t size) {
+void *shm_malloc_try ( size_t size )
+{
 	void *ret;
 #ifdef PARANOID
 	size++;
 #endif
-	int privileged_shmid = shmget(0, size, IPC_PRIVATE|IPC_CREAT|0600);
+	int privileged_shmid = shmget ( 0, size, IPC_PRIVATE | IPC_CREAT | 0600 );
 	struct shmid_ds shmid_ds;
-	if (privileged_shmid == -1) return NULL;
 
-	ret = shmat(privileged_shmid, NULL, 0);
-	if ((long)ret == -1) return NULL;
-	debug(15, "ret == %p", ret);
+	if ( privileged_shmid == -1 ) return NULL;
 
+	ret = shmat ( privileged_shmid, NULL, 0 );
+
+	if ( ( long ) ret == -1 ) return NULL;
+
+	debug ( 15, "ret == %p", ret );
 	// Forbidding access for others to the pointer
-	shmctl(privileged_shmid, IPC_STAT, &shmid_ds);
+	shmctl ( privileged_shmid, IPC_STAT, &shmid_ds );
 	shmid_ds.shm_perm.mode = 0;
-	shmctl(privileged_shmid, IPC_SET,  &shmid_ds);
-
+	shmctl ( privileged_shmid, IPC_SET,  &shmid_ds );
 	// Checking that nobody else attached to the shared memory before access forbidding
-	shmctl(privileged_shmid, IPC_STAT, &shmid_ds);
-	if (shmid_ds.shm_lpid != shmid_ds.shm_cpid) {
-		error("A process (pid %u) attached to my shared memory. It's a security problem. Emergency exit.");
-		shmdt (ret);
+	shmctl ( privileged_shmid, IPC_STAT, &shmid_ds );
+
+	if ( shmid_ds.shm_lpid != shmid_ds.shm_cpid ) {
+		error ( "A process (pid %u) attached to my shared memory. It's a security problem. Emergency exit." );
+		shmdt ( ret );
 		return NULL;
 	}
 
 	return ret;
 }
 
-void *shm_malloc(size_t size) {
+void *shm_malloc ( size_t size )
+{
 	void *ret;
-
-	ret = shm_malloc_try(size);
-	critical_on (ret == NULL);
-
+	ret = shm_malloc_try ( size );
+	critical_on ( ret == NULL );
 	return ret;
 }
 
-void *shm_calloc(size_t nmemb, size_t size) {
+void *shm_calloc ( size_t nmemb, size_t size )
+{
 	void *ret;
 	size_t total_size;
 #ifdef PARANOID
 	nmemb++;
 	size++;
 #endif
-
 	total_size = nmemb * size;
-
-	ret = shm_malloc(total_size);
-	critical_on (ret == NULL);
-
-	memset(ret, 0, total_size);
+	ret = shm_malloc ( total_size );
+	critical_on ( ret == NULL );
+	memset ( ret, 0, total_size );
 	return ret;
 }
 
-void shm_free(void *ptr) {
-	debug(25, "(%p)", ptr);
-	shmdt(ptr);
+void shm_free ( void *ptr )
+{
+	debug ( 25, "(%p)", ptr );
+	shmdt ( ptr );
 }
 

+ 11 - 11
malloc.h

@@ -19,21 +19,21 @@
 
 #include <sys/types.h>
 
-extern void *xmalloc(size_t size);
-extern void *xcalloc(size_t nmemb, size_t size);
-extern void *xrealloc(void *oldptr, size_t size);
+extern void *xmalloc ( size_t size );
+extern void *xcalloc ( size_t nmemb, size_t size );
+extern void *xrealloc ( void *oldptr, size_t size );
 #ifdef CAPABILITIES_SUPPORT
-extern void *malloc_align(size_t size);
-extern void *calloc_align(size_t nmemb, size_t size);
-extern char *strdup_protect(const char *src, int prot);
+extern void *malloc_align ( size_t size );
+extern void *calloc_align ( size_t nmemb, size_t size );
+extern char *strdup_protect ( const char *src, int prot );
 # ifdef SECCOMP_SUPPORT
-extern int is_protected(void *addr);
+extern int is_protected ( void *addr );
 # endif
 #endif
-extern void *shm_malloc(size_t size);
-extern void *shm_malloc_try(size_t size);
-extern void *shm_calloc(size_t nmemb, size_t size);
-extern void shm_free(void *ptr);
+extern void *shm_malloc ( size_t size );
+extern void *shm_malloc_try ( size_t size );
+extern void *shm_calloc ( size_t nmemb, size_t size );
+extern void shm_free ( void *ptr );
 
 extern int memory_init();
 

Разница между файлами не показана из-за своего большого размера
+ 376 - 346
mon_bsm.c


+ 9 - 9
mon_bsm.h

@@ -1,25 +1,25 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int bsm_init(ctx_t *ctx_p);
-extern int (*bsm_wait)(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *timeout_p);
-extern int (*bsm_handle)(struct ctx *ctx_p, struct indexes *indexes_p);
-extern int bsm_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
-extern int bsm_deinit(ctx_t *ctx_p);
+extern int bsm_init ( ctx_t *ctx_p );
+extern int ( *bsm_wait ) ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *timeout_p );
+extern int ( *bsm_handle ) ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern int bsm_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
+extern int bsm_deinit ( ctx_t *ctx_p );
 

+ 102 - 91
mon_dtracepipe.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -52,174 +52,185 @@ struct mondata {
 typedef struct mondata mondata_t;
 
 #define DTRACEPIPE_INIT_ERROR {\
-	free(ctx_p->fsmondata);\
-	ctx_p->fsmondata = NULL;\
-	return -1;\
-}
+		free(ctx_p->fsmondata);\
+		ctx_p->fsmondata = NULL;\
+		return -1;\
+	}
 
-int dtracepipe_init(ctx_t *ctx_p) {
+int dtracepipe_init ( ctx_t *ctx_p )
+{
 	char cmd[BUFSIZ];
-
-	ctx_p->fsmondata = xcalloc(sizeof(mondata_t), 1);
+	ctx_p->fsmondata = xcalloc ( sizeof ( mondata_t ), 1 );
 	mondata_t *mondata = ctx_p->fsmondata;
 
-	if (snprintf(cmd, "%s -n '%s' '%s'", DTRACE_PATH, DTRACE_SCRIPT, ) >= BUFSIZ) {
+	if ( snprintf ( cmd, "%s -n '%s' '%s'", DTRACE_PATH, DTRACE_SCRIPT, ) >= BUFSIZ ) {
 		errno = EMSGSIZE;
-		error("Too long cmd.");
+		error ( "Too long cmd." );
 		DTRACEPIPE_INIT_ERROR;
 	}
 
-	FILE *pipe = popen(cmd, "r");
-	if (pipe == NULL) {
-		error("Cannot popen(\""DTRACE_PATH"\", \"r\")");
+	FILE *pipe = popen ( cmd, "r" );
+
+	if ( pipe == NULL ) {
+		error ( "Cannot popen(\""DTRACE_PATH"\", \"r\")" );
 		DTRACEPIPE_INIT_ERROR;
 	}
-	
-	if (setvbuf(pipe, NULL, _IONBF, 0)) {
-		error("Cannot set unbuffered mode for pipe of \""DTRACE_PATH"\" process");
+
+	if ( setvbuf ( pipe, NULL, _IONBF, 0 ) ) {
+		error ( "Cannot set unbuffered mode for pipe of \""DTRACE_PATH"\" process" );
 		DTRACEPIPE_INIT_ERROR;
 	}
 
 	mondata->pipe = pipe;
-
 	return 0;
 }
 
 char  *dtracepipe_wait_line = NULL;
 size_t dtracepipe_wait_line_siz;
-int dtracepipe_wait(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *timeout_p) {
+int dtracepipe_wait ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *timeout_p )
+{
 	mondata_t *mondata = ctx_p->fsmondata;
 	struct timeval timeout_abs, tv_abs;
 	int dontwait = 0;
 	struct dtracepipe_event *event_p = &mondata->event;
 
-	if (timeout_p->tv_sec == 0 && timeout_p->tv_usec == 0)
+	if ( timeout_p->tv_sec == 0 && timeout_p->tv_usec == 0 )
 		dontwait = 1;
 
-	if (!dontwait) {
-		gettimeofday(&tv_abs, NULL);
-		timeradd(&tv_abs, timeout_p, &timeout_abs);
+	if ( !dontwait ) {
+		gettimeofday ( &tv_abs, NULL );
+		timeradd ( &tv_abs, timeout_p, &timeout_abs );
 	}
 
-	int pipe_fd = fileno(mondata->pipe);
+	int pipe_fd = fileno ( mondata->pipe );
 
-	while (42) {
+	while ( 42 ) {
 		int path_count;
 
 		// Checking if there already a recond in mondata
-		if (*event_p->path) {
-			debug(2, "we have an event. return 1.");
+		if ( *event_p->path ) {
+			debug ( 2, "we have an event. return 1." );
 			return 1;
 		}
 
 		// Getting a record
 		{
-			debug(3, "select() with timeout %li.%06li secs (dontwait == %u).", timeout_p->tv_sec, timeout_p->tv_usec, dontwait);
+			debug ( 3, "select() with timeout %li.%06li secs (dontwait == %u).", timeout_p->tv_sec, timeout_p->tv_usec, dontwait );
 			fd_set rfds;
-			FD_ZERO(&rfds);
-			FD_SET(pipe_fd, &rfds);
-			int rc = select(pipe_fd+1, &rfds, NULL, NULL, timeout_p);
+			FD_ZERO ( &rfds );
+			FD_SET ( pipe_fd, &rfds );
+			int rc = select ( pipe_fd + 1, &rfds, NULL, NULL, timeout_p );
 
-			if (rc == 0 || rc == -1)
+			if ( rc == 0 || rc == -1 )
 				return rc;
 
-			line_len = getline(&dtracepipe_wait_line, &dtracepipe_wait_line_siz, mondata->pipe);
-			if (line_len == -1) {
-				error("Cannot read line from \""DTRACE_PATH"\" pipe [using getline()]");
+			line_len = getline ( &dtracepipe_wait_line, &dtracepipe_wait_line_siz, mondata->pipe );
+
+			if ( line_len == -1 ) {
+				error ( "Cannot read line from \""DTRACE_PATH"\" pipe [using getline()]" );
 				return -1;
 			}
 
-			if (!dontwait) {
-				debug(5, "old timeout_p->: tv_sec == %lu; tv_usec == %lu", timeout_p->tv_sec, timeout_p->tv_usec);
-				gettimeofday(&tv_abs, NULL);
-				if (timercmp(&timeout_abs, &tv_abs, <))
-					timersub(&timeout_abs, &tv_abs, timeout_p);
+			if ( !dontwait ) {
+				debug ( 5, "old timeout_p->: tv_sec == %lu; tv_usec == %lu", timeout_p->tv_sec, timeout_p->tv_usec );
+				gettimeofday ( &tv_abs, NULL );
+
+				if ( timercmp ( &timeout_abs, &tv_abs, < ) )
+					timersub ( &timeout_abs, &tv_abs, timeout_p );
 				else
-					memset(timeout_p, 0, sizeof(*timeout_p));
-				debug(5, "new timeout_p->: tv_sec == %lu; tv_usec == %lu", timeout_p->tv_sec, timeout_p->tv_usec);
+					memset ( timeout_p, 0, sizeof ( *timeout_p ) );
+
+				debug ( 5, "new timeout_p->: tv_sec == %lu; tv_usec == %lu", timeout_p->tv_sec, timeout_p->tv_usec );
 			}
 		}
-
 		// Parsing the record
 		path_count = 0;
-		debug(3, "parsing the event");
-		while (au_parsed < au_len) {
+		debug ( 3, "parsing the event" );
 
-			if (au_fetch_tok(&tok, &au_buf[au_parsed], au_len - au_parsed) == -1)
+		while ( au_parsed < au_len ) {
+			if ( au_fetch_tok ( &tok, &au_buf[au_parsed], au_len - au_parsed ) == -1 )
 				return -1;
+
 			au_parsed += tok.len;
 
-			switch (tok.id) {
+			switch ( tok.id ) {
 				case AUT_HEADER32:
 				case AUT_HEADER32_EX:
 				case AUT_HEADER64:
 				case AUT_HEADER64_EX: {
-					event_p->type = tok.tt.hdr32.e_type;
-					path_count = 0;
-					break;
-				}
+						event_p->type = tok.tt.hdr32.e_type;
+						path_count = 0;
+						break;
+					}
+
 				case AUT_PATH: {
-					char *ptr;
-					int dir_wd, dir_iswatched;
+						char *ptr;
+						int dir_wd, dir_iswatched;
+						ptr = memrchr ( tok.tt.path.path, '/', tok.tt.path.len );
+#ifdef PARANOID
 
-					ptr = memrchr(tok.tt.path.path, '/', tok.tt.path.len);
+						if ( ptr == NULL )
+							critical ( "relative path received from au_fetch_tok(): \"%s\" (len: %u)", tok.tt.path.path, tok.tt.path.len );
 
-#ifdef PARANOID
-					if (ptr == NULL)
-						critical("relative path received from au_fetch_tok(): \"%s\" (len: %u)", tok.tt.path.path, tok.tt.path.len);
 #endif
-
-					debug(6, "Event on \"%s\".", tok.tt.path.path);
-					*ptr = 0;
-					dir_wd = indexes_fpath2wd(indexes_p, tok.tt.path.path);
-					dir_iswatched = (dir_wd != -1);
-					debug(7, "Directory is \"%s\". dir_wd == %i; dir_iswatched == %u", tok.tt.path.path, dir_wd, dir_iswatched);
-					*ptr = '/';
-
-					if (dir_iswatched) {
-						debug(5, "Event on \"%s\" is watched. Pushing. path_count == %u", tok.tt.path.path, path_count);
-						switch (path_count) {
-							case 0: 
-								memcpy(event_p->path,    tok.tt.path.path, tok.tt.path.len+1);
-								break;
-							case 1: 
-								memcpy(event_p->path_to, tok.tt.path.path, tok.tt.path.len+1);
-								break;
+						debug ( 6, "Event on \"%s\".", tok.tt.path.path );
+						*ptr = 0;
+						dir_wd = indexes_fpath2wd ( indexes_p, tok.tt.path.path );
+						dir_iswatched = ( dir_wd != -1 );
+						debug ( 7, "Directory is \"%s\". dir_wd == %i; dir_iswatched == %u", tok.tt.path.path, dir_wd, dir_iswatched );
+						*ptr = '/';
+
+						if ( dir_iswatched ) {
+							debug ( 5, "Event on \"%s\" is watched. Pushing. path_count == %u", tok.tt.path.path, path_count );
+
+							switch ( path_count ) {
+								case 0:
+									memcpy ( event_p->path,    tok.tt.path.path, tok.tt.path.len + 1 );
+									break;
+
+								case 1:
+									memcpy ( event_p->path_to, tok.tt.path.path, tok.tt.path.len + 1 );
+									break;
 #ifdef PARANOID
-							default:
-								warning("To many paths on BSM event: \"%s\" (already count: %u)", tok.tt.path.path, path_count);
-								break;
+
+								default:
+									warning ( "To many paths on BSM event: \"%s\" (already count: %u)", tok.tt.path.path, path_count );
+									break;
 #endif
+							}
 						}
+
+						path_count++;
+						break;
 					}
-					path_count++;
-					break;
-				}
+
 				default:
 					continue;
 			}
 		}
 
 		// Cleanup
-		debug(4, "clean up");
-		free(au_buf);
+		debug ( 4, "clean up" );
+		free ( au_buf );
 		au_buf    = NULL;
 		au_len    = 0;
 		au_parsed = 0;
 	}
+
 	return -1;
 }
-int dtracepipe_handle(struct ctx *ctx_p, struct indexes *indexes_p) {
+int dtracepipe_handle ( struct ctx *ctx_p, struct indexes *indexes_p )
+{
 	return -1;
 }
-int dtracepipe_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath) {
+int dtracepipe_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath )
+{
 	return -1;
 }
-int dtracepipe_deinit(ctx_t *ctx_p) {
+int dtracepipe_deinit ( ctx_t *ctx_p )
+{
 	mondata_t *mondata = ctx_p->fsmondata;
-
-	free(dtracepipe_wait_line);
-	free(mondata);
-
+	free ( dtracepipe_wait_line );
+	free ( mondata );
 	return -1;
 }

+ 9 - 9
mon_dtracepipe.h

@@ -1,25 +1,25 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int dtracepipe_init(ctx_t *ctx_p);
-extern int dtracepipe_wait(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
-extern int dtracepipe_handle(struct ctx *ctx_p, struct indexes *indexes_p);
-extern int dtracepipe_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
-extern int dtracepipe_deinit(ctx_t *ctx_p);
+extern int dtracepipe_init ( ctx_t *ctx_p );
+extern int dtracepipe_wait ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p );
+extern int dtracepipe_handle ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern int dtracepipe_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
+extern int dtracepipe_deinit ( ctx_t *ctx_p );
 

+ 35 - 26
mon_fanotify.c

@@ -1,61 +1,70 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
 #if 0
 
-int fanotify_add_watch_dir(int fanotify_d, struct ctx *ctx_p, const char *const accpath) {
-	return fanotify_mark(fanotify_d, FAN_MARK_ADD | FAN_MARK_DONT_FOLLOW,
-		FANOTIFY_MARKMASK, AT_FDCWD, accpath);
+int fanotify_add_watch_dir ( int fanotify_d, struct ctx *ctx_p, const char *const accpath )
+{
+	return fanotify_mark ( fanotify_d, FAN_MARK_ADD | FAN_MARK_DONT_FOLLOW,
+	                       FANOTIFY_MARKMASK, AT_FDCWD, accpath );
 }
 
-int fanotify_loop(int fanotify_d, ctx_t *ctx_p, indexes_t *indexes_p) {
-	struct fanotify_event_metadata buf[BUFSIZ/sizeof(struct fanotify_event_metadata) + 1];
+int fanotify_loop ( int fanotify_d, ctx_t *ctx_p, indexes_t *indexes_p )
+{
+	struct fanotify_event_metadata buf[BUFSIZ / sizeof ( struct fanotify_event_metadata ) + 1];
 	int state = STATE_RUNNING;
 	state_p = &state;
 
-	while(state != STATE_EXIT) {
+	while ( state != STATE_EXIT ) {
 		struct fanotify_event_metadata *metadata;
-		size_t len = read(fanotify_d, (void *)buf, sizeof(buf)-sizeof(*buf));
-		metadata=buf;
-		if(len == -1) {
-			error("cannot read(%i, &metadata, sizeof(metadata)).", fanotify_d);
+		size_t len = read ( fanotify_d, ( void * ) buf, sizeof ( buf ) - sizeof ( *buf ) );
+		metadata = buf;
+
+		if ( len == -1 ) {
+			error ( "cannot read(%i, &metadata, sizeof(metadata)).", fanotify_d );
 			return errno;
 		}
-		while(FAN_EVENT_OK(metadata, len)) {
-			debug(2, "metadata->pid: %i; metadata->fd: %i", metadata->pid, metadata->fd);
-			if (metadata->fd != FAN_NOFD) {
-				if (metadata->fd >= 0) {
-					char *fpath = fd2fpath_malloc(metadata->fd);
-					sync_queuesync(fpath_rel, 0, ctx_p, indexes_p, QUEUE_AUTO);
-					debug(2, "Event %i on \"%s\".", metadata->mask, fpath);
-					free(fpath);
+
+		while ( FAN_EVENT_OK ( metadata, len ) ) {
+			debug ( 2, "metadata->pid: %i; metadata->fd: %i", metadata->pid, metadata->fd );
+
+			if ( metadata->fd != FAN_NOFD ) {
+				if ( metadata->fd >= 0 ) {
+					char *fpath = fd2fpath_malloc ( metadata->fd );
+					sync_queuesync ( fpath_rel, 0, ctx_p, indexes_p, QUEUE_AUTO );
+					debug ( 2, "Event %i on \"%s\".", metadata->mask, fpath );
+					free ( fpath );
 				}
 			}
-			close(metadata->fd);
-			metadata = FAN_EVENT_NEXT(metadata, len);
+
+			close ( metadata->fd );
+			metadata = FAN_EVENT_NEXT ( metadata, len );
 		}
+
 		int ret;
-		if((ret=sync_idle(fanotify_d, ctx_p, indexes_p))) {
-			error("got error while sync_idle().");
+
+		if ( ( ret = sync_idle ( fanotify_d, ctx_p, indexes_p ) ) ) {
+			error ( "got error while sync_idle()." );
 			return ret;
 		}
 	}
+
 	return 0;
 }
 #endif

+ 5 - 5
mon_fanotify.h

@@ -1,21 +1,21 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int fanotify_add_watch_dir(struct ctx *ctx_p, const char *const accpath);
+extern int fanotify_add_watch_dir ( struct ctx *ctx_p, const char *const accpath );
 

+ 173 - 174
mon_gio.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue/bsm/gio
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -56,116 +56,120 @@ event_t *queue = NULL;
 int queue_length;
 int queue_alloc;
 
-static inline void event_free(event_t *ev) {
-	free(ev->path);
+static inline void event_free ( event_t *ev )
+{
+	free ( ev->path );
 	return;
 }
 
-static inline int event_push(char *path, gulong handle_id, GFileMonitorEvent event, eventobjtype_t objtype_event, eventobjtype_t objtype_old, eventobjtype_t objtype_new) {
+static inline int event_push ( char *path, gulong handle_id, GFileMonitorEvent event, eventobjtype_t objtype_event, eventobjtype_t objtype_old, eventobjtype_t objtype_new )
+{
 	event_t *ev;
+	debug ( 30, "pthread_spin_lock(&queue_lock);" );
+	pthread_spin_lock ( &queue_lock );
 
-	debug(30, "pthread_spin_lock(&queue_lock);");
-	pthread_spin_lock(&queue_lock);
-
-	if (queue_length >= queue_alloc) {
+	if ( queue_length >= queue_alloc ) {
 		queue_alloc += ALLOC_PORTION;
-		critical_on (queue_alloc >= GIO_QUEUE_LENGTH_MAX);
-		queue = xrealloc(queue, queue_alloc*sizeof(*queue));
+		critical_on ( queue_alloc >= GIO_QUEUE_LENGTH_MAX );
+		queue = xrealloc ( queue, queue_alloc * sizeof ( *queue ) );
 	}
 
 	ev = &queue[queue_length++];
-
 	ev->path          = path;
 	ev->event_id      = event;
 	ev->handle_id     = handle_id;
 	ev->objtype_event = objtype_event;
 	ev->objtype_old   = objtype_old;
 	ev->objtype_new   = objtype_new;
-
-	debug(30, "pthread_spin_unlock(&queue_lock);");
-	pthread_spin_unlock(&queue_lock);
+	debug ( 30, "pthread_spin_unlock(&queue_lock);" );
+	pthread_spin_unlock ( &queue_lock );
 	return 0;
 }
 
-static inline event_t *event_pop() {
+static inline event_t *event_pop()
+{
 	static event_t ev;
-
-	debug(30, "pthread_spin_lock(&queue_lock);");
-	pthread_spin_lock(&queue_lock);
-	critical_on (!queue_length);
-	memcpy(&ev, &queue[--queue_length], sizeof(ev));
-	debug(30, "pthread_spin_unlock(&queue_lock);");
-	pthread_spin_unlock(&queue_lock);
-
+	debug ( 30, "pthread_spin_lock(&queue_lock);" );
+	pthread_spin_lock ( &queue_lock );
+	critical_on ( !queue_length );
+	memcpy ( &ev, &queue[--queue_length], sizeof ( ev ) );
+	debug ( 30, "pthread_spin_unlock(&queue_lock);" );
+	pthread_spin_unlock ( &queue_lock );
 	return &ev;
 }
 
-static void dir_gotevent(
-		GFileMonitor		*filemon,
-		GFile			*file,
-		GFile			*file_other,
-		GFileMonitorEvent	 event,
-		gpointer		 arg
-) {
+static void dir_gotevent (
+    GFileMonitor		*filemon,
+    GFile			*file,
+    GFile			*file_other,
+    GFileMonitorEvent	 event,
+    gpointer		 arg
+)
+{
 	eventobjtype_t objtype_old, objtype_new, objtype;
 	filemondata_t *fmdat    = arg;
 	ctx_t         *ctx_p    = fmdat->ctx_p;
-	GFileType      filetype = g_file_query_file_type(file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL);
-	debug(10, "%p %p %p %i %p %i", filemon, file, file_other, event, arg, filetype);
-
+	GFileType      filetype = g_file_query_file_type ( file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL );
+	debug ( 10, "%p %p %p %i %p %i", filemon, file, file_other, event, arg, filetype );
 	char *path_full, *path_rel = NULL;
-	switch (event) {
+
+	switch ( event ) {
 		case G_FILE_MONITOR_EVENT_DELETED:
 		case G_FILE_MONITOR_EVENT_CREATED:
 		case G_FILE_MONITOR_EVENT_CHANGED:
 		case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
-			path_full = g_file_get_path(file);
-			path_rel  = strdup(&path_full[ctx_p->watchdirlen+1]);
-			g_free(path_full);
-			debug(9, "Got event %i for \"%s\" (%i)", event, path_rel, filetype);
+			path_full = g_file_get_path ( file );
+			path_rel  = strdup ( &path_full[ctx_p->watchdirlen + 1] );
+			g_free ( path_full );
+			debug ( 9, "Got event %i for \"%s\" (%i)", event, path_rel, filetype );
 			break;
+
 		default:
 			break;
 	}
 
-	switch (filetype) {
+	switch ( filetype ) {
 		case G_FILE_TYPE_DIRECTORY:
 			objtype = EOT_DIR;
 			break;
+
 		default:
 			objtype = EOT_FILE;
 			break;
 	}
 
-	switch (event) {
+	switch ( event ) {
 		case G_FILE_MONITOR_EVENT_DELETED:
 			objtype_old = objtype;
 			objtype_new = EOT_DOESNTEXIST;
 			break;
+
 		case G_FILE_MONITOR_EVENT_CREATED:
 			objtype_old = EOT_DOESNTEXIST;
 			objtype_new = objtype;
 			break;
+
 		default:
 			objtype_old = objtype;
 			objtype_new = objtype;
 			break;
 	}
 
-	switch (event) {
+	switch ( event ) {
 		case G_FILE_MONITOR_EVENT_DELETED:
-			debug(20, "g_hash_table_remove(mondirs_ht, \"%s\")", path_rel);
-			g_hash_table_remove(mondirs_ht, path_rel);
+			debug ( 20, "g_hash_table_remove(mondirs_ht, \"%s\")", path_rel );
+			g_hash_table_remove ( mondirs_ht, path_rel );
+
 		case G_FILE_MONITOR_EVENT_CREATED:
 		case G_FILE_MONITOR_EVENT_CHANGED:
 		case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
 #ifdef PARANOID
-			critical_on (path_rel == NULL);
+			critical_on ( path_rel == NULL );
 #endif
-
-			debug(20, "event_push(\"%s\", %i, %i, %i, %i, %i)", path_rel, fmdat->handle_id, event, objtype, objtype_old, objtype_new);
-			critical_on (event_push(path_rel, fmdat->handle_id, event, objtype, objtype_old, objtype_new));
+			debug ( 20, "event_push(\"%s\", %i, %i, %i, %i, %i)", path_rel, fmdat->handle_id, event, objtype, objtype_old, objtype_new );
+			critical_on ( event_push ( path_rel, fmdat->handle_id, event, objtype, objtype_old, objtype_new ) );
 			break;
+
 		default:
 			break;
 	}
@@ -173,154 +177,152 @@ static void dir_gotevent(
 	return;
 }
 
-int gio_add_watch_dir(ctx_t *ctx_p, indexes_t *indexes_p, const char *const accpath) {
-	(void) indexes_p;
-
+int gio_add_watch_dir ( ctx_t *ctx_p, indexes_t *indexes_p, const char *const accpath )
+{
+	( void ) indexes_p;
 	filemondata_t *fmdat;
 	GError        *error = NULL;
-	debug(3, "\"%s\"", accpath);
-
+	debug ( 3, "\"%s\"", accpath );
 #ifdef PARANOID
-	fmdat = g_hash_table_lookup(mondirs_ht, accpath);
-	if (fmdat != NULL) {
+	fmdat = g_hash_table_lookup ( mondirs_ht, accpath );
+
+	if ( fmdat != NULL ) {
 		errno = EADDRINUSE;
-		warning("Directory \"%s\" is already monitored.", accpath);
+		warning ( "Directory \"%s\" is already monitored.", accpath );
 		return -1;
 	}
-#endif
-
-	fmdat = xmalloc(sizeof(*fmdat));
 
+#endif
+	fmdat = xmalloc ( sizeof ( *fmdat ) );
 	fmdat->ctx_p     = ctx_p;
-	fmdat->file      = g_file_new_for_path(accpath);
-	fmdat->filemon   = g_file_monitor_directory(fmdat->file, 0, NULL, &error);
-
-	fmdat->handle_id = g_signal_connect (fmdat->filemon, "changed", G_CALLBACK(dir_gotevent), fmdat);
-
-	g_hash_table_replace(mondirs_ht, strdup(accpath), fmdat);
-
+	fmdat->file      = g_file_new_for_path ( accpath );
+	fmdat->filemon   = g_file_monitor_directory ( fmdat->file, 0, NULL, &error );
+	fmdat->handle_id = g_signal_connect ( fmdat->filemon, "changed", G_CALLBACK ( dir_gotevent ), fmdat );
+	g_hash_table_replace ( mondirs_ht, strdup ( accpath ), fmdat );
 	return fmdat->handle_id;
 }
 
 int cancel_g_iteration_stop;
 pthread_t thread_g_iteration_stop;
-void *g_iteration_stop(void *_timeout_p) {
+void *g_iteration_stop ( void *_timeout_p )
+{
 	struct timeval *timeout_p = _timeout_p;
 	struct timeval tv_abs, timeout_abs;
 	struct timespec ts_abs;
-	debug(10, "{%u, %u}", timeout_p->tv_sec, timeout_p->tv_usec);
-	critical_on (pthread_mutex_lock(&gio_mutex_prefetcher));
-	if (cancel_g_iteration_stop) {
-		critical_on (pthread_mutex_unlock(&gio_mutex_prefetcher));
+	debug ( 10, "{%u, %u}", timeout_p->tv_sec, timeout_p->tv_usec );
+	critical_on ( pthread_mutex_lock ( &gio_mutex_prefetcher ) );
+
+	if ( cancel_g_iteration_stop ) {
+		critical_on ( pthread_mutex_unlock ( &gio_mutex_prefetcher ) );
 		return NULL;
 	}
 
 #define INFINITETIME (3600 * 24 * 365 * 10) /* ~10 years */
-	if (timeout_p->tv_sec > INFINITETIME)
-		timeout_p->tv_sec = INFINITETIME;
-#undef INFINITETIME
 
-	gettimeofday(&tv_abs, NULL);
-	timeradd(&tv_abs, timeout_p, &timeout_abs);
+	if ( timeout_p->tv_sec > INFINITETIME )
+		timeout_p->tv_sec = INFINITETIME;
 
+#undef INFINITETIME
+	gettimeofday ( &tv_abs, NULL );
+	timeradd ( &tv_abs, timeout_p, &timeout_abs );
 	ts_abs.tv_sec  = timeout_abs.tv_sec;
-	ts_abs.tv_nsec = timeout_abs.tv_usec*1000;
-	debug(10, "{%u, %u}", ts_abs.tv_sec, ts_abs.tv_nsec);
-	
-	switch ((errno = pthread_cond_timedwait(&gio_cond_gotevent, &gio_mutex_prefetcher, &ts_abs))) {
+	ts_abs.tv_nsec = timeout_abs.tv_usec * 1000;
+	debug ( 10, "{%u, %u}", ts_abs.tv_sec, ts_abs.tv_nsec );
+
+	switch ( ( errno = pthread_cond_timedwait ( &gio_cond_gotevent, &gio_mutex_prefetcher, &ts_abs ) ) ) {
 		case 0:
 		case ETIMEDOUT:
 			break;
+
 		default:
-			critical ("Got error while pthread_cond_timedwait(&gio_cond_gotevent, &gio_mutex_prefetcher, &ts_abs)");
+			critical ( "Got error while pthread_cond_timedwait(&gio_cond_gotevent, &gio_mutex_prefetcher, &ts_abs)" );
 	}
-	g_main_context_wakeup(NULL);
-	pthread_mutex_unlock(&gio_mutex_prefetcher);
 
-	debug(10, "return");
+	g_main_context_wakeup ( NULL );
+	pthread_mutex_unlock ( &gio_mutex_prefetcher );
+	debug ( 10, "return" );
 	return NULL;
 }
 
-static inline int gio_wait_now(ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p) {
-	(void) ctx_p; (void) indexes_p;
-
+static inline int gio_wait_now ( ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p )
+{
+	( void ) ctx_p;
+	( void ) indexes_p;
 	void *ret;
 	int result;
-	debug(3, "(ctx_p, indexes_p, %p {%u, %u})", tv_p, tv_p == NULL?-1:tv_p->tv_sec, tv_p == NULL?0:tv_p->tv_usec);
+	debug ( 3, "(ctx_p, indexes_p, %p {%u, %u})", tv_p, tv_p == NULL ? -1 : tv_p->tv_sec, tv_p == NULL ? 0 : tv_p->tv_usec );
 #ifdef PARANOID
-	critical_on (tv_p == NULL);
+	critical_on ( tv_p == NULL );
 #endif
 
-	if (queue_length) {
-		debug(9, "already: queue_length == %i", queue_length);
+	if ( queue_length ) {
+		debug ( 9, "already: queue_length == %i", queue_length );
 		return queue_length;
 	}
 
-	if (tv_p->tv_sec == 0 && tv_p->tv_usec == 0) {
-		g_main_context_iteration(NULL, FALSE);
-		debug(9, "nowait: queue_length == %i", queue_length);
+	if ( tv_p->tv_sec == 0 && tv_p->tv_usec == 0 ) {
+		g_main_context_iteration ( NULL, FALSE );
+		debug ( 9, "nowait: queue_length == %i", queue_length );
 		return queue_length;
 	}
 
 	cancel_g_iteration_stop = 0;
-	pthread_create(&thread_g_iteration_stop, NULL, g_iteration_stop, tv_p);
-/*
-	debug(30, "pthread_spin_unlock(&queue_lock);");
-	pthread_spin_unlock(&queue_lock);
-	debug(30 , "g_main_context_iteration(NULL, FALSE);");
-	result  = g_main_context_iteration(NULL, FALSE);
-	debug(30, "pthread_spin_lock(&queue_lock);");
-	pthread_spin_lock(&queue_lock);
-
-	if (queue_length) {
-		debug(9, "already2: queue_length == %i", queue_length);
-		return queue_length;
-	}
-*/
-	debug_call  (40, pthread_spin_unlock(&queue_lock));
-	debug(20 , "g_main_context_iteration(NULL, TRUE); queue_length == %i", queue_length);
-	result  = g_main_context_iteration(NULL, TRUE);
-	debug(10, "g_main_context_iteration() -> %i", result);
-	debug_call  (40, pthread_spin_lock(&queue_lock));
-	critical_on (pthread_mutex_lock(&gio_mutex_prefetcher));
+	pthread_create ( &thread_g_iteration_stop, NULL, g_iteration_stop, tv_p );
+	/*
+		debug(30, "pthread_spin_unlock(&queue_lock);");
+		pthread_spin_unlock(&queue_lock);
+		debug(30 , "g_main_context_iteration(NULL, FALSE);");
+		result  = g_main_context_iteration(NULL, FALSE);
+		debug(30, "pthread_spin_lock(&queue_lock);");
+		pthread_spin_lock(&queue_lock);
+
+		if (queue_length) {
+			debug(9, "already2: queue_length == %i", queue_length);
+			return queue_length;
+		}
+	*/
+	debug_call  ( 40, pthread_spin_unlock ( &queue_lock ) );
+	debug ( 20 , "g_main_context_iteration(NULL, TRUE); queue_length == %i", queue_length );
+	result  = g_main_context_iteration ( NULL, TRUE );
+	debug ( 10, "g_main_context_iteration() -> %i", result );
+	debug_call  ( 40, pthread_spin_lock ( &queue_lock ) );
+	critical_on ( pthread_mutex_lock ( &gio_mutex_prefetcher ) );
 	cancel_g_iteration_stop = 1;
-	critical_on (pthread_mutex_unlock(&gio_mutex_prefetcher));
-	critical_on (pthread_cond_broadcast(&gio_cond_gotevent));
-	critical_on (pthread_join(thread_g_iteration_stop, &ret));
-
-	debug(9, "queue_length == %i", queue_length);
+	critical_on ( pthread_mutex_unlock ( &gio_mutex_prefetcher ) );
+	critical_on ( pthread_cond_broadcast ( &gio_cond_gotevent ) );
+	critical_on ( pthread_join ( thread_g_iteration_stop, &ret ) );
+	debug ( 9, "queue_length == %i", queue_length );
 	return queue_length;
 }
-int gio_wait(ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p) {
-	(void) ctx_p;
-
+int gio_wait ( ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p )
+{
+	( void ) ctx_p;
 	int ret;
-
-	debug(30, "pthread_spin_lock(&queue_lock);");
-	debug_call (40, pthread_spin_lock(&queue_lock));
-	ret = gio_wait_now(ctx_p, indexes_p, tv_p);
-	debug(30, "pthread_spin_unlock(&queue_lock);");
-	debug_call (40, pthread_spin_unlock(&queue_lock));
-
+	debug ( 30, "pthread_spin_lock(&queue_lock);" );
+	debug_call ( 40, pthread_spin_lock ( &queue_lock ) );
+	ret = gio_wait_now ( ctx_p, indexes_p, tv_p );
+	debug ( 30, "pthread_spin_unlock(&queue_lock);" );
+	debug_call ( 40, pthread_spin_unlock ( &queue_lock ) );
 	return ret;
 }
 
-int gio_handle(ctx_t *ctx_p, indexes_t *indexes_p) {
-	static struct timeval tv={0};
+int gio_handle ( ctx_t *ctx_p, indexes_t *indexes_p )
+{
+	static struct timeval tv = {0};
 	int count;
-
 	char   *path_full	 = NULL;
 	size_t  path_full_len	 = 0;
-
 	count = 0;
-	while (gio_wait(ctx_p, indexes_p, &tv)) {
+
+	while ( gio_wait ( ctx_p, indexes_p, &tv ) ) {
 		event_t *ev = event_pop();
 		stat64_t lstat, *lstat_p;
 		mode_t st_mode;
 		size_t st_size;
-		if ((ev->objtype_new == EOT_DOESNTEXIST) || (ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT) || lstat64(ev->path, &lstat)) {
-			debug(2, "Cannot lstat64(\"%s\", lstat). Seems, that the object had been deleted (%i) or option \"--cancel-syscalls mon_stat\" (%i) is set.", ev->path, ev->objtype_new == EOT_DOESNTEXIST, ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT);
-			st_mode = (ev->objtype_event == EOT_DIR ? S_IFDIR : S_IFREG);
+
+		if ( ( ev->objtype_new == EOT_DOESNTEXIST ) || ( ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT ) || lstat64 ( ev->path, &lstat ) ) {
+			debug ( 2, "Cannot lstat64(\"%s\", lstat). Seems, that the object had been deleted (%i) or option \"--cancel-syscalls mon_stat\" (%i) is set.", ev->path, ev->objtype_new == EOT_DOESNTEXIST, ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT );
+			st_mode = ( ev->objtype_event == EOT_DIR ? S_IFDIR : S_IFREG );
 			st_size = 0;
 			lstat_p = NULL;
 		} else {
@@ -329,65 +331,62 @@ int gio_handle(ctx_t *ctx_p, indexes_t *indexes_p) {
 			lstat_p = &lstat;
 		}
 
-		if (sync_prequeue_loadmark(1, ctx_p, indexes_p, NULL, ev->path, lstat_p, ev->objtype_old, ev->objtype_new, ev->event_id, ev->handle_id, st_mode, st_size, &path_full, &path_full_len, NULL)) {
-			event_free(ev);
+		if ( sync_prequeue_loadmark ( 1, ctx_p, indexes_p, NULL, ev->path, lstat_p, ev->objtype_old, ev->objtype_new, ev->event_id, ev->handle_id, st_mode, st_size, &path_full, &path_full_len, NULL ) ) {
+			event_free ( ev );
 			count = -1;
 			break;
 		}
-		event_free(ev);
+
+		event_free ( ev );
 		count++;
 	}
+
 	// Globally queueing captured events:
 	// Moving events from local queue to global ones
-	sync_prequeue_unload(ctx_p, indexes_p);
-
-	free(path_full);
+	sync_prequeue_unload ( ctx_p, indexes_p );
+	free ( path_full );
 #ifdef VERYPARANOID
 	path_full     = NULL;
 	path_full_len = 0;
 #endif
-
 	return count;
 }
 
-void free_filemondat(void *_fmdat) {
+void free_filemondat ( void *_fmdat )
+{
 	filemondata_t *fmdat = _fmdat;
-
-	g_signal_handler_disconnect(fmdat->file, fmdat->handle_id);
-
-	free(fmdat->file);
-	free(fmdat->filemon);
-
-	free(fmdat);
+	g_signal_handler_disconnect ( fmdat->file, fmdat->handle_id );
+	free ( fmdat->file );
+	free ( fmdat->filemon );
+	free ( fmdat );
 	return;
 }
 
 GMainLoop *gio_loop = NULL;
-int gio_init(ctx_t *ctx_p) {
-	(void) ctx_p;
-
+int gio_init ( ctx_t *ctx_p )
+{
+	( void ) ctx_p;
 	queue_length = 0;
 	queue_alloc  = 0;
-	pthread_mutex_init (&gio_mutex_prefetcher, NULL);
-	pthread_cond_init  (&gio_cond_gotevent,    NULL);
-	pthread_spin_init  (&queue_lock,           PTHREAD_PROCESS_SHARED);
-	mondirs_ht = g_hash_table_new_full(g_str_hash, g_str_equal, free, free_filemondat);
-	gio_loop   = g_main_loop_new(NULL, TRUE);
-
-	g_main_context_iteration(NULL, FALSE);
+	pthread_mutex_init ( &gio_mutex_prefetcher, NULL );
+	pthread_cond_init  ( &gio_cond_gotevent,    NULL );
+	pthread_spin_init  ( &queue_lock,           PTHREAD_PROCESS_SHARED );
+	mondirs_ht = g_hash_table_new_full ( g_str_hash, g_str_equal, free, free_filemondat );
+	gio_loop   = g_main_loop_new ( NULL, TRUE );
+	g_main_context_iteration ( NULL, FALSE );
 	return 0;
 }
 
-int gio_deinit(ctx_t *ctx_p) {
-	(void) ctx_p;
-
-/*
-	g_main_loop_quit(gio_loop);
-	g_hash_table_destroy  (mondirs_ht);
-	pthread_spin_destroy  (&queue_lock);
-	pthread_cond_destroy  (&gio_cond_gotevent);
-	pthread_mutex_destroy (&gio_mutex_prefetcher);
-*/
+int gio_deinit ( ctx_t *ctx_p )
+{
+	( void ) ctx_p;
+	/*
+		g_main_loop_quit(gio_loop);
+		g_hash_table_destroy  (mondirs_ht);
+		pthread_spin_destroy  (&queue_lock);
+		pthread_cond_destroy  (&gio_cond_gotevent);
+		pthread_mutex_destroy (&gio_mutex_prefetcher);
+	*/
 	return 0;
 }
 

+ 9 - 9
mon_gio.h

@@ -1,25 +1,25 @@
 /*
     clsync - file tree sync utility based on gio/kqueue/bsm/gio
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int gio_wait(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
-extern int gio_handle(struct ctx *ctx_p, struct indexes *indexes_p);
-extern int gio_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
-extern int gio_init(ctx_t *ctx_p);
-extern int gio_deinit(ctx_t *ctx_p);
+extern int gio_wait ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p );
+extern int gio_handle ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern int gio_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
+extern int gio_init ( ctx_t *ctx_p );
+extern int gio_deinit ( ctx_t *ctx_p );
 

+ 74 - 79
mon_inotify.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -35,123 +35,116 @@ struct recognize_event_return {
 	eventobjtype_t objtype_new;
 };
 
-static inline void recognize_event(struct recognize_event_return *r, uint32_t event) {
+static inline void recognize_event ( struct recognize_event_return *r, uint32_t event )
+{
 	eventobjtype_t type;
 	int is_created;
 	int is_deleted;
-
-	type = (event & IN_ISDIR ? EOT_DIR : EOT_FILE);
-	is_created = event & (IN_CREATE|IN_MOVED_TO);
-	is_deleted = event & (IN_DELETE_SELF|IN_DELETE|IN_MOVED_FROM);
-
-	debug(4, "type == %x; is_created == %x; is_deleted == %x", type, is_created, is_deleted);
-
-	r->objtype_old = (is_created ? EOT_DOESNTEXIST : type);
-	r->objtype_new = (is_deleted ? EOT_DOESNTEXIST : type);
-
+	type = ( event & IN_ISDIR ? EOT_DIR : EOT_FILE );
+	is_created = event & ( IN_CREATE | IN_MOVED_TO );
+	is_deleted = event & ( IN_DELETE_SELF | IN_DELETE | IN_MOVED_FROM );
+	debug ( 4, "type == %x; is_created == %x; is_deleted == %x", type, is_created, is_deleted );
+	r->objtype_old = ( is_created ? EOT_DOESNTEXIST : type );
+	r->objtype_new = ( is_deleted ? EOT_DOESNTEXIST : type );
 	return;
 }
 
-int inotify_add_watch_dir(ctx_t *ctx_p, indexes_t *indexes_p, const char *const accpath) {
-	(void) indexes_p;
-
-	int inotify_d = (int)(long)ctx_p->fsmondata;
-	return privileged_inotify_add_watch(inotify_d, accpath, INOTIFY_MARKMASK, PC_INOTIFY_ADD_WATCH_DIR);
+int inotify_add_watch_dir ( ctx_t *ctx_p, indexes_t *indexes_p, const char *const accpath )
+{
+	( void ) indexes_p;
+	int inotify_d = ( int ) ( long ) ctx_p->fsmondata;
+	return privileged_inotify_add_watch ( inotify_d, accpath, INOTIFY_MARKMASK, PC_INOTIFY_ADD_WATCH_DIR );
 }
 
-int inotify_wait(ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p) {
-	(void) indexes_p;
-
-	int inotify_d = (int)(long)ctx_p->fsmondata;
-
-	debug(3, "select with timeout %li secs (fd == %u).", tv_p->tv_sec, inotify_d);
+int inotify_wait ( ctx_t *ctx_p, struct indexes *indexes_p, struct timeval *tv_p )
+{
+	( void ) indexes_p;
+	int inotify_d = ( int ) ( long ) ctx_p->fsmondata;
+	debug ( 3, "select with timeout %li secs (fd == %u).", tv_p->tv_sec, inotify_d );
 	fd_set rfds;
-	FD_ZERO(&rfds);
-	FD_SET(inotify_d, &rfds);
-	return select(inotify_d+1, &rfds, NULL, NULL, tv_p);
+	FD_ZERO ( &rfds );
+	FD_SET ( inotify_d, &rfds );
+	return select ( inotify_d + 1, &rfds, NULL, NULL, tv_p );
 }
 
 #define INOTIFY_HANDLE_CONTINUE {\
-	ptr += sizeof(struct inotify_event) + event->len;\
-	count++;\
-	continue;\
-}
-
-int inotify_handle(ctx_t *ctx_p, indexes_t *indexes_p) {
-	static struct timeval tv={0};
-	int inotify_d = (int)(long)ctx_p->fsmondata;
+		ptr += sizeof(struct inotify_event) + event->len;\
+		count++;\
+		continue;\
+	}
 
+int inotify_handle ( ctx_t *ctx_p, indexes_t *indexes_p )
+{
+	static struct timeval tv = {0};
+	int inotify_d = ( int ) ( long ) ctx_p->fsmondata;
 	int count = 0;
-
 	fd_set rfds;
-	FD_ZERO(&rfds);
-	FD_SET(inotify_d, &rfds);
-
+	FD_ZERO ( &rfds );
+	FD_SET ( inotify_d, &rfds );
 	char   *path_rel	= NULL;
 	size_t  path_rel_len	= 0;
 	char   *path_full	= NULL;
 	size_t  path_full_size	= 0;
-	while (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
 
+	while ( select ( FD_SETSIZE, &rfds, NULL, NULL, &tv ) ) {
 		char buf[BUFSIZ + 1];
-		size_t r = read(inotify_d, buf, BUFSIZ);
-		if (r <= 0) {
-			error("Got error while reading events from inotify with read().");
+		size_t r = read ( inotify_d, buf, BUFSIZ );
+
+		if ( r <= 0 ) {
+			error ( "Got error while reading events from inotify with read()." );
 			count = -1;
 			goto l_inotify_handle_end;
 		}
 
 #ifdef PARANOID
-		g_hash_table_remove_all(indexes_p->fpath2ei_ht);
+		g_hash_table_remove_all ( indexes_p->fpath2ei_ht );
 #endif
-
 		char *ptr =  buf;
 		char *end = &buf[r];
-		while (ptr < end) {
-			struct inotify_event *event = (struct inotify_event *)ptr;
+
+		while ( ptr < end ) {
+			struct inotify_event *event = ( struct inotify_event * ) ptr;
 
 			// Removing stale wd-s
 
-			if (event->mask & IN_IGNORED) {
-				debug(2, "Cleaning up info about watch descriptor %i.", event->wd);
-				indexes_remove_bywd(indexes_p, event->wd);
+			if ( event->mask & IN_IGNORED ) {
+				debug ( 2, "Cleaning up info about watch descriptor %i.", event->wd );
+				indexes_remove_bywd ( indexes_p, event->wd );
 				INOTIFY_HANDLE_CONTINUE;
 			}
 
 			// Getting path
+			char *fpath = indexes_wd2fpath ( indexes_p, event->wd );
 
-			char *fpath = indexes_wd2fpath(indexes_p, event->wd);
-
-			if (fpath == NULL) {
-				debug(2, "Event %p on stale watch (wd: %i).", (void *)(long)event->mask, event->wd);
+			if ( fpath == NULL ) {
+				debug ( 2, "Event %p on stale watch (wd: %i).", ( void * ) ( long ) event->mask, event->wd );
 				INOTIFY_HANDLE_CONTINUE;
 			}
-			debug(2, "Event %p on \"%s\" (wd: %i; fpath: \"%s\").", (void *)(long)event->mask, event->len>0?event->name:"", event->wd, fpath);
 
+			debug ( 2, "Event %p on \"%s\" (wd: %i; fpath: \"%s\").", ( void * ) ( long ) event->mask, event->len > 0 ? event->name : "", event->wd, fpath );
 			// Getting full path
+			size_t path_full_memreq = strlen ( fpath ) + event->len + 2;
 
-			size_t path_full_memreq = strlen(fpath) + event->len + 2;
-			if (path_full_size < path_full_memreq) {
-				path_full      = xrealloc(path_full, path_full_memreq);
+			if ( path_full_size < path_full_memreq ) {
+				path_full      = xrealloc ( path_full, path_full_memreq );
 				path_full_size = path_full_memreq;
 			}
 
-			if (event->len>0)
-				sprintf(path_full, "%s/%s", fpath, event->name);
+			if ( event->len > 0 )
+				sprintf ( path_full, "%s/%s", fpath, event->name );
 			else
-				sprintf(path_full, "%s", fpath);
+				sprintf ( path_full, "%s", fpath );
 
 			// Getting infomation about file/dir/etc
-
 			struct  recognize_event_return r = {0};
-			recognize_event(&r, event->mask);
-
+			recognize_event ( &r, event->mask );
 			stat64_t lstat, *lstat_p;
 			mode_t st_mode;
 			size_t st_size;
-			if ((r.objtype_new == EOT_DOESNTEXIST) || (ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT) || privileged_lstat64(path_full, &lstat, PC_MON_HANDLE_LSTAT64)) {
-				debug(2, "Cannot lstat64(\"%s\", lstat). Seems, that the object had been deleted (%i) or option \"--cancel-syscalls mon_stat\" (%i) is set.", path_full, r.objtype_new == EOT_DOESNTEXIST, ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT);
-				st_mode = (event->mask & IN_ISDIR ? S_IFDIR : S_IFREG);
+
+			if ( ( r.objtype_new == EOT_DOESNTEXIST ) || ( ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT ) || privileged_lstat64 ( path_full, &lstat, PC_MON_HANDLE_LSTAT64 ) ) {
+				debug ( 2, "Cannot lstat64(\"%s\", lstat). Seems, that the object had been deleted (%i) or option \"--cancel-syscalls mon_stat\" (%i) is set.", path_full, r.objtype_new == EOT_DOESNTEXIST, ctx_p->flags[CANCEL_SYSCALLS]&CSC_MON_STAT );
+				st_mode = ( event->mask & IN_ISDIR ? S_IFDIR : S_IFREG );
 				st_size = 0;
 				lstat_p = NULL;
 			} else {
@@ -160,7 +153,7 @@ int inotify_handle(ctx_t *ctx_p, indexes_t *indexes_p) {
 				lstat_p = &lstat;
 			}
 
-			if (sync_prequeue_loadmark(1, ctx_p, indexes_p, path_full, NULL, lstat_p, r.objtype_old, r.objtype_new, event->mask, event->wd, st_mode, st_size, &path_rel, &path_rel_len, NULL)) {
+			if ( sync_prequeue_loadmark ( 1, ctx_p, indexes_p, path_full, NULL, lstat_p, r.objtype_old, r.objtype_new, event->mask, event->wd, st_mode, st_size, &path_rel, &path_rel_len, NULL ) ) {
 				count = -1;
 				goto l_inotify_handle_end;
 			}
@@ -170,22 +163,24 @@ int inotify_handle(ctx_t *ctx_p, indexes_t *indexes_p) {
 
 		// Globally queueing captured events:
 		// Moving events from local queue to global ones
-		sync_prequeue_unload(ctx_p, indexes_p);
+		sync_prequeue_unload ( ctx_p, indexes_p );
 	}
 
 l_inotify_handle_end:
-	if (path_full != NULL)
-		free(path_full);
 
-	if (path_rel != NULL)
-		free(path_rel);
+	if ( path_full != NULL )
+		free ( path_full );
+
+	if ( path_rel != NULL )
+		free ( path_rel );
 
 	return count;
 }
 
-int inotify_deinit(ctx_t *ctx_p) {
-	int inotify_d = (int)(long)ctx_p->fsmondata;
-	debug(3, "Closing inotify_d");
-	return close(inotify_d);
+int inotify_deinit ( ctx_t *ctx_p )
+{
+	int inotify_d = ( int ) ( long ) ctx_p->fsmondata;
+	debug ( 3, "Closing inotify_d" );
+	return close ( inotify_d );
 }
 

+ 8 - 8
mon_inotify.h

@@ -1,24 +1,24 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int inotify_wait(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
-extern int inotify_handle(struct ctx *ctx_p, struct indexes *indexes_p);
-extern int inotify_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
-extern int inotify_deinit(ctx_t *ctx_p);
+extern int inotify_wait ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p );
+extern int inotify_handle ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern int inotify_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
+extern int inotify_deinit ( ctx_t *ctx_p );
 

Разница между файлами не показана из-за своего большого размера
+ 350 - 342
mon_kqueue.c


+ 8 - 8
mon_kqueue.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2014  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -40,8 +40,8 @@
 #endif
 
 extern int kqueue_init();
-extern int kqueue_add_watch_dir(struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath);
-extern int kqueue_wait(struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p);
-extern int kqueue_handle(struct ctx *ctx_p, struct indexes *indexes_p);
-extern int kqueue_deinit(ctx_t *ctx_p);
+extern int kqueue_add_watch_dir ( struct ctx *ctx_p, struct indexes *indexes_p, const char *const accpath );
+extern int kqueue_wait ( struct ctx *ctx_p, struct indexes *indexes_p, struct timeval *tv_p );
+extern int kqueue_handle ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern int kqueue_deinit ( ctx_t *ctx_p );
 

+ 21 - 22
port-hacks.h

@@ -35,35 +35,34 @@
 #	include <pthread.h>
 
 #	ifdef THREADING_SUPPORT
-	static inline int pthread_tryjoin_np(pthread_t thread, void **retval) {
-		struct timespec abstime;
-		int rc;
-
-		abstime.tv_sec  = 0;
-		abstime.tv_nsec = 0;
-
-		extern int pthread_timedjoin_np(pthread_t thread, void **value_ptr, const struct timespec *abstime);
-
-		rc = pthread_timedjoin_np(thread, retval, &abstime);
-
-		if (rc == ETIMEDOUT)
-			rc = EBUSY;
-
-		return rc;
-	}
+static inline int pthread_tryjoin_np ( pthread_t thread, void **retval )
+{
+	struct timespec abstime;
+	int rc;
+	abstime.tv_sec  = 0;
+	abstime.tv_nsec = 0;
+	extern int pthread_timedjoin_np ( pthread_t thread, void **value_ptr, const struct timespec * abstime );
+	rc = pthread_timedjoin_np ( thread, retval, &abstime );
+
+	if ( rc == ETIMEDOUT )
+		rc = EBUSY;
+
+	return rc;
+}
 #	endif
 
 #	ifndef __USE_LARGEFILE64
-	typedef struct stat stat64_t;
-	static inline int lstat64(const char *pathname, struct stat *buf) {
-		return lstat(pathname, buf);
-	}
+typedef struct stat stat64_t;
+static inline int lstat64 ( const char *pathname, struct stat *buf )
+{
+	return lstat ( pathname, buf );
+}
 #	else
-	typedef struct stat64 stat64_t;
+typedef struct stat64 stat64_t;
 #	endif
 
 #else
-	typedef struct stat64 stat64_t;
+typedef struct stat64 stat64_t;
 #endif
 
 #ifdef CLSYNC_ITSELF

+ 33 - 27
posix-hacks.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue/bsm/gio
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -28,65 +28,71 @@
 
 #define __POSIX_HACKS_C
 
-int reserved_fd[FOPEN_MAX+1] = {-1};
+int reserved_fd[FOPEN_MAX + 1] = { -1};
 int reserved_fd_used;
 
 
-static inline int reserve_fdpair(int idx) {
+static inline int reserve_fdpair ( int idx )
+{
 	int pipe_fds[2];
 
-	if (pipe2(pipe_fds, O_CLOEXEC|O_NONBLOCK))
+	if ( pipe2 ( pipe_fds, O_CLOEXEC | O_NONBLOCK ) )
 		return errno;
 
 	reserved_fd[ idx     ] = pipe_fds[0];
 	reserved_fd[ idx + 1 ] = pipe_fds[1];
-
 	return 0;
 }
 
-int posixhacks_init() {
+int posixhacks_init()
+{
 	int i;
-
 	// Reserving file descriptors from start to bypass FOPEN_MAX limit on fopen()/fdopen()
 	i = 0;
-	while (i < (FOPEN_MAX+1)/2) {
-		if (reserve_fdpair (i<<1))
+
+	while ( i < ( FOPEN_MAX + 1 ) / 2 ) {
+		if ( reserve_fdpair ( i << 1 ) )
 			return errno;
+
 		i++;
 	}
-	reserved_fd_used = 0;
 
+	reserved_fd_used = 0;
 	return 0;
 }
 
-FILE *posixhacks_fopen(const char *path, const char *mode) {
-	close(reserved_fd[reserved_fd_used++]);
-	return fopen(path, mode);
+FILE *posixhacks_fopen ( const char *path, const char *mode )
+{
+	close ( reserved_fd[reserved_fd_used++] );
+	return fopen ( path, mode );
 }
 
-int posixhacks_fclose(FILE *fp) {
+int posixhacks_fclose ( FILE *fp )
+{
 	int rc;
 	int pipe_fds[2];
-
-	rc = fclose(fp);
+	rc = fclose ( fp );
 
 	// reserving the file descriptor
-	if (!(reserved_fd_used&1))
-		close(reserved_fd[reserved_fd_used++]);
+	if ( ! ( reserved_fd_used & 1 ) )
+		close ( reserved_fd[reserved_fd_used++] );
+
 	reserved_fd_used -= 2;
-	if (reserve_fdpair (reserved_fd_used))
+
+	if ( reserve_fdpair ( reserved_fd_used ) )
 		return errno;
 
 	return rc;
 }
 
-int posixhacks_deinit() {
+int posixhacks_deinit()
+{
 	int i;
-
 	i = 0;
-	while (i < (FOPEN_MAX+1)/2) {
-		close(reserved_fd[ (i<<1)     ]);
-		close(reserved_fd[ (i<<1) + 1 ]);
+
+	while ( i < ( FOPEN_MAX + 1 ) / 2 ) {
+		close ( reserved_fd[ ( i << 1 )     ] );
+		close ( reserved_fd[ ( i << 1 ) + 1 ] );
 		i++;
 	}
 

+ 6 - 6
posix-hacks.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue/bsm/gio
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -27,8 +27,8 @@
 
 
 extern int posixhacks_init();
-extern FILE *posixhacks_fopen(const char *path, const char *mode);
-extern int posixhacks_fclose(FILE *fp);
+extern FILE *posixhacks_fopen ( const char *path, const char *mode );
+extern int posixhacks_fclose ( FILE *fp );
 extern int posixhacks_deinit();
 #else
 #	define posixhacks_init() (0)

Разница между файлами не показана из-за своего большого размера
+ 1016 - 982
privileged.c


+ 47 - 47
privileged.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -40,58 +40,58 @@ enum priv_callid {
 	PC_MAX
 };
 
-extern int (*_privileged_lstat64)		(
-		const char *path, stat64_t *buf
+extern int ( *_privileged_lstat64 )		(
+    const char *path, stat64_t *buf
 # ifdef HL_LOCK_TRIES_AUTO
-		, int callid
+    , int callid
 # endif
-	);
+);
 
-extern FTS *(*_privileged_fts_open)		(
-		char * const *path_argv,
-		int options,
-		int (*compar)(const FTSENT **, const FTSENT **)
+extern FTS * ( *_privileged_fts_open )		(
+    char * const *path_argv,
+    int options,
+    int ( *compar ) ( const FTSENT **, const FTSENT ** )
 # ifdef HL_LOCK_TRIES_AUTO
-		, int callid
+    , int callid
 # endif
-	);
+);
 
-extern FTSENT *(*_privileged_fts_read)		(
-		FTS *ftsp
+extern FTSENT * ( *_privileged_fts_read )		(
+    FTS *ftsp
 # ifdef HL_LOCK_TRIES_AUTO
-		, int callid
+    , int callid
 # endif
-	);
+);
 
-extern int (*_privileged_fts_close)		(
-		FTS *ftsp
+extern int ( *_privileged_fts_close )		(
+    FTS *ftsp
 # ifdef HL_LOCK_TRIES_AUTO
-		, int callid
+    , int callid
 # endif
-	);
+);
 
-extern int (*_privileged_inotify_init)		();
-extern int (*_privileged_inotify_init1)		(int flags);
+extern int ( *_privileged_inotify_init )		();
+extern int ( *_privileged_inotify_init1 )		( int flags );
 
-extern int (*_privileged_inotify_add_watch)	(
-		int fd,
-		const char *pathname,
-		uint32_t mask
+extern int ( *_privileged_inotify_add_watch )	(
+    int fd,
+    const char *pathname,
+    uint32_t mask
 # ifdef HL_LOCK_TRIES_AUTO
-		, int callid
+    , int callid
 # endif
-	);
+);
 
-extern int (*_privileged_inotify_rm_watch)	(
-		int fd,
-		int wd
-	);
+extern int ( *_privileged_inotify_rm_watch )	(
+    int fd,
+    int wd
+);
 
 #ifdef CGROUP_SUPPORT
-extern int (*_privileged_clsync_cgroup_deinit)	(ctx_t *ctx_p);
+extern int ( *_privileged_clsync_cgroup_deinit )	( ctx_t *ctx_p );
 #endif
 
-extern pid_t (*_privileged_waitpid)		(pid_t pid, int *status, int options);
+extern pid_t ( *_privileged_waitpid )		( pid_t pid, int *status, int options );
 
 extern int privileged_check();
 
@@ -133,20 +133,20 @@ extern int privileged_check();
 # define privileged_waitpid			waitpid
 #endif
 
-extern int (*_privileged_kill_child)(
-		pid_t pid,
-		int   sig,
-		char  ignoreerrors
-	);
+extern int ( *_privileged_kill_child ) (
+    pid_t pid,
+    int   sig,
+    char  ignoreerrors
+);
 
-extern int (*_privileged_fork_execvp)(
-		const char *file,
-		char *const argv[]
-	);
+extern int ( *_privileged_fork_execvp ) (
+    const char *file,
+    char *const argv[]
+);
 
 #define privileged_kill_child			_privileged_kill_child
 #define privileged_fork_execvp			_privileged_fork_execvp
 
-extern int privileged_init(struct ctx *ctx_p);
-extern int privileged_deinit(struct ctx *ctx_p);
+extern int privileged_init ( struct ctx *ctx_p );
+extern int privileged_deinit ( struct ctx *ctx_p );
 

+ 32 - 29
pthreadex.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -22,56 +22,59 @@
 #include "pthreadex.h"
 #include "malloc.h"
 
-int pthread_mutex_init_shared(pthread_mutex_t **mutex_p) {
+int pthread_mutex_init_shared ( pthread_mutex_t **mutex_p )
+{
 	static pthread_mutex_t mutex_initial = PTHREAD_MUTEX_INITIALIZER;
-	*mutex_p = shm_malloc_try(sizeof(**mutex_p));
-	memcpy(*mutex_p, &mutex_initial, sizeof(mutex_initial));
-
+	*mutex_p = shm_malloc_try ( sizeof ( **mutex_p ) );
+	memcpy ( *mutex_p, &mutex_initial, sizeof ( mutex_initial ) );
 	pthread_mutexattr_t attr;
-	pthread_mutexattr_init(&attr);
-	pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
-	return pthread_mutex_init(*mutex_p, &attr);
+	pthread_mutexattr_init ( &attr );
+	pthread_mutexattr_setpshared ( &attr, PTHREAD_PROCESS_SHARED );
+	return pthread_mutex_init ( *mutex_p, &attr );
 }
 
-int pthread_mutex_destroy_shared(pthread_mutex_t *mutex_p) {
+int pthread_mutex_destroy_shared ( pthread_mutex_t *mutex_p )
+{
 	int rc;
-	rc = pthread_mutex_destroy(mutex_p);
-	shm_free(mutex_p);
+	rc = pthread_mutex_destroy ( mutex_p );
+	shm_free ( mutex_p );
 	return rc;
 }
 
-int pthread_cond_init_shared(pthread_cond_t **cond_p) {
+int pthread_cond_init_shared ( pthread_cond_t **cond_p )
+{
 	static pthread_cond_t cond_initial = PTHREAD_COND_INITIALIZER;
-	*cond_p = shm_malloc(sizeof(**cond_p));
-	memcpy(*cond_p, &cond_initial, sizeof(cond_initial));
-
+	*cond_p = shm_malloc ( sizeof ( **cond_p ) );
+	memcpy ( *cond_p, &cond_initial, sizeof ( cond_initial ) );
 	pthread_condattr_t attr;
-	pthread_condattr_init(&attr);
-	pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
-	return pthread_cond_init(*cond_p, &attr);
+	pthread_condattr_init ( &attr );
+	pthread_condattr_setpshared ( &attr, PTHREAD_PROCESS_SHARED );
+	return pthread_cond_init ( *cond_p, &attr );
 }
 
-int pthread_cond_destroy_shared(pthread_cond_t *cond_p) {
+int pthread_cond_destroy_shared ( pthread_cond_t *cond_p )
+{
 	int rc;
-	rc = pthread_cond_destroy(cond_p);
-	shm_free(cond_p);
+	rc = pthread_cond_destroy ( cond_p );
+	shm_free ( cond_p );
 	return rc;
 }
 
-int pthread_mutex_reltimedlock(pthread_mutex_t *mutex_p, long tv_sec, long tv_nsec) {
+int pthread_mutex_reltimedlock ( pthread_mutex_t *mutex_p, long tv_sec, long tv_nsec )
+{
 	struct timespec abs_time;
 
-	if (clock_gettime(CLOCK_REALTIME, &abs_time))
+	if ( clock_gettime ( CLOCK_REALTIME, &abs_time ) )
 		return -1;
 
 	abs_time.tv_sec  += tv_sec;
 	abs_time.tv_nsec += tv_nsec;
 
-	if (abs_time.tv_nsec > 1000*1000*1000) {
+	if ( abs_time.tv_nsec > 1000 * 1000 * 1000 ) {
 		abs_time.tv_sec++;
-		abs_time.tv_nsec -= 1000*1000*1000;
+		abs_time.tv_nsec -= 1000 * 1000 * 1000;
 	}
 
-	return pthread_mutex_timedlock(mutex_p, &abs_time);
+	return pthread_mutex_timedlock ( mutex_p, &abs_time );
 }
 

+ 9 - 9
pthreadex.h

@@ -1,27 +1,27 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
 #include <pthread.h>
 
-extern int pthread_mutex_init_shared(pthread_mutex_t **mutex_p);
-extern int pthread_mutex_destroy_shared(pthread_mutex_t *mutex_p);
-extern int pthread_cond_init_shared(pthread_cond_t **cond_p);
-extern int pthread_cond_destroy_shared(pthread_cond_t *cond_p);
-extern int pthread_mutex_reltimedlock(pthread_mutex_t *mutex_p, long tv_sec, long tv_nsec);
+extern int pthread_mutex_init_shared ( pthread_mutex_t **mutex_p );
+extern int pthread_mutex_destroy_shared ( pthread_mutex_t *mutex_p );
+extern int pthread_cond_init_shared ( pthread_cond_t **cond_p );
+extern int pthread_cond_destroy_shared ( pthread_cond_t *cond_p );
+extern int pthread_mutex_reltimedlock ( pthread_mutex_t *mutex_p, long tv_sec, long tv_nsec );
 

+ 163 - 146
rules.c

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -24,192 +24,205 @@
 #include "rules.h"
 #include "error.h"
 
-int rule_complete(rule_t *rule_p, char *expr, size_t *rules_count_p) {
-	debug(3, "<%s>.", expr);
+int rule_complete ( rule_t *rule_p, char *expr, size_t *rules_count_p )
+{
+	debug ( 3, "<%s>.", expr );
 #ifdef VERYPARANOID
-	if (rule_p->mask == RA_NONE) {
-		error("Received a rule with rule_p->mask == 0x00. Exit.");
+
+	if ( rule_p->mask == RA_NONE ) {
+		error ( "Received a rule with rule_p->mask == 0x00. Exit." );
 		return EINVAL;
 	}
-#endif
 
+#endif
 	char buf[BUFSIZ];
 	int ret = 0;
 
-	if (rule_p->num >= MAXRULES) {
-		error("Too many rules (%i >= %i).", rule_p->num, MAXRULES);
+	if ( rule_p->num >= MAXRULES ) {
+		error ( "Too many rules (%i >= %i).", rule_p->num, MAXRULES );
 		return ENOMEM;
 	}
 
-	if ((ret = regcomp(&rule_p->expr,       expr, REG_EXTENDED | REG_NOSUB))) {
-		regerror(ret, &rule_p->expr, buf, BUFSIZ);
-		error("Invalid regexp pattern <%s>: %s (regex-errno: %i).", expr, buf, ret);
+	if ( ( ret = regcomp ( &rule_p->expr,       expr, REG_EXTENDED | REG_NOSUB ) ) ) {
+		regerror ( ret, &rule_p->expr, buf, BUFSIZ );
+		error ( "Invalid regexp pattern <%s>: %s (regex-errno: %i).", expr, buf, ret );
 		return ret;
 	}
 
-	(*rules_count_p)++;
+	( *rules_count_p )++;
 	return ret;
 }
 
-int parse_rules_fromfile(ctx_t *ctx_p) {
+int parse_rules_fromfile ( ctx_t *ctx_p )
+{
 	int ret = 0;
 	char *rulfpath = ctx_p->rulfpath;
 	rule_t *rules  = ctx_p->rules;
 	size_t *rules_count_p = &ctx_p->rules_count;
+	char *line_buf = NULL;
+	FILE *f = fopen ( rulfpath, "r" );
 
-	char *line_buf=NULL;
-	FILE *f = fopen(rulfpath, "r");
-	
-	if(f == NULL) {
+	if ( f == NULL ) {
 		rules->mask   = RA_NONE;		// Terminator. End of rules' chain.
 		rules->perm   = DEFAULT_RULES_PERM;
-		error("Cannot open \"%s\" for reading.", rulfpath);
+		error ( "Cannot open \"%s\" for reading.", rulfpath );
 		return errno;
 	}
 
-	GHashTable *autowrules_ht = g_hash_table_new_full(g_str_hash,	g_str_equal,	free,    0);
-
-	int i=0;
+	GHashTable *autowrules_ht = g_hash_table_new_full ( g_str_hash,	g_str_equal,	free,    0 );
+	int i = 0;
 	ssize_t linelen;
-	 size_t size=0;
-	while((linelen = getline(&line_buf, &size, f)) != -1) {
-		if(linelen>1) {
+	size_t size = 0;
+
+	while ( ( linelen = getline ( &line_buf, &size, f ) ) != -1 ) {
+		if ( linelen > 1 ) {
 			uint8_t sign = 0;
 			char *line = line_buf;
 			rule_t *rule;
-
 			rule = &rules[i];
 #ifdef VERYPARANOID
-			memset(rule, 0, sizeof(*rule));
+			memset ( rule, 0, sizeof ( *rule ) );
 #endif
 			rule->num = i++;
-			line[--linelen] = 0; 
-
+			line[--linelen] = 0;
 
 			// Parsing the first character of the line
-			switch(*line) {
+			switch ( *line ) {
 				case '+':
 					sign = RS_PERMIT;
 					break;
+
 				case '-':
 					sign = RS_REJECT;
 					break;
+
 				case '#':	// Comment?
 					i--;	// Canceling new rule
 					continue;
+
 				default:
-					error("Wrong rule action <%c>.", *line);
+					error ( "Wrong rule action <%c>.", *line );
 					return EINVAL;
 			}
 
 			line++;
 			linelen--;
-
 			// Parsing the second character of the line
 			*line |= 0x20;	// lower-casing
 			// Default rule->mask and rule->perm
-
 			// rule->mask - sets bitmask of operations that are affected by the rule
 			// rule->perm - sets bitmask of permit/reject for every operation. Effect have only bits specified by the rule->mask.
-
 			rule->mask = RA_ALL;
-			switch(sign) {
+
+			switch ( sign ) {
 				case RS_REJECT:
 					rule->perm = RA_NONE;
 					break;
+
 				case RS_PERMIT:
 					rule->perm = RA_ALL;
 					break;
 			}
 
-			switch(*line) {
+			switch ( *line ) {
 				case '*':
 					rule->objtype = 0;	// "0" - means "of any type"
 					break;
 #ifdef DETAILED_FTYPE
+
 				case 's':
 					rule->objtype = S_IFSOCK;
 					break;
+
 				case 'l':
 					rule->objtype = S_IFLNK;
 					break;
+
 				case 'b':
 					rule->objtype = S_IFBLK;
 					break;
+
 				case 'c':
 					rule->objtype = S_IFCHR;
 					break;
+
 				case 'p':
 					rule->objtype = S_IFIFO;
 					break;
 #endif
+
 				case 'f':
 					rule->objtype = S_IFREG;
 					break;
+
 				case 'd':
 					rule->objtype = S_IFDIR;
 					break;
+
 				case 'W':
 				case 'w':
 				case 'm':
 				case 's':
 					if (
-						(ctx_p->flags[MODE] == MODE_RSYNCDIRECT) ||
-						(ctx_p->flags[MODE] == MODE_RSYNCSHELL)  ||
-						(ctx_p->flags[MODE] == MODE_RSYNCSO)
+					    ( ctx_p->flags[MODE] == MODE_RSYNCDIRECT ) ||
+					    ( ctx_p->flags[MODE] == MODE_RSYNCSHELL )  ||
+					    ( ctx_p->flags[MODE] == MODE_RSYNCSO )
 					)
-						warning("Used \"w\" or/and \"m\" or/and \"s\" rule in rsync \"--monitor\" case."
-							" This may cause unexpected problems.");
+						warning ( "Used \"w\" or/and \"m\" or/and \"s\" rule in rsync \"--monitor\" case."
+						          " This may cause unexpected problems." );
 
 					rule->objtype = S_IFDIR;
-					switch (*line) {
+
+					switch ( *line ) {
 						case 'W':
-							rule->mask    = RA_WALK|RA_MONITOR;
+							rule->mask    = RA_WALK | RA_MONITOR;
 							break;
+
 						case 'w':
 							rule->mask    = RA_WALK;
 							break;
+
 						case 'm':
 							rule->mask    = RA_MONITOR;
 							break;
+
 						case 's':
 							rule->mask    = RA_SYNC;
 							break;
 					}
+
 					break;
+
 				default:
-					warning("Cannot parse the rule <%s>", &line[-1]);
+					warning ( "Cannot parse the rule <%s>", &line[-1] );
 					i--;	// Canceling new rule
 					continue;
 			}
 
-
 			line++;
 			linelen--;
-
 			// Parsing the rest part of the line
+			debug ( 1, "Rule #%i <%c>[0x%02x 0x%02x] <%c>[0x%04x] pattern <%s> (length: %i).", rule->num, line[-2], rule->perm, rule->mask, line[-1], rule->objtype, line, linelen );
 
-			debug(1, "Rule #%i <%c>[0x%02x 0x%02x] <%c>[0x%04x] pattern <%s> (length: %i).", rule->num, line[-2], rule->perm, rule->mask, line[-1], rule->objtype, line, linelen);
-			if((ret=rule_complete(rule, line, rules_count_p)))
+			if ( ( ret = rule_complete ( rule, line, rules_count_p ) ) )
 				goto l_parse_rules_fromfile_end;
 
 			// Post-processing:
-
 			line--;
 			linelen++;
-
 #ifdef AUTORULESW
-			if(*line != 'w') {
+
+			if ( *line != 'w' ) {
 				// processing --auto-add-rules-w
-				if(ctx_p->flags[AUTORULESW] && (sign == RS_PERMIT)) {
+				if ( ctx_p->flags[AUTORULESW] && ( sign == RS_PERMIT ) ) {
 					// Preparing to add appropriate w-rules
 					char skip = 0;
-					char *expr = alloca(linelen+2);
-					memcpy(expr, line, linelen+1);
+					char *expr = alloca ( linelen + 2 );
+					memcpy ( expr, line, linelen + 1 );
 					size_t exprlen = linelen;
 
 					// Making expr to be starting with '^'
-					if(line[1] == '^') {
+					if ( line[1] == '^' ) {
 						expr++;
 						exprlen--;
 					} else
@@ -217,30 +230,30 @@ int parse_rules_fromfile(ctx_t *ctx_p) {
 
 					char *end;
 
-					if(*line == 'd' || *line == '*') {
+					if ( *line == 'd' || *line == '*' ) {
 						// "d" rule already doing what we need, so we can skip the last level
-
 						end = &expr[exprlen];
-						if(end[-1] != '$')
-							*(end++) = '$';
-						*end = 0;
 
+						if ( end[-1] != '$' )
+							* ( end++ ) = '$';
+
+						*end = 0;
 //						debug(3, "Don't adding w-rule for \"%s\" due to [*d]-rule for \"%s\"",
 //							expr, &line[1]);
-						g_hash_table_insert(autowrules_ht, strdup(expr), GINT_TO_POINTER(1));
-
+						g_hash_table_insert ( autowrules_ht, strdup ( expr ), GINT_TO_POINTER ( 1 ) );
 					}
 
-					if(!skip) {
-
+					if ( !skip ) {
 						do {
 							// Decreasing directory level and make the '$' ending
-							end = strrchr(expr, '/');
-							if(end != NULL) {
-								if(end[-1] != '$')
-									*(end++) = '$';
+							end = strrchr ( expr, '/' );
+
+							if ( end != NULL ) {
+								if ( end[-1] != '$' )
+									* ( end++ ) = '$';
+
 								*end = 0;
-								exprlen = (size_t)(end - expr);
+								exprlen = ( size_t ) ( end - expr );
 							} else {
 								expr[1] = '$';
 								expr[2] = 0;
@@ -248,60 +261,57 @@ int parse_rules_fromfile(ctx_t *ctx_p) {
 							}
 
 							// Checking if it not already set
-							if(!g_hash_table_lookup(autowrules_ht, expr)) {
-
+							if ( !g_hash_table_lookup ( autowrules_ht, expr ) ) {
 								// Switching to next rule:
-
 								rule = &rules[i];
 								rule->num = i++;
-
 								// Adding the rule
-
 								rule->objtype = S_IFDIR;
 								rule->mask    = RA_WALK;
 								rule->perm    = RA_WALK;
+								debug ( 1, "Rule #%i <+> <w> pattern <%s> (length: %i) [auto].",
+								        rule->num, expr, exprlen );
 
-								debug(1, "Rule #%i <+> <w> pattern <%s> (length: %i) [auto].", 
-									rule->num, expr, exprlen);
-								if((ret=rule_complete(rule, expr, rules_count_p)))
+								if ( ( ret = rule_complete ( rule, expr, rules_count_p ) ) )
 									goto l_parse_rules_fromfile_end;
-								g_hash_table_insert(autowrules_ht, strdup(expr), GINT_TO_POINTER(1));
 
+								g_hash_table_insert ( autowrules_ht, strdup ( expr ), GINT_TO_POINTER ( 1 ) );
 							}
-						} while (end != NULL);
+						} while ( end != NULL );
 					}
 				}
 			}
+
 #endif
 		}
 	}
 
 l_parse_rules_fromfile_end:
-	if (size)
-		free(line_buf);
-
-	fclose(f);
 
-	debug(3, "Adding tail-rule #%u (effective #%u).", -1, i);
+	if ( size )
+		free ( line_buf );
 
+	fclose ( f );
+	debug ( 3, "Adding tail-rule #%u (effective #%u).", -1, i );
 	rules[i].mask   = RA_NONE;		// Terminator. End of rules' chain.
 	rules[i].perm   = DEFAULT_RULES_PERM;
-
-	g_hash_table_destroy(autowrules_ht);
+	g_hash_table_destroy ( autowrules_ht );
 #ifdef _DEBUG_FORCE
-	debug(3, "Total (p == %p):", rules);
-	i=0;
+	debug ( 3, "Total (p == %p):", rules );
+	i = 0;
+
 	do {
-		debug(4, "\t%i\t%i\t%p/%p", i, rules[i].objtype, (void *)(long)rules[i].perm, (void *)(long)rules[i].mask);
+		debug ( 4, "\t%i\t%i\t%p/%p", i, rules[i].objtype, ( void * ) ( long ) rules[i].perm, ( void * ) ( long ) rules[i].mask );
 		i++;
-	} while(rules[i].mask != RA_NONE);
+	} while ( rules[i].mask != RA_NONE );
+
 #endif
 	return ret;
 }
 
 /**
  * @brief 			Checks file path by rules' expressions (parsed from file)
- * 
+ *
  * @param[in] 	fpath		Path to file of directory
  * @param[in] 	st_mode		st_mode received via *stat() functions
  * @param[in] 	rules_p		Pointer to start of rules array
@@ -309,102 +319,109 @@ l_parse_rules_fromfile_end:
  * @param[i/o] 	rule_pp		Pointer to pointer to rule, where the last search ended. Next search will be started from the specified rule. Can be "NULL" to disable this feature.
  *
  * @retval	perm		Permission bitmask
- * 
+ *
  */
 // Checks file path by rules' expressions (parsed from file)
 // Return: RS_PERMIT or RS_REJECT for the "file path" and specified ruleaction
 
-ruleaction_t rules_search_getperm(const char *fpath, mode_t st_mode, rule_t *rules_p, const ruleaction_t ruleaction, rule_t **rule_pp) {
-	debug(3, "rules_search_getperm(\"%s\", %p, %p, %p, %p)", 
-			fpath, (void *)(unsigned long)st_mode, rules_p,
-			(void *)(long)ruleaction, (void *)(long)rule_pp
-		);
-
+ruleaction_t rules_search_getperm ( const char *fpath, mode_t st_mode, rule_t *rules_p, const ruleaction_t ruleaction, rule_t **rule_pp )
+{
+	debug ( 3, "rules_search_getperm(\"%s\", %p, %p, %p, %p)",
+	        fpath, ( void * ) ( unsigned long ) st_mode, rules_p,
+	        ( void * ) ( long ) ruleaction, ( void * ) ( long ) rule_pp
+	      );
 	int i;
 	i = 0;
 	rule_t *rule_p = rules_p;
 	mode_t ftype = st_mode & S_IFMT;
-
 #ifdef _DEBUG_FORCE
-	debug(3, "Rules (p == %p):", rules_p);
-	i=0;
+	debug ( 3, "Rules (p == %p):", rules_p );
+	i = 0;
+
 	do {
-		debug(3, "\t%i\t%i\t%p/%p", i, rules_p[i].objtype, (void *)(long)rules_p[i].perm, (void *)(long)rules_p[i].mask);
+		debug ( 3, "\t%i\t%i\t%p/%p", i, rules_p[i].objtype, ( void * ) ( long ) rules_p[i].perm, ( void * ) ( long ) rules_p[i].mask );
 		i++;
-	} while (rules_p[i].mask != RA_NONE);
+	} while ( rules_p[i].mask != RA_NONE );
+
 #endif
+	i = 0;
+
+	if ( rule_pp != NULL )
+		if ( *rule_pp != NULL ) {
+			debug ( 3, "Previous position is set." );
 
-        i=0;
-	if (rule_pp != NULL)
-		if (*rule_pp != NULL) {
-			debug(3, "Previous position is set.");
-			if (rule_p->mask == RA_NONE)
+			if ( rule_p->mask == RA_NONE )
 				return rule_p->perm;
 
-			rule_p = ++(*rule_pp);
+			rule_p = ++ ( *rule_pp );
 			i = rule_p->num;
 		}
 
-	debug(3, "Starting from position %i", i);
-	while (rule_p->mask != RA_NONE) {
-		debug(3, "%i -> %p/%p: type compare: %p, %p -> %i", 
-				i,
-				(void *)(long)rule_p->perm, (void *)(long)rule_p->mask,
-				(void *)(unsigned long)ftype, (void *)(unsigned long)rule_p->objtype, 
-				(unsigned char)!(rule_p->objtype && (rule_p->objtype != ftype))
-			);
-
-		if (!(rule_p->mask & ruleaction)) {	// Checking wrong operation type
-			debug(3, "action-mask mismatch. Skipping.");
-			rule_p++;i++;// = &rules_p[++i];
+	debug ( 3, "Starting from position %i", i );
+
+	while ( rule_p->mask != RA_NONE ) {
+		debug ( 3, "%i -> %p/%p: type compare: %p, %p -> %i",
+		        i,
+		        ( void * ) ( long ) rule_p->perm, ( void * ) ( long ) rule_p->mask,
+		        ( void * ) ( unsigned long ) ftype, ( void * ) ( unsigned long ) rule_p->objtype,
+		        ( unsigned char ) ! ( rule_p->objtype && ( rule_p->objtype != ftype ) )
+		      );
+
+		if ( ! ( rule_p->mask & ruleaction ) ) {	// Checking wrong operation type
+			debug ( 3, "action-mask mismatch. Skipping." );
+			rule_p++;
+			i++;// = &rules_p[++i];
 			continue;
 		}
 
-		if (rule_p->objtype && (rule_p->objtype != ftype)) {
-			debug(3, "objtype mismatch. Skipping.");
-			rule_p++;i++;// = &rules_p[++i];
+		if ( rule_p->objtype && ( rule_p->objtype != ftype ) ) {
+			debug ( 3, "objtype mismatch. Skipping." );
+			rule_p++;
+			i++;// = &rules_p[++i];
 			continue;
 		}
 
-		if(!regexec(&rule_p->expr, fpath, 0, NULL, 0))
+		if ( !regexec ( &rule_p->expr, fpath, 0, NULL, 0 ) )
 			break;
 
-		debug(3, "doesn't match regex. Skipping.");
-		rule_p++;i++;// = &rules_p[++i];
-
+		debug ( 3, "doesn't match regex. Skipping." );
+		rule_p++;
+		i++;// = &rules_p[++i];
 	}
 
-	debug(2, "matched to rule #%u for \"%s\":\t%p/%p (queried: %p).", rule_p->mask==RA_NONE?-1:i, fpath, 
-			(void *)(long)rule_p->perm, (void *)(long)rule_p->mask,
-			(void *)(long)ruleaction
-		);
+	debug ( 2, "matched to rule #%u for \"%s\":\t%p/%p (queried: %p).", rule_p->mask == RA_NONE ? -1 : i, fpath,
+	        ( void * ) ( long ) rule_p->perm, ( void * ) ( long ) rule_p->mask,
+	        ( void * ) ( long ) ruleaction
+	      );
 
-	if (rule_pp != NULL)
+	if ( rule_pp != NULL )
 		*rule_pp = rule_p;
 
 	return rule_p->perm;
 }
 
-ruleaction_t rules_getperm(const char *fpath, mode_t st_mode, rule_t *rules_p, ruleaction_t ruleactions) {
+ruleaction_t rules_getperm ( const char *fpath, mode_t st_mode, rule_t *rules_p, ruleaction_t ruleactions )
+{
 	rule_t *rule_p = NULL;
 	ruleaction_t gotpermto  = 0;
 	ruleaction_t resultperm = 0;
-	debug(3, "rules_getperm(\"%s\", %p, %p (#%u), %p)", 
-		fpath, (void *)(long)st_mode, rules_p, rules_p->num, (void *)(long)ruleactions);
+	debug ( 3, "rules_getperm(\"%s\", %p, %p (#%u), %p)",
+	        fpath, ( void * ) ( long ) st_mode, rules_p, rules_p->num, ( void * ) ( long ) ruleactions );
+
+	while ( ( gotpermto & ruleactions ) != ruleactions ) {
+		rules_search_getperm ( fpath, st_mode, rules_p, ruleactions, &rule_p );
 
-	while((gotpermto&ruleactions) != ruleactions) {
-		rules_search_getperm(fpath, st_mode, rules_p, ruleactions, &rule_p);
-		if(rule_p->mask == RA_NONE) { // End of rules' list 
-			resultperm |= rule_p->perm & (gotpermto^RA_ALL);
+		if ( rule_p->mask == RA_NONE ) { // End of rules' list
+			resultperm |= rule_p->perm & ( gotpermto ^ RA_ALL );
 			break;
 		}
-		resultperm |= rule_p->perm & ((gotpermto^rule_p->mask)&rule_p->mask);	// Adding perm bitmask of operations that was unknown before
+
+		resultperm |= rule_p->perm & ( ( gotpermto ^ rule_p->mask ) &rule_p->mask );	// Adding perm bitmask of operations that was unknown before
 		gotpermto  |= rule_p->mask;						// Adding the mask
 	}
 
-	debug(3, "rules_getperm(\"%s\", %p, rules_p, %p): result perm is %p",
-		fpath, (void *)(long)st_mode, (void *)(long)ruleactions, (void *)(long)resultperm);
-
+	debug ( 3, "rules_getperm(\"%s\", %p, rules_p, %p): result perm is %p",
+	        fpath, ( void * ) ( long ) st_mode, ( void * ) ( long ) ruleactions, ( void * ) ( long ) resultperm );
 	return resultperm;
 }
 

+ 7 - 7
rules.h

@@ -1,23 +1,23 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int parse_rules_fromfile(struct ctx *ctx_p);
-extern ruleaction_t rules_search_getperm(const char *fpath, mode_t st_mode, rule_t *rules_p, const ruleaction_t ruleaction, rule_t **rule_pp);
-extern ruleaction_t rules_getperm(const char *fpath, mode_t st_mode, struct rule *rules_p, ruleaction_t ruleactions);
+extern int parse_rules_fromfile ( struct ctx *ctx_p );
+extern ruleaction_t rules_search_getperm ( const char *fpath, mode_t st_mode, rule_t *rules_p, const ruleaction_t ruleaction, rule_t **rule_pp );
+extern ruleaction_t rules_getperm ( const char *fpath, mode_t st_mode, struct rule *rules_p, ruleaction_t ruleactions );
 

Разница между файлами не показана из-за своего большого размера
+ 362 - 353
socket.c


+ 26 - 26
socket.h

@@ -1,18 +1,18 @@
 /*
     clsync - file tree sync utility based on inotify
-    
+
     Copyright (C) 2013  Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
@@ -53,7 +53,7 @@
 struct socket_sockthreaddata;
 struct sockcmd;
 
-typedef int (*clsyncsock_cb_funct_t)(struct socket_sockthreaddata *thread, struct sockcmd *sockcmd_p, void *arg);
+typedef int ( *clsyncsock_cb_funct_t ) ( struct socket_sockthreaddata *thread, struct sockcmd *sockcmd_p, void *arg );
 struct clsynccbqueue {
 	uint64_t		 cmd_num;
 
@@ -70,8 +70,8 @@ struct clsyncsock {
 	uint64_t cmd_num;
 
 	size_t		 cbqueue_len;
-	clsynccbqueue_t  cbqueue[CLSYNCSOCK_WINDOW+1];
-	clsynccbqueue_t *cbqueue_cache[4*CLSYNCSOCK_WINDOW+1];	// It's a hacky hash-table of size "CLSYNCSOCK_WINDOW*2"
+	clsynccbqueue_t  cbqueue[CLSYNCSOCK_WINDOW + 1];
+	clsynccbqueue_t *cbqueue_cache[4 * CLSYNCSOCK_WINDOW + 1];	// It's a hacky hash-table of size "CLSYNCSOCK_WINDOW*2"
 };
 typedef struct clsyncsock clsyncsock_t;
 
@@ -150,13 +150,13 @@ typedef struct sockcmd_dat_invalidcmd sockcmd_dat_invalidcmd_t;
 struct sockcmd_dat_version {
 	int		major;
 	int		minor;
-	char		revision[1<<8];
+	char		revision[1 << 8];
 };
 typedef struct sockcmd_dat_version sockcmd_dat_version_t;
 
 struct sockcmd_dat_info {
-	char		config_block[1<<8];
-	char		label[1<<8];
+	char		config_block[1 << 8];
+	char		label[1 << 8];
 	char		flags[OPTION_FLAGS];
 	char		flags_set[OPTION_FLAGS];
 };
@@ -204,8 +204,8 @@ enum sockauth_id {
 typedef enum sockauth_id sockauth_id_t;
 
 struct socket_sockthreaddata;
-typedef int (*clsyncsock_procfunct_t)(struct socket_sockthreaddata *, sockcmd_t *);
-typedef void (*freefunct_t)(void *);
+typedef int ( *clsyncsock_procfunct_t ) ( struct socket_sockthreaddata *, sockcmd_t * );
+typedef void ( *freefunct_t ) ( void * );
 struct socket_sockthreaddata {
 	int			 id;
 	clsyncsock_procfunct_t	 procfunct;
@@ -220,23 +220,23 @@ struct socket_sockthreaddata {
 };
 typedef struct socket_sockthreaddata socket_sockthreaddata_t;
 
-extern int socket_reply(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, sockcmd_id_t cmd_id, ...);
-extern int socket_send(clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ...);
-extern int socket_send_cb(clsyncsock_t *clsyncsock_p, sockcmd_id_t cmd_id, clsyncsock_cb_funct_t cb, void *cb_arg, ...);
-extern int socket_sendinvalid(clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p);
-extern int socket_recv(clsyncsock_t *clsyncsock, sockcmd_t *sockcmd);
-extern int socket_check_bysock(int sock);
-extern clsyncsock_t *socket_accept(int sock);
-extern int socket_cleanup(clsyncsock_t *clsyncsock_p);
-extern int socket_close(clsyncsock_t *clsyncsock_p);
+extern int socket_reply ( clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p, sockcmd_id_t cmd_id, ... );
+extern int socket_send ( clsyncsock_t *clsyncsock, sockcmd_id_t cmd_id, ... );
+extern int socket_send_cb ( clsyncsock_t *clsyncsock_p, sockcmd_id_t cmd_id, clsyncsock_cb_funct_t cb, void *cb_arg, ... );
+extern int socket_sendinvalid ( clsyncsock_t *clsyncsock_p, sockcmd_t *sockcmd_p );
+extern int socket_recv ( clsyncsock_t *clsyncsock, sockcmd_t *sockcmd );
+extern int socket_check_bysock ( int sock );
+extern clsyncsock_t *socket_accept ( int sock );
+extern int socket_cleanup ( clsyncsock_t *clsyncsock_p );
+extern int socket_close ( clsyncsock_t *clsyncsock_p );
 extern int socket_init();
 extern int socket_deinit();
-extern int socket_procclsyncsock(socket_sockthreaddata_t *arg);
-extern clsyncsock_t *socket_connect_unix(const char *const socket_path);
-extern clsyncsock_t *socket_listen_unix (const char *const socket_path);
+extern int socket_procclsyncsock ( socket_sockthreaddata_t *arg );
+extern clsyncsock_t *socket_connect_unix ( const char *const socket_path );
+extern clsyncsock_t *socket_listen_unix ( const char *const socket_path );
 
-extern socket_sockthreaddata_t *socket_thread_attach(clsyncsock_t *clsyncsock_p);
-extern int socket_thread_start(socket_sockthreaddata_t *threaddata_p);
+extern socket_sockthreaddata_t *socket_thread_attach ( clsyncsock_t *clsyncsock_p );
+extern int socket_thread_start ( socket_sockthreaddata_t *threaddata_p );
 
 extern int clsyncsocks_num;
 extern int clsyncsocks_count;

+ 69 - 53
stringex.c

@@ -24,121 +24,137 @@
 #include "malloc.h"
 #include "error.h"
 
-static int _str_splitargs(
-		char *ptr,
-		char **arg_start_p,
-		int quotes,
-		int (*handler)(char *, size_t, void *),
-		char *additional_arg
-) {
+static int _str_splitargs (
+    char *ptr,
+    char **arg_start_p,
+    int quotes,
+    int ( *handler ) ( char *, size_t, void * ),
+    char *additional_arg
+)
+{
 	char  *arg_start, *arg;
 	size_t arg_len;
 	int rc;
-
-	 arg_start       = *arg_start_p;
+	arg_start       = *arg_start_p;
 	*arg_start_p     = &ptr[1];
-
 	arg_len = ptr - arg_start;
 
-	if (arg_len == 0)  // Skipping nearby spaces
+	if ( arg_len == 0 ) // Skipping nearby spaces
 		return 0;
 
-	arg = xmalloc(arg_len+1);
-	if (quotes) {
+	arg = xmalloc ( arg_len + 1 );
+
+	if ( quotes ) {
 		size_t s, d;
 		s = d = 0;
-		while (s < arg_len) {
-			if (arg_start[s])
+
+		while ( s < arg_len ) {
+			if ( arg_start[s] )
 				arg[d++] = arg_start[s];
+
 			s++;
 		}
+
 		arg_len = d;
 	} else
-		memcpy(arg, arg_start, arg_len);
+		memcpy ( arg, arg_start, arg_len );
 
 #ifdef _DEBUG
-	debug(15, "%p %p %i: <%s>", arg_start, ptr, arg_len, arg);
+	debug ( 15, "%p %p %i: <%s>", arg_start, ptr, arg_len, arg );
 #endif
-
 	arg[arg_len] = 0;
 
-	if ((rc = handler(arg, arg_len, additional_arg))) {
-		free(arg);
+	if ( ( rc = handler ( arg, arg_len, additional_arg ) ) ) {
+		free ( arg );
 		return rc;
 	}
+
 	return 0;
 }
 
-int str_splitargs(
-		char *_instr,
-		int (*handler)(char *, size_t, void *),
-		void *arg
-) {
-	debug(9, "");
+int str_splitargs (
+    char *_instr,
+    int ( *handler ) ( char *, size_t, void * ),
+    void *arg
+)
+{
+	debug ( 9, "" );
 	char *arg_start, *ptr, *instr;
 	int quotes = 0;
-
-	instr     = strdup(_instr);
+	instr     = strdup ( _instr );
 	ptr       = instr;
 	arg_start = instr;
-	while (1) {
-		ptr = strpbrk(ptr, " \t\"\'");
+
+	while ( 1 ) {
+		ptr = strpbrk ( ptr, " \t\"\'" );
 #ifdef _DEBUG
-		debug(10, "ptr == %p", ptr);
+		debug ( 10, "ptr == %p", ptr );
 #endif
-		if (ptr == NULL)
+
+		if ( ptr == NULL )
 			break;
 
 #ifdef _DEBUG
-		debug(10, "*ptr == \"%c\" (%i)", *ptr, *ptr);
+		debug ( 10, "*ptr == \"%c\" (%i)", *ptr, *ptr );
 #endif
-		switch (*(ptr++)) {
+
+		switch ( * ( ptr++ ) ) {
 			case ' ':
 			case '\t': {
-				int rc;
+					int rc;
+
+					if ( ( rc = _str_splitargs ( &ptr[-1], &arg_start, quotes, handler, arg ) ) )
+						return rc;
+
+					quotes = 0;
+					break;
+				}
 
-				if ((rc = _str_splitargs(&ptr[-1], &arg_start, quotes, handler, arg)))
-					return rc;
-				quotes = 0;
-				break;
-			}
 			case '"':
 				ptr[-1] = 0;
 				quotes++;
-				while ((ptr = strchr(ptr, '"')) != NULL) {
+
+				while ( ( ptr = strchr ( ptr, '"' ) ) != NULL ) {
 					// Checking for escaping
 					char *p;
-
 					p = &ptr[-1];
-					while (*p == '\\') {
+
+					while ( *p == '\\' ) {
 						p--;
 #ifdef PARANOID
-						if (p < instr)
-							critical("Dangerous internal error");
+
+						if ( p < instr )
+							critical ( "Dangerous internal error" );
+
 #endif
 					}
 
-					if ((ptr-p)%2)
+					if ( ( ptr - p ) % 2 )
 						break;
 				}
-				if (ptr == NULL) {
+
+				if ( ptr == NULL ) {
 					errno = EINVAL;
-					error("Unterminated quote <\"> in string: <%s>", instr);
+					error ( "Unterminated quote <\"> in string: <%s>", instr );
 					return errno;
 				}
+
 				*ptr = 0;
 				quotes++;
 				ptr++;
 				break;
+
 			case '\'':
 				ptr[-1] = 0;
 				quotes++;
-				ptr = strchr(ptr, '\'');
-				if (ptr == NULL) {
+				ptr = strchr ( ptr, '\'' );
+
+				if ( ptr == NULL ) {
 					errno = EINVAL;
-					error("Unterminated quote <'> in string: <%s>", instr);
+					error ( "Unterminated quote <'> in string: <%s>", instr );
 					return errno;
 				}
+
 				*ptr = 0;
 				quotes++;
 				ptr++;
@@ -146,7 +162,7 @@ int str_splitargs(
 		}
 	}
 
-	int rc = _str_splitargs(strchr(arg_start, 0), &arg_start, quotes, handler, arg);
-	free(instr);
+	int rc = _str_splitargs ( strchr ( arg_start, 0 ), &arg_start, quotes, handler, arg );
+	free ( instr );
 	return rc;
 }

+ 1 - 1
stringex.h

@@ -17,5 +17,5 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-extern int str_splitargs(const char *const instr, int (*handler)(char *outstr, size_t outstr_len, void *arg), void *arg);
+extern int str_splitargs ( const char *const instr, int ( *handler ) ( char *outstr, size_t outstr_len, void *arg ), void *arg );
 

Разница между файлами не показана из-за своего большого размера
+ 2073 - 1870
sync.c


+ 24 - 24
sync.h

@@ -26,7 +26,7 @@ struct thread_callbackfunct_arg {
 };
 typedef struct thread_callbackfunct_arg thread_callbackfunct_arg_t;
 
-typedef int (*thread_callbackfunct_t)(ctx_t *ctx_p, thread_callbackfunct_arg_t *arg_p);
+typedef int ( *thread_callbackfunct_t ) ( ctx_t *ctx_p, thread_callbackfunct_arg_t *arg_p );
 struct threadinfo {
 	int				  thread_num;
 	uint32_t			  iteration;
@@ -65,38 +65,38 @@ struct threadsinfo {
 typedef struct threadsinfo threadsinfo_t;
 
 
-extern int sync_run(struct ctx *ctx);
-extern int sync_dump(struct ctx *ctx, const char *const dest_dir);
-extern int sync_term(int exitcode);
-extern int threads_foreach(int (*funct)(threadinfo_t *, void *), state_t state, void *arg);
+extern int sync_run ( struct ctx *ctx );
+extern int sync_dump ( struct ctx *ctx, const char *const dest_dir );
+extern int sync_term ( int exitcode );
+extern int threads_foreach ( int ( *funct ) ( threadinfo_t *, void * ), state_t state, void *arg );
 extern threadsinfo_t *thread_info();
 extern time_t thread_nextexpiretime();
 extern int sync_prequeue_loadmark
-	(
-		int fsmon_d,
+(
+    int fsmon_d,
 
-		struct ctx     *ctx_p,
-		struct indexes *indexes_p,
+    struct ctx     *ctx_p,
+    struct indexes *indexes_p,
 
-		const char *path_full,
-		const char *path_rel,
+    const char *path_full,
+    const char *path_rel,
 
-		stat64_t *lstat_p,
+    stat64_t *lstat_p,
 
-		eventobjtype_t objtype_old,
-		eventobjtype_t objtype_new,
+    eventobjtype_t objtype_old,
+    eventobjtype_t objtype_new,
 
-		uint32_t event_mask,
-		int      event_wd,
-		mode_t st_mode,
-		off_t  st_size,
+    uint32_t event_mask,
+    int      event_wd,
+    mode_t st_mode,
+    off_t  st_size,
 
-		char  **path_buf_p,
-		size_t *path_buf_len_p,
+    char  **path_buf_p,
+    size_t *path_buf_len_p,
 
-		struct eventinfo *evinfo
-	);
-extern int sync_prequeue_unload(struct ctx *ctx_p, struct indexes *indexes_p);
-extern const char *sync_parameter_get(const char *variable_name, void *_dosync_arg_p);
+    struct eventinfo *evinfo
+);
+extern int sync_prequeue_unload ( struct ctx *ctx_p, struct indexes *indexes_p );
+extern const char *sync_parameter_get ( const char *variable_name, void *_dosync_arg_p );
 extern pthread_t pthread_sighandler;
 

+ 5 - 5
syscalls.c

@@ -1,23 +1,23 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-/* based on busybox's code: 
+/* based on busybox's code:
  *	http://git.busybox.net/busybox/plain/libbb/syscalls.c?h=0_60_stable
  * /
 

+ 15 - 13
syscalls.h

@@ -1,42 +1,44 @@
 /*
     clsync - file tree sync utility based on inotify/kqueue
-    
+
     Copyright (C) 2013-2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
-    
+
     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 3 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, see <http://www.gnu.org/licenses/>.
  */
 
-extern int pivot_root(const char *new_root, const char *old_root);
+extern int pivot_root ( const char *new_root, const char *old_root );
 
-static inline ssize_t read_inf(int fd, void *buf, size_t count) {
+static inline ssize_t read_inf ( int fd, void *buf, size_t count )
+{
 	ssize_t ret;
-
 	errno = 0;
+
 	do {
-		ret = read(fd, buf, count);
-	} while ((ret == -1) && (errno == EINTR));
+		ret = read ( fd, buf, count );
+	} while ( ( ret == -1 ) && ( errno == EINTR ) );
 
 	return ret;
 }
 
-static inline ssize_t write_inf(int fd, const void *buf, size_t count) {
+static inline ssize_t write_inf ( int fd, const void *buf, size_t count )
+{
 	ssize_t ret;
-
 	errno = 0;
+
 	do {
-		ret = write(fd, buf, count);
-	} while ((ret == -1) && (errno == EINTR));
+		ret = write ( fd, buf, count );
+	} while ( ( ret == -1 ) && ( errno == EINTR ) );
 
 	return ret;
 }