yast2-core
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
PathInfo Class Reference

Wrapper class for ::stat/::lstat and other file/directory related operations. More...

#include <PathInfo.h>

Classes

class  devino_cache
 Simple cache remembering device/inode to detect hardlinks. More...
 
struct  direntry
 
class  stat_mode
 Wrapper class for mode_t values as derived from ::stat. More...
 

Public Types

enum  Mode { STAT, LSTAT }
 
enum  file_type {
  NOT_AVAIL = 0x00, NOT_EXIST = 0x01, T_FILE = 0x02, T_DIR = 0x04,
  T_CHARDEV = 0x08, T_BLOCKDEV = 0x10, T_FIFO = 0x20, T_LINK = 0x40,
  T_SOCKET = 0x80
}
 
enum  ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 }
 
typedef std::list< direntrydircontent
 

Public Member Functions

 PathInfo (const Pathname &path="", Mode initial=STAT)
 
 PathInfo (const std::string &path, Mode initial=STAT)
 
 PathInfo (const char *path, Mode initial=STAT)
 
virtual ~PathInfo ()
 
const Pathnamepath () const
 
const std::string & asString () const
 
Mode mode () const
 
int error () const
 
void setPath (const Pathname &path)
 
void setMode (Mode mode)
 
bool stat (const Pathname &path)
 
bool lstat (const Pathname &path)
 
bool operator() (const Pathname &path)
 
bool stat ()
 
bool lstat ()
 
bool operator() ()
 
bool isExist () const
 
file_type fileType () const
 
bool isFile () const
 
bool isDir () const
 
bool isLink () const
 
bool isChr () const
 
bool isBlk () const
 
bool isFifo () const
 
bool isSock () const
 
nlink_t nlink () const
 
uid_t owner () const
 
gid_t group () const
 
bool isRUsr () const
 
bool isWUsr () const
 
bool isXUsr () const
 
bool isR () const
 
bool isW () const
 
bool isX () const
 
bool isRGrp () const
 
bool isWGrp () const
 
bool isXGrp () const
 
bool isROth () const
 
bool isWOth () const
 
bool isXOth () const
 
bool isUid () const
 
bool isGid () const
 
bool isVtx () const
 
mode_t uperm () const
 
mode_t gperm () const
 
mode_t operm () const
 
mode_t perm () const
 
bool isPerm (mode_t m) const
 
bool hasPerm (mode_t m) const
 
mode_t st_mode () const
 
mode_t userMay () const
 
bool userMayR () const
 
bool userMayW () const
 
bool userMayX () const
 
bool userMayRW () const
 
bool userMayRX () const
 
bool userMayWX () const
 
bool userMayRWX () const
 
dev_t dev () const
 
dev_t rdev () const
 
ino_t ino () const
 
off_t size () const
 
unsigned long blksize () const
 
unsigned long blocks () const
 
time_t atime () const
 
time_t mtime () const
 
time_t ctime () const
 

Static Public Member Functions

static int mkdir (const Pathname &path, unsigned mode=0755)
 
static int assert_dir (const Pathname &path, unsigned mode=0755)
 
static int rmdir (const Pathname &path)
 
static int recursive_rmdir (const Pathname &path)
 
static int clean_dir (const Pathname &path)
 
static int copy_dir (const Pathname &srcpath, const Pathname &destpath)
 
static int readdir (std::list< std::string > &retlist, const Pathname &path, bool dots=true)
 
static int readdir (dircontent &retlist, const Pathname &path, bool dots=true, Mode statmode=STAT)
 
static int unlink (const Pathname &path)
 
static int rename (const Pathname &oldpath, const Pathname &newpath)
 
static int copy (const Pathname &file, const Pathname &dest)
 
static int symlink (const Pathname &oldpath, const Pathname &newpath)
 
static int hardlink (const Pathname &oldpath, const Pathname &newpath)
 
static int copy_file2dir (const Pathname &file, const Pathname &dest)
 
static int erase (const Pathname &path)
 
static int chmod (const Pathname &path, mode_t mode)
 
static ZIP_TYPE zipType (const Pathname &file)
 

Private Attributes

Pathname path_t
 
struct stat statbuf_C
 
Mode mode_e
 
int error_i
 

Friends

std::ostream & operator<< (std::ostream &str, const PathInfo &obj)
 
