Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : *
5 : : * Remove information from the database about files that had been deleted
6 : : * on the file system or have been ignored
7 : : *
8 : : */
9 : 361 : Return db_delete_missing_metadata(void)
10 : : {
11 : : /* Status returned by this function through provide()
12 : : Default value assumes successful completion */
13 : 361 : Return status = SUCCESS;
14 : :
15 : : /* Interrupt the function smoothly */
16 : : /* Interrupt when Ctrl+C */
17 [ - + ]: 361 : if(global_interrupt_flag == true)
18 : : {
19 : 0 : provide(status);
20 : : }
21 : :
22 : : /* Skip in comparison mode */
23 [ + + ]: 361 : if(config->compare == true)
24 : : {
25 : 112 : slog(TRACE,"Comparison mode is enabled. The primary database does not require cleanup\n");
26 : 112 : provide(status);
27 : : }
28 : :
29 : : /* Update mode should be enabled */
30 [ + + ]: 249 : if(config->update == true)
31 : : {
32 : 94 : slog(EVERY,"Searching for files that no longer exist on the file system…\n");
33 : :
34 : : } else {
35 : : // Don't do anything
36 : 155 : provide(status);
37 : : }
38 : :
39 [ + + + - ]: 94 : if(config->dry_run == true && config->db_primary_file_exists == true)
40 : : {
41 : 10 : slog(TRACE,"Dry Run mode is enabled. The primary database must not be modified\n");
42 : : }
43 : :
44 : : // Print deletion banners only once per run
45 : 94 : bool first_iteration = true;
46 : :
47 : 94 : sqlite3_stmt *select_stmt = NULL;
48 : :
49 : 94 : int rc = 0;
50 : :
51 : : #if 0 // Old multiPATH solutions
52 : : const char *select_sql = "SELECT files.ID,paths.prefix,files.relative_path FROM files LEFT JOIN paths ON files.path_prefix_index = paths.ID;";
53 : : #endif
54 : 94 : const char *select_sql = "SELECT files.ID,paths.prefix,files.relative_path FROM files,paths;";
55 : :
56 : 94 : rc = sqlite3_prepare_v2(config->db,select_sql,-1,&select_stmt,NULL);
57 : :
58 [ - + ]: 94 : if(SQLITE_OK != rc)
59 : : {
60 : 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare select statement");
61 : 0 : status = FAILURE;
62 : : }
63 : :
64 [ + + ]: 1286 : while(SQLITE_ROW == (rc = sqlite3_step(select_stmt)))
65 : : {
66 : : /* Interrupt the loop smoothly */
67 : : /* Interrupt when Ctrl+C */
68 [ - + ]: 1192 : if(global_interrupt_flag == true)
69 : : {
70 : 0 : break;
71 : : }
72 : :
73 : 1192 : sqlite_int64 ID = sqlite3_column_int64(select_stmt,0);
74 : 1192 : const char *runtime_path_prefix = (const char *)sqlite3_column_text(select_stmt,1);
75 : 1192 : const char *relative_path = (const char *)sqlite3_column_text(select_stmt,2);
76 : :
77 : : // Marks deletions triggered by --db-drop-ignored
78 : 1192 : bool drop_ignored = false;
79 : :
80 [ + - + - ]: 1192 : if(runtime_path_prefix != NULL && relative_path != NULL)
81 : : {
82 : : /*
83 : : * Remove from the database mention of
84 : : * files that matches the regular expression
85 : : * passed through the ignore option(s)
86 : : *
87 : : */
88 [ + + ]: 1192 : if(config->db_drop_ignored == true)
89 : : {
90 : : /*
91 : : *
92 : : * PCRE2 regexp to include the file
93 : : *
94 : : */
95 : :
96 : 196 : Include match_include_response = match_include_pattern(relative_path);
97 : :
98 [ + + ]: 196 : if(DO_NOT_INCLUDE == match_include_response)
99 : : {
100 : : /*
101 : : *
102 : : * PCRE2 regexp to ignore the file
103 : : *
104 : : */
105 : :
106 : 188 : Ignore match_ignore_response = match_ignore_pattern(relative_path);
107 : :
108 [ + + ]: 188 : if(IGNORE == match_ignore_response)
109 : : {
110 : 46 : drop_ignored = true;
111 : :
112 [ - + ]: 142 : } else if(FAIL_REGEXP_IGNORE == match_ignore_response){
113 : 0 : status = FAILURE;
114 : : }
115 : :
116 [ - + ]: 8 : } else if(FAIL_REGEXP_INCLUDE == match_include_response){
117 : 0 : status = FAILURE;
118 : 0 : break;
119 : : }
120 : : }
121 : : }
122 : :
123 : : // Decide on deletion; access check and messaging handled inside db_delete_the_record_by_id
124 [ + + + - ]: 1192 : if(drop_ignored == true || relative_path != NULL)
125 : : {
126 : 1192 : status = db_delete_the_record_by_id(&ID,
127 : : &first_iteration,
128 : : &drop_ignored,
129 : : relative_path,
130 : : runtime_path_prefix);
131 : :
132 [ - + ]: 1192 : if(SUCCESS != status)
133 : : {
134 : 0 : break;
135 : : }
136 : : }
137 : : }
138 : :
139 [ - + ]: 94 : if(SQLITE_DONE != rc)
140 : : {
141 [ # # ]: 0 : if(global_interrupt_flag == false)
142 : : {
143 : 0 : log_sqlite_error(config->db,rc,NULL,"Select statement didn't finish with DONE");
144 : 0 : status = FAILURE;
145 : : }
146 : : }
147 : :
148 : 94 : sqlite3_finalize(select_stmt);
149 : :
150 : 94 : slog(EVERY,"Missing file search completed\n");
151 : :
152 : 94 : provide(status);
153 : : }
|