]> git.lyx.org Git - lyx.git/blob - src/support/FileInfo.h
lyxserver cleanup patch + andre's small patches
[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 #if defined(__CYGWIN__) //Cygwin has it's own concept of X_OK ???
28 #undef X_OK
29 #define X_OK 1
30 #endif
31
32 /** Use objects of this class to get information about files.
33  */
34 class FileInfo : boost::noncopyable {
35 public:
36         ///
37         FileInfo();
38         
39         /** Get information about file.
40             If link is true, the information is about the link itself, not
41             the file that is obtained by tracing the links. */
42         explicit
43         FileInfo(string const & path, bool link = false);
44
45         /// File descriptor
46         explicit
47         FileInfo(int fildes);
48
49         /// Query a new file
50         FileInfo & newFile(string const & path, bool link = false);
51         
52         /// Query a new file descriptor
53         FileInfo & newFile(int fildes);
54         
55         /// Returns a character describing file type (ls -F)
56         char const * typeIndicator() const;
57         
58         /// File protection mode
59         mode_t getMode() const;
60
61         /// Constructs standard mode string (ls style)
62         void modeString(char * szString) const;
63         
64         /// returns a letter describing a file type (ls style)
65         char typeLetter() const;
66         
67         /// builds 'rwx' string describing file access rights
68         void flagRWX(mode_t i, char * szString) const;
69         
70         /// updates mode string to match suid/sgid/sticky bits
71         void setSticky(char * szString) const;
72         
73         ///
74         time_t getModificationTime() const;
75         
76         ///
77         time_t getAccessTime() const;
78         
79         ///
80         time_t getStatusChangeTime() const;
81         
82         /// Total file size in bytes
83         off_t getSize() const;
84         
85         /// Number of hard links
86         nlink_t getNumberOfLinks() const;
87         
88         /// User ID of owner
89         uid_t getUid() const;
90         
91         /// Group ID of owner
92         gid_t getGid() const;
93         
94         /// Is the file information correct? Did the query succeed?
95         bool isOK() const;
96         
97         /// Permission flags
98         enum perm_test {
99                 /// test for read permission
100                 rperm = R_OK,
101                 /// test for write permission
102                 wperm = W_OK,
103                 /// test for execute (search) permission
104                 xperm = X_OK,
105                 /// test for existence of file
106                 eperm = F_OK
107         };
108         /// Test whether the current user has a given set of permissions
109         bool access(int p) const;
110         /// Is the file writable for the current user?
111         bool writable() const { return access(FileInfo::wperm); }
112         /// Is the file readable for the current user?
113         bool readable() const { return access(FileInfo::rperm); }
114         /// Is the file executable for the current user?
115         bool executable() const { return access(FileInfo::xperm); }
116         /// Does the file exist?
117         bool exist() const { return access(FileInfo::eperm); }
118         ///
119         bool isLink() const;
120         ///
121         bool isRegular() const;
122         ///
123         bool isDir() const;
124         ///
125         bool isChar() const;
126         ///
127         bool isBlock() const;
128         ///
129         bool isFifo() const;
130         ///
131         bool isSocket() const;
132         ///
133         int getError() const;
134         ///
135         enum Err {
136                 ///
137                 NoErr = -1
138         };
139 private:
140         ///
141         void init();
142         ///
143         void dostat(bool);
144         ///
145         struct stat buf;
146         ///
147         int status;
148         ///
149         int err;
150         ///
151         string fname;
152 };
153
154 #endif