Boost.AFIO
v2.00 early alpha
|
A handle to a regular file or device, kept data layout compatible with async_file_handle. More...
#include "file_handle.hpp"
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_handle & | operator= (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_handle > | clone () const noexcept |
virtual result< path_type > | relink (path_type newpath) noexcept |
virtual result< void > | unlink () noexcept |
io_service * | service () 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_guard > | lock (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_guard > | lock (io_request< buffers_type > reqs, deadline d=deadline()) noexcept |
result< extent_guard > | lock (io_request< const_buffers_type > reqs, deadline d=deadline()) noexcept |
result< extent_guard > | try_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_handle > | file (path_type _path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept |
static result< file_handle > | random_file (path_type dirpath, mode _mode=mode::write, caching _caching=caching::temporary, flag flags=flag::none) noexcept |
static result< 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) noexcept |
static result< file_handle > | temp_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 |
A handle to a regular file or device, kept data layout compatible with async_file_handle.
|
pure virtualnoexceptinherited |
Issue a write reordering barrier such that writes preceding the barrier will reach storage before writes after this barrier.
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.reqs | A 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_device | True if you want the call to wait until data reaches storage and that storage has acknowledged the data is physically written. Slow. |
and_metadata | True if you want the call to sync the metadata for retrieving the writes before the barrier after a sudden power loss event. Slow. |
d | An 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. |
|
inlinevirtualnoexcept |
Clone this handle (copy constructor is disabled to avoid accidental copying)
|
inlinestaticnoexcept |
Create a file handle opening access to a file on path
|
inlinevirtualnoexcept |
Return the current maximum permitted extent of the file.
|
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.
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.offset | The 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. |
bytes | The 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). |
exclusive | Whether the lock is to be exclusive. |
d | An optional deadline by which the lock must complete, else it is cancelled. |
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).
|
inlinenoexceptinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inlinenoexceptinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
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.
|
inlinevirtualnoexceptinherited |
Read data from the open handle.
reqs | A scatter-gather and offset request. |
d | An 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. |
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).
|
inlinenoexceptinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
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.
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.newpath | The optionally partial new path to relink to. The current path is used as a base for any relative paths specified. |
|
inlinevirtualnoexceptinherited |
Changes whether this handle is append only or not.
|
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.
|
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.
temp_inode()
instead, it is far more secure.
|
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).
|
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.
newsize | The bytes to truncate the file to. |
|
inlinenoexceptinherited |
|
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.
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.
|
inlinevirtualnoexceptinherited |
Unlocks a byte range previously locked.
offset | The offset to unlock. This should be an offset previously locked. |
bytes | The number of bytes to unlock. This should be a byte extent previously locked. |
|
inlinevirtualnoexceptinherited |
Write data to the open handle.
reqs | A scatter-gather and offset request. |
d | An 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. |
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).
|
inlinenoexceptinherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.