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