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

A handle to a directory which can be enumerated. More...

#include "directory_handle.hpp"

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

Classes

struct  buffers_type
 
struct  enumerate_info
 Completion information for enumerate() More...
 

Public Types

enum  filter { filter::none, filter::fastdeleted }
 How to do deleted file elimination on Windows. More...
 
using path_type = path_handle::path_type
 
using extent_type = path_handle::extent_type
 
using size_type = path_handle::size_type
 
using mode = path_handle::mode
 
using creation = path_handle::creation
 
using caching = path_handle::caching
 
using flag = path_handle::flag
 
using dev_t = fs_handle::dev_t
 
using ino_t = fs_handle::ino_t
 
using path_view_type = fs_handle::path_view_type
 
using buffer_type = directory_entry
 The buffer type used by this handle, which is a directory_entry
 
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 unique_id_type = QUICKCPPLIB_NAMESPACE::integers128::uint128
 The unique identifier type used by this handle.
 

Public Member Functions

constexpr directory_handle ()
 Default constructor.
 
constexpr directory_handle (native_handle_type h, dev_t devid, ino_t inode, caching caching=caching::all, flag flags=flag::none)
 Construct a directory_handle from a supplied native path_handle.
 
constexpr directory_handle (directory_handle &&o) noexcept
 Implicit move construction of directory_handle permitted.
 
 directory_handle (const directory_handle &)=delete
 No copy construction (use clone())
 
constexpr directory_handle (handle &&o, dev_t devid, ino_t inode) noexcept
 Explicit conversion from handle permitted.
 
directory_handleoperator= (directory_handle &&o) noexcept
 Move assignment of directory_handle permitted.
 
directory_handleoperator= (const directory_handle &)=delete
 No copy assignment.
 
void swap (directory_handle &o) noexcept
 Swap with another instance.
 
virtual result< void > close () noexcept override
 Immediately close the native handle type managed by this handle.
 
virtual result< directory_handleclone (mode mode_=mode::unchanged, caching caching_=caching::unchanged, deadline d=std::chrono::seconds(30)) const noexcept
 
result< path_handleclone_to_path_handle () const noexcept
 
virtual result< enumerate_infoenumerate (buffers_type &&tofill, path_view_type glob=path_view_type(), filter filtering=filter::fastdeleted, span< char > kernelbuffer=span< char >()) const noexcept
 
void swap (handle &o) noexcept
 Swap with another instance.
 
virtual result< path_type > current_path () const noexcept
 
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.
 
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.
 
unique_id_type unique_id () const noexcept
 A unique identifier for this handle across the entire system. Can be used in hash tables etc.
 
virtual result< path_handleparent_path_handle (deadline d=std::chrono::seconds(30)) const noexcept
 
virtual result< void > relink (const path_handle &base, path_view_type path, bool atomic_replace=true, deadline d=std::chrono::seconds(30)) noexcept
 
virtual result< void > unlink (deadline d=std::chrono::seconds(30)) noexcept
 

Static Public Member Functions

