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