Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpiutil] use reinterpret_cast for kInvalidFile on Win32 via macro #7750

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions wpiutil/src/main/native/cpp/DataLogBackgroundWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
};
Expand Down Expand Up @@ -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(),
Expand All @@ -273,7 +273,7 @@ void DataLogBackgroundWriter::StartLogFile(WriterThreadState& state) {
}

// start file
if (state.f != fs::kInvalidFile) {
if (state.f != WPI_kInvalidFile) {
StartFile();
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
26 changes: 11 additions & 15 deletions wpiutil/src/main/native/cpp/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions wpiutil/src/main/native/include/wpi/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ using fstream = std::fstream;
#if defined(_WIN32)
// A Win32 HANDLE is a typedef of void*
using file_t = void*;
#define WPI_kInvalidFile reinterpret_cast<fs::file_t>(-1)
#else
using file_t = int;
#define WPI_kInvalidFile -1
#endif

extern const file_t kInvalidFile;

enum CreationDisposition : unsigned {
/// CD_CreateAlways - When opening a file:
/// * If it already exists, truncate it.
Expand Down Expand Up @@ -139,7 +139,7 @@ file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
* opened in, for example, read-write or in write-only mode.
* @param Mode The access permissions of the file, represented in octal.
* @returns a platform-specific file descriptor if \a Name has been opened,
* otherwise kInvalidFile.
* otherwise WPI_kInvalidFile.
*/
inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
CreationDisposition Disp, OpenFlags Flags,
Expand All @@ -162,7 +162,7 @@ inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
* opened in, for example, read-write or in write-only mode.
* @param Mode The access permissions of the file, represented in octal.
* @return a platform-specific file descriptor if \a Name has been opened,
* otherwise kInvalidFile.
* otherwise WPI_kInvalidFile.
*/
inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
CreationDisposition Disp, OpenFlags Flags,
Expand All @@ -181,7 +181,7 @@ inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
* @param EC Error code output, set to non-zero on error
* @param Flags Additional flags
* @return a platform-specific file descriptor if \a Name has been opened,
* otherwise kInvalidFile.
* otherwise WPI_kInvalidFile.
*/
file_t OpenFileForRead(const path& Path, std::error_code& EC,
OpenFlags Flags = OF_None);
Expand All @@ -191,7 +191,7 @@ file_t OpenFileForRead(const path& Path, std::error_code& EC,
* must be closed with ::close() instead of CloseFile().
*
* @param F On input, this is the file to convert to a file descriptor.
* On output, the file is set to kInvalidFile.
* On output, the file is set to WPI_kInvalidFile.
* @param EC Error code output, set to non-zero on error
* @param Flags Flags passed to the OpenFile function that created file_t
* @return file descriptor, or -1 on error
Expand All @@ -202,7 +202,7 @@ int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags);
* Closes the file object.
*
* @param F On input, this is the file to close. On output, the file is
* set to kInvalidFile.
* set to WPI_kInvalidFile.
*/
void CloseFile(file_t& F);

Expand Down
Loading