Boost.AFIO  v2.00 early alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
boost::afio::v2_xxx::file_handle Class Referenceabstract

A handle to a regular file or device, kept data layout compatible with async_file_handle. More...

#include "file_handle.hpp"

Inheritance diagram for boost::afio::v2_xxx::file_handle:
boost::afio::v2_xxx::io_handle boost::afio::v2_xxx::handle boost::afio::v2_xxx::async_file_handle

Public Types

using dev_t = uint64_t
 
using ino_t = uint64_t
 
using path_type = io_handle::path_type
 
using extent_type = io_handle::extent_type
 
using size_type = io_handle::size_type
 
using unique_id_type = io_handle::unique_id_type
 
using mode = io_handle::mode
 
using creation = io_handle::creation
 
using caching = io_handle::caching
 
using flag = io_handle::flag
 
using buffer_type = io_handle::buffer_type
 
using const_buffer_type = io_handle::const_buffer_type
 
using buffers_type = io_handle::buffers_type
 
using const_buffers_type = io_handle::const_buffers_type
 
template<class T >
using io_request = io_handle::io_request< T >
 
template<class T >
using io_result = io_handle::io_result< T >
 

Public Member Functions

 file_handle ()
 Default constructor.
 
 file_handle (native_handle_type h, dev_t devid, ino_t inode, path_type path, caching caching=caching::none, flag flags=flag::none)
 Construct a handle from a supplied native handle.
 
 file_handle (file_handle &&o) noexcept
 Implicit move construction of file_handle permitted.
 
 file_handle (handle &&o, path_type path, dev_t devid, ino_t inode) noexcept
 Explicit conversion from handle and io_handle permitted.
 
file_handleoperator= (file_handle &&o) noexcept
 Move assignment of file_handle permitted.
 
void swap (file_handle &o) noexcept
 Swap with another instance.
 
dev_t st_dev () const noexcept
 Unless flag::disable_safety_unlinks is set, the device id of the file when opened.
 
ino_t st_ino () const noexcept
 Unless flag::disable_safety_unlinks is set, the inode of the file when opened. When combined with st_dev(), forms a unique identifer on this system.
 
virtual unique_id_type unique_id () const noexceptoverride
 A unique identifier for this handle in this process (native handle). Subclasses like file_handle make this a unique identifier across the entire system.
 
virtual path_type path () const noexceptoverride
 The path this handle refers to, if any.
 
virtual result< void > close () noexceptoverride
 Immediately close the native handle type managed by this handle.
 
virtual io_result
< const_buffers_type > 
barrier (io_request< const_buffers_type > reqs=io_request< const_buffers_type >(), bool wait_for_device=false, bool and_metadata=false, deadline d=deadline()) noexceptoverride
 
virtual result< file_handleclone () const noexcept
 
virtual result< path_type > relink (path_type newpath) noexcept
 
virtual result< void > unlink () noexcept
 
io_serviceservice () const noexcept
 The i/o service this handle is attached to, if any.
 
virtual result< extent_type > length () const noexcept
 
virtual result< extent_type > truncate (extent_type newsize) noexcept
 
virtual io_result< buffers_type > read (io_request< buffers_type > reqs, deadline d=deadline()) noexcept
 Read data from the open handle. More...
 
io_result< buffer_type > read (extent_type offset, char *data, size_type bytes, deadline d=deadline()) noexcept
 
virtual io_result
< const_buffers_type > 
write (io_request< const_buffers_type > reqs, deadline d=deadline()) noexcept
 Write data to the open handle. More...
 
io_result< const_buffer_type > write (extent_type offset, const char *data, size_type bytes, deadline d=deadline()) noexcept
 
virtual io_result
< const_buffers_type > 
barrier (io_request< const_buffers_type > reqs=io_request< const_buffers_type >(), bool wait_for_device=false, bool and_metadata=false, deadline d=deadline()) noexcept=0
 Issue a write reordering barrier such that writes preceding the barrier will reach storage before writes after this barrier. More...
 
virtual result< extent_guardlock (extent_type offset, extent_type bytes, bool exclusive=true, deadline d=deadline()) noexcept
 Tries to lock the range of bytes specified for shared or exclusive access. Be aware this passes through the same semantics as the underlying OS call, including any POSIX insanity present on your platform. More...
 
result< extent_guardlock (io_request< buffers_type > reqs, deadline d=deadline()) noexcept
 
result< extent_guardlock (io_request< const_buffers_type > reqs, deadline d=deadline()) noexcept
 
