LCOV - code coverage report
Current view: top level - src - file_availability.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 22 22
Test Date: 2026-03-31 13:51:38 Functions: 100.0 % 1 1
Branches: 77.3 % 22 17

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Check whether the given path exists and matches requested filesystem object type
       5                 :             :  * @param path Path to verify
       6                 :             :  * @param output_stat Optional pointer to a stat structure to receive file metadata when the
       7                 :             :  *        path is found. Pass NULL if metadata is not needed
       8                 :             :  * @param fs_object_type Expected object type: SHOULD_BE_A_FILE or SHOULD_BE_A_DIRECTORY
       9                 :             :  * @return EXISTS when path exists and matches requested type, otherwise NOT_FOUND
      10                 :             :  * @details The special path ":memory:" is treated as not available and returns NOT_FOUND
      11                 :             :  */
      12                 :        1323 : FileAvailability file_availability(
      13                 :             :         const char          *path,
      14                 :             :         struct stat         *output_stat,
      15                 :             :         const unsigned char fs_object_type)
      16                 :             : {
      17                 :             :         /// The availability status returned by this function
      18                 :             :         /// By default, the path is treated as unavailable until verified
      19                 :        1323 :         FileAvailability presence = NOT_FOUND;
      20                 :             : 
      21                 :             :         // Do nothing if the path is not a real path but in-memory database
      22         [ +  + ]:        1323 :         if(strcmp(path,":memory:") == 0)
      23                 :             :         {
      24                 :         118 :                 return(presence);
      25                 :             :         }
      26                 :             : 
      27                 :             :         struct stat stats;
      28                 :             : 
      29                 :        1205 :         slog(TRACE,"Verify that the path %s exists\n",path);
      30                 :             : 
      31                 :             :         // Check for existence
      32         [ +  + ]:        1205 :         if(stat(path,&stats) == 0)
      33                 :             :         {
      34                 :             :                 // Check is it a directory or a file
      35         [ +  + ]:        1040 :                 if(fs_object_type == SHOULD_BE_A_FILE)
      36                 :             :                 {
      37                 :             :                         // Verify that the path points to a regular file
      38         [ +  - ]:         354 :                         if(S_ISREG(stats.st_mode))
      39                 :             :                         {
      40                 :         354 :                                 slog(TRACE,"The path %s is exists and it is a file\n",path);
      41                 :         354 :                                 presence = EXISTS;
      42                 :             :                         }
      43                 :             : 
      44         [ +  - ]:         686 :                 } else if(fs_object_type == SHOULD_BE_A_DIRECTORY){
      45                 :             :                         // Verify that the path points to a directory
      46         [ +  - ]:         686 :                         if(S_ISDIR(stats.st_mode))
      47                 :             :                         {
      48                 :         686 :                                 slog(TRACE,"The path %s is exists and it is a directory\n",path);
      49                 :         686 :                                 presence = EXISTS;
      50                 :             :                         }
      51                 :             :                 }
      52                 :             : 
      53                 :             :                 // Copy metadata to caller-provided buffer if the path was found
      54   [ +  -  +  + ]:        1040 :                 if(EXISTS == presence && output_stat != NULL)
      55                 :             :                 {
      56                 :         120 :                         *output_stat = stats;
      57                 :             :                 }
      58                 :             :         }
      59                 :             : 
      60         [ +  + ]:        1205 :         if(EXISTS != presence)
      61                 :             :         {
      62         [ +  + ]:         165 :                 if(fs_object_type == SHOULD_BE_A_FILE)
      63                 :             :                 {
      64                 :         163 :                         slog(EVERY,"The path %s doesn't exist or it is not a file\n",path);
      65         [ +  - ]:           2 :                 } else if(fs_object_type == SHOULD_BE_A_DIRECTORY){
      66                 :           2 :                         slog(EVERY,"The path %s doesn't exist or it is not a directory\n",path);
      67                 :             :                 }
      68                 :             :         }
      69                 :             : 
      70                 :        1205 :         return(presence);
      71                 :             : }
        

Generated by: LCOV version 2.0-1