summaryrefslogtreecommitdiff
path: root/src/miners/fs/tracker-main.c
blob: 17c67fb120ec93f0a1b1f7bc5907361349e0f646 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
/*
 * 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.h"

#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>

#include <libtracker-common/tracker-ioprio.h>
#include <libtracker-common/tracker-log.h>
#include <libtracker-common/tracker-ontologies.h>
#include <libtracker-common/tracker-file-utils.h>
#include <libtracker-common/tracker-sched.h>
#include <libtracker-common/tracker-enums.h>

#include <libtracker-miner/tracker-miner.h>

#include <libtracker-data/tracker-db-manager.h>

#include "tracker-config.h"
#include "tracker-marshal.h"
#include "tracker-miner-userguides.h"
#include "tracker-miner-applications.h"
#include "tracker-miner-files.h"
#include "tracker-miner-files-index.h"
#include "tracker-writeback.h"

#define ABOUT	  \
	"Tracker " PACKAGE_VERSION "\n"

#define LICENSE	  \
	"This program is free software and comes without any warranty.\n" \
	"It is licensed under version 2 or later of the General Public " \
	"License which can be viewed at:\n" \
	"\n" \
	"  http://www.gnu.org/licenses/gpl.txt\n"

#define SECONDS_PER_DAY 60 * 60 * 24

#define OPTION_DISABLE_FILES "files"
#define OPTION_DISABLE_APPLICATIONS "applications"
#define OPTION_DISABLE_USERGUIDES "userguides"

typedef enum {
	DISABLE_NONE,
	DISABLE_FILES,
	DISABLE_APPLICATIONS,
#ifdef HAVE_MAEMO
	DISABLE_USERGUIDES,
#endif /* HAVE_MAEMO */
} DisableOption;

static gboolean miner_disable_option_arg_func (const gchar  *option_value,
                                               const gchar  *value,
                                               gpointer      data,
                                               GError      **error);
static void     miner_handle_next             (void);


static GMainLoop *main_loop;
static GSList *miners;
static GSList *current_miner;
static gboolean finished_miners;

static gint verbosity = -1;
static gint initial_sleep = -1;
static gboolean no_daemon;
static gchar *eligible;
static GArray *disable_options = NULL;
/* static DisableOption disable_option[] = { 0 }; */
static gboolean version;
static guint miners_timeout_id = 0;

static GOptionEntry entries[] = {
	{ "verbosity", 'v', 0,
	  G_OPTION_ARG_INT, &verbosity,
	  N_("Logging, 0 = errors only, "
	     "1 = minimal, 2 = detailed and 3 = debug (default=0)"),
	  NULL },
	{ "initial-sleep", 's', 0,
	  G_OPTION_ARG_INT, &initial_sleep,
	  N_("Initial sleep time in seconds, "
	     "0->1000 (default=15)"),
	  NULL },
	{ "no-daemon", 'n', 0,
	  G_OPTION_ARG_NONE, &no_daemon,
	  N_("Runs until all configured locations are indexed and then exits"),
	  NULL },
	{ "eligible", 'e', 0,
	  G_OPTION_ARG_FILENAME, &eligible,
	  N_("Checks if FILE is eligible for being mined based on configuration"),
	  N_("FILE") },
	{ "disable-miner", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
	  G_OPTION_ARG_CALLBACK, miner_disable_option_arg_func,
	  N_("Disable miners started as part of this process, options include"
	     ": '" OPTION_DISABLE_FILES "'"
	     ", '" OPTION_DISABLE_APPLICATIONS "'"
#ifdef HAVE_MAEMO
	     ", '" OPTION_DISABLE_USERGUIDES "'"
#endif /* HAVE_MAEMO */
	  ),
	  N_("MINER") },
	{ "version", 'V', 0,
	  G_OPTION_ARG_NONE, &version,
	  N_("Displays version information"),
	  NULL },
	{ NULL }
};

