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.
15 #include <boost/assert.hpp>
21 # define S_IRUSR S_IREAD
23 # define S_IRUSR 00400
29 # define S_IWUSR S_IWRITE
31 # define S_IWUSR 00200
37 # define S_IXUSR S_IEXEC
39 # define S_IXUSR 00100
43 #ifdef STAT_MACROS_BROKEN
56 #if !defined(S_ISBLK) && defined(S_IFBLK)
57 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
59 #if !defined(S_ISCHR) && defined(S_IFCHR)
60 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
62 #if !defined(S_ISDIR) && defined(S_IFDIR)
63 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
65 #if !defined(S_ISREG) && defined(S_IFREG)
66 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
68 #if !defined(S_ISFIFO) && defined(S_IFIFO)
69 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
71 #if !defined(S_ISLNK) && defined(S_IFLNK)
72 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
74 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
75 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
77 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
78 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
79 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
81 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
82 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
88 // builds 'rwx' string describing file access rights
89 void flagRWX(mode_t i, char * str)
91 str[0] = (i & S_IRUSR) ? 'r' : '-';
92 str[1] = (i & S_IWUSR) ? 'w' : '-';
93 str[2] = (i & S_IXUSR) ? 'x' : '-';
96 // updates mode string to match suid/sgid/sticky bits
97 void setSticky(mode_t i, char * str)
101 str[3] = (str[3] == 'x') ? 's' : 'S';
105 str[6] = (str[6] == 'x') ? 's' : 'S';
109 str[9] = (str[9] == 'x') ? 's' : 'S';
113 // returns a letter describing a file type (ls style)
114 char typeLetter(mode_t i)
117 if (S_ISBLK(i)) return 'b';
119 if (S_ISCHR(i)) return 'c';
120 if (S_ISDIR(i)) return 'd';
121 if (S_ISREG(i)) return '-';
123 if (S_ISFIFO(i)) return 'p';
126 if (S_ISLNK(i)) return 'l';
129 if (S_ISSOCK(i)) return 's';
132 if (S_ISMPC(i)) return 'm';
135 if (S_ISNWK(i)) return 'n';
153 FileInfo::FileInfo(string const & path, bool link)
161 FileInfo::FileInfo(int fildes)
164 status_ = fstat(fildes, &buf_);
170 void FileInfo::init()
177 void FileInfo::dostat(bool link)
180 status_ = ::lstat(fname_.c_str(), &buf_);
182 status_ = ::stat(fname_.c_str(), &buf_);
188 FileInfo & FileInfo::newFile(string const & path, bool link)
198 FileInfo & FileInfo::newFile(int fildes)
202 status_ = fstat(fildes, &buf_);
209 // should not be in FileInfo
210 char FileInfo::typeIndicator() const
212 BOOST_ASSERT(isOK());
213 if (S_ISDIR(buf_.st_mode))
216 if (S_ISLNK(buf_.st_mode))
220 if (S_ISFIFO(buf_.st_mode))
224 if (S_ISSOCK(buf_.st_mode))
227 if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
233 mode_t FileInfo::getMode() const
235 BOOST_ASSERT(isOK());
240 // should not be in FileInfo
241 string FileInfo::modeString() const
243 BOOST_ASSERT(isOK());
245 str[0] = typeLetter(buf_.st_mode);
246 flagRWX((buf_.st_mode & 0700) << 0, &str[1]);
247 flagRWX((buf_.st_mode & 0070) << 3, &str[4]);
248 flagRWX((buf_.st_mode & 0007) << 6, &str[7]);
249 setSticky(buf_.st_mode, str);
256 time_t FileInfo::getModificationTime() const
258 BOOST_ASSERT(isOK());
259 return buf_.st_mtime;
263 time_t FileInfo::getAccessTime() const
265 BOOST_ASSERT(isOK());
266 return buf_.st_atime;
270 time_t FileInfo::getStatusChangeTime() const
272 BOOST_ASSERT(isOK());
273 return buf_.st_ctime;
277 nlink_t FileInfo::getNumberOfLinks() const
279 BOOST_ASSERT(isOK());
280 return buf_.st_nlink;
284 uid_t FileInfo::getUid() const
286 BOOST_ASSERT(isOK());
291 gid_t FileInfo::getGid() const
293 BOOST_ASSERT(isOK());
298 off_t FileInfo::getSize() const
300 BOOST_ASSERT(isOK());
305 int FileInfo::getError() const
311 bool FileInfo::isOK() const
317 bool FileInfo::isLink() const
319 BOOST_ASSERT(isOK());
320 return S_ISLNK(buf_.st_mode);
324 bool FileInfo::isRegular() const
326 BOOST_ASSERT(isOK());
327 return S_ISREG(buf_.st_mode);
331 bool FileInfo::isDir() const
333 BOOST_ASSERT(isOK());
334 return S_ISDIR(buf_.st_mode);
338 bool FileInfo::isChar() const
340 BOOST_ASSERT(isOK());
341 return S_ISCHR(buf_.st_mode);
345 bool FileInfo::isBlock() const
347 BOOST_ASSERT(isOK());
348 return S_ISBLK(buf_.st_mode);
352 bool FileInfo::isFifo() const
354 BOOST_ASSERT(isOK());
355 return S_ISFIFO(buf_.st_mode);
359 bool FileInfo::isSocket() const
361 BOOST_ASSERT(isOK());
363 return S_ISSOCK(buf_.st_mode);
370 // should not be in FileInfo
371 bool FileInfo::access(int p) const
373 // if we don't have a filename we fail
377 // If we were really kind, we would also tell why
378 // the file access failed.
379 return ::access(fname_.c_str(), p) == 0;
382 } // namespace support