[tracker-miners: 1/2] tracker-miner-files: save file creation time




commit bb96cec3c7f5fe0029d081b883934626357d643b
Author: Nishit Patel <nishitlimbani130 gmail com>
Date:   Fri Jun 18 15:57:09 2021 +0530

    tracker-miner-files: save file creation time
    
    Add support for storing the creation time in the database.
    as GLib version 2.70 (GNOME/glib!2017) will provide
    `g_file_info_set_creation_date_time`, Also make `GDateTime`
    as standard way of storing time in tracker-miners instead
    of storing time as string
    
    Closes: #158

 config-miners.h.meson.in                       |  3 ++
 meson.build                                    |  2 +
 src/miners/fs/tracker-miner-files.c            | 63 ++++++++++++++++++++------
 tests/libtracker-miner/tracker-miner-fs-test.c | 13 ++++--
 4 files changed, 63 insertions(+), 18 deletions(-)
---
diff --git a/config-miners.h.meson.in b/config-miners.h.meson.in
index 767521ea6..666630221 100644
--- a/config-miners.h.meson.in
+++ b/config-miners.h.meson.in
@@ -8,6 +8,9 @@
 /* Define the gettext package to be used */
 #mesondefine GETTEXT_PACKAGE
 
+/* Define if we have GLib version 2.70.0 or up */
+#mesondefine GIO_SUPPORTS_CREATION_TIME
+
 /* Define that GStreamer Discoverer should be used */
 #mesondefine GSTREAMER_BACKEND_DISCOVERER
 
diff --git a/meson.build b/meson.build
index 88749737b..1d0050d69 100644
--- a/meson.build
+++ b/meson.build
@@ -340,6 +340,7 @@ have_malloc_trim = meson.get_compiler('c').has_function('malloc_trim')
 # Config that goes in config.h
 conf.set('GUARANTEE_METADATA', get_option('guarantee_metadata') == true)
 conf.set('USING_UNZIPPSFILES', get_option('unzip_ps_gz_files') == true)
+conf.set('GIO_SUPPORTS_CREATION_TIME', glib.version().version_compare('>=2.70.0'))
 
 conf.set('HAVE_ENCA', charset_library_name == 'enca')
 conf.set('HAVE_EXEMPI', exempi.found())
