[gnome-disk-utility] gduutils: Implement gdu_utils_can_format



commit d47b1509a7551f81d77d2707a4259d8e1d5f5c77
Author: Manuel Wassermann <manuel wassermann97 gmail com>
Date:   Fri Mar 18 11:25:14 2022 +0100

    gduutils: Implement gdu_utils_can_format

 src/disks/gducreatefilesystempage.c |  4 +--
 src/libgdu/gduutils.c               | 71 +++++++++++++++++++++++--------------
 src/libgdu/gduutils.h               |  7 ++--
 3 files changed, 52 insertions(+), 30 deletions(-)
---
diff --git a/src/disks/gducreatefilesystempage.c b/src/disks/gducreatefilesystempage.c
index 326bd761..845d336a 100644
--- a/src/disks/gducreatefilesystempage.c
+++ b/src/disks/gducreatefilesystempage.c
@@ -195,7 +195,7 @@ gdu_create_filesystem_page_new (UDisksClient *client, UDisksDrive *drive)
       /* default FAT for flash and disks/media smaller than 20G (assumed to be flash cards) */
       if (gdu_utils_is_flash (drive) ||
           udisks_drive_get_size (drive) < 20UL * 1000UL*1000UL*1000UL ||
-          !gdu_utils_is_ntfs_available (client)
+          !gdu_utils_can_format (client, "ntfs", FALSE, NULL)
           )
         {
           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->all_radiobutton), TRUE);
@@ -206,7 +206,7 @@ gdu_create_filesystem_page_new (UDisksClient *client, UDisksDrive *drive)
         }
     }
 
-  gtk_widget_set_sensitive (GTK_WIDGET (priv->windows_radiobutton), gdu_utils_is_ntfs_available (client));
+  gtk_widget_set_sensitive (GTK_WIDGET (priv->windows_radiobutton), gdu_utils_can_format (client, "ntfs", 
FALSE, NULL));
 
   return page;
 }
diff --git a/src/libgdu/gduutils.c b/src/libgdu/gduutils.c
index 2e5b7be6..0d9fa9d7 100644
--- a/src/libgdu/gduutils.c
+++ b/src/libgdu/gduutils.c
@@ -882,32 +882,6 @@ gdu_utils_show_confirmation (GtkWindow    *parent_window,
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-gboolean
-gdu_utils_is_ntfs_available (UDisksClient *client)
-{
-  static gsize once = 0;
-  static gboolean available = FALSE;
-
-  if (g_once_init_enter (&once))
-    {
-      GVariant *out_available;
-      gchar *missing_util;
-
-      if (udisks_manager_call_can_format_sync (udisks_client_get_manager (client),
-                                               "ntfs", &out_available, NULL, NULL))
-        {
-          g_variant_get (out_available, "(bs)", &available, &missing_util);
-          g_variant_unref (out_available);
-          g_free (missing_util);
-        }
-
-      g_once_init_leave (&once, (gsize) 1);
-    }
-  return available;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
 
 typedef struct
 {
@@ -1020,6 +994,51 @@ gdu_utils_can_repair (UDisksClient *client,
   return result ? result->available : FALSE;
 }
 
+G_LOCK_DEFINE (can_format_lock);
+
+gboolean
+gdu_utils_can_format (UDisksClient *client,
+                      const gchar  *fstype,
+                      gboolean      flush,
+                      gchar       **missing_util_out)
+{
+  static GHashTable *cache = NULL;
+  const gchar *const *supported_fs;
+  UtilCacheEntry *result;
+
+  G_LOCK (can_format_lock);
+  if (flush)
+    g_clear_pointer (&cache, g_hash_table_destroy);
+
+  if (cache == NULL)
+  {
+    cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) util_cache_entry_free);
+    supported_fs = udisks_manager_get_supported_filesystems (udisks_client_get_manager (client));
+    for (gsize i = 0; supported_fs[i] != NULL; i++)
+    {
+      GVariant *out_available;
+
+      if (udisks_manager_call_can_format_sync (udisks_client_get_manager (client),
+                                               supported_fs[i], &out_available, NULL, NULL))
+      {
+        UtilCacheEntry *entry;
+
+        entry = g_new0 (UtilCacheEntry, 1);
+        g_variant_get (out_available, "(bs)", &entry->available, &entry->missing_util);
+        g_variant_unref (out_available);
+        g_hash_table_insert (cache, g_strdup (supported_fs[i]), entry);
+      }
+    }
+  }
+  G_UNLOCK (can_format_lock);
+
+  result = g_hash_table_lookup (cache, fstype);
+  if (missing_util_out != NULL)
+    *missing_util_out = result ? g_strdup (result->missing_util) : NULL;
+
+  return result ? result->available : FALSE;
+}
+
 gboolean
 gdu_utils_can_take_ownership (const gchar *fstype)
 {
diff --git a/src/libgdu/gduutils.h b/src/libgdu/gduutils.h
index de1f7c66..37775fea 100644
--- a/src/libgdu/gduutils.h
+++ b/src/libgdu/gduutils.h
@@ -76,8 +76,6 @@ gboolean        gdu_utils_show_confirmation (GtkWindow    *parent_window,
                                              GList        *objects,
                                              gboolean     destructive_action);
 
-gboolean gdu_utils_is_ntfs_available (UDisksClient *client);
-
 
 /* Defined by libblockdev/UDisks */
 typedef enum {
@@ -98,6 +96,11 @@ gboolean gdu_utils_can_repair (UDisksClient *client,
                                gboolean      flush,
                                gchar       **missing_util_out);
 
+gboolean gdu_utils_can_format (UDisksClient *client,
+                               const gchar  *fstype,
+                               gboolean      flush,
+                               gchar       **missing_util_out);
+
 gboolean gdu_utils_can_take_ownership (const gchar  *fstype);
 
 gboolean gdu_utils_can_check  (UDisksClient *client,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]