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