-
Notifications
You must be signed in to change notification settings - Fork 28
/
fargs.c
165 lines (144 loc) · 4.15 KB
/
fargs.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
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/stat.h>
#include <assert.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "extern.h"
#define RSYNC_PATH "rsync"
const char *
alt_base_mode(int mode)
{
switch (mode) {
case BASE_MODE_COMPARE:
return "--compare-dest";
case BASE_MODE_COPY:
return "--copy-dest";
case BASE_MODE_LINK:
return "--link-dest";
default:
errx(1, "unknown base mode %d", mode);
}
}
char **
fargs_cmdline(struct sess *sess, const struct fargs *f, size_t *skip)
{
arglist args;
size_t j;
char *rsync_path, *ap, *arg;
memset(&args, 0, sizeof args);
assert(f != NULL);
assert(f->sourcesz > 0);
if ((rsync_path = sess->opts->rsync_path) == NULL)
rsync_path = (char *)RSYNC_PATH;
if (f->host != NULL) {
/*
* Splice arguments from -e "foo bar baz" into array
* elements required for execve(2).
* This doesn't do anything fancy: it splits along
* whitespace into the array.
*/
if (sess->opts->ssh_prog) {
ap = strdup(sess->opts->ssh_prog);
if (ap == NULL)
err(ERR_NOMEM, NULL);
while ((arg = strsep(&ap, " \t")) != NULL) {
if (arg[0] == '\0') {
ap++; /* skip separators */
continue;
}
addargs(&args, "%s", arg);
}
} else
addargs(&args, "ssh");
addargs(&args, "%s", f->host);
addargs(&args, "%s", rsync_path);
if (skip)
*skip = args.num;
addargs(&args, "--server");
if (f->mode == FARGS_RECEIVER)
addargs(&args, "--sender");
} else {
addargs(&args, "%s", rsync_path);
addargs(&args, "--server");
}
/* Shared arguments. */
if (sess->opts->del)
addargs(&args, "--delete");
if (sess->opts->numeric_ids)
addargs(&args, "--numeric-ids");
if (sess->opts->preserve_gids)
addargs(&args, "-g");
if (sess->opts->preserve_links)
addargs(&args, "-l");
if (sess->opts->dry_run)
addargs(&args, "-n");
if (sess->opts->preserve_uids)
addargs(&args, "-o");
if (sess->opts->preserve_perms)
addargs(&args, "-p");
if (sess->opts->devices)
addargs(&args, "-D");
if (sess->opts->recursive)
addargs(&args, "-r");
if (sess->opts->preserve_times)
addargs(&args, "-t");
if (verbose > 3)
addargs(&args, "-v");
if (verbose > 2)
addargs(&args, "-v");
if (verbose > 1)
addargs(&args, "-v");
if (verbose > 0)
addargs(&args, "-v");
if (sess->opts->one_file_system > 1)
addargs(&args, "-x");
if (sess->opts->one_file_system > 0)
addargs(&args, "-x");
if (sess->opts->specials && !sess->opts->devices)
addargs(&args, "--specials");
if (!sess->opts->specials && sess->opts->devices)
/* --devices is sent as -D --no-specials */
addargs(&args, "--no-specials");
if (sess->opts->max_size >= 0)
addargs(&args, "--max-size=%lld", (long long)sess->opts->max_size);
if (sess->opts->min_size >= 0)
addargs(&args, "--min-size=%lld", (long long)sess->opts->min_size);
/* only add --compare-dest, etc if this is the sender */
if (sess->opts->alt_base_mode != 0 &&
f->mode == FARGS_SENDER) {
for (j = 0; j < MAX_BASEDIR; j++) {
if (sess->opts->basedir[j] == NULL)
break;
addargs(&args, "%s=%s",
alt_base_mode(sess->opts->alt_base_mode),
sess->opts->basedir[j]);
}
}
/* Terminate with a full-stop for reasons unknown. */
addargs(&args, ".");
if (f->mode == FARGS_RECEIVER) {
for (j = 0; j < f->sourcesz; j++)
addargs(&args, "%s", f->sources[j]);
} else
addargs(&args, "%s", f->sink);
return args.list;
}