redmine

Polishing examples

@@ -2,7 +2,9 @@ ACLOCAL_AMFLAGS = -I m4 @@ -2,7 +2,9 @@ ACLOCAL_AMFLAGS = -I m4
2 SUBDIRS = examples 2 SUBDIRS = examples
3 3
4 if CLSYNC 4 if CLSYNC
5 -bin_PROGRAMS = clsync 5 +bin_PROGRAMS = clsync gencompilerflags
  6 +
  7 +gencompilerflags_SOURCES = gencompilerflags.c
6 8
7 clsync_SOURCES = calc.c cluster.c error.c fileutils.c glibex.c \ 9 clsync_SOURCES = calc.c cluster.c error.c fileutils.c glibex.c \
8 indexes.c main.c malloc.c rules.c stringex.c sync.c \ 10 indexes.c main.c malloc.c rules.c stringex.c sync.c \
@@ -76,7 +78,12 @@ if SOCKET @@ -76,7 +78,12 @@ if SOCKET
76 clsync_SOURCES += socket.c control.c program.h 78 clsync_SOURCES += socket.c control.c program.h
77 endif 79 endif
78 80
79 -main.o: revision.h 81 +gencompilerflags_CFLAGS = $(clsync_CFLAGS)
  82 +
  83 +main.o: compilerflags.h
  84 +
  85 +compilerflags.h: gencompilerflags
  86 + ./gencompilerflags > compilerflags.h
80 87
81 dist_man_MANS = man/man1/clsync.1 88 dist_man_MANS = man/man1/clsync.1
82 endif 89 endif
@@ -108,11 +115,14 @@ libclsync_includedir = $(includedir)/libclsync @@ -108,11 +115,14 @@ libclsync_includedir = $(includedir)/libclsync
108 if CLSYNC 115 if CLSYNC
109 clsync_include_HEADERS = \ 116 clsync_include_HEADERS = \
110 clsync.h \ 117 clsync.h \
  118 + port-hacks.h \
111 configuration.h \ 119 configuration.h \
112 ctx.h \ 120 ctx.h \
113 error.h \ 121 error.h \
114 indexes.h \ 122 indexes.h \
115 - malloc.h 123 + malloc.h \
  124 + compilerflags.h
  125 +
116 if SOCKET 126 if SOCKET
117 clsync_include_HEADERS += \ 127 clsync_include_HEADERS += \
118 socket.h 128 socket.h
@@ -134,7 +144,7 @@ if LIBCLSYNC @@ -134,7 +144,7 @@ if LIBCLSYNC
134 pkgconfig_DATA = pkgconfig/libclsync.pc 144 pkgconfig_DATA = pkgconfig/libclsync.pc
135 endif 145 endif
136 146
137 -CLEANFILES = revision.h 147 +CLEANFILES = compilerflags.h gencompilerflags
138 if CLSYNC 148 if CLSYNC
139 CLEANFILES += examples/rules 149 CLEANFILES += examples/rules
140 clean-local: 150 clean-local:
@@ -24,6 +24,10 @@ @@ -24,6 +24,10 @@
24 #include <stdint.h> 24 #include <stdint.h>
25 #include <sys/types.h> 25 #include <sys/types.h>
26 26
  27 +#ifndef CLSYNC_ITSELF
  28 +# include <clsync/port-hacks.h>
  29 +#endif
  30 +
