-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlrsync.c
199 lines (162 loc) · 4.62 KB
/
lrsync.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
lrsync - rsync-like clsync wrapper
Copyright (C) 2014 Dmitry Yu Okunev <[email protected]> 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 <stdio.h> // printf()
#include <unistd.h> // fork()
#include <stdlib.h> // getenv()
#include <stdarg.h> // va_start()
#include <string.h> // memset()
#include <errno.h> // errno
#include <sys/types.h> // waitpid()
#include <sys/wait.h> // waitpid()
#include "common.h"
#include "error.h"
#include "malloc.h"
#include "lrsync.h"
int forkexecwaitvp(const char *const bin_path, const char **const argv)
{
pid_t pid;
int status;
// Forking
pid = fork();
switch(pid) {
case -1:
error("Cannot fork().");
return errno;
case 0:
argv[0] = bin_path;
execvp(bin_path, (char *const *)argv);
return errno;
}
if(waitpid(pid, &status, 0) != pid) {
error("Cannot waitid().");
return errno;
}
// Return
int exitcode = WEXITSTATUS(status);
return exitcode;
}
#define arglist2argv(argv, firstarg, COPYARG) {\
va_list arglist;\
va_start(arglist, firstarg);\
\
int i = 1;\
do {\
char *arg;\
if(i >= MAXARGUMENTS) {\
error("Too many arguments (%i >= %i).", i, MAXARGUMENTS);\
return ENOMEM;\
}\
arg = (char *)va_arg(arglist, const char *const);\
argv[i] = arg!=NULL ? COPYARG : NULL;\
} while(argv[i++] != NULL);\
va_end(arglist);\
}
const char *path_rsync() {
char *rsync_path;
rsync_path = getenv("RSYNC_PATH");
if (rsync_path == NULL)
rsync_path = DEFAULT_RSYNC_PATH;
return rsync_path;
}
const char *path_clsync() {
char *clsync_path;
clsync_path = getenv("CLSYNC_PATH");
if (clsync_path == NULL)
clsync_path = DEFAULT_CLSYNC_PATH;
return clsync_path;
}
const char *path_listsdir() {
char *listsdir_path;
listsdir_path = getenv("CLSYNC_LISTS_PATH");
if (listsdir_path == NULL)
listsdir_path = DEFAULT_LISTSDIR_PATH;
return listsdir_path;
}
static inline int exec_rsync_argv(const char **argv)
{
return forkexecwaitvp(path_rsync(), argv);
}
static inline int exec_clsync_argv(const char **argv)
{
return forkexecwaitvp(path_clsync(), argv);
}
int exec_rsync(ctx_t *const ctx_p, ...)
{
char **argv = (char **)xcalloc(sizeof(char *), MAXARGUMENTS);
memset(argv, 0, sizeof(char *)*MAXARGUMENTS);
arglist2argv(argv, ctx_p, arg);
int rc = exec_rsync_argv((const char **)argv);
free(argv);
return rc;
}
int exec_clsync(ctx_t *const ctx_p, ...)
{
char **argv = (char **)xcalloc(sizeof(char *), MAXARGUMENTS);
memset(argv, 0, sizeof(char *)*MAXARGUMENTS);
arglist2argv(argv, ctx_p, arg);
int rc = exec_clsync_argv((const char **)argv);
free(argv);
return rc;
}
int print_cmd(const char *const *const argv) {
const char *const *argv_p;
argv_p = argv;
if (*argv_p != NULL)
printf("%s ", *(argv_p++));
while (*argv_p != NULL)
printf("\\\n %s ", *(argv_p++));
printf("\n\n");
return 0;
}
static inline void push_arg(const char **argv, int *count_p, const char *arg) {
if ((*count_p) >= MAXARGUMENTS) {
errno = E2BIG;
critical("Too many arguments");
}
argv[(*count_p)++] = arg;
return;
}
int lrsync(ctx_t *const ctx_p)
{
int count = 0;
const char *argv[MAXARGUMENTS];
char **argv_p;
if (ctx_p->dir_from == NULL)
errno=EINVAL, critical("Source is not entered");
if (ctx_p->dir_to == NULL)
errno=EINVAL, critical("Destination is not entered");
push_arg(argv, &count, path_clsync());
push_arg(argv, &count, "-Klrsync");
push_arg(argv, &count, "-Mrsyncdirect");
push_arg(argv, &count, "-S");
push_arg(argv, &count, path_rsync());
push_arg(argv, &count, "-W");
push_arg(argv, &count, ctx_p->dir_from);
push_arg(argv, &count, "-D");
push_arg(argv, &count, ctx_p->dir_to);
push_arg(argv, &count, "-L");
push_arg(argv, &count, path_listsdir());
argv_p = ctx_p->clsync_argv;
while (*argv_p != NULL)
push_arg(argv, &count, *(argv_p++));
push_arg(argv, &count, "--");
push_arg(argv, &count, "%RSYNC-ARGS%");
argv_p = ctx_p->rsync_argv;
while (*argv_p != NULL)
push_arg(argv, &count, *(argv_p++));
if (ctx_p->flags[FL_CLSYNCCOMMANDONLY])
return print_cmd(argv);
return exec_clsync_argv(argv);
}