-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFs.cpp
More file actions
251 lines (207 loc) · 5.5 KB
/
Copy pathFs.cpp
File metadata and controls
251 lines (207 loc) · 5.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
*
* @file Fs.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/util/Fs.hpp>
#include <vix/utils/Env.hpp>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <system_error>
#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace vix::cli::util
{
bool file_exists(const fs::path &p)
{
std::error_code ec{};
return fs::exists(p, ec) && !ec;
}
bool dir_exists(const fs::path &p)
{
std::error_code ec{};
return fs::exists(p, ec) && fs::is_directory(p, ec) && !ec;
}
bool ensure_dir(const fs::path &p, std::string &err)
{
if (dir_exists(p))
return true;
std::error_code ec{};
fs::create_directories(p, ec);
if (ec)
{
err = ec.message();
return false;
}
return true;
}
std::string read_text_file_or_empty(const fs::path &p)
{
std::ifstream ifs(p, std::ios::binary);
if (!ifs)
return {};
std::ostringstream oss;
oss << ifs.rdbuf();
return oss.str();
}
bool write_text_file_atomic(const fs::path &p, const std::string &content)
{
std::error_code ec{};
if (!p.parent_path().empty())
fs::create_directories(p.parent_path(), ec);
const fs::path tmp = p.string() + ".tmp";
{
std::ofstream ofs(tmp, std::ios::binary);
if (!ofs)
return false;
ofs.write(content.data(), static_cast<std::streamsize>(content.size()));
ofs.flush();
if (!ofs)
return false;
}
fs::rename(tmp, p, ec);
if (ec)
{
fs::remove(tmp, ec);
return false;
}
return true;
}
std::optional<fs::path> find_project_root(fs::path start)
{
std::error_code ec{};
start = fs::weakly_canonical(start, ec);
if (ec)
return std::nullopt;
for (fs::path p = start; !p.empty(); p = p.parent_path())
{
if (file_exists(p / "CMakeLists.txt"))
return p;
if (p == p.root_path())
break;
}
return std::nullopt;
}
void collect_files_recursive(
const fs::path &root,
const std::string &ext,
std::vector<fs::path> &out)
{
std::error_code ec{};
if (!dir_exists(root))
return;
for (auto it = fs::recursive_directory_iterator(root, ec);
it != fs::recursive_directory_iterator(); it.increment(ec))
{
if (ec)
break;
if (!it->is_regular_file(ec))
continue;
const fs::path p = it->path();
if (ext.empty() || p.extension() == ext)
out.push_back(p);
}
}
void print_log_tail_fast(const fs::path &logPath, std::size_t maxLines)
{
std::ifstream ifs(logPath, std::ios::binary);
if (!ifs)
return;
ifs.seekg(0, std::ios::end);
const std::streamoff size = ifs.tellg();
if (size <= 0)
return;
const std::streamoff chunkSize = 64 * 1024; // 64 KB
std::string buffer;
buffer.reserve(static_cast<std::size_t>(std::min<std::streamoff>(size, chunkSize)));
std::streamoff pos = size;
std::size_t linesFound = 0;
std::string acc;
while (pos > 0 && linesFound <= maxLines)
{
const std::streamoff readSize = std::min<std::streamoff>(chunkSize, pos);
pos -= readSize;
ifs.seekg(pos, std::ios::beg);
buffer.assign(static_cast<std::size_t>(readSize), '\0');
ifs.read(&buffer[0], readSize);
if (!ifs)
break;
acc.insert(0, buffer);
linesFound = 0;
for (char c : acc)
if (c == '\n')
++linesFound;
if (linesFound > maxLines)
break;
}
std::vector<std::string> lines;
{
std::istringstream is(acc);
std::string line;
while (std::getline(is, line))
lines.push_back(line);
}
const std::size_t start = (lines.size() > maxLines) ? (lines.size() - maxLines) : 0;
std::cerr << "\n--- " << logPath.string() << " (last "
<< (lines.size() - start) << " lines) ---\n";
for (std::size_t i = start; i < lines.size(); ++i)
std::cerr << lines[i] << "\n";
std::cerr << "--- end ---\n\n";
}
#ifdef _WIN32
bool executable_on_path(const std::string &exeName)
{
char buf[MAX_PATH];
DWORD n = ::SearchPathA(nullptr, exeName.c_str(), nullptr,
static_cast<DWORD>(sizeof(buf)), buf, nullptr);
return n != 0 && n < sizeof(buf);
}
#else
static bool is_executable_file(const fs::path &p)
{
struct stat sb{};
if (::stat(p.c_str(), &sb) != 0)
return false;
if (!S_ISREG(sb.st_mode))
return false;
return (sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || (sb.st_mode & S_IXOTH);
}
bool executable_on_path(const std::string &exeName)
{
const char *pathEnv = vix::utils::vix_getenv("PATH");
if (!pathEnv)
return false;
std::string_view pathStr(pathEnv);
std::size_t start = 0;
while (start <= pathStr.size())
{
std::size_t end = pathStr.find(':', start);
if (end == std::string_view::npos)
end = pathStr.size();
std::string_view dir = pathStr.substr(start, end - start);
if (!dir.empty())
{
fs::path candidate = fs::path(std::string(dir)) / exeName;
if (is_executable_file(candidate))
return true;
}
start = end + 1;
}
return false;
}
#endif
} // namespace vix::cli::util