1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
1999-10-02 16:21:10 +00:00
|
|
|
* ======================================================
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
1999-10-02 16:21:10 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
1999-10-02 16:21:10 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
1999-11-09 22:20:24 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-24 13:53:19 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2000-12-29 12:48:02 +00:00
|
|
|
//#include <sys/types.h>
|
|
|
|
//#include <sys/stat.h>
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cerrno>
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "FileInfo.h"
|
2002-01-07 10:17:44 +00:00
|
|
|
#include "LAssert.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#if !S_IRUSR
|
|
|
|
# if S_IREAD
|
|
|
|
# define S_IRUSR S_IREAD
|
|
|
|
# else
|
|
|
|
# define S_IRUSR 00400
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !S_IWUSR
|
|
|
|
# if S_IWRITE
|
|
|
|
# define S_IWUSR S_IWRITE
|
|
|
|
# else
|
|
|
|
# define S_IWUSR 00200
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !S_IXUSR
|
|
|
|
# if S_IEXEC
|
|
|
|
# define S_IXUSR S_IEXEC
|
|
|
|
# else
|
|
|
|
# define S_IXUSR 00100
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef STAT_MACROS_BROKEN
|
|
|
|
#undef S_ISBLK
|
|
|
|
#undef S_ISCHR
|
|
|
|
#undef S_ISDIR
|
|
|
|
#undef S_ISFIFO
|
|
|
|
#undef S_ISLNK
|
|
|
|
#undef S_ISMPB
|
|
|
|
#undef S_ISMPC
|
|
|
|
#undef S_ISNWK
|
|
|
|
#undef S_ISREG
|
|
|
|
#undef S_ISSOCK
|
2002-03-21 17:09:55 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#if !defined(S_ISBLK) && defined(S_IFBLK)
|
|
|
|
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISCHR) && defined(S_IFCHR)
|
|
|
|
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISDIR) && defined(S_IFDIR)
|
|
|
|
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISREG) && defined(S_IFREG)
|
|
|
|
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISFIFO) && defined(S_IFIFO)
|
|
|
|
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISLNK) && defined(S_IFLNK)
|
|
|
|
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISSOCK) && defined(S_IFSOCK)
|
|
|
|
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
|
|
|
|
#define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
|
|
|
|
#define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
|
|
|
|
#endif
|
|
|
|
#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
|
|
|
|
#define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
|
|
|
|
#endif
|
|
|
|
|
2002-07-16 10:05:52 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// builds 'rwx' string describing file access rights
|
|
|
|
void flagRWX(mode_t i, char * str)
|
|
|
|
{
|
|
|
|
str[0] = (i & S_IRUSR) ? 'r' : '-';
|
|
|
|
str[1] = (i & S_IWUSR) ? 'w' : '-';
|
|
|
|
str[2] = (i & S_IXUSR) ? 'x' : '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// updates mode string to match suid/sgid/sticky bits
|
|
|
|
void setSticky(mode_t i, char * str)
|
|
|
|
{
|
|
|
|
#ifdef S_ISUID
|
|
|
|
if (i & S_ISUID)
|
|
|
|
str[3] = (str[3] == 'x') ? 's' : 'S';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2002-07-16 10:05:52 +00:00
|
|
|
#ifdef S_ISGID
|
|
|
|
if (i & S_ISGID)
|
|
|
|
str[6] = (str[6] == 'x') ? 's' : 'S';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2002-07-16 10:05:52 +00:00
|
|
|
#ifdef S_ISVTX
|
|
|
|
if (i & S_ISVTX)
|
|
|
|
str[9] = (str[9] == 'x') ? 's' : 'S';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2002-07-16 10:05:52 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2002-07-16 10:05:52 +00:00
|
|
|
} // namespace anon
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
FileInfo::FileInfo()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
FileInfo::FileInfo(string const & path, bool link)
|
2002-07-16 10:05:52 +00:00
|
|
|
: fname_(path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
dostat(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileInfo::FileInfo(int fildes)
|
|
|
|
{
|
|
|
|
init();
|
2002-07-16 10:05:52 +00:00
|
|
|
status_ = fstat(fildes, &buf_);
|
|
|
|
if (status_)
|
|
|
|
err_ = errno;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileInfo::init()
|
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
status_ = 0;
|
|
|
|
err_ = NoErr;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileInfo::dostat(bool link)
|
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
if (link)
|
|
|
|
status_ = ::lstat(fname_.c_str(), &buf_);
|
|
|
|
else
|
|
|
|
status_ = ::stat(fname_.c_str(), &buf_);
|
|
|
|
if (status_)
|
|
|
|
err_ = errno;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
FileInfo & FileInfo::newFile(string const & path, bool link)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
fname_ = path;
|
|
|
|
status_ = 0;
|
|
|
|
err_ = NoErr;
|
1999-09-27 18:44:28 +00:00
|
|
|
dostat(link);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
FileInfo & FileInfo::newFile(int fildes)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
status_ = 0;
|
|
|
|
err_ = NoErr;
|
|
|
|
status_ = fstat(fildes, &buf_);
|
|
|
|
if (status_)
|
|
|
|
err_ = errno;
|
1999-09-27 18:44:28 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 18:34:46 +00:00
|
|
|
// should not be in FileInfo
|
1999-10-02 16:21:10 +00:00
|
|
|
char const * FileInfo::typeIndicator() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISDIR(buf_.st_mode))
|
|
|
|
return "/";
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef S_ISLNK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISLNK(buf_.st_mode))
|
|
|
|
return "@";
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISFIFO
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISFIFO(buf_.st_mode))
|
|
|
|
return "|";
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISSOCK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISSOCK(buf_.st_mode))
|
|
|
|
return "=";
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
|
|
|
|
return "*";
|
1999-09-27 18:44:28 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mode_t FileInfo::getMode() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_mode;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-01-24 18:34:46 +00:00
|
|
|
|
|
|
|
// should not be in FileInfo
|
2002-07-16 10:05:52 +00:00
|
|
|
void FileInfo::modeString(char * str) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
str[0] = typeLetter();
|
|
|
|
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;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 18:34:46 +00:00
|
|
|
// should not be in FileInfo
|
1999-09-27 18:44:28 +00:00
|
|
|
char FileInfo::typeLetter() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef S_ISBLK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISBLK(buf_.st_mode)) return 'b';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISCHR(buf_.st_mode)) return 'c';
|
|
|
|
if (S_ISDIR(buf_.st_mode)) return 'd';
|
|
|
|
if (S_ISREG(buf_.st_mode)) return '-';
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef S_ISFIFO
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISFIFO(buf_.st_mode)) return 'p';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISLNK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISLNK(buf_.st_mode)) return 'l';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISSOCK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISSOCK(buf_.st_mode)) return 's';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISMPC
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISMPC(buf_.st_mode)) return 'm';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef S_ISNWK
|
2002-07-16 10:05:52 +00:00
|
|
|
if (S_ISNWK(buf_.st_mode)) return 'n';
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time_t FileInfo::getModificationTime() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_mtime;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t FileInfo::getAccessTime() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_atime;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 18:34:46 +00:00
|
|
|
time_t FileInfo::getStatusChangeTime() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_ctime;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nlink_t FileInfo::getNumberOfLinks() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_nlink;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
uid_t FileInfo::getUid() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_uid;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
gid_t FileInfo::getGid() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_gid;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t FileInfo::getSize() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return buf_.st_size;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FileInfo::getError() const
|
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
return err_;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isOK() const
|
|
|
|
{
|
2002-07-16 10:05:52 +00:00
|
|
|
return status_ == 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isLink() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISLNK(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isRegular() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISREG(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isDir() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISDIR(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isChar() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISCHR(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isBlock() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISBLK(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isFifo() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISFIFO(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileInfo::isSocket() const
|
|
|
|
{
|
2002-01-07 10:17:44 +00:00
|
|
|
lyx::Assert(isOK());
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef S_ISSOCK
|
2002-07-16 10:05:52 +00:00
|
|
|
return S_ISSOCK(buf_.st_mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-24 18:34:46 +00:00
|
|
|
// should not be in FileInfo
|
2000-11-04 10:00:12 +00:00
|
|
|
bool FileInfo::access(int p) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// if we don't have a filename we fail
|
2002-07-16 10:05:52 +00:00
|
|
|
if (fname_.empty())
|
1999-09-27 18:44:28 +00:00
|
|
|
return false;
|
2002-07-16 10:05:52 +00:00
|
|
|
|
|
|
|
// If we were really kind, we would also tell why
|
|
|
|
// the file access failed.
|
|
|
|
return ::access(fname_.c_str(), p) == 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|