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