Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
File.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Internal/IGrowableBuffer.h"
5#include "../Foundation/PrimitiveTypes.h"
6#include "../Foundation/Result.h"
7#include "../Foundation/Span.h"
8#include "../Foundation/StringPath.h"
9#include "../Foundation/StringSpan.h"
10#include "../Foundation/UniqueHandle.h"
11
14
15namespace SC
16{
17namespace detail
18{
19#if SC_PLATFORM_WINDOWS
20struct SC_COMPILER_EXPORT FileDescriptorDefinition
21{
22 using Handle = void*; // HANDLE
23 static Result releaseHandle(Handle& handle);
24#ifdef __clang__
25 static constexpr void* Invalid = __builtin_constant_p(-1) ? (void*)-1 : (void*)-1; // INVALID_HANDLE_VALUE
26#else
27 static constexpr void* Invalid = (void*)-1; // INVALID_HANDLE_VALUE
28#endif
29};
30
31#else
33
35struct SC_COMPILER_EXPORT FileDescriptorDefinition
36{
37 using Handle = int; // fd
38 static Result releaseHandle(Handle& handle);
39
40 static constexpr Handle Invalid = -1; // invalid fd
41};
43#endif
44} // namespace detail
45
48
51{
52 Unknown,
53 File,
54 Directory,
55 SymbolicLink,
56 Other,
57};
58
90
92struct SC_COMPILER_EXPORT FileOpen
93{
104
105 FileOpen(Mode mode = Read) : mode(mode) {}
106
108 bool inheritable = false;
109 bool blocking = true;
110 bool sync = false;
111 bool exclusive = false;
112
113#if !SC_PLATFORM_WINDOWS
114 int toPosixFlags() const;
115 int toPosixAccess() const;
116#endif
117};
119
121struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescriptorDefinition>
122{
123 using UniqueHandle::UniqueHandle;
124
127
131
135
139
143
149
155 Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
156
162 Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
163
168 Result read(Span<char> data, Span<char>& actuallyRead);
169
175
181
187 template <typename T>
188 Result readUntilEOF(T& destination)
189 {
190 return readUntilEOF(GrowableBuffer<T>{destination});
191 }
192
197 Result readUntilEOF(IGrowableBuffer&& buffer);
198
203
209
215
220
225
233
238 Result seek(SeekMode seekMode, int64_t offset);
239
243 Result currentPosition(size_t& position) const;
244
248 Result sizeInBytes(size_t& sizeInBytes) const;
249
254
259
265
269
273
278
279 private:
280 friend struct File;
281 friend struct PipeDescriptor;
282 struct Internal;
283};
284
285struct SC_COMPILER_EXPORT PipeOptions
286{
287 bool readInheritable = false;
288 bool writeInheritable = false;
289 bool blocking = true;
290};
291
306
307struct SC_COMPILER_EXPORT NamedPipeNameOptions
308{
309 StringSpan posixDirectory = "/tmp";
310};
311
313struct SC_COMPILER_EXPORT NamedPipeName
314{
320 static Result build(StringSpan logicalName, StringPath& outName, NamedPipeNameOptions options = {});
321};
322
323struct SC_COMPILER_EXPORT NamedPipeServerOptions
324{
325 PipeOptions connectionOptions = {};
326 uint32_t maxPendingConnections = 16;
327
328 struct Posix
329 {
330 bool removeEndpointOnClose = true;
331 bool removeEndpointBeforeCreate = false;
332 } posix;
333};
334
335struct SC_COMPILER_EXPORT NamedPipeClientOptions
336{
337 PipeOptions connectionOptions = {};
338 struct Windows
339 {
340 uint32_t connectTimeoutMilliseconds = 0xffffffff;
341 } windows;
342};
343
348struct SC_COMPILER_EXPORT NamedPipeServer
349{
360 Result accept(PipeDescriptor& outConnection);
365
366 private:
368 StringPath name;
369#if SC_PLATFORM_WINDOWS
370 FileDescriptor pendingConnection;
371 bool firstInstance = true;
372#else
373 FileDescriptor listeningSocket;
374#endif
375 bool created = false;
376};
377
379struct SC_COMPILER_EXPORT NamedPipeClient
380{
388 static Result connect(StringSpan name, PipeDescriptor& outConnection, NamedPipeClientOptions options = {});
389};
391} // namespace SC
@ Read
Check if path is readable.
FileDescriptorEntryType
A structure to describe metadata associated with an open descriptor.
Definition File.h:51
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
long long int64_t
Platform independent (8) bytes signed int.
Definition PrimitiveTypes.h:50
A structure to describe file stats queried through a file descriptor.
Definition File.h:61
uint32_t reparseTag
Windows reparse tag.
Definition File.h:85
uint32_t uid
POSIX st_uid.
Definition File.h:73
uint32_t mode
POSIX st_mode.
Definition File.h:72
uint64_t blockSize
POSIX st_blksize.
Definition File.h:79
uint64_t fileIndex
Windows file index.
Definition File.h:87
uint32_t attributes
Windows file attributes.
Definition File.h:84
FileDescriptorEntryType entryType
Type of entry associated with descriptor.
Definition File.h:62
TimeMs accessedTime
Time when file was last accessed when available.
Definition File.h:67
size_t hardLinkCount
Number of hard links to the entry.
Definition File.h:65
uint64_t device
POSIX st_dev.
Definition File.h:76
uint64_t specialDevice
POSIX st_rdev.
Definition File.h:77
TimeMs modifiedTime
Time when file was last modified.
Definition File.h:68
uint64_t blocks
POSIX st_blocks.
Definition File.h:78
size_t fileSize
Size of the file in bytes.
Definition File.h:64
uint32_t volumeSerialNumber
Windows volume serial number.
Definition File.h:86
uint32_t gid
POSIX st_gid.
Definition File.h:74
TimeMs creationTime
Time when file was created when available.
Definition File.h:66
uint64_t inode
POSIX st_ino.
Definition File.h:75
[UniqueHandleDeclaration2Snippet]
Definition File.h:122
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition File.h:228
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition File.h:231
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition File.h:230
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition File.h:229
Result syncData()
Flush file data to stable storage.
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result openForWriteToDevNull()
... [UniqueHandleDeclaration2Snippet]
Result write(Span< const uint8_t > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result openStdErrDuplicate()
Opens a duplicated file descriptor handle for reading from stderr.
Result chmod(uint32_t mode)
Change file permission bits for the currently open descriptor.
Result readUntilEOF(T &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Definition File.h:188
Result readUntilEOF(IGrowableBuffer &&buffer)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Result seek(SeekMode seekMode, int64_t offset)
Changes the current position in the file descriptor, if seekable.
Result stat(FileDescriptorStat &fileStat) const
Obtains richer metadata associated with the currently open descriptor.
Result read(Span< char > data, Span< char > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result currentPosition(size_t &position) const
Gets current descriptor position (if seekable)
Result write(Span< const uint8_t > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Result write(Span< const char > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result sizeInBytes(size_t &sizeInBytes) const
Gets total file size in bytes (if seekable)
Result truncate(uint64_t sizeInBytes)
Resize the underlying file to the specified size in bytes.
Result openStdOutDuplicate()
Opens a duplicated file descriptor handle for reading from stdout.
Result read(Span< char > data, Span< char > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result readUntilFullOrEOF(Span< char > data, Span< char > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into Span, until full or EOF is reached.
Result chown(uint32_t uid, uint32_t gid)
Change owner and group for the currently open descriptor.
Result sync()
Flush file data and metadata to stable storage.
Result openStdInDuplicate()
Opens a duplicated file descriptor handle for reading from stdin.
Result open(StringSpan path, FileOpen mode)
Opens a file descriptor handle from a file system path.
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result writeString(StringSpan data)
Writes a string to the file descriptor.
Result write(Span< const char > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Options used to open a file descriptor.
Definition File.h:93
Mode
Indicates the mode in which the file should be opened (read, write, append, etc.)
Definition File.h:96
@ Append
a Open for appending. The file is created if it does not exist.
Definition File.h:99
@ WriteRead
w+ Open for reading and writing. The file is created (if it does not exist) or truncated.
Definition File.h:102
@ ReadWrite
r+ Open for reading and writing. An error occurs if the file does not exist.
Definition File.h:98
@ Write
w Open for writing. The file is created (if it does not exist) or truncated (if it exists).
Definition File.h:101
@ AppendRead
a+ Open for reading and appending. The file is created if it does not exist.
Definition File.h:100
Mode mode
Open mode (read, write, append, etc.). See FileOpen::Mode for more details.
Definition File.h:107
Definition File.h:336
Named pipe client endpoint creator.
Definition File.h:380
static Result connect(StringSpan name, PipeDescriptor &outConnection, NamedPipeClientOptions options={})
Connects to an existing named pipe server endpoint.
Definition File.h:308
Utility for building platform-native named pipe endpoint names from logical names.
Definition File.h:314
static Result build(StringSpan logicalName, StringPath &outName, NamedPipeNameOptions options={})
Builds a platform-native named pipe endpoint path.
Definition File.h:324
Named pipe server endpoint.
Definition File.h:349
Result close()
Closes the listening endpoint.
Result create(StringSpan name, NamedPipeServerOptions options={})
Creates a named pipe server endpoint.
Result accept(PipeDescriptor &outConnection)
Accept one client connection and return it as a connected PipeDescriptor.
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition File.h:294
Result createPipe(PipeOptions options={})
Creates a Pipe.
FileDescriptor writePipe
The write side of the pipe.
Definition File.h:296
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition File.h:295
Definition File.h:286
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:42
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
A vocabulary type representing a time interval in milliseconds since epoch.
Definition PrimitiveTypes.h:63
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:27