CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
path.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_FILESYSTEM_PATH_H
10#define CPPCOMMON_FILESYSTEM_PATH_H
11
12#include "common/flags.h"
13#include "string/encoding.h"
14#include "string/format.h"
15#include "time/timestamp.h"
16
17#include <string>
18
19namespace CppCommon {
20
22enum class FileType
23{
24 NONE,
25 REGULAR,
26 DIRECTORY,
27 SYMLINK,
28 BLOCK,
29 CHARACTER,
30 FIFO,
31 SOCKET,
32 UNKNOWN
33};
34
37{
38 NONE = 0x00,
39 NORMAL = 0x01,
40 ARCHIVED = 0x02,
41 HIDDEN = 0x04,
42 INDEXED = 0x08,
43 OFFLINE = 0x10,
44 READONLY = 0x20,
45 SYSTEM = 0x40,
46 TEMPORARY = 0x80
47};
48
51{
52 NONE = 00000,
53 IRUSR = 00400,
54 IWUSR = 00200,
55 IXUSR = 00100,
56 IRWXU = 00700,
57 IRGRP = 00040,
58 IWGRP = 00020,
59 IXGRP = 00010,
60 IRWXG = 00070,
61 IROTH = 00004,
62 IWOTH = 00002,
63 IXOTH = 00001,
64 IRWXO = 00007,
65 ISUID = 04000,
66 ISGID = 02000,
67 ISVTX = 01000
68};
69
72{
73 uint64_t capacity;
74 uint64_t free;
75 uint64_t available;
76};
77
79
89class Path
90{
91public:
93 Path() : _path() {}
95
98 Path(const char* path) : _path(path) {}
100
103 Path(const std::string& path) : _path(path) {}
105
108 Path(const wchar_t* path) : _path(Encoding::ToUTF8(path)) {}
110
113 Path(const std::wstring& path) : _path(Encoding::ToUTF8(path)) {}
114 Path(const Path&) = default;
115 Path(Path&&) = default;
116 ~Path() = default;
117
118 Path& operator=(const Path&) = default;
119 Path& operator=(Path&&) = default;
120
122 explicit operator bool() const noexcept { return !empty(); }
123
124 // Append the given path with a path separator
125 Path& operator/=(const Path& path)
126 { return Append(path); }
127 friend Path operator/(const Path& path1, const Path& path2)
128 { return Path(path1).Append(path2); }
129
130 // Concatenate the given path without a path separator
131 Path& operator+=(const Path& path)
132 { return Concat(path); }
133 friend Path operator+(const Path& path1, const Path& path2)
134 { return Path(path1).Concat(path2); }
135
136 // Path comparison
137 friend bool operator==(const Path& path1, const Path& path2)
138 { return path1._path == path2._path; }
139 friend bool operator!=(const Path& path1, const Path& path2)
140 { return path1._path != path2._path; }
141 friend bool operator<(const Path& path1, const Path& path2)
142 { return path1._path < path2._path; }
143 friend bool operator>(const Path& path1, const Path& path2)
144 { return path1._path > path2._path; }
145 friend bool operator<=(const Path& path1, const Path& path2)
146 { return path1._path <= path2._path; }
147 friend bool operator>=(const Path& path1, const Path& path2)
148 { return path1._path >= path2._path; }
149
151 const std::string& string() const noexcept { return _path; }
153 std::wstring wstring() const { return Encoding::FromUTF8(_path); }
154
156 Path root() const;
158 Path relative() const;
160 Path parent() const;
162 Path filename() const;
164 Path stem() const;
166 Path extension() const;
167
169 Path absolute() const;
171 Path canonical() const;
173 Path validate(char placeholder = '_') const;
174
176 FileType type() const;
182 UtcTimestamp created() const;
184 UtcTimestamp modified() const;
186 size_t hardlinks() const;
188 SpaceInfo space() const;
189
191 bool empty() const noexcept { return _path.empty(); }
192
194 bool HasRoot() const { return !root().empty(); }
196 bool HasRelative() const { return !relative().empty(); }
198 bool HasParent() const { return !parent().empty(); }
200 bool HasFilename() const { return !filename().empty(); }
202 bool HasStem() const { return !stem().empty(); }
204 bool HasExtension() const { return !extension().empty(); }
205
207 bool IsAbsolute() const { return HasRoot(); }
209 bool IsRelative() const { return !HasRoot(); }
210
212 bool IsExists() const { return type() != FileType::NONE; }
213
215 bool IsRegularFile() const { return type() == FileType::REGULAR; }
217 bool IsDirectory() const { return type() == FileType::DIRECTORY; }
219 bool IsSymlink() const { return type() == FileType::SYMLINK; }
221 bool IsOther() const;
222
224 bool IsEquivalent(const Path& path) const;
225
227 void Clear() noexcept { return _path.clear(); }
228
230 Path& Assign(const Path& path);
232 Path& Append(const Path& path);
234 Path& Concat(const Path& path);
241
248
250 static bool deprecated(char character) noexcept;
252 static bool deprecated(wchar_t character) noexcept;
254 static std::string deprecated();
255
257 static char separator() noexcept;
258
260 static Path initial();
262 static Path current();
264 static Path executable();
266 static Path home();
268 static Path temp();
270 static Path unique();
271
273
279 static Path Copy(const Path& src, const Path& dst, bool overwrite = false);
281
288 static Path CopyIf(const Path& src, const Path& dst, const std::string& pattern = "", bool overwrite = false);
290
296 static Path CopyAll(const Path& src, const Path& dst, bool overwrite = false);
298
303 static Path Rename(const Path& src, const Path& dst);
305
309 static Path Remove(const Path& path);
311
315 static Path RemoveAll(const Path& path);
317
324 static Path RemoveIf(const Path& path, const std::string& pattern = "");
325
327
331 static void SetAttributes(const Path& path, const Flags<FileAttributes>& attributes);
333
337 static void SetPermissions(const Path& path, const Flags<FilePermissions>& permissions);
339
343 static void SetCreated(const Path& path, const UtcTimestamp& timestamp);
345
349 static void SetModified(const Path& path, const UtcTimestamp& timestamp);
350
352
355 static void SetCurrent(const Path& path);
356
358
361 static void Touch(const Path& path);
362
364 friend std::istream& operator>>(std::istream& is, Path& path)
365 { is >> path._path; return is; }
367 friend std::ostream& operator<<(std::ostream& os, const Path& path)
368 { os << path._path; return os; }
369
371 void swap(Path& path) noexcept;
372 friend void swap(Path& path1, Path& path2) noexcept;
373
374protected:
376 std::string _path;
377};
378
381} // namespace CppCommon
382
383#include "path.inl"
384
385// Resolve forward declaration issue
387
388#endif // CPPCOMMON_FILESYSTEM_PATH_H
Encoding utilities.
Definition encoding.h:24
static std::wstring FromUTF8(std::string_view str)
Convert UTF-8 encoded string to system wide-string.
Definition encoding.cpp:38
Enum-based flags.
Definition flags.h:65
Filesystem path.
Definition path.h:90
Path root() const
Decompose root path from the current path.
Definition path.cpp:132
Path absolute() const
Transform the current path to the real path on a filesystem.
Definition path.cpp:310
Path(const std::wstring &path)
Initialize path with a given wide string value.
Definition path.h:113
Path & ReplaceExtension(const Path &extension)
Replace the current path extension with a given one.
Definition path.cpp:792
bool IsSymlink() const
Is the path points to symbolic link?
Definition path.h:219
static Path unique()
Get the unique filename in UUID format "00000000-0000-0000-0000-000000000000".
Definition path.cpp:1012
UtcTimestamp created() const
Get the path created UTC timestamp.
Definition path.cpp:541
static Path RemoveAll(const Path &path)
Recursively remove the given path (file, empty directory, symlink, etc) from the filesystem.
Definition path.cpp:1282
Path & Assign(const Path &path)
Assign the given path to the current one.
Definition path.inl:20
friend void swap(Path &path1, Path &path2) noexcept
Definition path.inl:38
Path(const std::string &path)
Initialize path with a given string value.
Definition path.h:103
std::wstring wstring() const
Get the path value as a wide string.
Definition path.h:153
Path(const wchar_t *path)
Initialize path with a given wide C-string value.
Definition path.h:108
bool HasStem() const
Has stem?
Definition path.h:202
size_t hardlinks() const
Get the path count of hardlinks.
Definition path.cpp:614
Path validate(char placeholder='_') const
Transform the current path and replace all deprecated characters with a given placeholder (default is...
Definition path.cpp:412
friend bool operator>=(const Path &path1, const Path &path2)
Definition path.h:147
Path & RemoveExtension()
Remove the current path extension.
Definition path.h:245
bool HasExtension() const
Has extension?
Definition path.h:204
static Path CopyIf(const Path &src, const Path &dst, const std::string &pattern="", bool overwrite=false)
Copy all matched files from the the given source path to destination path (files, directories,...
Definition path.cpp:1118
Flags< FileAttributes > attributes() const
Get the path file attributes.
Definition path.cpp:472
Path()
Initialize path with an empty value.
Definition path.h:93
bool IsDirectory() const
Is the path points to directory?
Definition path.h:217
Path & RemoveTrailingSeparators()
Remove all trailing separators form the current path.
Definition path.cpp:831
friend bool operator!=(const Path &path1, const Path &path2)
Definition path.h:139
static Path Copy(const Path &src, const Path &dst, bool overwrite=false)
Copy the given source path to destination path (file, empty directory, symlink, etc)
Definition path.cpp:1017
Path & operator=(const Path &)=default
Path & operator=(Path &&)=default
Path & operator/=(const Path &path)
Definition path.h:125
Path relative() const
Decompose relative path from the current path.
Definition path.cpp:137
static Path Remove(const Path &path)
Remove the given path (file, empty directory, symlink, etc) from the filesystem.
Definition path.cpp:1209
Path & Concat(const Path &path)
Concatenate the given path to the current one.
Definition path.inl:26
bool IsOther() const
Is the path points to special file (block, character, FIFO, socket)?
Definition path.inl:14
bool IsRegularFile() const
Is the path points to regular file?
Definition path.h:215
bool HasParent() const
Has parent path?
Definition path.h:198
bool HasRoot() const
Has root path?
Definition path.h:194
Path(const Path &)=default
static Path initial()
Get the initial path of the process.
Definition path.cpp:875
bool IsExists() const
Is the path exists?
Definition path.h:212
bool IsAbsolute() const
Is absolute path?
Definition path.h:207
static Path home()
Get the home path of the process.
Definition path.cpp:945
Path & RemoveFilename()
Remove the current path filename.
Definition path.h:243
static void SetCreated(const Path &path, const UtcTimestamp &timestamp)
Set created UTC timestamp for the given path.
Definition path.cpp:1390
friend bool operator>(const Path &path1, const Path &path2)
Definition path.h:143
~Path()=default
Path & ReplaceFilename(const Path &filename)
Replace the current path filename with a given one.
Definition path.cpp:766
friend bool operator<=(const Path &path1, const Path &path2)
Definition path.h:145
Path stem() const
Decompose stem from the current path.
Definition path.cpp:232
friend bool operator==(const Path &path1, const Path &path2)
Definition path.h:137
bool HasFilename() const
Has filename?
Definition path.h:200
static void SetAttributes(const Path &path, const Flags< FileAttributes > &attributes)
Set file attributes for the given path.
Definition path.cpp:1309
static void SetModified(const Path &path, const UtcTimestamp &timestamp)
Set modified UTC timestamp for the given path.
Definition path.cpp:1434
SpaceInfo space() const
Get the path space information.
Definition path.cpp:644
friend std::ostream & operator<<(std::ostream &os, const Path &path)
Output instance into the given output stream.
Definition path.h:367
Path & MakePreferred()
Convert all path separators to system ones ('\' for Windows or '/' for Unix)
Definition path.cpp:756
static char separator() noexcept
Get the system path separator character ('\' for Windows or '/' for Unix)
Definition path.cpp:866
Path parent() const
Decompose parent path from the current path.
Definition path.cpp:144
static void Touch(const Path &path)
Touch the given path and set its modified UTC timestamp to the current value.
Definition path.cpp:1491
static Path CopyAll(const Path &src, const Path &dst, bool overwrite=false)
Recursively copy the given source path to destination path (files, directories, symlinks,...
Definition path.cpp:1156
Path canonical() const
Transform the current path and replace all '.' and '..' properly.
Definition path.cpp:349
friend bool operator<(const Path &path1, const Path &path2)
Definition path.h:141
Path & operator+=(const Path &path)
Definition path.h:131
Path(const char *path)
Initialize path with a given C-string value.
Definition path.h:98
Path extension() const
Decompose extension from the current path.
Definition path.cpp:280
static void SetCurrent(const Path &path)
Set the given path of the process as a current one.
Definition path.cpp:1478
bool empty() const noexcept
Is the path empty?
Definition path.h:191
Path & Append(const Path &path)
Append the given path to the current one.
Definition path.cpp:737
Path(Path &&)=default
Path filename() const
Decompose filename from the current path.
Definition path.cpp:209
Flags< FilePermissions > permissions() const
Get the path file permissions.
Definition path.cpp:499
FileType type() const
Get the path file type.
Definition path.cpp:423
static std::string deprecated()
Get filesystem deprecated characters ('\', '/', '?', '', '*', ':', '|', '"', '<', '>')
Definition path.cpp:861
static Path temp()
Get the temporary path of the process.
Definition path.cpp:987
friend Path operator/(const Path &path1, const Path &path2)
Definition path.h:127
friend Path operator+(const Path &path1, const Path &path2)
Definition path.h:133
static void SetPermissions(const Path &path, const Flags< FilePermissions > &permissions)
Set file permissions for the given path.
Definition path.cpp:1355
static Path RemoveIf(const Path &path, const std::string &pattern="")
Recursively remove the given path matched to the given pattern (file, empty directory,...
Definition path.cpp:1248
UtcTimestamp modified() const
Get the path modified UTC timestamp.
Definition path.cpp:577
bool IsEquivalent(const Path &path) const
Is the current path is equivalent to the given one (points to the same node on a filesystem)?
Definition path.cpp:676
static Path Rename(const Path &src, const Path &dst)
Rename the given source path to destination path (file, empty directory, symlink, etc)
Definition path.cpp:1196
bool IsRelative() const
Is relative path?
Definition path.h:209
static Path current()
Get the current path of the process.
Definition path.cpp:880
bool HasRelative() const
Has relative path?
Definition path.h:196
std::string _path
Path string.
Definition path.h:376
void Clear() noexcept
Clear the path content.
Definition path.h:227
const std::string & string() const noexcept
Get the path value as UTF-8 string.
Definition path.h:151
static Path executable()
Get the executable path of the process.
Definition path.cpp:910
Encoding utilities definition.
File system exceptions definition.
Enum-based flags definition.
Format string definition.
C++ Common project definitions.
FileType
File types.
Definition path.h:23
@ SYMLINK
Symbolic link.
@ DIRECTORY
Directory.
@ CHARACTER
Character device.
@ BLOCK
Block device.
@ REGULAR
Regular file.
@ NONE
None (file not found)
@ FIFO
FIFO (named pipe)
FilePermissions
File permissions (Unix specific)
Definition path.h:51
@ IXOTH
Execute or search permission bit for other users.
@ IWUSR
Write permission bit for the owner of the file.
@ IRWXG
This is equivalent to IRGRP | IWGRP | IXGRP.
@ IWGRP
Write permission bit for the group owner of the file.
@ IXUSR
Execute (for ordinary files) or search (for directories) permission bit for the owner of the file.
@ IRWXO
This is equivalent to IROTH | IWOTH | IXOTH.
@ ISVTX
This is the sticky bit.
@ IRGRP
Read permission bit for the group owner of the file.
@ IRUSR
Read permission bit for the owner of the file.
@ IRWXU
This is equivalent to IRUSR | IWUSR | IXUSR.
@ IXGRP
Execute or search permission bit for the group owner of the file.
@ IWOTH
Write permission bit for other users.
@ IROTH
Read permission bit for other users.
@ ISUID
This is the set-user-ID on execute bit.
@ ISGID
This is the set-group-ID on execute bit.
FileAttributes
File attributes (Windows specific)
Definition path.h:37
Filesystem path inline implementation.
Filesystem space information.
Definition path.h:72
uint64_t capacity
Total size of the filesystem, in bytes.
Definition path.h:73
uint64_t free
Free space on the filesystem, in bytes.
Definition path.h:74
uint64_t available
Free space available to a non-privileged process (may be equal or less than free)
Definition path.h:75
Timestamp definition.