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
19 # define S_IRUSR S_IREAD
21 # define S_IRUSR 00400
27 # define S_IWUSR S_IWRITE
29 # define S_IWUSR 00200
35 # define S_IXUSR S_IEXEC
37 # define S_IXUSR 00100
41 #ifdef STAT_MACROS_BROKEN
54 #if !defined(S_ISBLK) && defined(S_IFBLK)
55 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
57 #if !defined(S_ISCHR) && defined(S_IFCHR)
58 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
60 #if !defined(S_ISDIR) && defined(S_IFDIR)
61 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
63 #if !defined(S_ISREG) && defined(S_IFREG)
64 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
66 #if !defined(S_ISFIFO) && defined(S_IFIFO)
67 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
69 #if !defined(S_ISLNK) && defined(S_IFLNK)
70 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
72 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
73 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
75 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
76 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
77 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
79 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
80 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
86 // builds 'rwx' string describing file access rights
87 void flagRWX(mode_t i, char * str)
89 str[0] = (i & S_IRUSR) ? 'r' : '-';
90 str[1] = (i & S_IWUSR) ? 'w' : '-';
91 str[2] = (i & S_IXUSR) ? 'x' : '-';
94 // updates mode string to match suid/sgid/sticky bits
95 void setSticky(mode_t i, char * str)
99 str[3] = (str[3] == 'x') ? 's' : 'S';
103 str[6] = (str[6] == 'x') ? 's' : 'S';
107 str[9] = (str[9] == 'x') ? 's' : 'S';
111 // returns a letter describing a file type (ls style)
112 char typeLetter(mode_t i)
115 if (S_ISBLK(i)) return 'b';
117 if (S_ISCHR(i)) return 'c';
118 if (S_ISDIR(i)) return 'd';
119 if (S_ISREG(i)) return '-';
121 if (S_ISFIFO(i)) return 'p';
124 if (S_ISLNK(i)) return 'l';
127 if (S_ISSOCK(i)) return 's';
130 if (S_ISMPC(i)) return 'm';
133 if (S_ISNWK(i)) return 'n';
148 FileInfo::FileInfo(string const & path, bool link)
156 FileInfo::FileInfo(int fildes)
159 status_ = fstat(fildes, &buf_);
165 void FileInfo::init()
172 void FileInfo::dostat(bool link)
175 status_ = ::lstat(fname_.c_str(), &buf_);
177 status_ = ::stat(fname_.c_str(), &buf_);
183 FileInfo & FileInfo::newFile(string const & path, bool link)
193 FileInfo & FileInfo::newFile(int fildes)
197 status_ = fstat(fildes, &buf_);
204 // should not be in FileInfo
205 char FileInfo::typeIndicator() const
208 if (S_ISDIR(buf_.st_mode))
211 if (S_ISLNK(buf_.st_mode))
215 if (S_ISFIFO(buf_.st_mode))
219 if (S_ISSOCK(buf_.st_mode))
222 if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
228 mode_t FileInfo::getMode() const
235 // should not be in FileInfo
236 string FileInfo::modeString() const
240 str[0] = typeLetter(buf_.st_mode);
241 flagRWX((buf_.st_mode & 0700) << 0, &str[1]);
242 flagRWX((buf_.st_mode & 0070) << 3, &str[4]);
243 flagRWX((buf_.st_mode & 0007) << 6, &str[7]);
244 setSticky(buf_.st_mode, str);
251 time_t FileInfo::getModificationTime() const
254 return buf_.st_mtime;
258 time_t FileInfo::getAccessTime() const
261 return buf_.st_atime;
265 time_t FileInfo::getStatusChangeTime() const
268 return buf_.st_ctime;
272 nlink_t FileInfo::getNumberOfLinks() const
275 return buf_.st_nlink;
279 uid_t FileInfo::getUid() const
286 gid_t FileInfo::getGid() const
293 off_t FileInfo::getSize() const
300 int FileInfo::getError() const
306 bool FileInfo::isOK() const
312 bool FileInfo::isLink() const
315 return S_ISLNK(buf_.st_mode);
319 bool FileInfo::isRegular() const
322 return S_ISREG(buf_.st_mode);
326 bool FileInfo::isDir() const
329 return S_ISDIR(buf_.st_mode);
333 bool FileInfo::isChar() const
336 return S_ISCHR(buf_.st_mode);
340 bool FileInfo::isBlock() const
343 return S_ISBLK(buf_.st_mode);
347 bool FileInfo::isFifo() const
350 return S_ISFIFO(buf_.st_mode);
354 bool FileInfo::isSocket() const
358 return S_ISSOCK(buf_.st_mode);
365 // should not be in FileInfo
366 bool FileInfo::access(int p) const
368 // if we don't have a filename we fail
372 // If we were really kind, we would also tell why
373 // the file access failed.
374 return ::access(fname_.c_str(), p) == 0;