summaryrefslogtreecommitdiffstats
path: root/usage.c
blob: f346385f4015cfdd4a02695994ae027ab50bb4c8 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Some usage & version related functions.
 *
 * Copyright (C) 2002-2022 Wayne Davison
 *
 * 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, visit the http://fsf.org website.
 */

#include "rsync.h"
#include "version.h"
#include "latest-year.h"
#include "git-version.h"
#include "default-cvsignore.h"
#include "itypes.h"

extern struct name_num_obj valid_checksums, valid_compressions, valid_auth_checksums;

static char *istring(const char *fmt, int val)
{
	char *str;
	if (asprintf(&str, fmt, val) < 0)
		out_of_memory("istring");
	return str;
}

static void print_info_flags(enum logcode f)
{
	STRUCT_STAT *dumstat;
	BOOL as_json = f == FNONE ? 1 : 0; /* We use 1 == first attribute, 2 == need closing array */
	char line_buf[75], item_buf[32];
	int line_len, j;
	char *info_flags[] = {

	"*Capabilities",

		istring("%d-bit files", (int)(sizeof (OFF_T) * 8)),
		istring("%d-bit inums", (int)(sizeof dumstat->st_ino * 8)), /* Don't check ino_t! */
		istring("%d-bit timestamps", (int)(sizeof (time_t) * 8)),
		istring("%d-bit long ints", (int)(sizeof (int64) * 8)),

#ifndef HAVE_SOCKETPAIR
		"no "
#endif
			"socketpairs",

#ifndef SUPPORT_LINKS
		"no "
#endif
			"symlinks",

#ifndef CAN_SET_SYMLINK_TIMES
		"no "
#endif
			"symtimes",

#ifndef SUPPORT_HARD_LINKS
		"no "
#endif
			"hardlinks",

#ifndef CAN_HARDLINK_SPECIAL
		"no "
#endif
			"hardlink-specials",

#ifndef CAN_HARDLINK_SYMLINK
		"no "
#endif
			"hardlink-symlinks",

#ifndef INET6
		"no "
#endif
			"IPv6",

#ifndef SUPPORT_ATIMES
		"no "
#endif
			"atimes",

		"batchfiles",

#ifndef HAVE_FTRUNCATE
		"no "
#endif
			"inplace",

#ifndef HAVE_FTRUNCATE
		"no "
#endif
			"append",

#ifndef SUPPORT_ACLS
		"no "
#endif
			"ACLs",

#ifndef SUPPORT_XATTRS
		"no "
#endif
			"xattrs",

#ifdef RSYNC_USE_SECLUDED_ARGS
		"default "
#else
		"optional "
#endif
			"secluded-args",

#ifndef ICONV_OPTION
		"no "
#endif
			"iconv",

#ifndef SUPPORT_PREALLOCATION
		"no "
#endif
			"prealloc",

#ifndef HAVE_MKTIME
		"no "
#endif
			"stop-at",

#ifndef SUPPORT_CRTIMES
		"no "
#endif
			"crtimes",

	"*Optimizations",

#ifndef USE_ROLL_SIMD
		"no "
#endif
			"SIMD-roll",

#ifndef USE_ROLL_ASM
		"no "
#endif
			"asm-roll",

#ifndef USE_OPENSSL
		"no "
#endif
			"openssl-crypto",

#ifndef USE_MD5_ASM
		"no "
#endif
			"asm-MD5",

		NULL
	};

	for (line_len = 0, j = 0; ; j++) {
		char *str = info_flags[j], *next_nfo = str ? info_flags[j+1] : NULL;
		int need_comma = next_nfo && *next_nfo != '*' ? 1 : 0;
		int item_len;
		if (!str || *str == '*')
			item_len = 1000;
		else if (as_json) {
			char *space = strchr(str, ' ');
			int is_no = space && strncmp(str, "no ", 3) == 0;
			int is_bits = space && isDigit(str);
			char *quot = space && !is_no && !is_bits ? "\"" : "";
			char *item = space ? space + 1 : str;
			char *val = !space ? "true" : is_no ? "false" : str;
			int val_len = !space ? 4 : is_no ? 5 : space - str;
			if (is_bits && (space = strchr(val, '-')) != NULL)
			    val_len = space - str;
			item_len = snprintf(item_buf, sizeof item_buf,
					   " \"%s%s\": %s%.*s%s%s", item, is_bits ? "bits" : "",
					   quot, val_len, val, quot, need_comma ? "," : "");
			if (is_bits)
				item_buf[strlen(item)+2-1] = '_'; /* Turn the 's' into a '_' */
			for (space = item; (space = strpbrk(space, " -")) != NULL; space++)
				item_buf[space - item + 2] = '_';
		} else
			item_len = snprintf(item_buf, sizeof item_buf, " %s%s", str, need_comma ? "," : "");
		if (line_len && line_len + item_len >= (int)sizeof line_buf) {
			if (as_json)
				printf("   %s\n", line_buf);
			else
				rprintf(f, "   %s\n", line_buf);
			line_len = 0;
		}
		if (!str)
			break;
		if (*str == '*') {
			if (as_json) {
				if (as_json == 2)
					printf("  }");
				else
					as_json = 2;
				printf(",\n  \"%c%s\": {\n", toLower(str+1), str+2);
			} else
				rprintf(f, "%s:\n", str+1);
		} else {
			strlcpy(line_buf + line_len, item_buf, sizeof line_buf - line_len);
			line_len += item_len;
		}
	}
	if (as_json == 2)
		printf("  }");
}

