AFIO  v2.00 late alpha
afio_v2_xxx::handle Class Reference

A native_handle_type which is managed by the lifetime of this object instance. More...

#include "handle.hpp"

Inheritance diagram for afio_v2_xxx::handle:
afio_v2_xxx::io_handle afio_v2_xxx::path_handle afio_v2_xxx::section_handle afio_v2_xxx::file_handle afio_v2_xxx::map_handle afio_v2_xxx::directory_handle afio_v2_xxx::async_file_handle afio_v2_xxx::mapped_file_handle

Public Types

enum  mode : unsigned char {
  unchanged = 0, none = 2, mode::attr_read = 4, mode::attr_write = 5,
  mode::read = 6, mode::write = 7, mode::append = 9
}
 The behaviour of the handle: does it read, read and write, or atomic append? More...
 
enum  creation : unsigned char { open_existing = 0, only_if_not_exist, if_needed, creation::truncate }
 On opening, do we also create a new file or truncate an existing one? More...
 
enum  caching : unsigned char {
  unchanged = 0, none = 1, caching::only_metadata = 2, caching::reads = 3,
  caching::reads_and_metadata = 5, caching::all = 4, caching::safety_fsyncs = 7, caching::temporary = 6
}
 What i/o on the handle will complete immediately due to kernel caching. More...
 
enum  bitfield__flag : unsigned {
  none = 0, none = 2, none = 1, unlink_on_first_close = 1U << 0U,
  disable_safety_fsyncs = 1U << 2U, disable_safety_unlinks = 1U << 3U, disable_prefetching = 1U << 4U, maximum_prefetching = 1U << 5U,
  win_disable_unlink_emulation = 1U << 24U, win_disable_sparse_file_creation = 1U << 25U, overlapped = 1U << 28U, byte_lock_insanity = 1U << 29U,
  anonymous_inode = 1U << 30U
}
 Bitwise flags which can be specified. More...
 
using path_type = filesystem::path
 The path type used by this handle.
 
using extent_type = unsigned long long
 The file extent type used by this handle.
 
using size_type = size_t
 The memory extent type used by this handle.
 

Public Member Functions

constexpr handle ()
 Default constructor.
 
constexpr handle (native_handle_type h, caching caching=caching::none, flag flags=flag::none) noexcept
 Construct a handle from a supplied native handle.
 
 handle (const handle &)=delete
 No copy construction (use clone())
 
handleoperator= (const handle &o)=delete
 No copy assignment.
 
constexpr handle (handle &&o) noexcept
 Move the handle.
 
handleoperator= (handle &&o) noexcept
 Move assignment of handle.
 
void swap (handle &o) noexcept
 Swap with another instance.
 
virtual result< path_typecurrent_path () const noexcept
 
virtual result< void > close () noexcept
 Immediately close the native handle type managed by this handle.
 
result< handleclone () const noexcept
 
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.
 
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.
 

Protected Attributes

caching _caching {caching::none}
 
flag _flags {flag::none}
 
native_handle_type _v
 

Friends

class fs_handle
 
std::ostream & operator<< (std::ostream &s, const handle &v)
 

Detailed Description

A native_handle_type which is managed by the lifetime of this object instance.

Member Enumeration Documentation

◆ bitfield__flag

Bitwise flags which can be specified.

Enumerator
none 

No flags.

none 

No ability to read or write anything, but can synchronise (SYNCHRONIZE or 0)

none 

No caching whatsoever, all reads and writes come from storage (i.e. O_DIRECT|O_SYNC). Align all i/o to 4Kb boundaries for this to work. flag_disable_safety_fsyncs can be used here.

unlink_on_first_close 

Unlinks the file on handle close. On POSIX, this simply unlinks whatever is pointed to by path() upon the call of close() if and only if the inode matches. On Windows, if you are on Windows 10 1709 or later, exactly the same thing occurs. If on previous editions of Windows, the file entry does not disappears but becomes unavailable for anyone else to open with an errc::resource_unavailable_try_again error return. Because this is confusing, unless the win_disable_unlink_emulation flag is also specified, this POSIX behaviour is somewhat emulated by AFIO on older Windows by renaming the file to a random name on close() causing it to appear to have been unlinked immediately.

disable_safety_fsyncs 

Some kernel caching modes have unhelpfully inconsistent behaviours in getting your data onto storage, so by default unless this flag is specified AFIO adds extra fsyncs to the following operations for the caching modes specified below: truncation of file length either explicitly or during file open. closing of the handle either explicitly or in the destructor.

Additionally on Linux only to prevent loss of file metadata: On the parent directory whenever a file might have been created. On the parent directory on file close.

This only occurs for these kernel caching modes: caching::none caching::reads caching::reads_and_metadata caching::safety_fsyncs

