summaryrefslogtreecommitdiff
path: root/src/miners/fs/tracker-miner-files.c
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2017-06-09 14:41:16 +0200
committerCarlos Garnacho <carlosg@gnome.org>2017-06-29 20:49:04 +0200
commit2df688a7c26d5169ed8bb92c177df941f935c07d (patch)
tree2de5e3b6ea8072f3f4b38c57168190ecce586dd8 /src/miners/fs/tracker-miner-files.c
parent2f58789a7ea2038de0fd88603e6062e066e18d91 (diff)
downloadtracker-2df688a7c26d5169ed8bb92c177df941f935c07d.tar.gz
tracker-miner-fs: Adopt DBManager API to manipulate stamp files
Those just matter for indexing purposes, so move all management here. tracker-extract has just been made to equate SCHED_IDLE_FIRST_INDEX to SCHED_ALWAYS.
Diffstat (limited to 'src/miners/fs/tracker-miner-files.c')
-rw-r--r--src/miners/fs/tracker-miner-files.c249
1 files changed, 245 insertions, 4 deletions
diff --git a/src/miners/fs/tracker-miner-files.c b/src/miners/fs/tracker-miner-files.c
index e9dc1efef..e68ec0710 100644
--- a/src/miners/fs/tracker-miner-files.c
+++ b/src/miners/fs/tracker-miner-files.c
@@ -49,6 +49,11 @@
#define DISK_SPACE_CHECK_FREQUENCY 10
#define SECONDS_PER_DAY 86400
+/* Stamp files to know crawling/indexing state */
+#define FIRST_INDEX_FILENAME "first-index.txt"
+#define LAST_CRAWL_FILENAME "last-crawl.txt"
+#define NEED_MTIME_CHECK_FILENAME "no-need-mtime-check.txt"
+
#define TRACKER_MINER_FILES_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRACKER_TYPE_MINER_FILES, TrackerMinerFilesPrivate))
static GQuark miner_files_error_quark = 0;
@@ -1342,7 +1347,7 @@ check_battery_status (TrackerMinerFiles *mf)
if (!tracker_config_get_index_on_battery_first_time (mf->private->config)) {
g_message ("Running on battery, but not enabled, pausing");
should_pause = TRUE;
- } else if (tracker_db_manager_get_first_index_done ()) {
+ } else if (tracker_miner_files_get_first_index_done ()) {
g_message ("Running on battery and first-time index "
"already done, pausing");
should_pause = TRUE;
@@ -1413,8 +1418,8 @@ miner_finished_cb (TrackerMinerFS *fs,
TrackerMinerFiles *mf = TRACKER_MINER_FILES (fs);
/* Create stamp file if not already there */
- if (!tracker_db_manager_get_first_index_done ()) {
- tracker_db_manager_set_first_index_done (TRUE);
+ if (!tracker_miner_files_get_first_index_done ()) {
+ tracker_miner_files_set_first_index_done (TRUE);
}
/* And remove the signal handler so that it's not
@@ -2498,7 +2503,7 @@ miner_files_finished (TrackerMinerFS *fs,
gint files_found,
gint files_ignored)
{
- tracker_db_manager_set_last_crawl_done (TRUE);
+ tracker_miner_files_set_last_crawl_done (TRUE);
}
TrackerMiner *
@@ -2949,3 +2954,239 @@ tracker_miner_files_is_file_eligible (TrackerMinerFiles *miner,
/* file is eligible to be indexed */
return TRUE;
}
+
+inline static gchar *
+get_first_index_filename (void)
+{
+ return g_build_filename (g_get_user_cache_dir (),
+ "tracker",
+ FIRST_INDEX_FILENAME,
+ NULL);
+}
+
+/**
+ * tracker_miner_files_get_first_index_done:
+ *
+ * Check if first full index of files was already done.
+ *
+ * Returns: %TRUE if a first full index have been done, %FALSE otherwise.
+ **/
+gboolean
+tracker_miner_files_get_first_index_done (void)
+{
+ gboolean exists;
+ gchar *filename;
+
+ filename = get_first_index_filename ();
+ exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+ g_free (filename);
+
+ return exists;
+}
+
+/**
+ * tracker_miner_files_set_first_index_done:
+ *
+ * Set the status of the first full index of files. Should be set to
+ * %FALSE if the index was never done or if a reindex is needed. When
+ * the index is completed, should be set to %TRUE.
+ **/
+void
+tracker_miner_files_set_first_index_done (gboolean done)
+{
+ gboolean already_exists;
+ gchar *filename;
+
+ filename = get_first_index_filename ();
+ already_exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+
+ if (done && !already_exists) {
+ GError *error = NULL;
+
+ /* If done, create stamp file if not already there */
+ if (!g_file_set_contents (filename, PACKAGE_VERSION, -1, &error)) {
+ g_warning (" Could not create file:'%s' failed, %s",
+ filename,
+ error->message);
+ g_error_free (error);
+ } else {
+ g_info (" First index file:'%s' created", filename);
+ }
+ } else if (!done && already_exists) {
+ /* If NOT done, remove stamp file */
+ g_info (" Removing first index file:'%s'", filename);
+
+ if (g_remove (filename)) {
+ g_warning (" Could not remove file:'%s': %m",
+ filename);
+ }
+ }
+
+ g_free (filename);
+}
+
+static inline gchar *
+get_last_crawl_filename (void)
+{
+ return g_build_filename (g_get_user_cache_dir (),
+ "tracker",
+ LAST_CRAWL_FILENAME,
+ NULL);
+}
+
+/**
+ * tracker_miner_files_get_last_crawl_done:
+ *
+ * Check when last crawl was performed.
+ *
+ * Returns: time_t() value when last crawl occurred, otherwise 0.
+ **/
+guint64
+tracker_miner_files_get_last_crawl_done (void)
+{
+ gchar *filename;
+ gchar *content;
+ guint64 then;
+
+ filename = get_last_crawl_filename ();
+
+ if (!g_file_get_contents (filename, &content, NULL, NULL)) {
+ g_info (" No previous timestamp, crawling forced");
+ return 0;
+ }
+
+ then = g_ascii_strtoull (content, NULL, 10);
+ g_free (content);
+
+ return then;
+}
+
+/**
+ * tracker_miner_files_set_last_crawl_done:
+ *
+ * Set the status of the first full index of files. Should be set to
+ * %FALSE if the index was never done or if a reindex is needed. When
+ * the index is completed, should be set to %TRUE.
+ **/
+void
+tracker_miner_files_set_last_crawl_done (gboolean done)
+{
+ gboolean already_exists;
+ gchar *filename;
+
+ filename = get_last_crawl_filename ();
+ already_exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+
+ if (done && !already_exists) {
+ GError *error = NULL;
+ gchar *content;
+
+ content = g_strdup_printf ("%" G_GUINT64_FORMAT, (guint64) time (NULL));
+
+ /* If done, create stamp file if not already there */
+ if (!g_file_set_contents (filename, content, -1, &error)) {
+ g_warning (" Could not create file:'%s' failed, %s",
+ filename,
+ error->message);
+ g_error_free (error);
+ } else {
+ g_info (" Last crawl file:'%s' created", filename);
+ }
+
+ g_free (content);
+ } else if (!done && already_exists) {
+ /* If NOT done, remove stamp file */
+ g_info (" Removing last crawl file:'%s'", filename);
+
+ if (g_remove (filename)) {
+ g_warning (" Could not remove file:'%s': %m",
+ filename);
+ }
+ }
+
+ g_free (filename);
+}
+
+inline static gchar *
+get_need_mtime_check_filename (void)
+{
+ return g_build_filename (g_get_user_cache_dir (),
+ "tracker",
+ NEED_MTIME_CHECK_FILENAME,
+ NULL);
+}
+
+/**
+ * tracker_miner_files_get_need_mtime_check:
+ *
+ * Check if the miner-fs was cleanly shutdown or not.
+ *
+ * Returns: %TRUE if we need to check mtimes for directories against
+ * the database on the next start for the miner-fs, %FALSE otherwise.
+ **/
+gboolean
+tracker_miner_files_get_need_mtime_check (void)
+{
+ gboolean exists;
+ gchar *filename;
+
+ filename = get_need_mtime_check_filename ();
+ exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+ g_free (filename);
+
+ /* Existence of the file means we cleanly shutdown before and
+ * don't need to do the mtime check again on this start.
+ */
+ return !exists;
+}
+
+/**
+ * tracker_miner_files_set_need_mtime_check:
+ * @needed: a #gboolean
+ *
+ * If the next start of miner-fs should perform a full mtime check
+ * against each directory found and those in the database (for
+ * complete synchronisation), then @needed should be #TRUE, otherwise
+ * #FALSE.
+ *
+ * Creates a file in $HOME/.cache/tracker/ if an mtime check is not
+ * needed. The idea behind this is that a check is forced if the file
+ * is not cleaned up properly on shutdown (i.e. due to a crash or any
+ * other uncontrolled shutdown reason).
+ **/
+void
+tracker_miner_files_set_need_mtime_check (gboolean needed)
+{
+ gboolean already_exists;
+ gchar *filename;
+
+ filename = get_need_mtime_check_filename ();
+ already_exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+
+ /* !needed = add file
+ * needed = remove file
+ */
+ if (!needed && !already_exists) {
+ GError *error = NULL;
+
+ /* Create stamp file if not already there */
+ if (!g_file_set_contents (filename, PACKAGE_VERSION, -1, &error)) {
+ g_warning (" Could not create file:'%s' failed, %s",
+ filename,
+ error->message);
+ g_error_free (error);
+ } else {
+ g_info (" Need mtime check file:'%s' created", filename);
+ }
+ } else if (needed && already_exists) {
+ /* Remove stamp file */
+ g_info (" Removing need mtime check file:'%s'", filename);
+
+ if (g_remove (filename)) {
+ g_warning (" Could not remove file:'%s': %m",
+ filename);
+ }
+ }
+
+ g_free (filename);
+}