asar coverage - build #127


src/asar/
File: src/asar/platform/windows/file-helpers-win32.cpp
Date: 2024-01-21 09:08:27
Lines:
39/52
75.0%
Functions:
10/10
100.0%
Branches:
28/55
50.9%

Line Branch Exec Source
1 // RPG Hacker: We can't include that here, because it leads to crazy
2 // errors in windows.h - probably related to our string class?!?
3 //#include "platform/file-helpers.h"
4
5 #if defined(_MSC_VER)
6 # pragma warning(push)
7 # pragma warning(disable : 4668)
8 # pragma warning(disable : 4987)
9 #endif
10
11 #define NOMINMAX
12 #include <windows.h>
13 #include <ctype.h>
14
15 #if defined(_MSC_VER)
16 # pragma warning(pop)
17 #endif
18
19 #include <ctype.h>
20
21 #include "../file-helpers.h"
22 #include "../../unicode.h"
23
24
1/2
✓ Branch 0 taken 16424 times.
✗ Branch 1 not taken.
16424 bool file_exists(const char * filename)
25 {
26 std::wstring u16_str;
27
2/4
✓ Branch 0 taken 16424 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16424 times.
✗ Branch 3 not taken.
16424 if (!utf8_to_utf16(&u16_str, filename)) return false;
28
1/2
✓ Branch 0 taken 16424 times.
✗ Branch 1 not taken.
16424 return (GetFileAttributesW(u16_str.c_str()) != INVALID_FILE_ATTRIBUTES);
29 }
30
31 34376 bool path_is_absolute(const char* path)
32 {
33 68671 return ((isalpha(path[0]) &&
34
3/4
✓ Branch 0 taken 3543 times.
✓ Branch 1 taken 30752 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3543 times.
34295 (':' == path[1]) && (('\\' == path[2]) || ('/' == path[2]))) /* drive letter + root, e.g. C:\dir or C:/dir */
35
4/6
✓ Branch 0 taken 34295 times.
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30833 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30833 times.
65128 || ('\\' == path[0]) || ('/' == path[0])); /* just root, e.g. \dir or /dir */
36 }
37
38 38 char get_native_path_separator()
39 {
40 38 return '\\';
41 }
42
43
1/2
✓ Branch 0 taken 773 times.
✗ Branch 1 not taken.
773 bool check_is_regular_file(const char* path)
44 {
45 std::wstring u16_str;
46
2/4
✓ Branch 0 taken 773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 773 times.
✗ Branch 3 not taken.
773 if (!utf8_to_utf16(&u16_str, path)) return false;
47
1/2
✓ Branch 0 taken 773 times.
✗ Branch 1 not taken.
773 return !(GetFileAttributesW(u16_str.c_str()) & FILE_ATTRIBUTE_DIRECTORY);
48 }
49
50
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error)
52 {
53 HANDLE out_handle = NULL;
54 DWORD access_mode = 0;
55 DWORD share_mode = 0;
56 DWORD disposition = 0;
57
58 switch (mode)
59 {
60 case FileOpenMode_ReadWrite:
61 access_mode = GENERIC_READ | GENERIC_WRITE;
62 disposition = OPEN_EXISTING;
63 break;
64 case FileOpenMode_Read:
65 access_mode = GENERIC_READ;
66 // This should be fine, right?
67 share_mode = FILE_SHARE_READ;
68 disposition = OPEN_EXISTING;
69 break;
70 case FileOpenMode_Write:
71 access_mode = GENERIC_WRITE;
72 disposition = CREATE_ALWAYS;
73 break;
74 }
75
76 std::wstring u16_str;
77
2/4
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1346 times.
1346 if (!utf8_to_utf16(&u16_str, path))
78 {
79 // RPG Hacker: I treat encoding error as "file not found", which I guess is what
80 // Windows would do, anyways.
81 *error = FileOpenError_NotFound;
82 return INVALID_HANDLE_VALUE;
83 }
84
85
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 out_handle = CreateFileW(u16_str.c_str(), access_mode, share_mode, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL);
86
87
2/2
✓ Branch 0 taken 755 times.
✓ Branch 1 taken 591 times.
1346 if (error != NULL)
88 {
89
1/2
✓ Branch 0 taken 755 times.
✗ Branch 1 not taken.
755 if (out_handle != INVALID_HANDLE_VALUE)
90 {
91 755 *error = FileOpenError_None;
92 }
93 else
94 {
95 DWORD win_error = GetLastError();
96
97 switch (win_error)
98 {
99 case ERROR_FILE_NOT_FOUND:
100 *error = FileOpenError_NotFound;
101 break;
102 case ERROR_ACCESS_DENIED:
103 *error = FileOpenError_AccessDenied;
104 break;
105 default:
106 *error = FileOpenError_Unknown;
107 break;
108 }
109 }
110 }
111
112 return out_handle;
113 }
114
115 1346 void close_file(FileHandleType handle)
116 {
117
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if (handle == InvalidFileHandle) return;
118
119 1346 CloseHandle(handle);
120 }
121
122 1345 uint64_t get_file_size(FileHandleType handle)
123 {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1345 times.
1345 if (handle == InvalidFileHandle) return 0u;
125
126 LARGE_INTEGER f_size;
127
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1345 times.
1345 if (!GetFileSizeEx(handle, &f_size)) return 0u;
129
130 1345 return (uint64_t)f_size.QuadPart;
131 }
132
133 974 void set_file_pos(FileHandleType handle, uint64_t pos)
134 {
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 974 times.
974 if (handle == InvalidFileHandle) return;
136
137 // TODO: Some error handling would be wise here.
138
139 LARGE_INTEGER new_pos;
140 974 new_pos.QuadPart = (LONGLONG)pos;
141
142 974 SetFilePointerEx(handle, new_pos, NULL, FILE_BEGIN);
143 }
144
145 1366 uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes)
146 {
147
1/2
✓ Branch 0 taken 1366 times.
✗ Branch 1 not taken.
1366 if (handle == InvalidFileHandle) return 0u;
148
149 1366 DWORD bytes_read = 0u;
150
151 // TODO: Some error handling would be wise here.
152
153 1366 ReadFile(handle, buffer, (DWORD)num_bytes, &bytes_read, NULL);
154
155 1366 return (uint32_t)bytes_read;
156 }
157
158 80 uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes)
159 {
160
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
80 if (handle == InvalidFileHandle) return 0u;
161
162 80 DWORD bytes_written = 0u;
163
164 // TODO: Some error handling would be wise here.
165
166 80 WriteFile(handle, buffer, (DWORD)num_bytes, &bytes_written, NULL);
167
168 80 return (uint32_t)bytes_written;
169 }
170