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