File: clsync-synchandler-rsyncso.c

package info (click to toggle)
clsync 0.4.5-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,404 kB
  • sloc: ansic: 15,074; sh: 466; makefile: 269
file content (97 lines) | stat: -rw-r--r-- 2,065 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <stdlib.h>
#include <errno.h>

// Required header:
#include <clsync/compilerflags.h>
#include <clsync/clsync.h>

// Optional headers:
#include <clsync/configuration.h>
#include <clsync/error.h>
#include <clsync/ctx.h>

static struct ctx *ctx_p         = NULL;
static struct indexes *indexes_p = NULL;

static const char *argv[11]   = {NULL};

// Optional function, you can erase it.
int clsyncapi_init(struct ctx *_ctx_p, struct indexes *_indexes_p) {
	debug(1, "Hello world!");

	ctx_p = _ctx_p;
	indexes_p = _indexes_p;

	if (ctx_p->destdir == NULL) {
		error("destination-dir is not set.");
		return EINVAL;
	}

	if (ctx_p->flags[RSYNCPREFERINCLUDE]) {
		error("clsync-synchandler-rsyncso.so cannot be used in conjunction with \"--rsync-prefer-include\" option.");
		return EINVAL;
	}

	if (ctx_p->flags[THREADING]) {
		error("this handler is not pthread-safe.");
		return EINVAL;
	}

	argv[0] = "/usr/bin/rsync";
	argv[1] = ctx_p->flags[DEBUG] >= 4 ? "-avvvvvvH" : "-aH";
	argv[2] = "--exclude-from";
	argv[4] = "--include-from";
	argv[6] = "--exclude=*";
	argv[7] = "--delete-before";
	argv[8] = ctx_p->watchdirwslash;
	argv[9] = ctx_p->destdirwslash;

	return 0;
}

int clsyncapi_rsync(const char *inclistfile, const char *exclistfile) {
	debug(1, "inclistfile == \"%s\"; exclistfile == \"%s\"", inclistfile, exclistfile);

	argv[3] = exclistfile;
	argv[5] = inclistfile;

	if (ctx_p->flags[DEBUG] >= 3) {
		int i=0;
		while (argv[i] != NULL) {
			debug(3, "argv[%i] == \"%s\"", i, argv[i]);
			i++;
		}
	}

	// Forking
	int pid = clsyncapi_fork(ctx_p);
	switch (pid) {
		case -1: 
			error("Cannot fork().");
			return errno;
		case  0:
			execvp(argv[0], (char *const *)argv);
			return errno;
	}

	int status;
	if (waitpid(pid, &status, 0) != pid) {
		error("Cannot waitid().");
		return errno;
	}

	// Return
	int exitcode = WEXITSTATUS(status);
	debug(1, "Execution completed with exitcode %i.", exitcode);

	return exitcode;
}

// Optional function, you can erase it.
int clsyncapi_deinit() {
	debug(1, "Goodbye cruel world!");

	return 0;
}