static result< directory_handledirectory (const path_handle &base, path_view_type path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept
 
static result< directory_handlerandom_directory (const path_handle &dirpath, mode _mode=mode::write, caching _caching=caching::temporary, flag flags=flag::none) noexcept
 
static result< directory_handletemp_directory (path_view_type name=path_view_type(), mode _mode=mode::write, creation _creation=creation::if_needed, caching _caching=caching::all, flag flags=flag::none) noexcept
 
static result< path_handlepath (const path_handle &base, path_view_type path) noexcept
 
static result< path_handlepath (path_view_type _path) noexcept
 

Protected Member Functions

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

Protected Attributes

caching _caching {caching::none}
 
flag _flags {flag::none}
 
native_handle_type _v
 
dev_t _devid {0}
 
ino_t _inode {0}
 

Detailed Description

A handle to a directory which can be enumerated.

Member Enumeration Documentation

◆ bitfield__flag

enum afio_v2_xxx::handle::bitfield__flag : unsigned
inherited

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

◆ filter

How to do deleted file elimination on Windows.

Enumerator
none 

Do no filtering at all.

fastdeleted 

Filter out AFIO deleted files based on their filename (fast and fairly reliable)

124  {
125  none, //!< Do no filtering at all
126  fastdeleted //!< Filter out AFIO deleted files based on their filename (fast and fairly reliable)
127  };
No flags.
Definition: handle.hpp:98

Member Function Documentation

◆ clone() [1/2]

virtual result<directory_handle> afio_v2_xxx::directory_handle::clone ( mode  mode_ = mode::unchanged,
caching  caching_ = caching::unchanged,
deadline  d = std::chrono::seconds(30) 
) const
inlinevirtualnoexcept

Clone this handle (copy constructor is disabled to avoid accidental copying), optionally race free reopening the handle with different access or caching.

Microsoft Windows provides a syscall for cloning an existing handle but with new access. On POSIX, we must loop calling current_path(), trying to open the path returned and making sure it is the same inode.

Errors returnable
Any of the values POSIX dup() or DuplicateHandle() can return.
Memory Allocations
On POSIX if changing the mode, we must loop calling current_path() and trying to open the path returned. Thus many allocations may occur.

◆ clone() [2/2]

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

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

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

◆ clone_to_path_handle()

result<path_handle> afio_v2_xxx::directory_handle::clone_to_path_handle ( ) const
inlinenoexcept

Return a copy of this directory handle, but as a path handle.

Errors returnable
Any of the values POSIX dup() or DuplicateHandle() can return.
Memory Allocations
On POSIX, we must loop calling current_path() and trying to open the path returned. Thus many allocations may occur.

◆ current_path()

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

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.

◆ directory()

static result<directory_handle> afio_v2_xxx::directory_handle::directory ( const path_handle base,
path_view_type  path,
mode  _mode = mode::read,
creation  _creation = creation::open_existing,
caching  _caching = caching::all,
flag  flags = flag::none 
)
inlinestaticnoexcept

Create a handle opening access to a directory on path.

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

◆ enumerate()

virtual result<enumerate_info> afio_v2_xxx::directory_handle::enumerate ( buffers_type &&  tofill,
path_view_type  glob = path_view_type(),
filter  filtering = filter::fastdeleted,
span< char >  kernelbuffer = span< char >() 
) const
inlinevirtualnoexcept

Fill the buffers type with as many directory entries as will fit.

Returns
Returns the buffers filled, what metadata was filled in and whether the entire directory was read into tofill.
Parameters
tofillThe buffers to fill, returned to you on exit.
globAn optional shell glob by which to filter the items filled. Done kernel side on Windows, user side on POSIX.
filteringWhether to filter out fake-deleted files on Windows or not.
kernelbufferA buffer to use for the kernel to fill. If left defaulted, a kernel buffer is allocated internally and stored into tofill which needs to not be destructed until one is no longer using any items within (leafnames are views onto the original kernel data).
Errors returnable
todo
Memory Allocations
If the kernelbuffer parameter is set on entry, no memory allocations. If unset, at least one memory allocation, possibly more is performed.

◆ parent_path_handle()

virtual result<path_handle> afio_v2_xxx::fs_handle::parent_path_handle ( deadline  d = std::chrono::seconds(30)) const
inlinevirtualnoexceptinherited

Obtain a handle to the path currently containing this handle's file entry.

Warning
This call is racy and can result in the wrong path handle being returned. Note that unless flag::disable_safety_unlinks is set, this implementation opens a path_handle to the source containing directory, then checks if the file entry within has the same inode as the open file handle. It will retry this matching until success until the deadline given.
Memory Allocations
Calls current_path() and thus is both expensive and calls malloc many times.
See also
algorithm::cached_parent_handle_adapter<T> which overrides this with a zero cost implementation, thus making unlinking and relinking very considerably quicker.

◆ path() [1/2]

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

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)
inlinestaticnoexceptinherited

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

◆ random_directory()

static result<directory_handle> afio_v2_xxx::directory_handle::random_directory ( const path_handle dirpath,
mode  _mode = mode::write,
caching  _caching = caching::temporary,
flag  flags = flag::none 
)
inlinestaticnoexcept

Create a directory 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 entry.