std::ostream & operator<< (std::ostream &str, file_type obj)
 

Detailed Description

Wrapper class for ::stat/::lstat and other file/directory related operations.

Member Typedef Documentation

typedef std::list<direntry> PathInfo::dircontent

Member Enumeration Documentation

Enumerator
NOT_AVAIL 
NOT_EXIST 
T_FILE 
T_DIR 
T_CHARDEV 
T_BLOCKDEV 
T_FIFO 
T_LINK 
T_SOCKET 
Enumerator
STAT 
LSTAT 

Test whether a file is compressed (gzip/bzip2).

Returns
ZT_GZ, ZT_BZ2 if file is compressed, otherwise ZT_NONE.
Enumerator
ZT_NONE 
ZT_GZ 
ZT_BZ2 

Constructor & Destructor Documentation

PathInfo::PathInfo ( const Pathname path = "",
Mode  initial = STAT 
)

References operator()().

PathInfo::PathInfo ( const std::string &  path,
Mode  initial = STAT 
)

References operator()().

PathInfo::PathInfo ( const char *  path,
Mode  initial = STAT 
)

References operator()().

PathInfo::~PathInfo ( )
virtual

Member Function Documentation

int PathInfo::assert_dir ( const Pathname path,
unsigned  mode = 0755 
)
static

Like 'mkdir -p'. No error if directory exists. Make parent directories as needed. mode specifies the permissions to use, if directories have to be created. It is modified by the process's umask in the usual way.

Returns
0 on success, errno on failure

References Pathname::asString(), Pathname::empty(), mkdir(), mode(), and Pathname::relative().

Referenced by ctime(), open_logfile(), and IniParser::write_file().

const std::string& PathInfo::asString ( ) const
inline

References Pathname::asString().

Referenced by operator<<().

time_t PathInfo::atime ( ) const
inline

References isExist(), and statbuf_C.

unsigned long PathInfo::blksize ( ) const
inline

References isExist(), and statbuf_C.

unsigned long PathInfo::blocks ( ) const
inline

References isExist(), and statbuf_C.

int PathInfo::chmod ( const Pathname path,
mode_t  mode 
)
static

Like '::chmod'. The mode of the file given by path is changed.

Returns
0 on success, errno on failure

References _Log_Result(), Pathname::asString(), DBG, mode(), and stringutil::octstring().

int PathInfo::clean_dir ( const Pathname path)
static

Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.

Returns
0 on success, ENOTDIR if path is not a directory, otherwise the commands return value.

References _Log_Result(), ExternalProgram::close(), DBG, stringutil::form(), isDir(), isExist(), output(), ExternalDataSource::receiveLine(), and ExternalProgram::Stderr_To_Stdout.

Referenced by ctime().

int PathInfo::copy ( const Pathname file,
const Pathname dest 
)
static

Like 'cp file dest'. Copy file to destination file.

Returns
0 on success, EINVAL if file is not a file, EISDIR if destiantion is a directory, otherwise the commands return value.

References _Log_Result(), Pathname::asString(), ExternalProgram::close(), DBG, isDir(), isFile(), output(), ExternalDataSource::receiveLine(), and ExternalProgram::Stderr_To_Stdout.

int PathInfo::copy_dir ( const Pathname srcpath,
const Pathname destpath 
)
static

Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be directories. 'basename srcpath' must not exist in destpath.

Returns
0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if 'basename srcpath' exists in destpath, otherwise the commands return value.

References _Log_Result(), Pathname::asString(), Pathname::basename(), ExternalProgram::close(), DBG, isDir(), output(), ExternalDataSource::receiveLine(), and ExternalProgram::Stderr_To_Stdout.

Referenced by ctime().

int PathInfo::copy_file2dir ( const Pathname file,
const Pathname dest 
)
static

Like 'cp file dest'. Copy file to dest dir.

Returns
0 on success, EINVAL if file is not a file, ENOTDIR if dest is no directory, otherwise the commands return value.

References _Log_Result(), Pathname::asString(), ExternalProgram::close(), DBG, isDir(), isFile(), output(), ExternalDataSource::receiveLine(), and ExternalProgram::Stderr_To_Stdout.

time_t PathInfo::ctime ( ) const
inline
dev_t PathInfo::dev ( ) const
inline

