Remove file_mode::append_new (API Change):

* Tidying
* Increase test coverage
* Fix file_mode::append_existing

API Changes:

* file_mode::append_new is removed, as it makes no sense

Actions Required:

* Replace file_mode::append_new with file_mode::append
  or file_mode::append_existing instead of file_mode::append_new
This commit is contained in:
Vinnie Falco committed 2018-12-19 21:51:49 -08:00
1 parent 321af29d25
commit 61a35bd936
11 files changed
+458 -163

No files matched your search

+27 -51
View File
@@ -36,30 +36,29 @@
namespace boost {
namespace beast {
namespace detail {
inline
int
file_posix_close(int fd)
file_posix::
native_close(native_handle_type& fd)
{
for(;;)
if(fd != -1)
{
if(! ::close(fd))
break;
int const ev = errno;
if(errno != EINTR)
return ev;
for(;;)
{
if(::close(fd) == 0)
break;
int const ev = errno;
if(errno != EINTR)
return ev;
}
fd = -1;
}
return 0;
}
} // detail
file_posix::
~file_posix()
{
if(fd_ != -1)
detail::file_posix_close(fd_);
native_close(fd_);
}
file_posix::
@@ -74,8 +73,7 @@ operator=(file_posix&& other)
{
if(&other == this)
return *this;
if(fd_ != -1)
detail::file_posix_close(fd_);
native_close(fd_);
fd_ = other.fd_;
other.fd_ = -1;
return *this;
@@ -85,8 +83,7 @@ void
file_posix::
native_handle(native_handle_type fd)
{
if(fd_ != -1)
detail::file_posix_close(fd_);
native_close(fd_);
fd_ = fd;
}
@@ -94,36 +91,23 @@ void
file_posix::
close(error_code& ec)
{
if(fd_ != -1)
{
auto const ev =
detail::file_posix_close(fd_);
if(ev)
ec.assign(ev, system_category());
else
ec = {};
fd_ = -1;
}
auto const ev = native_close(fd_);
if(ev)
ec.assign(ev, system_category());
else
{
ec = {};
}
}
void
file_posix::
open(char const* path, file_mode mode, error_code& ec)
{
if(fd_ != -1)
{
auto const ev =
detail::file_posix_close(fd_);
if(ev)
ec.assign(ev, system_category());
else
ec = {};
fd_ = -1;
}
auto const ev = native_close(fd_);
if(ev)
ec.assign(ev, system_category());
else
ec = {};
int f = 0;
#if BOOST_BEAST_USE_POSIX_FADVISE
int advise = 0;
@@ -166,21 +150,14 @@ open(char const* path, file_mode mode, error_code& ec)
break;
case file_mode::append:
f = O_RDWR | O_CREAT | O_TRUNC;
#if BOOST_BEAST_USE_POSIX_FADVISE
advise = POSIX_FADV_SEQUENTIAL;
#endif
break;
case file_mode::append_new:
f = O_RDWR | O_CREAT | O_EXCL;
f = O_WRONLY | O_CREAT | O_TRUNC;
#if BOOST_BEAST_USE_POSIX_FADVISE
advise = POSIX_FADV_SEQUENTIAL;
#endif
break;
case file_mode::append_existing:
f = O_RDWR | O_EXCL;
f = O_WRONLY;
#if BOOST_BEAST_USE_POSIX_FADVISE
advise = POSIX_FADV_SEQUENTIAL;
#endif
@@ -202,8 +179,7 @@ open(char const* path, file_mode mode, error_code& ec)
if(::posix_fadvise(fd_, 0, 0, advise))
{
auto const ev = errno;
detail::file_posix_close(fd_);
fd_ = -1;
native_close(fd_);
ec.assign(ev, system_category());
return;
}
+58 -9
View File
@@ -10,6 +10,7 @@
#ifndef BOOST_BEAST_CORE_IMPL_FILE_STDIO_HPP
#define BOOST_BEAST_CORE_IMPL_FILE_STDIO_HPP
#include <boost/config/workaround.hpp>
#include <boost/core/exchange.hpp>
#include <limits>
@@ -81,17 +82,65 @@ open(char const* path, file_mode mode, error_code& ec)
switch(mode)
{
default:
case file_mode::read: s = "rb"; break;
case file_mode::scan: s = "rb"; break;
case file_mode::write: s = "wb"; break;
case file_mode::write_new: s = "wbx"; break;
case file_mode::write_existing: s = "wb"; break;
case file_mode::append: s = "ab"; break;
case file_mode::append_new: s = "abx"; break;
case file_mode::append_existing: s = "ab"; break;
case file_mode::read:
s = "rb";
break;
case file_mode::scan:
#if BOOST_MSVC
s = "rbS";
#else
s = "rb";
#endif
break;
case file_mode::write:
s = "wb+";
break;
case file_mode::write_new:
{
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
auto const f0 = std::fopen(path, "rb");
if(f0)
{
std::fclose(f0);
ec = make_error_code(
errc::file_exists);
return;
}
s = "wb";
#else
s = "wbx";
#endif
break;
}
case file_mode::write_existing:
s = "rb+";
break;
case file_mode::append:
s = "ab";
break;
case file_mode::append_existing:
{
auto const f0 = std::fopen(path, "rb+");
if(! f0)
{
ec = make_error_code(
errc::no_such_file_or_directory);
return;
}
std::fclose(f0);
s = "ab";
break;
}
}
#if BOOST_MSVC
auto const ev = fopen_s(&f_, path, s);
auto const ev = ::fopen_s(&f_, path, s);
if(ev)
{
f_ = nullptr;
@@ -175,13 +175,6 @@ open(char const* path, file_mode mode, error_code& ec)
flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
break;
case file_mode::append_new:
desired_access = boost::winapi::GENERIC_READ_ |
boost::winapi::GENERIC_WRITE_;
creation_disposition = boost::winapi::CREATE_NEW_;
flags_and_attributes = 0x08000000; // FILE_FLAG_SEQUENTIAL_SCAN
break;
case file_mode::append_existing:
desired_access = boost::winapi::GENERIC_READ_ |
boost::winapi::GENERIC_WRITE_;