FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mcpp/src/platform/fs.cppm at main · mcpp-community/mcpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
mcpp-community
/
mcpp
Public
Notifications
You must be signed in to change notification settings
Fork
12
Star
111
Code
Issues
35
Pull requests
6
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mcpp
/
src
/
platform
/
fs.cppm
Copy path
More file actions
More file actions
Latest commit
History
History
History
259 lines (226 loc) · 8.37 KB
Breadcrumbs
mcpp
/
src
/
platform
/
fs.cppm
Copy path
File metadata and controls
259 lines (226 loc) · 8.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
252
253
254
255
256
257
258
259
//
mcpp.platform.fs — platform-specific filesystem operations.
//
//
Consolidates three patterns that were duplicated across the codebase:
//
1. self_exe_path() — current executable path (was in config.cppm &
//
ninja_backend.cppm)
//
2. which() — find an executable in PATH (was in config.cppm &
//
probe.cppm)
//
3. FileLock — RAII exclusive file lock (was in bmi_cache.cppm)
module
;
#
include
<
cstdio
>
#
include
<
cstdlib
>
#
if
defined(_WIN32)
#
include
<
io.h
>
#
include
<
windows.h
>
#
define
popen
_popen
#
define
pclose
_pclose
#
elif
defined(__APPLE__)
#
include
<
mach-o/dyld.h
>
//
_NSGetExecutablePath
#
else
#
include
<
fcntl.h
>
#
include
<
sys/file.h
>
#
include
<
unistd.h
>
#
endif
//
POSIX file locking (shared between macOS and Linux)
#
if
!defined(_WIN32)
#
include
<
fcntl.h
>
#
include
<
sys/file.h
>
#
include
<
unistd.h
>
#
endif
export
module
mcpp.platform.fs;
import
std;
import
mcpp.platform.common;
export
namespace
mcpp
::platform::fs {
//
── self_exe_path ─────────────────────────────────────────────────────────
//
//
Returns the absolute path of the currently running executable.
//
Windows: GetModuleFileNameA
//
macOS: _NSGetExecutablePath
//
Linux: /proc/self/exe
std::filesystem::path
self_exe_path
();
//
── which ─────────────────────────────────────────────────────────────────
//
//
Find an executable by name in PATH.
//
Windows: `where <name>`
//
POSIX: `command -v <name>`
std::optional<std::filesystem::path>
which
(std::string_view binary_name);
//
不先删除目标文件,避免发布失败时丢失最后一份可用的编译数据库。
bool
replace_file
(
const
std::filesystem::path& source,
const
std::filesystem::path& destination,
std::error_code& ec);
//
── FileLock ──────────────────────────────────────────────────────────────
//
//
RAII exclusive non-blocking file lock.
//
Windows: LockFileEx on a HANDLE
//
POSIX: flock(2) on a file descriptor
class
FileLock
{
public:
//
Try to acquire an exclusive lock on <dir>/.lock.
//
Returns nullopt if another process holds the lock.
static
std::optional<FileLock>
try_acquire
(
const
std::filesystem::path& dir);
~FileLock
();
FileLock
(FileLock&& o)
noexcept
;
FileLock&
operator
=(FileLock&& o)
noexcept
;
FileLock
(
const
FileLock&) =
delete
;
FileLock&
operator
=(
const
FileLock&) =
delete
;
private:
#
if
defined(_WIN32)
void
* handle_ =
reinterpret_cast
<
void
*>(
static_cast
<std::
intptr_t
>(-
1
));
//
INVALID_HANDLE_VALUE
explicit
FileLock
(
void
* h) : handle_(h) {}
#
else
int
fd_ = -
1
;
explicit
FileLock
(
int
fd) : fd_(fd) {}
#
endif
};
}
//
namespace mcpp::platform::fs
//
─── Implementation ──────────────────────────────────────────────────────
namespace
mcpp
::platform::fs {
std::filesystem::path
self_exe_path
() {
std::error_code ec;
#
if
defined(_WIN32)
char
buf[
MAX_PATH
];
DWORD
len =
GetModuleFileNameA
(
NULL
, buf,
MAX_PATH
);
if
(len >
0
&& len <
MAX_PATH
) {
auto
p =
std::filesystem::canonical
(buf, ec);
if
(!ec)
return
p;
}
#
elif
defined(__APPLE__)
char
buf[
4096
];
uint32_t
size =
sizeof
(buf);
if
(
_NSGetExecutablePath
(buf, &size) ==
0
) {
auto
p =
std::filesystem::canonical
(buf, ec);
if
(!ec)
return
p;
}
#
else
auto
p =
std::filesystem::read_symlink
(
"
/proc/self/exe
"
, ec);
if
(!ec)
return
p;
#
endif
//
Fallback: hope the binary is in PATH
return
"
mcpp
"
;
}
std::optional<std::filesystem::path>
which
(std::string_view binary_name) {
std::string
name
(binary_name);
#
if
defined(_WIN32)
std::string cmd =
"
where
"
+ name +
"
2>nul
"
;
#
else
std::string cmd =
"
command -v '
"
+ name +
"
' 2>/dev/null </dev/null
"
;
#
endif
std::array<
char
,
4096
> buf{};
std::string out;
std::
FILE
* fp = ::
popen
(cmd.
c_str
(),
"
r
"
);
if
(!fp)
return
std::
nullopt
;
while
(
std::fgets
(buf.
data
(),
static_cast
<
int
>(buf.
size
()), fp) !=
nullptr
)
out += buf.
data
();
int
rc = ::
pclose
(fp);
//
Trim and take first line
while
(!out.
empty
() && (out.
back
() ==
'
\n
'
|| out.
back
() ==
'
\r
'
))
out.
pop_back
();
auto
nl = out.
find
(
'
\n
'
);
if
(nl != std::string::npos) out.
resize
(nl);
if
(rc !=
0
|| out.
empty
())
return
std::
nullopt
;
if
(!
std::filesystem::exists
(out))
return
std::
nullopt
;
return
std::filesystem::path
(out);
}
bool
replace_file
(
const
std::filesystem::path& source,
const
std::filesystem::path& destination,
std::error_code& ec) {
#
if
defined(_WIN32)
//
直接替换已有文件,不能先删除 last-known-good CDB。编辑器或杀毒软件
//
可能短暂占用目标文件,sharing violation 仅做有限退避后再报告失败。
auto
delay = std::chrono::milliseconds{
50
};
for
(
int
attempt =
0
; attempt <
4
; ++attempt) {
if
(
MoveFileExW
(source.
wstring
().
c_str
(), destination.
wstring
().
c_str
(),
MOVEFILE_REPLACE_EXISTING
)) {
ec.
clear
();
return
true
;
}
const
auto
error =
GetLastError
();
if
(error !=
ERROR_SHARING_VIOLATION
|| attempt ==
3
) {
ec =
std::error_code
(
static_cast
<
int
>(error),
std::system_category
());
return
false
;
}
std::this_thread::sleep_for
(delay);
delay *=
3
;
}
return
false
;
#
else
//
临时文件与目标文件位于同一文件系统时,rename 提供原子替换。
std::filesystem::rename
(source, destination, ec);
return
!ec;
#
endif
}
//
── FileLock ──────────────────────────────────────────────────────────────
#
if
defined(_WIN32)
std::optional<FileLock>
FileLock::try_acquire
(
const
std::filesystem::path& dir) {
std::error_code ec;
std::filesystem::create_directories
(dir, ec);
auto
lockPath = dir /
"
.lock
"
;
HANDLE
h =
CreateFileW
(lockPath.
wstring
().
c_str
(),
GENERIC_READ
|
GENERIC_WRITE
,
FILE_SHARE_READ
|
FILE_SHARE_WRITE
,
NULL
,
OPEN_ALWAYS
,
FILE_ATTRIBUTE_NORMAL
,
NULL
);
if
(h ==
INVALID_HANDLE_VALUE
)
return
std::
nullopt
;
OVERLAPPED
ov = {};
if
(!
LockFileEx
(h,
LOCKFILE_EXCLUSIVE_LOCK
|
LOCKFILE_FAIL_IMMEDIATELY
,
0
,
1
,
0
, &ov)) {
CloseHandle
(h);
return
std::
nullopt
;
}
return
FileLock{h};
}
FileLock::~FileLock
() {
if
(handle_ !=
INVALID_HANDLE_VALUE
) {
OVERLAPPED
ov = {};
UnlockFileEx
(
static_cast
<
HANDLE
>(handle_),
0
,
1
,
0
, &ov);
CloseHandle
(
static_cast
<
HANDLE
>(handle_));
}
}
FileLock::FileLock
(FileLock&& o)
noexcept
: handle_(o.handle_) {
o.
handle_
=
INVALID_HANDLE_VALUE
;
}
FileLock& FileLock::
operator
=(FileLock&& o)
noexcept
{
if
(
this
!= &o) {
if
(handle_ !=
INVALID_HANDLE_VALUE
) {
OVERLAPPED
ov = {};
UnlockFileEx
(
static_cast
<
HANDLE
>(handle_),
0
,
1
,
0
, &ov);
CloseHandle
(
static_cast
<
HANDLE
>(handle_));
}
handle_ = o.
handle_
;
o.
handle_
=
INVALID_HANDLE_VALUE
;
}
return
*
this
;
}
#
else
//
POSIX
std::optional<FileLock>
FileLock::try_acquire
(
const
std::filesystem::path& dir) {
std::error_code ec;
std::filesystem::create_directories
(dir, ec);
auto
lockPath = dir /
"
.lock
"
;
int
fd = ::
open
(lockPath.
c_str
(),
O_CREAT
|
O_RDWR
|
O_CLOEXEC
,
0644
);
if
(fd <
0
)
return
std::
nullopt
;
if
(::
flock
(fd,
LOCK_EX
|
LOCK_NB
) !=
0
) {
::close
(fd);
return
std::
nullopt
;
}
return
FileLock{fd};
}
FileLock::~FileLock
() {
if
(fd_ >=
0
) {
::flock
(fd_,
LOCK_UN
);
::close
(fd_);
}
}
FileLock::FileLock
(FileLock&& o)
noexcept
: fd_(o.fd_) {
o.
fd_
= -
1
;
}
FileLock& FileLock::
operator
=(FileLock&& o)
noexcept
{
if
(
this
!= &o) {
if
(fd_ >=
0
) {
::flock
(fd_,
LOCK_UN
);
::close
(fd_);
}
fd_ = o.
fd_
;
o.
fd_
= -
1
;
}
return
*
this
;
}
#
endif
}
//
namespace mcpp::platform::fs
Back
|
FazBrowse Home
|
New Git URL