static gboolean
miner_disable_option_arg_func (const gchar  *option_value,
                               const gchar  *value,
                               gpointer      data,
                               GError      **error)
{
	DisableOption option;
	gchar *value_casefold;
	gboolean found = FALSE;
	gint i;

	if (!value || *value == '\0') {
		/* Show help */
#ifdef HAVE_MAEMO
		g_set_error_literal (error,
		                     G_OPTION_ERROR,
		                     G_OPTION_ERROR_FAILED,
		                     "A value is required, either "
		                     "'" OPTION_DISABLE_FILES "', "
		                     "'" OPTION_DISABLE_APPLICATIONS "' or "
		                     "'" OPTION_DISABLE_USERGUIDES "'");
#else  /* HAVE_MAEMO */
		g_set_error_literal (error,
		                     G_OPTION_ERROR,
		                     G_OPTION_ERROR_FAILED,
		                     "A value is required, either "
		                     "'" OPTION_DISABLE_FILES "', "
		                     "'" OPTION_DISABLE_APPLICATIONS "'");
#endif /* HAVE_MAEMO */
		return FALSE;
	}

	value_casefold = g_utf8_casefold (value, -1);

	if (strcmp (value_casefold, OPTION_DISABLE_FILES) == 0) {
		option = DISABLE_FILES;
	} else if (strcmp (value_casefold, OPTION_DISABLE_APPLICATIONS) == 0) {
		option = DISABLE_APPLICATIONS;
#ifdef HAVE_MAEMO
	} else if (strcmp (value_casefold, OPTION_DISABLE_USERGUIDES) == 0) {
		option = DISABLE_USERGUIDES;
#endif /* HAVE_MAEMO */
	} else {
		g_set_error (error,
		             G_OPTION_ERROR,
		             G_OPTION_ERROR_FAILED,
		             "Miner '%s' is not recognized",
		             value);
		return FALSE;
	}

	g_free (value_casefold);

	/* Check we didn't already disable this miner */
	for (i = 0; i < disable_options->len; i++) {
		if (g_array_index (disable_options, gint, i) == option) {
			found = TRUE;
			break;
		}
	}

	if (!found) {
		g_array_append_val (disable_options, option);
	}

	return TRUE;
}

static void
sanity_check_option_values (TrackerConfig *config)
{
	g_message ("General options:");
	g_message ("  Verbosity  ............................  %d",
	           tracker_config_get_verbosity (config));
	g_message ("  Sched Idle  ...........................  %d",
	           tracker_config_get_sched_idle (config));
	g_message ("  Initial Sleep  ........................  %d",
	           tracker_config_get_initial_sleep (config));

	g_message ("Indexer options:");
	g_message ("  Throttle level  .......................  %d",
	           tracker_config_get_throttle (config));
	g_message ("  Indexing while on battery  ............  %s (first time only = %s)",
	           tracker_config_get_index_on_battery (config) ? "yes" : "no",
	           tracker_config_get_index_on_battery_first_time (config) ? "yes" : "no");

	if (tracker_config_get_low_disk_space_limit (config) == -1) {
		g_message ("  Low disk space limit  .................  Disabled");
	} else {
		g_message ("  Low disk space limit  .................  %d%%",
		           tracker_config_get_low_disk_space_limit (config));
	}
}

static void
signal_handler (int signo)
{
	static gboolean in_loop = FALSE;

	/* Die if we get re-entrant signals handler calls */
	if (in_loop) {
		_exit (EXIT_FAILURE);
	}

	switch (signo) {
	case SIGTERM:
	case SIGINT:
		in_loop = TRUE;
		if (main_loop != NULL) {
			g_main_loop_quit (main_loop);
		} else {
			exit (0);
		}
		/* Fall through */
	default:
		if (g_strsignal (signo)) {
			g_print ("\n");
			g_print ("Received signal:%d->'%s'\n",
			         signo,
			         g_strsignal (signo));
		}
		break;
	}
}

