]> git.tdb.fi Git - libs/core.git/blob - source/fs/stat.h
Add move semantics to Variant
[libs/core.git] / source / fs / stat.h
1 #ifndef MSP_FS_STAT_H_
2 #define MSP_FS_STAT_H_
3
4 #include <cstdint>
5 #include <string>
6 #include <msp/core/mspcore_api.h>
7 #include <msp/time/timestamp.h>
8 #include "path.h"
9
10 namespace Msp {
11 namespace FS {
12
13 enum FileType
14 {
15         UNKNOWN,
16         REGULAR,
17         DIRECTORY,
18         SYMLINK
19 };
20
21 typedef uint64_t FileSize;
22
23 /**
24 Holds file information.
25 */
26 class MSPCORE_API Stat
27 {
28 private:
29         struct Private;
30
31         struct OwnerInfo
32         {
33                 std::string owner;
34                 std::string group;
35         };
36
37         bool exists = false;
38         FileType type = UNKNOWN;
39         FileSize size = 0;
40         FileSize alloc_size = 0;
41         Time::TimeStamp mtime;
42         mutable OwnerInfo owner_info;
43         Private *priv = nullptr;
44
45 public:
46         Stat() = default;
47         Stat(const Stat &);
48         Stat &operator=(const Stat &);
49         ~Stat();
50
51         FileType get_type() const { return type; }
52         bool is_regular() const { return type==REGULAR; }
53         bool is_directory() const { return type==DIRECTORY; }
54         bool is_symlink() const { return type==SYMLINK; }
55         FileSize get_size() const { return size; }
56         FileSize get_alloc_size() const { return alloc_size; }
57         const Time::TimeStamp &get_modify_time() const { return mtime; }
58         const std::string &get_owner() const;
59         const std::string &get_group() const;
60
61         operator bool() const { return exists; }
62
63         /// Returns a Stat object describing a file.
64         static Stat stat(const Path &);
65         static Stat lstat(const Path &);
66 };
67
68 /// Convenience wrapper for Stat::stat
69 inline Stat stat(const Path &path)
70 { return Stat::stat(path); }
71
72 /// Convenience wrapper for Stat::lstat
73 inline Stat lstat(const Path &path)
74 { return Stat::lstat(path); }
75
76 /// Tests for existence of a file
77 MSPCORE_API bool exists(const Path &path);
78
79 /// Tests whether a path refers to an existing regular file
80 inline bool is_reg(const Path &path)
81 { return stat(path).is_regular(); }
82
83 /// Tests whether a path refers to an existing directory
84 inline bool is_dir(const Path &path)
85 { return stat(path).is_directory(); }
86
87 /// Tests whether a path refers to a symbolic link
88 inline bool is_link(const Path &path)
89 { return lstat(path).is_symlink(); }
90
91 } // namespace FS
92 } // namespace Msp
93
94 #endif