asar coverage - build #301


src/asar/
File: src/asar/platform/linux/file-helpers-linux.cpp
Date: 2025-03-18 20:16:22
Lines:
57/63
90.5%
Functions:
10/10
100.0%
Branches:
28/43
65.1%

Line Branch Exec Source
1 #include "platform/file-helpers.h"
2 #include <sys/stat.h>
3 #include <errno.h>
4
5 5491 bool file_exists(const char * filename)
6 {
7 5491 struct stat st;
8
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5491 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5491 times.
5491 return (!stat(filename, &st));
9 }
10
11 11720 bool path_is_absolute(const char* path)
12 {
13 11720 return ('/' == path[0]);
14 }
15
16 33 char get_native_path_separator()
17 {
18 33 return '/';
19 }
20
21 292 bool check_is_regular_file(const char* path)
22 {
23 292 struct stat finfo;
24
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 292 times.
✓ Branch 4 taken 292 times.
✗ Branch 5 not taken.
292 if (stat(path, &finfo) == 0)
25 {
26 // either regular file or symlink
27
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 12 times.
292 if ((finfo.st_mode & S_IFMT) == S_IFREG)
28 280 return true;
29 }
30 12 return false;
31 }
32
33
34 303 FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error)
35 {
36 // ban fopen(".")
37 // this calls stat twice instead of once but whatever lol
38
6/6
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 280 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 297 times.
303 if(file_exists(path) && !check_is_regular_file(path)) {
39 // this error has a vfe error id, but not a file-helpers error id for some reason?????
40 // why do we have 2 different error enums in the first place????????
41 6 *error = FileOpenError_Unknown;
42 6 return NULL;
43 }
44
45 297 FILE* out_handle = NULL;
46 const char* open_mode;
47
48
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
297 switch (mode)
49 {
50 3 case FileOpenMode_ReadWrite:
51 3 open_mode = "r+b";
52 3 break;
53 291 case FileOpenMode_Read:
54 291 open_mode = "rb";
55 291 break;
56 3 case FileOpenMode_Write:
57 3 open_mode = "wb";
58 3 break;
59 }
60
61 297 out_handle = fopen(path, open_mode);
62
63
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 14 times.
297 if (error != NULL)
64 {
65
2/2
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 8 times.
283 if (out_handle != NULL)
66 {
67 275 *error = FileOpenError_None;
68 }
69 else
70 {
71 8 int fopen_error = errno;
72
73
1/3
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
8 switch (fopen_error)
74 {
75 8 case ENOENT:
76 8 *error = FileOpenError_NotFound;
77 8 break;
78 case EACCES:
79 *error = FileOpenError_AccessDenied;
80 break;
81 default:
82 *error = FileOpenError_Unknown;
83 break;
84 }
85 }
86 }
87
88 297 return out_handle;
89 }
90
91 283 void close_file(FileHandleType handle)
92 {
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (handle == InvalidFileHandle) return;
94
95 283 fclose(handle);
96 }
97
98 280 uint64_t get_file_size(FileHandleType handle)
99 {
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (handle == InvalidFileHandle) return 0u;
101
102 280 long int f_pos = ftell(handle);
103 280 fseek(handle, 0, SEEK_END);
104 280 long int f_size = ftell(handle);
105 280 fseek(handle, f_pos, SEEK_SET);
106
107 280 return (uint64_t)f_size;
108 }
109
110 287 void set_file_pos(FileHandleType handle, uint64_t pos)
111 {
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
287 if (handle == InvalidFileHandle) return;
113
114 // TODO: Some error handling would be wise here.
115
116 287 fseek(handle, pos, SEEK_SET);
117 }
118
119 286 uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes)
120 {
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
286 if (handle == InvalidFileHandle) return 0u;
122
123 // TODO: Some error handling would be wise here.
124
125 286 return (uint32_t)fread(buffer, 1, num_bytes, handle);
126 }
127
128 3 uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes)
129 {
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (handle == InvalidFileHandle) return 0u;
131
132 // TODO: Some error handling would be wise here.
133
134
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 return (uint32_t)fwrite(buffer, 1, num_bytes, handle);
135 }
136