result< extent_guardtry_lock (extent_type offset, extent_type bytes, bool exclusive=true) noexcept
 
virtual void unlock (extent_type offset, extent_type bytes) noexcept
 Unlocks a byte range previously locked. More...
 
void swap (handle &o) noexcept
 Swap with another instance.
 
virtual native_handle_type release () noexcept
 Release the native handle type managed by this handle.
 
bool is_valid () const noexcept
 True if the handle is valid (and usually open)
 
bool is_readable () const noexcept
 True if the handle is readable.
 
bool is_writable () const noexcept
 True if the handle is writable.
 
bool is_append_only () const noexcept
 True if the handle is append only.
 
virtual result< void > set_append_only (bool enable) noexcept
 
bool is_overlapped () const noexcept
 True if overlapped.
 
bool is_seekable () const noexcept
 True if seekable.
 
bool requires_aligned_io () const noexcept
 True if requires aligned i/o.
 
bool is_regular () const noexcept
 True if a regular file or device.
 
bool is_directory () const noexcept
 True if a directory.
 
bool is_symlink () const noexcept
 True if a symlink.
 
bool is_multiplexer () const noexcept
 True if a multiplexer like BSD kqueues, Linux epoll or Windows IOCP.
 
bool is_process () const noexcept
 True if a process.
 
bool is_section () const noexcept
 True if a memory section.
 
caching kernel_caching () const noexcept
 Kernel cache strategy used by this handle.
 
bool are_reads_from_cache () const noexcept
 True if the handle uses the kernel page cache for reads.
 
bool are_writes_durable () const noexcept
 True if writes are safely on storage on completion.
 
bool are_safety_fsyncs_issued () const noexcept
 True if issuing safety fsyncs is on.
 
virtual result< void > set_kernel_caching (caching caching) noexcept
 
flag flags () const noexcept
 The flags this handle was opened with.
 
native_handle_type native_handle () const noexcept
 The native handle used by this handle.
 

Static Public Member Functions