27 #define CLSYNC_API_VERSION 2 31 #define CLSYNC_API_VERSION 2
28 32
29 enum eventobjtype { 33 enum eventobjtype {
@@ -68,6 +68,8 @@ @@ -68,6 +68,8 @@
68 #include <libgen.h> 68 #include <libgen.h>
69 #include <pthread.h> 69 #include <pthread.h>
70 70
  71 +#define CLSYNC_ITSELF
  72 +
71 #include "configuration.h" 73 #include "configuration.h"
72 #ifdef HAVE_CONFIG_H 74 #ifdef HAVE_CONFIG_H
73 # include "config.h" 75 # include "config.h"
@@ -395,11 +395,5 @@ struct ctx { @@ -395,11 +395,5 @@ struct ctx {
395 }; 395 };
396 typedef struct ctx ctx_t; 396 typedef struct ctx ctx_t;
397 397
398 -  
399 -struct fileinfo {  
400 - stat64_t lstat;  
401 -};  
402 -typedef struct fileinfo fileinfo_t;  
403 -  
404 #endif 398 #endif
405 399
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 #include <errno.h> 3 #include <errno.h>
4 4
5 // Required header: 5 // Required header:
  6 +#include <clsync/compilerflags.h>
6 #include <clsync/clsync.h> 7 #include <clsync/clsync.h>
7 8
8 // Optional headers: 9 // Optional headers:
@@ -10,10 +11,10 @@ @@ -10,10 +11,10 @@
10 #include <clsync/error.h> 11 #include <clsync/error.h>
11 #include <clsync/ctx.h> 12 #include <clsync/ctx.h>
12 13
13 -struct ctx *ctx_p = NULL; 14 +static struct ctx *ctx_p = NULL;
14 -struct indexes *indexes_p = NULL; 15 +static struct indexes *indexes_p = NULL;
15 16
16 -char *argv[11] = {NULL}; 17 +static const char *argv[11] = {NULL};
17 18
18 // Optional function, you can erase it. 19 // Optional function, you can erase it.
19 int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) { 20 int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) {
@@ -22,17 +23,17 @@ int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) { @@ -22,17 +23,17 @@ int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) {
22 ctx_p = _ctx_p; 23 ctx_p = _ctx_p;
23 indexes_p = _indexes_p; 24 indexes_p = _indexes_p;
24 25
25 - if(ctx_p->destdir == NULL) { 26 + if (ctx_p->destdir == NULL) {
26 error("dest-dir is not set."); 27 error("dest-dir is not set.");
27 return EINVAL; 28 return EINVAL;
28 } 29 }
29 30
30 - if(ctx_p->flags[RSYNCPREFERINCLUDE]) { 31 + if (ctx_p->flags[RSYNCPREFERINCLUDE]) {
31 error("clsync-synchandler-rsyncso.so cannot be used in conjunction with \"--rsync-prefer-include\" option."); 32 error("clsync-synchandler-rsyncso.so cannot be used in conjunction with \"--rsync-prefer-include\" option.");
32 return EINVAL; 33 return EINVAL;
33 } 34 }
34 35
35 - if(ctx_p->flags[THREADING]) { 36 + if (ctx_p->flags[THREADING]) {
36 error("this handler is not pthread-safe."); 37 error("this handler is not pthread-safe.");
37 return EINVAL; 38 return EINVAL;
38 } 39 }
@@ -52,12 +53,12 @@ int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) { @@ -52,12 +53,12 @@ int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) {
52 int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) { 53 int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) {
53 debug(1, "inclistfile == \"%s\"; exclistfile == \"%s\"", inclistfile, exclistfile); 54 debug(1, "inclistfile == \"%s\"; exclistfile == \"%s\"", inclistfile, exclistfile);
54 55
55 - argv[3] = (char *)exclistfile; 56 + argv[3] = exclistfile;
56 - argv[5] = (char *)inclistfile; 57 + argv[5] = inclistfile;
57 58
58 - if(ctx_p->flags[DEBUG] >= 3) { 59 + if (ctx_p->flags[DEBUG] >= 3) {
59 int i=0; 60 int i=0;
60 - while(argv[i] != NULL) { 61 + while (argv[i] != NULL) {
61 debug(3, "argv[%i] == \"%s\"", i, argv[i]); 62 debug(3, "argv[%i] == \"%s\"", i, argv[i]);
62 i++; 63 i++;
63 } 64 }
@@ -65,7 +66,7 @@ int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) { @@ -65,7 +66,7 @@ int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) {
65 66
66 // Forking 67 // Forking
67 int pid = clsyncapi_fork(ctx_p); 68 int pid = clsyncapi_fork(ctx_p);
68 - switch(pid) { 69 + switch (pid) {
69 case -1: 70 case -1:
70 error("Cannot fork()."); 71 error("Cannot fork().");
71 return errno; 72 return errno;
@@ -75,7 +76,7 @@ int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) { @@ -75,7 +76,7 @@ int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) {
75 } 76 }
76 77
77 int status; 78 int status;
78 - if(waitpid(pid, &status, 0) != pid) { 79 + if (waitpid(pid, &status, 0) != pid) {
79 error("Cannot waitid()."); 80 error("Cannot waitid().");
80 return errno; 81 return errno;
81 } 82 }
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 #include <errno.h> 3 #include <errno.h>
4 4
5 // Required header: 5 // Required header:
  6 +#include <clsync/compilerflags.h>
6 #include <clsync/clsync.h> 7 #include <clsync/clsync.h>
7 8
8 // Optional headers: 9 // Optional headers:
  1 +#include <stdio.h>
  2 +
  3 +int main() {
  4 + printf("%s",
  5 +
  6 +#ifdef _DEBUG_SUPPORT
  7 + "#define _DEBUG_SUPPORT\n"
  8 +#endif
  9 +#ifdef _DEBUG_FORCE
  10 + "#define _DEBUG_FORCE\n"
  11 +#endif
  12 +#ifdef KQUEUE_SUPPORT
  13 + "#define KQUEUE_SUPPORT\n"
  14 +#endif
  15 +#ifdef INOTIFY_SUPPORT
  16 + "#define INOTIFY_SUPPORT\n"
  17 +#endif
  18 +#ifdef INOTIFY_OLD
  19 + "#define INOTIFY_OLD\n"
  20 +#endif
  21 +#ifdef FANOTIFY_SUPPORT
  22 + "#define FANOTIFY_SUPPORT\n"
  23 +#endif
  24 +#ifdef BSM_SUPPORT
  25 + "#define BSM_SUPPORT\n"
  26 +#endif
  27 +#ifdef GIO_SUPPORT
  28 + "#define GIO_SUPPORT\n"
  29 +#endif
  30 +#ifdef DTRACEPIPE_SUPPORT
  31 + "#define DTRACEPIPE_SUPPORT\n"
  32 +#endif
  33 +#ifdef BACKTRACE_SUPPORT
  34 + "#define BACKTRACE_SUPPORT\n"
  35 +#endif
  36 +#ifdef CAPABILITIES_SUPPORT
  37 + "#define CAPABILITIES_SUPPORT\n"
  38 +#endif
  39 +#ifdef SECCOMP_SUPPORT
  40 + "#define SECCOMP_SUPPORT\n"
  41 +#endif
  42 +#ifdef GETMNTENT_SUPPORT
  43 + "#define GETMNTENT_SUPPORT\n"
  44 +#endif
  45 +#ifdef UNSHARE_SUPPORT
  46 + "#define UNSHARE_SUPPORT\n"
  47 +#endif
  48 +#ifdef PIVOTROOT_OPT_SUPPORT
  49 + "#define PIVOTROOT_OPT_SUPPORT\n"
  50 +#endif
  51 +#ifdef CGROUP_SUPPORT
  52 + "#define CGROUP_SUPPORT\n"
  53 +#endif
  54 +#ifdef TRE_SUPPORT
  55 + "#define TRE_SUPPORT\n"
  56 +#endif
  57 +#ifdef HL_LOCKS
  58 + "#define HL_LOCKS\n"
  59 +#endif
  60 + );
  61 + return 0;
  62 +}
@@ -26,6 +26,11 @@ @@ -26,6 +26,11 @@
26 #include "error.h" 26 #include "error.h"
27 #include "malloc.h" 27 #include "malloc.h"
28 28
  29 +struct fileinfo {
  30 + stat64_t lstat;
  31 +};
  32 +typedef struct fileinfo fileinfo_t;
  33 +
29 struct indexes { 34 struct indexes {
30 GHashTable *wd2fpath_ht; // watching descriptor -> file path 35 GHashTable *wd2fpath_ht; // watching descriptor -> file path
31 GHashTable *fpath2wd_ht; // file path -> watching descriptor 36 GHashTable *fpath2wd_ht; // file path -> watching descriptor
@@ -2488,6 +2488,7 @@ int main(int _argc, char *_argv[]) { @@ -2488,6 +2488,7 @@ int main(int _argc, char *_argv[]) {
2488 error("Got error while realpath() on \"%s\" [#1].", ctx_p->destdir); 2488 error("Got error while realpath() on \"%s\" [#1].", ctx_p->destdir);
2489 ret = errno; 2489 ret = errno;
2490 } 2490 }
  2491 + debug(5, "rdestdir == \"%s\"", rdestdir);
2491 2492
2492 if (!ret) { 2493 if (!ret) {
2493 parse_parameter(ctx_p, DESTDIR, rdestdir, PS_CORRECTION); 2494 parse_parameter(ctx_p, DESTDIR, rdestdir, PS_CORRECTION);
@@ -2523,6 +2524,17 @@ int main(int _argc, char *_argv[]) { @@ -2523,6 +2524,17 @@ int main(int _argc, char *_argv[]) {
2523 } 2524 }
2524 } 2525 }
2525 2526
  2527 + if (ctx_p->handlerfpath != NULL) {
  2528 + char *rhandlerfpath = realpath(ctx_p->handlerfpath, NULL);
  2529 + if (rhandlerfpath == NULL) {
  2530 + error("Got error while realpath() on \"%s\" [#0].", ctx_p->handlerfpath);
  2531 + ret = errno;
  2532 + }
  2533 + debug(5, "rhandlerfpath == \"%s\"", rhandlerfpath);
  2534 + ctx_p->handlerfpath = rhandlerfpath;
  2535 +
  2536 + }
  2537 +
2526 debug(9, "chdir(\"%s\");", ctx_p->watchdir); 2538 debug(9, "chdir(\"%s\");", ctx_p->watchdir);
2527 if (chdir(ctx_p->watchdir)) { 2539 if (chdir(ctx_p->watchdir)) {
2528 error("Got error while chdir(\"%s\")", ctx_p->watchdir); 2540 error("Got error while chdir(\"%s\")", ctx_p->watchdir);
@@ -2792,7 +2804,7 @@ int main(int _argc, char *_argv[]) { @@ -2792,7 +2804,7 @@ int main(int _argc, char *_argv[]) {
2792 } 2804 }
2793 } 2805 }
2794 2806
2795 - debug(1, "%s [%s] (%p) -> %s [%s]", ctx_p->watchdir, ctx_p->watchdirwslash, ctx_p->watchdirwslash, ctx_p->destdir?ctx_p->destdir:"", ctx_p->destdirwslash?ctx_p->destdirwslash:""); 2807 + debug(1, "%s [%s] (%p) -> %s [%s] (%p)", ctx_p->watchdir, ctx_p->watchdirwslash, ctx_p->watchdirwslash, ctx_p->destdir?ctx_p->destdir:"", ctx_p->destdirwslash?ctx_p->destdirwslash:"", ctx_p->destdirwslash);
2796 2808
2797 { 2809 {
2798 int rc = ctx_check(ctx_p); 2810 int rc = ctx_check(ctx_p);
@@ -62,9 +62,11 @@ @@ -62,9 +62,11 @@
62 62
63 #endif 63 #endif
64 64
65 -#ifndef O_PATH 65 +#ifdef CLSYNC_ITSELF
66 -# warning O_PATH is not set 66 +# ifndef O_PATH
67 -# define O_PATH 0 67 +# warning O_PATH is not set
  68 +# define O_PATH 0
  69 +# endif
68 #endif 70 #endif
69 71
70 #endif // __PORT_HACKS_H 72 #endif // __PORT_HACKS_H
@@ -3799,7 +3799,7 @@ int sync_run(ctx_t *ctx_p) { @@ -3799,7 +3799,7 @@ int sync_run(ctx_t *ctx_p) {
3799 3799
3800 // dlopen() 3800 // dlopen()
3801 void *synchandler_handle = dlopen(ctx_p->handlerfpath, RTLD_NOW|RTLD_LOCAL); 3801 void *synchandler_handle = dlopen(ctx_p->handlerfpath, RTLD_NOW|RTLD_LOCAL);
3802 - if(synchandler_handle == NULL) { 3802 + if (synchandler_handle == NULL) {
3803 error("Cannot load shared object file \"%s\": %s", ctx_p->handlerfpath, dlerror()); 3803 error("Cannot load shared object file \"%s\": %s", ctx_p->handlerfpath, dlerror());
3804 return -1; 3804 return -1;
3805 } 3805 }
@@ -3807,16 +3807,16 @@ int sync_run(ctx_t *ctx_p) { @@ -3807,16 +3807,16 @@ int sync_run(ctx_t *ctx_p) {
3807 // resolving init, sync and deinit functions' handlers 3807 // resolving init, sync and deinit functions' handlers
3808 ctx_p->handler_handle = synchandler_handle; 3808 ctx_p->handler_handle = synchandler_handle;
3809 ctx_p->handler_funct.init = (api_funct_init) dlsym(ctx_p->handler_handle, API_PREFIX"init"); 3809 ctx_p->handler_funct.init = (api_funct_init) dlsym(ctx_p->handler_handle, API_PREFIX"init");
3810 - if(ctx_p->flags[MODE] == MODE_RSYNCSO) { 3810 + if (ctx_p->flags[MODE] == MODE_RSYNCSO) {
3811 ctx_p->handler_funct.rsync = (api_funct_rsync)dlsym(ctx_p->handler_handle, API_PREFIX"rsync"); 3811 ctx_p->handler_funct.rsync = (api_funct_rsync)dlsym(ctx_p->handler_handle, API_PREFIX"rsync");
3812 - if(ctx_p->handler_funct.rsync == NULL) { 3812 + if (ctx_p->handler_funct.rsync == NULL) {
3813 char *dlerror_str = dlerror(); 3813 char *dlerror_str = dlerror();
3814 error("Cannot resolve symbol "API_PREFIX"rsync in shared object \"%s\": %s", 3814 error("Cannot resolve symbol "API_PREFIX"rsync in shared object \"%s\": %s",
3815 ctx_p->handlerfpath, dlerror_str != NULL ? dlerror_str : "No error description returned."); 3815 ctx_p->handlerfpath, dlerror_str != NULL ? dlerror_str : "No error description returned.");
3816 } 3816 }
3817 } else { 3817 } else {
3818 ctx_p->handler_funct.sync = (api_funct_sync)dlsym(ctx_p->handler_handle, API_PREFIX"sync"); 3818 ctx_p->handler_funct.sync = (api_funct_sync)dlsym(ctx_p->handler_handle, API_PREFIX"sync");
3819 - if(ctx_p->handler_funct.sync == NULL) { 3819 + if (ctx_p->handler_funct.sync == NULL) {
3820 char *dlerror_str = dlerror(); 3820 char *dlerror_str = dlerror();
3821 error("Cannot resolve symbol "API_PREFIX"sync in shared object \"%s\": %s", 3821 error("Cannot resolve symbol "API_PREFIX"sync in shared object \"%s\": %s",
3822 ctx_p->handlerfpath, dlerror_str != NULL ? dlerror_str : "No error description returned."); 3822 ctx_p->handlerfpath, dlerror_str != NULL ? dlerror_str : "No error description returned.");
@@ -3825,8 +3825,8 @@ int sync_run(ctx_t *ctx_p) { @@ -3825,8 +3825,8 @@ int sync_run(ctx_t *ctx_p) {
3825 ctx_p->handler_funct.deinit = (api_funct_deinit)dlsym(ctx_p->handler_handle, API_PREFIX"deinit"); 3825 ctx_p->handler_funct.deinit = (api_funct_deinit)dlsym(ctx_p->handler_handle, API_PREFIX"deinit");
3826 3826
3827 // running init function 3827 // running init function
3828 - if(ctx_p->handler_funct.init != NULL) 3828 + if (ctx_p->handler_funct.init != NULL)
3829 - if((ret = ctx_p->handler_funct.init(ctx_p, &indexes))) { 3829 + if ((ret = ctx_p->handler_funct.init(ctx_p, &indexes))) {
3830 error("Cannot init sync-handler module."); 3830 error("Cannot init sync-handler module.");
3831 return ret; 3831 return ret;
3832 } 3832 }
@@ -3834,7 +3834,7 @@ int sync_run(ctx_t *ctx_p) { @@ -3834,7 +3834,7 @@ int sync_run(ctx_t *ctx_p) {
3834 3834
3835 // Initializing rand-generator if it's required 3835 // Initializing rand-generator if it's required
3836 3836
3837 - if(ctx_p->listoutdir) 3837 + if (ctx_p->listoutdir)
3838 srand(time(NULL)); 3838 srand(time(NULL));
3839 3839
3840 if (!ctx_p->flags[ONLYINITSYNC]) { 3840 if (!ctx_p->flags[ONLYINITSYNC]) {