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