static void
initialize_signal_handler (void)
{
#ifndef G_OS_WIN32
	struct sigaction act;
	sigset_t         empty_mask;

	sigemptyset (&empty_mask);
	act.sa_handler = signal_handler;
	act.sa_mask    = empty_mask;
	act.sa_flags   = 0;

	sigaction (SIGTERM, &act, NULL);
	sigaction (SIGINT,  &act, NULL);
	sigaction (SIGHUP,  &act, NULL);
#endif /* G_OS_WIN32 */
}

static void
initialize_priority_and_scheduling (TrackerSchedIdle sched_idle,
                                    gboolean         first_time_index)
{
	/* Set CPU priority */
	if (sched_idle == TRACKER_SCHED_IDLE_ALWAYS ||
	    (sched_idle == TRACKER_SCHED_IDLE_FIRST_INDEX && first_time_index)) {
		tracker_sched_idle ();
	}

	/* Set disk IO priority and scheduling */
	tracker_ioprio_init ();

	/* Set process priority:
	 * The nice() function uses attribute "warn_unused_result" and
	 * so complains if we do not check its returned value. But it
	 * seems that since glibc 2.2.4, nice() can return -1 on a
	 * successful call so we have to check value of errno too.
	 * Stupid...
	 */

	g_message ("Setting priority nice level to 19");

	errno = 0;
	if (nice (19) == -1 && errno != 0) {
		const gchar *str = g_strerror (errno);

		g_message ("Couldn't set nice value to 19, %s",
		           str ? str : "no error given");
	}
}

static gboolean
should_crawl (TrackerConfig *config,
              gboolean      *forced)
{
	gint crawling_interval;

	crawling_interval = tracker_config_get_crawling_interval (config);

	g_message ("Checking whether to crawl file system based on configured crawling interval:");

	if (crawling_interval == -2) {
		g_message ("  Disabled");
		return FALSE;
	} else if (crawling_interval == -1) {
		g_message ("  Maybe (depends on a clean last shutdown)");
		return TRUE;
	} else if (crawling_interval == 0) {
		g_message ("  Forced");

		if (forced) {
			*forced = TRUE;
		}

		return TRUE;
	} else {
		guint64 then, now;

		then = tracker_db_manager_get_last_crawl_done ();

		if (then < 1) {
			return TRUE;
		}

		now = (guint64) time (NULL);

		if (now < then + (crawling_interval * SECONDS_PER_DAY)) {
			g_message ("  Postponed");
			return FALSE;
		} else {
			g_message ("  (More than) %d days after last crawling, enabled", crawling_interval);
			return FALSE;
		}
	}
}

static gboolean
miner_disabled_check (void)
{
	gchar *name = NULL;
	gchar *name_casefold;
	gboolean disabled = FALSE;
	gint i;

	if (!disable_options || disable_options->len < 1) {
		return FALSE;
	}

	g_object_get (current_miner->data, "name", &name, NULL);

	if (!name || *name == '\0') {
		g_warning ("Expected miner to have 'name' property set. "
		           "Can not disable this particular miner as a result...");
		return FALSE;
	}

	name_casefold = g_utf8_casefold (name, -1);

	for (i = 0; i < disable_options->len && !disabled; i++) {
		DisableOption o = (gint) g_array_index (disable_options, gint, i);

		switch (o) {
		case DISABLE_FILES:
			disabled = strcmp (name_casefold, OPTION_DISABLE_FILES) == 0;
			break;
		case DISABLE_APPLICATIONS:
			disabled = strcmp (name_casefold, OPTION_DISABLE_APPLICATIONS) == 0;
			break;

#ifdef HAVE_MAEMO
		case DISABLE_USERGUIDES:
			disabled = strcmp (name_casefold, OPTION_DISABLE_USERGUIDES) == 0;
			break;
#endif /* HAVE_MAEMO */

		default:
			g_assert_not_reached ();
			break;
		}
	}

	if (disabled) {
		g_message ("Miner '%s' was disabled on the command line, moving to next...", name);
		miner_handle_next ();
	}

	g_free (name_casefold);
	g_free (name);

	return disabled;
}

