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