Repository: fmtlib/fmt
File: src/os.cc (C++)

Code before:
  int result = FMT_SYSTEM(fclose(file_));
  file_ = nullptr;
  if (result != 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
}

int buffered_file::descriptor() const {
  int fd = FMT_POSIX_CALL(fileno(file_));
  if (fd == -1)
    FMT_THROW(system_error(errno, FMT_STRING("cannot get file descriptor")));
  return fd;
}

#if FMT_USE_FCNTL
#  ifdef _WIN32
using mode_t = int;
#  endif
constexpr mode_t default_open_mode =
    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

file::file(cstring_view path, int oflag) {
#  if defined(_WIN32) && !defined(__MINGW32__)
  fd_ = -1;
  auto converted = detail::utf8_to_utf16(string_view(path.c_str()));
  auto err =
      _wsopen_s(&fd_, converted.c_str(), oflag, _SH_DENYNO, default_open_mode);
#  else
  FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, default_open_mode)));
  auto err = errno;
#  endif
  if (fd_ == -1)
    FMT_THROW(
        system_error(err, FMT_STRING("cannot open file {}"), path.c_str()));
}

file::~file() noexcept {
  // Don't retry close in case of EINTR!
  // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
  if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
    report_system_error(errno, "cannot close file");
}

void file::close() {
  if (fd_ == -1) return;
  // Don't retry close in case of EINTR!
  // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
  int result = FMT_POSIX_CALL(close(fd_));
  fd_ = -1;
  if (result != 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
}

Review comment (suggestion):
I think we could avoid a bit of duplication by calling `open_windows_file` here and moving its result into `*this`.

Code after:
  int result = FMT_SYSTEM(fclose(file_));
  file_ = nullptr;
  if (result != 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
}

int buffered_file::descriptor() const {
  int fd = FMT_POSIX_CALL(fileno(file_));
  if (fd == -1)
    FMT_THROW(system_error(errno, FMT_STRING("cannot get file descriptor")));
  return fd;
}

#if FMT_USE_FCNTL
#  ifdef _WIN32
using mode_t = int;
#  endif
constexpr mode_t default_open_mode =
    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

file::file(cstring_view path, int oflag) {
#  if defined(_WIN32) && !defined(__MINGW32__)
  fd_ = -1;
  auto converted = detail::utf8_to_utf16(string_view(path.c_str()));
  *this = file::open_windows_file(converted.c_str(), oflag);
#  else
  FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, default_open_mode)));
  if (fd_ == -1)
    FMT_THROW(
        system_error(errno, FMT_STRING("cannot open file {}"), path.c_str()));
#  endif
}

file::~file() noexcept {
  // Don't retry close in case of EINTR!
  // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
  if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
    report_system_error(errno, "cannot close file");
}

void file::close() {
  if (fd_ == -1) return;
  // Don't retry close in case of EINTR!
  // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
  int result = FMT_POSIX_CALL(close(fd_));
  fd_ = -1;
  if (result != 0)
    FMT_THROW(system_error(errno, FMT_STRING("cannot close file")));
}

long long file::size() const {