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