disable_safety_unlinks 

file_handle::unlink() could accidentally delete the wrong file if someone has renamed the open file handle since the time it was opened. To prevent this occuring, where the OS doesn't provide race free unlink-by-open-handle we compare the inode of the path we are about to unlink with that of the open handle before unlinking.

Warning
This does not prevent races where in between the time of checking the inode and executing the unlink a third party changes the item about to be unlinked. Only operating systems with a true race-free unlink syscall are race free.
disable_prefetching 

Ask the OS to disable prefetching of data. This can improve random i/o performance.

maximum_prefetching 

Ask the OS to maximise prefetching of data, possibly prefetching the entire file into kernel cache. This can improve sequential i/o performance.

win_disable_unlink_emulation 

See the documentation for unlink_on_first_close

win_disable_sparse_file_creation 

Microsoft Windows NTFS, having been created in the late 1980s, did not originally implement extents-based storage and thus could only represent sparse files via efficient compression of intermediate zeros. With NTFS v3.0 (Microsoft Windows 2000), a proper extents-based on-storage representation was added, thus allowing only 64Kb extent chunks written to be stored irrespective of whatever the maximum file extent was set to.

For various historical reasons, extents-based storage is disabled by default in newly created files on NTFS, unlike in almost every other major filing system. You have to explicitly "opt in" to extents-based storage.

As extents-based storage is nearly cost free on NTFS, AFIO by default opts in to extents-based storage for any empty file it creates. If you don't want this, you can specify this flag to prevent that happening.

overlapped 

On Windows, create any new handles with OVERLAPPED semantics.

byte_lock_insanity 

Using insane POSIX byte range locks.

anonymous_inode 

This is an inode created with no representation on the filing system.

97  {
98  none = 0, //!< No flags
99  /*! Unlinks the file on handle close. On POSIX, this simply unlinks whatever is pointed
100  to by `path()` upon the call of `close()` if and only if the inode matches. On Windows,
101  if you are on Windows 10 1709 or later, exactly the same thing occurs. If on previous
102  editions of Windows, the file entry does not disappears but becomes unavailable for
103  anyone else to open with an `errc::resource_unavailable_try_again` error return. Because this is confusing, unless the
104  `win_disable_unlink_emulation` flag is also specified, this POSIX behaviour is
105  somewhat emulated by AFIO on older Windows by renaming the file to a random name on `close()`
106  causing it to appear to have been unlinked immediately.
107  */
108  unlink_on_first_close = 1U << 0U,
109 
110  /*! Some kernel caching modes have unhelpfully inconsistent behaviours
111  in getting your data onto storage, so by default unless this flag is
112  specified AFIO adds extra fsyncs to the following operations for the
113  caching modes specified below:
114  * truncation of file length either explicitly or during file open.
115  * closing of the handle either explicitly or in the destructor.
116 
117  Additionally on Linux only to prevent loss of file metadata:
118  * On the parent directory whenever a file might have been created.
119  * On the parent directory on file close.
120 
121  This only occurs for these kernel caching modes:
122  * caching::none
123  * caching::reads
124  * caching::reads_and_metadata
125  * caching::safety_fsyncs
126  */
127  disable_safety_fsyncs = 1U << 2U,
128  /*! `file_handle::unlink()` could accidentally delete the wrong file if someone has
129  renamed the open file handle since the time it was opened. To prevent this occuring,
130  where the OS doesn't provide race free unlink-by-open-handle we compare the inode of
131  the path we are about to unlink with that of the open handle before unlinking.
132  \warning This does not prevent races where in between the time of checking the inode
133  and executing the unlink a third party changes the item about to be unlinked. Only
134  operating systems with a true race-free unlink syscall are race free.
135  */
136  disable_safety_unlinks = 1U << 3U,
137  /*! Ask the OS to disable prefetching of data. This can improve random
138  i/o performance.
139  */
140  disable_prefetching = 1U << 4U,
141  /*! Ask the OS to maximise prefetching of data, possibly prefetching the entire file
142  into kernel cache. This can improve sequential i/o performance.
143  */
144  maximum_prefetching = 1U << 5U,
145 
146  win_disable_unlink_emulation = 1U << 24U, //!< See the documentation for `unlink_on_first_close`
147  /*! Microsoft Windows NTFS, having been created in the late 1980s, did not originally
148  implement extents-based storage and thus could only represent sparse files via
149  efficient compression of intermediate zeros. With NTFS v3.0 (Microsoft Windows 2000),
150  a proper extents-based on-storage representation was added, thus allowing only 64Kb
151  extent chunks written to be stored irrespective of whatever the maximum file extent
152  was set to.
153 
154  For various historical reasons, extents-based storage is disabled by default in newly
155  created files on NTFS, unlike in almost every other major filing system. You have to
156  explicitly "opt in" to extents-based storage.
157 
158  As extents-based storage is nearly cost free on NTFS, AFIO by default opts in to
159  extents-based storage for any empty file it creates. If you don't want this, you
160  can specify this flag to prevent that happening.
161  */
163 
164  // NOTE: IF UPDATING THIS UPDATE THE std::ostream PRINTER BELOW!!!
165 
166  overlapped = 1U << 28U, //!< On Windows, create any new handles with OVERLAPPED semantics
167  byte_lock_insanity = 1U << 29U, //!< Using insane POSIX byte range locks
168  anonymous_inode = 1U << 30U //!< This is an inode created with no representation on the filing system
169  }
See the documentation for unlink_on_first_close
Definition: handle.hpp:146
This is an inode created with no representation on the filing system.
Definition: handle.hpp:168
On Windows, create any new handles with OVERLAPPED semantics.
Definition: handle.hpp:166
No flags.
Definition: handle.hpp:98
Using insane POSIX byte range locks.
Definition: handle.hpp:167

