]> git.lyx.org Git - lyx.git/blob - src/support/FileInfo.h
* lyxfunctional.h: delete compare_memfun and helper classes
[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         /// Number of hard links
74         nlink_t getNumberOfLinks() const;
75
76         /// User ID of owner
77         uid_t getUid() const;
78
79         /// Group ID of owner
80         gid_t getGid() const;
81
82         /// Is the file information correct? Did the query succeed?
83         bool isOK() const;
84
85         /// Permission flags
86         enum perm_test {
87                 /// test for read permission
88                 rperm = R_OK,
89                 /// test for write permission
90                 wperm = W_OK,
91                 /// test for execute (search) permission
92                 xperm = X_OK,
93                 /// test for existence of file
94                 eperm = F_OK
95         };
96         /// Test whether the current user has a given set of permissions
97         bool access(int p) const;
98         /// Is the file writable for the current user?
99         bool writable() const { return access(FileInfo::wperm); }
100         /// Is the file readable for the current user?
101         bool readable() const { return access(FileInfo::rperm); }
102         /// Is the file executable for the current user?
103         bool executable() const { return access(FileInfo::xperm); }
104         /// Does the file exist?
105         bool exist() const { return access(FileInfo::eperm); }
106         ///
107         bool isLink() const;
108         ///
109         bool isRegular() const;
110         ///
111         bool isDir() const;
112         ///
113         bool isChar() const;
114         ///
115         bool isBlock() const;
116         ///
117         bool isFifo() const;
118         ///
119         bool isSocket() const;
120         ///
121         int getError() const;
122         ///
123         enum Err {
124                 ///
125                 NoErr = -1
126         };
127 private:
128         ///
129         void init();
130         ///
131         void dostat(bool);
132
133         ///
134         struct stat buf_;
135         ///
136         int status_;
137         ///
138         int err_;
139         ///
140         std::string fname_;
141 };
142
143 } // namespace support
144 } // namespace lyx
145
146 #endif