LCOV - code coverage report
Current view: top level - asar/platform/linux - file-helpers-linux.cpp (source / functions) Coverage Total Hit
Test: asar build #66 Lines: 79.2 % 53 42
Test Date: 2024-01-16 02:45:19 Functions: 100.0 % 10 10
Branches: 56.0 % 25 14

             Branch data     Line data    Source code
       1                 :             : #include "platform/file-helpers.h"
       2                 :             : #include <sys/stat.h>
       3                 :             : #include <errno.h>
       4                 :             : 
       5                 :        1052 : bool file_exists(const char * filename)
       6                 :             : {
       7                 :             :         struct stat st;
       8                 :        1052 :         return (!stat(filename, &st));
       9                 :             : }
      10                 :             : 
      11                 :        2288 : bool path_is_absolute(const char* path)
      12                 :             : {
      13                 :        2288 :         return ('/' == path[0]);
      14                 :             : }
      15                 :             : 
      16                 :          16 : char get_native_path_separator()
      17                 :             : {
      18                 :          16 :         return '/';
      19                 :             : }
      20                 :             : 
      21                 :         258 : bool check_is_regular_file(const char* path)
      22                 :             : {
      23                 :             :         struct stat finfo;
      24         [ +  - ]:         258 :         if (stat(path, &finfo) == 0)
      25                 :             :         {
      26                 :             :                 // either regular file or symlink
      27         [ +  + ]:         258 :                 if (finfo.st_mode & (S_IFREG | S_IFLNK))
      28                 :         252 :                         return true;
      29                 :             :         }
      30                 :             :         return false;
      31                 :             : }
      32                 :             : 
      33                 :             : 
      34                 :         686 : FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error)
      35                 :             : {
      36                 :             :         FILE* out_handle = NULL;
      37                 :             :         const char* open_mode;
      38                 :             : 
      39   [ +  +  +  - ]:         686 :         switch (mode)
      40                 :             :         {
      41                 :         115 :         case FileOpenMode_ReadWrite:
      42                 :             :                 open_mode = "r+b";
      43                 :         115 :                 break;
      44                 :         482 :         case FileOpenMode_Read:
      45                 :             :                 open_mode = "rb";
      46                 :         482 :                 break;
      47                 :          89 :         case FileOpenMode_Write:
      48                 :             :                 open_mode = "wb";
      49                 :          89 :                 break;
      50                 :             :         }
      51                 :             : 
      52                 :         686 :         out_handle = fopen(path, open_mode);
      53                 :             : 
      54         [ +  + ]:         686 :         if (error != NULL)
      55                 :             :         {
      56         [ +  - ]:         252 :                 if (out_handle != NULL)
      57                 :             :                 {
      58                 :         252 :                         *error = FileOpenError_None;
      59                 :             :                 }
      60                 :             :                 else
      61                 :             :                 {
      62                 :           0 :                         int fopen_error = errno;
      63                 :             : 
      64      [ #  #  # ]:           0 :                         switch (fopen_error)
      65                 :             :                         {
      66                 :           0 :                         case ENOENT:
      67                 :           0 :                                 *error = FileOpenError_NotFound;
      68                 :           0 :                                 break;
      69                 :           0 :                         case EACCES:
      70                 :           0 :                                 *error = FileOpenError_AccessDenied;
      71                 :           0 :                                 break;
      72                 :           0 :                         default:
      73                 :           0 :                                 *error = FileOpenError_Unknown;
      74                 :           0 :                                 break;
      75                 :             :                         }
      76                 :             :                 }
      77                 :             :         }
      78                 :             : 
      79                 :         686 :         return out_handle;
      80                 :             : }
      81                 :             : 
      82                 :         686 : void close_file(FileHandleType handle)
      83                 :             : {
      84         [ +  - ]:         686 :         if (handle == InvalidFileHandle) return;
      85                 :             : 
      86                 :         686 :         fclose(handle);
      87                 :             : }
      88                 :             : 
      89                 :         597 : uint64_t get_file_size(FileHandleType handle)
      90                 :             : {
      91         [ +  - ]:         597 :         if (handle == InvalidFileHandle) return 0u;
      92                 :             : 
      93                 :         597 :         long int f_pos = ftell(handle);
      94                 :         597 :         fseek(handle, 0, SEEK_END);
      95                 :         597 :         long int f_size = ftell(handle);
      96                 :         597 :         fseek(handle, f_pos, SEEK_SET);
      97                 :             : 
      98                 :         597 :         return (uint64_t)f_size;
      99                 :             : }
     100                 :             : 
     101                 :         452 : void set_file_pos(FileHandleType handle, uint64_t pos)
     102                 :             : {
     103         [ +  - ]:         452 :         if (handle == InvalidFileHandle) return;
     104                 :             : 
     105                 :             :         // TODO: Some error handling would be wise here.
     106                 :             : 
     107                 :         452 :         fseek(handle, pos, SEEK_SET);
     108                 :             : }
     109                 :             : 
     110                 :         604 : uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes)
     111                 :             : {
     112         [ +  - ]:         604 :         if (handle == InvalidFileHandle) return 0u;
     113                 :             : 
     114                 :             :         // TODO: Some error handling would be wise here.
     115                 :             : 
     116                 :         604 :         return (uint32_t)fread(buffer, 1, num_bytes, handle);
     117                 :             : }
     118                 :             : 
     119                 :          78 : uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes)
     120                 :             : {
     121         [ +  - ]:          78 :         if (handle == InvalidFileHandle) return 0u;
     122                 :             : 
     123                 :             :         // TODO: Some error handling would be wise here.
     124                 :             : 
     125                 :          78 :         return (uint32_t)fwrite(buffer, 1, num_bytes, handle);
     126                 :             : }
        

Generated by: LCOV version 2.0-1