Sane C++ Libraries
C++ Platform Abstraction Libraries
FileSystem.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Buffer.h"
5#include "../Foundation/Result.h"
6#include "../Strings/String.h"
7#include "../Time/Time.h"
8
9namespace SC
10{
11struct SC_COMPILER_EXPORT FileSystem;
12struct StringConverter;
13} // namespace SC
14
17
20
28{
29 private:
31
32 public:
33 bool preciseErrorMessages = false;
34
38 Result init(StringView initialDirectory);
39
44
46 struct CopyFlags
47 {
48 CopyFlags()
49 {
50 overwrite = false;
51 useCloneIfSupported = true;
52 }
53
58 {
59 overwrite = value;
60 return *this;
61 }
62
67 {
68 useCloneIfSupported = value;
69 return *this;
70 }
71
72 private:
73 friend struct FileSystem;
74 bool overwrite;
75 bool useCloneIfSupported;
76 };
77
80 {
84 };
85
92 [[nodiscard]] Result copyFiles(Span<const CopyOperation> sourceDestination);
93
102 [[nodiscard]] Result copyFile(StringView source, StringView destination, CopyFlags copyFlags = CopyFlags())
103 {
104 return copyFiles(CopyOperation{source, destination, copyFlags});
105 }
106
111 [[nodiscard]] Result copyDirectories(Span<const CopyOperation> sourceDestination);
112
121 [[nodiscard]] Result copyDirectory(StringView source, StringView destination, CopyFlags copyFlags = CopyFlags())
122 {
123 return copyDirectories(CopyOperation{source, destination, copyFlags});
124 }
125
130
135 [[nodiscard]] Result removeFile(StringView source) { return removeFiles({source}); }
136
140 [[nodiscard]] Result removeFileIfExists(StringView source);
141
145 [[nodiscard]] Result removeLinkIfExists(StringView source);
146
152
160 {
161 return removeDirectoriesRecursive({directory});
162 }
163
169
174 [[nodiscard]] Result removeEmptyDirectory(StringView directory) { return removeEmptyDirectories({directory}); }
175
181
187 {
188 return removeEmptyDirectoriesRecursive({directory});
189 }
190
196
201 [[nodiscard]] Result makeDirectory(StringView directory) { return makeDirectories({directory}); }
202
207
212 {
213 return makeDirectoriesIfNotExists({directory});
214 }
215
221
226 [[nodiscard]] Result makeDirectoryRecursive(StringView directory) { return makeDirectoriesRecursive({directory}); }
227
232 [[nodiscard]] Result createSymbolicLink(StringView sourceFileOrDirectory, StringView linkFile);
233
238 [[nodiscard]] bool exists(StringView fileOrDirectory);
239
243 [[nodiscard]] bool existsAndIsDirectory(StringView directory);
244
251 [[nodiscard]] bool existsAndIsFile(StringView file);
252
256 [[nodiscard]] bool existsAndIsLink(StringView file);
257
262 [[nodiscard]] bool moveDirectory(StringView sourceDirectory, StringView destinationDirectory);
263
271 [[nodiscard]] Result write(StringView file, Span<const char> data);
272 [[nodiscard]] Result write(StringView file, Span<const uint8_t> data);
273
279 [[nodiscard]] Result read(StringView file, Buffer& data);
280
286 [[nodiscard]] Result writeString(StringView file, StringView text);
287
293
300 [[nodiscard]] Result read(StringView file, String& data, StringEncoding encoding);
301
303 struct FileStat
304 {
305 size_t fileSize = 0;
307 };
312 [[nodiscard]] Result getFileStat(StringView file, FileStat& fileStat);
313
319
320 private:
321 [[nodiscard]] bool convert(const StringView file, String& destination, StringView* encodedPath = nullptr);
322
323 StringNative<128> fileFormatBuffer1 = StringEncoding::Native;
324 StringNative<128> fileFormatBuffer2 = StringEncoding::Native;
325 StringNative<128> errorMessageBuffer = StringEncoding::Native;
326
327 Result formatError(int errorNumber, StringView item, bool isWindowsNativeError);
328 struct Internal;
329
330 // Fallbacks
331 [[nodiscard]] static Result fallbackCopyDirectory(String& sourceDirectory, String& destinationDirectory,
332 FileSystem::CopyFlags options);
333 [[nodiscard]] static Result fallbackRemoveDirectoryRecursive(String& sourceDirectory);
334};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
StringEncoding
String Encoding (Ascii, Utf8, Utf16)
Definition: StringIterator.h:17
@ Native
Encoding is UTF8.
An heap allocated byte buffer that can optionally use an inline buffer.
Definition: Buffer.h:28
Specify copy options like overwriting existing files.
Definition: FileSystem.h:47
CopyFlags & setUseCloneIfSupported(bool value)
If true copy will use native filesystem clone os api.
Definition: FileSystem.h:66
CopyFlags & setOverwrite(bool value)
If true copy will overwrite existing files in the destination.
Definition: FileSystem.h:57
Specify source, destination and flags for a copy operation.
Definition: FileSystem.h:80
StringView 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
StringView source
Copy operation source (can be a {relative | absolute} {file | directory} path)
Definition: FileSystem.h:81
A structure to describe modified time.
Definition: FileSystem.h:304
Time::Realtime modifiedTime
Time when file was last modified.
Definition: FileSystem.h:306
Execute fs operations { exists, copy, delete } for { files and directories }.
Definition: FileSystem.h:28
Result createSymbolicLink(StringView sourceFileOrDirectory, StringView linkFile)
Creates a symbolic link at location linkFile pointing at sourceFileOrDirectory.
Result changeDirectory(StringView newDirectory)
Changes current directory.
Result removeEmptyDirectories(Span< const StringView > directories)
Removes multiple empty directories.
bool existsAndIsFile(StringView file)
Check if a file exists at given path.
Result copyDirectory(StringView source, StringView destination, CopyFlags copyFlags=CopyFlags())
Copy a single directory.
Definition: FileSystem.h:121
Result writeString(StringView file, StringView text)
Replace the entire content of a file with the provided StringView.
Result removeFile(StringView source)
Remove a single file.
Definition: FileSystem.h:135
Result removeEmptyDirectoryRecursive(StringView directory)
Removes an empty directory that only contains other empty directories (but no files)
Definition: FileSystem.h:186
Result makeDirectoriesRecursive(Span< const StringView > directories)
Create new directories, creating also intermediate non existing directories (like posix mkdir -p)
Result init(StringView initialDirectory)
Call init function when instantiating the class to set directory for all operations using relative pa...
Result copyFiles(Span< const CopyOperation > sourceDestination)
Copies many files.
bool exists(StringView fileOrDirectory)
Check if a file or directory exists at a given path.
Result makeDirectories(Span< const StringView > directories)
Creates new directories that do not already exist.
Result write(StringView file, Span< const char > data)
Writes a block of memory to a file.
Result setLastModifiedTime(StringView file, Time::Realtime time)
Change last modified time of a given file.
Result getFileStat(StringView file, FileStat &fileStat)
Obtains stats (size, modified time) about a file.
Result removeEmptyDirectoriesRecursive(Span< const StringView > directories)
Removes multiple empty directories that only contains other empty directories (but no files)
bool existsAndIsLink(StringView file)
Check if a link exists at given path.
Result removeFileIfExists(StringView source)
Remove a single file, giving no error if it doesn't exist.
Result removeLinkIfExists(StringView source)
Remove a single link, giving no error if it doesn't exist.
Result removeDirectoryRecursive(StringView directory)
Remove single directory with its entire content (like posix rm -rf)
Definition: FileSystem.h:159
Result removeEmptyDirectory(StringView directory)
Removes an empty directory.
Definition: FileSystem.h:174
Result removeFiles(Span< const StringView > files)
Remove multiple files.
Result writeStringAppend(StringView file, StringView text)
Appends a StringView to a file.
bool existsAndIsDirectory(StringView directory)
Check if a directory exists at given path.
Result read(StringView file, Buffer &data)
Reads contents of a file into a SC::Buffer.
bool preciseErrorMessages
Formats errors in an internal buffer when returning failed Result.
Definition: FileSystem.h:33
Result copyDirectories(Span< const CopyOperation > sourceDestination)
Copy many directories.
bool moveDirectory(StringView sourceDirectory, StringView destinationDirectory)
Moves a directory from source to destination.
Result copyFile(StringView source, StringView destination, CopyFlags copyFlags=CopyFlags())
Copy a single file.
Definition: FileSystem.h:102
Result makeDirectoryRecursive(StringView directory)
Create a new directory, creating also intermediate non existing directories (like posix mkdir -p)
Definition: FileSystem.h:226
Result makeDirectory(StringView directory)
Creates a new directory that does not already exist.
Definition: FileSystem.h:201
Result removeDirectoriesRecursive(Span< const StringView > directories)
Remove multiple directories with their entire content (like posix rm -rf)
Result read(StringView file, String &data, StringEncoding encoding)
Read contents of a file into a string with given encoding.
Result makeDirectoriesIfNotExists(Span< const StringView > directories)
Creates new directories, if they don't already exist at the given path.
Result makeDirectoryIfNotExists(StringView directory)
Creates a new directory, if it doesn't already exists at the given path.
Definition: FileSystem.h:211
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
String with compile time configurable inline storage (small string optimization)
Definition: String.h:168
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:32
A non-modifiable owning string with associated encoding.
Definition: String.h:29
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
Represents a realtime clock in milliseconds since epoch (use Realtime::now for current time)
Definition: Time.h:192