lfilesystem  0.0.1
C++ filesystem library
lfilesystem_FilesystemEntry.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <cstdint> // for uintmax_t
18 #include <filesystem> // for copy_options, path, perm_options
19 #include <string> // for string
20 #include <functional> // for std::hash
21 #include <optional>
22 #include <ostream>
23 #include "lfilesystem/lfilesystem_Export.h"
25 
32 namespace limes::files
33 {
34 
35 class Directory;
36 class File;
37 class SymLink;
38 class Volume;
39 
43 using Path = std::filesystem::path;
44 
79 class LFILE_EXPORT FilesystemEntry
80 {
81 public:
84 
86  using PermOptions = std::filesystem::perm_options;
87 
89  using CopyOptions = std::filesystem::copy_options;
90 
92  using Time = std::filesystem::file_time_type;
93 
95 
98 
103  FilesystemEntry() = default;
104 
111  explicit FilesystemEntry (const Path& pathToUse);
112 
114 
116  virtual ~FilesystemEntry() = default;
117 
118  FilesystemEntry (const FilesystemEntry&) = default;
119  FilesystemEntry& operator= (const FilesystemEntry&) = default;
120 
121  FilesystemEntry (FilesystemEntry&&) = default;
122  FilesystemEntry& operator= (FilesystemEntry&&) = default;
123 
126 
133  FilesystemEntry& operator= (const Path& newPath);
134  FilesystemEntry& operator= (const std::string_view& newPath);
135  FilesystemEntry& assignPath (const Path& newPath) noexcept;
137 
140 
146  [[nodiscard]] bool operator== (const FilesystemEntry& other) const noexcept;
147  [[nodiscard]] bool operator== (const Path& other) const noexcept;
148  [[nodiscard]] bool operator!= (const FilesystemEntry& other) const noexcept;
149  [[nodiscard]] bool operator!= (const Path& other) const noexcept;
151 
154 
160  [[nodiscard]] bool operator<(const FilesystemEntry& other) const noexcept;
161  [[nodiscard]] bool operator> (const FilesystemEntry& other) const noexcept;
162  [[nodiscard]] bool operator<(const Path& other) const noexcept;
163  [[nodiscard]] bool operator> (const Path& other) const noexcept;
165 
168 
185  [[nodiscard]] FilesystemEntry operator/ (const std::string_view& subpathName) const;
186 
203  FilesystemEntry& operator/= (const std::string_view& subpathName);
204 
219  FilesystemEntry& changeName (const std::string_view& newName);
220 
245  bool makeAbsoluteRelativeTo (const Path& basePath) noexcept;
246 
253  bool makeAbsoluteRelativeToCWD() noexcept;
254 
256 
259 
272  [[nodiscard]] Path getPath (bool makePreferred = false) const noexcept;
273 
285  [[nodiscard]] Path getAbsolutePath (bool makePreferred = false) const noexcept;
286 
290  operator Path() const noexcept;
291 
293  [[nodiscard]] std::string getName() const noexcept;
294 
314  [[nodiscard]] Directory getDirectory() const;
315 
335  [[nodiscard]] Directory getParentDirectory() const;
336 
348  [[nodiscard]] bool isBelow (const Directory& directory, std::size_t depthLimit = 50) const;
349 
357  [[nodiscard]] bool isAbsolutePath() const noexcept;
358 
366  [[nodiscard]] bool isRelativePath() const noexcept;
367 
372  [[nodiscard]] bool isValid() const noexcept;
373 
378  [[nodiscard]] bool isHidden() const;
379 
381  [[nodiscard]] FilesystemEntry getSibling (const std::string_view& siblingName) const;
382 
384 
387 
394  [[nodiscard]] virtual bool isFile() const noexcept;
395 
399  [[nodiscard]] virtual bool isDirectory() const noexcept;
400 
404  [[nodiscard]] virtual bool isSymLink() const noexcept;
405 
409  [[nodiscard]] bool exists() const noexcept;
410 
416  operator bool() const noexcept;
417 
422  [[nodiscard]] virtual std::uintmax_t sizeInBytes() const;
423 
427  [[nodiscard]] Time getLastModificationTime() const noexcept;
428 
432  [[nodiscard]] std::optional<Volume> getVolume() const noexcept;
433 
435 
438 
444  [[nodiscard]] std::optional<File> getFileObject() const noexcept;
445 
451  [[nodiscard]] std::optional<Directory> getDirectoryObject() const noexcept;
452 
458  [[nodiscard]] std::optional<SymLink> getSymLinkObject() const noexcept;
459 
461 
464 
472  virtual bool createIfDoesntExist() const noexcept;
473 
480  bool deleteIfExists() const noexcept;
481 
488  bool moveToTrash() noexcept;
489 
496  void touch() const;
497 
504  bool touch_noCreate() const;
505 
517  bool rename (const Path& newPath) noexcept;
518 
520 
523 
530  bool setPermissions (FSPerms permissions, PermOptions options = PermOptions::replace) const noexcept;
531 
535  [[nodiscard]] Permissions getPermissions() const;
536 
538 
541 
545  bool copyTo (const Path& dest, CopyOptions options = CopyOptions::update_existing) const noexcept;
546 
551  bool copyTo (const FilesystemEntry& dest, CopyOptions options = CopyOptions::update_existing) const noexcept;
552 
559  std::optional<FilesystemEntry> copyToDirectory (const Path& destDirectory, CopyOptions options = CopyOptions::update_existing) const noexcept;
561 
567  bool copyFrom (const Path& source, CopyOptions options = CopyOptions::update_existing) const noexcept;
568  bool copyFrom (const FilesystemEntry& source, CopyOptions options = CopyOptions::update_existing) const noexcept;
570 
576  bool revealToUserInFileBrowser() const;
577 
578 private:
579  bool filenameBeginsWithDot() const;
580 
581  Path path;
582 };
583 
589 std::ostream& operator<< (std::ostream& os, const FilesystemEntry& value);
590 
591 } // namespace limes::files
592 
593 namespace std
594 {
595 
602 template <>
603 struct LFILE_EXPORT hash<limes::files::FilesystemEntry> final
604 {
605  size_t operator() (const limes::files::FilesystemEntry& e) const noexcept;
606 };
607 } // namespace std
This class represents a directory on the filesystem.
This class represents a file on the filesystem.
The base class for any kind of object on the filesystem.
bool makeAbsoluteRelativeToCWD() noexcept
Similar to makeAbsoluteRelativeTo(), but uses the current working directory as the base path.
bool operator<(const FilesystemEntry &other) const noexcept
Lexicographically compares this filesystem entry's absolute path with another path.
FilesystemEntry()=default
Creates a FilesystemEntry with an empty path.
std::filesystem::copy_options CopyOptions
Options bitfield for copying.
FilesystemEntry & changeName(const std::string_view &newName)
Changes the last section of the path (the filename).
FilesystemEntry & assignPath(const Path &newPath) noexcept
Assigns the path this filesystem entry represents.
bool makeAbsoluteRelativeTo(const Path &basePath) noexcept
If the path this object holds is already absolute, this function does nothing and returns false.
virtual ~FilesystemEntry()=default
Destructor.
std::filesystem::file_time_type Time
A time point used for filesystem time.
std::filesystem::perm_options PermOptions
Options bitfield for setting permissions.
bool operator<(const Path &other) const noexcept
Lexicographically compares this filesystem entry's absolute path with another path.
FilesystemEntry(const Path &pathToUse)
Creates a FilesystemEntry referring to the specified path.
This class encapsulates the standard library's permissions bitmask type, and provides some higher-lev...
This class represents a logical filesystem volume.
std::filesystem::perms FSPerms
A typedef for the standard library permissions type.
std::filesystem::path Path
Convenience typedef for filesystem paths.
This file defines the Permissions class.
Filesystem utilities.