[epiphany/mcatanzaro/#336: 1/2] Update GVDB



commit c5ccd9f7ef801e2deff08bf5d8ec1996656f5e2e
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sat Jun 22 13:04:59 2019 -0500

    Update GVDB
    
    In particular, this adds gvdb_table_write_contents_async() from gvdb!4.
    
    It also requires changing some integer types due to an API break in
    gvdb_table_get_names().

 lib/contrib/gvdb/README.epiphany             |   6 +-
 lib/contrib/gvdb/gvdb-builder.c              | 111 +++++++++++++++++++++++++--
 lib/contrib/gvdb/gvdb-builder.h              |  20 ++++-
 lib/contrib/gvdb/gvdb-format.h               |   6 +-
 lib/contrib/gvdb/gvdb-reader.c               |  62 +++++++++------
 lib/contrib/gvdb/gvdb-reader.h               |  17 ++--
 src/bookmarks/ephy-bookmarks-import.c        |   8 +-
 src/profile-migrator/ephy-profile-migrator.c |   6 +-
 8 files changed, 185 insertions(+), 51 deletions(-)
---
diff --git a/lib/contrib/gvdb/README.epiphany b/lib/contrib/gvdb/README.epiphany
index 4347db97c..bda8c72ac 100644
--- a/lib/contrib/gvdb/README.epiphany
+++ b/lib/contrib/gvdb/README.epiphany
@@ -1,5 +1,5 @@
 GVariant Database
 
- * Copied from https://git.gnome.org/browse/gvdb
- * Removed G_GNUC_INTERNAL
- * Fixed -Wsign-compare
+ * Copied from https://git.gnome.org/browse/gvdb commit a6f0eadb51aae5351a0cfd6b9da3dcba6b134e1a
+ * Removed G_GNUC_INTERNAL and GVDB_GNUC_WEAK
+ * Fix -Wsign-compare
diff --git a/lib/contrib/gvdb/gvdb-builder.c b/lib/contrib/gvdb/gvdb-builder.c
index 16a5283dd..70c0ac1cf 100644
--- a/lib/contrib/gvdb/gvdb-builder.c
+++ b/lib/contrib/gvdb/gvdb-builder.c
@@ -4,7 +4,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt desrt ca>
  */
@@ -293,7 +291,8 @@ file_builder_add_string (FileBuilder *fb,
   chunk->offset = fb->offset;
   chunk->size = length;
   chunk->data = g_malloc (length);
-  memcpy (chunk->data, string, length);
+  if (length != 0)
+    memcpy (chunk->data, string, length);
 
   *start = guint32_to_le (fb->offset);
   *size = guint16_to_le (length);
@@ -512,6 +511,10 @@ gvdb_table_write_contents (GHashTable   *table,
   FileBuilder *fb;
   GString *str;
 
+  g_return_val_if_fail (table != NULL, FALSE);
+  g_return_val_if_fail (filename != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
   fb = file_builder_new (byteswap);
   file_builder_add_hash (fb, table, &root);
   str = file_builder_serialise (fb, root);
@@ -521,3 +524,101 @@ gvdb_table_write_contents (GHashTable   *table,
 
   return status;
 }
+
+typedef struct {
+  GBytes *contents;  /* (owned) */
+  GFile  *file;      /* (owned) */
+} WriteContentsData;
+
+static WriteContentsData *
+write_contents_data_new (GBytes *contents,
+                         GFile  *file)
+{
+  WriteContentsData *data;
+
+  data = g_slice_new (WriteContentsData);
+  data->contents = g_bytes_ref (contents);
+  data->file = g_object_ref (file);
+
+  return data;
+}
+
+static void
+write_contents_data_free (WriteContentsData *data)
+{
+  g_bytes_unref (data->contents);
+  g_object_unref (data->file);
+  g_slice_free (WriteContentsData, data);
+}
+
+static void
+replace_contents_cb (GObject      *source_object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+  GTask *task = user_data;
+  WriteContentsData *data = g_task_get_task_data (task);
+  GError *error = NULL;
+
+  g_return_if_fail (g_task_get_source_tag (task) == gvdb_table_write_contents_async);
+
+  if (!g_file_replace_contents_finish (data->file, result, NULL, &error))
+    g_task_return_error (task, g_steal_pointer (&error));
+  else
+    g_task_return_boolean (task, TRUE);
+
+  g_object_unref (task);
+}
+
+void
+gvdb_table_write_contents_async (GHashTable          *table,
+                                 const gchar         *filename,
+                                 gboolean             byteswap,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
+{
+  struct gvdb_pointer root;
+  FileBuilder *fb;
+  WriteContentsData *data;
+  GString *str;
+  GBytes *bytes;
+  GFile *file;
+  GTask *task;
+
+  g_return_if_fail (table != NULL);
+  g_return_if_fail (filename != NULL);
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+
+  fb = file_builder_new (byteswap);
+  file_builder_add_hash (fb, table, &root);
+  str = file_builder_serialise (fb, root);
+  bytes = g_string_free_to_bytes (str);
+
+  file = g_file_new_for_path (filename);
+  data = write_contents_data_new (bytes, file);
+
+  task = g_task_new (NULL, cancellable, callback, user_data);
+  g_task_set_task_data (task, data, (GDestroyNotify)write_contents_data_free);
+  g_task_set_source_tag (task, gvdb_table_write_contents_async);
+
+  g_file_replace_contents_async (file, str->str, str->len,
+                                 NULL, FALSE,
+                                 G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION,
+                                 cancellable, replace_contents_cb, g_steal_pointer (&task));
+
+  g_bytes_unref (bytes);
+  g_object_unref (file);
+}
+
+gboolean
+gvdb_table_write_contents_finish (GHashTable    *table,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+  g_return_val_if_fail (table != NULL, FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, NULL), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
diff --git a/lib/contrib/gvdb/gvdb-builder.h b/lib/contrib/gvdb/gvdb-builder.h
index 8e32e144b..41ba957dd 100644
--- a/lib/contrib/gvdb/gvdb-builder.h
+++ b/lib/contrib/gvdb/gvdb-builder.h
@@ -4,7 +4,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt desrt ca>
  */
@@ -31,14 +29,17 @@ GHashTable *            gvdb_hash_table_new                             (GHashTa
 
 GvdbItem *              gvdb_hash_table_insert                          (GHashTable    *table,
                                                                          const gchar   *key);
+
 void                    gvdb_hash_table_insert_string                   (GHashTable    *table,
                                                                          const gchar   *key,
                                                                          const gchar   *value);
 
 void                    gvdb_item_set_value                             (GvdbItem      *item,
                                                                          GVariant      *value);
+
 void                    gvdb_item_set_hash_table                        (GvdbItem      *item,
                                                                          GHashTable    *table);
+
 void                    gvdb_item_set_parent                            (GvdbItem      *item,
                                                                          GvdbItem      *parent);
 
@@ -47,4 +48,15 @@ gboolean                gvdb_table_write_contents                       (GHashTa
                                                                          gboolean        byteswap,
                                                                          GError        **error);
 
+void                    gvdb_table_write_contents_async                 (GHashTable          *table,
+                                                                         const gchar         *filename,
+                                                                         gboolean             byteswap,
+                                                                         GCancellable        *cancellable,
+                                                                         GAsyncReadyCallback  callback,
+                                                                         gpointer             user_data);
+
+gboolean                gvdb_table_write_contents_finish                (GHashTable          *table,
+                                                                         GAsyncResult        *result,
+                                                                         GError             **error);
+
 #endif /* __gvdb_builder_h__ */
diff --git a/lib/contrib/gvdb/gvdb-format.h b/lib/contrib/gvdb/gvdb-format.h
index 886aa5697..ed6adabfa 100644
--- a/lib/contrib/gvdb/gvdb-format.h
+++ b/lib/contrib/gvdb/gvdb-format.h
@@ -4,7 +4,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt desrt ca>
  */
diff --git a/lib/contrib/gvdb/gvdb-reader.c b/lib/contrib/gvdb/gvdb-reader.c
index 47e23a882..2dbb5b414 100644
--- a/lib/contrib/gvdb/gvdb-reader.c
+++ b/lib/contrib/gvdb/gvdb-reader.c
@@ -4,7 +4,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt desrt ca>
  */
@@ -127,14 +125,16 @@ gvdb_table_setup_root (GvdbTable                 *file,
  * @bytes: the #GBytes with the data
  * @trusted: if the contents of @bytes are trusted
  * @error: %NULL, or a pointer to a %NULL #GError
- * @returns: a new #GvdbTable
  *
  * Creates a new #GvdbTable from the contents of @bytes.
  *
- * This call can fail if the header contained in @bytes is invalid.
+ * This call can fail if the header contained in @bytes is invalid or if @bytes
+ * is empty; if so, %G_FILE_ERROR_INVAL will be returned.
  *
  * You should call gvdb_table_free() on the return result when you no
  * longer require it.
+ *
+ * Returns: a new #GvdbTable
  **/
 GvdbTable *
 gvdb_table_new_from_bytes (GBytes    *bytes,
@@ -186,10 +186,17 @@ invalid:
  * @filename: a filename
  * @trusted: if the contents of @bytes are trusted
  * @error: %NULL, or a pointer to a %NULL #GError
- * @returns: a new #GvdbTable
  *
  * Creates a new #GvdbTable using the #GMappedFile for @filename as the
  * #GBytes.
+ *
+ * This function will fail if the file cannot be opened.
+ * In that case, the #GError that is returned will be an error from
+ * g_mapped_file_new().
+ *
+ * An empty or corrupt file will result in %G_FILE_ERROR_INVAL.
+ *
+ * Returns: a new #GvdbTable
  **/
 GvdbTable *
 gvdb_table_new (const gchar  *filename,
@@ -325,7 +332,7 @@ gvdb_table_list_from_item (GvdbTable                    *table,
 /**
  * gvdb_table_get_names:
  * @table: a #GvdbTable
- * @length: the number of items returned, or %NULL
+ * @length: (optional): the number of items returned, or %NULL
  *
  * Gets a list of all names contained in @table.
  *
@@ -337,17 +344,17 @@ gvdb_table_list_from_item (GvdbTable                    *table,
  * above calls in the case of the corrupted file.  Note also that the
  * returned strings may not be utf8.
  *
- * Returns: a %NULL-terminated list of strings, of length @length
+ * Returns: (array length=length): a %NULL-terminated list of strings, of length @length
  **/
 gchar **
 gvdb_table_get_names (GvdbTable *table,
-                      gint      *length)
+                      gsize     *length)
 {
   gchar **names;
-  gint n_names;
-  gint filled;
-  gint total;
-  gint i;
+  guint n_names;
+  guint filled;
+  guint total;
+  guint i;
 
   /* We generally proceed by iterating over the list of items in the
    * hash table (in order of appearance) recording them into an array.
@@ -418,7 +425,7 @@ gvdb_table_get_names (GvdbTable *table,
                 }
             }
 
-          else if (parent < (guint32)n_names && names[parent] != NULL)
+          else if (parent < n_names && names[parent] != NULL)
             {
               /* It's a non-root item whose parent was filled in already.
                *
@@ -455,7 +462,7 @@ gvdb_table_get_names (GvdbTable *table,
     {
       GPtrArray *fixed_names;
 
-      fixed_names = g_ptr_array_new ();
+      fixed_names = g_ptr_array_sized_new (n_names + 1  /* NULL terminator */);
       for (i = 0; i < n_names; i++)
         if (names[i] != NULL)
           g_ptr_array_add (fixed_names, names[i]);
@@ -467,7 +474,10 @@ gvdb_table_get_names (GvdbTable *table,
     }
 
   if (length)
-    *length = n_names;
+    {
+      G_STATIC_ASSERT (sizeof (*length) >= sizeof (n_names));
+      *length = n_names;
+    }
 
   return names;
 }
@@ -476,7 +486,6 @@ gvdb_table_get_names (GvdbTable *table,
  * gvdb_table_list:
  * @file: a #GvdbTable
  * @key: a string
- * @returns: a %NULL-terminated string array
  *
  * List all of the keys that appear below @key.  The nesting of keys
  * within the hash file is defined by the program that created the hash
@@ -489,6 +498,8 @@ gvdb_table_get_names (GvdbTable *table,
  *
  * You should call g_strfreev() on the return result when you no longer
  * require it.
+ *
+ * Returns: a %NULL-terminated string array
  **/
 gchar **
 gvdb_table_list (GvdbTable   *file,
@@ -539,12 +550,13 @@ gvdb_table_list (GvdbTable   *file,
  * gvdb_table_has_value:
  * @file: a #GvdbTable
  * @key: a string
- * @returns: %TRUE if @key is in the table
  *
  * Checks for a value named @key in @file.
  *
  * Note: this function does not consider non-value nodes (other hash
  * tables, for example).
+ *
+ * Returns: %TRUE if @key is in the table
  **/
 gboolean
 gvdb_table_has_value (GvdbTable    *file,
@@ -588,7 +600,6 @@ gvdb_table_value_from_item (GvdbTable                   *table,
  * gvdb_table_get_value:
  * @file: a #GvdbTable
  * @key: a string
- * @returns: a #GVariant, or %NULL
  *
  * Looks up a value named @key in @file.
  *
@@ -598,6 +609,8 @@ gvdb_table_value_from_item (GvdbTable                   *table,
  *
  * You should call g_variant_unref() on the return result when you no
  * longer require it.
+ *
+ * Returns: a #GVariant, or %NULL
  **/
 GVariant *
 gvdb_table_get_value (GvdbTable    *file,
@@ -627,12 +640,13 @@ gvdb_table_get_value (GvdbTable    *file,
  * gvdb_table_get_raw_value:
  * @table: a #GvdbTable
  * @key: a string
- * @returns: a #GVariant, or %NULL
  *
  * Looks up a value named @key in @file.
  *
  * This call is equivalent to gvdb_table_get_value() except that it
  * never byteswaps the value.
+ *
+ * Returns: a #GVariant, or %NULL
  **/
 GVariant *
 gvdb_table_get_raw_value (GvdbTable   *table,
@@ -650,7 +664,6 @@ gvdb_table_get_raw_value (GvdbTable   *table,
  * gvdb_table_get_table:
  * @file: a #GvdbTable
  * @key: a string
- * @returns: a new #GvdbTable, or %NULL
  *
  * Looks up the hash table named @key in @file.
  *
@@ -664,6 +677,8 @@ gvdb_table_get_raw_value (GvdbTable   *table,
  *
  * You should call gvdb_table_free() on the return result when you no
  * longer require it.
+ *
+ * Returns: a new #GvdbTable, or %NULL
  **/
 GvdbTable *
 gvdb_table_get_table (GvdbTable   *file,
@@ -705,13 +720,14 @@ gvdb_table_free (GvdbTable *file)
 /**
  * gvdb_table_is_valid:
  * @table: a #GvdbTable
- * @returns: %TRUE if @table is still valid
  *
  * Checks if the table is still valid.
  *
  * An on-disk GVDB can be marked as invalid.  This happens when the file
  * has been replaced.  The appropriate action is typically to reopen the
  * file.
+ *
+ * Returns: %TRUE if @table is still valid
  **/
 gboolean
 gvdb_table_is_valid (GvdbTable *table)
diff --git a/lib/contrib/gvdb/gvdb-reader.h b/lib/contrib/gvdb/gvdb-reader.h
index 689721c32..c3fc64613 100644
--- a/lib/contrib/gvdb/gvdb-reader.h
+++ b/lib/contrib/gvdb/gvdb-reader.h
@@ -4,7 +4,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Ryan Lortie <desrt desrt ca>
  */
@@ -31,23 +29,32 @@ G_BEGIN_DECLS
 GvdbTable *             gvdb_table_new_from_bytes                       (GBytes       *bytes,
                                                                          gboolean      trusted,
                                                                          GError      **error);
+
 GvdbTable *             gvdb_table_new                                  (const gchar  *filename,
                                                                          gboolean      trusted,
                                                                          GError      **error);
+
 void                    gvdb_table_free                                 (GvdbTable    *table);
+
 gchar **                gvdb_table_get_names                            (GvdbTable    *table,
-                                                                         gint         *length);
+                                                                         gsize        *length);
+
 gchar **                gvdb_table_list                                 (GvdbTable    *table,
                                                                          const gchar  *key);
+
 GvdbTable *             gvdb_table_get_table                            (GvdbTable    *table,
                                                                          const gchar  *key);
+
 GVariant *              gvdb_table_get_raw_value                        (GvdbTable    *table,
                                                                          const gchar  *key);
+
 GVariant *              gvdb_table_get_value                            (GvdbTable    *table,
                                                                          const gchar  *key);
 
+
 gboolean                gvdb_table_has_value                            (GvdbTable    *table,
                                                                          const gchar  *key);
+
 gboolean                gvdb_table_is_valid                             (GvdbTable    *table);
 
 G_END_DECLS
diff --git a/src/bookmarks/ephy-bookmarks-import.c b/src/bookmarks/ephy-bookmarks-import.c
index c942ab323..cdc8dded5 100644
--- a/src/bookmarks/ephy-bookmarks-import.c
+++ b/src/bookmarks/ephy-bookmarks-import.c
@@ -42,8 +42,8 @@ get_bookmarks_from_table (GvdbTable *table)
 {
   GSequence *bookmarks = NULL;
   char **list = NULL;
-  int length;
-  int i;
+  gsize length;
+  guint i;
 
   bookmarks = g_sequence_new (g_object_unref);
 
@@ -102,8 +102,8 @@ ephy_bookmarks_import (EphyBookmarksManager  *manager,
   GSequence *bookmarks = NULL;
   char **list = NULL;
   gboolean res = TRUE;
-  int length;
-  int i;
+  gsize length;
+  guint i;
 
   /* Create a new table to hold data stored in file. */
   root_table = gvdb_table_new (filename, TRUE, error);
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 7a3dda466..75496479a 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -842,7 +842,7 @@ migrate_bookmarks_timestamp (void)
   char **tags = NULL;
   char **urls = NULL;
   char *filename;
-  int length;
+  gsize length;
 
   filename = g_build_filename (legacy_profile_dir (), EPHY_BOOKMARKS_FILE, NULL);
   root_table_in = gvdb_table_new (filename, TRUE, &error);
@@ -869,11 +869,11 @@ migrate_bookmarks_timestamp (void)
   tags_table_out = gvdb_hash_table_new (root_table_out, "tags");
 
   tags = gvdb_table_get_names (tags_table_in, &length);
-  for (int i = 0; i < length; i++)
+  for (guint i = 0; i < length; i++)
     gvdb_hash_table_insert (tags_table_out, tags[i]);
 
   urls = gvdb_table_get_names (bookmarks_table_in, &length);
-  for (int i = 0; i < length; i++) {
+  for (guint i = 0; i < length; i++) {
     GVariant *value = gvdb_table_get_value (bookmarks_table_in, urls[i]);
     GVariant *new_value = convert_bookmark_timestamp (value);
     if (new_value != NULL) {


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