static void
miner_handle_next (void)
{
	if (finished_miners) {
		return;
	}

	if (!current_miner) {
		current_miner = miners;
	} else {
		current_miner = current_miner->next;
	}

	if (!current_miner) {
		finished_miners = TRUE;

		g_message ("All miners are now finished");

		/* We're not sticking around for file updates, so stop
		 * the mainloop and exit.
		 */
		if (no_daemon && main_loop) {
			g_main_loop_quit (main_loop);
		}

		return;
	}

	/* Check disabled miners */
	if (miner_disabled_check ()) {
		return;
	}

	if (!tracker_miner_is_started (current_miner->data)) {
		g_message ("Starting next miner...");
		tracker_miner_start (current_miner->data);
	}
}

static gboolean
miner_handle_first_cb (gpointer data)
{
	miners_timeout_id = 0;
	miner_handle_next ();
	return FALSE;
}

static void
miner_handle_first (TrackerConfig *config,
		    gboolean       do_mtime_checking)
{
	gint initial_sleep;

	if (!do_mtime_checking) {
		g_debug ("Avoiding initial sleep, no mtime check needed");
		miner_handle_next ();
		return;
	}

	/* If requesting to run as no-daemon, start right away */
	if (no_daemon) {
		miner_handle_next ();
		return;
	}

	/* If no need to initially sleep, start right away */
	initial_sleep = tracker_config_get_initial_sleep (config);

	if (initial_sleep <= 0) {
		miner_handle_next ();
		return;
	}

	g_debug ("Performing initial sleep of %d seconds",
	         initial_sleep);
	miners_timeout_id = g_timeout_add_seconds (initial_sleep,
						   miner_handle_first_cb,
						   NULL);
}

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)
{
	tracker_info ("Finished mining in seconds:%f, total directories:%d, total files:%d",
	              seconds_elapsed,
	              total_directories_found,
	              total_files_found);

	if (TRACKER_IS_MINER_FILES (fs) &&
	    tracker_miner_fs_get_initial_crawling (fs)) {
		tracker_db_manager_set_last_crawl_done (TRUE);
	}

	miner_handle_next ();
}

static void
finalize_miner (TrackerMiner *miner)
{
	g_object_unref (G_OBJECT (miner));
}

static GList *
get_dir_children_as_gfiles (const gchar *path)
{
	GList *children = NULL;
	GDir *dir;

	dir = g_dir_open (path, 0, NULL);

	if (dir) {
		const gchar *basename;

		while ((basename = g_dir_read_name (dir)) != NULL) {
			GFile *child;
			gchar *str;

			str = g_build_filename (path, basename, NULL);
			child = g_file_new_for_path (str);
			g_free (str);

			children = g_list_prepend (children, child);
		}

		g_dir_close (dir);
	}

	return children;
}

static void
dummy_log_handler (const gchar    *domain,
                   GLogLevelFlags  log_level,
                   const gchar    *message,
                   gpointer        user_data)
{
	return;
}

