[gnome-multi-writer] Various C99 cleanups
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-multi-writer] Various C99 cleanups
- Date: Thu, 1 Nov 2018 11:37:19 +0000 (UTC)
commit 30bbce17e8f7402be300447fcd1a398dd9c7d44b
Author: Richard Hughes <richard hughsie com>
Date: Thu Nov 1 11:25:40 2018 +0000
Various C99 cleanups
meson.build | 1 +
src/gmw-device.c | 27 ++++------
src/gmw-main.c | 155 ++++++++++++++++++++++---------------------------------
src/gmw-probe.c | 79 +++++++++-------------------
4 files changed, 97 insertions(+), 165 deletions(-)
---
diff --git a/meson.build b/meson.build
index 9eed499..1a9ff07 100644
--- a/meson.build
+++ b/meson.build
@@ -36,6 +36,7 @@ test_args = [
'-Wno-strict-aliasing',
'-Wno-suggest-attribute=format',
'-Wno-unused-parameter',
+ '-Wno-cast-function-type',
'-Wold-style-definition',
'-Woverride-init',
'-Wpacked',
diff --git a/src/gmw-device.c b/src/gmw-device.c
index e94d007..f62cf21 100644
--- a/src/gmw-device.c
+++ b/src/gmw-device.c
@@ -184,7 +184,6 @@ const gchar *
gmw_device_get_icon (GmwDevice *device)
{
GmwDevicePrivate *priv = gmw_device_get_instance_private (device);
- const gchar *tmp;
g_return_val_if_fail (GMW_IS_DEVICE (device), NULL);
@@ -195,7 +194,7 @@ gmw_device_get_icon (GmwDevice *device)
/* try to get from UDisks */
if (priv->udisks_block != NULL) {
- tmp = udisks_block_get_hint_icon_name (priv->udisks_block);
+ const gchar *tmp = udisks_block_get_hint_icon_name (priv->udisks_block);
if (tmp != NULL && tmp[0] != '\0')
return tmp;
}
@@ -207,13 +206,12 @@ gchar *
gmw_device_get_description (GmwDevice *device)
{
GmwDevicePrivate *priv = gmw_device_get_instance_private (device);
- guint64 size;
g_return_val_if_fail (GMW_IS_DEVICE (device), NULL);
/* waiting to be written */
if (priv->state == GMW_DEVICE_STATE_IDLE) {
- size = gmw_device_get_size (device) / (1000 * 1000 * 1000);
+ guint64 size = gmw_device_get_size (device) / (1000 * 1000 * 1000);
if (size == 0)
return g_strdup (priv->name);
return g_strdup_printf ("%s (%" G_GUINT64_FORMAT "GB)",
@@ -322,7 +320,6 @@ void
gmw_device_set_name (GmwDevice *device, const gchar *name)
{
GmwDevicePrivate *priv = gmw_device_get_instance_private (device);
- guint i;
struct {
const gchar *old;
const gchar *new;
@@ -335,7 +332,7 @@ gmw_device_set_name (GmwDevice *device, const gchar *name)
g_return_if_fail (GMW_IS_DEVICE (device));
/* replace any generic names */
- for (i = 0; replace[i].old != NULL; i++) {
+ for (guint i = 0; replace[i].old != NULL; i++) {
if (g_strcmp0 (replace[i].old, name) == 0) {
name = replace[i].new;
break;
@@ -537,8 +534,6 @@ gchar *
gmw_device_get_quirk_string (GmwDevice *device)
{
GmwDevicePrivate *priv = gmw_device_get_instance_private (device);
- GUsbDevice *child_tmp;
- guint i;
guint number_ics = 0;
g_autoptr(GUsbDevice) usb_hub_child = NULL;
g_autoptr(GUsbDevice) usb_hub = NULL;
@@ -582,8 +577,8 @@ gmw_device_get_quirk_string (GmwDevice *device)
/* get any child hub */
children = g_usb_device_get_children (usb_hub);
- for (i = 0; i < children->len; i++) {
- child_tmp = g_ptr_array_index (children, i);
+ for (guint i = 0; i < children->len; i++) {
+ GUsbDevice *child_tmp = g_ptr_array_index (children, i);
if (g_usb_device_get_device_class (child_tmp) != 0x09)
continue;
if (usb_hub_child == NULL)
@@ -601,8 +596,8 @@ gmw_device_get_quirk_string (GmwDevice *device)
/* count child USB hubs in the device */
if (usb_hub_top != NULL) {
children_top = g_usb_device_get_children (usb_hub_top);
- for (i = 0; i < children_top->len; i++) {
- child_tmp = g_ptr_array_index (children_top, i);
+ for (guint i = 0; i < children_top->len; i++) {
+ GUsbDevice *child_tmp = g_ptr_array_index (children_top, i);
if (g_usb_device_get_device_class (child_tmp) != 0x09)
continue;
number_ics++;
@@ -630,8 +625,6 @@ void
gmw_device_set_usb_device (GmwDevice *device, GUsbDevice *usb_device)
{
GmwDevicePrivate *priv = gmw_device_get_instance_private (device);
- guint i;
- guint j;
g_autofree gchar *hub_id = NULL;
g_autoptr(GUsbDevice) usb_hub = NULL;
g_autoptr(GUsbDevice) usb_hub_parent = NULL;
@@ -826,7 +819,7 @@ gmw_device_set_usb_device (GmwDevice *device, GUsbDevice *usb_device)
(guint) g_usb_device_get_vid (usb_device),
(guint) g_usb_device_get_pid (usb_device),
(guint) g_usb_device_get_port_number (usb_device));
- for (i = 0; quirks[i].hub_label != NULL; i++) {
+ for (guint i = 0; quirks[i].hub_label != NULL; i++) {
/* check grandparent */
if (usb_hub_parent != NULL && quirks[i].hub_parent_vid != 0x0000) {
if (quirks[i].hub_parent_vid != g_usb_device_get_vid (usb_hub_parent))
@@ -855,7 +848,7 @@ gmw_device_set_usb_device (GmwDevice *device, GUsbDevice *usb_device)
/* the specified child just has to exist once */
children = g_usb_device_get_children (usb_hub);
- for (j = 0; j < children->len; j++) {
+ for (guint j = 0; j < children->len; j++) {
tmp = g_ptr_array_index (children, j);
if (g_usb_device_get_vid (tmp) == quirks[i].child_vid &&
g_usb_device_get_pid (tmp) == quirks[i].child_pid) {
@@ -874,7 +867,7 @@ gmw_device_set_usb_device (GmwDevice *device, GUsbDevice *usb_device)
g_autoptr(GPtrArray) children = NULL;
children = g_usb_device_get_children (usb_hub_toplevel);
- for (j = 0; j < children->len; j++) {
+ for (guint j = 0; j < children->len; j++) {
tmp = g_ptr_array_index (children, j);
if (g_usb_device_get_device_class (tmp) == 0x09)
child_cnt++;
diff --git a/src/gmw-main.c b/src/gmw-main.c
index 56cf17c..78a5820 100644
--- a/src/gmw-main.c
+++ b/src/gmw-main.c
@@ -136,11 +136,8 @@ gmw_root_hub_new (guint idx)
static GmwRootHub *
gmw_root_hub_find_by_idx (GPtrArray *root_hubs, guint idx)
{
- GmwRootHub *rh;
- guint i;
-
- for (i = 0; i < root_hubs->len; i++) {
- rh = g_ptr_array_index (root_hubs, i);
+ for (guint i = 0; i < root_hubs->len; i++) {
+ GmwRootHub *rh = g_ptr_array_index (root_hubs, i);
if (rh->idx == idx)
return rh;
}
@@ -158,18 +155,14 @@ gmw_root_hub_find_by_idx (GPtrArray *root_hubs, guint idx)
static GPtrArray *
gmw_root_hub_enumerate (GPtrArray *devices)
{
- GmwDevice *device;
- GmwRootHub *rh;
GPtrArray *array;
- guint i;
- guint idx;
/* put each device into an array of its root hub */
array = g_ptr_array_new_with_free_func ((GDestroyNotify) gmw_root_hub_free);
- for (i = 0; i < devices->len; i++) {
- device = g_ptr_array_index (devices, i);
- idx = gmw_device_get_hub_root (device);
- rh = gmw_root_hub_find_by_idx (array, idx);
+ for (guint i = 0; i < devices->len; i++) {
+ GmwDevice *device = g_ptr_array_index (devices, i);
+ guint idx = gmw_device_get_hub_root (device);
+ GmwRootHub *rh = gmw_root_hub_find_by_idx (array, idx);
if (rh == NULL) {
rh = gmw_root_hub_new (idx);
g_ptr_array_add (array, rh);
@@ -183,9 +176,6 @@ static void
gmw_device_list_sort (GmwPrivate *priv)
{
GmwDevice *device;
- GmwRootHub *rh;
- guint j;
- guint i;
guint idx = 0;
g_autoptr(GPtrArray) root_hubs = NULL;
@@ -195,10 +185,10 @@ gmw_device_list_sort (GmwPrivate *priv)
/* queue the devices according to the root hub they are connected
* to ensure we saturate as many busses as possible */
root_hubs = gmw_root_hub_enumerate (priv->devices);
- for (i = 0; i < priv->devices->len; i++) {
- for (j = 0; j < root_hubs->len; j++) {
+ for (guint i = 0; i < priv->devices->len; i++) {
+ for (guint j = 0; j < root_hubs->len; j++) {
g_autofree gchar *key = NULL;
- rh = g_ptr_array_index (root_hubs, j);
+ GmwRootHub *rh = g_ptr_array_index (root_hubs, j);
if (rh->devices->len <= i)
continue;
device = g_ptr_array_index (rh->devices, i);
@@ -222,14 +212,11 @@ static void gmw_refresh_ui (GmwPrivate *priv);
static void
gmw_main_show_quirks (GmwPrivate *priv)
{
- GmwDevice *device;
- guint i;
-
g_print ("\t/* hub hub-port parent-hub child-device"
" chps dprt chn labl */\n");
- for (i = 0; i < priv->devices->len; i++) {
+ for (guint i = 0; i < priv->devices->len; i++) {
g_autofree gchar *tmp = NULL;
- device = g_ptr_array_index (priv->devices, i);
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
tmp = gmw_device_get_quirk_string (device);
g_print ("\t%s\n", tmp);
}
@@ -315,11 +302,9 @@ gmw_main_rename_labels_cb (GtkLabel *label, const gchar *uri, GmwPrivate *priv)
static void
gmw_refresh_ui (GmwPrivate *priv)
{
- GmwDevice *device;
GtkWidget *grid;
GtkWidget *w;
const guint max_devices_per_column = 10;
- guint i;
/* remove old children */
grid = GTK_WIDGET (gtk_builder_get_object (priv->builder, "grid_status"));
@@ -327,14 +312,13 @@ gmw_refresh_ui (GmwPrivate *priv)
(GtkCallback) gtk_widget_destroy, priv);
/* add new children */
- for (i = 0; i < priv->devices->len; i++) {
+ for (guint i = 0; i < priv->devices->len; i++) {
g_autofree gchar *label_markup = NULL;
g_autofree gchar *label = NULL;
g_autofree gchar *title = NULL;
guint row = i % max_devices_per_column;
guint col = (i / max_devices_per_column) * 4;
-
- device = g_ptr_array_index (priv->devices, i);
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
/* add label */
w = gtk_label_new (NULL);
@@ -786,16 +770,14 @@ out:
static void
gmw_refresh_titlebar (GmwPrivate *priv)
{
- GmwDevice *device;
GtkWidget *w;
gdouble speed_read = 0.f;
gdouble speed_write = 0.f;
- guint i;
g_autoptr(GString) title = NULL;
/* find the throughput totals */
- for (i = 0; i < priv->devices->len; i++) {
- device = g_ptr_array_index (priv->devices, i);
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
speed_read += gmw_device_get_speed_read (device);
speed_write += gmw_device_get_speed_write (device);
}
@@ -1055,11 +1037,9 @@ gmw_udisks_unmount_cb (GObject *source_object,
static UDisksFilesystem *
gmw_udisks_get_filesystem_for_device (GmwPrivate *priv, GmwDevice *device)
{
- guint i;
-
/* this is crude, but fast -- LiveUSB devices will only typically have
* one partition, but very occasionaly two or more */
- for (i = 1; i <= 4; i++) {
+ for (guint i = 1; i <= 4; i++) {
UDisksFilesystem *udisks_fs = NULL;
g_autoptr(GError) error = NULL;
g_autofree gchar *object_path = NULL;
@@ -1118,10 +1098,8 @@ gmw_udisks_unmount_filesystems_sync (GmwPrivate *priv, GmwDevice *device, GError
static void
gmw_start_copy (GmwPrivate *priv)
{
- GmwDevice *device;
GtkWindow *window;
const gchar *action_id = "org.freedesktop.udisks2.open-device";
- guint i;
g_autoptr(GError) error = NULL;
g_autoptr(GPermission) permission = NULL;
@@ -1134,8 +1112,8 @@ gmw_start_copy (GmwPrivate *priv)
g_cancellable_reset (priv->cancellable);
/* unmount all filesystems */
- for (i = 0; i < priv->devices->len; i++) {
- device = g_ptr_array_index (priv->devices, i);
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
if (!gmw_udisks_unmount_filesystems_sync (priv, device, &error)) {
gmw_error_dialog (priv, _("Failed to copy"), error->message);
return;
@@ -1150,7 +1128,7 @@ gmw_start_copy (GmwPrivate *priv)
return;
}
if (!g_permission_get_allowed (permission)) {
- device = g_ptr_array_index (priv->devices, 0);
+ GmwDevice *device = g_ptr_array_index (priv->devices, 0);
if (!gmw_auth_dummy_restore (priv, device, &error)) {
g_dbus_error_strip_remote_error (error);
gmw_error_dialog (priv,
@@ -1176,9 +1154,9 @@ gmw_start_copy (GmwPrivate *priv)
_("Writing ISO to devices"));
/* start a thread for each copy operation */
- for (i = 0; i < priv->devices->len; i++) {
+ for (guint i = 0; i < priv->devices->len; i++) {
g_autofree gchar *title = NULL;
- device = g_ptr_array_index (priv->devices, i);
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
gmw_device_set_state (device, GMW_DEVICE_STATE_WAITING);
g_thread_pool_push (priv->thread_pool, device, &error);
}
@@ -1595,14 +1573,9 @@ gmw_udisks_object_removed_cb (GDBusObjectManager *object_manager,
GDBusObject *dbus_object,
GmwPrivate *priv)
{
- GmwDevice *device;
- guint i;
- const gchar *tmp;
-
- /* remove device */
- tmp = g_dbus_object_get_object_path (dbus_object);
- for (i = 0; i < priv->devices->len; i++) {
- device = g_ptr_array_index (priv->devices, i);
+ const gchar *tmp = g_dbus_object_get_object_path (dbus_object);
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GmwDevice *device = g_ptr_array_index (priv->devices, i);
if (g_strcmp0 (gmw_device_get_object_path (device), tmp) == 0) {
g_ptr_array_remove (priv->devices, device);
gmw_device_list_sort (priv);
@@ -1620,7 +1593,6 @@ gmw_udisks_client_connect_cb (GObject *source_object,
{
GmwPrivate *priv = (GmwPrivate *) user_data;
GDBusObjectManager *object_manager;
- GList *l;
GList *objects;
g_autoptr(GError) error = NULL;
@@ -1635,7 +1607,7 @@ gmw_udisks_client_connect_cb (GObject *source_object,
g_signal_connect (object_manager, "object-removed",
G_CALLBACK (gmw_udisks_object_removed_cb), priv);
objects = g_dbus_object_manager_get_objects (object_manager);
- for (l = objects; l != NULL; l = l->next)
+ for (GList *l = objects; l != NULL; l = l->next)
gmw_udisks_object_add (priv, G_DBUS_OBJECT (l->data));
gmw_device_list_sort (priv);
gmw_update_max_threads (priv);
@@ -1665,17 +1637,44 @@ gmw_thread_pool_sort_func (gconstpointer a, gconstpointer b, gpointer user_data)
gmw_device_get_order_process (devb));
}
+static void
+gmw_priv_free (GmwPrivate *priv)
+{
+ if (priv->builder != NULL)
+ g_object_unref (priv->builder);
+ if (priv->settings != NULL)
+ g_object_unref (priv->settings);
+ if (priv->udisks_client != NULL)
+ g_object_unref (priv->udisks_client);
+ if (priv->usb_ctx != NULL)
+ g_object_unref (priv->usb_ctx);
+ if (priv->image_file != NULL)
+ g_object_unref (priv->image_file);
+ if (priv->image_monitor != NULL)
+ g_object_unref (priv->image_monitor);
+ if (priv->cancellable != NULL)
+ g_object_unref (priv->cancellable);
+ if (priv->application != NULL)
+ g_object_unref (priv->application);
+ g_thread_pool_free (priv->thread_pool, TRUE, TRUE);
+ g_mutex_clear (&priv->thread_pool_mutex);
+ g_mutex_clear (&priv->devices_mutex);
+ g_mutex_clear (&priv->idle_id_mutex);
+ g_ptr_array_unref (priv->devices);
+ g_free (priv);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GmwPrivate, gmw_priv_free)
+
int
main (int argc, char **argv)
{
- GmwPrivate *priv = NULL;
- GOptionContext *context;
gboolean verbose = FALSE;
- gboolean rename_labels = FALSE;
- int status = EXIT_SUCCESS;
g_autoptr(GError) error = NULL;
+ g_autoptr(GmwPrivate) priv = g_new0 (GmwPrivate, 1);
+ g_autoptr(GOptionContext) context = NULL;
const GOptionEntry options[] = {
- { "rename-labels", '\0', 0, G_OPTION_ARG_NONE, &rename_labels,
+ { "rename-labels", '\0', 0, G_OPTION_ARG_NONE, &priv->rename_labels,
/* TRANSLATORS: command line option */
_("Allow renaming the labels on hubs"), NULL },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
@@ -1697,15 +1696,12 @@ main (int argc, char **argv)
g_option_context_add_group (context, gtk_get_option_group (TRUE));
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) {
- status = EXIT_FAILURE;
/* TRANSLATORS: the user has sausages for fingers */
g_print ("%s: %s\n", _("Failed to parse command line options"),
error->message);
- goto out;
+ return EXIT_FAILURE;
}
- priv = g_new0 (GmwPrivate, 1);
- priv->rename_labels = rename_labels;
g_mutex_init (&priv->thread_pool_mutex);
g_mutex_init (&priv->devices_mutex);
g_mutex_init (&priv->idle_id_mutex);
@@ -1716,9 +1712,8 @@ main (int argc, char **argv)
priv->thread_pool = g_thread_pool_new (gmw_copy_thread_cb, priv,
1, FALSE, &error);
if (priv->thread_pool == NULL) {
- status = EXIT_FAILURE;
g_print ("Failed to create thread pool: %s\n", error->message);
- goto out;
+ return EXIT_FAILURE;
}
g_thread_pool_set_sort_function (priv->thread_pool,
gmw_thread_pool_sort_func,
@@ -1726,9 +1721,8 @@ main (int argc, char **argv)
priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
priv->usb_ctx = g_usb_context_new (&error);
if (priv->usb_ctx == NULL) {
- status = EXIT_FAILURE;
g_print ("Failed to load libusb: %s\n", error->message);
- goto out;
+ return EXIT_FAILURE;
}
/* connect to UDisks */
@@ -1759,32 +1753,5 @@ main (int argc, char **argv)
G_SETTINGS_BIND_DEFAULT);
/* wait */
- status = g_application_run (G_APPLICATION (priv->application), argc, argv);
-out:
- g_option_context_free (context);
- if (priv != NULL) {
- if (priv->builder != NULL)
- g_object_unref (priv->builder);
- if (priv->settings != NULL)
- g_object_unref (priv->settings);
- if (priv->udisks_client != NULL)
- g_object_unref (priv->udisks_client);
- if (priv->usb_ctx != NULL)
- g_object_unref (priv->usb_ctx);
- if (priv->image_file != NULL)
- g_object_unref (priv->image_file);
- if (priv->image_monitor != NULL)
- g_object_unref (priv->image_monitor);
- if (priv->cancellable != NULL)
- g_object_unref (priv->cancellable);
- if (priv->application != NULL)
- g_object_unref (priv->application);
- g_thread_pool_free (priv->thread_pool, TRUE, TRUE);
- g_mutex_clear (&priv->thread_pool_mutex);
- g_mutex_clear (&priv->devices_mutex);
- g_mutex_clear (&priv->idle_id_mutex);
- g_ptr_array_unref (priv->devices);
- g_free (priv);
- }
- return status;
+ return g_application_run (G_APPLICATION (priv->application), argc, argv);
}
diff --git a/src/gmw-probe.c b/src/gmw-probe.c
index 93cddab..114f293 100644
--- a/src/gmw-probe.c
+++ b/src/gmw-probe.c
@@ -67,11 +67,8 @@ typedef struct {
static guint8 *
gmw_probe_get_random_data (guint len)
{
- guint i;
- guint8 *data;
-
- data = g_new (guint8, len);
- for (i = 0; i < len; i++)
+ guint8 *data = g_new (guint8, len);
+ for (guint i = 0; i < len; i++)
data[i] = g_random_int_range ('a', 'z');
return data;
}
@@ -176,16 +173,12 @@ gmw_probe_device_data_save (GmwProbeDevice *dev,
GCancellable *cancellable,
GError **error)
{
- GmwProbeBlock *item;
- guint64 chunk_size;
- guint i;
-
/* aim for roughtly the same number of chunks for all device sizes */
- chunk_size = dev->disk_size / 256;
+ guint64 chunk_size = dev->disk_size / 256;
g_debug ("using chunk size of %" G_GUINT64_FORMAT "MB",
chunk_size / ONE_MB);
- for (i = 1; i < 40; i++) {
- item = g_new0 (GmwProbeBlock, 1);
+ for (guint i = 1; i < 40; i++) {
+ GmwProbeBlock *item = g_new0 (GmwProbeBlock, 1);
item->valid = TRUE;
item->offset = g_random_int_range (1, 0xff);
item->address = i * chunk_size;
@@ -214,11 +207,8 @@ gmw_probe_device_data_set_dummy (GmwProbeDevice *dev,
GCancellable *cancellable,
GError **error)
{
- GmwProbeBlock *item;
- guint i;
-
- for (i = 0; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ for (guint i = 0; i < dev->data_save->len; i++) {
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
item->bytes_wrote = gmw_probe_device_write (dev,
item->address +
item->offset,
@@ -239,18 +229,14 @@ gmw_probe_device_data_verify (GmwProbeDevice *dev,
GCancellable *cancellable,
GError **error)
{
- GmwProbeBlock *item;
guint i;
- guint32 offset;
- g_autofree guint8 *wbuf2 = NULL;
-
- wbuf2 = g_new (guint8, ONE_BLOCK + 0xff);
+ g_autofree guint8 *wbuf2 = g_new (guint8, ONE_BLOCK + 0xff);
for (i = 0; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
/* use a random offset to confuse drives that are just saving
* the address and data in some phantom FAT */
- offset = g_random_int_range (1, 0xff);
+ guint32 offset = g_random_int_range (1, 0xff);
item->bytes_read = gmw_probe_device_read (dev,
item->address +
item->offset - offset,
@@ -276,7 +262,7 @@ gmw_probe_device_data_verify (GmwProbeDevice *dev,
/* if we aborted early, the rest of the drive is junk */
for (i = i; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
item->valid = FALSE;
}
@@ -288,11 +274,8 @@ gmw_probe_device_data_restore (GmwProbeDevice *dev,
GCancellable *cancellable,
GError **error)
{
- GmwProbeBlock *item;
- guint i;
-
- for (i = 0; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ for (guint i = 0; i < dev->data_save->len; i++) {
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
if (!item->valid)
continue;
item->bytes_wrote = gmw_probe_device_write (dev,
@@ -312,9 +295,6 @@ gmw_probe_device_data_restore (GmwProbeDevice *dev,
static gboolean
gmw_probe_scan_device (GmwProbeDevice *dev, GCancellable *cancellable, GError **error)
{
- GmwProbeBlock *item;
- guint i;
-
/* open block device */
if (!gmw_probe_device_open (dev, error))
return FALSE;
@@ -357,8 +337,8 @@ gmw_probe_scan_device (GmwProbeDevice *dev, GCancellable *cancellable, GError **
}
/* sanity check for really broken devices */
- for (i = 0; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ for (guint i = 0; i < dev->data_save->len; i++) {
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
if (item->bytes_read != item->bytes_wrote) {
g_set_error (error,
GMW_ERROR,
@@ -392,8 +372,8 @@ gmw_probe_scan_device (GmwProbeDevice *dev, GCancellable *cancellable, GError **
return FALSE;
/* get results */
- for (i = 0; i < dev->data_save->len; i++) {
- item = g_ptr_array_index (dev->data_save, i);
+ for (guint i = 0; i < dev->data_save->len; i++) {
+ GmwProbeBlock *item = g_ptr_array_index (dev->data_save, i);
if (!item->valid) {
g_set_error (error,
GMW_ERROR,
@@ -456,14 +436,12 @@ gmw_probe_use_device (GUdevClient *udev_client,
static gboolean
gmw_probe_is_block_device_valid (const gchar *block_device)
{
- guint i;
-
/* dev prefix */
if (!g_str_has_prefix (block_device, "/dev/"))
return FALSE;
/* has no partition number */
- for (i = 5; block_device[i] != '\0'; i++) {
+ for (guint i = 5; block_device[i] != '\0'; i++) {
if (g_ascii_isdigit (block_device[i]))
return FALSE;
}
@@ -483,13 +461,12 @@ gmw_probe_is_block_device_mounted (const gchar *block_device)
int
main (int argc, char **argv)
{
- GOptionContext *context;
const gchar *subsystems[] = { "usb", NULL };
gboolean verbose = FALSE;
- int status = EXIT_SUCCESS;
_cleanup_object_unref_ GUdevClient *udev_client = NULL;
- g_autoptr(GError) error = NULL;
g_autoptr(GCancellable) cancellable = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GOptionContext) context = NULL;
const GOptionEntry options[] = {
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
@@ -505,9 +482,8 @@ main (int argc, char **argv)
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) {
- status = EXIT_FAILURE;
g_print ("Failed to parse command line: %s\n", error->message);
- goto out;
+ return EXIT_FAILURE;
}
if (verbose)
@@ -515,32 +491,27 @@ main (int argc, char **argv)
/* valid arguments */
if (argc != 2 || !gmw_probe_is_block_device_valid (argv[1])) {
- status = EXIT_FAILURE;
g_print ("Block device required as argument\n");
- goto out;
+ return EXIT_FAILURE;
}
/* already mounted */
if (gmw_probe_is_block_device_mounted (argv[1])) {
- status = EXIT_FAILURE;
g_print ("Partition mounted from block device\n");
- goto out;
+ return EXIT_FAILURE;
}
/* probe device */
cancellable = g_cancellable_new ();
udev_client = g_udev_client_new (subsystems);
if (!gmw_probe_use_device (udev_client, argv[1], cancellable, &error)) {
- status = EXIT_FAILURE;
if (g_error_matches (error, GMW_ERROR, GMW_ERROR_IS_FAKE)) {
g_print ("Device is FAKE: %s\n", error->message);
} else {
g_print ("Failed to scan device: %s\n", error->message);
}
- goto out;
+ return EXIT_FAILURE;
}
g_print ("Device is GOOD\n");
-out:
- g_option_context_free (context);
- return status;
+ return EXIT_SUCCESS;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]