static void output_nno_list(enum logcode f, const char *name, struct name_num_obj *nno)
{
	char namebuf[64], tmpbuf[256];
	char *tok, *next_tok, *comma = ",";
	char *cp;

	/* Using '(' ensures that we get a trailing "none" but also includes aliases. */
	get_default_nno_list(nno, tmpbuf, sizeof tmpbuf - 1, '(');
	if (f != FNONE) {
		rprintf(f, "%s:\n", name);
		rprintf(f, "    %s\n", tmpbuf);
		return;
	}

	strlcpy(namebuf, name, sizeof namebuf);
	for (cp = namebuf; *cp; cp++) {
		if (*cp == ' ')
			*cp = '_';
		else if (isUpper(cp))
			*cp = toLower(cp);
	}

	printf(",\n  \"%s\": [\n   ", namebuf);

	for (tok = strtok(tmpbuf, " "); tok; tok = next_tok) {
		next_tok = strtok(NULL, " ");
		if (*tok != '(') /* Ignore the alises in the JSON output */
			printf(" \"%s\"%s", tok, comma + (next_tok ? 0 : 1));
	}

	printf("\n  ]");
}

/* A request of f == FNONE wants json on stdout. */
void print_rsync_version(enum logcode f)
{
	char copyright[] = "(C) 1996-" LATEST_YEAR " by Andrew Tridgell, Wayne Davison, and others.";
	char url[] = "https://rsync.samba.org/";
	BOOL first_line = 1;

#define json_line(name, value) \
	do { \
		printf("%c\n  \"%s\": \"%s\"", first_line ? '{' : ',', name, value); \
		first_line = 0; \
	} while (0)

	if (f == FNONE) {
		char verbuf[32];
		json_line("program", RSYNC_NAME);
		json_line("version", rsync_version());
		(void)snprintf(verbuf, sizeof verbuf, "%d.%d", PROTOCOL_VERSION, SUBPROTOCOL_VERSION);
		json_line("protocol", verbuf);
		json_line("copyright", copyright);
		json_line("url", url);
	} else {
#if SUBPROTOCOL_VERSION != 0
		char *subprotocol = istring(".PR%d", SUBPROTOCOL_VERSION);
#else
		char *subprotocol = "";
#endif
		rprintf(f, "%s  version %s  protocol version %d%s\n",
			RSYNC_NAME, rsync_version(), PROTOCOL_VERSION, subprotocol);
		rprintf(f, "Copyright %s\n", copyright);
		rprintf(f, "Web site: %s\n", url);
	}

	print_info_flags(f);

	init_checksum_choices();

	output_nno_list(f, "Checksum list", &valid_checksums);
	output_nno_list(f, "Compress list", &valid_compressions);
	output_nno_list(f, "Daemon auth list", &valid_auth_checksums);

	if (f == FNONE) {
		json_line("license", "GPLv3");
		json_line("caveat", "rsync comes with ABSOLUTELY NO WARRANTY");
		printf("\n}\n");
		fflush(stdout);
		return;
	}

#ifdef MAINTAINER_MODE
	rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
#endif

#if SIZEOF_INT64 < 8
	rprintf(f, "WARNING: no 64-bit integers on this platform!\n");
#endif
	if (sizeof (int64) != SIZEOF_INT64) {
		rprintf(f,
			"WARNING: size mismatch in SIZEOF_INT64 define (%d != %d)\n",
			(int) SIZEOF_INT64, (int) sizeof (int64));
	}

	rprintf(f,"\n");
	rprintf(f,"rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you\n");
	rprintf(f,"are welcome to redistribute it under certain conditions.  See the GNU\n");
	rprintf(f,"General Public Licence for details.\n");
}

void usage(enum logcode F)
{
  print_rsync_version(F);

  rprintf(F,"\n");
  rprintf(F,"rsync is a file transfer program capable of efficient remote update\n");
  rprintf(F,"via a fast differencing algorithm.\n");

  rprintf(F,"\n");
  rprintf(F,"Usage: rsync [OPTION]... SRC [SRC]... DEST\n");
  rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
  rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
  rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST\n");
  rprintf(F,"  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]\n");
  rprintf(F,"  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
  rprintf(F,"  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
  rprintf(F,"The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect\n");
  rprintf(F,"to an rsync daemon, and require SRC or DEST to start with a module name.\n");
  rprintf(F,"\n");
  rprintf(F,"Options\n");
#include "help-rsync.h"
  rprintf(F,"\n");
  rprintf(F,"Use \"rsync --daemon --help\" to see the daemon-mode command-line options.\n");
  rprintf(F,"Please see the rsync(1) and rsyncd.conf(5) manpages for full documentation.\n");
  rprintf(F,"See https://rsync.samba.org/ for updates, bug reports, and answers\n");
}

void daemon_usage(enum logcode F)
{
  print_rsync_version(F);

  rprintf(F,"\n");
  rprintf(F,"Usage: rsync --daemon [OPTION]...\n");
#include "help-rsyncd.h"
  rprintf(F,"\n");
  rprintf(F,"If you were not trying to invoke rsync as a daemon, avoid using any of the\n");
  rprintf(F,"daemon-specific rsync options.  See also the rsyncd.conf(5) manpage.\n");
}

const char *rsync_version(void)
{
	char *ver;
#ifdef RSYNC_GITVER
	ver = RSYNC_GITVER;
#else
	ver = RSYNC_VERSION;
#endif
	return *ver == 'v' ? ver+1 : ver;
}

const char *default_cvsignore(void)
{
	return DEFAULT_CVSIGNORE;
}