LCOV - code coverage report
Current view: top level - src - db_primary_file_validate_existence.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.3 % 24 20
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 68.8 % 16 11

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Validates the existence of the primary database file
       5                 :             :  * @details Checks if the primary database file exists and is accessible. Updates the
       6                 :             :  *    global config->db_primary_file_exists flag based on the check result.
       7                 :             :  *    When the file exists, its metadata is stored in config->db_file_stat in a
       8                 :             :  *    single stat() call to avoid a TOCTOU race between existence check and metadata
       9                 :             :  *    retrieval. The function attempts to access the file using the path stored
      10                 :             :  *    in the global configuration config->db_primary_file_path
      11                 :             :  *
      12                 :             :  * @return Return status code indicating the operation result:
      13                 :             :  *    - SUCCESS: Check completed successfully
      14                 :             :  *    - FAILURE: File check operation failed
      15                 :             :  *
      16                 :             :  * @note This function only verifies file existence and basic accessibility.
      17                 :             :  *    It does not validate file format or content integrity.
      18                 :             :  *
      19                 :             :  * @see config->db_primary_file_exists
      20                 :             :  * @see config->db_primary_file_path
      21                 :             :  * @see config->db_file_stat
      22                 :             :  */
      23                 :         498 : Return db_primary_file_validate_existence(void)
      24                 :             : {
      25                 :             :         /* Status returned by this function through provide()
      26                 :             :            Default value assumes successful completion */
      27                 :         498 :         Return status = SUCCESS;
      28                 :             : 
      29                 :             :         /* Interrupt the function smoothly */
      30                 :             :         /* Interrupt when Ctrl+C */
      31         [ -  + ]:         498 :         if(global_interrupt_flag == true)
      32                 :             :         {
      33                 :           0 :                 provide(status);
      34                 :             :         }
      35                 :             : 
      36                 :             :         // Primary DB file exists or not
      37                 :         498 :         config->db_primary_file_exists = false;
      38                 :             : 
      39                 :         498 :         const char *db_primary_file_path = confstr(db_primary_file_path);
      40                 :             : 
      41                 :             :         size_t db_primary_file_path_length;
      42                 :             : 
      43                 :         498 :         status = m_string_length(conf(db_primary_file_path),&db_primary_file_path_length);
      44                 :             : 
      45                 :             :         // Path is initialized in db_determine_name() and must contain visible content
      46   [ +  -  -  + ]:         498 :         if(SUCCESS & status && db_primary_file_path_length == 0)
      47                 :             :         {
      48                 :           0 :                 status = FAILURE;
      49                 :             :         }
      50                 :             : 
      51         [ +  - ]:         498 :         if(SUCCESS == status)
      52                 :             :         {
      53                 :         498 :                 char *db_file_full_path = strdup(db_primary_file_path);
      54                 :             : 
      55         [ -  + ]:         498 :                 if(db_file_full_path == NULL)
      56                 :             :                 {
      57                 :           0 :                         report("Memory allocation failed for database path copy");
      58                 :           0 :                         provide(FAILURE);
      59                 :             :                 }
      60                 :             : 
      61                 :         498 :                 char *db_file_dir = dirname(db_file_full_path);
      62                 :             : 
      63         [ +  + ]:         498 :                 if(NOT_FOUND == file_availability(db_file_dir,NULL,SHOULD_BE_A_DIRECTORY))
      64                 :             :                 {
      65                 :           2 :                         slog(ERROR,"Unable to create database file. Directory %s not found\n",db_file_dir);
      66                 :           2 :                         status = FAILURE;
      67                 :             :                 }
      68                 :             : 
      69                 :         498 :                 free(db_file_full_path);
      70                 :             : 
      71         [ +  + ]:         498 :                 if(SUCCESS == status)
      72                 :             :                 {
      73         [ +  + ]:         496 :                         if(EXISTS == file_availability(db_primary_file_path,&config->db_file_stat,SHOULD_BE_A_FILE))
      74                 :             :                         {
      75                 :         167 :                                 config->db_primary_file_exists = true;
      76                 :             :                         } else {
      77                 :         329 :                                 config->db_primary_file_exists = false;
      78                 :             :                         }
      79                 :             :                 }
      80                 :             :         }
      81                 :             : 
      82                 :         498 :         provide(status);
      83                 :             : }
        

Generated by: LCOV version 2.0-1