]> git.lyx.org Git - lyx.git/blob - src/support/FileInfo.h
Remove executable status info from typeIndicator.
[lyx.git] / src / support / FileInfo.h
1 // -*- C++ -*-
2 /**
3  * \file FileInfo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FILE_INFO_H
13 #define FILE_INFO_H
14
15 #include <boost/utility.hpp>
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #include <ctime>
22 #include <string>
23
24
25 namespace lyx {
26 namespace support {
27
28 /** Use objects of this class to get information about files.
29  *
30  * Users must make sure to check fi.isOK() before any operations
31  * requiring the file to exist such as fi.isDir()
32  */
33 class FileInfo : boost::noncopyable {
34 public:
35         ///
36         FileInfo();
37
38         /** Get information about file.
39             If link is true, the information is about the link itself, not
40             the file that is obtained by tracing the links. */
41         explicit FileInfo(std::string const & path, bool link = false);
42
43         /// File descriptor
44         explicit FileInfo(int fildes);
45
46         /// Query a new file
47         FileInfo & newFile(std::string const & path, bool link = false);
48
49         /// Query a new file descriptor
50         FileInfo & newFile(int fildes);
51
52         /// Returns a character describing file type (ls -F)
53         char typeIndicator() const;
54
55         /// File protection mode
56         mode_t getMode() const;
57
58         /// Constructs standard mode string (ls style)
59         std::string modeString() const;
60
61         ///
62         time_t getModificationTime() const;
63
64         ///
65         time_t getAccessTime() const;
66
67         ///
68         time_t getStatusChangeTime() const;
69
70         /// Total file size in bytes
71         off_t getSize() const;
72
73         /// User ID of owner
74         uid_t getUid() const;
75
76         /// Group ID of owner
77         gid_t getGid() const;
78
79         /// Is the file information correct? Did the query succeed?
80         bool isOK() const;
81
82         /// Permission flags
83         enum perm_test {
84                 /// test for read permission
85                 rperm = R_OK,
86                 /// test for write permission
87                 wperm = W_OK,
88                 /// test for execute (search) permission
89                 xperm = X_OK,
90                 /// test for existence of file
91                 eperm = F_OK
92         };
93         /// Test whether the current user has a given set of permissions
94         bool access(int p) const;
95         /// Is the file writable for the current user?
96         bool writable() const { return access(FileInfo::wperm); }
97         /// Is the file readable for the current user?
98         bool readable() const { return access(FileInfo::rperm); }
99         /// Is the file executable for the current user?
100         bool executable() const { return access(FileInfo::xperm); }
101         /// Does the file exist?
102         bool exist() const { return access(FileInfo::eperm); }
103         ///
104         bool isLink() const;
105         ///
106         bool isRegular() const;
107         ///
108         bool isDir() const;
109         ///
110         bool isChar() const;
111         ///
112         bool isBlock() const;
113         ///
114         bool isFifo() const;
115         ///
116         bool isSocket() const;
117         ///
118         int getError() const;
119         ///
120         enum Err {
121                 ///
122                 NoErr = -1
123         };
124 private:
125         ///
126         void init();
127         ///
128         void dostat(bool);
129
130         ///
131         struct stat buf_;
132         ///
133         int status_;
134         ///
135         int err_;
136         ///
137         std::string fname_;
138 };
139
140 } // namespace support
141 } // namespace lyx
142
143 #endif