static void
check_eligible (void)
{
	TrackerConfig *config;
	GFile *file;
	GFileInfo *info;
	GError *error = NULL;
	gchar *path;
	guint log_handler_id;
	gboolean exists = TRUE;
	gboolean is_dir;
	gboolean print_dir_check;
	gboolean print_dir_check_with_content;
	gboolean print_file_check;
	gboolean print_monitor_check;
	gboolean would_index = TRUE;
	gboolean would_notice = TRUE;

	/* Set log handler for library messages */
	log_handler_id = g_log_set_handler (NULL,
	                                    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,
	                                    dummy_log_handler,
	                                    NULL);

	g_log_set_default_handler (dummy_log_handler, NULL);

	/* Start check */
	file = g_file_new_for_commandline_arg (eligible);
	info = g_file_query_info (file,
	                          G_FILE_ATTRIBUTE_STANDARD_TYPE,
	                          G_FILE_QUERY_INFO_NONE,
	                          NULL,
	                          &error);

	if (error) {
		if (error->code == G_IO_ERROR_NOT_FOUND) {
			exists = FALSE;
		}

		g_error_free (error);
	}

	if (info) {
		is_dir = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
		g_object_unref (info);
	} else {
		/* Assume not a dir */
		is_dir = FALSE;
	}

	config = tracker_config_new ();
	path = g_file_get_path (file);

	if (exists) {
		if (is_dir) {
			print_dir_check = TRUE;
			print_dir_check_with_content = TRUE;
			print_file_check = FALSE;
			print_monitor_check = TRUE;
		} else {
			print_dir_check = FALSE;
			print_dir_check_with_content = FALSE;
			print_file_check = TRUE;
			print_monitor_check = TRUE;
		}
	} else {
		print_dir_check = TRUE;
		print_dir_check_with_content = FALSE;
		print_file_check = TRUE;
		print_monitor_check = TRUE;
	}

	g_print (exists ?
	         _("Data object '%s' currently exists") :
	         _("Data object '%s' currently does not exist"),
	         path);

	g_print ("\n");

	if (print_dir_check) {
		gboolean check;

		check = tracker_miner_files_check_directory (file,
		                                             tracker_config_get_index_recursive_directories (config),
			                                     tracker_config_get_index_single_directories (config),
			                                     tracker_config_get_ignored_directory_paths (config),
			                                     tracker_config_get_ignored_directory_patterns (config));
		g_print ("  %s\n",
		         check ?
		         _("Directory is eligible to be mined (based on rules)") :
		         _("Directory is NOT eligible to be mined (based on rules)"));

		would_index &= check;
	}

	if (print_dir_check_with_content) {
		GList *children;
		gboolean check;

		children = get_dir_children_as_gfiles (path);

		check = tracker_miner_files_check_directory_contents (file,
			                                              children,
			                                              tracker_config_get_ignored_directories_with_content (config));

		g_list_foreach (children, (GFunc) g_object_unref, NULL);
		g_list_free (children);

		g_print ("  %s\n",
		         check ?
		         _("Directory is eligible to be mined (based on contents)") :
		         _("Directory is NOT eligible to be mined (based on contents)"));

		would_index &= check;
	}

	if (print_monitor_check) {
		gboolean check = TRUE;

		check &= tracker_config_get_enable_monitors (config);

		if (check) {
			GSList *dirs_to_check, *l;
			gboolean is_covered_single;
			gboolean is_covered_recursive;

			is_covered_single = FALSE;
			dirs_to_check = tracker_config_get_index_single_directories (config);

			for (l = dirs_to_check; l && !is_covered_single; l = l->next) {
				GFile *dir;
				GFile *parent;

				parent = g_file_get_parent (file);
				dir = g_file_new_for_path (l->data);
				is_covered_single = g_file_equal (parent, dir) || g_file_equal (file, dir);

				g_object_unref (dir);
				g_object_unref (parent);
			}

			is_covered_recursive = FALSE;
			dirs_to_check = tracker_config_get_index_recursive_directories (config);

			for (l = dirs_to_check; l && !is_covered_recursive; l = l->next) {
				GFile *dir;

				dir = g_file_new_for_path (l->data);
				is_covered_recursive = g_file_has_prefix (file, dir) || g_file_equal (file, dir);
				g_object_unref (dir);
			}

			check &= is_covered_single || is_covered_recursive;
		}

		if (exists && is_dir) {
			g_print ("  %s\n",
			         check ?
			         _("Directory is eligible to be monitored (based on config)") :
			         _("Directory is NOT eligible to be monitored (based on config)"));
		} else if (exists && !is_dir) {
			g_print ("  %s\n",
			         check ?
			         _("File is eligible to be monitored (based on config)") :
			         _("File is NOT eligible to be monitored (based on config)"));
		} else {
			g_print ("  %s\n",
			         check ?
			         _("File or Directory is eligible to be monitored (based on config)") :
			         _("File or Directory is NOT eligible to be monitored (based on config)"));
		}

		would_notice &= check;
	}

	if (print_file_check) {
		gboolean check;

		check = tracker_miner_files_check_file (file,
		                                        tracker_config_get_ignored_file_paths (config),
		                                        tracker_config_get_ignored_file_patterns (config));

		g_print ("  %s\n",
		         check ?
		         _("File is eligible to be mined (based on rules)") :
		         _("File is NOT eligible to be mined (based on rules)"));

		would_index &= check;
	}

	g_print ("\n"
	         "%s: %s\n"
	         "%s: %s\n"
	         "\n",
	         _("Would be indexed"),
	         would_index ? _("Yes") : _("No"),
	         _("Would be monitored"),
	         would_notice ? _("Yes") : _("No"));

	if (log_handler_id != 0) {
		/* Unset log handler */
		g_log_remove_handler (NULL, log_handler_id);
	}

	g_free (path);
	g_object_unref (config);
	g_object_unref (file);
}

