LCOV - code coverage report
Current view: top level - src - init_config.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 54 54
Test Date: 2026-10-20 14:27:43 Functions: 100.0 % 1 1
Branches: 87.5 % 8 7

             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                 :             :  * Global logger and color settings are initialized from their defaults and
      11                 :             :  * environment overrides before the shared `Config` object is cleared.
      12                 :             :  * Command-line options can override these settings during argument parsing
      13                 :             :  *
      14                 :             :  * The function clears the shared `Config` object and then assigns explicit
      15                 :             :  * runtime defaults, including the file transaction state. Path-like data owned
      16                 :             :  * by libmem is prepared as string descriptors or descriptor arrays: `db_primary_file_path`,
      17                 :             :  * `db_file_name`, `roots`, and `db_file_paths`
      18                 :             :  *
      19                 :             :  * Normal scan roots are collected later in `config->roots`, while `--compare`
      20                 :             :  * database arguments are collected in `config->db_file_paths`
      21                 :             :  */
      22                 :        1252 : void init_config(void)
      23                 :             : {
      24                 :             :         /* This function was reviewed line by line by a human and is not AI-generated
      25                 :             :            Any change to this function requires separate explicit approval */
      26                 :             : 
      27                 :             :         /* Initialize process-wide output settings before the Config fields.
      28                 :             :            These settings do not depend on Config and apply to initialization
      29                 :             :            diagnostics as well as later application output */
      30                 :             : 
      31                 :             : #ifdef TESTITALL_TEST_HOOKS
      32                 :             :         // Define the comparison string
      33                 :        1252 :         const char *compare_string = "true";
      34                 :             : 
      35                 :             :         // Read the TESTING environment variable for the initial logger mode
      36                 :        1252 :         const char *env_var = getenv("TESTING");
      37                 :             : 
      38                 :             :         // Enable test diagnostics only when the value equals "true", ignoring case
      39                 :             :         /* rational_logger_mode controls which categories of log messages are shown.
      40                 :             :            TESTING=true selects diagnostic output for test comparisons; otherwise,
      41                 :             :            REGULAR selects normal user-facing output. The command-line options
      42                 :             :            --silent and --verbose can override this initial mode */
      43   [ +  +  +  + ]:        1252 :         if(env_var != NULL && strcasecmp(env_var,compare_string) == 0)
      44                 :             :         {
      45                 :             :                 // Use predictable diagnostic output for test comparisons
      46                 :        1099 :                 rational_logger_mode = TESTING;
      47                 :             :         } else {
      48                 :             :                 // Use normal application output when test diagnostics are not requested
      49                 :         153 :                 rational_logger_mode = REGULAR;
      50                 :             :         }
      51                 :             : #else
      52                 :             :         rational_logger_mode = REGULAR;
      53                 :             : #endif
      54                 :             : 
      55                 :             :         /* rational_color_mode controls whether terminal styling may be emitted.
      56                 :             :            AUTO lets the output policy inspect each stream, NO_COLOR, and TERM when
      57                 :             :            a message is written. Resetting the mode prevents a previous in-process
      58                 :             :            run from leaving its color setting active. An explicit --color option
      59                 :             :            parsed later takes priority over this default and the test override */
      60                 :        1252 :         rational_color_mode = COLOR_MODE_AUTO;
      61                 :             : 
      62                 :             :         /* Test runners use ANSI-decorated golden output by default.
      63                 :             :            The test-only environment override applies to internal calls and is
      64                 :             :            inherited by external runit and runit_background processes. Only builds
      65                 :             :            with TESTITALL_TEST_HOOKS honor this override */
      66                 :             : #ifdef TESTITALL_TEST_HOOKS
      67                 :        1252 :         const char *test_color_mode = getenv("TESTITALL_TEST_ENV_COLOR_MODE");
      68                 :             : 
      69   [ +  +  +  - ]:        1252 :         if(test_color_mode != NULL && 0 == strcmp(test_color_mode,"always"))
      70                 :             :         {
      71                 :        1249 :                 rational_color_mode = COLOR_MODE_ALWAYS;
      72                 :             :         }
      73                 :             : #endif
      74                 :             : 
      75                 :             :         // Fill out with zeroes
      76                 :        1252 :         memset(config,0,sizeof(Config));
      77                 :             : 
      78                 :             :         // Application start time for total runtime reporting.
      79                 :        1252 :         config->app_start_time_ns = cur_time_monotonic_ns();
      80                 :             : 
      81                 :             :         // Show progress bar
      82                 :        1252 :         config->progress = false;
      83                 :             : 
      84                 :             :         // Print remembered warnings and errors before exit
      85                 :        1252 :         config->show_remembered_messages_at_exit = false;
      86                 :             : 
      87                 :             :         // Force update of the database
      88                 :        1252 :         config->force = false;
      89                 :             : 
      90                 :             :         // Additional output for debugging
      91                 :        1252 :         config->verbose = false;
      92                 :             : 
      93                 :             :         // Force update of the database with new,
      94                 :             :         // changed or deleted files. This is special
      95                 :             :         // protection against accidental deletion of
      96                 :             :         // information from the database.
      97                 :        1252 :         config->update = false;
      98                 :             : 
      99                 :             :         // Parameter to compare database
     100                 :        1252 :         config->compare = false;
     101                 :             : 
     102                 :             :         // Show all compare output categories by default
     103                 :        1252 :         config->compare_filter = CF_NONE_SPECIFIED;
     104                 :             : 
     105                 :             :         // No configuration file selected
     106                 :        1252 :         config->config_file_path = NULL;
     107                 :             : 
     108                 :             :         // No configuration entry is being processed for option error reporting
     109                 :        1252 :         config->config_file_error_line = -1;
     110                 :             : 
     111                 :             :         // Managed array of root paths to traverse
     112                 :        1252 :         config->roots = m_init(memory);
     113                 :             : 
     114                 :             :         // The pointer to the primary database
     115                 :        1252 :         config->db = NULL;
     116                 :             : 
     117                 :             :         /// The flags parameter to sqlite3_open_v2()
     118                 :             :         /// must include, at a minimum, one of the
     119                 :             :         /// following flag combinations:
     120                 :             :         ///   - SQLITE_OPEN_READONLY
     121                 :             :         ///   - SQLITE_OPEN_READWRITE
     122                 :             :         ///   - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
     123                 :             :         /// Default value: RO
     124                 :        1252 :         config->sqlite_open_flag = SQLITE_OPEN_READONLY;
     125                 :             : 
     126                 :             :         // The path of DB file
     127                 :        1252 :         config->db_primary_file_path = m_init(char,MEMORY_STRING);
     128                 :             : 
     129                 :             :         // Set true only when the primary database path is ":memory:"
     130                 :        1252 :         config->db_primary_path_is_memory = false;
     131                 :             : 
     132                 :             :         // The name of DB file
     133                 :        1252 :         config->db_file_name = m_init(char,MEMORY_STRING);
     134                 :             : 
     135                 :             :         // Managed array of database file paths
     136                 :        1252 :         config->db_file_paths = m_init(memory);
     137                 :             : 
     138                 :             :         // Pointers to the array with database file names
     139                 :        1252 :         config->db_file_names = NULL;
     140                 :             : 
     141                 :             :         /// Allow or disallow database table
     142                 :             :         /// initialization. True by default
     143                 :        1252 :         config->db_initialize_tables = true;
     144                 :             : 
     145                 :             :         /// Flag indicating whether the primary database file exists
     146                 :        1252 :         config->db_primary_file_exists = false;
     147                 :             : 
     148                 :             :         // The flag means that the DB already exists
     149                 :             :         // and not empty
     150                 :        1252 :         config->db_contains_data = false;
     151                 :             : 
     152                 :             :         // Must be specified additionally in order
     153                 :             :         // to remove from the database mention of
     154                 :             :         // files that matches the regular expression
     155                 :             :         // passed through the ignore option(s)
     156                 :             :         // This is special protection against accidental
     157                 :             :         // deletion of information from the database.
     158                 :        1252 :         config->db_drop_ignored = false;
     159                 :             : 
     160                 :             :         // Allow dropping database records for inaccessible files
     161                 :             :         // (permission denied). Disabled by default.
     162                 :        1252 :         config->db_drop_inaccessible = false;
     163                 :             : 
     164                 :             :         /// Select database validation level: 'quick' for basic
     165                 :             :         /// structure check, 'full' (default) for comprehensive
     166                 :             :         /// integrity verification
     167                 :        1252 :         config->db_check_level = FULL;
     168                 :             : 
     169                 :             :         // Flag that reflects the presence of any changes
     170                 :             :         // since the last research
     171                 :        1252 :         config->db_primary_file_modified = false;
     172                 :             : 
     173                 :             :         /* Track the file transaction owned by the main traversal.
     174                 :             :            Starts false, is set after BEGIN IMMEDIATE succeeds, and is cleared after
     175                 :             :            a successful COMMIT or completed rollback cleanup. SQLite can roll back
     176                 :             :            automatically while this flag remains set, so transaction helpers also
     177                 :             :            check SQLite's actual transaction state */
     178                 :        1252 :         config->file_transaction_active = false;
     179                 :             : 
     180                 :             :         /* Monotonic start time of the current file transaction, in nanoseconds.
     181                 :             :            Starts at zero and is replaced with the current time after each successful
     182                 :             :            BEGIN. Later saves in the same batch leave it unchanged, so the shared
     183                 :             :            DB_CHECKPOINT_INTERVAL_NS applies to the whole batch. The value is used
     184                 :             :            only while file_transaction_active is true */
     185                 :        1252 :         config->file_transaction_started_ns = 0LL;
     186                 :             : 
     187                 :             :         // Recursion depth limit. The depth of the traversal,
     188                 :             :         // numbered from 0 to N, where a file could be found.
     189                 :             :         // Representing the maximum of the starting
     190                 :             :         // point (from root) of the traversal.
     191                 :             :         // The root itself is numbered 0
     192                 :        1252 :         config->maxdepth = -1;
     193                 :             : 
     194                 :             :         // Ignore those relative paths
     195                 :             :         // The string array of PCRE2 regular expressions
     196                 :        1252 :         config->ignore = NULL;
     197                 :             : 
     198                 :             :         // Suppress per-file log output for paths matched by --ignore
     199                 :        1252 :         config->quiet_ignored = false;
     200                 :             : 
     201                 :             :         // Include those relative paths even if
     202                 :             :         // they were excluded via the --ignore option
     203                 :             :         // The string array of PCRE2 regular expressions
     204                 :        1252 :         config->include = NULL;
     205                 :        1252 :         config->include_specified = false;
     206                 :             : 
     207                 :             :         // Relative paths whose checksums must never be recalculated
     208                 :             :         // after the initial write. PCRE2 regular expressions.
     209                 :        1252 :         config->lock_checksum = NULL;
     210                 :             : 
     211                 :             :         // Force a full rehash for checksum-locked entries
     212                 :        1252 :         config->rehash_locked = false;
     213                 :             : 
     214                 :             :         // Perform a trial run with no changes made
     215                 :        1252 :         config->dry_run = false;
     216                 :             : 
     217                 :             :         // Allow hashing in dry-run mode (--dry-run=with-checksums)
     218                 :        1252 :         config->dry_run_with_checksums = false;
     219                 :             : 
     220                 :             :         /// This option prevents directory traversal from descending into
     221                 :             :         /// directories that have a different device number than the file
     222                 :             :         /// from  which the descent began
     223                 :        1252 :         config->one_file_system = false;
     224                 :             : 
     225                 :             :         // Pre-compiled PCRE2 patterns for --ignore, populated by compile_patterns() only if --ignore was specified
     226                 :        1252 :         config->ignore_pcre_compiled = NULL;
     227                 :             : 
     228                 :             :         // Pre-compiled PCRE2 patterns for --include, populated by compile_patterns() only if --include was specified
     229                 :        1252 :         config->include_pcre_compiled = NULL;
     230                 :             : 
     231                 :             :         // Pre-compiled PCRE2 patterns for --lock-checksum, populated by compile_patterns() only if --lock-checksum was specified
     232                 :        1252 :         config->lock_checksum_pcre_compiled = NULL;
     233                 :             : 
     234                 :             :         /// Rehash regular files when ctime or allocated block count changes.
     235                 :             :         /// Size and mtime changes trigger rehashing by default
     236                 :        1252 :         config->watch_timestamps = false;
     237                 :             : 
     238                 :             : #if 0
     239                 :             :         if(NULL != setlocale(LC_ALL,""))
     240                 :             :         {
     241                 :             :                 slog(TRACE,"Enable locale support\n");
     242                 :             :         } else {
     243                 :             :                 slog(TRACE,"Failed to set locale\n");
     244                 :             :         }
     245                 :             : #endif
     246                 :             : 
     247                 :        1252 :         slog(TRACE,"Configuration initialization is finished\n");
     248                 :        1252 : }
        

Generated by: LCOV version 2.0-1