Skip to content

Commit

Permalink
Added basic iteration features:
Browse files Browse the repository at this point in the history
* Env-variable "CLSYNC_ITERATION"
* Option "--max-iterations"
  • Loading branch information
mechanicalapple committed Apr 23, 2014
1 parent 8d3c6e9 commit 875f5bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
5 changes: 5 additions & 0 deletions main.c
Expand Up @@ -69,6 +69,7 @@ static const struct option long_options[] =
{"cluster-hash-dl-max", required_argument, NULL, CLUSTERHDLMAX},
{"cluster-scan-dl-max", required_argument, NULL, CLUSTERSDLMAX},
#endif
{"max-iterations", required_argument, NULL, MAXITERATIONS},
{"standby-file", required_argument, NULL, STANDBYFILE},
{"timeout-sync", required_argument, NULL, SYNCTIMEOUT},
{"delay-sync", required_argument, NULL, SYNCDELAY},
Expand Down Expand Up @@ -1024,6 +1025,10 @@ int main(int argc, char *argv[]) {
printf_e("Error: Conflicting options: \"--pthread\" and \"--exit-on-no-events\" cannot be used together.\n");
ret = EINVAL;
}
if(options.flags[PTHREAD] && options.flags[MAXITERATIONS]) {
printf_e("Error: Conflicting options: \"--pthread\" and \"--max-iterations\" cannot be used together.\n");
ret = EINVAL;
}
if(options.flags[SKIPINITSYNC] && options.flags[EXITONNOEVENTS]) {
printf_e("Error: Conflicting options: \"--skip-initialsync\" and \"--exit-on-no-events\" cannot be used together.\n");
ret = EINVAL;
Expand Down
11 changes: 11 additions & 0 deletions man/man1/clsync.1
Expand Up @@ -435,6 +435,13 @@ freeze destination directory while running some scripts.
Is not set by default.
.RE

.PP
.B \-\-max\-iterations
.I iterations\-count
.RS 8
Sets maximal count of synchronization iterations.
.RE

.PP
.B \-k, \-\-timeout\-sync
.I sync-timeout
Expand Down Expand Up @@ -1089,6 +1096,10 @@ status (see possible statuses in description of
.IR \-\-status\-file )
.RE

.RS
CLSYNC_ITERATION - count of done synchronizaton iterations after initial sync
.RE

.SH RULES
Filter riles can be placed into
.I rules\-file
Expand Down
3 changes: 3 additions & 0 deletions options.h
Expand Up @@ -87,6 +87,8 @@ enum flags_enum {
SOCKETAUTH = 12|OPTION_LONGOPTONLY,
SOCKETMOD = 13|OPTION_LONGOPTONLY,
SOCKETOWN = 14|OPTION_LONGOPTONLY,

MAXITERATIONS = 15|OPTION_LONGOPTONLY,
};
typedef enum flags_enum flags_t;

Expand Down Expand Up @@ -159,6 +161,7 @@ struct options {
gid_t gid;
pid_t child_pid[MAXCHILDREN]; // Used only for non-pthread mode
int children; // Used only for non-pthread mode
uint32_t iteration_num;
rule_t rules[MAXRULES];
dev_t st_dev;
int flags[OPTION_FLAGS];
Expand Down
37 changes: 32 additions & 5 deletions sync.c
Expand Up @@ -28,6 +28,7 @@
#include "glibex.h"
#include "control.h"

#include <stdio.h>
#include <dlfcn.h>


Expand Down Expand Up @@ -2271,7 +2272,12 @@ void sync_idle_dosync_collectedevents_listpush(gpointer fpath_gp, gpointer evinf
return;
}


static inline void setenv_iteration(uint32_t iteration_num)
{
char iterations[sizeof("4294967296")]; // 4294967296 == 2**32
sprintf(iterations, "%i", iteration_num);
setenv("CLSYNC_ITERATION", iterations, 1);
}

int sync_idle_dosync_collectedevents(options_t *options_p, indexes_t *indexes_p) {
printf_ddd("Debug3: sync_idle_dosync_collectedevents()\n");
Expand Down Expand Up @@ -2321,7 +2327,6 @@ int sync_idle_dosync_collectedevents(options_t *options_p, indexes_t *indexes_p)
return 0;
}


if(options_p->flags[MODE] == MODE_SO) {
//dosync_arg.evcount = g_hash_table_size(indexes_p->fpath2ei_ht);
printf_ddd("Debug3: sync_idle_dosync_collectedevents(): There's %i events. Processing.\n", dosync_arg.evcount);
Expand Down Expand Up @@ -2370,6 +2375,15 @@ int sync_idle_dosync_collectedevents(options_t *options_p, indexes_t *indexes_p)
g_hash_table_remove_all(indexes_p->fpath2ei_ht);
}

if(!options_p->flags[PTHREAD]) {
if(options_p->iteration_num < ~0) // ~0 is the max value for unsigned variables
options_p->iteration_num++;
setenv_iteration(options_p->iteration_num);

printf_ddd("Debug3: sync_idle_dosync_collectedevents(): next iteration: %u/%u\n",
options_p->iteration_num, options_p->flags[MAXITERATIONS]);
}

return 0;
}

Expand Down Expand Up @@ -2782,7 +2796,8 @@ int sync_inotify_loop(int inotify_d, options_t *options_p, indexes_t *indexes_p)

threadsinfo_t *threadsinfo_p = thread_getinfo();
pthread_mutex_lock(&threadsinfo_p->mutex[PTHREAD_MUTEX_STATE]);
printf_ddd("Debug3: sync_inotify_loop(): current state is %i\n", state);
printf_ddd("Debug3: sync_inotify_loop(): current state is %i (iteration: %u/%u)\n",
state, options_p->iteration_num, options_p->flags[MAXITERATIONS]);
events = 0;
switch(state) {
case STATE_PTHREAD_GC:
Expand All @@ -2794,6 +2809,11 @@ int sync_inotify_loop(int inotify_d, options_t *options_p, indexes_t *indexes_p)
state = STATE_RUNNING;
SYNC_INOTIFY_LOOP_CONTINUE_UNLOCK;
case STATE_INITSYNC:
if(!options_p->flags[PTHREAD]) {
options_p->iteration_num = 0;
setenv_iteration(options_p->iteration_num);
}

main_status_update(options_p, state);
pthread_cond_broadcast(&threadsinfo_p->cond[PTHREAD_MUTEX_STATE]);
pthread_mutex_unlock(&threadsinfo_p->mutex[PTHREAD_MUTEX_STATE]);
Expand All @@ -2809,7 +2829,14 @@ int sync_inotify_loop(int inotify_d, options_t *options_p, indexes_t *indexes_p)
state = STATE_RUNNING;
continue;
case STATE_RUNNING:
events = sync_inotify_wait(inotify_d, options_p, indexes_p);
if(!options_p->flags[PTHREAD])
if (options_p->flags[MAXITERATIONS] &&
options_p->flags[MAXITERATIONS] <= options_p->iteration_num)
state = STATE_EXIT;

if(state == STATE_RUNNING)
events = sync_inotify_wait(inotify_d, options_p, indexes_p);

if(state != STATE_RUNNING)
SYNC_INOTIFY_LOOP_CONTINUE_UNLOCK;
break;
Expand All @@ -2832,6 +2859,7 @@ int sync_inotify_loop(int inotify_d, options_t *options_p, indexes_t *indexes_p)
if(events == 0) {
printf_dd("Debug2: sync_inotify_wait(%i, options_p, indexes_p) timed-out.\n", inotify_d);
SYNC_INOTIFY_LOOP_IDLE;
main_status_update(options_p, state);
continue; // Timeout
}
if(events < 0) {
Expand All @@ -2844,7 +2872,6 @@ int sync_inotify_loop(int inotify_d, options_t *options_p, indexes_t *indexes_p)
printf_e("Error: Cannot handle with inotify events: %s (errno: %i).\n", strerror(errno), errno);
return errno;
}
main_status_update(options_p, state);

if(options_p->flags[EXITONNOEVENTS]) // clsync exits on no events, so sync_idle() is never called. We have to force the calling of it.
SYNC_INOTIFY_LOOP_IDLE;
Expand Down

0 comments on commit 875f5bc

Please sign in to comment.