-/* This file is part of
- * ======================================================
+/**
+ * \file FileInfo.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * LyX, The Document Processor
+ * \author Lars Gullik Bjønnes
*
- * Copyright 1995 Matthias Ettrich
- * Copyright 1995-2001 The LyX Team.
- *
- * ====================================================== */
+ * Full author contact details are available in file CREDITS.
+ */
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include "support/FileInfo.h"
+#include "support/lstrings.h"
-//#include <sys/types.h>
-//#include <sys/stat.h>
+#include <boost/assert.hpp>
#include <cerrno>
-#include "FileInfo.h"
-#include "LAssert.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+using std::string;
+
#if !S_IRUSR
# if S_IREAD
namespace {
-/// builds 'rwx' string describing file access rights
+// builds 'rwx' string describing file access rights
void flagRWX(mode_t i, char * str)
{
str[0] = (i & S_IRUSR) ? 'r' : '-';
str[2] = (i & S_IXUSR) ? 'x' : '-';
}
-/// updates mode string to match suid/sgid/sticky bits
+// updates mode string to match suid/sgid/sticky bits
void setSticky(mode_t i, char * str)
{
#ifdef S_ISUID
- if (i & S_ISUID)
+ if (i & S_ISUID)
str[3] = (str[3] == 'x') ? 's' : 'S';
#endif
#ifdef S_ISGID
str[6] = (str[6] == 'x') ? 's' : 'S';
#endif
#ifdef S_ISVTX
- if (i & S_ISVTX)
+ if (i & S_ISVTX)
str[9] = (str[9] == 'x') ? 's' : 'S';
#endif
}
+// returns a letter describing a file type (ls style)
+char typeLetter(mode_t i)
+{
+#ifdef S_ISBLK
+ if (S_ISBLK(i)) return 'b';
+#endif
+ if (S_ISCHR(i)) return 'c';
+ if (S_ISDIR(i)) return 'd';
+ if (S_ISREG(i)) return '-';
+#ifdef S_ISFIFO
+ if (S_ISFIFO(i)) return 'p';
+#endif
+#ifdef S_ISLNK
+ if (S_ISLNK(i)) return 'l';
+#endif
+#ifdef S_ISSOCK
+ if (S_ISSOCK(i)) return 's';
+#endif
+#ifdef S_ISMPC
+ if (S_ISMPC(i)) return 'm';
+#endif
+#ifdef S_ISNWK
+ if (S_ISNWK(i)) return 'n';
+#endif
+ return '?';
+}
+
} // namespace anon
+namespace lyx {
+namespace support {
+
FileInfo::FileInfo()
{
init();
FileInfo::FileInfo(string const & path, bool link)
- : fname_(path)
+ // Win32 stat() doesn't dig trailing slashes.
+ // Posix stat() doesn't care, but we'll remove it anyway.
+ : fname_(rtrim(path, "/"))
{
init();
dostat(link);
void FileInfo::dostat(bool link)
{
+#ifdef HAVE_LSTAT
if (link)
status_ = ::lstat(fname_.c_str(), &buf_);
else
status_ = ::stat(fname_.c_str(), &buf_);
+#else
+ status_ = ::stat(fname_.c_str(), &buf_);
+#endif
+
if (status_)
err_ = errno;
}
FileInfo & FileInfo::newFile(string const & path, bool link)
{
- fname_ = path;
+ // Win32 stat() doesn't dig trailing slashes.
+ // Posix stat() doesn't care, but we'll remove it anyway.
+ fname_ = rtrim(path, "/");
status_ = 0;
err_ = NoErr;
dostat(link);
// should not be in FileInfo
-char const * FileInfo::typeIndicator() const
+char FileInfo::typeIndicator() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
if (S_ISDIR(buf_.st_mode))
- return "/";
+ return '/';
#ifdef S_ISLNK
if (S_ISLNK(buf_.st_mode))
- return "@";
+ return '@';
#endif
#ifdef S_ISFIFO
if (S_ISFIFO(buf_.st_mode))
- return "|";
+ return '|';
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(buf_.st_mode))
- return "=";
+ return '=';
#endif
- if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
- return "*";
- return "";
+ return ' ';
}
mode_t FileInfo::getMode() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_mode;
}
// should not be in FileInfo
-void FileInfo::modeString(char * str) const
+string FileInfo::modeString() const
{
- str[0] = typeLetter();
+ BOOST_ASSERT(isOK());
+ char str[11];
+ str[0] = typeLetter(buf_.st_mode);
flagRWX((buf_.st_mode & 0700) << 0, &str[1]);
flagRWX((buf_.st_mode & 0070) << 3, &str[4]);
flagRWX((buf_.st_mode & 0007) << 6, &str[7]);
- lyx::Assert(isOK());
setSticky(buf_.st_mode, str);
str[10] = 0;
-}
-
-
-// should not be in FileInfo
-char FileInfo::typeLetter() const
-{
- lyx::Assert(isOK());
-
-#ifdef S_ISBLK
- if (S_ISBLK(buf_.st_mode)) return 'b';
-#endif
- if (S_ISCHR(buf_.st_mode)) return 'c';
- if (S_ISDIR(buf_.st_mode)) return 'd';
- if (S_ISREG(buf_.st_mode)) return '-';
-#ifdef S_ISFIFO
- if (S_ISFIFO(buf_.st_mode)) return 'p';
-#endif
-#ifdef S_ISLNK
- if (S_ISLNK(buf_.st_mode)) return 'l';
-#endif
-#ifdef S_ISSOCK
- if (S_ISSOCK(buf_.st_mode)) return 's';
-#endif
-#ifdef S_ISMPC
- if (S_ISMPC(buf_.st_mode)) return 'm';
-#endif
-#ifdef S_ISNWK
- if (S_ISNWK(buf_.st_mode)) return 'n';
-#endif
- return '?';
+ return str;
}
time_t FileInfo::getModificationTime() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_mtime;
}
time_t FileInfo::getAccessTime() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_atime;
}
time_t FileInfo::getStatusChangeTime() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_ctime;
}
-nlink_t FileInfo::getNumberOfLinks() const
-{
- lyx::Assert(isOK());
- return buf_.st_nlink;
-}
-
-
uid_t FileInfo::getUid() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_uid;
}
gid_t FileInfo::getGid() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_gid;
}
off_t FileInfo::getSize() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return buf_.st_size;
}
bool FileInfo::isLink() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
+#ifdef S_ISLNK
return S_ISLNK(buf_.st_mode);
+#else
+ return false;
+#endif
}
bool FileInfo::isRegular() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return S_ISREG(buf_.st_mode);
}
bool FileInfo::isDir() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return S_ISDIR(buf_.st_mode);
}
bool FileInfo::isChar() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return S_ISCHR(buf_.st_mode);
}
bool FileInfo::isBlock() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return S_ISBLK(buf_.st_mode);
}
bool FileInfo::isFifo() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
return S_ISFIFO(buf_.st_mode);
}
bool FileInfo::isSocket() const
{
- lyx::Assert(isOK());
+ BOOST_ASSERT(isOK());
#ifdef S_ISSOCK
return S_ISSOCK(buf_.st_mode);
#else
// the file access failed.
return ::access(fname_.c_str(), p) == 0;
}
+
+} // namespace support
+} // namespace lyx