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/Result.h"
5#include "../Strings/SmallString.h"
6#include "../Time/Time.h"
7
8namespace SC
9{
10struct SC_COMPILER_EXPORT FileSystem;
11struct StringConverter;
12} // namespace SC
13
16
19
25{
26 private:
28
29 public:
30 bool preciseErrorMessages = false;
31
35 Result init(StringView initialDirectory);
36
41
43 struct CopyFlags
44 {
45 CopyFlags()
46 {
47 overwrite = false;
48 useCloneIfSupported = true;
49 }
50
55 {
56 overwrite = value;
57 return *this;
58 }
59
64 {
65 useCloneIfSupported = value;
66 return *this;
67 }
68
69 private:
70 friend struct FileSystem;
71 bool overwrite;
72 bool useCloneIfSupported;
73 };
74
77 {
81 };
82
89 [[nodiscard]] Result copyFiles(Span<const CopyOperation> sourceDestination);
90
99 [[nodiscard]] Result copyFile(StringView source, StringView destination, CopyFlags copyFlags = CopyFlags())
100 {
101 return copyFiles(CopyOperation{source, destination, copyFlags});
102 }
103
108 [[nodiscard]] Result copyDirectories(Span<const CopyOperation> sourceDestination);
109
118 [[nodiscard]] Result copyDirectory(StringView source, StringView destination, CopyFlags copyFlags = CopyFlags())
119 {
120 return copyDirectories(CopyOperation{source, destination, copyFlags});
121 }
122
127
132 [[nodiscard]] Result removeFile(StringView source) { return removeFiles({source}); }
133
137 [[nodiscard]] Result removeFileIfExists(StringView source);
138
142 [[nodiscard]] Result removeLinkIfExists(StringView source);
143
149
157 {
158 return removeDirectoriesRecursive({directory});
159 }
160
166
171 [[nodiscard]] Result removeEmptyDirectory(StringView directory) { return removeEmptyDirectories({directory}); }
172
178
184 {
185 return removeEmptyDirectoriesRecursive({directory});
186 }
187
193
198 [[nodiscard]] Result makeDirectory(StringView directory) { return makeDirectories({directory}); }
199
204
209 {
210 return makeDirectoriesIfNotExists({directory});
211 }
212
218
223 [[nodiscard]] Result makeDirectoryRecursive(StringView directory) { return makeDirectoriesRecursive({directory}); }
224
229 [[nodiscard]] Result createSymbolicLink(StringView sourceFileOrDirectory, StringView linkFile);
230
235 [[nodiscard]] bool exists(StringView fileOrDirectory);
236
240 [[nodiscard]] bool existsAndIsDirectory(StringView directory);
241
248 [[nodiscard]] bool existsAndIsFile(StringView file);
249
253 [[nodiscard]] bool existsAndIsLink(StringView file);
254
259 [[nodiscard]] bool moveDirectory(StringView sourceDirectory, StringView destinationDirectory);
260
268 [[nodiscard]] Result write(StringView file, Span<const char> data);
269 [[nodiscard]] Result write(StringView file, Span<const uint8_t> data);
270
276 [[nodiscard]] Result read(StringView file, Vector<char>& data);
277 [[nodiscard]] Result read(StringView file, Vector<uint8_t>& data);
278
284 [[nodiscard]] Result writeString(StringView file, StringView text);
285
291
298 [[nodiscard]] Result read(StringView file, String& data, StringEncoding encoding);
299
301 struct FileStat
302 {
303 size_t fileSize = 0;
305 };
310 [[nodiscard]] Result getFileStat(StringView file, FileStat& fileStat);
311
317
318 private:
319 [[nodiscard]] bool convert(const StringView file, String& destination, StringView* encodedPath = nullptr);
320
321 StringNative<128> fileFormatBuffer1 = StringEncoding::Native;
322 StringNative<128> fileFormatBuffer2 = StringEncoding::Native;
323 StringNative<128> errorMessageBuffer = StringEncoding::Native;
324
325 Result formatError(int errorNumber, StringView item, bool isWindowsNativeError);
326 struct Internal;
327
328 // Fallbacks
329 [[nodiscard]] static Result fallbackCopyDirectory(String& sourceDirectory, String& destinationDirectory,
330 FileSystem::CopyFlags options);
331 [[nodiscard]] static Result fallbackRemoveDirectoryRecursive(String& sourceDirectory);
332};
#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.
Specify copy options like overwriting existing files.
Definition: FileSystem.h:44
CopyFlags & setUseCloneIfSupported(bool value)
If true copy will use native filesystem clone os api.
Definition: FileSystem.h:63
CopyFlags & setOverwrite(bool value)
If true copy will overwrite existing files in the destination.
Definition: FileSystem.h:54
Specify source, destination and flags for a copy operation.
Definition: FileSystem.h:77
StringView destination
Copy operation sink (can be a {relative | absolute} {file | directory} path)
Definition: FileSystem.h:79
CopyFlags copyFlags
Copy operation flags (overwrite, use clone api etc.)
Definition: FileSystem.h:80
StringView source
Copy operation source (can be a {relative | absolute} {file | directory} path)
Definition: FileSystem.h:78
A structure to describe modified time.
Definition: FileSystem.h:302
Time::Absolute modifiedTime
Time when file was last modified.
Definition: FileSystem.h:304
Execute fs operations { exists, copy, delete } for { files and directories }.
Definition: FileSystem.h:25
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:118
Result setLastModifiedTime(StringView file, Time::Absolute time)
Change last modified time of a given file.
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:132
Result removeEmptyDirectoryRecursive(StringView directory)
Removes an empty directory that only contains other empty directories (but no files)
Definition: FileSystem.h:183
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 read(StringView file, Vector< char > &data)
Reads contents of a file into a SC::Vector buffer.
Result write(StringView file, Span< const char > data)
Writes a block of memory to a 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:156
Result removeEmptyDirectory(StringView directory)
Removes an empty directory.
Definition: FileSystem.h:171
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.
bool preciseErrorMessages
Formats errors in an internal buffer when returning failed Result.
Definition: FileSystem.h:30
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:99
Result makeDirectoryRecursive(StringView directory)
Create a new directory, creating also intermediate non existing directories (like posix mkdir -p)
Definition: FileSystem.h:223
Result makeDirectory(StringView directory)
Creates a new directory that does not already exist.
Definition: FileSystem.h:198
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:208
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
Absolute time represented with milliseconds since epoch.
Definition: Time.h:74
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51