@@ -491,6 +492,7 @@ summary = [
   '    Battery/mains power detection:          ' + battery_detection_library_name,
   '    Support for network status detection:   ' + have_network_manager.to_string(),
   '    Releasing heap memory with malloc_trim: ' + have_malloc_trim.to_string(),
+  '    Store creation time:                    ' + glib.version().version_compare('>=2.70.0').to_string(),
   '\nData Miners / Writebacks:',
   '    FS (File System):                       ' + have_tracker_miner_fs.to_string(),
   '    RSS:                                    ' + have_tracker_miner_rss.to_string(),
diff --git a/src/miners/fs/tracker-miner-files.c b/src/miners/fs/tracker-miner-files.c
index 098b105dc..1ff3b26c6 100644
--- a/src/miners/fs/tracker-miner-files.c
+++ b/src/miners/fs/tracker-miner-files.c
@@ -61,6 +61,7 @@
        G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
        G_FILE_ATTRIBUTE_STANDARD_SIZE "," \
        G_FILE_ATTRIBUTE_TIME_MODIFIED "," \
+       G_FILE_ATTRIBUTE_TIME_CREATED "," \
        G_FILE_ATTRIBUTE_TIME_ACCESS
 
 #define TRACKER_MINER_FILES_GET_PRIVATE(o) (tracker_miner_files_get_instance_private (TRACKER_MINER_FILES 
(o)))
@@ -2065,11 +2066,16 @@ miner_files_process_file (TrackerMinerFS      *fs,
        const gchar *mime_type, *graph;
        gchar *parent_urn;
        gchar *delete_properties_sparql = NULL;
-       time_t time_;
        GFile *parent;
-       gchar *uri, *time_str;
+       gchar *uri;
        gboolean is_directory;
        GDateTime *modified;
+#ifdef GIO_SUPPORTS_CREATION_TIME
+       GDateTime *accessed, *created;
+#else
+       time_t time_;
+       gchar *time_str;
+#endif
 
        priv = TRACKER_MINER_FILES (fs)->private;
 
@@ -2125,14 +2131,27 @@ miner_files_process_file (TrackerMinerFS      *fs,
        tracker_resource_set_int64 (resource, "nfo:fileSize",
                                    g_file_info_get_size (file_info));
 
-       time_str = g_date_time_format_iso8601 (modified);
-       tracker_resource_set_string (resource, "nfo:fileLastModified", time_str);
-       g_free (time_str);
+       tracker_resource_set_datetime (resource, "nfo:fileLastModified", modified);
 
+#ifdef GIO_SUPPORTS_CREATION_TIME
+       accessed = g_file_info_get_access_date_time (file_info);
+       if (!accessed)
+               accessed = g_date_time_new_from_unix_utc (0);
+
+       tracker_resource_set_datetime (resource, "nfo:fileLastAccessed", accessed);
+       g_date_time_unref (accessed);
+
+       created = g_file_info_get_creation_date_time (file_info);
+       if (created) {
+               tracker_resource_set_datetime (resource, "nfo:fileCreated", created);
+               g_date_time_unref (created);
+       }
+#else
        time_ = (time_t) g_file_info_get_attribute_uint64 (file_info, G_FILE_ATTRIBUTE_TIME_ACCESS);
        time_str = tracker_date_to_string (time_);
        tracker_resource_set_string (resource, "nfo:fileLastAccessed", time_str);
        g_free (time_str);
+#endif
 
        /* The URL of the DataObject (because IE = DO, this is correct) */
        tracker_resource_set_string (resource, "nie:url", uri);
@@ -2159,9 +2178,7 @@ miner_files_process_file (TrackerMinerFS      *fs,
                tracker_resource_set_string (graph_file, "nfo:fileName",
                                             g_file_info_get_display_name (file_info));
 
-               time_str = g_date_time_format_iso8601 (modified);
-               tracker_resource_set_string (graph_file, "nfo:fileLastModified", time_str);
-               g_free (time_str);
+               tracker_resource_set_datetime (graph_file, "nfo:fileLastModified", modified);
        }
 
        if (delete_properties_sparql)
@@ -2188,9 +2205,14 @@ miner_files_process_file_attributes (TrackerMinerFS      *fs,
                                      TrackerSparqlBuffer *buffer)
 {
        TrackerResource *resource;
-       time_t time_;
-       gchar *uri, *time_str;
+       gchar *uri;
        GDateTime *modified;
+#ifdef GIO_SUPPORTS_CREATION_TIME
+       GDateTime *accessed, *created;
+#else
+       gchar *time_str;
+       time_t time_;
+#endif
 
        uri = g_file_get_uri (file);
        resource = tracker_resource_new (uri);
@@ -2198,7 +2220,8 @@ miner_files_process_file_attributes (TrackerMinerFS      *fs,
        if (!info) {
                info = g_file_query_info (file,
                                          G_FILE_ATTRIBUTE_TIME_MODIFIED ","
-                                         G_FILE_ATTRIBUTE_TIME_ACCESS,
+                                         G_FILE_ATTRIBUTE_TIME_ACCESS ","
+                                         G_FILE_ATTRIBUTE_TIME_CREATED,
                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                          NULL, NULL);
        }
@@ -2208,16 +2231,28 @@ miner_files_process_file_attributes (TrackerMinerFS      *fs,
                modified = g_date_time_new_from_unix_utc (0);
 
        /* Update nfo:fileLastModified */
-       time_str = g_date_time_format_iso8601 (modified);
-       tracker_resource_set_string (resource, "nfo:fileLastModified", time_str);
+       tracker_resource_set_datetime (resource, "nfo:fileLastModified", modified);
        g_date_time_unref (modified);
-       g_free (time_str);
 
+#ifdef GIO_SUPPORTS_CREATION_TIME
        /* Update nfo:fileLastAccessed */
+       accessed = g_file_info_get_access_date_time (info);
+       tracker_resource_set_datetime (resource, "nfo:fileLastAccessed", accessed);
+       g_date_time_unref (accessed);
+
+       /* Update nfo:fileCreated */
+       created = g_file_info_get_creation_date_time (info);
+
+       if (created) {
+               tracker_resource_set_datetime (resource, "nfo:fileCreated", created);
+               g_date_time_unref (created);
+       }
+#else
        time_ = (time_t) g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
        time_str = tracker_date_to_string (time_);
        tracker_resource_set_string (resource, "nfo:fileLastAccessed", time_str);
        g_free (time_str);
+#endif
 
        g_free (uri);
 
diff --git a/tests/libtracker-miner/tracker-miner-fs-test.c b/tests/libtracker-miner/tracker-miner-fs-test.c
index 8f13d764b..eae9c23ff 100644
--- a/tests/libtracker-miner/tracker-miner-fs-test.c
+++ b/tests/libtracker-miner/tracker-miner-fs-test.c
@@ -35,7 +35,7 @@ test_miner_process_file (TrackerMinerFS      *miner,
                          gboolean             created)
 {
        TrackerResource *resource;
-       GDateTime *modification_time;
+       GDateTime *modification_time, *creation_time;
        TrackerIndexingTree *tree;
        gchar *uri, *parent_uri, *str;
        GFile *parent;
@@ -57,11 +57,16 @@ test_miner_process_file (TrackerMinerFS      *miner,
        if (info) {
                modification_time = g_file_info_get_modification_date_time (info);
                if (modification_time) {
-                       str = g_date_time_format_iso8601 (modification_time);
-                       tracker_resource_set_string (resource, "nfo:fileLastModified", str);
-                       g_free (str);
+                       tracker_resource_set_datetime (resource, "nfo:fileLastModified", modification_time);
                        g_date_time_unref (modification_time);
                }
+#ifdef GIO_SUPPORTS_CREATION_TIME
+               creation_time = g_file_info_get_creation_date_time (info);
+               if (creation_time) {
+                       tracker_resource_set_datetime (resource, "nfo:fileCreated", creation_time);
+                       g_date_time_unref (creation_time);
+               }
+#endif
        }
 
        tracker_resource_set_string (resource, "nie:url", uri);


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