Skip to content

Commit

Permalink
tree-wide: refactoring to avoid onditionals with Omitted Operands
Browse files Browse the repository at this point in the history
  • Loading branch information
slyon committed Aug 21, 2024
1 parent 51d6ad9 commit e6e0c26
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ int main(int argc, char** argv)
start_unit_jit("systemd-networkd.service");
}
g_autofree char* glob_run = g_build_path(G_DIR_SEPARATOR_S,
rootdir ?: G_DIR_SEPARATOR_S,
!!rootdir ? rootdir : G_DIR_SEPARATOR_S,
"run/systemd/system/netplan-*.service",
NULL);
if (!glob(glob_run, 0, NULL, &gl)) {
Expand Down
7 changes: 4 additions & 3 deletions src/netplan.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,8 @@ netplan_netdef_write_yaml(
filename = g_strconcat("90-NM-", netdef->backend_settings.uuid, ".yaml", NULL);
else
filename = g_strconcat("10-netplan-", netdef->id, ".yaml", NULL);
path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
path = g_build_path(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : G_DIR_SEPARATOR_S,
"etc", "netplan", filename, NULL);

/* Start rendering YAML output */
yaml_emitter_t emitter_data;
Expand Down Expand Up @@ -1149,7 +1150,7 @@ netplan_state_write_yaml_file(const NetplanState* np_state, const char* filename
GList* to_write = NULL;
int out_fd;

path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
path = g_build_path(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);

while (iter) {
NetplanNetDefinition* netdef = iter->data;
Expand Down Expand Up @@ -1218,7 +1219,7 @@ netplan_state_update_yaml_hierarchy(const NetplanState* np_state, const char* de
g_assert(default_filename != NULL && *default_filename != '\0');

perfile_netdefs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)g_list_free);
default_path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", default_filename, NULL);
default_path = g_build_path(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : G_DIR_SEPARATOR_S, "etc", "netplan", default_filename, NULL);
int out_fd = -1;

/* Dump global conf to the default path */
Expand Down
11 changes: 7 additions & 4 deletions src/nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -983,11 +983,12 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
/* Create /run/NetworkManager/ with 755 permissions if the folder is missing.
* Letting the next invokation of _netplan_safe_mkdir_p_dir do it would
* result in more restrictive access because of the call to umask. */
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", "run/NetworkManager/", NULL);
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : "",
"run/NetworkManager/", NULL);
if (!g_file_test(nm_run_path, G_FILE_TEST_EXISTS))
_netplan_safe_mkdir_p_dir(nm_run_path);

full_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", conf_path, NULL);
full_path = g_strjoin(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : "", conf_path, NULL);

/* NM connection files might contain secrets, and NM insists on tight permissions */
orig_umask = umask(077);
Expand Down Expand Up @@ -1180,8 +1181,10 @@ netplan_state_finish_nm_write(
gboolean
_netplan_nm_cleanup(const char* rootdir)
{
g_autofree char* confpath = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/netplan.conf", NULL);
g_autofree char* global_manage_path = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
g_autofree char* confpath = g_strjoin(NULL, !!rootdir ? rootdir : "",
"/run/NetworkManager/conf.d/netplan.conf", NULL);
g_autofree char* global_manage_path = g_strjoin(NULL, !!rootdir ? rootdir : "",
"/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
unlink(confpath);
unlink(global_manage_path);
_netplan_unlink_glob(rootdir, "/run/NetworkManager/system-connections/netplan-*");
Expand Down
3 changes: 2 additions & 1 deletion src/openvswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ STATIC gboolean
write_ovs_systemd_unit(const char* id, const GString* cmds, const char* rootdir, gboolean physical, gboolean cleanup, const char* dependency, GError** error)
{
g_autofree char* escaped_netdef_id = g_uri_escape_string(id, NULL, TRUE);
g_autofree char* link = g_strjoin(NULL, rootdir ?: "", "/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
g_autofree char* link = g_strjoin(NULL, !!rootdir ? rootdir : "",
"/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
g_autofree char* path = g_strjoin(NULL, "/run/systemd/system/netplan-ovs-", escaped_netdef_id, ".service", NULL);

GString* s = g_string_new("[Unit]\n");
Expand Down
6 changes: 4 additions & 2 deletions src/sriov.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ STATIC gboolean
write_sriov_rebind_systemd_unit(GHashTable* pfs, const char* rootdir, GError** error)
{
g_autofree gchar* id_escaped = NULL;
g_autofree char* link = g_strjoin(NULL, rootdir ?: "", "/run/systemd/system/multi-user.target.wants/netplan-sriov-rebind.service", NULL);
g_autofree char* link = g_strjoin(NULL, !!rootdir ? rootdir : "",
"/run/systemd/system/multi-user.target.wants/netplan-sriov-rebind.service", NULL);
g_autofree char* path = g_strjoin(NULL, "/run/systemd/system/netplan-sriov-rebind.service", NULL);

GHashTableIter iter;
Expand Down Expand Up @@ -75,7 +76,8 @@ STATIC gboolean
write_sriov_apply_systemd_unit(GHashTable* pfs, const char* rootdir, GError** error)
{
g_autofree gchar* id_escaped = NULL;
g_autofree char* link = g_strjoin(NULL, rootdir ?: "", "/run/systemd/system/multi-user.target.wants/netplan-sriov-apply.service", NULL);
g_autofree char* link = g_strjoin(NULL, !!rootdir ? rootdir : "",
"/run/systemd/system/multi-user.target.wants/netplan-sriov-apply.service", NULL);
g_autofree char* path = g_strjoin(NULL, "/run/systemd/system/netplan-sriov-apply.service", NULL);
GHashTableIter iter;
gpointer key;
Expand Down
17 changes: 10 additions & 7 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void _netplan_g_string_free_to_file(GString* s, const char* rootdir, const char*
GError* error = NULL;

path_suffix = g_strjoin(NULL, path, suffix, NULL);
full_path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, path_suffix, NULL);
full_path = g_build_path(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : G_DIR_SEPARATOR_S, path_suffix, NULL);
_netplan_safe_mkdir_p_dir(full_path);
if (!g_file_set_contents(full_path, contents, -1, &error)) {
/* the mkdir() just succeeded, there is no sensible
Expand All @@ -104,7 +104,7 @@ void _netplan_g_string_free_to_file_with_permissions(GString* s, const char* roo
int ret = 0;

path_suffix = g_strjoin(NULL, path, suffix, NULL);
full_path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, path_suffix, NULL);
full_path = g_build_path(G_DIR_SEPARATOR_S, !!rootdir ? rootdir : G_DIR_SEPARATOR_S, path_suffix, NULL);
_netplan_safe_mkdir_p_dir(full_path);
if (!g_file_set_contents_full(full_path, contents, -1, G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING, mode, &error)) {
/* the mkdir() just succeeded, there is no sensible
Expand Down Expand Up @@ -144,7 +144,7 @@ _netplan_unlink_glob(const char* rootdir, const char* _glob)
{
glob_t gl;
int rc;
g_autofree char* rglob = g_strjoin(NULL, rootdir ?: "", G_DIR_SEPARATOR_S, _glob, NULL);
g_autofree char* rglob = g_strjoin(NULL, !!rootdir ? rootdir : "", G_DIR_SEPARATOR_S, _glob, NULL);

rc = glob(rglob, GLOB_BRACE, NULL, &gl);
if (rc != 0 && rc != GLOB_NOMATCH) {
Expand All @@ -166,7 +166,7 @@ int _netplan_find_yaml_glob(const char* rootdir, glob_t* out_glob)
{
int rc;
g_autofree char* rglob = g_build_path(G_DIR_SEPARATOR_S,
rootdir ?: G_DIR_SEPARATOR_S,
!!rootdir ? rootdir : G_DIR_SEPARATOR_S,
"{lib,etc,run}/netplan/*.yaml", NULL);
rc = glob(rglob, GLOB_BRACE, NULL, out_glob);
if (rc != 0 && rc != GLOB_NOMATCH) {
Expand Down Expand Up @@ -1014,20 +1014,23 @@ netplan_netdef_match_interface(const NetplanNetDefinition* netdef, const char* n
return !g_strcmp0(name, netdef->id);

if (netdef->match.mac) {
if (g_ascii_strcasecmp(netdef->match.mac ?: "", mac ?: ""))
if (g_ascii_strcasecmp(!!netdef->match.mac ? netdef->match.mac : "", !!mac ? mac : "")) {
return FALSE;
}
}

if (netdef->match.original_name) {
if (!name || fnmatch(netdef->match.original_name, name, 0))
if (!name || fnmatch(netdef->match.original_name, name, 0)) {
return FALSE;
}
}

if (netdef->match.driver) {
gboolean matches_driver = FALSE;
char** tokens;
if (!driver_name)
if (!driver_name) {
return FALSE;
}
tokens = g_strsplit(netdef->match.driver, "\t", -1);
for (char** it = tokens; *it; it++) {
if (fnmatch(*it, driver_name, 0) == 0) {
Expand Down

0 comments on commit e6e0c26

Please sign in to comment.