remove FMT_POSIX_CALL on wopen

This commit is contained in:
Fros1er 2023-02-14 11:52:36 +08:00
parent 4c4389589b
commit 68fa8e7bff
3 changed files with 14 additions and 28 deletions

View File

@ -213,22 +213,25 @@ int buffered_file::descriptor() const {
} }
#if FMT_USE_FCNTL #if FMT_USE_FCNTL
file::file(cstring_view path, int oflag) {
# ifdef _WIN32 # ifdef _WIN32
using mode_t = int; using mode_t = int;
# endif # endif
constexpr mode_t mode = constexpr mode_t default_open_mode =
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
file::file(cstring_view path, int oflag) {
# if defined(_WIN32) && !defined(__MINGW32__) # if defined(_WIN32) && !defined(__MINGW32__)
fd_ = -1; fd_ = -1;
auto converted = detail::utf8_to_utf16(string_view(path.c_str())); auto converted = detail::utf8_to_utf16(string_view(path.c_str()));
FMT_POSIX_CALL(wsopen_s(&fd_, converted.c_str(), oflag, _SH_DENYNO, mode)); auto err =
_wsopen_s(&fd_, converted.c_str(), oflag, _SH_DENYNO, default_open_mode);
# else # else
FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, mode))); FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, default_open_mode)));
auto err = errno;
# endif # endif
if (fd_ == -1) if (fd_ == -1)
FMT_THROW( FMT_THROW(
system_error(errno, FMT_STRING("cannot open file {}"), path.c_str())); system_error(err, FMT_STRING("cannot open file {}"), path.c_str()));
} }
file::~file() noexcept { file::~file() noexcept {
@ -357,12 +360,10 @@ buffered_file file::fdopen(const char* mode) {
# if defined(_WIN32) && !defined(__MINGW32__) # if defined(_WIN32) && !defined(__MINGW32__)
file file::open_windows_file(wcstring_view path, int oflag) { file file::open_windows_file(wcstring_view path, int oflag) {
int fd_ = -1; int fd_ = -1;
using mode_t = int; auto err =
constexpr mode_t mode = _wsopen_s(&fd_, path.c_str(), oflag, _SH_DENYNO, default_open_mode);
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
FMT_POSIX_CALL(wsopen_s(&fd_, path.c_str(), oflag, _SH_DENYNO, mode));
if (fd_ == -1) if (fd_ == -1)
FMT_THROW(system_error(errno, FMT_STRING("cannot open file {}"), FMT_THROW(system_error(err, FMT_STRING("cannot open file {}"),
detail::utf16_to_utf8(path.c_str()).c_str())); detail::utf16_to_utf8(path.c_str()).c_str()));
return file(fd_); return file(fd_);
} }

View File

@ -72,17 +72,6 @@ int test::open(const char* path, int oflag, int mode) {
EMULATE_EINTR(open, -1); EMULATE_EINTR(open, -1);
return ::open(path, oflag, mode); return ::open(path, oflag, mode);
} }
#else
errno_t test::sopen_s(int* pfh, const char* filename, int oflag, int shflag,
int pmode) {
EMULATE_EINTR(open, EINTR);
return _sopen_s(pfh, filename, oflag, shflag, pmode);
}
errno_t test::wsopen_s(int* pfh, const wchar_t* filename, int oflag, int shflag,
int pmode) {
EMULATE_EINTR(open, EINTR);
return _wsopen_s(pfh, filename, oflag, shflag, pmode);
}
#endif #endif
#ifndef _WIN32 #ifndef _WIN32
@ -225,11 +214,11 @@ TEST(os_test, getpagesize) {
} }
TEST(file_test, open_retry) { TEST(file_test, open_retry) {
# ifndef _WIN32
write_file("temp", "there must be something here"); write_file("temp", "there must be something here");
std::unique_ptr<file> f{nullptr}; std::unique_ptr<file> f{nullptr};
EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open, EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open,
"cannot open file temp"); "cannot open file temp");
# ifndef _WIN32
char c = 0; char c = 0;
f->read(&c, 1); f->read(&c, 1);
# endif # endif

View File

@ -37,10 +37,6 @@ int fstat(int fd, struct stat* buf);
#else #else
typedef unsigned size_t; typedef unsigned size_t;
typedef int ssize_t; typedef int ssize_t;
errno_t sopen_s(int* pfh, const char* filename, int oflag, int shflag,
int pmode);
errno_t wsopen_s(int* pfh, const wchar_t* filename, int oflag, int shflag,
int pmode);
#endif #endif
#ifndef _WIN32 #ifndef _WIN32