[gnome-software/wip/mcrha/odrs-download-etag: 10/11] gs-utils: Add functions to get/set file metadata ETag attribute




commit d563fef8a8e5f3e6e8806ed6de18a6c4e2ce19cb
Author: Milan Crha <mcrha redhat com>
Date:   Wed Oct 20 13:55:55 2021 +0200

    gs-utils: Add functions to get/set file metadata ETag attribute
    
    These can be used to get/set a metadata ETag attribute on existing files.

 lib/gs-utils.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/gs-utils.h |  5 ++++
 2 files changed, 77 insertions(+)
---
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 537a20f9f..ec4092ba0 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -1514,4 +1514,76 @@ gs_utils_get_file_size (const gchar *filename,
        return size;
 }
 
+#define METADATA_ETAG_ATTRIBUTE "metadata::etag"
+
+/**
+ * gs_utils_get_file_etag:
+ * @filename: a file name to get the ETag for
+ * @cancellable: (nullable): an optional #GCancellable or %NULL
+ *
+ * Gets the ETag for the @filename, previously stored by
+ * gs_utils_set_file_etag().
+ *
+ * Returns: (nullable) (transfer full): The ETag stored for the @filename,
+ *    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)
+{
+       g_autoptr(GFile) file = NULL;
+       g_autoptr(GFileInfo) info = NULL;
+
+       g_return_val_if_fail (filename != NULL, NULL);
+
+       if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+               return 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)
+               return NULL;
+
+       return g_strdup (g_file_info_get_attribute_string (info, METADATA_ETAG_ATTRIBUTE));
+}
+
+/**
+ * gs_utils_set_file_etag:
+ * @filename: a file name 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
+ * back with gs_utils_get_file_etag().
+ *
+ * The @filename 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)
+{
+       g_autoptr(GFile) file = NULL;
+
+       g_return_val_if_fail (filename != NULL, 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);
+       }
+
+       return g_file_set_attribute_string (file, METADATA_ETAG_ATTRIBUTE, etag, G_FILE_QUERY_INFO_NONE, 
cancellable, NULL);
+}
+
 /* vim: set noexpandtab: */
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index 66d49bc42..d83d79938 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -128,6 +128,11 @@ 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,
+                                                GCancellable           *cancellable);
+gboolean        gs_utils_set_file_etag         (const gchar            *filename,
+                                                const gchar            *etag,
+                                                GCancellable           *cancellable);
 
 #if !GLIB_CHECK_VERSION(2, 64, 0)
 typedef void GsMainContextPusher;


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