static gboolean
store_is_available (void)
{
	GDBusConnection *connection;
	GDBusProxy *proxy;
	gchar *name_owner;

	connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);

	if (!connection) {
		return FALSE;
	}

	proxy = g_dbus_proxy_new_sync (connection,
	                               G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
	                               NULL,
	                               "org.freedesktop.Tracker1",
	                               "/org/freedesktop/Tracker1/Status",
	                               "org.freedesktop.Tracker1.Status",
	                               NULL, NULL);

	if (!proxy) {
		g_object_unref (connection);
		return FALSE;
	}

	name_owner = g_dbus_proxy_get_name_owner (proxy);

	g_object_unref (connection);
	g_object_unref (proxy);

	if (name_owner) {
		g_free (name_owner);
		return TRUE;
	}

	return FALSE;
}

static gboolean
miner_needs_check (TrackerMiner *miner,
                   gboolean      store_available)
{
	/* Reasons to not mark ourselves as cleanly shutdown include:
	 *
	 * 1. Still crawling or with files to process in our queues.
	 * 2. We crash (out of our control usually anyway).
	 * 3. At least one of the miners is PAUSED, we have
	 *    to exclude the situations where the miner is
	 *    exclusively paused due to the store not being
	 *    available, but the miner is actually done.
	 */
	if (!tracker_miner_is_paused (miner)) {
		if (TRACKER_IS_MINER_FS (miner) &&
		    tracker_miner_fs_has_items_to_process (TRACKER_MINER_FS (miner))) {
			/* There are items left to process */
			return TRUE;
		}

		/* FIXME: We currently don't check the applications
		 *  miner OR the userguides miner if we are finished
		 * before returning TRUE/FALSE here, should we?
		 */

		/* We consider the miner finished */
		return FALSE;
	} else {
		if (store_available) {
			/* Paused for other reasons, so probably not done */
			return TRUE;
		} else {
			/* Check whether there are more pause
			 * reasons than the store being out.
			 */
			return tracker_miner_get_n_pause_reasons (miner) > 1;
		}
	}
}

