-
Notifications
You must be signed in to change notification settings - Fork 60
/
file_man.cpp
193 lines (161 loc) 路 5.25 KB
/
file_man.cpp
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
/// @file
#include "file_man.h"
#include <map>
#include <mutex>
#include <sstream>
#include "win_str_convert.h"
namespace fineftp
{
namespace {
std::mutex guard;
std::map<ReadableFile::Str, std::weak_ptr<ReadableFile>> files;
} // namespace
ReadableFile::~ReadableFile()
{
if (data_ != nullptr)
::UnmapViewOfFile(data_);
if (map_handle_ != INVALID_HANDLE_VALUE)
::CloseHandle(map_handle_);
if (handle_ != INVALID_HANDLE_VALUE)
::CloseHandle(handle_);
const std::lock_guard<std::mutex> lock{guard};
if (!path_.empty())
{
(void)files.erase(path_);
}
}
std::shared_ptr<ReadableFile> ReadableFile::get(const Str& file_path)
{
std::basic_ostringstream<Str::value_type> os;
for (auto c : file_path)
{
if (c == '/')
{
os << '\\';
}
else
{
os << c;
}
}
auto&& file_path_fixed_separators = os.str();
// See if we already have this file mapped
const std::lock_guard<std::mutex> lock{guard};
auto existing_files_it = files.find(file_path_fixed_separators);
if (files.end() != existing_files_it)
{
auto readable_file_ptr = existing_files_it->second.lock();
if (readable_file_ptr)
{
return readable_file_ptr;
}
}
#if !defined(__GNUG__)
HANDLE file_handle =
::CreateFileW(file_path_fixed_separators.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
#else
auto file_handle =
::CreateFileA(file_path_fixed_separators.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
#endif
if (INVALID_HANDLE_VALUE == file_handle)
{
return {};
}
// Get the file size by Using GetFileInformationByHandle
BY_HANDLE_FILE_INFORMATION file_info;
if (0 == ::GetFileInformationByHandle(file_handle, &file_info))
{
::CloseHandle(file_handle);
return {};
}
LARGE_INTEGER file_size;
file_size.LowPart = file_info.nFileSizeLow;
file_size.HighPart = file_info.nFileSizeHigh;
// Create new ReadableFile ptr
std::shared_ptr<ReadableFile> readable_file_ptr(new ReadableFile{});
if (file_size.QuadPart == 0)
{
// Handle zero-size files
readable_file_ptr->path_ = std::move(file_path_fixed_separators);
readable_file_ptr->size_ = file_size.QuadPart;
readable_file_ptr->data_ = static_cast<uint8_t*>(nullptr);
readable_file_ptr->handle_ = file_handle;
readable_file_ptr->map_handle_ = INVALID_HANDLE_VALUE;
}
else
{
// Handle non-zero-size files
auto* map_handle = ::CreateFileMapping(file_handle, nullptr, PAGE_READONLY, file_size.HighPart, file_size.LowPart, nullptr);
if ((map_handle == INVALID_HANDLE_VALUE) || (map_handle == nullptr))
{
::CloseHandle(file_handle);
return {};
}
auto* map_start = ::MapViewOfFile(map_handle, FILE_MAP_READ, 0, 0, file_size.QuadPart);
if (nullptr == map_start)
{
::CloseHandle(map_handle);
::CloseHandle(file_handle);
return {};
}
readable_file_ptr->path_ = std::move(file_path_fixed_separators);
readable_file_ptr->size_ = file_size.QuadPart;
readable_file_ptr->data_ = static_cast<uint8_t*>(map_start);
readable_file_ptr->handle_ = file_handle;
readable_file_ptr->map_handle_ = map_handle;
}
// Add readable_file_ptr to the map and return it to the user
files[readable_file_ptr->path_] = readable_file_ptr;
return readable_file_ptr;
}
WriteableFile::WriteableFile(const std::string& filename, std::ios::openmode mode)
{
// std::ios::binary is ignored in mode because, on Windows, even ASCII files have to be stored as
// binary files as they come in with the right line endings.
DWORD dwDesiredAccess = GENERIC_WRITE; // It's always a writeable file
if (bool(mode & std::ios::app))
{
dwDesiredAccess |= FILE_APPEND_DATA; // Append to the file
}
DWORD dwCreationDisposition = 0;
if (bool(mode & std::ios::app))
{
dwCreationDisposition = OPEN_EXISTING; // Append => Open existing file
}
else
{
dwCreationDisposition = CREATE_ALWAYS; // Not Append => Create new file
}
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
#if !defined(__GNUG__)
auto wfilename = StrConvert::Utf8ToWide(filename);
handle_ = ::CreateFileW(wfilename.c_str(), dwDesiredAccess, FILE_SHARE_DELETE, nullptr, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
#else
handle_ = ::CreateFileA(filename.c_str(), dwDesiredAccess, FILE_SHARE_DELETE, nullptr, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
#endif
if (INVALID_HANDLE_VALUE != handle_ && (mode & std::ios::app) == std::ios::app)
{
if (INVALID_SET_FILE_POINTER == ::SetFilePointer(handle_, 0, nullptr, FILE_END))
{
close();
}
}
}
WriteableFile::~WriteableFile()
{
close();
}
void WriteableFile::close()
{
if (INVALID_HANDLE_VALUE != handle_)
{
::CloseHandle(handle_);
handle_ = INVALID_HANDLE_VALUE;
}
}
void WriteableFile::write(const char* data, std::size_t sz)
{
DWORD bytes_written{}; // Unused, but required according to https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
(void)::WriteFile(handle_, data, static_cast<DWORD>(sz), &bytes_written, nullptr);
}
}