[gnome-photos/wip/rishi/fix-tracker-assertions: 2/2] base-item, utils: Avoid CRITICALs when reading an item's metadata




commit 530d0f3e305156233b7955817e5ecfebeb047fbe
Author: Sam Thursfield <sam afuera me uk>
Date:   Tue Sep 1 13:20:48 2020 +0200

    base-item, utils: Avoid CRITICALs when reading an item's metadata
    
    If the Tracker extractor has not yet run for an item then various
    variables in the query will be unbound. This would led to things like:
      Tracker-CRITICAL **: tracker_sparql_cursor_real_get_integer:
        assertion 'tracker_sparql_cursor_get_value_type (self, column) ==
        TRACKER_SPARQL_VALUE_TYPE_INTEGER' failed
    
    These CRITICALs were more often (always?) triggered when using
    TrackerSparqlConnection's 'bus' backend that uses D-Bus to access the
    Tracker database. eg., the Flatpak builds that don't bundle the Tracker
    D-Bus services.
    
    https://gitlab.gnome.org/GNOME/gnome-photos/-/merge_requests/149

 src/photos-base-item.c | 29 +++++++++++++++++++++++------
 src/photos-utils.c     | 38 ++++++++++++++++++++++++++++++++++++++
 src/photos-utils.h     |  8 ++++++++
 3 files changed, 69 insertions(+), 6 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index d3d49dd8..0d72f9c5 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -2863,8 +2863,12 @@ photos_base_item_populate_from_cursor (PhotosBaseItem *self, TrackerSparqlCursor
       priv->orientation = PHOTOS_ORIENTATION_TOP;
     }
 
-  height = tracker_sparql_cursor_get_integer (cursor, PHOTOS_QUERY_COLUMNS_HEIGHT);
-  width = tracker_sparql_cursor_get_integer (cursor, PHOTOS_QUERY_COLUMNS_WIDTH);
+  height = photos_utils_get_integer_from_sparql_cursor_with_default (cursor,
+                                                                     PHOTOS_QUERY_COLUMNS_HEIGHT,
+                                                                     0);
+  width = photos_utils_get_integer_from_sparql_cursor_with_default (cursor,
+                                                                    PHOTOS_QUERY_COLUMNS_WIDTH,
+                                                                    0);
   if (priv->orientation == PHOTOS_ORIENTATION_BOTTOM
       || priv->orientation == PHOTOS_ORIENTATION_BOTTOM_MIRROR
       || priv->orientation == PHOTOS_ORIENTATION_TOP
@@ -2879,10 +2883,23 @@ photos_base_item_populate_from_cursor (PhotosBaseItem *self, TrackerSparqlCursor
       priv->width = height;
     }
 
-  priv->exposure_time = tracker_sparql_cursor_get_double (cursor, PHOTOS_QUERY_COLUMNS_EXPOSURE_TIME);
-  priv->fnumber = tracker_sparql_cursor_get_double (cursor, PHOTOS_QUERY_COLUMNS_FNUMBER);
-  priv->focal_length = tracker_sparql_cursor_get_double (cursor, PHOTOS_QUERY_COLUMNS_FOCAL_LENGTH);
-  priv->iso_speed = tracker_sparql_cursor_get_double (cursor, PHOTOS_QUERY_COLUMNS_ISO_SPEED);
+  priv->exposure_time
+    = photos_utils_get_double_from_sparql_cursor_with_default (cursor,
+                                                               PHOTOS_QUERY_COLUMNS_EXPOSURE_TIME,
+                                                               0.0);
+
+  priv->fnumber = photos_utils_get_double_from_sparql_cursor_with_default (cursor,
+                                                                           PHOTOS_QUERY_COLUMNS_FNUMBER,
+                                                                           0.0);
+
+  priv->focal_length
+    = photos_utils_get_double_from_sparql_cursor_with_default (cursor,
+                                                               PHOTOS_QUERY_COLUMNS_FOCAL_LENGTH,
+                                                               0.0);
+
+  priv->iso_speed = photos_utils_get_double_from_sparql_cursor_with_default (cursor,
+                                                                             PHOTOS_QUERY_COLUMNS_ISO_SPEED,
+                                                                             0.0);
 
   flash = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_FLASH, NULL);
   priv->flash = g_quark_from_string (flash);
diff --git a/src/photos-utils.c b/src/photos-utils.c
index d2d7fb51..1d7be577 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -917,6 +917,25 @@ photos_utils_get_controller (PhotosWindowMode mode,
 }
 
 
+gdouble
+photos_utils_get_double_from_sparql_cursor_with_default (TrackerSparqlCursor *cursor,
+                                                         PhotosQueryColumns column,
+                                                         gdouble default_value)
+{
+  TrackerSparqlValueType value_type;
+  gdouble ret_val = default_value;
+
+  value_type = tracker_sparql_cursor_get_value_type (cursor, column);
+  if (value_type == TRACKER_SPARQL_VALUE_TYPE_UNBOUND)
+    goto out;
+
+  ret_val = tracker_sparql_cursor_get_double (cursor, column);
+
+ out:
+  return ret_val;
+}
+
+
 gchar *
 photos_utils_get_extension_from_mime_type (const gchar *mime_type)
 {
@@ -971,6 +990,25 @@ photos_utils_get_icon_size_unscaled (void)
 }
 
 
+gint64
+photos_utils_get_integer_from_sparql_cursor_with_default (TrackerSparqlCursor *cursor,
+                                                          PhotosQueryColumns column,
+                                                          gint64 default_value)
+{
+  TrackerSparqlValueType value_type;
+  gint64 ret_val = default_value;
+
+  value_type = tracker_sparql_cursor_get_value_type (cursor, column);
+  if (value_type == TRACKER_SPARQL_VALUE_TYPE_UNBOUND)
+    goto out;
+
+  ret_val = tracker_sparql_cursor_get_integer (cursor, column);
+
+ out:
+  return ret_val;
+}
+
+
 gint64
 photos_utils_get_mtime_from_sparql_cursor (TrackerSparqlCursor *cursor)
 {
diff --git a/src/photos-utils.h b/src/photos-utils.h
index 132d7558..a4d69abf 100644
--- a/src/photos-utils.h
+++ b/src/photos-utils.h
@@ -127,6 +127,10 @@ void             photos_utils_get_controller              (PhotosWindowMode mode
                                                            PhotosOffsetController **out_offset_cntrlr,
                                                            PhotosTrackerController **out_trk_cntrlr);
 
+gdouble          photos_utils_get_double_from_sparql_cursor_with_default (TrackerSparqlCursor *cursor,
+                                                                          PhotosQueryColumns column,
+                                                                          gdouble default_value);
+
 gchar           *photos_utils_get_extension_from_mime_type (const gchar *mime_type);
 
 
@@ -134,6 +138,10 @@ gint             photos_utils_get_icon_size               (void);
 
 gint             photos_utils_get_icon_size_unscaled      (void);
 
+gint64           photos_utils_get_integer_from_sparql_cursor_with_default (TrackerSparqlCursor *cursor,
+                                                                           PhotosQueryColumns column,
+                                                                           gint64 default_value);
+
 gint64           photos_utils_get_mtime_from_sparql_cursor (TrackerSparqlCursor *cursor);
 
 char*            photos_utils_get_pixbuf_common_suffix    (GdkPixbufFormat *format);


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