LCOV - code coverage report
Current view: top level - src - db_determine_name.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.4 % 44 27
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            Line data    Source code
       1              : #include "precizer.h"
       2              : #define IN_MEMORY_DB_NAME "DisposableDB"
       3              : 
       4              : /**
       5              :  * Determine file name of the database.
       6              :  * This database file name can be passed as an argument --database=FILE
       7              :  * Unless specified, the default database filename
       8              :  * will be the hostname and ".db" as the filename extension
       9              :  */
      10          202 : Return db_determine_name(void)
      11              : {
      12              :         /// The status that will be passed to return() before exiting.
      13              :         /// By default, the function worked without errors.
      14          202 :         Return status = SUCCESS;
      15              : 
      16              :         /* Interrupt the function smoothly */
      17              :         /* Interrupt when Ctrl+C */
      18          202 :         if(global_interrupt_flag == true)
      19              :         {
      20            0 :                 provide(status);
      21              :         }
      22              : 
      23          202 :         if(config->compare == true)
      24              :         {
      25           36 :                 if(config->db_primary_file_path == NULL)
      26              :                 {
      27           36 :                         config->db_file_name = NULL;
      28              : 
      29              :                         // In-memory database
      30           36 :                         const char *in_memory_db_path = ":memory:";
      31           36 :                         config->db_primary_file_path = strdup(in_memory_db_path);
      32              : 
      33           36 :                         if(config->db_primary_file_path == NULL)
      34              :                         {
      35            0 :                                 report("Memory allocation failed for config->db_primary_file_path");
      36            0 :                                 status = FAILURE;
      37              :                         }
      38              : 
      39           36 :                         if(SUCCESS == status)
      40              :                         {
      41           36 :                                 config->db_file_name = strdup(IN_MEMORY_DB_NAME);
      42              : 
      43           36 :                                 if(config->db_file_name == NULL)
      44              :                                 {
      45            0 :                                         report("Memory allocation failed for config->db_file_name");
      46            0 :                                         free(config->db_primary_file_path);
      47            0 :                                         config->db_primary_file_path = NULL;
      48            0 :                                         status = FAILURE;
      49              :                                 }
      50              :                         }
      51              :                 } else {
      52            0 :                         slog(ERROR,"General failure. config->db_primary_file_path should be NULL in this case\n");
      53            0 :                         status = FAILURE;
      54              :                 }
      55              : 
      56              :         } else {
      57              : 
      58          166 :                 if(config->db_primary_file_path == NULL)
      59              :                 {
      60              : 
      61           18 :                         config->db_file_name = NULL;
      62              : 
      63           18 :                         struct utsname utsname = {0};
      64              : 
      65              :                         // Determine local host name
      66           18 :                         if(uname(&utsname) != 0)
      67              :                         {
      68            0 :                                 slog(ERROR,"Failed to get hostname\n");
      69            0 :                                 status = FAILURE;
      70              :                         }
      71              : 
      72           18 :                         if(SUCCESS == status)
      73              :                         {
      74              :                                 // Create temporary string with full path
      75           18 :                                 if(asprintf(&config->db_primary_file_path,"%s.db",utsname.nodename) == -1)
      76              :                                 {
      77            0 :                                         report("Failed to allocate memory for database path");
      78            0 :                                         status = FAILURE;
      79              :                                 }
      80              :                         }
      81              : 
      82           18 :                         if(SUCCESS == status)
      83              :                         {
      84              :                                 // Copy the same path to db_file_name
      85           18 :                                 config->db_file_name = strdup(config->db_primary_file_path);
      86              : 
      87           18 :                                 if(config->db_file_name == NULL)
      88              :                                 {
      89            0 :                                         report("Memory allocation failed, requested size: %zu bytes",strlen(config->db_primary_file_path) + 1 * sizeof(char));
      90            0 :                                         free(config->db_primary_file_path);
      91            0 :                                         config->db_primary_file_path = NULL;
      92            0 :                                         status = FAILURE;
      93              :                                 }
      94              :                         }
      95              :                 }
      96              :         }
      97              : 
      98              :         // Log message when database file is specified and confirmed as persistent storage (non-memory database)
      99          202 :         if(SUCCESS == status)
     100              :         {
     101          202 :                 if(!(strcmp(config->db_primary_file_path,":memory:") == 0 && rational_logger_mode & REGULAR))
     102              :                 {
     103          198 :                         slog(EVERY,"Primary database file name: %s\n",config->db_file_name);
     104              :                 }
     105              : 
     106          202 :                 slog(TRACE,"Primary database file path: %s\n",config->db_primary_file_path);
     107              :         }
     108              : 
     109          202 :         slog(TRACE,"DB name determined\n");
     110              : 
     111          202 :         return(status);
     112              : }
        

Generated by: LCOV version 2.0-1