Skip to content

Commit 39c6383

Browse files
committed
btrfs-progs: add support for dry-run options
Some commands could be run in a dry-run mode, i.e. not doing any write/change actions, only printing the steps and ignoring errors. There are two possibilities where to put the option: - as a global one: btrfs --dry-run subvolume delete /path - local option: btrfs subvolume delete --dry-run /path As we have several global options already, let's put it there, dry-run should not be very common so the slight inconvenience of writing the option out of order of command arguments should be acceptable. Issue: #629 Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 2c747dd commit 39c6383

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

btrfs.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static const char * const btrfs_cmd_group_usage[] = {
4444
" -v|--verbose increase verbosity of the subcommand\n"
4545
" -q|--quiet print only errors\n"
4646
" --log <level> set log level (default, info, verbose, debug, quiet)\n"
47+
" --dry-run if supported, do not do any active/changing actions\n"
4748
"\n"
4849
"Options for the main command only:\n"
4950
" --help print condensed help for all subcommands\n"
@@ -119,7 +120,7 @@ parse_command_token(const char *arg, const struct cmd_group *grp)
119120
return cmd;
120121
}
121122

122-
static void check_output_format(const struct cmd_struct *cmd)
123+
static void check_command_flags(const struct cmd_struct *cmd)
123124
{
124125
if (cmd->next)
125126
return;
@@ -129,6 +130,11 @@ static void check_output_format(const struct cmd_struct *cmd)
129130
output_format_name(bconf.output_format));
130131
exit(1);
131132
}
133+
134+
if (bconf.dry_run && !(cmd->flags & CMD_DRY_RUN)) {
135+
error("--dry-run option not supported for %s\n", cmd->token);
136+
exit(1);
137+
}
132138
}
133139

134140
static void handle_help_options_next_level(const struct cmd_struct *cmd,
@@ -165,7 +171,7 @@ int handle_command_group(const struct cmd_struct *cmd, int argc,
165171
subcmd = parse_command_token(argv[0], cmd->next);
166172

167173
handle_help_options_next_level(subcmd, argc, argv);
168-
check_output_format(subcmd);
174+
check_command_flags(subcmd);
169175

170176
fixup_argv0(argv, subcmd->token);
171177
return cmd_execute(subcmd, argc, argv);
@@ -288,6 +294,7 @@ static int handle_global_options(int argc, char **argv)
288294
{ "quiet", no_argument, NULL, 'q' },
289295
{ "log", required_argument, NULL, OPT_LOG },
290296
{ "param", required_argument, NULL, GETOPT_VAL_PARAM },
297+
{ "dry-run", no_argument, NULL, GETOPT_VAL_DRY_RUN },
291298
{ NULL, 0, NULL, 0}
292299
};
293300
int shift;
@@ -316,6 +323,9 @@ static int handle_global_options(int argc, char **argv)
316323
case GETOPT_VAL_PARAM:
317324
bconf_save_param(optarg);
318325
break;
326+
case GETOPT_VAL_DRY_RUN:
327+
bconf_set_dry_run();
328+
break;
319329
case 'v':
320330
bconf_be_verbose();
321331
break;

cmds/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum {
2222
CMD_ALIAS = (1 << 1), /* alias of next command in cmd_group */
2323
CMD_FORMAT_TEXT = (1 << 2), /* output as plain text */
2424
CMD_FORMAT_JSON = (1 << 3), /* output in json */
25+
CMD_DRY_RUN = (1 << 4), /* Accepts global --dry-run option. */
2526
};
2627

2728
#define CMD_FORMAT_MASK (CMD_FORMAT_TEXT | CMD_FORMAT_JSON)

common/help.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct cmd_group;
3737

3838
#define GETOPT_VAL_HELP 520
3939
#define GETOPT_VAL_PARAM 521
40+
#define GETOPT_VAL_DRY_RUN 522
4041

4142
#define ARGV0_BUF_SIZE PATH_MAX
4243

@@ -83,7 +84,7 @@ struct cmd_group;
8384
"Global options:"
8485

8586
#define HELPINFO_INSERT_FORMAT "--format TYPE"
86-
87+
#define HELPINFO_INSERT_DRY_RUN OPTLINE("--dry-run", "do not do any active/changing actions")
8788
#define HELPINFO_INSERT_VERBOSE OPTLINE("-v|--verbose", "increase output verbosity")
8889
#define HELPINFO_INSERT_QUIET OPTLINE("-q|--quiet", "print only errors")
8990

common/utils.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,16 @@ void bconf_save_param(const char *str)
10001000
}
10011001
}
10021002

1003+
void bconf_set_dry_run(void)
1004+
{
1005+
pr_verbose(LOG_INFO, "Dry-run requested\n");
1006+
bconf.dry_run = 1;
1007+
}
1008+
1009+
bool bconf_is_dry_run(void)
1010+
{
1011+
return bconf.dry_run == 1;
1012+
}
10031013

10041014
/* Returns total size of main memory in bytes, -1UL if error. */
10051015
unsigned long total_memory(void)

common/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ struct btrfs_config {
8787
* > 0: verbose level
8888
*/
8989
int verbose;
90+
/* Command line request to skip any modification actions. */
91+
int dry_run;
9092
struct list_head params;
9193
};
9294
extern struct btrfs_config bconf;
@@ -102,6 +104,8 @@ void bconf_be_verbose(void);
102104
void bconf_be_quiet(void);
103105
void bconf_add_param(const char *key, const char *value);
104106
void bconf_save_param(const char *str);
107+
void bconf_set_dry_run(void);
108+
bool bconf_is_dry_run(void);
105109
const char *bconf_param_value(const char *key);
106110

107111
/* Pseudo random number generator wrappers */

0 commit comments

Comments
 (0)