/
tracker-miner-files.c
1441 lines (1206 loc) · 45.5 KB
/
tracker-miner-files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config-miners.h"
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <libtracker-miners-common/tracker-common.h>
#include "tracker-miner-files.h"
#include "tracker-miner-files-methods.h"
#include "tracker-config.h"
#include "tracker-storage.h"
#include "tracker-extract-watchdog.h"
#include "tracker-utils.h"
#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 DEFAULT_GRAPH "tracker:FileSystem"
#define FILE_ATTRIBUTES \
G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT "," \
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
G_FILE_ATTRIBUTE_STANDARD_NAME "," \
G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
G_FILE_ATTRIBUTE_STANDARD_SIZE "," \
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
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)))
static GQuark miner_files_error_quark = 0;
struct TrackerMinerFilesPrivate {
TrackerConfig *config;
TrackerStorage *storage;
TrackerExtractWatchdog *extract_watchdog;
guint grace_period_timeout_id;
TrackerDomainOntology *domain_ontology;
GSettings *extract_settings;
GList *allowed_text_patterns;
guint disk_space_check_id;
gboolean disk_space_pause;
gboolean low_battery_pause;
#ifdef HAVE_POWER
TrackerPower *power;
#endif /* HAVE_POWER) */
gulong finished_handler;
gboolean mtime_check;
guint stale_volumes_check_id;
};
enum {
PROP_0,
PROP_CONFIG,
PROP_DOMAIN_ONTOLOGY,
PROP_STORAGE,
};
#define TEXT_ALLOWLIST "text-allowlist"
static void miner_files_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void miner_files_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void miner_files_constructed (GObject *object);
static void miner_files_finalize (GObject *object);
#ifdef HAVE_POWER
static void check_battery_status (TrackerMinerFiles *fs);
static void battery_status_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data);
static void index_on_battery_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data);
#endif /* HAVE_POWER */
static void init_index_roots (TrackerMinerFiles *miner);
static void init_stale_volume_removal (TrackerMinerFiles *miner);
static void disk_space_check_start (TrackerMinerFiles *mf);
static void disk_space_check_stop (TrackerMinerFiles *mf);
static void low_disk_space_limit_cb (GObject *gobject,
GParamSpec *arg1,
gpointer user_data);
static void miner_files_process_file (TrackerMinerFS *fs,
GFile *file,
GFileInfo *info,
TrackerSparqlBuffer *buffer,
gboolean create);
static void miner_files_process_file_attributes (TrackerMinerFS *fs,
GFile *file,
GFileInfo *info,
TrackerSparqlBuffer *buffer);
static void miner_files_remove_children (TrackerMinerFS *fs,
GFile *file,
TrackerSparqlBuffer *buffer);
static void miner_files_remove_file (TrackerMinerFS *fs,
GFile *file,
TrackerSparqlBuffer *buffer,
gboolean is_dir);
static void miner_files_move_file (TrackerMinerFS *fs,
GFile *file,
GFile *source_file,
TrackerSparqlBuffer *buffer,
gboolean recursive);
static void miner_files_finished (TrackerMinerFS *fs,
gdouble elapsed,
gint directories_found,
gint directories_ignored,
gint files_found,
gint files_ignored);
static void miner_finished_cb (TrackerMinerFS *fs,
gdouble seconds_elapsed,
guint total_directories_found,
guint total_directories_ignored,
guint total_files_found,
guint total_files_ignored,
gpointer user_data);
static void miner_files_in_removable_media_remove_by_date (TrackerMinerFiles *miner,
GDateTime *datetime);
G_DEFINE_TYPE_WITH_PRIVATE (TrackerMinerFiles, tracker_miner_files, TRACKER_TYPE_MINER_FS)
static void
miner_files_started (TrackerMiner *miner)
{
TRACKER_MINER_CLASS (tracker_miner_files_parent_class)->started (miner);
init_index_roots (TRACKER_MINER_FILES (miner));
}
static void
tracker_miner_files_class_init (TrackerMinerFilesClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
TrackerMinerClass *miner_class = TRACKER_MINER_CLASS (klass);
TrackerMinerFSClass *miner_fs_class = TRACKER_MINER_FS_CLASS (klass);
object_class->constructed = miner_files_constructed;
object_class->finalize = miner_files_finalize;
object_class->get_property = miner_files_get_property;
object_class->set_property = miner_files_set_property;
miner_class->started = miner_files_started;
miner_fs_class->process_file = miner_files_process_file;
miner_fs_class->process_file_attributes = miner_files_process_file_attributes;
miner_fs_class->finished = miner_files_finished;
miner_fs_class->remove_file = miner_files_remove_file;
miner_fs_class->remove_children = miner_files_remove_children;
miner_fs_class->move_file = miner_files_move_file;
g_object_class_install_property (object_class,
PROP_CONFIG,
g_param_spec_object ("config",
"Config",
"Config",
TRACKER_TYPE_CONFIG,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_DOMAIN_ONTOLOGY,
g_param_spec_boxed ("domain-ontology",
NULL, NULL,
TRACKER_TYPE_DOMAIN_ONTOLOGY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_STORAGE,
g_param_spec_object ("storage",
NULL, NULL,
TRACKER_TYPE_STORAGE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
miner_files_error_quark = g_quark_from_static_string ("TrackerMinerFiles");
}
static void
tracker_miner_files_check_unextracted (TrackerMinerFiles *mf)
{
g_debug ("Starting extractor");
tracker_extract_watchdog_ensure_started (mf->private->extract_watchdog);
}
static gboolean
extractor_lost_timeout_cb (gpointer user_data)
{
TrackerMinerFiles *mf = user_data;
tracker_miner_files_check_unextracted (mf);
mf->private->grace_period_timeout_id = 0;
return G_SOURCE_REMOVE;
}
static void
on_extractor_lost (TrackerExtractWatchdog *watchdog,
TrackerMinerFiles *mf)
{
g_debug ("tracker-extract vanished, maybe restarting.");
/* Give a period of grace before restarting, so we allow replacing
* from eg. a terminal.
*/
mf->private->grace_period_timeout_id =
g_timeout_add_seconds (1, extractor_lost_timeout_cb, mf);
}
static void
on_extractor_status (TrackerExtractWatchdog *watchdog,
const gchar *status,
gdouble progress,
gint remaining,
TrackerMinerFiles *mf)
{
if (!tracker_miner_is_paused (TRACKER_MINER (mf))) {
g_object_set (mf,
"status", status,
"progress", progress,
"remaining-time", remaining,
NULL);
}
}
static void
tracker_miner_files_init (TrackerMinerFiles *mf)
{
TrackerMinerFilesPrivate *priv;
priv = mf->private = TRACKER_MINER_FILES_GET_PRIVATE (mf);
#ifdef HAVE_POWER
priv->power = tracker_power_new ();
if (priv->power) {
g_signal_connect (priv->power, "notify::on-low-battery",
G_CALLBACK (battery_status_cb),
mf);
g_signal_connect (priv->power, "notify::on-battery",
G_CALLBACK (battery_status_cb),
mf);
}
#endif /* HAVE_POWER */
priv->finished_handler = g_signal_connect_after (mf, "finished",
G_CALLBACK (miner_finished_cb),
NULL);
priv->mtime_check = TRUE;
}
static void
removable_days_threshold_changed (TrackerMinerFiles *mf)
{
/* Check if the stale volume removal configuration changed from enabled to disabled
* or from disabled to enabled */
if (tracker_config_get_removable_days_threshold (mf->private->config) == 0 &&
mf->private->stale_volumes_check_id != 0) {
/* From having the check enabled to having it disabled, remove the timeout */
TRACKER_NOTE (CONFIG, g_message ("Stale volume removal now disabled, removing timeout"));
g_source_remove (mf->private->stale_volumes_check_id);
mf->private->stale_volumes_check_id = 0;
} else if (tracker_config_get_removable_days_threshold (mf->private->config) > 0 &&
mf->private->stale_volumes_check_id == 0) {
TRACKER_NOTE (CONFIG, g_message ("Stale volume removal now enabled, initializing timeout"));
/* From having the check disabled to having it enabled, so fire up the
* timeout. */
init_stale_volume_removal (TRACKER_MINER_FILES (mf));
}
}
static void
miner_files_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TrackerMinerFilesPrivate *priv;
priv = TRACKER_MINER_FILES_GET_PRIVATE (object);
switch (prop_id) {
case PROP_CONFIG:
priv->config = g_value_dup_object (value);
break;
case PROP_DOMAIN_ONTOLOGY:
priv->domain_ontology = g_value_dup_boxed (value);
break;
case PROP_STORAGE:
priv->storage = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
miner_files_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TrackerMinerFilesPrivate *priv;
priv = TRACKER_MINER_FILES_GET_PRIVATE (object);
switch (prop_id) {
case PROP_CONFIG:
g_value_set_object (value, priv->config);
break;
case PROP_DOMAIN_ONTOLOGY:
g_value_set_boxed (value, priv->domain_ontology);
break;
case PROP_STORAGE:
g_value_set_object (value, priv->storage);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
miner_files_finalize (GObject *object)
{
TrackerMinerFiles *mf;
TrackerMinerFilesPrivate *priv;
TrackerIndexingTree *indexing_tree;
mf = TRACKER_MINER_FILES (object);
priv = mf->private;
indexing_tree = tracker_miner_fs_get_indexing_tree (TRACKER_MINER_FS (mf));
g_signal_handlers_disconnect_by_data (indexing_tree, mf);
if (priv->grace_period_timeout_id != 0) {
g_source_remove (priv->grace_period_timeout_id);
priv->grace_period_timeout_id = 0;
}
g_clear_object (&mf->private->extract_settings);
g_list_free_full (mf->private->allowed_text_patterns, (GDestroyNotify) g_pattern_spec_free);
g_signal_handlers_disconnect_by_func (priv->extract_watchdog,
on_extractor_lost,
NULL);
g_clear_object (&priv->extract_watchdog);
if (priv->config) {
g_signal_handlers_disconnect_by_func (priv->config,
low_disk_space_limit_cb,
NULL);
g_object_unref (priv->config);
}
disk_space_check_stop (TRACKER_MINER_FILES (object));
#ifdef HAVE_POWER
if (priv->power) {
g_object_unref (priv->power);
}
#endif /* HAVE_POWER */
tracker_domain_ontology_unref (priv->domain_ontology);
if (priv->storage) {
g_object_unref (priv->storage);
}
if (priv->stale_volumes_check_id) {
g_source_remove (priv->stale_volumes_check_id);
priv->stale_volumes_check_id = 0;
}
G_OBJECT_CLASS (tracker_miner_files_parent_class)->finalize (object);
}
static void
set_up_mount_point_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
g_autofree GError *error = NULL;
tracker_sparql_statement_update_finish (TRACKER_SPARQL_STATEMENT (source),
result, &error);
if (error) {
g_critical ("Could not set mount point in database, %s",
error->message);
}
}
static void
set_up_mount_point (TrackerMinerFiles *miner,
GFile *mount_point,
gboolean mounted,
TrackerBatch *batch)
{
TrackerSparqlConnection *conn;
g_autoptr (TrackerSparqlStatement) stmt = NULL;
g_autofree gchar *uri = NULL;
g_autoptr (GDateTime) now = NULL;
uri = g_file_get_uri (mount_point);
now = g_date_time_new_now_utc ();
g_debug ("Mount point state (%s) being set in DB for mount_point '%s'",
mounted ? "MOUNTED" : "UNMOUNTED",
uri);
conn = tracker_miner_get_connection (TRACKER_MINER (miner));
stmt = tracker_load_statement (conn, "update-mountpoint.rq", NULL);
if (batch) {
tracker_batch_add_statement (batch, stmt,
"mountPoint", G_TYPE_STRING, uri,
"mounted", G_TYPE_BOOLEAN, mounted,
"currentDate", G_TYPE_DATE_TIME, now,
NULL);
} else {
tracker_sparql_statement_bind_string (stmt, "mountPoint", uri);
tracker_sparql_statement_bind_boolean (stmt, "mounted", mounted);
tracker_sparql_statement_bind_datetime (stmt, "currentDate", now);
tracker_sparql_statement_update_async (stmt,
NULL,
set_up_mount_point_cb,
NULL);
}
}
static void
delete_index_root (TrackerMinerFiles *miner,
GFile *mount_point,
TrackerBatch *batch)
{
g_autofree gchar *uri = NULL;
TrackerSparqlConnection *conn = tracker_batch_get_connection (batch);
g_autoptr (TrackerSparqlStatement) stmt = NULL;
uri = g_file_get_uri (mount_point);
stmt = tracker_load_statement (conn, "delete-index-root.rq", NULL);
tracker_batch_add_statement (batch, stmt,
"rootFolder", G_TYPE_STRING, uri,
NULL);
}
static void
init_index_roots_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
g_autofree GError *error = NULL;
tracker_batch_execute_finish (TRACKER_BATCH (source), result, &error);
if (error) {
g_critical ("Could not initialize currently active mount points: %s",
error->message);
} else {
init_stale_volume_removal (user_data);
}
}
static void
init_index_roots (TrackerMinerFiles *miner_files)
{
TrackerMiner *miner = TRACKER_MINER (miner_files);
TrackerSparqlConnection *conn;
g_autoptr (TrackerSparqlStatement) stmt = NULL;
TrackerIndexingTree *indexing_tree;
g_autoptr (GList) roots = NULL;
g_autoptr (GHashTable) handled = NULL;
g_autoptr (TrackerBatch) batch = NULL;
g_autoptr (GError) error = NULL;
g_autoptr (TrackerSparqlCursor) cursor = NULL;
GList *l;
g_debug ("Initializing mount points...");
conn = tracker_miner_get_connection (miner);
stmt = tracker_load_statement (conn, "get-index-roots.rq", &error);
if (stmt) {
/* First, get all mounted volumes, according to tracker-store (SYNC!) */
cursor = tracker_sparql_statement_execute (stmt, NULL, &error);
}
if (error) {
g_critical ("Could not obtain the mounted volumes: %s", error->message);
return;
}
batch = tracker_sparql_connection_create_batch (conn);
indexing_tree = tracker_miner_fs_get_indexing_tree (TRACKER_MINER_FS (miner));
handled = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal,
g_object_unref, NULL);
while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
const gchar *uri;
gboolean is_removable, is_optical;
GFile *file;
uri = tracker_sparql_cursor_get_string (cursor, 0, NULL);
is_removable = tracker_sparql_cursor_get_boolean (cursor, 1);
is_optical = tracker_sparql_cursor_get_boolean (cursor, 2);
file = g_file_new_for_uri (uri);
g_hash_table_add (handled, file);
if (tracker_indexing_tree_file_is_root (indexing_tree, file)) {
/* Directory is indexed and configured */
if (is_removable || is_optical) {
set_up_mount_point (TRACKER_MINER_FILES (miner),
file,
TRUE,
batch);
}
} else {
/* Directory is indexed, but no longer configured */
if (tracker_config_get_removable_days_threshold (miner_files->private->config) > 0 &&
((is_optical &&
tracker_config_get_index_optical_discs (miner_files->private->config)) ||
(!is_optical && is_removable &&
tracker_config_get_index_removable_devices (miner_files->private->config)))) {
/* Preserve */
set_up_mount_point (TRACKER_MINER_FILES (miner),
file, FALSE, batch);
} else {
/* Not a removable device to preserve, or a no
* longer configured folder.
*/
delete_index_root (TRACKER_MINER_FILES (miner),
file, batch);
}
}
}
roots = tracker_indexing_tree_list_roots (indexing_tree);
for (l = roots; l; l = l->next) {
TrackerStorage *storage = miner_files->private->storage;
TrackerStorageType type;
GFile *file = l->data;
if (g_hash_table_contains (handled, file))
continue;
type = tracker_storage_get_type_for_file (storage, file);
if ((type & TRACKER_STORAGE_REMOVABLE) != 0)
set_up_mount_point (miner_files, file, TRUE, NULL);
}
tracker_batch_execute_async (batch,
NULL,
init_index_roots_cb,
miner);
}
static gboolean
cleanup_stale_removable_volumes_cb (gpointer user_data)
{
TrackerMinerFiles *miner = TRACKER_MINER_FILES (user_data);
g_autoptr (GDateTime) now = NULL, n_days_ago = NULL;
gint n_days_threshold;
n_days_threshold = tracker_config_get_removable_days_threshold (miner->private->config);
if (n_days_threshold == 0)
return TRUE;
g_debug ("Running stale volumes check...");
now = g_date_time_new_now_utc ();
n_days_ago = g_date_time_add_days (now, -n_days_threshold);
miner_files_in_removable_media_remove_by_date (miner, n_days_ago);
return TRUE;
}
static void
init_stale_volume_removal (TrackerMinerFiles *miner)
{
/* If disabled, make sure we don't do anything */
if (tracker_config_get_removable_days_threshold (miner->private->config) == 0) {
g_debug ("Stale volume check is disabled");
return;
}
/* Run right away the first check */
cleanup_stale_removable_volumes_cb (miner);
g_debug ("Initializing stale volume check timeout...");
/* Then, setup new timeout event every day */
miner->private->stale_volumes_check_id =
g_timeout_add_seconds (SECONDS_PER_DAY + 1,
cleanup_stale_removable_volumes_cb,
miner);
}
#ifdef HAVE_POWER
static void
set_up_throttle (TrackerMinerFiles *mf,
gboolean enable)
{
gdouble throttle;
gint config_throttle;
config_throttle = tracker_config_get_throttle (mf->private->config);
throttle = (1.0 / 20) * config_throttle;
if (enable) {
throttle += 0.25;
}
throttle = CLAMP (throttle, 0, 1);
g_debug ("Setting new throttle to %0.3f", throttle);
tracker_miner_fs_set_throttle (TRACKER_MINER_FS (mf), throttle);
}
static void
check_battery_status (TrackerMinerFiles *mf)
{
gboolean on_battery, on_low_battery;
gboolean should_pause = FALSE;
gboolean should_throttle = FALSE;
if (mf->private->power == NULL) {
return;
}
on_low_battery = tracker_power_get_on_low_battery (mf->private->power);
on_battery = tracker_power_get_on_battery (mf->private->power);
if (!on_battery) {
g_debug ("Running on AC power");
should_pause = FALSE;
should_throttle = FALSE;
} else if (on_low_battery) {
g_message ("Running on LOW Battery, pausing");
should_pause = TRUE;
should_throttle = TRUE;
} else {
should_throttle = TRUE;
/* Check if miner should be paused based on configuration */
if (!tracker_config_get_index_on_battery (mf->private->config)) {
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_miner_files_get_first_index_done (mf)) {
g_debug ("Running on battery and first-time index "
"already done, pausing");
should_pause = TRUE;
} else {
g_debug ("Running on battery, but first-time index not "
"already finished, keeping on");
}
} else {
g_debug ("Running on battery");
}
}
if (should_pause) {
/* Don't try to pause again */
if (!mf->private->low_battery_pause) {
mf->private->low_battery_pause = TRUE;
tracker_miner_pause (TRACKER_MINER (mf));
}
} else {
/* Don't try to resume again */
if (mf->private->low_battery_pause) {
tracker_miner_resume (TRACKER_MINER (mf));
mf->private->low_battery_pause = FALSE;
}
}
set_up_throttle (mf, should_throttle);
}
/* Called when battery status change is detected */
static void
battery_status_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
TrackerMinerFiles *mf = user_data;
check_battery_status (mf);
}
/* Called when battery-related configuration change is detected */
static void
index_on_battery_cb (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
TrackerMinerFiles *mf = user_data;
check_battery_status (mf);
}
#endif /* HAVE_POWER */
/* Called when mining has finished the first time */
static void
miner_finished_cb (TrackerMinerFS *fs,
gdouble seconds_elapsed,
guint total_directories_found,
guint total_directories_ignored,
guint total_files_found,
guint total_files_ignored,
gpointer user_data)
{
TrackerMinerFiles *mf = TRACKER_MINER_FILES (fs);
/* Create stamp file if not already there */
if (!tracker_miner_files_get_first_index_done (mf)) {
tracker_miner_files_set_first_index_done (mf, TRUE);
}
/* And remove the signal handler so that it's not
* called again */
if (mf->private->finished_handler) {
g_signal_handler_disconnect (fs, mf->private->finished_handler);
mf->private->finished_handler = 0;
}
#ifdef HAVE_POWER
check_battery_status (mf);
#endif /* HAVE_POWER */
}
static GFile *
get_cache_dir (TrackerMinerFiles *mf)
{
GFile *cache;
cache = tracker_domain_ontology_get_cache (mf->private->domain_ontology);
return g_file_get_child (cache, "files");
}
static gboolean
disk_space_check (TrackerMinerFiles *mf)
{
GFile *file;
gint limit;
gchar *data_dir;
gdouble remaining;
limit = tracker_config_get_low_disk_space_limit (mf->private->config);
if (limit < 1) {
return FALSE;
}
/* Get % of remaining space in the partition where the cache is */
file = get_cache_dir (mf);
data_dir = g_file_get_path (file);
remaining = tracker_file_system_get_remaining_space_percentage (data_dir);
g_free (data_dir);
g_object_unref (file);
if (remaining <= limit) {
g_message ("WARNING: Available disk space (%lf%%) is below "
"configured threshold for acceptable working (%d%%)",
remaining, limit);
return TRUE;
}
return FALSE;
}
static gboolean
disk_space_check_cb (gpointer user_data)
{
TrackerMinerFiles *mf = user_data;
if (disk_space_check (mf)) {
/* Don't try to pause again */
if (!mf->private->disk_space_pause) {
mf->private->disk_space_pause = TRUE;
tracker_miner_pause (TRACKER_MINER (mf));
}
} else {
/* Don't try to resume again */
if (mf->private->disk_space_pause) {
tracker_miner_resume (TRACKER_MINER (mf));
mf->private->disk_space_pause = FALSE;
}
}
return TRUE;
}
static void
disk_space_check_start (TrackerMinerFiles *mf)
{
gint limit;
if (mf->private->disk_space_check_id != 0) {
return;
}
limit = tracker_config_get_low_disk_space_limit (mf->private->config);
if (limit != -1) {
TRACKER_NOTE (CONFIG, g_message ("Starting disk space check for every %d seconds",
DISK_SPACE_CHECK_FREQUENCY));
mf->private->disk_space_check_id =
g_timeout_add_seconds (DISK_SPACE_CHECK_FREQUENCY,
disk_space_check_cb,
mf);
/* Call the function now too to make sure we have an
* initial value too!
*/
disk_space_check_cb (mf);
} else {
TRACKER_NOTE (CONFIG, g_message ("Not setting disk space, configuration is set to -1 (disabled)"));
}
}
static void
disk_space_check_stop (TrackerMinerFiles *mf)
{
if (mf->private->disk_space_check_id) {
TRACKER_NOTE (CONFIG, g_message ("Stopping disk space check"));
g_source_remove (mf->private->disk_space_check_id);
mf->private->disk_space_check_id = 0;
}
}
static void
low_disk_space_limit_cb (GObject *gobject,
GParamSpec *arg1,
gpointer user_data)
{
TrackerMinerFiles *mf = user_data;
disk_space_check_cb (mf);
}
static void
indexing_tree_directory_added_cb (TrackerIndexingTree *indexing_tree,
GFile *directory,
gpointer user_data)
{
TrackerMinerFiles *miner_files = user_data;
TrackerStorage *storage = miner_files->private->storage;
TrackerStorageType type;
type = tracker_storage_get_type_for_file (storage, directory);
if ((type & TRACKER_STORAGE_REMOVABLE) != 0)
set_up_mount_point (miner_files, directory, TRUE, NULL);
}
static void
indexing_tree_directory_removed_cb (TrackerIndexingTree *indexing_tree,
GFile *directory,
gpointer user_data)
{
TrackerMinerFiles *miner_files = user_data;
TrackerStorage *storage = miner_files->private->storage;
TrackerStorageType type;
TrackerSparqlConnection *conn;
g_autoptr (TrackerBatch) batch = NULL;
g_autoptr (GError) error = NULL;
gboolean delete = FALSE, update_mount = FALSE;
type = tracker_storage_get_type_for_file (storage, directory);
if ((type & TRACKER_STORAGE_REMOVABLE) != 0) {
if (!tracker_config_get_index_removable_devices (miner_files->private->config))
delete = TRUE;
else if ((type & TRACKER_STORAGE_OPTICAL) != 0 &&
!tracker_config_get_index_optical_discs (miner_files->private->config))
delete = TRUE;
else if (tracker_config_get_removable_days_threshold (miner_files->private->config) == 0)
delete = TRUE;
else
update_mount = TRUE;
} else {
delete = TRUE;
}
conn = tracker_miner_get_connection (TRACKER_MINER (miner_files));
batch = tracker_sparql_connection_create_batch (conn);
if (delete)
delete_index_root (miner_files, directory, batch);
else if (update_mount)
set_up_mount_point (miner_files, directory, FALSE, batch);
if (!tracker_batch_execute (batch, NULL, &error))
g_warning ("Error updating indexed folder: %s", error->message);
}
static void
miner_files_process_file (TrackerMinerFS *fs,
GFile *file,
GFileInfo *info,
TrackerSparqlBuffer *buffer,
gboolean create)
{
tracker_miner_files_process_file (fs, file, info, buffer, create);
}
static void
miner_files_process_file_attributes (TrackerMinerFS *fs,
GFile *file,
GFileInfo *info,
TrackerSparqlBuffer *buffer)
{
tracker_miner_files_process_file_attributes (fs, file, info, buffer);
}
static void
miner_files_finished (TrackerMinerFS *fs,
gdouble elapsed,
gint directories_found,
gint directories_ignored,
gint files_found,
gint files_ignored)
{
tracker_miner_files_set_last_crawl_done (TRACKER_MINER_FILES (fs), TRUE);
tracker_miner_files_check_unextracted (TRACKER_MINER_FILES (fs));
}
static void
miner_files_remove_children (TrackerMinerFS *fs,
GFile *file,
TrackerSparqlBuffer *buffer)
{
tracker_sparql_buffer_log_delete_content (buffer, file);
}
static void
miner_files_remove_file (TrackerMinerFS *fs,
GFile *file,
TrackerSparqlBuffer *buffer,
gboolean is_dir)
{
if (is_dir)
tracker_sparql_buffer_log_delete_content (buffer, file);
tracker_sparql_buffer_log_delete (buffer, file);
}
static void
miner_files_move_file (TrackerMinerFS *fs,
GFile *file,
GFile *source_file,