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

            Line data    Source code
       1              : #include "precizer.h"
       2              : 
       3              : /**
       4              :  *
       5              :  * Function to check whether a directory exists or not.
       6              :  * @returns Returns EXISTS if given path is directory and exists
       7              :  *                  NOT_FOUND otherwise
       8              :  *
       9              :  */
      10          638 : FileAvailability file_availability(
      11              :         const char          *path,
      12              :         const unsigned char fs_object_type)
      13              : {
      14              :         /// The status that will be passed to return() before exiting.
      15              :         /// By default, the function worked without errors.
      16          638 :         FileAvailability presence = NOT_FOUND;
      17              : 
      18              :         // Do nothing if the path is not a real path but in-memory database
      19          638 :         if(strcmp(path,":memory:") == 0)
      20              :         {
      21           36 :                 return(presence);
      22              :         }
      23              : 
      24              :         struct stat stats;
      25              : 
      26          602 :         slog(TRACE,"Verify that the path %s exists\n",path);
      27              : 
      28              :         // Check for existence
      29          602 :         if(stat(path,&stats) == 0)
      30              :         {
      31              :                 // Check is it a directory or a file
      32          518 :                 if(fs_object_type == SHOULD_BE_A_FILE)
      33              :                 {
      34          152 :                         if(S_ISREG(stats.st_mode))
      35              :                         {
      36          152 :                                 slog(TRACE,"The path %s is exists and it is a file\n",path);
      37          152 :                                 presence = EXISTS;
      38              :                         } else {
      39            0 :                                 presence = NOT_FOUND;
      40              :                         }
      41              : 
      42              :                 } else {
      43          366 :                         if(S_ISDIR(stats.st_mode))
      44              :                         {
      45          366 :                                 slog(TRACE,"The path %s is exists and it is a directory\n",path);
      46          366 :                                 presence = EXISTS;
      47              :                         } else {
      48            0 :                                 presence = NOT_FOUND;
      49              :                         }
      50              :                 }
      51              : 
      52              :         } else {
      53           84 :                 presence = NOT_FOUND;
      54              :         }
      55              : 
      56          602 :         if(EXISTS != presence)
      57              :         {
      58           84 :                 if(fs_object_type == SHOULD_BE_A_FILE)
      59              :                 {
      60           82 :                         slog(EVERY,"The path %s doesn't exist or it is not a file\n",path);
      61              :                 } else {
      62            2 :                         slog(EVERY,"The path %s doesn't exist or it is not a directory\n",path);
      63              :                 }
      64              :         }
      65              : 
      66          602 :         return(presence);
      67              : }
        

Generated by: LCOV version 2.0-1