diff options
author | David Zeuthen <davidz@redhat.com> | 2010-10-05 16:13:34 -0400 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2010-10-05 16:13:34 -0400 |
commit | d03c35c1d0a486a45e9206267aca155fc4448394 (patch) | |
tree | 63f36de87b7c8f7cead5533746fd510524b855ef | |
parent | 273829916f9969c81d53e5966fa7e671327c7e73 (diff) |
Don't use flags for StcItemState
All the possible flags were actually mutually exclusive already. So
better just use a normal enumeration.
Signed-off-by: David Zeuthen <davidz@redhat.com>
-rw-r--r-- | stc/stc.c | 65 | ||||
-rw-r--r-- | stc/stcenums.h | 35 | ||||
-rw-r--r-- | stc/stcitem.c | 44 | ||||
-rw-r--r-- | stc/test.c | 4 |
4 files changed, 42 insertions, 106 deletions
@@ -38,70 +38,24 @@ static GMainLoop *loop = NULL; /* ---------------------------------------------------------------------------------------------------- */ -static gchar * -flags_to_str (GType flags_type, guint value) -{ - GString *str; - GFlagsClass *klass; - GFlagsValue *flags_value; - guint n; - - str = g_string_new (NULL); - - klass = g_type_class_ref (flags_type); - if (klass == NULL) - { - g_string_append (str, "<Unregistered flags type>"); - goto out; - } - - for (n = 0; n < 32; n++) - { - if ((value & (1<<n)) != 0) - { - flags_value = g_flags_get_first_value (klass, (1<<n)); - if (str->len > 0) - g_string_append_c (str, ','); - if (flags_value == NULL) - g_string_append_printf (str, "<Unregistered bit %d>", n); - else - g_string_append (str, flags_value->value_nick); - } - } - - if (str->len == 0) - { - flags_value = g_flags_get_first_value (klass, 0); - if (flags_value == NULL) - g_string_append (str, "<No flags set>"); - else - g_string_append (str, flags_value->value_nick); - } - - out: - if (klass != NULL) - g_type_class_unref (klass); - return g_string_free (str, FALSE); -} - static const gchar * -item_type_to_str (StcItemType type) +enum_to_str (GType gtype, StcItemType value) { GEnumClass *klass; - GEnumValue *value; + GEnumValue *enum_value; const gchar *ret; ret = "<Unregistered enum type>"; - klass = g_type_class_ref (STC_TYPE_ITEM_TYPE); + klass = g_type_class_ref (gtype); if (klass == NULL) goto out; ret = "<Invalid enum value>"; - value = g_enum_get_value (klass, type); - if (value == NULL) + enum_value = g_enum_get_value (klass, value); + if (enum_value == NULL) goto out; - ret = value->value_nick; + ret = enum_value->value_nick; out: if (klass != NULL) @@ -331,7 +285,6 @@ handle_command_list (gint *argc, const gchar *target; const gchar *const *options; const gchar *const *deps; - gchar *state_str; type = stc_item_get_item_type (item); id = stc_item_get_id (item); @@ -348,7 +301,7 @@ handle_command_list (gint *argc, _color_get (_COLOR_FG_WHITE), _color_get (_COLOR_RESET), _color_get (_COLOR_FG_GREEN), - item_type_to_str (type), + enum_to_str (STC_TYPE_ITEM_TYPE, type), _color_get (_COLOR_RESET)); g_print (" %sTarget:%s %s%s%s\n", @@ -426,15 +379,13 @@ handle_command_list (gint *argc, g_print ("\n"); } - state_str = flags_to_str (STC_TYPE_ITEM_STATE, stc_item_get_state (item)); g_print (" %sState:%s %s%s%s%s\n", _color_get (_COLOR_FG_WHITE), _color_get (_COLOR_RESET), _color_get (_COLOR_FG_YELLOW), _color_get (_COLOR_BOLD_ON), - state_str, + enum_to_str (STC_TYPE_ITEM_STATE, stc_item_get_state (item)), _color_get (_COLOR_RESET)); - g_free (state_str); g_print ("\n"); } diff --git a/stc/stcenums.h b/stc/stcenums.h index b736359..28b5ad6 100644 --- a/stc/stcenums.h +++ b/stc/stcenums.h @@ -69,35 +69,20 @@ typedef enum /** * StcItemState: - * @STC_ITEM_STATE_NONE: No flags are set. - * @STC_ITEM_STATE_IS_APPLIED: A device/object matching the configuration item is already configured. - * @STC_ITEM_STATE_CAN_APPLY: The configuration can be applied (e.g. all requisite devices are present and all dependencies are fulfilled). - * @STC_ITEM_STATE_CAN_APPLY_DEGRADED: The configuration can be applied but the device will be started degraded. + * @STC_ITEM_STATE_NOT_APPLIED: Configuration item is not applied. + * @STC_ITEM_STATE_CAN_APPLY_DEGRADED: Like %STC_ITEM_STATE_CAN_APPLY but the device will be started degraded. + * @STC_ITEM_STATE_CAN_APPLY: The configuration item is not applied but it can be applied (e.g. all requisite devices are present and all dependencies are fulfilled). + * @STC_ITEM_STATE_APPLIED: The configuration item is applied. * - * Flag enumeration describing what state a configuration item currently is in. - * - * Note that %STC_ITEM_STATE_IS_APPLIED is set for an item even if the - * requested options does not exactly match the request options. For - * example, a %STC_ITEM_TYPE_FILESYSTEM configuration item may be set - * if a filesystem matching the item is mounted but the mount options - * doesn't match. On the other hand, for a filesystem, the mount path - * needs to match - e.g. if the item has the option - * <literal>Filesystem:mount_path</literal> set to - * <filename>/mnt/bar</filename> then it won't be tagged with - * %STC_ITEM_STATE_IS_APPLIED if the filesystem is mounted at - * <filename>/mnt/foo</filename>. - * - * States %STC_ITEM_STATE_CAN_APPLY and - * %STC_ITEM_STATE_CAN_APPLY_DEGRADED may be set even if - * %STC_ITEM_STATE_IS_APPLIED is also set - e.g. they are independent - * of whether the configuration is actually applied or not. + * Enumeration describing the current state of a configuration + * item */ typedef enum { - STC_ITEM_STATE_NONE = 0, - STC_ITEM_STATE_IS_APPLIED = (1<<0), - STC_ITEM_STATE_CAN_APPLY = (1<<1), - STC_ITEM_STATE_CAN_APPLY_DEGRADED = (1<<2) + STC_ITEM_STATE_NOT_APPLIED, + STC_ITEM_STATE_CAN_APPLY_DEGRADED, + STC_ITEM_STATE_CAN_APPLY, + STC_ITEM_STATE_APPLIED, } StcItemState; G_END_DECLS diff --git a/stc/stcitem.c b/stc/stcitem.c index ab0b189..c4fd4ae 100644 --- a/stc/stcitem.c +++ b/stc/stcitem.c @@ -179,7 +179,7 @@ stc_item_get_target (StcItem *item) StcItemState stc_item_get_state (StcItem *item) { - g_return_val_if_fail (STC_IS_ITEM (item), STC_ITEM_STATE_NONE); + g_return_val_if_fail (STC_IS_ITEM (item), -1); return item->state; } @@ -364,7 +364,7 @@ _stc_item_update_state_filesystem (StcItem *item, GList *devices; GList *l; - ret = STC_ITEM_STATE_NONE; + ret = STC_ITEM_STATE_NOT_APPLIED; device_with_fs = NULL; @@ -375,7 +375,7 @@ _stc_item_update_state_filesystem (StcItem *item, if (device != NULL) { if (g_strcmp0 (g_udev_device_get_property (device, "ID_FS_USAGE"), "filesystem") == 0) - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; device_with_fs = g_object_ref (device); g_object_unref (device); } @@ -392,7 +392,7 @@ _stc_item_update_state_filesystem (StcItem *item, if (g_strcmp0 (g_udev_device_get_property (device, "ID_FS_USAGE"), "filesystem") == 0) { device_with_fs = g_object_ref (device); - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; break; } } @@ -411,7 +411,7 @@ _stc_item_update_state_filesystem (StcItem *item, if (g_strcmp0 (g_udev_device_get_property (device, "ID_FS_USAGE"), "filesystem") == 0) { device_with_fs = g_object_ref (device); - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; break; } } @@ -423,7 +423,7 @@ _stc_item_update_state_filesystem (StcItem *item, g_warning ("Unsupported target type %s for Filesystem", item->target); } - /* figure out IS_APPLIED */ + /* figure out if IS_APPLIED */ if (device_with_fs != NULL) { GList *mounts; @@ -439,7 +439,7 @@ _stc_item_update_state_filesystem (StcItem *item, _StcMount *mount = _STC_MOUNT (l->data); if (g_strcmp0 (desired_mount_path, _stc_mount_get_mount_path (mount)) == 0) { - ret |= STC_ITEM_STATE_IS_APPLIED; + ret = STC_ITEM_STATE_APPLIED; break; } } @@ -483,7 +483,7 @@ _stc_item_update_state_luks (StcItem *item, GList *devices; GList *l; - ret = STC_ITEM_STATE_NONE; + ret = STC_ITEM_STATE_NOT_APPLIED; device_with_crypto = NULL; @@ -496,7 +496,7 @@ _stc_item_update_state_luks (StcItem *item, if (g_strcmp0 (g_udev_device_get_property (device, "ID_FS_USAGE"), "crypto") == 0 && g_strcmp0 (g_udev_device_get_property (device, "ID_FS_TYPE"), "crypto_LUKS") == 0) { - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; device_with_crypto = g_object_ref (device); } g_object_unref (device); @@ -514,7 +514,7 @@ _stc_item_update_state_luks (StcItem *item, if (g_strcmp0 (g_udev_device_get_property (device, "ID_FS_USAGE"), "crypto") == 0 && g_strcmp0 (g_udev_device_get_property (device, "ID_FS_TYPE"), "crypto_LUKS") == 0) { - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; device_with_crypto = g_object_ref (device); break; } @@ -527,7 +527,7 @@ _stc_item_update_state_luks (StcItem *item, g_warning ("Unsupported target type %s for LUKS", item->target); } - /* figure out IS_APPLIED */ + /* figure out if IS_APPLIED */ if (device_with_crypto != NULL) { gchar *uuid_prefix; @@ -545,7 +545,7 @@ _stc_item_update_state_luks (StcItem *item, dm_uuid = g_udev_device_get_property (device, "DM_UUID"); if (dm_uuid != NULL && g_str_has_prefix (dm_uuid, uuid_prefix)) { - ret |= STC_ITEM_STATE_IS_APPLIED; + ret = STC_ITEM_STATE_APPLIED; break; } } @@ -571,7 +571,7 @@ _stc_item_update_state_md_raid (StcItem *item, gchar *md_level; gboolean array_seen; - ret = STC_ITEM_STATE_NONE; + ret = STC_ITEM_STATE_NOT_APPLIED; num_components_seen = 0; array_seen = FALSE; @@ -642,28 +642,28 @@ _stc_item_update_state_md_raid (StcItem *item, if (g_strcmp0 (md_level, "raid0") == 0) { if (num_components_seen == md_devices) - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; } else if (g_strcmp0 (md_level, "raid1") == 0) { if (num_components_seen >= 1) - ret |= STC_ITEM_STATE_CAN_APPLY_DEGRADED; + ret = STC_ITEM_STATE_CAN_APPLY_DEGRADED; if (num_components_seen == md_devices) - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; } else if (g_strcmp0 (md_level, "raid5") == 0) { if (md_devices - num_components_seen <= 1) - ret |= STC_ITEM_STATE_CAN_APPLY_DEGRADED; + ret = STC_ITEM_STATE_CAN_APPLY_DEGRADED; if (num_components_seen == md_devices) - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; } else if (g_strcmp0 (md_level, "raid6") == 0) { if (md_devices - num_components_seen <= 2) - ret |= STC_ITEM_STATE_CAN_APPLY_DEGRADED; + ret = STC_ITEM_STATE_CAN_APPLY_DEGRADED; if (num_components_seen == md_devices) - ret |= STC_ITEM_STATE_CAN_APPLY; + ret = STC_ITEM_STATE_CAN_APPLY; } else if (md_level != NULL) { @@ -671,7 +671,7 @@ _stc_item_update_state_md_raid (StcItem *item, } if (array_seen) - ret |= STC_ITEM_STATE_IS_APPLIED; + ret = STC_ITEM_STATE_APPLIED; g_free (md_level); @@ -687,7 +687,7 @@ _stc_item_update_state (StcItem *item) g_return_if_fail (STC_IS_ITEM (item)); - state = STC_ITEM_STATE_NONE; + state = STC_ITEM_STATE_NOT_APPLIED; gudev_client = _stc_monitor_get_gudev_client (item->monitor); mount_monitor = _stc_monitor_get_mount_monitor (item->monitor); @@ -530,8 +530,8 @@ main (int argc, g_test_add_func ("/stc/monitoring", test_stc_monitoring); /* it would be nice if we could easily run tests to check the - * returned StcItemState value. That would require returning fake - * udev and /proc/self/mountinfo data... + * returned StcItemState value. Unfortunately that requires + * returning fake udev and /proc/self/mountinfo data... */ ret = g_test_run(); |