-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-map.hpp
225 lines (181 loc) · 5.16 KB
/
file-map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#pragma once
#include <nall/file.hpp>
#include <nall/stdint.hpp>
#include <stdio.h>
#include <stdlib.h>
#if defined(PLATFORM_WINDOWS)
#include <nall/windows/utf8.hpp>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#if !defined(MAP_NORESERVE)
//not supported on FreeBSD; flag removed in 11.0
#define MAP_NORESERVE 0
#endif
namespace nall {
struct file_map {
struct mode { enum : u32 { read, write, modify, append }; };
file_map(const file_map&) = delete;
auto operator=(const file_map&) = delete;
file_map() = default;
file_map(file_map&& source) { operator=(move(source)); }
file_map(const string& filename, u32 mode) { open(filename, mode); }
~file_map() { close(); }
explicit operator bool() const { return _open; }
auto size() const -> u64 { return _size; }
auto data() -> u8* { return _data; }
auto data() const -> const u8* { return _data; }
//auto operator=(file_map&& source) -> file_map&;
//auto open(const string& filename, u32 mode) -> bool;
//auto close() -> void;
private:
bool _open = false; //zero-byte files return _data = nullptr, _size = 0
u8* _data = nullptr;
u64 _size = 0;
#if defined(API_WINDOWS)
HANDLE _file = INVALID_HANDLE_VALUE;
HANDLE _map = INVALID_HANDLE_VALUE;
public:
auto operator=(file_map&& source) -> file_map& {
_open = source._open;
_data = source._data;
_size = source._size;
_file = source._file;
_map = source._map;
source._open = false;
source._data = nullptr;
source._size = 0;
source._file = INVALID_HANDLE_VALUE;
source._map = INVALID_HANDLE_VALUE;
return *this;
}
auto open(const string& filename, u32 mode_) -> bool {
close();
if(file::exists(filename) && file::size(filename) == 0) return _open = true;
s32 desiredAccess, creationDisposition, protection, mapAccess;
switch(mode_) {
default: return false;
case mode::read:
desiredAccess = GENERIC_READ;
creationDisposition = OPEN_EXISTING;
protection = PAGE_READONLY;
mapAccess = FILE_MAP_READ;
break;
case mode::write:
//write access requires read access
desiredAccess = GENERIC_WRITE;
creationDisposition = CREATE_ALWAYS;
protection = PAGE_READWRITE;
mapAccess = FILE_MAP_ALL_ACCESS;
break;
case mode::modify:
desiredAccess = GENERIC_READ | GENERIC_WRITE;
creationDisposition = OPEN_EXISTING;
protection = PAGE_READWRITE;
mapAccess = FILE_MAP_ALL_ACCESS;
break;
case mode::append:
desiredAccess = GENERIC_READ | GENERIC_WRITE;
creationDisposition = CREATE_NEW;
protection = PAGE_READWRITE;
mapAccess = FILE_MAP_ALL_ACCESS;
break;
}
_file = CreateFileW(utf16_t(filename), desiredAccess, FILE_SHARE_READ, nullptr,
creationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
if(_file == INVALID_HANDLE_VALUE) return false;
_size = GetFileSize(_file, nullptr);
_map = CreateFileMapping(_file, nullptr, protection, 0, _size, nullptr);
if(_map == INVALID_HANDLE_VALUE) {
CloseHandle(_file);
_file = INVALID_HANDLE_VALUE;
return false;
}
_data = (u8*)MapViewOfFile(_map, mapAccess, 0, 0, _size);
return _open = true;
}
auto close() -> void {
if(_data) {
UnmapViewOfFile(_data);
_data = nullptr;
}
if(_map != INVALID_HANDLE_VALUE) {
CloseHandle(_map);
_map = INVALID_HANDLE_VALUE;
}
if(_file != INVALID_HANDLE_VALUE) {
CloseHandle(_file);
_file = INVALID_HANDLE_VALUE;
}
_open = false;
}
#else
s32 _fd = -1;
public:
auto operator=(file_map&& source) -> file_map& {
_open = source._open;
_data = source._data;
_size = source._size;
_fd = source._fd;
source._open = false;
source._data = nullptr;
source._size = 0;
source._fd = -1;
return *this;
}
auto open(const string& filename, u32 mode_) -> bool {
close();
if(file::exists(filename) && file::size(filename) == 0) return _open = true;
s32 openFlags = 0;
s32 mmapFlags = 0;
switch(mode_) {
default: return false;
case mode::read:
openFlags = O_RDONLY;
mmapFlags = PROT_READ;
break;
case mode::write:
openFlags = O_RDWR | O_CREAT; //mmap() requires read access
mmapFlags = PROT_WRITE;
break;
case mode::modify:
openFlags = O_RDWR;
mmapFlags = PROT_READ | PROT_WRITE;
break;
case mode::append:
openFlags = O_RDWR | O_CREAT;
mmapFlags = PROT_READ | PROT_WRITE;
break;
}
_fd = ::open(filename, openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if(_fd < 0) return false;
struct stat _stat;
fstat(_fd, &_stat);
_size = _stat.st_size;
_data = (u8*)mmap(nullptr, _size, mmapFlags, MAP_SHARED | MAP_NORESERVE, _fd, 0);
if(_data == MAP_FAILED) {
_data = nullptr;
::close(_fd);
_fd = -1;
return false;
}
return _open = true;
}
auto close() -> void {
if(_data) {
munmap(_data, _size);
_data = nullptr;
}
if(_fd >= 0) {
::close(_fd);
_fd = -1;
}
_open = false;
}
#endif
};
}