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