Errors returnable
Any of the values POSIX open() or CreateFile() can return.
176  {
177  try
178  {
179  for(;;)
180  {
181  auto randomname = utils::random_string(32);
182  result<directory_handle> ret = directory(dirpath, randomname, _mode, creation::only_if_not_exist, _caching, flags);
183  if(ret || (!ret && ret.error() != errc::file_exists))
184  {
185  return ret;
186  }
187  }
188  }
189  catch(...)
190  {
191  return error_from_exception();
192  }
193  }
static result< directory_handle > directory(const path_handle &base, path_view_type path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept
std::string random_string(size_t randomlen)
Returns a cryptographically random string capable of being used as a filename. Essentially random_fil...
Definition: utils.hpp:134
flag flags() const noexcept
The flags this handle was opened with.
Definition: handle.hpp:314

◆ relink()

virtual result<void> afio_v2_xxx::fs_handle::relink ( const path_handle base,
path_view_type  path,
bool  atomic_replace = true,
deadline  d = std::chrono::seconds(30) 
)
inlinevirtualnoexceptinherited

Relinks the current path of this open handle to the new path specified. If atomic_replace is true, the relink atomically and silently replaces 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 relinked. Note that unless flag::disable_safety_unlinks is set, this implementation opens a path_handle to the source containing directory first, then checks before relinking that the item about to be relinked has the same inode as the open file handle. It will retry this matching until success until the deadline given. This should prevent most unmalicious accidental loss of data.
Parameters
baseBase for any relative path.
pathThe relative or absolute new path to relink to.
atomic_replaceAtomically replace the destination if a file entry already is present there. Choosing false for this will fail if a file entry is already present at the destination, and may not be an atomic operation on some platforms (i.e. both the old and new names may be linked to the same inode for a very short period of time). Windows and recent Linuxes are always atomic.
dThe deadline by which the matching of the containing directory to the open handle's inode must succeed, else errc::timed_out will be returned.
Memory Allocations
Except on platforms with race free syscalls for renaming open handles (Windows), calls current_path() via parent_path_handle() and thus is both expensive and calls malloc many times.

◆ set_append_only()

virtual result<void> 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.

◆ temp_directory()

static result<directory_handle> afio_v2_xxx::directory_handle::temp_directory ( path_view_type  name = path_view_type(),
mode  _mode = mode::write,
creation  _creation = creation::if_needed,
caching  _caching = caching::all,
flag  flags = flag::none 
)
inlinestaticnoexcept

Create a directory handle creating the named directory on some path which the OS declares to be suitable for temporary files. Note also that an empty name is equivalent to calling random_file(path_discovery::storage_backed_temporary_files_directory()) and the creation parameter is ignored.

Errors returnable
Any of the values POSIX open() or CreateFile() can return.
204  {
206  return name.empty() ? random_directory(tempdirh, _mode, _caching, flags) : directory(tempdirh, name, _mode, _creation, _caching, flags);
207  }
const path_handle & storage_backed_temporary_files_directory() noexcept
Returns a reference to an open handle to a verified temporary directory where files created are store...
static result< directory_handle > directory(const path_handle &base, path_view_type path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept
flag flags() const noexcept
The flags this handle was opened with.
Definition: handle.hpp:314
static result< directory_handle > random_directory(const path_handle &dirpath, mode _mode=mode::write, caching _caching=caching::temporary, flag flags=flag::none) noexcept
Definition: directory_handle.hpp:175

◆ unlink()

virtual result<void> afio_v2_xxx::fs_handle::unlink ( deadline  d = std::chrono::seconds(30))
inlinevirtualnoexceptinherited

Unlinks the current path of this open handle, causing its entry to immediately disappear from the filing system. On Windows before Windows 10 1709 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. Note that Windows may prevent the renaming of a file in use by another process, if so it will NOT be renamed. 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 eventually removed 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 unlinked. Note that unless flag::disable_safety_unlinks is set, this implementation opens a path_handle to the containing directory first, then checks that the item about to be unlinked has the same inode as the open file handle. It will retry this matching until success until the deadline given. This should prevent most unmalicious accidental loss of data.
Parameters
dThe deadline by which the matching of the containing directory to the open handle's inode must succeed, else errc::timed_out will be returned.
Memory Allocations
Except on platforms with race free syscalls for unlinking open handles (Windows), calls current_path() and thus is both expensive and calls malloc many times. On Windows, also calls current_path() if flag::disable_safety_unlinks is not set.

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