Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
FileSystem.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../File/File.h"
5#include "../Foundation/Result.h"
6#include "../Foundation/StringPath.h"
7#include "../Time/Time.h"
8
9namespace SC
10{
13
16
23
26{
28 {
29 overwrite = false;
31 }
32
37 {
38 overwrite = value;
39 return *this;
40 }
41
46 {
47 useCloneIfSupported = value;
48 return *this;
49 }
50
51 bool overwrite;
53};
54
62{
63 bool preciseErrorMessages = false;
64
68 Result init(StringSpan initialDirectory);
69
74
77
85
93
102 Result copyFile(StringSpan source, StringSpan destination, CopyFlags copyFlags = CopyFlags())
103 {
104 return copyFiles(CopyOperation{source, destination, copyFlags});
105 }
106
112
121 Result copyDirectory(StringSpan source, StringSpan destination, CopyFlags copyFlags = CopyFlags())
122 {
123 return copyDirectories(CopyOperation{source, destination, copyFlags});
124 }
125
135
140
145 Result removeFile(StringSpan source) { return removeFiles({source}); }
146
151
156
162
169 Result removeDirectoryRecursive(StringSpan directory) { return removeDirectoriesRecursive({directory}); }
170
176
181 Result removeEmptyDirectory(StringSpan directory) { return removeEmptyDirectories({directory}); }
182
188
193 Result makeDirectory(StringSpan directory) { return makeDirectories({directory}); }
194
199
203 Result makeDirectoryIfNotExists(StringSpan directory) { return makeDirectoriesIfNotExists({directory}); }
204
210
215 Result makeDirectoryRecursive(StringSpan directory) { return makeDirectoriesRecursive({directory}); }
216
221 Result createSymbolicLink(StringSpan sourceFileOrDirectory, StringSpan linkFile);
222
227 [[nodiscard]] bool exists(StringSpan fileOrDirectory);
228
232 [[nodiscard]] bool existsAndIsDirectory(StringSpan directory);
233
240 [[nodiscard]] bool existsAndIsFile(StringSpan file);
241
245 [[nodiscard]] bool existsAndIsLink(StringSpan file);
246
251 [[nodiscard]] bool moveDirectory(StringSpan sourceDirectory, StringSpan destinationDirectory);
252
261 Result write(StringSpan file, Span<const uint8_t> data);
262
269
275
281 template <typename T>
282 Result read(StringSpan file, T& data)
283 {
285 StringSpan encodedPath;
286 SC_TRY(convert(file, fileFormatBuffer1, &encodedPath))
287 SC_TRY(fd.open(encodedPath, FileOpen::Read));
288 return fd.readUntilEOF(data);
289 }
290
293
299
305
309 {
310 static Result createSymbolicLink(StringSpan sourceFileOrDirectory, StringSpan linkFile);
311 static Result makeDirectory(StringSpan dir);
312 static Result exists(StringSpan path);
313 static Result existsAndIsDirectory(StringSpan path);
314 static Result existsAndIsFile(StringSpan path);
315 static Result existsAndIsLink(StringSpan path);
316 static Result makeDirectoryRecursive(StringSpan path);
317 static Result removeEmptyDirectory(StringSpan path);
318 static Result moveDirectory(StringSpan source, StringSpan destination);
319 static Result removeFile(StringSpan path);
320 static Result copyFile(StringSpan srcPath, StringSpan destPath, FileSystemCopyFlags flags);
321 static Result rename(StringSpan path, StringSpan newPath);
322 static Result copyDirectory(StringSpan srcPath, StringSpan destPath, FileSystemCopyFlags flags);
323 static Result removeDirectoryRecursive(StringSpan directory);
324 static Result getFileStat(StringSpan path, FileSystemStat& fileStat);
325 static Result setLastModifiedTime(StringSpan path, Time::Realtime time);
326
327 static StringSpan getExecutablePath(StringPath& executablePath);
328 static StringSpan getApplicationRootDirectory(StringPath& applicationRootDirectory);
329
330 private:
331 struct Internal;
332 };
333
334 private:
335 [[nodiscard]] bool convert(const StringSpan file, StringPath& destination, StringSpan* encodedPath = nullptr);
336
337 StringPath fileFormatBuffer1;
338 StringPath fileFormatBuffer2;
339 StringPath currentDirectory;
340
341 char errorMessageBuffer[256] = {0};
342
343 Result formatError(int errorNumber, StringSpan item, bool isWindowsNativeError);
344 struct Internal;
345};
347} // namespace SC
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition Result.h:48
Open, read and write to/from a file descriptor (like a file or pipe).
Definition File.h:76
Result readUntilEOF(T &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Definition File.h:136
Result open(StringSpan path, FileOpen mode)
Opens a file descriptor handle from a file system path.
A structure to describe copy flags.
Definition FileSystem.h:26
FileSystemCopyFlags & setUseCloneIfSupported(bool value)
If true copy will use native filesystem clone os api.
Definition FileSystem.h:45
FileSystemCopyFlags & setOverwrite(bool value)
If true copy will overwrite existing files in the destination.
Definition FileSystem.h:36
bool useCloneIfSupported
If true copy will use native filesystem clone os api.
Definition FileSystem.h:52
bool overwrite
If true copy will overwrite existing files in the destination.
Definition FileSystem.h:51
A structure to describe file stats.
Definition FileSystem.h:19
Time::Realtime modifiedTime
Time when file was last modified.
Definition FileSystem.h:21
size_t fileSize
Size of the file in bytes.
Definition FileSystem.h:20
Specify source, destination and flags for a copy operation.
Definition FileSystem.h:80
StringSpan destination
Copy operation sink (can be a {relative | absolute} {file | directory} path)
Definition FileSystem.h:82
CopyFlags copyFlags
Copy operation flags (overwrite, use clone api etc.)
Definition FileSystem.h:83
StringSpan source
Copy operation source (can be a {relative | absolute} {file | directory} path)
Definition FileSystem.h:81
Low level filesystem API, requiring paths in native encoding (UTF-16 on Windows, UTF-8 elsewhere)
Definition FileSystem.h:309
Execute fs operations { exists, copy, delete } for { files and directories }.
Definition FileSystem.h:62
bool existsAndIsFile(StringSpan file)
Check if a file exists at given path.
Result removeFiles(Span< const StringSpan > files)
Remove multiple files.
Result removeFile(StringSpan source)
Remove a single file.
Definition FileSystem.h:145
Result init(StringSpan initialDirectory)
Call init function when instantiating the class to set directory for all operations using relative pa...
Result setLastModifiedTime(StringSpan file, Time::Realtime time)
Change last modified time of a given file.
Result read(StringSpan file, T &data)
Read contents of a file into a String or Buffer.
Definition FileSystem.h:282
Result changeDirectory(StringSpan newDirectory)
Changes current directory.
Result makeDirectoriesRecursive(Span< const StringSpan > directories)
Create new directories, creating also intermediate non existing directories (like posix mkdir -p)
Result removeLinkIfExists(StringSpan source)
Remove a single link, giving no error if it doesn't exist.
bool exists(StringSpan fileOrDirectory)
Check if a file or directory exists at a given path.
bool moveDirectory(StringSpan sourceDirectory, StringSpan destinationDirectory)
Moves a directory from source to destination.
Result removeDirectoriesRecursive(Span< const StringSpan > directories)
Remove multiple directories with their entire content (like posix rm -rf)
Result copyFiles(Span< const CopyOperation > sourceDestination)
Copies many files.
Result removeEmptyDirectories(Span< const StringSpan > directories)
Removes multiple empty directories.
Result makeDirectoryRecursive(StringSpan directory)
Create a new directory, creating also intermediate non existing directories (like posix mkdir -p)
Definition FileSystem.h:215
bool existsAndIsDirectory(StringSpan directory)
Check if a directory exists at given path.
Result writeStringAppend(StringSpan file, StringSpan text)
Appends a StringSpan to a file.
Result getFileStat(StringSpan file, FileStat &fileStat)
Obtains stats (size, modified time) about a file.
Result removeFileIfExists(StringSpan source)
Remove a single file, giving no error if it doesn't exist.
Result rename(StringSpan path, StringSpan newPath)
Rename a file or directory.
Result makeDirectory(StringSpan directory)
Creates a new directory that does not already exist.
Definition FileSystem.h:193
Result removeDirectoryRecursive(StringSpan directory)
Remove single directory with its entire content (like posix rm -rf)
Definition FileSystem.h:169
Result makeDirectories(Span< const StringSpan > directories)
Creates new directories that do not already exist.
Result write(StringSpan file, Span< const char > data)
Writes a block of memory to a file.
Result makeDirectoriesIfNotExists(Span< const StringSpan > directories)
Creates new directories, if they don't already exist at the given path.
Result copyDirectory(StringSpan source, StringSpan destination, CopyFlags copyFlags=CopyFlags())
Copy a single directory.
Definition FileSystem.h:121
Result createSymbolicLink(StringSpan sourceFileOrDirectory, StringSpan linkFile)
Creates a symbolic link at location linkFile pointing at sourceFileOrDirectory.
Result copyDirectories(Span< const CopyOperation > sourceDestination)
Copy many directories.
Result copyFile(StringSpan source, StringSpan destination, CopyFlags copyFlags=CopyFlags())
Copy a single file.
Definition FileSystem.h:102
Result makeDirectoryIfNotExists(StringSpan directory)
Creates a new directory, if it doesn't already exists at the given path.
Definition FileSystem.h:203
Result removeEmptyDirectory(StringSpan directory)
Removes an empty directory.
Definition FileSystem.h:181
bool existsAndIsLink(StringSpan file)
Check if a link exists at given path.
Result writeString(StringSpan file, StringSpan text)
Replace the entire content of a file with the provided StringSpan.
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
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:10
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:31
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition Time.h:192