Skip to content

Commit

Permalink
btrfs-progs: prop set: add force parameter
Browse files Browse the repository at this point in the history
Add option support to force the value change. This allows to do safety
checks by default and warn user that something might break. Using the
force will override that and changing the property should do change
itself and additionally any other changes that could break some
use cases.

Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
kdave committed Oct 7, 2021
1 parent 49bd94c commit 332c193
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
8 changes: 7 additions & 1 deletion Documentation/btrfs-property.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ Lists available properties with their descriptions for the given object.
+
See the description of *get* subcommand for the meaning of each option.

*set* [-t <type>] <object> <name> <value>::
*set* [-f] [-t <type>] <object> <name> <value>::
Sets a property on a btrfs object.
+
See the description of *get* subcommand for the meaning of each option.
+
`Options`
+
-f::::
Force the change. Changing some properties may involve safety checks or
additional changes that depend on the properties semantics.

EXIT STATUS
-----------
Expand Down
42 changes: 28 additions & 14 deletions cmds/property.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
static int prop_read_only(enum prop_object_type type,
const char *object,
const char *name,
const char *value)
const char *value,
bool force)
{
enum btrfs_util_error err;
bool read_only;
Expand Down Expand Up @@ -79,7 +80,8 @@ static int prop_read_only(enum prop_object_type type,
static int prop_label(enum prop_object_type type,
const char *object,
const char *name,
const char *value)
const char *value,
bool force)
{
int ret;

Expand All @@ -99,7 +101,8 @@ static int prop_label(enum prop_object_type type,
static int prop_compression(enum prop_object_type type,
const char *object,
const char *name,
const char *value)
const char *value,
bool force)
{
int ret;
ssize_t sret;
Expand Down Expand Up @@ -347,7 +350,7 @@ static int dump_prop(const struct prop_handler *prop,

if ((types & type) && (prop->types & type)) {
if (!name_and_help)
ret = prop->handler(type, object, prop->name, NULL);
ret = prop->handler(type, object, prop->name, NULL, false);
else
printf("%-20s%s\n", prop->name, prop->desc);
}
Expand Down Expand Up @@ -379,7 +382,7 @@ static int dump_props(int types, const char *object, int name_and_help)
}

static int setget_prop(int types, const char *object,
const char *name, const char *value)
const char *name, const char *value, bool force)
{
int ret;
const struct prop_handler *prop = NULL;
Expand Down Expand Up @@ -407,7 +410,7 @@ static int setget_prop(int types, const char *object,
return 1;
}

ret = prop->handler(types, object, name, value);
ret = prop->handler(types, object, name, value, force);

if (ret < 0)
ret = 1;
Expand All @@ -420,19 +423,26 @@ static int setget_prop(int types, const char *object,

static int parse_args(const struct cmd_struct *cmd, int argc, char **argv,
int *types, char **object,
char **name, char **value, int min_nonopt_args)
char **name, char **value, int min_nonopt_args,
bool *force)
{
int ret;
char *type_str = NULL;
int max_nonopt_args = 1;

*force = false;

optind = 1;
while (1) {
int c = getopt(argc, argv, "t:");
int c = getopt(argc, argv, "ft:");
if (c < 0)
break;

switch (c) {
case 'f':
/* TODO: do not accept for get/list */
*force = true;
break;
case 't':
type_str = optarg;
break;
Expand Down Expand Up @@ -516,12 +526,13 @@ static int cmd_property_get(const struct cmd_struct *cmd,
char *object = NULL;
char *name = NULL;
int types = 0;
bool force;

if (parse_args(cmd, argc, argv, &types, &object, &name, NULL, 1))
if (parse_args(cmd, argc, argv, &types, &object, &name, NULL, 1, &force))
return 1;

if (name)
ret = setget_prop(types, object, name, NULL);
ret = setget_prop(types, object, name, NULL, force);
else
ret = dump_props(types, object, 0);

Expand All @@ -530,13 +541,14 @@ static int cmd_property_get(const struct cmd_struct *cmd,
static DEFINE_SIMPLE_COMMAND(property_get, "get");

static const char * const cmd_property_set_usage[] = {
"btrfs property set [-t <type>] <object> <name> <value>",
"btrfs property set [-f] [-t <type>] <object> <name> <value>",
"Set a property on a btrfs object",
"Set a property on a btrfs object where object is a path to file or",
"directory and can also represent the filesystem or device based on the type",
"",
"-t <TYPE> list properties for the given object type (inode, subvol,",
" filesystem, device)",
"-f force the change, could potentially break something\n",
NULL
};

Expand All @@ -548,11 +560,12 @@ static int cmd_property_set(const struct cmd_struct *cmd,
char *name = NULL;
char *value = NULL;
int types = 0;
bool force = false;

if (parse_args(cmd, argc, argv, &types, &object, &name, &value, 3))
if (parse_args(cmd, argc, argv, &types, &object, &name, &value, 3, &force))
return 1;

ret = setget_prop(types, object, name, value);
ret = setget_prop(types, object, name, value, force);

return ret;
}
Expand All @@ -576,8 +589,9 @@ static int cmd_property_list(const struct cmd_struct *cmd,
int ret;
char *object = NULL;
int types = 0;
bool force;

if (parse_args(cmd, argc, argv, &types, &object, NULL, NULL, 1))
if (parse_args(cmd, argc, argv, &types, &object, NULL, NULL, 1, &force))
return 1;

ret = dump_props(types, object, 1);
Expand Down
3 changes: 2 additions & 1 deletion cmds/props.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ enum prop_object_type {
typedef int (*prop_handler_t)(enum prop_object_type type,
const char *object,
const char *name,
const char *value);
const char *value,
bool force);

struct prop_handler {
const char *name;
Expand Down

0 comments on commit 332c193

Please sign in to comment.