[wpiutil] Change kInvalidFile to macro (#7750)

This is needed on Windows because accessing global variables across shared library boundaries doesn’t work.
This commit is contained in:
DeltaDizzy
2025-01-30 22:01:35 -05:00
committed by GitHub
parent ad29d45dfb
commit 297f0d1b03
3 changed files with 25 additions and 29 deletions

View File

@@ -183,9 +183,9 @@ struct DataLogBackgroundWriter::WriterThreadState {
~WriterThreadState() { Close(); }
void Close() {
if (f != fs::kInvalidFile) {
if (f != WPI_kInvalidFile) {
fs::CloseFile(f);
f = fs::kInvalidFile;
f = WPI_kInvalidFile;
}
}
@@ -207,7 +207,7 @@ struct DataLogBackgroundWriter::WriterThreadState {
std::string baseFilename;
std::string filename;
fs::path path;
fs::file_t f = fs::kInvalidFile;
fs::file_t f = WPI_kInvalidFile;
uintmax_t freeSpace = UINTMAX_MAX;
int segmentCount = 1;
};
@@ -264,7 +264,7 @@ void DataLogBackgroundWriter::StartLogFile(WriterThreadState& state) {
}
}
if (state.f == fs::kInvalidFile) {
if (state.f == WPI_kInvalidFile) {
WPI_ERROR(m_msglog, "Could not open log file, no log being saved");
} else {
WPI_INFO(m_msglog, "Logging to '{}' ({} free space)", state.path.string(),
@@ -273,7 +273,7 @@ void DataLogBackgroundWriter::StartLogFile(WriterThreadState& state) {
}
// start file
if (state.f != fs::kInvalidFile) {
if (state.f != WPI_kInvalidFile) {
StartFile();
}
}
@@ -347,7 +347,7 @@ void DataLogBackgroundWriter::WriterThreadMain(std::string_view dir) {
written = 0;
}
if (!m_newFilename.empty() && state.f != fs::kInvalidFile) {
if (!m_newFilename.empty() && state.f != WPI_kInvalidFile) {
auto newFilename = std::move(m_newFilename);
m_newFilename.clear();
// rename
@@ -374,7 +374,7 @@ void DataLogBackgroundWriter::WriterThreadMain(std::string_view dir) {
continue;
}
if (state.f != fs::kInvalidFile && !blocked) {
if (state.f != WPI_kInvalidFile && !blocked) {
lock.unlock();
// update free space every 10 flushes (in case other things are writing)

View File

@@ -59,8 +59,6 @@ namespace fs {
#pragma warning(disable : 4244 4267 4146)
#endif
const file_t kInvalidFile = INVALID_HANDLE_VALUE;
static DWORD nativeDisposition(CreationDisposition Disp, OpenFlags Flags) {
switch (Disp) {
case CD_CreateAlways:
@@ -107,12 +105,12 @@ static file_t openFileInternal(const path& Path, std::error_code& EC,
// This only runs if we failed to open the file, so there is probably
// no performances issues.
if (LastError != ERROR_ACCESS_DENIED) {
return kInvalidFile;
return WPI_kInvalidFile;
}
if (is_directory(Path)) {
EC = std::make_error_code(std::errc::is_a_directory);
}
return kInvalidFile;
return WPI_kInvalidFile;
}
EC = std::error_code();
return H;
@@ -156,14 +154,14 @@ file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
DWORD LastError = ::GetLastError();
::CloseHandle(Result);
EC = wpi::mapWindowsError(LastError);
return kInvalidFile;
return WPI_kInvalidFile;
}
}
if (Flags & OF_Delete) {
if ((EC = setDeleteDisposition(Result, true))) {
::CloseHandle(Result);
return kInvalidFile;
return WPI_kInvalidFile;
}
}
return Result;
@@ -174,7 +172,7 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC, OpenFlags Flags) {
}
int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
if (F == kInvalidFile) {
if (F == WPI_kInvalidFile) {
EC = wpi::mapWindowsError(ERROR_INVALID_HANDLE);
return -1;
}
@@ -196,19 +194,17 @@ int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
}
EC = std::error_code();
F = kInvalidFile;
F = WPI_kInvalidFile;
return ResultFD;
}
void CloseFile(file_t& F) {
::CloseHandle(F);
F = kInvalidFile;
F = WPI_kInvalidFile;
}
#else // _WIN32
const file_t kInvalidFile = -1;
static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
FileAccess Access) {
int Result = 0;
@@ -248,14 +244,14 @@ static int nativeOpenFlags(CreationDisposition Disp, OpenFlags Flags,
file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
FileAccess Access, OpenFlags Flags, unsigned Mode) {
int OpenFlags = nativeOpenFlags(Disp, Flags, Access);
file_t ResultFD = kInvalidFile;
file_t ResultFD = WPI_kInvalidFile;
// Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
// when open is overloaded, such as in Bionic.
auto Open = [&]() { return ::open(Path.c_str(), OpenFlags, Mode); };
if ((ResultFD = wpi::sys::RetryAfterSignal(-1, Open)) < 0) {
EC = std::error_code(errno, std::generic_category());
return kInvalidFile;
return WPI_kInvalidFile;
}
#ifndef O_CLOEXEC
if (!(Flags & OF_ChildInherit)) {
@@ -274,14 +270,14 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC, OpenFlags Flags) {
int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags) {
int fd = F;
F = kInvalidFile;
F = WPI_kInvalidFile;
EC = std::error_code();
return fd;
}
void CloseFile(file_t& F) {
::close(F);
F = kInvalidFile;
F = WPI_kInvalidFile;
}
#endif // _WIN32