4#include "../Common/CompilerMacrosExport.h"
5#ifndef SC_EXPORT_LIBRARY_FILE_SYSTEM
6#define SC_EXPORT_LIBRARY_FILE_SYSTEM 0
8#define SC_FILE_SYSTEM_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_FILE_SYSTEM)
10#include "../Common/IGrowableBufferSpan.h"
11#include "../Common/IGrowableBufferStringPath.h"
12#include "../Common/PrimitiveDefinitions.h"
13#include "../Common/Result.h"
14#include "FileSystemError.h"
156 return copyFiles(
CopyOperation{source, destination, copyFlags});
175 return copyDirectories(
CopyOperation{source, destination, copyFlags});
285 [[nodiscard]]
bool exists(StringSpan fileOrDirectory);
355 template <
typename T>
358 return read(file, GrowableBuffer<T>{data});
430 static bool access(StringSpan path,
AccessMode accessMode);
432 static ResultFileSystem createSymbolicLink(StringSpan sourceFileOrDirectory, StringSpan linkFile);
433 static ResultFileSystem createHardLink(StringSpan sourceFile, StringSpan linkFile);
435 static bool exists(StringSpan path);
437 static bool existsAndIsDirectory(StringSpan path);
438 static ResultFileSystem existsAndIsDirectory(StringSpan path,
bool& isDirectory);
439 static bool existsAndIsFile(StringSpan path);
441 static bool existsAndIsLink(StringSpan path);
445 static ResultFileSystem moveDirectory(StringSpan source, StringSpan destination);
454 static ResultFileSystem readSymbolicLink(StringSpan path, StringPath& destination);
457 static ResultFileSystem lchown(StringSpan path, uint32_t uid, uint32_t gid);
461 static StringSpan getExecutablePath(StringPath& executablePath);
462 static StringSpan getCurrentWorkingDirectory(StringPath& currentWorkingDirectory);
463 static StringSpan getApplicationRootDirectory(StringPath& applicationRootDirectory);
470 static constexpr size_t WindowsPathTransportCapacity = StringPath::MaxPath + 6;
472 [[nodiscard]]
ResultFileSystem convert(
const StringSpan file, StringPath& destination,
473 StringNativeBuffer<WindowsPathTransportCapacity + 1>& transportPath,
474 StringSpan* encodedPath =
nullptr);
476 StringPath fileFormatBuffer1;
477 StringPath fileFormatBuffer2;
478 StringPath currentDirectory;
480 StringNativeBuffer<WindowsPathTransportCapacity + 1> fileTransportBuffer1;
481 StringNativeBuffer<WindowsPathTransportCapacity + 1> fileTransportBuffer2;
FileSystemAccessMode
Access mode for path checks.
Definition FileSystem.h:68
FileSystemEntryType
A structure to describe file stats.
Definition FileSystem.h:26
@ Exists
Check if path exists.
@ Execute
Check if path is executable / traversable.
A structure to describe copy flags.
Definition FileSystem.h:77
FileSystemCopyFlags & setUseCloneIfSupported(bool value)
If true copy will use native filesystem clone os api.
Definition FileSystem.h:96
FileSystemCopyFlags & setOverwrite(bool value)
If true copy will overwrite existing files in the destination.
Definition FileSystem.h:87
bool useCloneIfSupported
If true copy will use native filesystem clone os api.
Definition FileSystem.h:103
bool overwrite
If true copy will overwrite existing files in the destination.
Definition FileSystem.h:102
A structure to describe file stats.
Definition FileSystem.h:36
uint32_t mode
POSIX st_mode.
Definition FileSystem.h:47
uint32_t reparseTag
Windows reparse tag.
Definition FileSystem.h:60
size_t hardLinkCount
Number of hard links to the entry.
Definition FileSystem.h:40
TimeMs accessedTime
Time when file was last accessed when available.
Definition FileSystem.h:42
TimeMs creationTime
Time when file was created when available.
Definition FileSystem.h:41
uint64_t blockSize
POSIX st_blksize.
Definition FileSystem.h:54
FileSystemEntryType entryType
Type of entry stored at path.
Definition FileSystem.h:37
size_t fileSize
Size of the file in bytes.
Definition FileSystem.h:39
uint64_t blocks
POSIX st_blocks.
Definition FileSystem.h:53
uint32_t gid
POSIX st_gid.
Definition FileSystem.h:49
uint64_t inode
POSIX st_ino.
Definition FileSystem.h:50
uint64_t fileIndex
Windows file index.
Definition FileSystem.h:62
uint64_t specialDevice
POSIX st_rdev.
Definition FileSystem.h:52
uint32_t uid
POSIX st_uid.
Definition FileSystem.h:48
TimeMs modifiedTime
Time when file was last modified.
Definition FileSystem.h:43
uint32_t volumeSerialNumber
Windows volume serial number.
Definition FileSystem.h:61
uint64_t device
POSIX st_dev.
Definition FileSystem.h:51
uint32_t attributes
Windows file attributes.
Definition FileSystem.h:59
Specify source, destination and flags for a copy operation.
Definition FileSystem.h:132
StringSpan destination
Copy operation sink (can be a {relative | absolute} {file | directory} path)
Definition FileSystem.h:134
CopyFlags copyFlags
Copy operation flags (overwrite, use clone api etc.)
Definition FileSystem.h:135
StringSpan source
Copy operation source (can be a {relative | absolute} {file | directory} path)
Definition FileSystem.h:133
Low level filesystem API, requiring paths in native encoding (UTF-16 on Windows, UTF-8 elsewhere)
Definition FileSystem.h:429
Execute fs operations { exists, copy, delete } for { files and directories }.
Definition FileSystem.h:113
ResultFileSystem existsAndIsDirectory(StringSpan directory, bool &isDirectory)
Detailed query: absence or another entry type is a successful false answer.
bool existsAndIsFile(StringSpan file)
Check if a file exists at given path.
ResultFileSystem rename(StringSpan path, StringSpan newPath)
Rename a file or directory.
ResultFileSystem canAccess(StringSpan fileOrDirectory, AccessMode accessMode, bool &canAccessPath)
Detailed query: denied access or absence is a successful false answer.
ResultFileSystem exists(StringSpan fileOrDirectory, bool &doesExist)
Detailed query: a missing entry is a successful false answer; validation and I/O failures are errors.
ResultFileSystem lchown(StringSpan path, uint32_t uid, uint32_t gid)
Change owner and group for a path without following symbolic links.
ResultFileSystem removeEmptyDirectories(Span< const StringSpan > directories)
Removes multiple empty directories.
ResultFileSystem chmod(StringSpan path, uint32_t mode)
Change file permission bits for a path, following symbolic links.
ResultFileSystem removeFile(StringSpan source)
Remove a single file.
Definition FileSystem.h:197
ResultFileSystem makeDirectoryIfNotExists(StringSpan directory)
Creates a new directory, if it doesn't already exists at the given path.
Definition FileSystem.h:255
ResultFileSystem readSymbolicLink(StringSpan linkFile, StringPath &destination)
Reads the target path stored by a symbolic link.
bool exists(StringSpan fileOrDirectory)
Check if a file or directory exists at a given path.
ResultFileSystem lchmod(StringSpan path, uint32_t mode)
Change file permission bits for a symbolic link without following it.
ResultFileSystem existsAndIsFile(StringSpan file, bool &isFile)
Detailed query: absence or another entry type is a successful false answer.
ResultFileSystem removeDirectoriesRecursive(Span< const StringSpan > directories)
Remove multiple directories with their entire content (like posix rm -rf)
bool existsAndIsDirectory(StringSpan directory)
Check if a directory exists at given path.
ResultFileSystem removeDirectoryRecursive(StringSpan directory)
Remove single directory with its entire content (like posix rm -rf)
Definition FileSystem.h:221
ResultFileSystem getFileStat(StringSpan file, FileStat &fileStat)
Legacy convenience alias for stat(StringSpan, FileStat&)
ResultFileSystem makeDirectory(StringSpan directory)
Creates a new directory that does not already exist.
Definition FileSystem.h:245
ResultFileSystem moveDirectory(StringSpan sourceDirectory, StringSpan destinationDirectory)
Moves a directory from source to destination.
ResultFileSystem write(StringSpan file, Span< const char > data)
Writes a block of memory to a file.
ResultFileSystem init(StringSpan initialDirectory)
Call init function when instantiating the class to set directory for all operations using relative pa...
ResultFileSystem writeStringAppend(StringSpan file, StringSpan text)
Appends a StringSpan to a file.
ResultFileSystem chown(StringSpan path, uint32_t uid, uint32_t gid)
Change owner and group for a path, following symbolic links.
ResultFileSystem makeDirectoriesRecursive(Span< const StringSpan > directories)
Create new directories, creating also intermediate non existing directories (like posix mkdir -p)
ResultFileSystem writeString(StringSpan file, StringSpan text)
Replace the entire content of a file with the provided StringSpan.
ResultFileSystem changeDirectory(StringSpan newDirectory)
Changes current directory.
ResultFileSystem setLastModifiedTime(StringSpan file, TimeMs time)
Change last modified time of a given file.
ResultFileSystem copyFiles(Span< const CopyOperation > sourceDestination)
Copies many files.
ResultFileSystem makeDirectories(Span< const StringSpan > directories)
Creates new directories that do not already exist.
ResultFileSystem removeFiles(Span< const StringSpan > files)
Remove multiple files.
ResultFileSystem read(StringSpan file, T &data)
Read contents of a file into a String or Buffer.
Definition FileSystem.h:356
ResultFileSystem removeLinkIfExists(StringSpan source)
Remove a single link, giving no error if it doesn't exist.
ResultFileSystem copyDirectories(Span< const CopyOperation > sourceDestination)
Copy many directories.
ResultFileSystem stat(StringSpan file, FileStat &fileStat)
Obtains richer metadata about a path, following symbolic links.
ResultFileSystem makeDirectoriesIfNotExists(Span< const StringSpan > directories)
Creates new directories, if they don't already exist at the given path.
bool canAccess(StringSpan fileOrDirectory, AccessMode accessMode=AccessMode::Exists)
Check whether the current process can access a path with the requested mode.
ResultFileSystem removeFileIfExists(StringSpan source)
Remove a single file, giving no error if it doesn't exist.
ResultFileSystem existsAndIsLink(StringSpan file, bool &isLink)
Detailed query: absence or another entry type is a successful false answer.
ResultFileSystem removeEmptyDirectory(StringSpan directory)
Removes an empty directory.
Definition FileSystem.h:233
ResultFileSystem read(StringSpan file, IGrowableBuffer &&buffer)
Read contents of a file into an IGrowableBuffer.
ResultFileSystem lstat(StringSpan file, FileStat &fileStat)
Obtains richer metadata about a path without following symbolic links.
ResultFileSystem copyDirectory(StringSpan source, StringSpan destination, CopyFlags copyFlags=CopyFlags())
Copy a single directory.
Definition FileSystem.h:173
ResultFileSystem makeDirectoryRecursive(StringSpan directory)
Create a new directory, creating also intermediate non existing directories (like posix mkdir -p)
Definition FileSystem.h:267
ResultFileSystem copyFile(StringSpan source, StringSpan destination, CopyFlags copyFlags=CopyFlags())
Copy a single file.
Definition FileSystem.h:154
bool existsAndIsLink(StringSpan file)
Check if a link exists at given path.
ResultFileSystem createSymbolicLink(StringSpan sourceFileOrDirectory, StringSpan linkFile)
Creates a symbolic link at location linkFile pointing at sourceFileOrDirectory.
ResultFileSystem createHardLink(StringSpan sourceFile, StringSpan linkFile)
Creates a hard link at location linkFile pointing at sourceFile.
FileSystem result retaining an operation detail and one scalar diagnostic context.
Definition FileSystemError.h:126