SPH
FileSystem.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "io/Path.h"
8 #include <dirent.h>
9 
11 
12 namespace FileSystem {
13 
18 std::string readFile(const Path& path);
19 
21 bool pathExists(const Path& path);
22 
26 Size fileSize(const Path& path);
27 
29 bool isPathWritable(const Path& path);
30 
33 
37 Path getAbsolutePath(const Path& relativePath);
38 
39 enum class PathType {
40  FILE,
41  DIRECTORY,
42  SYMLINK,
43  OTHER,
44 };
45 
47 Expected<PathType> pathType(const Path& path);
48 
49 
50 enum class CreateDirectoryFlag {
52  ALLOW_EXISTING = 1 << 0
53 };
54 
61 Outcome createDirectory(const Path& path,
63 
64 enum class RemovePathFlag {
67  RECURSIVE = 1 << 1
68 };
69 
74 Outcome removePath(const Path& path, const Flags<RemovePathFlag> flags = EMPTY_FLAGS);
75 
83 Outcome copyFile(const Path& from, const Path& to);
84 
89 Outcome copyDirectory(const Path& from, const Path& to);
90 
92 void setWorkingDirectory(const Path& path);
93 
97 private:
98  Path originalDir;
99 
100 public:
102  originalDir = Path::currentPath();
103  setWorkingDirectory(path);
104  }
105 
107  setWorkingDirectory(originalDir);
108  }
109 };
110 
113  friend class DirectoryAdapter;
114 
115 private:
117  DIR* dir;
118  dirent* entry = nullptr;
119 
120 public:
123 
125  Path operator*() const;
126 
128  bool operator==(const DirectoryIterator& other) const;
129 
131  bool operator!=(const DirectoryIterator& other) const;
132 
133 private:
137  DirectoryIterator(DIR* dir);
138 };
139 
146 private:
147  DIR* dir;
148 
149 public:
155  DirectoryAdapter(const Path& directory);
156 
158 
160 
164  DirectoryIterator begin() const;
165 
167  DirectoryIterator end() const;
168 };
169 
178 DirectoryAdapter iterateDirectory(const Path& directory);
179 
183 Array<Path> getFilesInDirectory(const Path& directory);
184 
189 class FileLock {
190 private:
191  int handle;
192 
193 public:
194  explicit FileLock(const Path& path);
195 
196  void lock();
197 
198  void unlock();
199 };
200 
201 bool isFileLocked(const Path& path);
202 
203 } // namespace FileSystem
204 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Wrapper of type containing either a value or an error message.
Wrapper over enum allowing setting (and querying) individual bits of the stored value.
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define NAMESPACE_SPH_END
Definition: Object.h:12
Return value of function that may fail, containing either SUCCEES (true) or error message.
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
Wrapper of type that either contains a value of given type, or an error message.
Definition: Expected.h:25
Object providing begin and end directory iterator for given directory path.
Definition: FileSystem.h:145
DirectoryIterator begin() const
Returns the directory iterator to the first entry in the directory.
Definition: FileSystem.cpp:330
DirectoryIterator end() const
Returns the directory iterator to the one-past-last entry in the directory.
Definition: FileSystem.cpp:334
DirectoryAdapter(const Path &directory)
Creates the directory adapter for given path.
Definition: FileSystem.cpp:310
Iterator allowing to enumerate files and subdirectories in given directory.
Definition: FileSystem.h:112
Path operator*() const
Return the path of the file. The path is returned relative to the parent directory.
Definition: FileSystem.cpp:296
DirectoryIterator & operator++()
Moves to the next file in the directory.
Definition: FileSystem.cpp:286
bool operator==(const DirectoryIterator &other) const
Checks for equality. Returns true in case both iterators are nullptr.
Definition: FileSystem.cpp:301
bool operator!=(const DirectoryIterator &other) const
Checks for inequality. Returns false in case both iterators are nullptr.
Definition: FileSystem.cpp:305
Locks a file.
Definition: FileSystem.h:189
FileLock(const Path &path)
Definition: FileSystem.cpp:350
ScopedWorkingDirectory(const Path &path)
Definition: FileSystem.h:101
Wrapper of an integral value providing functions for reading and modifying individual bits.
Definition: Flags.h:20
Object representing a path on a filesystem.
Definition: Path.h:17
static Path currentPath()
Returns the current working directory, or empty path if the function failed.
Definition: Path.cpp:166
bool isPathWritable(const Path &path)
Checks whether the given file is writable.
Definition: FileSystem.cpp:35
bool pathExists(const Path &path)
Checks if a file or directory exists (or more precisely, if a file or directory is accessible).
Definition: FileSystem.cpp:21
@ OTHER
Pipe, socket, ...
@ SYMLINK
Symbolic link.
@ DIRECTORY
Directory.
@ FILE
Regular file.
bool isFileLocked(const Path &path)
void setWorkingDirectory(const Path &path)
Changes the current working directory.
Definition: FileSystem.cpp:273
Outcome removePath(const Path &path, const Flags< RemovePathFlag > flags=EMPTY_FLAGS)
Definition: FileSystem.cpp:137
Array< Path > getFilesInDirectory(const Path &directory)
Alternatitve to iterateDirectory, returning all files in directory in an array.
Definition: FileSystem.cpp:342
Outcome copyDirectory(const Path &from, const Path &to)
Copies a directory (and all files and subdirectories it contains) to a different path.
Definition: FileSystem.cpp:247
CreateDirectoryFlag
Definition: FileSystem.h:50
@ ALLOW_EXISTING
If the named directory already exists, function returns SUCCESS instead of error message.
Size fileSize(const Path &path)
Returns the size of a file.
Definition: FileSystem.cpp:29
Outcome createDirectory(const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
Creates a directory with given path. Creates all parent directories as well.
Definition: FileSystem.cpp:119
Outcome copyFile(const Path &from, const Path &to)
Copies a file on given path to a different path.
Definition: FileSystem.cpp:219
DirectoryAdapter iterateDirectory(const Path &directory)
Definition: FileSystem.cpp:338
Expected< PathType > pathType(const Path &path)
Returns the type of the given path, or error message if the function fails.
Definition: FileSystem.cpp:54
std::string readFile(const Path &path)
Reads the whole file into the string.
Definition: FileSystem.cpp:14
Expected< Path > getHomeDirectory()
Returns the home directory of the current user.
Definition: FileSystem.cpp:39
Path getAbsolutePath(const Path &relativePath)
Returns the absolute path to the file.
Definition: FileSystem.cpp:48
Object with deleted copy constructor and copy operator.
Definition: Object.h:54