]> git.tdb.fi Git - libs/core.git/blob - source/fs/stat.h
0d5f6de184b6f7a725e648119172573125799375
[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         FileType type;
34         FileSize size;
35         FileSize alloc_size;
36         Time::TimeStamp mtime;
37         std::string owner;
38         std::string group;
39
40         Stat();
41 public:
42         FileType get_type() const { return type; }
43         bool is_regular() const { return type==REGULAR; }
44         bool is_directory() const { return type==DIRECTORY; }
45         bool is_symlink() const { return type==SYMLINK; }
46         FileSize get_size() const { return size; }
47         FileSize get_alloc_size() const { return alloc_size; }
48         const Time::TimeStamp &get_modify_time() const { return mtime; }
49         const std::string &get_owner() const { return owner; }
50         const std::string &get_group() const { return group; }
51
52         /// Returns a Stat object describing a file.
53         static Stat stat(const Path &);
54         static Stat lstat(const Path &);
55 };
56
57 /// Convenience wrapper for Stat::stat
58 inline Stat stat(const Path &path)
59 { return Stat::stat(path); }
60
61 /// Convenience wrapper for Stat::lstat
62 inline Stat lstat(const Path &path)
63 { return Stat::lstat(path); }
64
65 /// Tests for existence of a file
66 bool exists(const Path &path);
67
68 /// Tests whether a path refers to an existing regular file
69 inline bool is_reg(const Path &path)
70 { return stat(path).is_regular(); }
71
72 /// Tests whether a path refers to an existing directory
73 inline bool is_dir(const Path &path)
74 { return stat(path).is_directory(); }
75
76 /// Tests whether a path refers to a symbolic link
77 inline bool is_link(const Path &path)
78 { return lstat(path).is_symlink(); }
79
80 } // namespace FS
81 } // namespace Msp
82
83 #endif