References isExist(), and statbuf_C.

int PathInfo::erase ( const Pathname path)
static

Erase whatever happens to be located at path (file or directory).

Returns
0 on success.

References isDir(), isExist(), LSTAT, recursive_rmdir(), and unlink().

int PathInfo::error ( ) const
inline

References error_i.

PathInfo::file_type PathInfo::fileType ( ) const
mode_t PathInfo::gperm ( ) const
inline

References isExist(), and statbuf_C.

Referenced by userMay().

gid_t PathInfo::group ( ) const
inline

References isExist(), and statbuf_C.

Referenced by operator<<(), and userMay().

int PathInfo::hardlink ( const Pathname oldpath,
const Pathname newpath 
)
static

Like '::link'. Creates a hard link named newpath to an existing file oldpath. If newpath exists it will not be overwritten.

Returns
0 on success, errno on failure.

References _Log_Result(), Pathname::asString(), and DBG.

bool PathInfo::hasPerm ( mode_t  m) const
inline

References perm().

ino_t PathInfo::ino ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isBlk ( ) const
inline

References isExist(), and statbuf_C.

Referenced by PathInfo::stat_mode::fileType().

bool PathInfo::isChr ( ) const
inline

References isExist(), and statbuf_C.

Referenced by PathInfo::stat_mode::fileType().

bool PathInfo::isDir ( ) const
inline
bool PathInfo::isExist ( ) const
inline
bool PathInfo::isFifo ( ) const
inline

References isExist(), and statbuf_C.

Referenced by PathInfo::stat_mode::fileType().

bool PathInfo::isFile ( ) const
inline
bool PathInfo::isGid ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isLink ( ) const
inline

References isExist(), and statbuf_C.

Referenced by PathInfo::stat_mode::fileType().

bool PathInfo::isPerm ( mode_t  m) const
inline

References perm().

bool PathInfo::isR ( ) const
inline

References isRUsr().

bool PathInfo::isRGrp ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isROth ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isRUsr ( ) const
inline

References isExist(), and statbuf_C.

Referenced by isR(), and PathInfo::stat_mode::isR().

bool PathInfo::isSock ( ) const
inline

References isExist(), and statbuf_C.

Referenced by PathInfo::stat_mode::fileType().

bool PathInfo::isUid ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isVtx ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isW ( ) const
inline

References isWUsr().

bool PathInfo::isWGrp ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isWOth ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isWUsr ( ) const
inline

References isExist(), and statbuf_C.

Referenced by isW(), and PathInfo::stat_mode::isW().

bool PathInfo::isX ( ) const
inline

References isXUsr().

bool PathInfo::isXGrp ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isXOth ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::isXUsr ( ) const
inline

References isExist(), and statbuf_C.

Referenced by isX(), and PathInfo::stat_mode::isX().

bool PathInfo::lstat ( const Pathname path)
inline

References operator()(), setMode(), and setPath().

bool PathInfo::lstat ( )
inline

References operator()(), and setMode().

Referenced by operator()().

int PathInfo::mkdir ( const Pathname path,
unsigned  mode = 0755 
)
static

Like '::mkdir'. Attempt to create a new directory named path. mode specifies the permissions to use. It is modified by the process's umask in the usual way.

Returns
0 on success, errno on failure

References _Log_Result(), Pathname::asString(), DBG, mode(), and stringutil::octstring().

Referenced by assert_dir(), and ctime().

Mode PathInfo::mode ( ) const
inline

References mode_e.

Referenced by assert_dir(), chmod(), ctime(), mkdir(), and setMode().

time_t PathInfo::mtime ( ) const
inline

References isExist(), and statbuf_C.

nlink_t PathInfo::nlink ( ) const
inline

References isExist(), and statbuf_C.

bool PathInfo::operator() ( const Pathname path)
inline

References operator()(), and setPath().

Referenced by operator()().

bool PathInfo::operator() ( )
mode_t PathInfo::operm ( ) const
inline

References isExist(), and statbuf_C.

Referenced by userMay().

uid_t PathInfo::owner ( ) const
inline

References isExist(), and statbuf_C.

Referenced by operator<<(), and userMay().

const Pathname& PathInfo::path ( ) const
inline

References path_t.

Referenced by ctime(), rmdir(), setPath(), and unlink().

