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_IFREG | S_IFLNK)) |
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)) return NULL; |
39 |
|
|
|
40 |
|
297 |
FILE* out_handle = NULL; |
41 |
|
|
const char* open_mode; |
42 |
|
|
|
43 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
297 |
switch (mode) |
44 |
|
|
{ |
45 |
|
3 |
case FileOpenMode_ReadWrite: |
46 |
|
3 |
open_mode = "r+b"; |
47 |
|
3 |
break; |
48 |
|
291 |
case FileOpenMode_Read: |
49 |
|
291 |
open_mode = "rb"; |
50 |
|
291 |
break; |
51 |
|
3 |
case FileOpenMode_Write: |
52 |
|
3 |
open_mode = "wb"; |
53 |
|
3 |
break; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
297 |
out_handle = fopen(path, open_mode); |
57 |
|
|
|
58 |
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 14 times.
|
297 |
if (error != NULL) |
59 |
|
|
{ |
60 |
2/2
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 8 times.
|
283 |
if (out_handle != NULL) |
61 |
|
|
{ |
62 |
|
275 |
*error = FileOpenError_None; |
63 |
|
|
} |
64 |
|
|
else |
65 |
|
|
{ |
66 |
|
8 |
int fopen_error = errno; |
67 |
|
|
|
68 |
1/3
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
8 |
switch (fopen_error) |
69 |
|
|
{ |
70 |
|
8 |
case ENOENT: |
71 |
|
8 |
*error = FileOpenError_NotFound; |
72 |
|
8 |
break; |
73 |
|
✗ |
case EACCES: |
74 |
|
✗ |
*error = FileOpenError_AccessDenied; |
75 |
|
✗ |
break; |
76 |
|
✗ |
default: |
77 |
|
✗ |
*error = FileOpenError_Unknown; |
78 |
|
✗ |
break; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
297 |
return out_handle; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
283 |
void close_file(FileHandleType handle) |
87 |
|
|
{ |
88 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
|
283 |
if (handle == InvalidFileHandle) return; |
89 |
|
|
|
90 |
|
283 |
fclose(handle); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
280 |
uint64_t get_file_size(FileHandleType handle) |
94 |
|
|
{ |
95 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
|
280 |
if (handle == InvalidFileHandle) return 0u; |
96 |
|
|
|
97 |
|
280 |
long int f_pos = ftell(handle); |
98 |
|
280 |
fseek(handle, 0, SEEK_END); |
99 |
|
280 |
long int f_size = ftell(handle); |
100 |
|
280 |
fseek(handle, f_pos, SEEK_SET); |
101 |
|
|
|
102 |
|
280 |
return (uint64_t)f_size; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
287 |
void set_file_pos(FileHandleType handle, uint64_t pos) |
106 |
|
|
{ |
107 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287 times.
|
287 |
if (handle == InvalidFileHandle) return; |
108 |
|
|
|
109 |
|
|
// TODO: Some error handling would be wise here. |
110 |
|
|
|
111 |
|
287 |
fseek(handle, pos, SEEK_SET); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
286 |
uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes) |
115 |
|
|
{ |
116 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
|
286 |
if (handle == InvalidFileHandle) return 0u; |
117 |
|
|
|
118 |
|
|
// TODO: Some error handling would be wise here. |
119 |
|
|
|
120 |
|
286 |
return (uint32_t)fread(buffer, 1, num_bytes, handle); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
3 |
uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes) |
124 |
|
|
{ |
125 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
if (handle == InvalidFileHandle) return 0u; |
126 |
|
|
|
127 |
|
|
// TODO: Some error handling would be wise here. |
128 |
|
|
|
129 |
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); |
130 |
|
|
} |
131 |
|
|
|