asar coverage - build #265


src/asar/
File: src/asar/platform/linux/file-helpers-linux.cpp
Date: 2025-02-28 06:59:41
Lines:
49/60
81.7%
Functions:
10/10
100.0%
Branches:
20/37
54.1%

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