◆ caching

enum afio_v2_xxx::handle::caching : unsigned char
strong

What i/o on the handle will complete immediately due to kernel caching.

Enumerator
none 

No caching whatsoever, all reads and writes come from storage (i.e. O_DIRECT|O_SYNC). Align all i/o to 4Kb boundaries for this to work. flag_disable_safety_fsyncs can be used here.

only_metadata 

Cache reads and writes of metadata but avoid caching data (O_DIRECT), thus i/o here does not affect other cached data for other handles. Align all i/o to 4Kb boundaries for this to work.

reads 

Cache reads only. Writes of data and metadata do not complete until reaching storage (O_SYNC). flag_disable_safety_fsyncs can be used here.

reads_and_metadata 

Cache reads and writes of metadata, but writes of data do not complete until reaching storage (O_DSYNC). flag_disable_safety_fsyncs can be used here.

all 

Cache reads and writes of data and metadata so they complete immediately, sending writes to storage at some point when the kernel decides (this is the default file system caching on a system).

safety_fsyncs 

Cache reads and writes of data and metadata so they complete immediately, but issue safety fsyncs at certain points. See documentation for flag_disable_safety_fsyncs.

temporary 

Cache reads and writes of data and metadata so they complete immediately, only sending any updates to storage on last handle close in the system or if memory becomes tight as this file is expected to be temporary (Windows and FreeBSD only).

83  : unsigned char // bit 0 set means safety fsyncs enabled
84  {
85  unchanged = 0,
86  none = 1, //!< No caching whatsoever, all reads and writes come from storage (i.e. <tt>O_DIRECT|O_SYNC</tt>). Align all i/o to 4Kb boundaries for this to work. <tt>flag_disable_safety_fsyncs</tt> can be used here.
87  only_metadata = 2, //!< Cache reads and writes of metadata but avoid caching data (<tt>O_DIRECT</tt>), thus i/o here does not affect other cached data for other handles. Align all i/o to 4Kb boundaries for this to work.
88  reads = 3, //!< Cache reads only. Writes of data and metadata do not complete until reaching storage (<tt>O_SYNC</tt>). <tt>flag_disable_safety_fsyncs</tt> can be used here.
89  reads_and_metadata = 5, //!< Cache reads and writes of metadata, but writes of data do not complete until reaching storage (<tt>O_DSYNC</tt>). <tt>flag_disable_safety_fsyncs</tt> can be used here.
90  all = 4, //!< Cache reads and writes of data and metadata so they complete immediately, sending writes to storage at some point when the kernel decides (this is the default file system caching on a system).
91  safety_fsyncs = 7, //!< Cache reads and writes of data and metadata so they complete immediately, but issue safety fsyncs at certain points. See documentation for <tt>flag_disable_safety_fsyncs</tt>.
92  temporary = 6 //!< Cache reads and writes of data and metadata so they complete immediately, only sending any updates to storage on last handle close in the system or if memory becomes tight as this file is expected to be temporary (Windows and FreeBSD only).
93  // NOTE: IF UPDATING THIS UPDATE THE std::ostream PRINTER BELOW!!!
94  };
No flags.
Definition: handle.hpp:98

◆ creation

enum afio_v2_xxx::handle::creation : unsigned char
strong

On opening, do we also create a new file or truncate an existing one?

Enumerator
truncate 

Atomically truncate on open, leaving creation date unmodified.

74  : unsigned char
75  {
76  open_existing = 0,
77  only_if_not_exist,
78  if_needed,
79  truncate //!< Atomically truncate on open, leaving creation date unmodified.
80  // NOTE: IF UPDATING THIS UPDATE THE std::ostream PRINTER BELOW!!!
81  };
result< file_handle::extent_type > truncate(file_handle &self, file_handle::extent_type newsize) noexcept
Definition: file_handle.hpp:378

