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