[glib: 3/4] gfileenumerator: Warn if name is not available for get_child()




commit b6424b5ce101309a89b6c8686305c60f10a82966
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Oct 21 11:13:29 2021 +0100

    gfileenumerator: Warn if name is not available for get_child()
    
    `standard::name` must be available for `g_file_enumerator_get_child()`
    to work. Emit a critical warning and return if it’s not. This is similar
    to the existing behaviour in `g_file_enumerator_iterate()`.
    
    Improve the documentation to mention this.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Fixes: #2507

 gio/gfile.c           |  4 +++-
 gio/gfileenumerator.c | 16 ++++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)
---
diff --git a/gio/gfile.c b/gio/gfile.c
index d7feca6b9..17aee12a7 100644
--- a/gio/gfile.c
+++ b/gio/gfile.c
@@ -1025,7 +1025,9 @@ g_file_resolve_relative_path (GFile      *file,
  * "standard::*" means all attributes in the standard namespace.
  * An example attribute query be "standard::*,owner::user".
  * The standard attributes are available as defines, like
- * #G_FILE_ATTRIBUTE_STANDARD_NAME.
+ * #G_FILE_ATTRIBUTE_STANDARD_NAME. #G_FILE_ATTRIBUTE_STANDARD_NAME should
+ * always be specified if you plan to call g_file_enumerator_get_child() or
+ * g_file_enumerator_iterate() on the returned enumerator.
  *
  * If @cancellable is not %NULL, then the operation can be cancelled
  * by triggering the cancellable object from another thread. If the
diff --git a/gio/gfileenumerator.c b/gio/gfileenumerator.c
index 6d651745c..429d972ab 100644
--- a/gio/gfileenumerator.c
+++ b/gio/gfileenumerator.c
@@ -721,6 +721,9 @@ g_file_enumerator_get_container (GFileEnumerator *enumerator)
  * directory of @enumerator.  This function is primarily intended to be used
  * inside loops with g_file_enumerator_next_file().
  *
+ * To use this, #G_FILE_ATTRIBUTE_STANDARD_NAME must have been listed in the
+ * attributes list used when creating the #GFileEnumerator.
+ *
  * This is a convenience method that's equivalent to:
  * |[<!-- language="C" -->
  *   gchar *name = g_file_info_get_name (info);
@@ -736,11 +739,20 @@ GFile *
 g_file_enumerator_get_child (GFileEnumerator *enumerator,
                              GFileInfo       *info)
 {
+  const gchar *name;
+
   g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
 
-  return g_file_get_child (enumerator->priv->container,
-                           g_file_info_get_name (info));
+  name = g_file_info_get_name (info);
+
+  if (G_UNLIKELY (name == NULL))
+    {
+      g_critical ("GFileEnumerator created without standard::name");
+      g_return_val_if_reached (NULL);
+    }
+
+  return g_file_get_child (enumerator->priv->container, name);
 }
 
 static void


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