◆ mode

enum afio_v2_xxx::handle::mode : unsigned char
strong

The behaviour of the handle: does it read, read and write, or atomic append?

Enumerator
none 

No ability to read or write anything, but can synchronise (SYNCHRONIZE or 0)

attr_read 

Ability to read attributes (FILE_READ_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)

attr_write 

Ability to read and write attributes (FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)

read 

Ability to read (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|SYNCHRONISE or O_RDONLY)

write 

Ability to read and write (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA|FILE_APPEND_DATA|SYNCHRONISE or O_RDWR)

append 

All mainstream OSs and CIFS guarantee this is atomic with respect to all other appenders (FILE_APPEND_DATA|SYNCHRONISE or O_APPEND)

62  : unsigned char // bit 0 set means writable
63  {
64  unchanged = 0,
65  none = 2, //!< No ability to read or write anything, but can synchronise (SYNCHRONIZE or 0)
66  attr_read = 4, //!< Ability to read attributes (FILE_READ_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)
67  attr_write = 5, //!< Ability to read and write attributes (FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|SYNCHRONIZE or O_RDONLY)
68  read = 6, //!< Ability to read (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|SYNCHRONISE or O_RDONLY)
69  write = 7, //!< Ability to read and write (READ_CONTROL|FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA|FILE_APPEND_DATA|SYNCHRONISE or O_RDWR)
70  append = 9 //!< All mainstream OSs and CIFS guarantee this is atomic with respect to all other appenders (FILE_APPEND_DATA|SYNCHRONISE or O_APPEND)
71  // NOTE: IF UPDATING THIS UPDATE THE std::ostream PRINTER BELOW!!!
72  };
io_handle::io_result< io_handle::buffers_type > read(io_handle &self, io_handle::io_request< io_handle::buffers_type > reqs, deadline d=deadline()) noexcept
Read data from the open handle.
Definition: io_handle.hpp:483
io_handle::io_result< io_handle::const_buffers_type > write(io_handle &self, io_handle::io_request< io_handle::const_buffers_type > reqs, deadline d=deadline()) noexcept
Write data to the open handle.
Definition: io_handle.hpp:514
No flags.
Definition: handle.hpp:98

Member Function Documentation

◆ clone()

result<handle> afio_v2_xxx::handle::clone ( ) const
inlinenoexcept

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

Errors returnable
Any of the values POSIX dup() or DuplicateHandle() can return.

◆ current_path()

virtual result<path_type> afio_v2_xxx::handle::current_path ( ) const
inlinevirtualnoexcept

Returns the current path of the open handle as said by the operating system. Note that you are NOT guaranteed that any path refreshed bears any resemblance to the original, some operating systems will return some different path which still reaches the same inode via some other route e.g. hardlinks, dereferenced symbolic links, etc. Windows and Linux correctly track changes to the specific path the handle was opened with, not getting confused by other hard links. MacOS nearly gets it right, but under some circumstances e.g. renaming may switch to a different hard link's path which is almost certainly a bug.

If AFIO was not able to determine the current path for this open handle e.g. the inode has been unlinked, it returns an empty path. Be aware that FreeBSD can return an empty (deleted) path for file inodes no longer cached by the kernel path cache, AFIO cannot detect the difference. FreeBSD will also return any path leading to the inode if it is hard linked. FreeBSD does implement path retrieval for directory inodes correctly however, and see algorithm::stablized_path<T> for a handle adapter which makes use of that.

On Linux if /proc is not mounted, this call fails with an error. All APIs in AFIO which require the use of current_path() can be told to not use it e.g. flag::disable_safety_unlinks. It is up to you to detect if current_path() is not working, and to change how you call AFIO appropriately.

Warning
This call is expensive, it always asks the kernel for the current path, and no checking is done to ensure what the kernel returns is accurate or even sensible. Be aware that despite these precautions, paths are unstable and can change randomly at any moment. Most code written to use absolute file systems paths is racy, so don't do it, use path_handle to fix a base location on the file system and work from that anchor instead!
Memory Allocations
At least one malloc for the path_type, likely several more.
See also
algorithm::cached_parent_handle_adapter<T> which overrides this with an implementation based on retrieving the current path of a cached handle to the parent directory. On platforms with instability or failure to retrieve the correct current path for regular files, the cached parent handle adapter works around the problem by taking advantage of directory inodes not having the same instability problems on any platform.

◆ set_append_only()

virtual result<void> afio_v2_xxx::handle::set_append_only ( bool  enable)
inlinevirtualnoexcept

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.

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