mode_t PathInfo::perm ( ) const
inline
dev_t PathInfo::rdev ( ) const
inline

References isExist(), and statbuf_C.

int PathInfo::readdir ( std::list< std::string > &  retlist,
const Pathname path,
bool  dots = true 
)
static

Return content of directory via retlist. If dots is false entries starting with '.' are not reported. "." and ".." are never reported.

Returns
0 on success, errno on failure.

References _Log_Result(), and DBG.

Referenced by ctime(), and readdir().

int PathInfo::readdir ( dircontent retlist,
const Pathname path,
bool  dots = true,
Mode  statmode = STAT 
)
static

Return content of directory via retlist. If dots is false entries starting with '.' are not reported. "." and ".." are never reported.

The type of individual directory entries is determined accoding to statmode (i.e. via stat or lstat).

Returns
0 on success, errno on failure.

References fileType(), and readdir().

int PathInfo::recursive_rmdir ( const Pathname path)
static

Like 'rm -r DIR'. Delete a directory, recursively removing its contents.

Returns
0 on success, ENOTDIR if path is not a directory, otherwise the commands return value.

References _Log_Result(), ExternalProgram::close(), DBG, isDir(), isExist(), output(), ExternalDataSource::receiveLine(), and ExternalProgram::Stderr_To_Stdout.

Referenced by ctime(), and erase().

int PathInfo::rename ( const Pathname oldpath,
const Pathname newpath 
)
static

Like '::rename'. Renames a file, moving it between directories if required.

Returns
0 on success, errno on failure

References _Log_Result(), Pathname::asString(), and DBG.

int PathInfo::rmdir ( const Pathname path)
static

Like '::rmdir'. Delete a directory, which must be empty.

Returns
0 on success, errno on failure

References _Log_Result(), DBG, and path().

Referenced by ctime().

void PathInfo::setMode ( Mode  mode)
inline

References mode().

Referenced by lstat(), and stat().

void PathInfo::setPath ( const Pathname path)
inline

References path().

Referenced by lstat(), operator()(), and stat().

off_t PathInfo::size ( ) const
inline

References isExist(), and statbuf_C.

Referenced by operator<<().

mode_t PathInfo::st_mode ( ) const
inline

References isExist(), statbuf_C, and userMay().

Referenced by fileType(), and operator<<().

bool PathInfo::stat ( const Pathname path)
inline

References operator()(), setMode(), setPath(), and STAT.

bool PathInfo::stat ( )
inline

References operator()(), setMode(), and STAT.

Referenced by operator()().

int PathInfo::symlink ( const Pathname oldpath,
const Pathname newpath 
)
static

Like '::symlink'. Creates a symbolic link named newpath which contains the string oldpath. If newpath exists it will not be overwritten.

Returns
0 on success, errno on failure.

References _Log_Result(), Pathname::asString(), and DBG.

int PathInfo::unlink ( const Pathname path)
static

Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).

Returns
0 on success, errno on failure

References _Log_Result(), DBG, and path().

Referenced by erase().

mode_t PathInfo::uperm ( ) const
inline

References isExist(), and statbuf_C.

Referenced by userMay().

mode_t PathInfo::userMay ( ) const
bool PathInfo::userMayR ( ) const
inline

References userMay().

bool PathInfo::userMayRW ( ) const
inline

References userMay().

bool PathInfo::userMayRWX ( ) const
inline

References userMay().

bool PathInfo::userMayRX ( ) const
inline

References userMay().

bool PathInfo::userMayW ( ) const
inline

References userMay().

bool PathInfo::userMayWX ( ) const
inline

References userMay().

bool PathInfo::userMayX ( ) const
inline

References userMay().

PathInfo::ZIP_TYPE PathInfo::zipType ( const Pathname file)
static

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  str,
const PathInfo obj 
)
friend
std::ostream& operator<< ( std::ostream &  str,
PathInfo::file_type  obj 
)
friend

Member Data Documentation

int PathInfo::error_i
private

Referenced by error(), isExist(), and operator()().

Mode PathInfo::mode_e
private

Referenced by mode(), and operator()().

Pathname PathInfo::path_t
private

Referenced by operator()(), and path().

struct stat PathInfo::statbuf_C
private

The documentation for this class was generated from the following files:

Generated on a sunny day for yast2-core by doxygen 1.8.11