Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : #if 0
4 : : #include <locale.h>
5 : : #endif
6 : :
7 : : /**
8 : : * @brief Initialize global runtime configuration with explicit defaults
9 : : *
10 : : * The function clears the shared `Config` object and then initializes every
11 : : * field that needs a non-zero default. Path-like data owned by libmem is
12 : : * prepared as string descriptors or descriptor arrays: `db_primary_file_path`,
13 : : * `db_file_name`, `roots`, and `db_file_paths`
14 : : *
15 : : * Normal scan roots are collected later in `config->roots`, while `--compare`
16 : : * database arguments are collected in `config->db_file_paths`
17 : : */
18 : 557 : void init_config(void)
19 : : {
20 : : /* This function was reviewed line by line by a human and is not AI-generated
21 : : Any change to this function requires separate explicit approval */
22 : :
23 : : // Fill out with zeroes
24 : 557 : memset(config,0,sizeof(Config));
25 : :
26 : : // Application start time for total runtime reporting.
27 : 557 : config->app_start_time_ns = cur_time_monotonic_ns();
28 : :
29 : : // Show progress bar
30 : 557 : config->progress = false;
31 : :
32 : : // Print remembered warnings and errors before exit
33 : 557 : config->show_remembered_messages_at_exit = false;
34 : :
35 : : // Force update of the database
36 : 557 : config->force = false;
37 : :
38 : : // Additional output for debugging
39 : 557 : config->verbose = false;
40 : :
41 : : // Force update of the database with new,
42 : : // changed or deleted files. This is special
43 : : // protection against accidental deletion of
44 : : // information from the database.
45 : 557 : config->update = false;
46 : :
47 : : // Parameter to compare database
48 : 557 : config->compare = false;
49 : :
50 : : // Show all compare output categories by default
51 : 557 : config->compare_filter = CF_NONE_SPECIFIED;
52 : :
53 : : // Managed array of root paths to traverse
54 : 557 : config->roots = m_init(memory);
55 : :
56 : : // The pointer to the primary database
57 : 557 : config->db = NULL;
58 : :
59 : : /// The flags parameter to sqlite3_open_v2()
60 : : /// must include, at a minimum, one of the
61 : : /// following flag combinations:
62 : : /// - SQLITE_OPEN_READONLY
63 : : /// - SQLITE_OPEN_READWRITE
64 : : /// - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
65 : : /// Default value: RO
66 : 557 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
67 : :
68 : : // The path of DB file
69 : 557 : config->db_primary_file_path = m_init(char,MEMORY_STRING);
70 : :
71 : : // Set true only when the primary database path is ":memory:"
72 : 557 : config->db_primary_path_is_memory = false;
73 : :
74 : : // The name of DB file
75 : 557 : config->db_file_name = m_init(char,MEMORY_STRING);
76 : :
77 : : // Managed array of database file paths
78 : 557 : config->db_file_paths = m_init(memory);
79 : :
80 : : // Pointers to the array with database file names
81 : 557 : config->db_file_names = NULL;
82 : :
83 : : /// Allow or disallow database table
84 : : /// initialization. True by default
85 : 557 : config->db_initialize_tables = true;
86 : :
87 : : /// Flag indicating whether the primary database file exists
88 : 557 : config->db_primary_file_exists = false;
89 : :
90 : : // The flag means that the DB already exists
91 : : // and not empty
92 : 557 : config->db_contains_data = false;
93 : :
94 : : // Must be specified additionally in order
95 : : // to remove from the database mention of
96 : : // files that matches the regular expression
97 : : // passed through the ignore option(s)
98 : : // This is special protection against accidental
99 : : // deletion of information from the database.
100 : 557 : config->db_drop_ignored = false;
101 : :
102 : : // Allow dropping database records for inaccessible files
103 : : // (permission denied). Disabled by default.
104 : 557 : config->db_drop_inaccessible = false;
105 : :
106 : : /// Select database validation level: 'quick' for basic
107 : : /// structure check, 'full' (default) for comprehensive
108 : : /// integrity verification
109 : 557 : config->db_check_level = FULL;
110 : :
111 : : // Flag that reflects the presence of any changes
112 : : // since the last research
113 : 557 : config->db_primary_file_modified = false;
114 : :
115 : : // Recursion depth limit. The depth of the traversal,
116 : : // numbered from 0 to N, where a file could be found.
117 : : // Representing the maximum of the starting
118 : : // point (from root) of the traversal.
119 : : // The root itself is numbered 0
120 : 557 : config->maxdepth = -1;
121 : :
122 : : // Ignore those relative paths
123 : : // The string array of PCRE2 regular expressions
124 : 557 : config->ignore = NULL;
125 : :
126 : : // Suppress per-file log output for paths matched by --ignore
127 : 557 : config->quiet_ignored = false;
128 : :
129 : : // Include those relative paths even if
130 : : // they were excluded via the --ignore option
131 : : // The string array of PCRE2 regular expressions
132 : 557 : config->include = NULL;
133 : 557 : config->include_specified = false;
134 : :
135 : : // Relative paths whose checksums must never be recalculated
136 : : // after the initial write. PCRE2 regular expressions.
137 : 557 : config->lock_checksum = NULL;
138 : :
139 : : // Force a full rehash for checksum-locked entries
140 : 557 : config->rehash_locked = false;
141 : :
142 : : // Perform a trial run with no changes made
143 : 557 : config->dry_run = false;
144 : :
145 : : // Allow hashing in dry-run mode (--dry-run=with-checksums)
146 : 557 : config->dry_run_with_checksums = false;
147 : :
148 : : // Define the comparison string
149 : 557 : const char *compare_string = "true";
150 : :
151 : : // Retrieve the value of the "TESTING" environment variable,
152 : : // Validate if the environment variable TESTING exists
153 : : // and if it match to "true" display ONLY testing
154 : : // messages for System Testing purposes.
155 : 557 : const char *env_var = getenv("TESTING");
156 : :
157 : : // Check if it exists and compare it to "true"
158 [ + + + + ]: 557 : if(env_var != NULL && strcasecmp(env_var,compare_string) == 0)
159 : : {
160 : : // Global variable
161 : 436 : rational_logger_mode = TESTING;
162 : : } else {
163 : : // Global variable, default value
164 : 121 : rational_logger_mode = REGULAR;
165 : : }
166 : :
167 : : /// This option prevents directory traversal from descending into
168 : : /// directories that have a different device number than the file
169 : : /// from which the descent began
170 : 557 : config->start_device_only = false;
171 : :
172 : : // Pre-compiled PCRE2 patterns for --ignore, populated by compile_patterns() only if --ignore was specified
173 : 557 : config->ignore_pcre_compiled = NULL;
174 : :
175 : : // Pre-compiled PCRE2 patterns for --include, populated by compile_patterns() only if --include was specified
176 : 557 : config->include_pcre_compiled = NULL;
177 : :
178 : : // Pre-compiled PCRE2 patterns for --lock-checksum, populated by compile_patterns() only if --lock-checksum was specified
179 : 557 : config->lock_checksum_pcre_compiled = NULL;
180 : :
181 : : /// Track both file metadata (created/modified dates) and size changes
182 : : /// for change detection. Out of the box, only size changes trigger
183 : : /// a rescan. When enabled, any update to timestamps or file size
184 : : /// will force a rescan and update the checksum in the database.
185 : 557 : config->watch_timestamps = false;
186 : :
187 : : #if 0
188 : : if(NULL != setlocale(LC_ALL,""))
189 : : {
190 : : slog(TRACE,"Enable locale support\n");
191 : : } else {
192 : : slog(TRACE,"Failed to set locale\n");
193 : : }
194 : : #endif
195 : :
196 : 557 : slog(TRACE,"Configuration initialization is finished\n");
197 : 557 : }
|