feat: support rsync no-option handling
CI / lint (pull_request) Successful in 12s
CI / sanitizers (address) (pull_request) Successful in 37s
CI / sanitizers (undefined) (pull_request) Successful in 37s
CI / fuzz-build (pull_request) Successful in 15s
CI / coverage (pull_request) Successful in 31s
CI / build-and-test (pull_request) Successful in 1m15s
CI / valgrind (pull_request) Successful in 33s
CI / lint (pull_request) Successful in 12s
CI / sanitizers (address) (pull_request) Successful in 37s
CI / sanitizers (undefined) (pull_request) Successful in 37s
CI / fuzz-build (pull_request) Successful in 15s
CI / coverage (pull_request) Successful in 31s
CI / build-and-test (pull_request) Successful in 1m15s
CI / valgrind (pull_request) Successful in 33s
This commit is contained in:
@@ -134,6 +134,12 @@ typedef struct {
|
||||
size_t offset; /* offsetof of the target field in Config */
|
||||
} OptionEntry;
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
const char* alias;
|
||||
size_t offset; /* offsetof of the boolean target field in Config */
|
||||
} NegatableOption;
|
||||
|
||||
/* Options that map directly onto a Config field with no side effects. */
|
||||
static const OptionEntry OPTION_TABLE[] = {
|
||||
{"--dry-run", "-n", OPT_FLAG, offsetof(Config, dry_run)},
|
||||
@@ -173,6 +179,34 @@ static const OptionEntry OPTION_TABLE[] = {
|
||||
{"--min-size", NULL, OPT_ULL, offsetof(Config, min_size)},
|
||||
};
|
||||
|
||||
/* Only boolean options with no required argument are safe to negate. */
|
||||
static const NegatableOption NEGATABLE_OPTIONS[] = {
|
||||
{"dry-run", "n", offsetof(Config, dry_run)},
|
||||
{"delete", NULL, offsetof(Config, use_delete)},
|
||||
{"incremental", NULL, offsetof(Config, use_incremental)},
|
||||
{"delta", NULL, offsetof(Config, use_delta)},
|
||||
{"save-to-disk", NULL, offsetof(Config, save_to_disk)},
|
||||
{"progress", NULL, offsetof(Config, show_progress)},
|
||||
{"tls", NULL, offsetof(Config, use_tls)},
|
||||
{"backup", NULL, offsetof(Config, backup)},
|
||||
{"stats", NULL, offsetof(Config, stats)},
|
||||
{"partial", NULL, offsetof(Config, partial)},
|
||||
{"links", "l", offsetof(Config, follow_symlinks)},
|
||||
{"copy-links", NULL, offsetof(Config, copy_links)},
|
||||
{"safe-links", NULL, offsetof(Config, safe_links)},
|
||||
{"copy-unsafe-links", NULL, offsetof(Config, copy_unsafe_links)},
|
||||
{"sparse", "S", offsetof(Config, preserve_sparse)},
|
||||
{"inplace", NULL, offsetof(Config, inplace)},
|
||||
{"checksum", NULL, offsetof(Config, checksum)},
|
||||
|
||||
/* These options are also implied by --archive or handled outside the table. */
|
||||
{"compress", "c", offsetof(Config, use_compression)},
|
||||
{"multithreading", "m", offsetof(Config, use_multithreading)},
|
||||
{"preserve", "M", offsetof(Config, use_metadata)},
|
||||
{"sendfile", "f", offsetof(Config, use_sendfile)},
|
||||
{"chunk-serialization", "s", offsetof(Config, use_chunk_serialization)},
|
||||
};
|
||||
|
||||
static bool opt_is(const char* arg, const char* name, const char* alias) {
|
||||
return strcmp(arg, name) == 0 || (alias && strcmp(arg, alias) == 0);
|
||||
}
|
||||
@@ -184,6 +218,29 @@ static const OptionEntry* find_table_option(const char* arg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const NegatableOption* find_negatable_option(const char* name) {
|
||||
for (size_t i = 0; i < sizeof(NEGATABLE_OPTIONS) / sizeof(NEGATABLE_OPTIONS[0]); i++)
|
||||
if (strcmp(name, NEGATABLE_OPTIONS[i].name) == 0 ||
|
||||
(NEGATABLE_OPTIONS[i].alias && strcmp(name, NEGATABLE_OPTIONS[i].alias) == 0))
|
||||
return &NEGATABLE_OPTIONS[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int apply_negation(Config* config, const char* arg) {
|
||||
const char* name = arg + strlen("--no-");
|
||||
if (*name == '\0') {
|
||||
fprintf(stderr, "Cannot negate an empty option name: %s\n", arg);
|
||||
return -1;
|
||||
}
|
||||
const NegatableOption* entry = find_negatable_option(name);
|
||||
if (!entry) {
|
||||
fprintf(stderr, "Cannot negate unsupported or unsafe option: %s\n", arg);
|
||||
return -1;
|
||||
}
|
||||
*(bool*)((char*)config + entry->offset) = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int apply_table_option(Config* config, const OptionEntry* entry, const char* value) {
|
||||
void* field = (char*)config + entry->offset;
|
||||
switch (entry->kind) {
|
||||
@@ -211,6 +268,11 @@ static int apply_table_option(Config* config, const OptionEntry* entry, const ch
|
||||
int parse_args(Config* config, int argc, char* argv[], int* positional_args,
|
||||
int* positional_count) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strncmp(argv[i], "--no-", strlen("--no-")) == 0) {
|
||||
if (apply_negation(config, argv[i]) != 0)
|
||||
return -1;
|
||||
continue;
|
||||
}
|
||||
const OptionEntry* entry = find_table_option(argv[i]);
|
||||
if (entry) {
|
||||
if (entry->kind != OPT_FLAG) {
|
||||
|
||||
Reference in New Issue
Block a user