int
main (gint argc, gchar *argv[])
{
	TrackerConfig *config;
	TrackerMiner *miner_applications, *miner_files;
	TrackerMinerFilesIndex *miner_files_index;
#ifdef HAVE_MAEMO
	TrackerMiner *miner_userguides;
#endif /* HAVE_MAEMO */
	GOptionContext *context;
	GError *error = NULL;
	gchar *log_filename = NULL;
	gboolean do_mtime_checking;
	gboolean do_crawling;
	gboolean force_mtime_checking = FALSE;
	gboolean store_available;

	main_loop = NULL;

	setlocale (LC_ALL, "");

	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	/* Set timezone info */
	tzset ();

	/* Translators: this messagge will apper immediately after the
	 * usage string - Usage: COMMAND <THIS_MESSAGE>
	 */
	context = g_option_context_new (_("- start the tracker indexer"));

	disable_options = g_array_new (FALSE, FALSE, sizeof (gint));
	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_parse (context, &argc, &argv, &error);
	g_option_context_free (context);

	if (error) {
		g_printerr ("%s\n", error->message);
		g_error_free (error);
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}

	if (version) {
		g_print ("\n" ABOUT "\n" LICENSE "\n");
		g_array_free (disable_options, TRUE);
		return EXIT_SUCCESS;
	}

	if (eligible) {
		check_eligible ();
		g_array_free (disable_options, TRUE);
		return EXIT_SUCCESS;
	}

	initialize_signal_handler ();

	/* Initialize logging */
	config = tracker_config_new ();

	if (verbosity > -1) {
		tracker_config_set_verbosity (config, verbosity);
	}

	if (initial_sleep > -1) {
		tracker_config_set_initial_sleep (config, initial_sleep);
	}

	tracker_log_init (tracker_config_get_verbosity (config),
	                  &log_filename);
	if (log_filename) {
		g_message ("Using log file:'%s'", log_filename);
		g_free (log_filename);
	}

	sanity_check_option_values (config);

	/* This makes sure we don't steal all the system's resources */
	initialize_priority_and_scheduling (tracker_config_get_sched_idle (config),
	                                    tracker_db_manager_get_first_index_done () == FALSE);

	main_loop = g_main_loop_new (NULL, FALSE);

	g_message ("Checking if we're running as a daemon:");
	g_message ("  %s %s",
	           no_daemon ? "No" : "Yes",
	           no_daemon ? "(forced by command line)" : "");

	/* Create new TrackerMinerFiles object */
	miner_files = tracker_miner_files_new (config, &error);
	if (!miner_files) {
		g_critical ("Couldn't create new Files miner: '%s'",
		            error ? error->message : "unknown error");
		g_object_unref (config);
		tracker_log_shutdown ();
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}

	tracker_writeback_init (TRACKER_MINER_FILES (miner_files),
	                        config,
	                        &error);

	if (error) {
		g_critical ("Couldn't create writeback handling: '%s'",
		            error ? error->message : "unknown error");
		g_object_unref (config);
		g_object_unref (miner_files);
		tracker_log_shutdown ();
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}

	/* Create miner for applications */
	miner_applications = tracker_miner_applications_new (&error);
	if (!miner_applications) {
		g_critical ("Couldn't create new Applications miner: '%s'",
		            error ? error->message : "unknown error");
		g_object_unref (miner_files);
		tracker_writeback_shutdown ();
		g_object_unref (config);
		tracker_log_shutdown ();
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}

	/* Create new TrackerMinerFilesIndex object */
	miner_files_index = tracker_miner_files_index_new (TRACKER_MINER_FILES (miner_files));
	if (!miner_files_index) {
		g_object_unref (miner_applications);
		g_object_unref (miner_files);
		tracker_writeback_shutdown ();
		g_object_unref (config);
		tracker_log_shutdown ();
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}

#ifdef HAVE_MAEMO
	/* Create miner for userguides */
	miner_userguides = tracker_miner_userguides_new (&error);
	if (!miner_userguides) {
		g_critical ("Couldn't create new User Guides miner: '%s'",
		            error ? error->message : "unknown error");
		g_object_unref (miner_applications);
		g_object_unref (miner_files);
		g_object_unref (miner_files_index);
		tracker_writeback_shutdown ();
		g_object_unref (config);
		tracker_log_shutdown ();
		g_array_free (disable_options, TRUE);
		return EXIT_FAILURE;
	}
#endif /* HAVE_MAEMO */

	/* Check if we should crawl and if we should force mtime
	 * checking based on the config.
	 */
	do_crawling = should_crawl (config, &force_mtime_checking);

	/* Get the last shutdown state to see if we need to perform a
	 * full mtime check against the db or not.
	 *
	 * Set to TRUE here in case we crash and miss file system
	 * events.
	 */
	g_message ("Checking whether to force mtime checking during crawling (based on last clean shutdown):");

	/* Override the shutdown state decision based on the config */
	if (force_mtime_checking) {
		do_mtime_checking = TRUE;
	} else {
		do_mtime_checking = tracker_db_manager_get_need_mtime_check ();
	}

	g_message ("  %s %s",
	           do_mtime_checking ? "Yes" : "No",
	           force_mtime_checking ? "(forced from config)" : "");

	/* Set the need for an mtime check to TRUE so we check in the
	 * event of a crash, this is changed back on shutdown if
	 * everything appears to be fine.
	 */
	tracker_db_manager_set_need_mtime_check (TRUE);

	/* Configure files miner */
	tracker_miner_fs_set_initial_crawling (TRACKER_MINER_FS (miner_files), do_crawling);
	tracker_miner_fs_set_mtime_checking (TRACKER_MINER_FS (miner_files), do_mtime_checking);
	g_signal_connect (miner_files, "finished",
			  G_CALLBACK (miner_finished_cb),
			  NULL);

	/* Configure applications miner */
	tracker_miner_fs_set_initial_crawling (TRACKER_MINER_FS (miner_applications), do_crawling);

#ifdef HAVE_MAEMO
	/* Configure userguides miner */
	tracker_miner_fs_set_initial_crawling (TRACKER_MINER_FS (miner_userguides), do_crawling);
#endif /* HAVE_MAEMO */

	/* If a locale change was detected, always do mtime checks */
	if (tracker_miner_applications_detect_locale_changed (miner_applications)) {
		if (!do_mtime_checking)
			g_debug ("Forcing mtime check in applications miner as locale change was detected");
		tracker_miner_fs_set_mtime_checking (TRACKER_MINER_FS (miner_applications), TRUE);
	} else {
		tracker_miner_fs_set_mtime_checking (TRACKER_MINER_FS (miner_applications), do_mtime_checking);
	}


#ifdef HAVE_MAEMO
	/* If a locale change was detected, always do mtime checks */
	if (tracker_miner_userguides_detect_locale_changed (miner_userguides)) {
		if (!do_mtime_checking)
			g_debug ("Forcing mtime check in userguides miner as locale change was detected");
		tracker_miner_fs_set_mtime_checking (TRACKER_MINER_FS (miner_userguides), TRUE);
	} else {
		tracker_miner_fs_set_mtime_checking (TRACKER_MINER_FS (miner_userguides), do_mtime_checking);
	}
#endif /* HAVE_MAEMO */

	g_signal_connect (miner_applications, "finished",
	                  G_CALLBACK (miner_finished_cb),
	                  NULL);

#ifdef HAVE_MAEMO
	g_signal_connect (miner_userguides, "finished",
			  G_CALLBACK (miner_finished_cb),
			  NULL);
#endif /* HAVE_MAEMO */

	/* Setup miners, applications first in list */
#ifdef HAVE_MAEMO
	miners = g_slist_prepend (miners, miner_userguides);
#endif /* HAVE_MAEMO */

	miners = g_slist_prepend (miners, miner_files);
	miners = g_slist_prepend (miners, miner_applications);

	tracker_thumbnailer_init ();

	miner_handle_first (config, do_mtime_checking);

	/* Go, go, go! */
	g_main_loop_run (main_loop);

	g_message ("Shutdown started");

	store_available = store_is_available ();

	if (miners_timeout_id == 0 &&
	    !miner_needs_check (miner_files, store_available) &&
#ifdef HAVE_MAEMO
	    !miner_needs_check (miner_userguides, store_available) &&
#endif
	    !miner_needs_check (miner_applications, store_available)) {
		tracker_db_manager_set_need_mtime_check (FALSE);
	}

	g_main_loop_unref (main_loop);
	g_object_unref (config);
	g_object_unref (miner_files_index);

	tracker_thumbnailer_shutdown ();

	g_slist_foreach (miners, (GFunc) finalize_miner, NULL);
	g_slist_free (miners);

	tracker_writeback_shutdown ();
	tracker_log_shutdown ();

	g_array_free (disable_options, TRUE);

	g_print ("\nOK\n\n");

	return EXIT_SUCCESS;
}