static result< file_handlefile (path_type _path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept
 
static result< file_handlerandom_file (path_type dirpath, mode _mode=mode::write, caching _caching=caching::temporary, flag flags=flag::none) noexcept
 
static result< file_handletemp_file (path_type name=path_type(), mode _mode=mode::write, creation _creation=creation::if_needed, caching _caching=caching::temporary, flag flags=flag::unlink_on_close) noexcept
 
static result< file_handletemp_inode (path_type dirpath=fixme_temporary_files_directory(), mode _mode=mode::write, flag flags=flag::none) noexcept
 

Protected Member Functions

result< void > _fetch_inode () noexcept
 Fill in _devid and _inode from the handle via fstat()
 

Protected Attributes

dev_t _devid
 
ino_t _inode
 
path_type _path
 
io_service_service
 
caching _caching
 
flag _flags
 
native_handle_type _v
 

Detailed Description

A handle to a regular file or device, kept data layout compatible with async_file_handle.

Todo:
file_handle needs to be split into a pathed_handle for the file and directory common parts

Member Function Documentation

virtual io_result<const_buffers_type> boost::afio::v2_xxx::io_handle::barrier ( io_request< const_buffers_type reqs = io_requestconst_buffers_type >(),
bool  wait_for_device = false,
bool  and_metadata = false,
deadline  d = deadline() 
)
pure virtualnoexceptinherited

Issue a write reordering barrier such that writes preceding the barrier will reach storage before writes after this barrier.

Warning
Assume that this call is a no-op. It is not reliably implemented in many common use cases, for example if your code is running inside a LXC container, or if the user has mounted the filing system with non-default options. Instead open the handle with caching::reads which means that all writes form a strict sequential order not completing until acknowledged by the storage device. Filing system can and do use different algorithms to give much better performance with caching::reads, some (e.g. ZFS) spectacularly better.
Let me repeat again: consider this call to be a hint to poke the kernel with a stick to go start to do some work sooner rather than later. It may be ignored entirely.
For portability, you can only assume that barriers write order for a single handle instance. You cannot assume that barriers write order across multiple handles to the same inode, or across processes.
Returns
The buffers barriered, which may not be the buffers input. The size of each scatter-gather buffer is updated with the number of bytes of that buffer barriered.
Parameters
reqsA scatter-gather and offset request for what range to barrier. May be ignored on some platforms which always write barrier the entire file. Supplying a default initialised reqs write barriers the entire file.
wait_for_deviceTrue if you want the call to wait until data reaches storage and that storage has acknowledged the data is physically written. Slow.
and_metadataTrue if you want the call to sync the metadata for retrieving the writes before the barrier after a sudden power loss event. Slow.
dAn optional deadline by which the i/o must complete, else it is cancelled. Note function may return significantly after this deadline if the i/o takes long to cancel.
Errors returnable
Any of the values POSIX fdatasync() or Windows NtFlushBuffersFileEx() can return.
Memory Allocations
None.
virtual result<file_handle> boost::afio::v2_xxx::file_handle::clone ( ) const
inlinevirtualnoexcept

Clone this handle (copy constructor is disabled to avoid accidental copying)

Errors returnable
Any of the values POSIX dup() or DuplicateHandle() can return.
static result<file_handle> boost::afio::v2_xxx::file_handle::file ( path_type  _path,
mode  _mode = mode::read,
creation  _creation = creation::open_existing,
caching  _caching = caching::all,
flag  flags = flag::none 
)
inlinestaticnoexcept

Create a file handle opening access to a file on path

Errors returnable
Any of the values POSIX open() or CreateFile() can return.
virtual result<extent_type> boost::afio::v2_xxx::file_handle::length ( ) const
inlinevirtualnoexcept

Return the current maximum permitted extent of the file.

Errors returnable
Any of the values POSIX fstat() or GetFileInformationByHandleEx() can return.
virtual result<extent_guard> boost::afio::v2_xxx::io_handle::lock ( extent_type  offset,
extent_type  bytes,
bool  exclusive = true,
deadline  d = deadline() 
)
inlinevirtualnoexceptinherited

Tries to lock the range of bytes specified for shared or exclusive access. Be aware this passes through the same semantics as the underlying OS call, including any POSIX insanity present on your platform.

Warning
On older Linuxes and POSIX, this uses fcntl() with the well known insane POSIX semantics that closing ANY handle to this file releases all bytes range locks on it. If your OS isn't new enough to support the non-insane lock API, flag::byte_lock_insanity will be set in flags() after the first call to this function.
Returns
An extent guard, the destruction of which will call unlock().
Parameters
offsetThe offset to lock. Note that on POSIX the top bit is always cleared before use as POSIX uses signed transport for offsets. If you want an advisory rather than mandatory lock on Windows, one technique is to force top bit set so the region you lock is not the one you will i/o - obviously this reduces maximum file size to (2^63)-1.
bytesThe number of bytes to lock. Zero means lock the entire file using any more efficient alternative algorithm where available on your platform (specifically, on BSD and OS X use flock() for non-insane semantics).
exclusiveWhether the lock is to be exclusive.
dAn optional deadline by which the lock must complete, else it is cancelled.
Errors returnable
Any of the values POSIX fcntl() can return, errc::timed_out, errc::not_supported may be returned if deadline i/o is not possible with this particular handle configuration (e.g. non-overlapped HANDLE on Windows).
Memory Allocations
The default synchronous implementation in file_handle performs no memory allocation. The asynchronous implementation in async_file_handle performs one calloc and one free.
result<extent_guard> boost::afio::v2_xxx::io_handle::lock ( io_request< buffers_type reqs,
deadline  d = deadline() 
)
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

603  {
604  size_t bytes = 0;
605  for(auto &i : reqs.buffers)
606  bytes += i.second;
607  return lock(reqs.offset, bytes, false, std::move(d));
608  }
result<extent_guard> boost::afio::v2_xxx::io_handle::lock ( io_request< const_buffers_type reqs,
deadline  d = deadline() 
)
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

611  {
612  size_t bytes = 0;
613  for(auto &i : reqs.buffers)
614  bytes += i.second;
615  return lock(reqs.offset, bytes, true, std::move(d));
616  }
static result<file_handle> boost::afio::v2_xxx::file_handle::random_file ( path_type  dirpath,
mode  _mode = mode::write,
caching  _caching = caching::temporary,
flag  flags = flag::none 
)
inlinestaticnoexcept

Create a file handle creating a randomly named file on a path. The file is opened exclusively with creation::only_if_not_exist so it will never collide with nor overwrite any existing file. Note also that caching defaults to temporary which hints to the OS to only flush changes to physical storage as lately as possible.

Errors returnable
Any of the values POSIX open() or CreateFile() can return.
150  {
151  try
152  {
153  result<file_handle> ret;
154  do
155  {
156  auto randomname = utils::random_string(32);
157  randomname.append(".random");
158  ret = file(dirpath / randomname, _mode, creation::only_if_not_exist, _caching, flags);
159  if(!ret && ret.get_error().value() != EEXIST)
160  return ret;
161  } while(!ret);
162  return ret;
163  }
164  BOOST_OUTCOME_CATCH_ALL_EXCEPTION_TO_RESULT
165  }
virtual io_result<buffers_type> boost::afio::v2_xxx::io_handle::read ( io_request< buffers_type reqs,
deadline  d = deadline() 
)
inlinevirtualnoexceptinherited

Read data from the open handle.

Returns
The buffers read, which may not be the buffers input. The size of each scatter-gather buffer is updated with the number of bytes of that buffer transferred, and the pointer to the data may be completely different to what was submitted (e.g. it may point into a memory map).
Parameters
reqsA scatter-gather and offset request.
dAn optional deadline by which the i/o must complete, else it is cancelled. Note function may return significantly after this deadline if the i/o takes long to cancel.
Errors returnable
Any of the values POSIX read() can return, errc::timed_out, errc::operation_canceled. errc::not_supported may be returned if deadline i/o is not possible with this particular handle configuration (e.g. reading from regular files on POSIX or reading from a non-overlapped HANDLE on Windows).
Memory Allocations
The default synchronous implementation in file_handle performs no memory allocation. The asynchronous implementation in async_file_handle performs one calloc and one free.
io_result<buffer_type> boost::afio::v2_xxx::io_handle::read ( extent_type  offset,
char *  data,
size_type  bytes,
deadline  d = deadline() 
)
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

432  {
433  buffer_type _reqs[1] = {{data, bytes}};
434  io_request<buffers_type> reqs(buffers_type(_reqs), offset);
435  BOOST_OUTCOME_TRY(v, read(reqs, d));
436  return *v.data();
437  }
virtual result<path_type> boost::afio::v2_xxx::file_handle::relink ( path_type  newpath)
inlinevirtualnoexcept

Atomically relinks the current path of this open handle to the new path specified, atomically and silently replacing any item at the new path specified. This operation is both atomic and silent matching POSIX behaviour even on Microsoft Windows where no Win32 API can match POSIX semantics.

Warning
Some operating systems provide a race free syscall for renaming an open handle (Windows). On all other operating systems this call is racy and can result in the wrong file entry being deleted. Note that unless flag::disable_safety_unlinks is set, this implementation checks before relinking that the item about to be relinked has the same inode as the open file handle. This should prevent most unmalicious accidental loss of data.
Returns
The full new path of the relinked filesystem entry.
Parameters
newpathThe optionally partial new path to relink to. The current path is used as a base for any relative paths specified.
virtual result<void> boost::afio::v2_xxx::handle::set_append_only ( bool  enable)
inlinevirtualnoexceptinherited

Changes whether this handle is append only or not.

Warning
On Windows this is implemented as a bit of a hack to make it fast like on POSIX, so make sure you open the handle for read/write originally. Note unlike on POSIX the append_only disposition will be the only one toggled, seekable and readable will remain turned on.
Errors returnable
Whatever POSIX fcntl() returns. On Windows nothing is changed on the handle.
Memory Allocations
No memory allocation.
virtual result<void> boost::afio::v2_xxx::handle::set_kernel_caching ( caching  caching)
inlinevirtualnoexceptinherited

Changes the kernel cache strategy used by this handle. Note most OSs impose severe restrictions on what can be changed and will error out, it may be easier to simply create a new handle.

Warning
On Windows this reopens the file, it is no slower than opening the file fresh but equally it is vastly slower than on POSIX.
Errors returnable
Whatever POSIX fcntl() or ReOpenFile() returns.
Memory Allocations
No memory allocation.
static result<file_handle> boost::afio::v2_xxx::file_handle::temp_file ( path_type  name = path_type(),
mode  _mode = mode::write,
creation  _creation = creation::if_needed,
caching  _caching = caching::temporary,
flag  flags = flag::unlink_on_close 
)
inlinestaticnoexcept

Create a file handle creating the named file on some path which the OS declares to be suitable for temporary files. Most OSs are very lazy about flushing changes made to these temporary files. Note the default flags are to have the newly created file deleted on first handle close. Note also that an empty name is equivalent to calling random_file(fixme_temporary_files_directory()) and the creation parameter is ignored.

Note
If the temporary file you are creating is not going to have its path sent to another process for usage, this is the WRONG function to use. Use temp_inode() instead, it is far more secure.
Errors returnable
Any of the values POSIX open() or CreateFile() can return.
183  {
184  return name.empty() ? random_file(fixme_temporary_files_directory(), _mode, _caching, flags) : file(fixme_temporary_files_directory() / name, _mode, _creation, _caching, flags);
185  }
static result<file_handle> boost::afio::v2_xxx::file_handle::temp_inode ( path_type  dirpath = fixme_temporary_files_directory(),
mode  _mode = mode::write,
flag  flags = flag::none 
)
inlinestaticnoexcept

Securely create a file handle creating a temporary anonymous inode in the filesystem referred to by dirpath. The inode created has no name nor accessible path on the filing system and ceases to exist as soon as the last handle is closed, making it ideal for use as a temporary file where other processes do not need to have access to its contents via some path on the filing system (a classic use case is for backing shared memory maps).

Errors returnable
Any of the values POSIX open() or CreateFile() can return.
virtual result<extent_type> boost::afio::v2_xxx::file_handle::truncate ( extent_type  newsize)
inlinevirtualnoexcept

Resize the current maximum permitted extent of the file to the given extent, avoiding any new allocation of physical storage where supported. Note that on extents based filing systems this will succeed even if there is insufficient free space on the storage medium.

Returns
The bytes actually truncated to.
Parameters
newsizeThe bytes to truncate the file to.
Errors returnable
Any of the values POSIX ftruncate() or SetFileInformationByHandle() can return.
result<extent_guard> boost::afio::v2_xxx::io_handle::try_lock ( extent_type  offset,
extent_type  bytes,
bool  exclusive = true 
)
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

600 { return lock(offset, bytes, exclusive, deadline(stl11::chrono::seconds(0))); }
virtual result<void> boost::afio::v2_xxx::file_handle::unlink ( )
inlinevirtualnoexcept

Unlinks the current path of this open handle, causing its entry to immediately disappear from the filing system. On Windows unless flag::win_disable_unlink_emulation is set, this behaviour is simulated by renaming the file to something random and setting its delete-on-last-close flag. After the next handle to that file closes, it will become permanently unopenable by anyone else until the last handle is closed, whereupon the entry will be deleted by the operating system.

Warning
Some operating systems provide a race free syscall for unlinking an open handle (Windows). On all other operating systems this call is racy and can result in the wrong file entry being deleted. Note that unless flag::disable_safety_unlinks is set, this implementation checks before unlinking that the item about to be unlinked has the same inode as the open file handle. This should prevent most unmalicious accidental loss of data.
virtual void boost::afio::v2_xxx::io_handle::unlock ( extent_type  offset,
extent_type  bytes 
)
inlinevirtualnoexceptinherited

Unlocks a byte range previously locked.

Parameters
offsetThe offset to unlock. This should be an offset previously locked.
bytesThe number of bytes to unlock. This should be a byte extent previously locked.
Errors returnable
Any of the values POSIX fcntl() can return.
Memory Allocations
None.
virtual io_result<const_buffers_type> boost::afio::v2_xxx::io_handle::write ( io_request< const_buffers_type reqs,
deadline  d = deadline() 
)
inlinevirtualnoexceptinherited

Write data to the open handle.

Returns
The buffers written, which may not be the buffers input. The size of each scatter-gather buffer is updated with the number of bytes of that buffer transferred.
Parameters
reqsA scatter-gather and offset request.
dAn optional deadline by which the i/o must complete, else it is cancelled. Note function may return significantly after this deadline if the i/o takes long to cancel.
Errors returnable
Any of the values POSIX write() can return, errc::timed_out, errc::operation_canceled. errc::not_supported may be returned if deadline i/o is not possible with this particular handle configuration (e.g. writing to regular files on POSIX or writing to a non-overlapped HANDLE on Windows).
Memory Allocations
The default synchronous implementation in file_handle performs no memory allocation. The asynchronous implementation in async_file_handle performs one calloc and one free.
io_result<const_buffer_type> boost::afio::v2_xxx::io_handle::write ( extent_type  offset,
const char *  data,
size_type  bytes,
deadline  d = deadline() 
)
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

456  {
457  const_buffer_type _reqs[1] = {{data, bytes}};
458  io_request<const_buffers_type> reqs(const_buffers_type(_reqs), offset);
459  BOOST_OUTCOME_TRY(v, write(reqs, d));
460  return *v.data();
461  }

The documentation for this class was generated from the following file: