mirror of
https://github.com/boostorg/beast.git
synced 2026-05-04 11:44:27 +02:00
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:
@@ -16,6 +16,20 @@
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
/*
|
||||
|
||||
file_mode acesss sharing seeking file std mode
|
||||
--------------------------------------------------------------------------------------
|
||||
read read-only shared random must exist "rb"
|
||||
scan read-only shared sequential must exist "rbS"
|
||||
write read/write exclusive random create/truncate "wb+"
|
||||
write_new read/write exclusive random must not exist "wbx"
|
||||
write_existing read/write exclusive random must exist "rb+"
|
||||
append write-only exclusive sequential create/truncate "ab"
|
||||
append_existing write-only exclusive sequential must exist "ab"
|
||||
|
||||
*/
|
||||
|
||||
/** File open modes
|
||||
|
||||
These modes are used when opening files using
|
||||
@@ -25,28 +39,33 @@ namespace beast {
|
||||
*/
|
||||
enum class file_mode
|
||||
{
|
||||
/// Random reading
|
||||
/// Random read-only access to an existing file
|
||||
read,
|
||||
|
||||
/// Sequential reading
|
||||
/// Sequential read-only access to an existing file
|
||||
scan,
|
||||
|
||||
/** Random writing to a new or truncated file
|
||||
/** Random reading and writing to a new or truncated file
|
||||
|
||||
@li If the file does not exist, it is created.
|
||||
|
||||
@li If the file exists, it is truncated to
|
||||
zero size upon opening.
|
||||
This mode permits random-access reading and writing
|
||||
for the specified file. If the file does not exist
|
||||
prior to the function call, it is created with an
|
||||
initial size of zero bytes. Otherwise if the file
|
||||
already exists, the size is truncated to zero bytes.
|
||||
*/
|
||||
write,
|
||||
|
||||
/** Random writing to new file only
|
||||
/** Random reading and writing to a new file only
|
||||
|
||||
If the file exists, an error is generated.
|
||||
This mode permits random-access reading and writing
|
||||
for the specified file. The file will be created with
|
||||
an initial size of zero bytes. If the file already exists
|
||||
prior to the function call, an error is returned and
|
||||
no file is opened.
|
||||
*/
|
||||
write_new,
|
||||
|
||||
/** Random writing to existing file
|
||||
/** Random write-only access to existing file
|
||||
|
||||
If the file does not exist, an error is generated.
|
||||
*/
|
||||
@@ -64,15 +83,6 @@ enum class file_mode
|
||||
*/
|
||||
append,
|
||||
|
||||
/** Appending to a new file only
|
||||
|
||||
The current file position shall be set to the end of
|
||||
the file prior to each write.
|
||||
|
||||
If the file exists, an error is generated.
|
||||
*/
|
||||
append_new,
|
||||
|
||||
/** Appending to an existing file
|
||||
|
||||
The current file position shall be set to the end of
|
||||
|
||||
@@ -43,6 +43,11 @@ class file_posix
|
||||
{
|
||||
int fd_ = -1;
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
static
|
||||
int
|
||||
native_close(int& fd);
|
||||
|
||||
public:
|
||||
/** The type of the underlying file handle.
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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_;
|
||||
|
||||
Reference in New Issue
Block a user