Newer
Older
#include <vereign/fs/util.hh>
#include <vereign/fs/operations.hh>
#include <vereign/fs/path.hh>
#include <vereign/fs/errors.hh>
#include <vereign/core/rand.hh>
#include <vereign/core/string.hh>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
// FIXME: remove
#include <iostream>
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
#ifdef _WIN32
# include <shlobj_core.h>
#endif
namespace vereign::fs {
namespace detail {
auto PathToString(const boost::filesystem::path& path) -> std::string {
#ifdef _WIN32
return string::narrow(path.wstring());
#else
return path.string();
#endif
}
auto StringToPath(std::string_view path) -> boost::filesystem::path {
#ifdef _WIN32
return boost::filesystem::path{string::widen(path)};
#else
return boost::filesystem::path{std::string{path}};
#endif
}
}
RemoveFileGuard::RemoveFileGuard(std::string path)
: path_{std::move(path)}
{
}
RemoveFileGuard::~RemoveFileGuard() {
boost::filesystem::remove(detail::StringToPath(path_));
}
RemoveAllGuard::RemoveAllGuard(std::string path)
: path_{std::move(path)}
{
}
RemoveAllGuard::~RemoveAllGuard() {
boost::filesystem::remove_all(detail::StringToPath(path_));
}
auto TempFilePath(std::string_view dir, std::string_view prefix) -> std::string {
return path::Join(std::string(dir), std::string(prefix) + core::RandLowerAlpha(10));
}
auto TempFilePath(std::string_view prefix) -> std::string {
return TempFilePath(detail::PathToString(boost::filesystem::temp_directory_path()), prefix);
}
auto TempDir(std::string_view prefix) -> std::string {
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
return dir;
}
auto HomePath() -> std::string {
#ifdef _WIN32
PWSTR home;
SHGetKnownFolderPath(
FOLDERID_LocalAppData,
0,
nullptr,
&home
);
std::wstring wpath;
try {
wpath = home;
CoTaskMemFree(home);
} catch (...) {
CoTaskMemFree(home);
throw;
}
return "";
auto path = boost::filesystem::path(wpath);
if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)) {
return detail::PathToString(path);
}
throw HomeNotFoundError{};
#else
auto home = std::getenv("HOME");
if (home == nullptr) {
throw HomeNotFoundError{};
}
auto path = boost::filesystem::path{home};
if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)) {
return detail::PathToString(path);
}
throw HomeNotFoundError{};
#endif
}
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
auto ReadFile(std::string_view path) -> bytes::Buffer {
boost::filesystem::ifstream file{
detail::StringToPath(path),
boost::filesystem::fstream::binary | boost::filesystem::fstream::ate,
};
if (!file.is_open()) {
throw fs::Error("open file for reading failed");
}
auto size = file.tellg();
bytes::Buffer result{static_cast<size_t>(size)};
file.seekg(0);
file.read((char*) result.end(), size);
result.IncSize(file.gcount());
file.close();
return result;
}
void WriteFile(std::string_view path, bytes::View data) {
boost::filesystem::ofstream file{
detail::StringToPath(path),
boost::filesystem::fstream::binary | boost::filesystem::fstream::trunc,
};
if (!file.is_open()) {
throw fs::Error("open file for writing failed");
}
file.write(data.CharData(), data.Size());
file.close();
}