[gnome-software: 6/10] gs-utils: Use GFile rather than filename arguments




commit eff62801fc2e2ef4dc232784ceb0f6f8558a81c3
Author: Philip Withnall <pwithnall endlessos org>
Date:   Sun Feb 20 13:13:18 2022 +0000

    gs-utils: Use GFile rather than filename arguments
    
    The callers can already provide a `GFile`, and these two functions just
    create one internally, so avoid the potential for creating a redundant
    `GFile` object and use one from the caller directly.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-plugin.c |  7 +++++--
 lib/gs-utils.c  | 34 ++++++++++++++--------------------
 lib/gs-utils.h  |  4 ++--
 3 files changed, 21 insertions(+), 24 deletions(-)
---
diff --git a/lib/gs-plugin.c b/lib/gs-plugin.c
index 40dffbf87..48a2ab26f 100644
--- a/lib/gs-plugin.c
+++ b/lib/gs-plugin.c
@@ -1163,12 +1163,15 @@ gs_plugin_download_file (GsPlugin *plugin,
        gsize downloaded_data_length = 0;
        g_autoptr(GError) error_local = NULL;
        g_autoptr(SoupMessage) msg = NULL;
+       g_autoptr(GFile) file = NULL;
 
        g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE);
        g_return_val_if_fail (uri != NULL, FALSE);
        g_return_val_if_fail (filename != NULL, FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
+       file = g_file_new_for_path (filename);
+
        /* local */
        if (g_str_has_prefix (uri, "file://")) {
                gsize length = 0;
@@ -1204,7 +1207,7 @@ gs_plugin_download_file (GsPlugin *plugin,
                return FALSE;
        }
        if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
-               g_autofree gchar *last_etag = gs_utils_get_file_etag (filename, cancellable);
+               g_autofree gchar *last_etag = gs_utils_get_file_etag (file, cancellable);
                if (last_etag != NULL && *last_etag != '\0') {
 #if SOUP_CHECK_VERSION(3, 0, 0)
                        soup_message_headers_append (soup_message_get_request_headers (msg), "If-None-Match", 
last_etag);
@@ -1274,7 +1277,7 @@ gs_plugin_download_file (GsPlugin *plugin,
 #endif
        if (new_etag != NULL && *new_etag == '\0')
                new_etag = NULL;
-       gs_utils_set_file_etag (filename, new_etag, cancellable);
+       gs_utils_set_file_etag (file, new_etag, cancellable);
        return TRUE;
 }
 
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index a976c06f7..b3e9e5f5e 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -1524,29 +1524,27 @@ gs_utils_get_file_size (const gchar *filename,
 
 /**
  * gs_utils_get_file_etag:
- * @filename: a file name to get the ETag for
+ * @file: a file to get the ETag for
  * @cancellable: (nullable): an optional #GCancellable or %NULL
  *
- * Gets the ETag for the @filename, previously stored by
+ * Gets the ETag for the @file, previously stored by
  * gs_utils_set_file_etag().
  *
- * Returns: (nullable) (transfer full): The ETag stored for the @filename,
+ * Returns: (nullable) (transfer full): The ETag stored for the @file,
  *    or %NULL, when the file does not exist, no ETag is stored for it
  *    or other error occurs.
  *
  * Since: 42
  **/
 gchar *
-gs_utils_get_file_etag (const gchar *filename,
-                       GCancellable *cancellable)
+gs_utils_get_file_etag (GFile        *file,
+                        GCancellable *cancellable)
 {
-       g_autoptr(GFile) file = NULL;
        g_autoptr(GFileInfo) info = NULL;
 
-       g_return_val_if_fail (filename != NULL, NULL);
+       g_return_val_if_fail (G_IS_FILE (file), NULL);
        g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 
-       file = g_file_new_for_path (filename);
        info = g_file_query_info (file, METADATA_ETAG_ATTRIBUTE, G_FILE_QUERY_INFO_NONE, cancellable, NULL);
 
        if (info == NULL)
@@ -1557,32 +1555,28 @@ gs_utils_get_file_etag (const gchar *filename,
 
 /**
  * gs_utils_set_file_etag:
- * @filename: a file name to get the ETag for
+ * @file: a file to get the ETag for
  * @etag: (nullable): an ETag to set
  * @cancellable: (nullable): an optional #GCancellable or %NULL
  *
- * Sets the ETag for the @filename. When the @etag is %NULL or an empty
- * string, then unsets the ETag for the @filename. The ETag can be read
+ * Sets the ETag for the @file. When the @etag is %NULL or an empty
+ * string, then unsets the ETag for the @file. The ETag can be read
  * back with gs_utils_get_file_etag().
  *
- * The @filename should exist, otherwise the function fails.
+ * The @file should exist, otherwise the function fails.
  *
  * Returns: whether succeeded.
  *
  * Since: 42
  **/
 gboolean
-gs_utils_set_file_etag (const gchar *filename,
-                       const gchar *etag,
-                       GCancellable *cancellable)
+gs_utils_set_file_etag (GFile        *file,
+                        const gchar  *etag,
+                        GCancellable *cancellable)
 {
-       g_autoptr(GFile) file = NULL;
-
-       g_return_val_if_fail (filename != NULL, FALSE);
+       g_return_val_if_fail (G_IS_FILE (file), FALSE);
        g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
 
-       file = g_file_new_for_path (filename);
-
        if (etag == NULL || *etag == '\0') {
                return g_file_set_attribute (file, METADATA_ETAG_ATTRIBUTE, G_FILE_ATTRIBUTE_TYPE_INVALID,
                                             NULL, G_FILE_QUERY_INFO_NONE, cancellable, NULL);
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index ef33ed9ac..a1f1514f7 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -134,9 +134,9 @@ guint64              gs_utils_get_file_size         (const gchar            *filename,
                                                 GsFileSizeIncludeFunc   include_func,
                                                 gpointer                user_data,
                                                 GCancellable           *cancellable);
-gchar *                 gs_utils_get_file_etag         (const gchar            *filename,
+gchar *                 gs_utils_get_file_etag         (GFile                  *file,
                                                 GCancellable           *cancellable);
-gboolean        gs_utils_set_file_etag         (const gchar            *filename,
+gboolean        gs_utils_set_file_etag         (GFile                  *file,
                                                 const gchar            *etag,
                                                 GCancellable           *cancellable);
 


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