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

A handle to somewhere originally identified by a path on the filing system. Typically used as the lightest weight handle to some location on the filing system which may unpredictably relocate over time. This handle is thus an anchor to a subset island of the filing system, free of any race conditions introduced by third party changes to any part of the path leading to that island. More...

#include "path_handle.hpp"

Inheritance diagram for afio_v2_xxx::path_handle:
afio_v2_xxx::handle afio_v2_xxx::directory_handle

Public Types

using path_type = handle::path_type
using extent_type = handle::extent_type
using size_type = handle::size_type
using mode = handle::mode
using creation = handle::creation
using caching = handle::caching
using flag = handle::flag
using path_view_type = path_view
 The path view type used by this handle.

Public Member Functions

constexpr path_handle ()
 Default constructor.
constexpr path_handle (native_handle_type h, caching caching=caching::all, flag flags=flag::none)
 Construct a handle from a supplied native handle.
constexpr path_handle (handle &&o) noexcept
 Explicit conversion from handle permitted.
 path_handle (path_handle &&)=default
 Move construction permitted.
 path_handle (const path_handle &)=delete
 No copy construction (use clone())
path_handleoperator= (path_handle &&)=default
 Move assignment permitted.
path_handleoperator= (const path_handle &)=delete
 No copy assignment.
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.

Static Public Member Functions

static result< path_handlepath (const path_handle &base, path_view_type path) noexcept
static result< path_handlepath (path_view_type _path) noexcept

Protected Attributes

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


class directory_handle

Detailed Description

A handle to somewhere originally identified by a path on the filing system. Typically used as the lightest weight handle to some location on the filing system which may unpredictably relocate over time. This handle is thus an anchor to a subset island of the filing system, free of any race conditions introduced by third party changes to any part of the path leading to that island.

Member Function Documentation

◆ clone()

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

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

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.

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.

◆ path() [1/2]

static result<path_handle> afio_v2_xxx::path_handle::path ( const path_handle base,
path_view_type  path 

Create a path handle opening access to some location on the filing system. Some operating systems provide a particularly lightweight method of doing this (Linux: O_PATH, Windows: no access perms) which is much faster than opening a directory. For other systems, we open a directory with read only permissions.

Errors returnable
Any of the values POSIX open() or CreateFile() can return.

◆ path() [2/2]

static result<path_handle> afio_v2_xxx::path_handle::path ( path_view_type  _path)

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

95 { return path(path_handle(), _path); }
static result< path_handle > path(const path_handle &base, path_view_type path) noexcept
constexpr path_handle()
Default constructor.
Definition: path_handle.hpp:66

◆ set_append_only()

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

Changes whether this handle is append only or not.

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: