3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 //#include <sys/types.h>
18 //#include <sys/stat.h>
26 # define S_IRUSR S_IREAD
28 # define S_IRUSR 00400
34 # define S_IWUSR S_IWRITE
36 # define S_IWUSR 00200
42 # define S_IXUSR S_IEXEC
44 # define S_IXUSR 00100
48 #ifdef STAT_MACROS_BROKEN
61 #if !defined(S_ISBLK) && defined(S_IFBLK)
62 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
64 #if !defined(S_ISCHR) && defined(S_IFCHR)
65 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
67 #if !defined(S_ISDIR) && defined(S_IFDIR)
68 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
70 #if !defined(S_ISREG) && defined(S_IFREG)
71 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
73 #if !defined(S_ISFIFO) && defined(S_IFIFO)
74 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
79 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
80 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
82 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
83 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
84 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
86 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
87 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
93 // builds 'rwx' string describing file access rights
94 void flagRWX(mode_t i, char * str)
96 str[0] = (i & S_IRUSR) ? 'r' : '-';
97 str[1] = (i & S_IWUSR) ? 'w' : '-';
98 str[2] = (i & S_IXUSR) ? 'x' : '-';
101 // updates mode string to match suid/sgid/sticky bits
102 void setSticky(mode_t i, char * str)
106 str[3] = (str[3] == 'x') ? 's' : 'S';
110 str[6] = (str[6] == 'x') ? 's' : 'S';
114 str[9] = (str[9] == 'x') ? 's' : 'S';
118 // returns a letter describing a file type (ls style)
119 char typeLetter(mode_t i)
122 if (S_ISBLK(i)) return 'b';
124 if (S_ISCHR(i)) return 'c';
125 if (S_ISDIR(i)) return 'd';
126 if (S_ISREG(i)) return '-';
128 if (S_ISFIFO(i)) return 'p';
131 if (S_ISLNK(i)) return 'l';
134 if (S_ISSOCK(i)) return 's';
137 if (S_ISMPC(i)) return 'm';
140 if (S_ISNWK(i)) return 'n';
155 FileInfo::FileInfo(string const & path, bool link)
163 FileInfo::FileInfo(int fildes)
166 status_ = fstat(fildes, &buf_);
172 void FileInfo::init()
179 void FileInfo::dostat(bool link)
182 status_ = ::lstat(fname_.c_str(), &buf_);
184 status_ = ::stat(fname_.c_str(), &buf_);
190 FileInfo & FileInfo::newFile(string const & path, bool link)
200 FileInfo & FileInfo::newFile(int fildes)
204 status_ = fstat(fildes, &buf_);
211 // should not be in FileInfo
212 char FileInfo::typeIndicator() const
215 if (S_ISDIR(buf_.st_mode))
218 if (S_ISLNK(buf_.st_mode))
222 if (S_ISFIFO(buf_.st_mode))
226 if (S_ISSOCK(buf_.st_mode))
229 if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
235 mode_t FileInfo::getMode() const
242 // should not be in FileInfo
243 string FileInfo::modeString() const
247 str[0] = typeLetter(buf_.st_mode);
248 flagRWX((buf_.st_mode & 0700) << 0, &str[1]);
249 flagRWX((buf_.st_mode & 0070) << 3, &str[4]);
250 flagRWX((buf_.st_mode & 0007) << 6, &str[7]);
251 setSticky(buf_.st_mode, str);
258 time_t FileInfo::getModificationTime() const
261 return buf_.st_mtime;
265 time_t FileInfo::getAccessTime() const
268 return buf_.st_atime;
272 time_t FileInfo::getStatusChangeTime() const
275 return buf_.st_ctime;
279 nlink_t FileInfo::getNumberOfLinks() const
282 return buf_.st_nlink;
286 uid_t FileInfo::getUid() const
293 gid_t FileInfo::getGid() const
300 off_t FileInfo::getSize() const
307 int FileInfo::getError() const
313 bool FileInfo::isOK() const
319 bool FileInfo::isLink() const
322 return S_ISLNK(buf_.st_mode);
326 bool FileInfo::isRegular() const
329 return S_ISREG(buf_.st_mode);
333 bool FileInfo::isDir() const
336 return S_ISDIR(buf_.st_mode);
340 bool FileInfo::isChar() const
343 return S_ISCHR(buf_.st_mode);
347 bool FileInfo::isBlock() const
350 return S_ISBLK(buf_.st_mode);
354 bool FileInfo::isFifo() const
357 return S_ISFIFO(buf_.st_mode);
361 bool FileInfo::isSocket() const
365 return S_ISSOCK(buf_.st_mode);
372 // should not be in FileInfo
373 bool FileInfo::access(int p) const
375 // if we don't have a filename we fail
379 // If we were really kind, we would also tell why
380 // the file access failed.
381 return ::access(fname_.c_str(), p) == 0;