Skip to content

Commit 297f0d1

Browse files
authored
[wpiutil] Change kInvalidFile to macro (#7750)
This is needed on Windows because accessing global variables across shared library boundaries doesn’t work.
1 parent ad29d45 commit 297f0d1

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

wpiutil/src/main/native/cpp/DataLogBackgroundWriter.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ struct DataLogBackgroundWriter::WriterThreadState {
183183
~WriterThreadState() { Close(); }
184184

185185
void Close() {
186-
if (f != fs::kInvalidFile) {
186+
if (f != WPI_kInvalidFile) {
187187
fs::CloseFile(f);
188-
f = fs::kInvalidFile;
188+
f = WPI_kInvalidFile;
189189
}
190190
}
191191

@@ -207,7 +207,7 @@ struct DataLogBackgroundWriter::WriterThreadState {
207207
std::string baseFilename;
208208
std::string filename;
209209
fs::path path;
210-
fs::file_t f = fs::kInvalidFile;
210+
fs::file_t f = WPI_kInvalidFile;
211211
uintmax_t freeSpace = UINTMAX_MAX;
212212
int segmentCount = 1;
213213
};
@@ -264,7 +264,7 @@ void DataLogBackgroundWriter::StartLogFile(WriterThreadState& state) {
264264
}
265265
}
266266

267-
if (state.f == fs::kInvalidFile) {
267+
if (state.f == WPI_kInvalidFile) {
268268
WPI_ERROR(m_msglog, "Could not open log file, no log being saved");
269269
} else {
270270
WPI_INFO(m_msglog, "Logging to '{}' ({} free space)", state.path.string(),
@@ -273,7 +273,7 @@ void DataLogBackgroundWriter::StartLogFile(WriterThreadState& state) {
273273
}
274274

275275
// start file
276-
if (state.f != fs::kInvalidFile) {
276+
if (state.f != WPI_kInvalidFile) {
277277
StartFile();
278278
}
279279
}
@@ -347,7 +347,7 @@ void DataLogBackgroundWriter::WriterThreadMain(std::string_view dir) {
347347
written = 0;
348348
}
349349

350-
if (!m_newFilename.empty() && state.f != fs::kInvalidFile) {
350+
if (!m_newFilename.empty() && state.f != WPI_kInvalidFile) {
351351
auto newFilename = std::move(m_newFilename);
352352
m_newFilename.clear();
353353
// rename
@@ -374,7 +374,7 @@ void DataLogBackgroundWriter::WriterThreadMain(std::string_view dir) {
374374
continue;
375375
}
376376

377-
if (state.f != fs::kInvalidFile && !blocked) {
377+
if (state.f != WPI_kInvalidFile && !blocked) {
378378
lock.unlock();
379379

380380
// update free space every 10 flushes (in case other things are writing)

wpiutil/src/main/native/cpp/fs.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ namespace fs {
5959
#pragma warning(disable : 4244 4267 4146)
6060
#endif
6161

62-
const file_t kInvalidFile = INVALID_HANDLE_VALUE;
63-
6462
static DWORD nativeDisposition(CreationDisposition Disp, OpenFlags Flags) {
6563
switch (Disp) {
6664
case CD_CreateAlways:
@@ -107,12 +105,12 @@ static file_t openFileInternal(const path& Path, std::error_code& EC,
107105
// This only runs if we failed to open the file, so there is probably
108106
// no performances issues.
109107
if (LastError != ERROR_ACCESS_DENIED) {
110-
return kInvalidFile;
108+
return WPI_kInvalidFile;
111109
}
112110
if (is_directory(Path)) {
113111
EC = std::make_error_code(std::errc::is_a_directory);
114112
}
115-
return kInvalidFile;
113+
return WPI_kInvalidFile;
116114
}
117115
EC = std::error_code();
118116
return H;
@@ -156,14 +154,14 @@ file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
156154
DWORD LastError = ::GetLastError();
157155
::CloseHandle(Result);
158156
EC = wpi::mapWindowsError(LastError);
159-
return kInvalidFile;
157+
return WPI_kInvalidFile;
160158
}
161159
}
162160

163161
if (Flags & OF_Delete) {
164162
if ((EC = setDeleteDisposition(Result, true))) {
165163
::CloseHandle(Result);
166-
return kInvalidFile;
164+
return WPI_kInvalidFile;
167165
}
168166
}
169167
return Result;
@@ -174,7 +172,7 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC, OpenFlags Flags) {
174172
}
175173

176174
int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
177-
if (F == kInvalidFile) {
175+
if (F == WPI_kInvalidFile) {
178176
EC = wpi::mapWindowsError(ERROR_INVALID_HANDLE);
179177
return -1;
180178
}
@@ -196,19 +194,17 @@ int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
196194
}
197195

198196
EC = std::error_code();
199-
F = kInvalidFile;
197+
F = WPI_kInvalidFile;
200198
return ResultFD;
201199
}
202200

203201
void CloseFile(file_t& F) {
204202
::CloseHandle(F);
205-
F = kInvalidFile;
203+
F = WPI_kInvalidFile;
206204
}
207205

208206
#else // _WIN32
209207

210-
const file_t kInvalidFile = -1;
211-
212208
static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
213209
FileAccess Access) {
214210
int Result = 0;
@@ -248,14 +244,14 @@ static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
248244
file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
249245
FileAccess Access, OpenFlags Flags, unsigned Mode) {
250246
int OpenFlags = nativeOpenFlags(Disp, Flags, Access);
251-
file_t ResultFD = kInvalidFile;
247+
file_t ResultFD = WPI_kInvalidFile;
252248

253249
// Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
254250
// when open is overloaded, such as in Bionic.
255251
auto Open = [&]() { return ::open(Path.c_str(), OpenFlags, Mode); };
256252
if ((ResultFD = wpi::sys::RetryAfterSignal(-1, Open)) < 0) {
257253
EC = std::error_code(errno, std::generic_category());
258-
return kInvalidFile;
254+
return WPI_kInvalidFile;
259255
}
260256
#ifndef O_CLOEXEC
261257
if (!(Flags & OF_ChildInherit)) {
@@ -274,14 +270,14 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC, OpenFlags Flags) {
274270

275271
int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
276272
int fd = F;
277-
F = kInvalidFile;
273+
F = WPI_kInvalidFile;
278274
EC = std::error_code();
279275
return fd;
280276
}
281277

282278
void CloseFile(file_t& F) {
283279
::close(F);
284-
F = kInvalidFile;
280+
F = WPI_kInvalidFile;
285281
}
286282

287283
#endif // _WIN32

wpiutil/src/main/native/include/wpi/fs.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ using fstream = std::fstream;
2828
#if defined(_WIN32)
2929
// A Win32 HANDLE is a typedef of void*
3030
using file_t = void*;
31+
#define WPI_kInvalidFile reinterpret_cast<fs::file_t>(-1)
3132
#else
3233
using file_t = int;
34+
#define WPI_kInvalidFile -1
3335
#endif
3436

35-
extern const file_t kInvalidFile;
36-
3737
enum CreationDisposition : unsigned {
3838
/// CD_CreateAlways - When opening a file:
3939
/// * If it already exists, truncate it.
@@ -139,7 +139,7 @@ file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
139139
* opened in, for example, read-write or in write-only mode.
140140
* @param Mode The access permissions of the file, represented in octal.
141141
* @returns a platform-specific file descriptor if \a Name has been opened,
142-
* otherwise kInvalidFile.
142+
* otherwise WPI_kInvalidFile.
143143
*/
144144
inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
145145
CreationDisposition Disp, OpenFlags Flags,
@@ -162,7 +162,7 @@ inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
162162
* opened in, for example, read-write or in write-only mode.
163163
* @param Mode The access permissions of the file, represented in octal.
164164
* @return a platform-specific file descriptor if \a Name has been opened,
165-
* otherwise kInvalidFile.
165+
* otherwise WPI_kInvalidFile.
166166
*/
167167
inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
168168
CreationDisposition Disp, OpenFlags Flags,
@@ -181,7 +181,7 @@ inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
181181
* @param EC Error code output, set to non-zero on error
182182
* @param Flags Additional flags
183183
* @return a platform-specific file descriptor if \a Name has been opened,
184-
* otherwise kInvalidFile.
184+
* otherwise WPI_kInvalidFile.
185185
*/
186186
file_t OpenFileForRead(const path& Path, std::error_code& EC,
187187
OpenFlags Flags = OF_None);
@@ -191,7 +191,7 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC,
191191
* must be closed with ::close() instead of CloseFile().
192192
*
193193
* @param F On input, this is the file to convert to a file descriptor.
194-
* On output, the file is set to kInvalidFile.
194+
* On output, the file is set to WPI_kInvalidFile.
195195
* @param EC Error code output, set to non-zero on error
196196
* @param Flags Flags passed to the OpenFile function that created file_t
197197
* @return file descriptor, or -1 on error
@@ -202,7 +202,7 @@ int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags);
202202
* Closes the file object.
203203
*
204204
* @param F On input, this is the file to close. On output, the file is
205-
* set to kInvalidFile.
205+
* set to WPI_kInvalidFile.
206206
*/
207207
void CloseFile(file_t& F);
208208

0 commit comments

Comments
 (0)