LCOV - code coverage report
Current view: top level - src - db_primary_file_validate_existence.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.9 % 29 22
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 66.7 % 18 12

             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                 :             :  *    The function attempts to access the file using the path stored
       8                 :             :  *    in the global configuration config->db_primary_file_path
       9                 :             :  *
      10                 :             :  * @return Return status code indicating the operation result:
      11                 :             :  *    - SUCCESS: Check completed successfully
      12                 :             :  *    - FAILURE: File check operation failed
      13                 :             :  *
      14                 :             :  * @note This function only verifies file existence and basic accessibility.
      15                 :             :  *    It does not validate file format or content integrity.
      16                 :             :  *
      17                 :             :  * @see config->db_primary_file_exists
      18                 :             :  * @see config->db_primary_file_path
      19                 :             :  */
      20                 :         325 : Return db_primary_file_validate_existence(void)
      21                 :             : {
      22                 :             :         /* Status returned by this function through provide()
      23                 :             :            Default value assumes successful completion */
      24                 :         325 :         Return status = SUCCESS;
      25                 :             : 
      26                 :             :         /* Interrupt the function smoothly */
      27                 :             :         /* Interrupt when Ctrl+C */
      28         [ -  + ]:         325 :         if(global_interrupt_flag == true)
      29                 :             :         {
      30                 :           0 :                 provide(status);
      31                 :             :         }
      32                 :             : 
      33                 :             :         // Primary DB file exists or not
      34                 :         325 :         config->db_primary_file_exists = false;
      35                 :         325 :         const char *db_primary_file_path = confstr(db_primary_file_path);
      36                 :             : 
      37                 :             :         // Path is initialized in db_determine_name() and must contain at least one
      38                 :             :         // real character beyond the trailing '\0' terminator
      39         [ -  + ]:         325 :         if(conf(db_primary_file_path)->length <= 1)
      40                 :             :         {
      41                 :           0 :                 status = FAILURE;
      42                 :             :         }
      43                 :             : 
      44         [ +  - ]:         325 :         if(SUCCESS == status)
      45                 :             :         {
      46                 :         325 :                 char *db_file_full_path = strdup(db_primary_file_path);
      47                 :             : 
      48         [ -  + ]:         325 :                 if(db_file_full_path == NULL)
      49                 :             :                 {
      50                 :           0 :                         report("Memory allocation failed for database path copy");
      51                 :           0 :                         status = FAILURE;
      52                 :             :                 }
      53                 :             : 
      54         [ -  + ]:         325 :                 if(SUCCESS != status)
      55                 :             :                 {
      56                 :           0 :                         provide(status);
      57                 :             :                 }
      58                 :             : 
      59                 :         325 :                 char *db_file_dir = dirname(db_file_full_path);
      60                 :             : 
      61         [ +  + ]:         325 :                 if(NOT_FOUND == file_availability(db_file_dir,SHOULD_BE_A_DIRECTORY))
      62                 :             :                 {
      63                 :           2 :                         slog(ERROR,"Unable to create database file. Directory %s not found\n",db_file_dir);
      64                 :           2 :                         status = FAILURE;
      65                 :             :                 }
      66                 :             : 
      67                 :         325 :                 free(db_file_full_path);
      68                 :             : 
      69         [ +  + ]:         325 :                 if(SUCCESS == status)
      70                 :             :                 {
      71         [ +  + ]:         323 :                         if(EXISTS == file_availability(db_primary_file_path,SHOULD_BE_A_FILE))
      72                 :             :                         {
      73                 :         118 :                                 config->db_primary_file_exists = true;
      74                 :             : 
      75                 :         118 :                                 int rc = stat(db_primary_file_path,&config->db_file_stat);
      76                 :             : 
      77         [ -  + ]:         118 :                                 if(rc < 0)
      78                 :             :                                 {
      79                 :           0 :                                         report("Stat of %s failed with error code: %d",db_primary_file_path,rc);
      80                 :           0 :                                         status = FAILURE;
      81                 :             :                                 }
      82                 :             : 
      83                 :             :                         } else {
      84                 :         205 :                                 config->db_primary_file_exists = false;
      85                 :             :                         }
      86                 :             :                 }
      87                 :             :         }
      88                 :             : 
      89                 :         325 :         provide(status);
      90                 :             : }
        

Generated by: LCOV version 2.0-1