networkctl-config-file: check for masked config before editing/showing

This commit is contained in:
Mike Yuan
2023-12-20 12:46:46 +08:00
parent 078fad363e
commit 7ce6a0960f
+24 -3
View File
@@ -29,7 +29,12 @@ typedef enum ReloadFlags {
RELOAD_UDEVD = 1 << 1,
} ReloadFlags;
static int get_config_files_by_name(const char *name, char **ret_path, char ***ret_dropins) {
static int get_config_files_by_name(
const char *name,
bool allow_masked,
char **ret_path,
char ***ret_dropins) {
_cleanup_free_ char *path = NULL;
int r;
@@ -45,6 +50,16 @@ static int get_config_files_by_name(const char *name, char **ret_path, char ***r
r = RET_NERRNO(access(p, F_OK));
if (r >= 0) {
if (!allow_masked) {
r = null_or_empty_path(p);
if (r < 0)
return log_debug_errno(r,
"Failed to check if network config '%s' is masked: %m",
name);
if (r > 0)
return -ERFKILL;
}
path = TAKE_PTR(p);
break;
}
@@ -425,7 +440,9 @@ int verb_edit(int argc, char *argv[], void *userdata) {
else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid network config name '%s'.", *name);
r = get_config_files_by_name(*name, &path, &dropins);
r = get_config_files_by_name(*name, /* allow_masked = */ false, &path, &dropins);
if (r == -ERFKILL)
return log_error_errno(r, "Network config '%s' is masked.", *name);
if (r == -ENOENT) {
if (arg_drop_in)
return log_error_errno(r, "Cannot find network config '%s'.", *name);
@@ -474,11 +491,15 @@ int verb_cat(int argc, char *argv[], void *userdata) {
if (r < 0)
return RET_GATHER(ret, r);
} else {
r = get_config_files_by_name(*name, &path, &dropins);
r = get_config_files_by_name(*name, /* allow_masked = */ false, &path, &dropins);
if (r == -ENOENT) {
RET_GATHER(ret, log_error_errno(r, "Cannot find network config file '%s'.", *name));
continue;
}
if (r == -ERFKILL) {
RET_GATHER(ret, log_debug_errno(r, "Network config '%s' is masked, ignoring.", *name));
continue;
}
if (r < 0) {
log_error_errno(r, "Failed to get the path of network config '%s': %m", *name);
return RET_GATHER(ret, r);