#include <string>
#include <zypp/base/Logger.h>
#include <zypp/base/String.h>
+#include <zypp/base/IOStream.h>
#include <zypp/base/PtrTypes.h>
#include <zypp/CapFactory.h>
}
///////////////////////////////////////////////////////////////////
-// Return one line from stream
-std::string getline( std::istream & str )
-{
- static const unsigned tmpBuffLen = 1024;
- static char tmpBuff[tmpBuffLen];
- string ret;
- do {
- str.clear();
- str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
- ret += tmpBuff;
- } while( str.rdstate() == ios::failbit );
-
- return ret;
-}
-
-///////////////////////////////////////////////////////////////////
-// Simple lineparser: Call function do_r for each line.
-template<class _Function>
- _Function & forEachLine( std::istream & str_r, _Function & do_r )
- {
- while ( str_r )
- {
- std::string l = getline( str_r );
- if ( ! (str_r.fail() || str_r.bad()) )
- {
- // l contains valid data to be consumed.
- do_r( l );
- }
- }
- return do_r;
- }
-
-///////////////////////////////////////////////////////////////////
// Fits forEachLine. A simple 'do' function
void collect( const std::string & linre_r )
{
virtual void doConsume( const std::string & line_r )
{
if ( _lineNo == 1234 )
- ZYPP_THROW( "takes to long" );
+ ZYPP_THROW( Exception, "takes to long" );
}
};
///////////////////////////////////////////////////////////////////
PackageParseCollector datacollect;
try
{
- forEachLine( str, datacollect );
+ iostr::forEachLine( str, datacollect );
}
catch ( Exception & excpt_r )
{
#include "zypp/base/Logger.h"
#include "zypp/base/PtrTypes.h"
#include "zypp/base/Exception.h"
+#include "zypp/base/Fd.h"
+#include "zypp/Pathname.h"
using namespace std;
using namespace zypp;
-class MediaException : public Exception
-{
- public:
- MediaException( const std::string & msg_r )
- : Exception( msg_r )
- {}
-};
-
-void ex()
-{
- try
- {
- ZYPP_THROW( Exception, "plain exeption" );
- }
- catch ( Exception & excpt )
- {
- ZYPP_CAUGHT( excpt );
- }
-}
-
-void mex()
-{
- try
- {
- ZYPP_THROW( MediaException, "media error" );
- }
- catch ( MediaException & excpt )
- {
- SEC << "CAUGHT MediaException" << endl;
- ZYPP_CAUGHT( excpt );
- }
- catch ( Exception & excpt )
- {
- ZYPP_CAUGHT( excpt );
- }
-}
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
/******************************************************************
{
INT << "===[START]==========================================" << endl;
- ex();
- mex();
+ base::Fd( "packages", O_RDONLY );
INT << "===[END]============================================" << endl;
return 0;
* \verbinclude g_EnumerationClass
*/
////////////////////////////////////////////////////////////////////////////////
+/*! \defgroup g_RAII RAII soloutions
+ * \verbinclude g_RAII
+*/
+////////////////////////////////////////////////////////////////////////////////
/*! \defgroup g_BackenSpecific Backend Specific
* \verbinclude g_BackenSpecific
*/
--- /dev/null
+Resolvables are reference counted objects. They are usually created and
+provided by some kind of Source. They are accessed via smart Ptr classes,
+which handle the reference counting.
+
+ Resolvable (identification and dependencies)
+ ^
+ ResObject (common data, mostly UI driven: summary descrition...)
+ ^
+ Package, Patch,... (object specific data)
+
+
+The Ptr types are typedefed inside the classes:
+
+ Resolvable::Ptr (--> Resolvable*)
+ Resolvable::constPtr (--> const Resolvable*)
+
+ ResObject::Ptr, ... accordingly.
+
+Automatic conversion is the same as with ordinary pointer.
+
+Available casts are:
+
+ base::static_pointer_cast
+ base::const_pointer_cast
+ base::dynamic_pointer_cast
+
+
+
+Structure
+=========
+
+ base::ReferenceCounted
+ ^
+ -------------------------------------------------------
+
+ Resolvable <---------+
+ ^ backlink
+ ResObject +-----------ResObjectImplIf
+ ^ ^
+ Package PackageImplIf
+
+ -------------------------------------------------------
+ ^ ^
+ ResImplConnect<Package> --------> MyPackageImplementation
+
+++ /dev/null
-/*---------------------------------------------------------------------\
-| |
-| __ __ ____ _____ ____ |
-| \ \ / /_ _/ ___|_ _|___ \ |
-| \ V / _` \___ \ | | __) | |
-| | | (_| |___) || | / __/ |
-| |_|\__,_|____/ |_| |_____| |
-| |
-| core system |
-| (C) SuSE GmbH |
-\----------------------------------------------------------------------/
-
- File: PathInfo.cc
-
- Author: Michael Andres <ma@suse.de>
- Maintainer: Michael Andres <ma@suse.de>
-
-/-*/
-
-#include <iostream>
-#include <fstream>
-#include <iomanip>
-
-#include <y2util/Y2SLog.h>
-#include <y2util/stringutil.h>
-#include <y2util/ExternalProgram.h>
-
-#include <y2util/PathInfo.h>
-#include <y2util/Digest.h>
-
-using namespace std;
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const Pathname & path, Mode initial )
- : path_t( path )
- , mode_e( initial )
- , error_i( -1 )
-{
- operator()();
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const string & path, Mode initial )
- : path_t( path )
- , mode_e( initial )
- , error_i( -1 )
-{
- operator()();
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const char * path, Mode initial )
- : path_t( path )
- , mode_e( initial )
- , error_i( -1 )
-{
- operator()();
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::~PathInfo
-// METHOD TYPE : Destructor
-//
-// DESCRIPTION :
-//
-PathInfo::~PathInfo()
-{
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::operator()
-// METHOD TYPE : bool
-//
-// DESCRIPTION :
-//
-bool PathInfo::operator()()
-{
- if ( path_t.empty() ) {
- error_i = -1;
- } else {
- switch ( mode_e ) {
- case STAT:
- error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
- break;
- case LSTAT:
- error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
- break;
- }
- if ( error_i == -1 )
- error_i = errno;
- }
- return !error_i;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::fileType
-// METHOD TYPE : PathInfo::file_type
-//
-PathInfo::file_type PathInfo::fileType() const
-{
- if ( isExist() )
- return stat_mode( st_mode() ).fileType();
- return NOT_EXIST;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::userMay
-// METHOD TYPE : mode_t
-//
-// DESCRIPTION :
-//
-mode_t PathInfo::userMay() const
-{
- if ( !isExist() )
- return 0;
- if ( owner() == getuid() ) {
- return( uperm()/0100 );
- } else if ( group() == getgid() ) {
- return( gperm()/010 );
- }
- return operm();
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : ostream &
-**
-** DESCRIPTION :
-*/
-ostream & operator<<( ostream & str, const PathInfo & obj )
-{
- ios::fmtflags state_ii = str.flags();
-
- str << obj.asString() << "{";
- if ( !obj.isExist() ) {
- str << "does not exist}";
- } else {
- str << PathInfo::stat_mode( obj.st_mode() ) << " " << dec << obj.owner() << "/" << obj.group();
-
- if ( obj.isFile() )
- str << " size " << obj.size();
-
- str << "}";
- }
- str.flags( state_ii );
- return str;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-ostream & operator<<( ostream & str, PathInfo::file_type obj )
-{
- switch ( obj ) {
-#define EMUMOUT(T) case PathInfo::T: return str << #T; break
- EMUMOUT( NOT_AVAIL );
- EMUMOUT( NOT_EXIST );
- EMUMOUT( T_FILE );
- EMUMOUT( T_DIR );
- EMUMOUT( T_CHARDEV );
- EMUMOUT( T_BLOCKDEV );
- EMUMOUT( T_FIFO );
- EMUMOUT( T_LINK );
- EMUMOUT( T_SOCKET );
-#undef EMUMOUT
- }
- return str;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::stat_mode::fileType
-// METHOD TYPE : PathInfo::file_type
-//
-PathInfo::file_type PathInfo::stat_mode::fileType() const
-{
- if ( isFile() )
- return T_FILE;
- if ( isDir() )
- return T_DIR;
- if ( isLink() )
- return T_LINK;
- if ( isChr() )
- return T_CHARDEV;
- if ( isBlk() )
- return T_BLOCKDEV;
- if ( isFifo() )
- return T_FIFO;
- if ( isSock() )
- return T_SOCKET ;
-
- return NOT_AVAIL;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-std::ostream & operator<<( std::ostream & str, const PathInfo::stat_mode & obj )
-{
- ios::fmtflags state_ii = str.flags();
- char t = '?';
- if ( obj.isFile() )
- t = '-';
- else if ( obj.isDir() )
- t = 'd';
- else if ( obj.isLink() )
- t = 'l';
- else if ( obj.isChr() )
- t = 'c';
- else if ( obj.isBlk() )
- t = 'b';
- else if ( obj.isFifo() )
- t = 'p';
- else if ( obj.isSock() )
- t = 's';
-
- str << t << " " << setfill( '0' ) << setw( 4 ) << oct << obj.perm();
- str.flags( state_ii );
- return str;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : _Log_Result
-** FUNCTION TYPE : int
-**
-** DESCRIPTION : Helper function to log return values.
-*/
-inline int _Log_Result( const int res, const char * rclass = "errno" )
-{
- if ( res )
- DBG << " FAILED: " << rclass << " " << res;
- DBG << endl;
- return res;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::mkdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::mkdir( const Pathname & path, unsigned mode )
-{
- DBG << "mkdir " << path << ' ' << stringutil::octstring( mode );
- if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::assert_dir()
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::assert_dir( const Pathname & path, unsigned mode )
-{
- string::size_type pos, lastpos = 0;
- string spath = path.asString()+"/";
- int ret = 0;
-
- if(path.empty())
- return ENOENT;
-
- // skip ./
- if(path.relative())
- lastpos=2;
- // skip /
- else
- lastpos=1;
-
-// DBG << "about to create " << spath << endl;
- while((pos = spath.find('/',lastpos)) != string::npos )
- {
- string dir = spath.substr(0,pos);
- ret = ::mkdir(dir.c_str(), mode);
- if(ret == -1)
- {
- // ignore errors about already existing directorys
- if(errno == EEXIST)
- ret=0;
- else
- ret=errno;
- }
-// DBG << "creating directory " << dir << (ret?" failed":" succeeded") << endl;
- lastpos = pos+1;
- }
- return ret;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::rmdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::rmdir( const Pathname & path )
-{
- DBG << "rmdir " << path;
- if ( ::rmdir( path.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::recursive_rmdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::recursive_rmdir( const Pathname & path )
-{
- DBG << "recursive_rmdir " << path << ' ';
- PathInfo p( path );
-
- if ( !p.isExist() ) {
- return _Log_Result( 0 );
- }
-
- if ( !p.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- const char *const argv[] = {
- "/bin/rm",
- "-rf",
- "--preserve-root",
- "--",
- path.asString().c_str(),
- NULL
- };
-
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::clean_dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::clean_dir( const Pathname & path )
-{
- DBG << "clean_dir " << path << ' ';
- PathInfo p( path );
-
- if ( !p.isExist() ) {
- return _Log_Result( 0 );
- }
-
- if ( !p.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- string cmd( stringutil::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
- ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy_dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy_dir( const Pathname & srcpath, const Pathname & destpath )
-{
- DBG << "copy_dir " << srcpath << " -> " << destpath << ' ';
-
- PathInfo sp( srcpath );
- if ( !sp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- PathInfo dp( destpath );
- if ( !dp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- PathInfo tp( destpath + srcpath.basename() );
- if ( tp.isExist() ) {
- return _Log_Result( EEXIST );
- }
-
-
- const char *const argv[] = {
- "/bin/cp",
- "-dR",
- "--",
- srcpath.asString().c_str(),
- destpath.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::readdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::readdir( std::list<std::string> & retlist,
- const Pathname & path, bool dots )
-{
- retlist.clear();
-
- DBG << "readdir " << path << ' ';
-
- DIR * dir = ::opendir( path.asString().c_str() );
- if ( ! dir ) {
- return _Log_Result( errno );
- }
-
- struct dirent *entry;
- while ( (entry = ::readdir( dir )) != 0 ) {
-
- if ( entry->d_name[0] == '.' ) {
- if ( !dots )
- continue;
- if ( entry->d_name[1] == '\0'
- || ( entry->d_name[1] == '.'
- && entry->d_name[2] == '\0' ) )
- continue;
- }
- retlist.push_back( entry->d_name );
- }
-
- ::closedir( dir );
-
- return _Log_Result( 0 );
-}
-
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::readdir
-// METHOD TYPE : int
-//
-int PathInfo::readdir( dircontent & retlist, const Pathname & path,
- bool dots, Mode statmode )
-{
- retlist.clear();
-
- list<string> content;
- int res = readdir( content, path, dots );
-
- if ( !res ) {
- for ( list<string>::const_iterator it = content.begin(); it != content.end(); ++it ) {
- PathInfo p( path + *it, statmode );
- retlist.push_back( direntry( *it, p.fileType() ) );
- }
- }
-
- return res;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::unlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::unlink( const Pathname & path )
-{
- DBG << "unlink " << path;
- if ( ::unlink( path.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::rename
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::rename( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "rename " << oldpath << " -> " << newpath;
- if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy( const Pathname & file, const Pathname & dest )
-{
- DBG << "copy " << file << " -> " << dest << ' ';
-
- PathInfo sp( file );
- if ( !sp.isFile() ) {
- return _Log_Result( EINVAL );
- }
-
- PathInfo dp( dest );
- if ( dp.isDir() ) {
- return _Log_Result( EISDIR );
- }
-
- const char *const argv[] = {
- "/bin/cp",
- "--",
- file.asString().c_str(),
- dest.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::symlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::symlink( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "symlink " << newpath << " -> " << oldpath;
- if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::hardlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::hardlink( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "hardlink " << newpath << " -> " << oldpath;
- if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy_file2dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy_file2dir( const Pathname & file, const Pathname & dest )
-{
- DBG << "copy_file2dir " << file << " -> " << dest << ' ';
-
- PathInfo sp( file );
- if ( !sp.isFile() ) {
- return _Log_Result( EINVAL );
- }
-
- PathInfo dp( dest );
- if ( !dp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- const char *const argv[] = {
- "/bin/cp",
- "--",
- file.asString().c_str(),
- dest.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::md5sum
-// METHOD TYPE : std::string
-//
-std::string PathInfo::md5sum( const Pathname & file )
-{
- if ( ! PathInfo( file ).isFile() ) {
- return string();
- }
- ifstream istr( file.asString().c_str() );
- if ( ! istr ) {
- return string();
- }
- return Digest::digest( "MD5", istr );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::sha1sum
-// METHOD TYPE : std::string
-//
-std::string PathInfo::sha1sum( const Pathname & file )
-{
- if ( ! PathInfo( file ).isFile() ) {
- return string();
- }
- ifstream istr( file.asString().c_str() );
- if ( ! istr ) {
- return string();
- }
- return Digest::digest( "SHA1", istr );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::erase
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::erase( const Pathname & path )
-{
- int res = 0;
- PathInfo p( path, LSTAT );
- if ( p.isExist() )
- {
- if ( p.isDir() )
- res = PathInfo::recursive_rmdir( path );
- else
- res = PathInfo::unlink( path );
- }
- return res;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::chmod
-// METHOD TYPE : int
-//
-int PathInfo::chmod( const Pathname & path, mode_t mode )
-{
- DBG << "chmod " << path << ' ' << stringutil::octstring( mode );
- if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::zipType
-// METHOD TYPE : PathInfo::ZIP_TYPE
-//
-PathInfo::ZIP_TYPE PathInfo::zipType( const Pathname & file )
-{
- ZIP_TYPE ret = ZT_NONE;
-
- int fd = open( file.asString().c_str(), O_RDONLY );
-
- if ( fd != -1 ) {
- const int magicSize = 3;
- unsigned char magic[magicSize];
- memset( magic, 0, magicSize );
- if ( read( fd, magic, magicSize ) == magicSize ) {
- if ( magic[0] == 0037 && magic[1] == 0213 ) {
- ret = ZT_GZ;
- } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
- ret = ZT_BZ2;
- }
- }
- close( fd );
- }
-
- return ret;
-}
+++ /dev/null
-/*---------------------------------------------------------------------\
-| |
-| __ __ ____ _____ ____ |
-| \ \ / /_ _/ ___|_ _|___ \ |
-| \ V / _` \___ \ | | __) | |
-| | | (_| |___) || | / __/ |
-| |_|\__,_|____/ |_| |_____| |
-| |
-| core system |
-| (C) SuSE GmbH |
-\----------------------------------------------------------------------/
-
- File: PathInfo.h
-
- Author: Michael Andres <ma@suse.de>
- Maintainer: Michael Andres <ma@suse.de>
-
-/-*/
-#ifndef PathInfo_h
-#define PathInfo_h
-
-extern "C"
-{
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <dirent.h>
-}
-
-#include <cerrno>
-#include <iosfwd>
-#include <list>
-#include <set>
-#include <map>
-
-#include <y2util/Pathname.h>
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo
-/**
- * @short Wrapper class for ::stat/::lstat and other file/directory related operations.
- **/
-class PathInfo {
-
- friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
-
- public:
-
- enum Mode { STAT, LSTAT };
-
- enum file_type {
- NOT_AVAIL = 0x00, // no typeinfo available
- NOT_EXIST = 0x01, // file does not exist
- T_FILE = 0x02,
- T_DIR = 0x04,
- T_CHARDEV = 0x08,
- T_BLOCKDEV = 0x10,
- T_FIFO = 0x20,
- T_LINK = 0x40,
- T_SOCKET = 0x80
- };
- friend std::ostream & operator<<( std::ostream & str, file_type obj );
-
- /**
- * Wrapper class for mode_t values as derived from ::stat
- **/
- class stat_mode;
-
- /**
- * Simple cache remembering device/inode to detect hardlinks.
- **/
- class devino_cache;
-
- private:
-
- Pathname path_t;
-
- struct stat statbuf_C;
- Mode mode_e;
- int error_i;
-
- public:
-
- 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 Pathname & path() const { return path_t; }
- const std::string & asString() const { return path_t.asString(); }
- Mode mode() const { return mode_e; }
- int error() const { return error_i; }
-
- void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
- void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
-
- bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
- bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
- bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
-
- bool stat() { setMode( STAT ); return operator()(); }
- bool lstat() { setMode( LSTAT ); return operator()(); }
- bool operator()();
-
- public:
-
- bool isExist() const { return !error_i; }
-
- // file type
- file_type fileType() const;
-
- bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
- bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
- bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
- bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
- bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
- bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
- bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
-
- nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
-
- // owner
- uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
- gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
-
- // permission
- bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
- bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
- bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
-
- bool isR() const { return isRUsr(); }
- bool isW() const { return isWUsr(); }
- bool isX() const { return isXUsr(); }
-
- bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
- bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
- bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
-
- bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
- bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
- bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
-
- bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
- bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
- bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
-
- mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
- mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
- mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
- mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
-
- bool isPerm ( mode_t m ) const { return (m == perm()); }
- bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
-
- mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
-
- // permission according to current uid/gid (returns [0-7])
- mode_t userMay() const;
-
- bool userMayR() const { return( userMay() & 01 ); }
- bool userMayW() const { return( userMay() & 02 ); }
- bool userMayX() const { return( userMay() & 04 ); }
-
- bool userMayRW() const { return( (userMay() & 03) == 03 ); }
- bool userMayRX() const { return( (userMay() & 05) == 05 ); }
- bool userMayWX() const { return( (userMay() & 06) == 06 ); }
-
- bool userMayRWX() const { return( userMay() == 07 ); }
-
- // device
- dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
- dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
- ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
-
- // size
- off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
- unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
- unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
-
- // time
- time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
- time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
- time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
-
- public:
-
- ///////////////////////////////////////////////////////////////////
- // convenience stuff
- ///////////////////////////////////////////////////////////////////
- // static functions as they may or may not invalidate any stat info
- // stored by a PathiInfo.
- ///////////////////////////////////////////////////////////////////
-
- ///////////////////////////////////////////////////////////////////
- // Directories
- ///////////////////////////////////////////////////////////////////
-
- /**
- * 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.
- *
- * @return 0 on success, errno on failure
- **/
- static int mkdir( const Pathname & path, unsigned mode = 0755 );
-
- /**
- * 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.
- *
- * @return 0 on success, errno on failure
- **/
- static int assert_dir( const Pathname & path, unsigned mode = 0755 );
-
- /**
- * Like '::rmdir'. Delete a directory, which must be empty.
- *
- * @return 0 on success, errno on failure
- **/
- static int rmdir( const Pathname & path );
-
- /**
- * Like 'rm -r DIR'. Delete a directory, recursively removing its contents.
- *
- * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
- * commands return value.
- **/
- static int recursive_rmdir( const Pathname & path );
-
- /**
- * Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.
- *
- * @return 0 on success, ENOTDIR if path is not a directory, otherwise the
- * commands return value.
- **/
- static int clean_dir( const Pathname & path );
-
- /**
- * Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be
- * directories. 'basename srcpath' must not exist in destpath.
- *
- * @return 0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if
- * 'basename srcpath' exists in destpath, otherwise the commands return value.
- **/
- static int copy_dir( const Pathname & srcpath, const Pathname & destpath );
-
- /**
- * Return content of directory via retlist. If dots is false
- * entries starting with '.' are not reported. "." and ".."
- * are never reported.
- *
- * @return 0 on success, errno on failure.
- **/
- static int readdir( std::list<std::string> & retlist,
- const Pathname & path, bool dots = true );
-
- struct direntry {
- std::string name;
- file_type type;
- direntry( const std::string & name_r = std::string(), file_type type_r = NOT_AVAIL )
- : name( name_r )
- , type( type_r )
- {}
- };
-
- typedef std::list<direntry> dircontent;
-
- /**
- * 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).
- *
- * @return 0 on success, errno on failure.
- **/
- static int readdir( dircontent & retlist, const Pathname & path,
- bool dots = true, Mode statmode = STAT );
-
- ///////////////////////////////////////////////////////////////////
- // Files
- ///////////////////////////////////////////////////////////////////
-
- /**
- * Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
- *
- * @return 0 on success, errno on failure
- **/
- static int unlink( const Pathname & path );
-
- /**
- * Like '::rename'. Renames a file, moving it between directories if required.
- *
- * @return 0 on success, errno on failure
- **/
- static int rename( const Pathname & oldpath, const Pathname & newpath );
-
- /**
- * Like 'cp file dest'. Copy file to destination file.
- *
- * @return 0 on success, EINVAL if file is not a file, EISDIR if
- * destiantion is a directory, otherwise the commands return value.
- **/
- static int copy( const Pathname & file, const Pathname & dest );
-
- /**
- * Like '::symlink'. Creates a symbolic link named newpath which contains
- * the string oldpath. If newpath exists it will not be overwritten.
- *
- * @return 0 on success, errno on failure.
- **/
- static int symlink( const Pathname & oldpath, const Pathname & newpath );
-
- /**
- * Like '::link'. Creates a hard link named newpath to an existing file
- * oldpath. If newpath exists it will not be overwritten.
- *
- * @return 0 on success, errno on failure.
- **/
- static int hardlink( const Pathname & oldpath, const Pathname & newpath );
-
- /**
- * Like 'cp file dest'. Copy file to dest dir.
- *
- * @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
- * is no directory, otherwise the commands return value.
- **/
- static int copy_file2dir( const Pathname & file, const Pathname & dest );
-
- /**
- * Compute a files md5sum.
- *
- * @return the files md5sum on success, otherwise an empty string..
- **/
- static std::string md5sum( const Pathname & file );
-
- /**
- * Compute a files sha1sum.
- *
- * @return the files sha1sum on success, otherwise an empty string..
- **/
- static std::string sha1sum( const Pathname & file );
-
- ///////////////////////////////////////////////////////////////////
- //
- ///////////////////////////////////////////////////////////////////
-
- /**
- * Erase whatever happens to be located at path (file or directory).
- *
- * @return 0 on success.
- **/
- static int erase( const Pathname & path );
-
- ///////////////////////////////////////////////////////////////////
- // permissions
- ///////////////////////////////////////////////////////////////////
-
- /**
- * Like '::chmod'. The mode of the file given by path is changed.
- *
- * @return 0 on success, errno on failure
- **/
- static int chmod( const Pathname & path, mode_t mode );
-
- ///////////////////////////////////////////////////////////////////
- // magic
- ///////////////////////////////////////////////////////////////////
-
- /**
- * Test whether a file is compressed (gzip/bzip2).
- *
- * @return ZT_GZ, ZT_BZ2 if file is compressed, otherwise ZT_NONE.
- **/
- enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
-
- static ZIP_TYPE zipType( const Pathname & file );
-};
-
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo::stat_mode
-/**
- * @short Wrapper class for mode_t values as derived from ::stat
- **/
-class PathInfo::stat_mode {
- friend std::ostream & operator<<( std::ostream & str, const stat_mode & obj );
- private:
- mode_t _mode;
- public:
- stat_mode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {}
- public:
- // file type
- file_type fileType() const;
-
- bool isFile() const { return S_ISREG( _mode ); }
- bool isDir () const { return S_ISDIR( _mode ); }
- bool isLink() const { return S_ISLNK( _mode ); }
- bool isChr() const { return S_ISCHR( _mode ); }
- bool isBlk() const { return S_ISBLK( _mode ); }
- bool isFifo() const { return S_ISFIFO( _mode ); }
- bool isSock() const { return S_ISSOCK( _mode ); }
-
- // permission
- bool isRUsr() const { return (_mode & S_IRUSR); }
- bool isWUsr() const { return (_mode & S_IWUSR); }
- bool isXUsr() const { return (_mode & S_IXUSR); }
-
- bool isR() const { return isRUsr(); }
- bool isW() const { return isWUsr(); }
- bool isX() const { return isXUsr(); }
-
- bool isRGrp() const { return (_mode & S_IRGRP); }
- bool isWGrp() const { return (_mode & S_IWGRP); }
- bool isXGrp() const { return (_mode & S_IXGRP); }
-
- bool isROth() const { return (_mode & S_IROTH); }
- bool isWOth() const { return (_mode & S_IWOTH); }
- bool isXOth() const { return (_mode & S_IXOTH); }
-
- bool isUid() const { return (_mode & S_ISUID); }
- bool isGid() const { return (_mode & S_ISGID); }
- bool isVtx() const { return (_mode & S_ISVTX); }
-
- mode_t uperm() const { return (_mode & S_IRWXU); }
- mode_t gperm() const { return (_mode & S_IRWXG); }
- mode_t operm() const { return (_mode & S_IRWXO); }
- mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
-
- bool isPerm ( mode_t m ) const { return (m == perm()); }
- bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
-
- mode_t st_mode() const { return _mode; }
-};
-
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo::devino_cache
-/**
- * @short Simple cache remembering device/inode to detect hardlinks.
- * <pre>
- * PathInfo::devino_cache trace;
- * for ( all files ) {
- * if ( trace.insert( file.device, file.inode ) ) {
- * // 1st occurance of file
- * }
- * // else: hardlink; already counted this device/inode
- * }
- * }
- * </pre>
- **/
-class PathInfo::devino_cache {
-
- private:
-
- std::map<dev_t,std::set<ino_t> > _devino;
-
- public:
- /**
- * Constructor
- **/
- devino_cache() {}
-
- /**
- * Clear cache
- **/
- void clear() { _devino.clear(); }
-
- /**
- * Remember dev/ino. Return <code>true</code> if it's inserted the first
- * time, <code>false</code> if alredy present in cache (a hardlink to a
- * previously remembered file.
- **/
- bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
- return _devino[dev_r].insert( ino_r ).second;
- }
-};
-
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-
-#endif // PathInfo_h
+++ /dev/null
-/*---------------------------------------------------------------------\
-| |
-| __ __ ____ _____ ____ |
-| \ \ / /_ _/ ___|_ _|___ \ |
-| \ V / _` \___ \ | | __) | |
-| | | (_| |___) || | / __/ |
-| |_|\__,_|____/ |_| |_____| |
-| |
-| core system |
-| (C) SuSE GmbH |
-\----------------------------------------------------------------------/
-
- File: Pathname.cc
-
- Author: Michael Andres <ma@suse.de>
- Maintainer: Michael Andres <ma@suse.de>
-
-/-*/
-
-#include <iostream>
-
-#include <y2util/Pathname.h>
-
-using namespace std;
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : DirStack
-//
-// DESCRIPTION :
-//
-class DirStack {
-
- struct Dir {
-
- Dir * up;
- Dir * dn;
- string name;
-
- Dir( const string & n = "" ) {
- name = n;
- up = dn = 0;
- }
-
- ~Dir() {
- if ( up )
- up->dn = dn;
- if ( dn )
- dn->up = up;
- }
- };
-
- Dir * top;
- Dir * bot;
-
- void Pop() {
- if ( !top )
- return;
- top = top->dn;
- if ( top )
- delete top->up;
- else {
- delete bot;
- bot = 0;
- }
- }
-
- public:
-
- DirStack() { top = bot = 0; }
- ~DirStack() {
- while ( bot )
- Pop();
- }
-
- void Push( const string & n ) {
- if ( n.empty() || n == "." ) { // '.' or '/' only for bot
- if ( bot )
- return;
- } else if ( n == ".." && top ) {
- if ( top->name == "" ) // "/.." ==> "/"
- return;
-
- if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
- Pop();
- return;
- }
- // "../.." "./.." stays
- }
-
- Dir * d = new Dir( n );
- if ( !top )
- top = bot = d;
- else {
- top->up = d;
- d->dn = top;
- d->up = 0;
- top = d;
- }
- }
-
- string str() {
- if ( !bot )
- return "";
- string ret;
- for ( Dir * d = bot; d; d = d->up ) {
- if ( d != bot )
- ret += "/";
- ret += d->name;
- }
- if ( ret.empty() )
- return "/";
- return ret;
- }
-};
-
-///////////////////////////////////////////////////////////////////
-
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::_assign
-// METHOD TYPE : void
-//
-// DESCRIPTION :
-//
-void Pathname::_assign( const string & name_tv )
-{
- prfx_i = 0;
- name_t = name_tv;
-
- if ( name_t.empty() )
- return;
-
- string Tprfx;
- DirStack Stack_Ci;
-
- char * Buf_aci = new char[name_tv.length() + 1];
- char * W_pci = Buf_aci;
- const char * R_pci = name_tv.c_str();
-
- // check for prefix
- if ( name_t.length() >= 2
- && name_t[1] == ':'
- && ( 'a' <= name_t[0] && name_t[0] <= 'z'
- || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
- Tprfx = name_t.substr( 0, 2 );
- prfx_i = 2;
- R_pci += 2;
- }
-
- // rel or abs path
- if ( *R_pci == '/' || *R_pci == '\\' ) {
- Stack_Ci.Push( "" );
- ++R_pci;
- } else {
- Stack_Ci.Push( "." );
- }
-
- do {
- switch ( *R_pci ) {
- case '/':
- case '\\':
- case '\0':
- if ( W_pci != Buf_aci ) {
- *W_pci = '\0';
- W_pci = Buf_aci;
- Stack_Ci.Push( Buf_aci );
- }
- break;
-
- default:
- *W_pci++ = *R_pci;
- break;
- }
- } while( *R_pci++ );
-
- delete Buf_aci;
- name_t = Tprfx + Stack_Ci.str();
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::dirname
-// METHOD TYPE : Pathname
-//
-// DESCRIPTION :
-//
-Pathname Pathname::dirname( const Pathname & name_tv )
-{
- if ( name_tv.empty() )
- return "";
-
- Pathname ret_t( name_tv );
- string::size_type idx = ret_t.name_t.find_last_of( '/' );
-
- if ( idx == string::npos ) {
- ret_t.name_t.erase( ret_t.prfx_i );
- ret_t.name_t += ".";
- } else if ( idx == ret_t.prfx_i ) {
- ret_t.name_t.erase( ret_t.prfx_i );
- ret_t.name_t += "/";
- } else {
- ret_t.name_t.erase( idx );
- }
-
- return ret_t;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::basename
-// METHOD TYPE : string
-//
-// DESCRIPTION :
-//
-string Pathname::basename( const Pathname & name_tv )
-{
- if ( name_tv.empty() )
- return "";
-
- string ret_t( name_tv.asString() );
- ret_t.erase( 0, name_tv.prfx_i );
- string::size_type idx = ret_t.find_last_of( '/' );
- if ( idx != string::npos ) {
- ret_t.erase( 0, idx+1 );
- }
-
- return ret_t;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::cat
-// METHOD TYPE : Pathname
-//
-// DESCRIPTION :
-//
-Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
-{
- if ( add_tv.empty() )
- return name_tv;
- if ( name_tv.empty() )
- return add_tv;
-
- string ret_ti( add_tv.asString() );
- ret_ti.replace( 0, add_tv.prfx_i, "/" );
-
- return name_tv.asString() + ret_ti;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::Extend
-// METHOD TYPE : Pathname
-//
-// DESCRIPTION :
-//
-Pathname Pathname::extend( const Pathname & l, const string & r )
-{
- return l.asString() + r;
-}
-
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : Pathname::equal
-// METHOD TYPE : bool
-//
-// DESCRIPTION :
-//
-bool Pathname::equal( const Pathname & l, const Pathname & r )
-{
- return l.asString() == r.asString();
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : inline std::ostream &
-**
-** DESCRIPTION :
-*/
-ostream & operator<<( ostream & str, const Pathname & obj )
-{
- return str << obj.asString();
-}
+++ /dev/null
-/*---------------------------------------------------------------------\
-| ____ _ __ __ ___ |
-| |__ / \ / / . \ . \ |
-| / / \ V /| _/ _/ |
-| / /__ | | | | | | |
-| /_____||_| |_| |_| |
-| |
-\---------------------------------------------------------------------*/
-/** \file zypp/base/Pathname.h
- *
-*/
-#ifndef ZYPP_BASE_PATHNAME_H
-#define ZYPP_BASE_PATHNAME_H
-
-#include <iosfwd>
-#include <string>
-
-///////////////////////////////////////////////////////////////////
-namespace zypp
-{ /////////////////////////////////////////////////////////////////
-
- ///////////////////////////////////////////////////////////////////
- //
- // CLASS NAME : Pathname
- //
- /** Pathname.
- * \todo Review. Maybe use COW pimpl, ckeck storage.
- */
- class Pathname
- {
- public:
- /** */
- Pathname()
- : prfx_i( 0 )
- {}
- /** */
- Pathname( const Pathname & path_tv )
- : prfx_i( path_tv.prfx_i )
- , name_t( path_tv.name_t )
- {}
- /** */
- Pathname( const std::string & name_tv )
- { _assign( name_tv ); }
- /** */
- Pathname( const char * name_tv )
- { _assign( name_tv ? name_tv : "" ); }
- /** */
- ~Pathname()
- {}
-
- Pathname & operator= ( const Pathname & path_tv );
- Pathname & operator+=( const Pathname & path_tv );
-
- const std::string & asString() const { return name_t; }
-
- bool empty() const { return !name_t.size(); }
- bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
- bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
-
- Pathname dirname() const { return dirname( *this ); }
- std::string basename() const { return basename( *this ); }
- Pathname absolutename() const { return absolutename( *this ); }
- Pathname relativename() const { return relativename( *this ); }
-
- static Pathname dirname ( const Pathname & name_tv );
- static std::string basename ( const Pathname & name_tv );
- static Pathname absolutename( const Pathname & name_tv ) { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
- static Pathname relativename( const Pathname & name_tv ) { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
-
- Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
- static Pathname cat( const Pathname & l, const Pathname & r );
-
- Pathname extend( const std::string & r ) const { return extend( *this, r ); }
- static Pathname extend( const Pathname & l, const std::string & r );
-
- bool equal( const Pathname & r ) const { return equal( *this, r ); }
- static bool equal( const Pathname & l, const Pathname & r );
-
- private:
- std::string::size_type prfx_i;
- std::string name_t;
- void _assign( const std::string & name_tv );
- };
- ///////////////////////////////////////////////////////////////////
-
- inline bool operator==( const Pathname & l, const Pathname & r ) {
- return Pathname::equal( l, r );
- }
-
- inline bool operator!=( const Pathname & l, const Pathname & r ) {
- return !Pathname::equal( l, r );
- }
-
- inline Pathname operator+( const Pathname & l, const Pathname & r ) {
- return Pathname::cat( l, r );
- }
-
- inline Pathname & Pathname::operator=( const Pathname & path_tv ) {
- if ( &path_tv != this ) {
- prfx_i = path_tv.prfx_i;
- name_t = path_tv.name_t;
- }
- return *this;
- }
-
- inline Pathname & Pathname::operator+=( const Pathname & path_tv ) {
- return( *this = *this + path_tv );
- }
-
- ///////////////////////////////////////////////////////////////////
-
- /** \relates Pathname Stream output */
- extern std::ostream & operator<<( std::ostream & str, const Pathname & obj );
-
- /////////////////////////////////////////////////////////////////
-} // namespace zypp
-///////////////////////////////////////////////////////////////////
-#endif // ZYPP_BASE_PATHNAME_H
return ret;
}
-/******************************************************************
-**
-**
-** FUNCTION NAME : ltrim
-** FUNCTION TYPE : std::string
-**
-** DESCRIPTION :
-*/
-std::string ltrim( const std::string & s )
-{
- if ( s.empty() )
- return s;
-
- string::size_type p = s.find_first_not_of( " \t\n" );
- if ( p == string::npos )
- return "";
-
- return s.substr( p );
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : rtrim
-** FUNCTION TYPE : std::string
-**
-** DESCRIPTION :
-*/
-std::string rtrim( const std::string & s )
-{
- if ( s.empty() )
- return s;
-
- string::size_type p = s.find_last_not_of( " \t\n" );
- if ( p == string::npos )
- return "";
-
- return s.substr( 0, p+1 );
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : toLower
-** FUNCTION TYPE : std::string
-**
-** DESCRIPTION :
-*/
-std::string toLower( const std::string & s )
-{
- if ( s.empty() )
- return s;
-
- string ret( s );
- for ( string::size_type i = 0; i < ret.length(); ++i ) {
- if ( isupper( ret[i] ) )
- ret[i] = static_cast<char>(tolower( ret[i] ));
- }
- return ret;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : toUpper
-** FUNCTION TYPE : std::string
-**
-** DESCRIPTION :
-*/
-std::string toUpper( const std::string & s )
-{
- if ( s.empty() )
- return s;
-
- string ret( s );
- for ( string::size_type i = 0; i < ret.length(); ++i ) {
- if ( islower( ret[i] ) )
- ret[i] = static_cast<char>(toupper( ret[i] ));
- }
- return ret;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : dumpOn
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered )
-{
- unsigned i = 0;
- for ( std::list<std::string>::const_iterator it = l.begin(); it != l.end(); ++it, ++i ) {
- if ( numbered )
- str << '[' << i << ']';
- str << *it << endl;
- }
- return str;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : dumpOn
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered )
-{
- for ( unsigned i = 0; i < l.size(); ++i ) {
- if ( numbered )
- str << '[' << i << ']';
- str << l[i] << endl;
- }
- return str;
-}
-
///////////////////////////////////////////////////////////////////
} // namespace stringutil
///////////////////////////////////////////////////////////////////
namespace stringutil {
;//////////////////////////////////////////////////////////////////
-enum Trim {
- NO_TRIM = 0x00,
- L_TRIM = 0x01,
- R_TRIM = 0x02,
- TRIM = (L_TRIM|R_TRIM)
-};
-
-inline std::string form( const char * format, ... )
- __attribute__ ((format (printf, 1, 2)));
-
-/**
- * Printf style building of strings via format string.
- * <PRE>
- * std::string ex( stringutil::form( "Example number %d", 1 ) );
- * std::cout << ex << stringutil::form( " and number %d.", 2 ) << endl;
- *
- * Will print: Example number 1 and number 2.
- * </PRE>
- **/
-inline std::string form( const char * format, ... ) {
- char * buf = 0;
- std::string val;
-
- va_list ap;
- va_start( ap, format );
-
-#if 1
- vasprintf( &buf, format, ap );
- if ( buf ) {
- val = buf;
- free( buf );
- }
-#else
- // Don't know wheter we actually nedd two va_lists, one to
- // evaluate the buffer size needed, and one to actually fill
- // the buffer. Maybe there's a save way to reuse a va_lists.
- va_list ap1;
- va_start( ap1, format );
- buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
- vsprintf( buf, format, ap1 );
- val = buf;
- delete [] buf;
- va_end( ap1 );
-#endif
-
- va_end( ap );
- return val;
-}
-
-/**
- * Print number. Optional second argument sets the minimal string width (' ' padded).
- * Negative values will cause the number to be left adjusted within the string. Default
- * width is 0.
- * <PRE>
- * numstring(42) -> "42"
- * numstring(42, 4) -> " 42"
- * numstring(42,-4) -> "42 "
- * </PRE>
- **/
-inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
-inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
-inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
-inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
-inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
-inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
-inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
-inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
-inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
-inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
-
-/**
- * Print number as hex value with leading '0x'. Optional second argument sets the minimal
- * string width (0 padded). Negative values will cause the number to be left adjusted
- * within the string. Default width is 10 (4 for char).
- * <PRE>
- * hexstring(42) -> "0x0000002a"
- * hexstring(42, 4) -> "0x2a"
- * hexstring(42,-4) -> "0x2a"
- * </PRE>
- **/
-inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
-inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
-inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
-inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
-inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
-inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
-inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
-inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
-inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
-inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
-
-/**
- * Print number as octal value with leading '0'. Optional second argument sets the minimal
- * string width (0 padded). Negative values will cause the number to be left adjusted
- * within the string. Default width is 5 (4 for char).
- * <PRE>
- * octstring(42) -> "00052"
- * octstring(42, 4) -> "0052"
- * octstring(42,-4) -> "052 "
- * </PRE>
- **/
-inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
-inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
-inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
-inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
-inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
-inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
-inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
-inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
-inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
-inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
-
-/**
- * String to integer type determined by template arg: time_t t = strtonum<time_t>( "42" );
- **/
-template<typename _It>
- inline _It strtonum( const std::string & str );
-
-template<>
- inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
-template<>
- inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
-template<>
- inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
-template<>
- inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
-
-template<>
- inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
-template<>
- inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
-template<>
- inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
-template<>
- inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
-
-/**
- * String to integer type detemined function arg: time_t t; strtonum( "42", t );
- **/
-template<typename _It>
- inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
-
-/** \brief read one line from a stream
- * Return one line read from istream. Afterwards the streampos is behind the delimiting '\n'
- * (or at EOF). The delimiting '\n' is <b>not</b> returned.
- *
- * If trim is true, the string returned is trimmed (surrounding whitespaces removed).
- * <PRE>
- * ifstream s( "somefile" );
- *
- * while ( s ) {
- * string l = getline( s );
- * if ( !(s.fail() || s.bad()) ) {
- *
- * // l contains valid data to be consumed.
- * // In case it makes any difference to you:
- * if ( s.good() ) {
- * // A delimiting '\n' was read.
- * } else {
- * // s.eof() is set: There's no '\n' at the end of file.
- * // Note: The line returned may netvertheless be empty if trimed is true.
- * }
- * }
- * }
- * </PRE>
- **/
-extern std::string getline( std::istream & str, bool trim = false );
-
/** \brief read one line from a stream
*
* like above but with allows to specify trimming direction
return s;
}
-/**
- * Return string converted to lower/upper case
- **/
-extern std::string toLower( const std::string & s );
-extern std::string toUpper( const std::string & s );
-
-/**
- * Helper for stream output
- **/
-extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
-extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
-
///////////////////////////////////////////////////////////////////
} // namespace stringutil
///////////////////////////////////////////////////////////////////
\---------------------------------------------------------------------*/
/** \file zypp/PathInfo.cc
*
- * \todo replace by Blocxx
- *
*/
#include <iostream>
#include <iomanip>
#include "zypp/base/Logger.h"
-#include "zypp/base/stringutil.h"
-#include "zypp/ExternalProgram.h"
+#include "zypp/base/String.h"
+#include "zypp/base/IOStream.h"
+#include "zypp/ExternalProgram.h"
#include "zypp/PathInfo.h"
#include "zypp/Digest.h"
-using namespace std;
-using namespace zypp::base;
-
-namespace zypp {
+using std::string;
///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const Pathname & path, Mode initial )
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace filesystem
+ { /////////////////////////////////////////////////////////////////
+
+ /******************************************************************
+ **
+ ** FUNCTION NAME : operator<<
+ ** FUNCTION TYPE : std::ostream &
+ */
+ std::ostream & operator<<( std::ostream & str, FileType obj )
+ {
+ switch ( obj ) {
+#define EMUMOUT(T) case T: return str << #T; break
+ EMUMOUT( FT_NOT_AVAIL );
+ EMUMOUT( FT_NOT_EXIST );
+ EMUMOUT( FT_FILE );
+ EMUMOUT( FT_DIR );
+ EMUMOUT( FT_CHARDEV );
+ EMUMOUT( FT_BLOCKDEV );
+ EMUMOUT( FT_FIFO );
+ EMUMOUT( FT_LINK );
+ EMUMOUT( FT_SOCKET );
+#undef EMUMOUT
+ }
+ return str;
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : StatMode::fileType
+ // METHOD TYPE : FileType
+ //
+ FileType StatMode::fileType() const
+ {
+ if ( isFile() )
+ return FT_FILE;
+ if ( isDir() )
+ return FT_DIR;
+ if ( isLink() )
+ return FT_LINK;
+ if ( isChr() )
+ return FT_CHARDEV;
+ if ( isBlk() )
+ return FT_BLOCKDEV;
+ if ( isFifo() )
+ return FT_FIFO;
+ if ( isSock() )
+ return FT_SOCKET ;
+
+ return FT_NOT_AVAIL;
+ }
+
+ /******************************************************************
+ **
+ ** FUNCTION NAME : operator<<
+ ** FUNCTION TYPE : std::ostream &
+ */
+ std::ostream & operator<<( std::ostream & str, const StatMode & obj )
+ {
+ iostr::IosFmtFlagsSaver autoResoreState( str );
+
+ char t = '?';
+ if ( obj.isFile() )
+ t = '-';
+ else if ( obj.isDir() )
+ t = 'd';
+ else if ( obj.isLink() )
+ t = 'l';
+ else if ( obj.isChr() )
+ t = 'c';
+ else if ( obj.isBlk() )
+ t = 'b';
+ else if ( obj.isFifo() )
+ t = 'p';
+ else if ( obj.isSock() )
+ t = 's';
+
+ str << t << " " << std::setfill( '0' ) << std::setw( 4 ) << std::oct << obj.perm();
+ return str;
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // Class : PathInfo
+ //
+ ///////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::PathInfo
+ // METHOD TYPE : Constructor
+ //
+ PathInfo::PathInfo()
+ : mode_e( STAT )
+ , error_i( -1 )
+ {}
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::PathInfo
+ // METHOD TYPE : Constructor
+ //
+ PathInfo::PathInfo( const Pathname & path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
-{
- operator()();
-}
+ {
+ operator()();
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const string & path, Mode initial )
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::PathInfo
+ // METHOD TYPE : Constructor
+ //
+ PathInfo::PathInfo( const std::string & path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
-{
- operator()();
-}
+ {
+ operator()();
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::PathInfo
-// METHOD TYPE : Constructor
-//
-// DESCRIPTION :
-//
-PathInfo::PathInfo( const char * path, Mode initial )
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::PathInfo
+ // METHOD TYPE : Constructor
+ //
+ PathInfo::PathInfo( const char * path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
-{
- operator()();
-}
+ {
+ operator()();
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::~PathInfo
-// METHOD TYPE : Destructor
-//
-// DESCRIPTION :
-//
-PathInfo::~PathInfo()
-{
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::~PathInfo
+ // METHOD TYPE : Destructor
+ //
+ PathInfo::~PathInfo()
+ {
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::operator()
-// METHOD TYPE : bool
-//
-// DESCRIPTION :
-//
-bool PathInfo::operator()()
-{
- if ( path_t.empty() ) {
- error_i = -1;
- } else {
- switch ( mode_e ) {
- case STAT:
- error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
- break;
- case LSTAT:
- error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
- break;
- }
- if ( error_i == -1 )
- error_i = errno;
- }
- return !error_i;
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::operator()
+ // METHOD TYPE : bool
+ //
+ bool PathInfo::operator()()
+ {
+ if ( path_t.empty() ) {
+ error_i = -1;
+ } else {
+ switch ( mode_e ) {
+ case STAT:
+ error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
+ break;
+ case LSTAT:
+ error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
+ break;
+ }
+ if ( error_i == -1 )
+ error_i = errno;
+ }
+ return !error_i;
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::fileType
-// METHOD TYPE : PathInfo::file_type
-//
-PathInfo::file_type PathInfo::fileType() const
-{
- if ( isExist() )
- return stat_mode( st_mode() ).fileType();
- return NOT_EXIST;
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::fileType
+ // METHOD TYPE : File_type
+ //
+ FileType PathInfo::fileType() const
+ {
+ if ( isExist() )
+ return asStatMode().fileType();
+ return FT_NOT_EXIST;
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::userMay
-// METHOD TYPE : mode_t
-//
-// DESCRIPTION :
-//
-mode_t PathInfo::userMay() const
-{
- if ( !isExist() )
- return 0;
- if ( owner() == getuid() ) {
- return( uperm()/0100 );
- } else if ( group() == getgid() ) {
- return( gperm()/010 );
- }
- return operm();
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : ostream &
-**
-** DESCRIPTION :
-*/
-ostream & operator<<( ostream & str, const PathInfo & obj )
-{
- ios::fmtflags state_ii = str.flags();
-
- str << obj.asString() << "{";
- if ( !obj.isExist() ) {
- str << "does not exist}";
- } else {
- str << PathInfo::stat_mode( obj.st_mode() ) << " " << dec << obj.owner() << "/" << obj.group();
-
- if ( obj.isFile() )
- str << " size " << obj.size();
-
- str << "}";
- }
- str.flags( state_ii );
- return str;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-ostream & operator<<( ostream & str, PathInfo::file_type obj )
-{
- switch ( obj ) {
-#define EMUMOUT(T) case PathInfo::T: return str << #T; break
- EMUMOUT( NOT_AVAIL );
- EMUMOUT( NOT_EXIST );
- EMUMOUT( T_FILE );
- EMUMOUT( T_DIR );
- EMUMOUT( T_CHARDEV );
- EMUMOUT( T_BLOCKDEV );
- EMUMOUT( T_FIFO );
- EMUMOUT( T_LINK );
- EMUMOUT( T_SOCKET );
-#undef EMUMOUT
- }
- return str;
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::userMay
+ // METHOD TYPE : mode_t
+ //
+ mode_t PathInfo::userMay() const
+ {
+ if ( !isExist() )
+ return 0;
+ if ( owner() == getuid() ) {
+ return( uperm()/0100 );
+ } else if ( group() == getgid() ) {
+ return( gperm()/010 );
+ }
+ return operm();
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::stat_mode::fileType
-// METHOD TYPE : PathInfo::file_type
-//
-PathInfo::file_type PathInfo::stat_mode::fileType() const
-{
- if ( isFile() )
- return T_FILE;
- if ( isDir() )
- return T_DIR;
- if ( isLink() )
- return T_LINK;
- if ( isChr() )
- return T_CHARDEV;
- if ( isBlk() )
- return T_BLOCKDEV;
- if ( isFifo() )
- return T_FIFO;
- if ( isSock() )
- return T_SOCKET ;
-
- return NOT_AVAIL;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : operator<<
-** FUNCTION TYPE : std::ostream &
-**
-** DESCRIPTION :
-*/
-std::ostream & operator<<( std::ostream & str, const PathInfo::stat_mode & obj )
-{
- ios::fmtflags state_ii = str.flags();
- char t = '?';
- if ( obj.isFile() )
- t = '-';
- else if ( obj.isDir() )
- t = 'd';
- else if ( obj.isLink() )
- t = 'l';
- else if ( obj.isChr() )
- t = 'c';
- else if ( obj.isBlk() )
- t = 'b';
- else if ( obj.isFifo() )
- t = 'p';
- else if ( obj.isSock() )
- t = 's';
-
- str << t << " " << setfill( '0' ) << setw( 4 ) << oct << obj.perm();
- str.flags( state_ii );
- return str;
-}
-
-/******************************************************************
-**
-**
-** FUNCTION NAME : _Log_Result
-** FUNCTION TYPE : int
-**
-** DESCRIPTION : Helper function to log return values.
-*/
-inline int _Log_Result( const int res, const char * rclass = "errno" )
-{
- if ( res )
- DBG << " FAILED: " << rclass << " " << res;
- DBG << endl;
- return res;
-}
+ /******************************************************************
+ **
+ ** FUNCTION NAME : operator<<
+ ** FUNCTION TYPE : std::ostream &
+ */
+ std::ostream & operator<<( std::ostream & str, const PathInfo & obj )
+ {
+ iostr::IosFmtFlagsSaver autoResoreState( str );
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::mkdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::mkdir( const Pathname & path, unsigned mode )
-{
- DBG << "mkdir " << path << ' ' << stringutil::octstring( mode );
- if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ str << obj.asString() << "{";
+ if ( !obj.isExist() ) {
+ str << "does not exist}";
+ } else {
+ str << obj.asStatMode() << " " << std::dec << obj.owner() << "/" << obj.group();
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::assert_dir()
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::assert_dir( const Pathname & path, unsigned mode )
-{
- string::size_type pos, lastpos = 0;
- string spath = path.asString()+"/";
- int ret = 0;
-
- if(path.empty())
- return ENOENT;
-
- // skip ./
- if(path.relative())
- lastpos=2;
- // skip /
- else
- lastpos=1;
-
-// DBG << "about to create " << spath << endl;
- while((pos = spath.find('/',lastpos)) != string::npos )
- {
- string dir = spath.substr(0,pos);
- ret = ::mkdir(dir.c_str(), mode);
- if(ret == -1)
- {
- // ignore errors about already existing directorys
- if(errno == EEXIST)
- ret=0;
- else
- ret=errno;
- }
-// DBG << "creating directory " << dir << (ret?" failed":" succeeded") << endl;
- lastpos = pos+1;
- }
- return ret;
-}
+ if ( obj.isFile() )
+ str << " size " << obj.size();
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::rmdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::rmdir( const Pathname & path )
-{
- DBG << "rmdir " << path;
- if ( ::rmdir( path.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ str << "}";
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::recursive_rmdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::recursive_rmdir( const Pathname & path )
-{
- DBG << "recursive_rmdir " << path << ' ';
- PathInfo p( path );
-
- if ( !p.isExist() ) {
- return _Log_Result( 0 );
- }
-
- if ( !p.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- const char *const argv[] = {
- "/bin/rm",
- "-rf",
- "--preserve-root",
- "--",
- path.asString().c_str(),
- NULL
- };
-
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
+ return str;
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::clean_dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::clean_dir( const Pathname & path )
-{
- DBG << "clean_dir " << path << ' ';
- PathInfo p( path );
-
- if ( !p.isExist() ) {
- return _Log_Result( 0 );
- }
-
- if ( !p.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- string cmd( stringutil::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
- ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // filesystem utilities
+ //
+ ///////////////////////////////////////////////////////////////////
+
+ /******************************************************************
+ **
+ ** FUNCTION NAME : _Log_Result
+ ** FUNCTION TYPE : int
+ **
+ ** DESCRIPTION : Helper function to log return values.
+ */
+ inline int _Log_Result( const int res, const char * rclass = "errno" )
+ {
+ if ( res )
+ DBG << " FAILED: " << rclass << " " << res;
+ DBG << std::endl;
+ return res;
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy_dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy_dir( const Pathname & srcpath, const Pathname & destpath )
-{
- DBG << "copy_dir " << srcpath << " -> " << destpath << ' ';
-
- PathInfo sp( srcpath );
- if ( !sp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- PathInfo dp( destpath );
- if ( !dp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- PathInfo tp( destpath + srcpath.basename() );
- if ( tp.isExist() ) {
- return _Log_Result( EEXIST );
- }
-
-
- const char *const argv[] = {
- "/bin/cp",
- "-dR",
- "--",
- srcpath.asString().c_str(),
- destpath.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : PathInfo::mkdir
+ // METHOD TYPE : int
+ //
+ int mkdir( const Pathname & path, unsigned mode )
+ {
+ DBG << "mkdir " << path << ' ' << str::octstring( mode );
+ if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::readdir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::readdir( std::list<std::string> & retlist,
- const Pathname & path, bool dots )
-{
- retlist.clear();
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : assert_dir()
+ // METHOD TYPE : int
+ //
+ int assert_dir( const Pathname & path, unsigned mode )
+ {
+ string::size_type pos, lastpos = 0;
+ string spath = path.asString()+"/";
+ int ret = 0;
+
+ if(path.empty())
+ return ENOENT;
+
+ // skip ./
+ if(path.relative())
+ lastpos=2;
+ // skip /
+ else
+ lastpos=1;
+
+ // DBG << "about to create " << spath << endl;
+ while((pos = spath.find('/',lastpos)) != string::npos )
+ {
+ string dir = spath.substr(0,pos);
+ ret = ::mkdir(dir.c_str(), mode);
+ if(ret == -1)
+ {
+ // ignore errors about already existing directorys
+ if(errno == EEXIST)
+ ret=0;
+ else
+ ret=errno;
+ }
+ // DBG << "creating directory " << dir << (ret?" failed":" succeeded") << endl;
+ lastpos = pos+1;
+ }
+ return ret;
+ }
- DBG << "readdir " << path << ' ';
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : rmdir
+ // METHOD TYPE : int
+ //
+ int rmdir( const Pathname & path )
+ {
+ DBG << "rmdir " << path;
+ if ( ::rmdir( path.asString().c_str() ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
- DIR * dir = ::opendir( path.asString().c_str() );
- if ( ! dir ) {
- return _Log_Result( errno );
- }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : recursive_rmdir
+ // METHOD TYPE : int
+ //
+ int recursive_rmdir( const Pathname & path )
+ {
+ DBG << "recursive_rmdir " << path << ' ';
+ PathInfo p( path );
- struct dirent *entry;
- while ( (entry = ::readdir( dir )) != 0 ) {
+ if ( !p.isExist() ) {
+ return _Log_Result( 0 );
+ }
- if ( entry->d_name[0] == '.' ) {
- if ( !dots )
- continue;
- if ( entry->d_name[1] == '\0'
- || ( entry->d_name[1] == '.'
- && entry->d_name[2] == '\0' ) )
- continue;
+ if ( !p.isDir() ) {
+ return _Log_Result( ENOTDIR );
+ }
+
+ const char *const argv[] = {
+ "/bin/rm",
+ "-rf",
+ "--preserve-root",
+ "--",
+ path.asString().c_str(),
+ NULL
+ };
+
+ ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ DBG << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
}
- retlist.push_back( entry->d_name );
- }
- ::closedir( dir );
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : clean_dir
+ // METHOD TYPE : int
+ //
+ int clean_dir( const Pathname & path )
+ {
+ DBG << "clean_dir " << path << ' ';
+ PathInfo p( path );
- return _Log_Result( 0 );
-}
+ if ( !p.isExist() ) {
+ return _Log_Result( 0 );
+ }
+ if ( !p.isDir() ) {
+ return _Log_Result( ENOTDIR );
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::readdir
-// METHOD TYPE : int
-//
-int PathInfo::readdir( dircontent & retlist, const Pathname & path,
- bool dots, Mode statmode )
-{
- retlist.clear();
+ string cmd( str::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
+ ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ DBG << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : copy_dir
+ // METHOD TYPE : int
+ //
+ int copy_dir( const Pathname & srcpath, const Pathname & destpath )
+ {
+ DBG << "copy_dir " << srcpath << " -> " << destpath << ' ';
+
+ PathInfo sp( srcpath );
+ if ( !sp.isDir() ) {
+ return _Log_Result( ENOTDIR );
+ }
+
+ PathInfo dp( destpath );
+ if ( !dp.isDir() ) {
+ return _Log_Result( ENOTDIR );
+ }
+
+ PathInfo tp( destpath + srcpath.basename() );
+ if ( tp.isExist() ) {
+ return _Log_Result( EEXIST );
+ }
- list<string> content;
- int res = readdir( content, path, dots );
- if ( !res ) {
- for ( list<string>::const_iterator it = content.begin(); it != content.end(); ++it ) {
- PathInfo p( path + *it, statmode );
- retlist.push_back( direntry( *it, p.fileType() ) );
+ const char *const argv[] = {
+ "/bin/cp",
+ "-dR",
+ "--",
+ srcpath.asString().c_str(),
+ destpath.asString().c_str(),
+ NULL
+ };
+ ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ DBG << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
}
- }
- return res;
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : readdir
+ // METHOD TYPE : int
+ //
+ int readdir( std::list<std::string> & retlist,
+ const Pathname & path, bool dots )
+ {
+ retlist.clear();
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::unlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::unlink( const Pathname & path )
-{
- DBG << "unlink " << path;
- if ( ::unlink( path.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ DBG << "readdir " << path << ' ';
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::rename
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::rename( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "rename " << oldpath << " -> " << newpath;
- if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ DIR * dir = ::opendir( path.asString().c_str() );
+ if ( ! dir ) {
+ return _Log_Result( errno );
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy( const Pathname & file, const Pathname & dest )
-{
- DBG << "copy " << file << " -> " << dest << ' ';
-
- PathInfo sp( file );
- if ( !sp.isFile() ) {
- return _Log_Result( EINVAL );
- }
-
- PathInfo dp( dest );
- if ( dp.isDir() ) {
- return _Log_Result( EISDIR );
- }
-
- const char *const argv[] = {
- "/bin/cp",
- "--",
- file.asString().c_str(),
- dest.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
+ struct dirent *entry;
+ while ( (entry = ::readdir( dir )) != 0 ) {
+
+ if ( entry->d_name[0] == '.' ) {
+ if ( !dots )
+ continue;
+ if ( entry->d_name[1] == '\0'
+ || ( entry->d_name[1] == '.'
+ && entry->d_name[2] == '\0' ) )
+ continue;
+ }
+ retlist.push_back( entry->d_name );
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::symlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::symlink( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "symlink " << newpath << " -> " << oldpath;
- if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ ::closedir( dir );
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::hardlink
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::hardlink( const Pathname & oldpath, const Pathname & newpath )
-{
- DBG << "hardlink " << newpath << " -> " << oldpath;
- if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ return _Log_Result( 0 );
+ }
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::copy_file2dir
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::copy_file2dir( const Pathname & file, const Pathname & dest )
-{
- DBG << "copy_file2dir " << file << " -> " << dest << ' ';
-
- PathInfo sp( file );
- if ( !sp.isFile() ) {
- return _Log_Result( EINVAL );
- }
-
- PathInfo dp( dest );
- if ( !dp.isDir() ) {
- return _Log_Result( ENOTDIR );
- }
-
- const char *const argv[] = {
- "/bin/cp",
- "--",
- file.asString().c_str(),
- dest.asString().c_str(),
- NULL
- };
- ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
- for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
- DBG << " " << output;
- }
- int ret = prog.close();
- return _Log_Result( ret, "returned" );
-}
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::md5sum
-// METHOD TYPE : std::string
-//
-std::string PathInfo::md5sum( const Pathname & file )
-{
- if ( ! PathInfo( file ).isFile() ) {
- return string();
- }
- ifstream istr( file.asString().c_str() );
- if ( ! istr ) {
- return string();
- }
- return Digest::digest( "MD5", istr );
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : readdir
+ // METHOD TYPE : int
+ //
+ int readdir( std::list<Pathname> & retlist,
+ const Pathname & path, bool dots )
+ {
+ retlist.clear();
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::sha1sum
-// METHOD TYPE : std::string
-//
-std::string PathInfo::sha1sum( const Pathname & file )
-{
- if ( ! PathInfo( file ).isFile() ) {
- return string();
- }
- ifstream istr( file.asString().c_str() );
- if ( ! istr ) {
- return string();
- }
- return Digest::digest( "SHA1", istr );
-}
+ std::list<string> content;
+ int res = readdir( content, path, dots );
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::erase
-// METHOD TYPE : int
-//
-// DESCRIPTION :
-//
-int PathInfo::erase( const Pathname & path )
-{
- int res = 0;
- PathInfo p( path, LSTAT );
- if ( p.isExist() )
- {
- if ( p.isDir() )
- res = PathInfo::recursive_rmdir( path );
- else
- res = PathInfo::unlink( path );
+ if ( !res ) {
+ for ( std::list<string>::const_iterator it = content.begin(); it != content.end(); ++it ) {
+ retlist.push_back( path + *it );
+ }
+ }
+
+ return res;
}
- return res;
-}
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::chmod
-// METHOD TYPE : int
-//
-int PathInfo::chmod( const Pathname & path, mode_t mode )
-{
- DBG << "chmod " << path << ' ' << stringutil::octstring( mode );
- if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
- return _Log_Result( errno );
- }
- return _Log_Result( 0 );
-}
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : readdir
+ // METHOD TYPE : int
+ //
+ int readdir( DirContent & retlist, const Pathname & path,
+ bool dots, PathInfo::Mode statmode )
+ {
+ retlist.clear();
-///////////////////////////////////////////////////////////////////
-//
-//
-// METHOD NAME : PathInfo::zipType
-// METHOD TYPE : PathInfo::ZIP_TYPE
-//
-PathInfo::ZIP_TYPE PathInfo::zipType( const Pathname & file )
-{
- ZIP_TYPE ret = ZT_NONE;
-
- int fd = open( file.asString().c_str(), O_RDONLY );
-
- if ( fd != -1 ) {
- const int magicSize = 3;
- unsigned char magic[magicSize];
- memset( magic, 0, magicSize );
- if ( read( fd, magic, magicSize ) == magicSize ) {
- if ( magic[0] == 0037 && magic[1] == 0213 ) {
- ret = ZT_GZ;
- } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
- ret = ZT_BZ2;
- }
- }
- close( fd );
- }
-
- return ret;
-}
+ std::list<string> content;
+ int res = readdir( content, path, dots );
+
+ if ( !res ) {
+ for ( std::list<string>::const_iterator it = content.begin(); it != content.end(); ++it ) {
+ PathInfo p( path + *it, statmode );
+ retlist.push_back( DirEntry( *it, p.fileType() ) );
+ }
+ }
+
+ return res;
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : unlink
+ // METHOD TYPE : int
+ //
+ int unlink( const Pathname & path )
+ {
+ DBG << "unlink " << path;
+ if ( ::unlink( path.asString().c_str() ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : rename
+ // METHOD TYPE : int
+ //
+ int rename( const Pathname & oldpath, const Pathname & newpath )
+ {
+ DBG << "rename " << oldpath << " -> " << newpath;
+ if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : copy
+ // METHOD TYPE : int
+ //
+ int copy( const Pathname & file, const Pathname & dest )
+ {
+ DBG << "copy " << file << " -> " << dest << ' ';
+
+ PathInfo sp( file );
+ if ( !sp.isFile() ) {
+ return _Log_Result( EINVAL );
+ }
+
+ PathInfo dp( dest );
+ if ( dp.isDir() ) {
+ return _Log_Result( EISDIR );
+ }
+
+ const char *const argv[] = {
+ "/bin/cp",
+ "--",
+ file.asString().c_str(),
+ dest.asString().c_str(),
+ NULL
+ };
+ ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ DBG << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : symlink
+ // METHOD TYPE : int
+ //
+ int symlink( const Pathname & oldpath, const Pathname & newpath )
+ {
+ DBG << "symlink " << newpath << " -> " << oldpath;
+ if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : hardlink
+ // METHOD TYPE : int
+ //
+ int hardlink( const Pathname & oldpath, const Pathname & newpath )
+ {
+ DBG << "hardlink " << newpath << " -> " << oldpath;
+ if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : copy_file2dir
+ // METHOD TYPE : int
+ //
+ int copy_file2dir( const Pathname & file, const Pathname & dest )
+ {
+ DBG << "copy_file2dir " << file << " -> " << dest << ' ';
+
+ PathInfo sp( file );
+ if ( !sp.isFile() ) {
+ return _Log_Result( EINVAL );
+ }
+
+ PathInfo dp( dest );
+ if ( !dp.isDir() ) {
+ return _Log_Result( ENOTDIR );
+ }
+
+ const char *const argv[] = {
+ "/bin/cp",
+ "--",
+ file.asString().c_str(),
+ dest.asString().c_str(),
+ NULL
+ };
+ ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ DBG << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : md5sum
+ // METHOD TYPE : std::string
+ //
+ std::string md5sum( const Pathname & file )
+ {
+ if ( ! PathInfo( file ).isFile() ) {
+ return string();
+ }
+ std::ifstream istr( file.asString().c_str() );
+ if ( ! istr ) {
+ return string();
+ }
+ return Digest::digest( "MD5", istr );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : sha1sum
+ // METHOD TYPE : std::string
+ //
+ std::string sha1sum( const Pathname & file )
+ {
+ if ( ! PathInfo( file ).isFile() ) {
+ return string();
+ }
+ std::ifstream istr( file.asString().c_str() );
+ if ( ! istr ) {
+ return string();
+ }
+ return Digest::digest( "SHA1", istr );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : erase
+ // METHOD TYPE : int
+ //
+ int erase( const Pathname & path )
+ {
+ int res = 0;
+ PathInfo p( path, PathInfo::LSTAT );
+ if ( p.isExist() )
+ {
+ if ( p.isDir() )
+ res = recursive_rmdir( path );
+ else
+ res = unlink( path );
+ }
+ return res;
+ }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : chmod
+ // METHOD TYPE : int
+ //
+ int chmod( const Pathname & path, mode_t mode )
+ {
+ DBG << "chmod " << path << ' ' << str::octstring( mode );
+ if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
+ return _Log_Result( errno );
+ }
+ return _Log_Result( 0 );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : zipType
+ // METHOD TYPE : ZIP_TYPE
+ //
+ ZIP_TYPE zipType( const Pathname & file )
+ {
+ ZIP_TYPE ret = ZT_NONE;
+
+ int fd = open( file.asString().c_str(), O_RDONLY );
+
+ if ( fd != -1 ) {
+ const int magicSize = 3;
+ unsigned char magic[magicSize];
+ memset( magic, 0, magicSize );
+ if ( read( fd, magic, magicSize ) == magicSize ) {
+ if ( magic[0] == 0037 && magic[1] == 0213 ) {
+ ret = ZT_GZ;
+ } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
+ ret = ZT_BZ2;
+ }
+ }
+ close( fd );
+ }
+
+ return ret;
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace filesystem
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
} // namespace zypp
+///////////////////////////////////////////////////////////////////
\---------------------------------------------------------------------*/
/** \file zypp/PathInfo.h
*
- * \todo replace by Blocxx
- *
*/
#ifndef ZYPP_PATHINFO_H
#define ZYPP_PATHINFO_H
#include "zypp/Pathname.h"
-namespace zypp {
-
///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo
-/**
- * @short Wrapper class for ::stat/::lstat and other file/directory related operations.
- **/
-class PathInfo {
-
- friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
-
- public:
-
- enum Mode { STAT, LSTAT };
-
- enum file_type {
- NOT_AVAIL = 0x00, // no typeinfo available
- NOT_EXIST = 0x01, // file does not exist
- T_FILE = 0x02,
- T_DIR = 0x04,
- T_CHARDEV = 0x08,
- T_BLOCKDEV = 0x10,
- T_FIFO = 0x20,
- T_LINK = 0x40,
- T_SOCKET = 0x80
- };
- friend std::ostream & operator<<( std::ostream & str, file_type obj );
-
- /**
- * Wrapper class for mode_t values as derived from ::stat
- **/
- class stat_mode;
-
- /**
- * Simple cache remembering device/inode to detect hardlinks.
- **/
- class devino_cache;
-
- private:
-
- Pathname path_t;
-
- struct stat statbuf_C;
- Mode mode_e;
- int error_i;
-
- public:
-
- 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 Pathname & path() const { return path_t; }
- const std::string & asString() const { return path_t.asString(); }
- Mode mode() const { return mode_e; }
- int error() const { return error_i; }
-
- void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
- void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
-
- bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
- bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
- bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
-
- bool stat() { setMode( STAT ); return operator()(); }
- bool lstat() { setMode( LSTAT ); return operator()(); }
- bool operator()();
-
- public:
-
- bool isExist() const { return !error_i; }
-
- // file type
- file_type fileType() const;
-
- bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
- bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
- bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
- bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
- bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
- bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
- bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
-
- nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
-
- // owner
- uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
- gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
-
- // permission
- bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
- bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
- bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
-
- bool isR() const { return isRUsr(); }
- bool isW() const { return isWUsr(); }
- bool isX() const { return isXUsr(); }
-
- bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
- bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
- bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ /** Types and functions for filesystem operations.
+ * \todo move zypp::filesystem stuff into separate header
+ * \todo Add tmpfile and tmpdir handling.
+ * \todo think about using Exceptions in zypp::filesystem
+ * \todo provide a readdir iterator; at least provide an interface
+ * using an insert_iterator to be independent from std::container.
+ */
+ namespace filesystem
+ { /////////////////////////////////////////////////////////////////
- bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
- bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
- bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
-
- bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
- bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
- bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
-
- mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
- mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
- mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
- mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
-
- bool isPerm ( mode_t m ) const { return (m == perm()); }
- bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
-
- mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
-
- // permission according to current uid/gid (returns [0-7])
- mode_t userMay() const;
-
- bool userMayR() const { return( userMay() & 01 ); }
- bool userMayW() const { return( userMay() & 02 ); }
- bool userMayX() const { return( userMay() & 04 ); }
-
- bool userMayRW() const { return( (userMay() & 03) == 03 ); }
- bool userMayRX() const { return( (userMay() & 05) == 05 ); }
- bool userMayWX() const { return( (userMay() & 06) == 06 ); }
+ ///////////////////////////////////////////////////////////////////
+ /** File type information.
+ * \todo Think about an \ref g_EnumerationClass
+ */
+ enum FileType
+ {
+ FT_NOT_AVAIL = 0x00, // no typeinfo available
+ FT_NOT_EXIST = 0x01, // file does not exist
+ FT_FILE = 0x02,
+ FT_DIR = 0x04,
+ FT_CHARDEV = 0x08,
+ FT_BLOCKDEV = 0x10,
+ FT_FIFO = 0x20,
+ FT_LINK = 0x40,
+ FT_SOCKET = 0x80
+ };
+ ///////////////////////////////////////////////////////////////////
- bool userMayRWX() const { return( userMay() == 07 ); }
+ /** \relates FileType Stram output. */
+ extern std::ostream & operator<<( std::ostream & str, FileType obj );
- // device
- dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
- dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
- ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
+ ///////////////////////////////////////////////////////////////////
- // size
- off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
- unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
- unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : StatMode
+ /**
+ * @short Wrapper class for mode_t values as derived from ::stat
+ **/
+ class StatMode
+ {
+ friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
+
+ public:
+ /** Ctor taking mode_t value from ::stat. */
+ StatMode( const mode_t & mode_r = 0 )
+ : _mode( mode_r )
+ {}
- // time
- time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
- time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
- time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
+ public:
+
+ /** \name Query FileType. */
+ //@{
+ FileType fileType() const;
+
+ bool isFile() const { return S_ISREG( _mode ); }
+ bool isDir () const { return S_ISDIR( _mode ); }
+ bool isLink() const { return S_ISLNK( _mode ); }
+ bool isChr() const { return S_ISCHR( _mode ); }
+ bool isBlk() const { return S_ISBLK( _mode ); }
+ bool isFifo() const { return S_ISFIFO( _mode ); }
+ bool isSock() const { return S_ISSOCK( _mode ); }
+ //@}
+
+ /** \name Query user permissions. */
+ //@{
+ bool isRUsr() const { return (_mode & S_IRUSR); }
+ bool isWUsr() const { return (_mode & S_IWUSR); }
+ bool isXUsr() const { return (_mode & S_IXUSR); }
+
+ /** Short for isRUsr().*/
+ bool isR() const { return isRUsr(); }
+ /** Short for isWUsr().*/
+ bool isW() const { return isWUsr(); }
+ /** Short for isXUsr().*/
+ bool isX() const { return isXUsr(); }
+ //@}
+
+ /** \name Query group permissions. */
+ //@{
+ bool isRGrp() const { return (_mode & S_IRGRP); }
+ bool isWGrp() const { return (_mode & S_IWGRP); }
+ bool isXGrp() const { return (_mode & S_IXGRP); }
+ //@}
+
+ /** \name Query others permissions. */
+ //@{
+ bool isROth() const { return (_mode & S_IROTH); }
+ bool isWOth() const { return (_mode & S_IWOTH); }
+ bool isXOth() const { return (_mode & S_IXOTH); }
+ //@}
+
+ /** \name Query special permissions. */
+ //@{
+ /** Set UID bit. */
+ bool isUid() const { return (_mode & S_ISUID); }
+ /** Set GID bit. */
+ bool isGid() const { return (_mode & S_ISGID); }
+ /** Sticky bit. */
+ bool isVtx() const { return (_mode & S_ISVTX); }
+ //@}
+
+ /** \name Query permission */
+ //@{
+ /** Test for equal permission bits. */
+ bool isPerm ( mode_t m ) const { return (m == perm()); }
+ /** Test for set permission bits. */
+ bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
+ //@}
+
+ /** \name Extract permission bits only. */
+ //@{
+ mode_t uperm() const { return (_mode & S_IRWXU); }
+ mode_t gperm() const { return (_mode & S_IRWXG); }
+ mode_t operm() const { return (_mode & S_IRWXO); }
+ mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
+ //@}
+
+ /** Return the mode_t value. */
+ mode_t st_mode() const { return _mode; }
+
+ private:
+ mode_t _mode;
+ };
+ ///////////////////////////////////////////////////////////////////
- public:
+ /** \relates StatMode Stream output. */
+ extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
///////////////////////////////////////////////////////////////////
- // convenience stuff
+
///////////////////////////////////////////////////////////////////
- // static functions as they may or may not invalidate any stat info
- // stored by a PathiInfo.
+ //
+ // CLASS NAME : DevInoCache
+ /** Simple cache remembering device/inode to detect hardlinks.
+ * \code
+ * DevInoCache trace;
+ * for ( all files ) {
+ * if ( trace.insert( file.device, file.inode ) ) {
+ * // 1st occurance of file
+ * }
+ * // else: hardlink; already counted this device/inode
+ * }
+ * }
+ * \endcode
+ **/
+ class DevInoCache
+ {
+ public:
+ /** Ctor */
+ DevInoCache() {}
+
+ /** Clear cache. */
+ void clear() { _devino.clear(); }
+
+ /** Remember dev/ino.
+ * \Return <code>true</code> if it's inserted the first
+ * time, <code>false</code> if alredy present in cache
+ * (a hardlink to a previously remembered file).
+ **/
+ bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
+ return _devino[dev_r].insert( ino_r ).second;
+ }
+
+ private:
+ std::map<dev_t,std::set<ino_t> > _devino;
+ };
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
- // Directories
+ //
+ // CLASS NAME : PathInfo
+ /** Wrapper class for ::stat/::lstat.
+ *
+ * \note All attribute quieries test for isExist(), and return \c false or
+ * \c 0, if stat was not successful.
+ *
+ * \note For convenience PathInfo is available as zypp::PathInfo too.
+ **/
+ class PathInfo
+ {
+ friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
+
+ public:
+ /** stat() or lstat() */
+ enum Mode { STAT, LSTAT };
+
+ public:
+ /** \name Construct from Pathname.
+ * Default mode is \c STAT.
+ */
+ //@{
+ PathInfo();
+ explicit
+ PathInfo( const Pathname & path, Mode initial = STAT );
+ explicit
+ PathInfo( const std::string & path, Mode initial = STAT );
+ explicit
+ PathInfo( const char * path, Mode initial = STAT );
+ //@}
+
+ /**Dtor */
+ ~PathInfo();
+
+ /** Return current Pathname. */
+ const Pathname & path() const { return path_t; }
+ /** Return current Pathname as String. */
+ const std::string & asString() const { return path_t.asString(); }
+ /** Return current stat Mode. */
+ Mode mode() const { return mode_e; }
+ /** Return error returned from last stat/lstat call. */
+ int error() const { return error_i; }
+
+ /** Set a new Pathname. */
+ void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
+ /** Set a new Mode . */
+ void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
+
+ /** STAT \a path. */
+ bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
+ /** LSTAT \a path. */
+ bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
+ /** Restat \a path using current mode. */
+ bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
+
+ /** STAT current path. */
+ bool stat() { setMode( STAT ); return operator()(); }
+ /** LSTAT current path. */
+ bool lstat() { setMode( LSTAT ); return operator()(); }
+ /** Restat current path using current mode. */
+ bool operator()();
+
+ public:
+
+ /** Return whether valid stat info exists.
+ * That's usg. whether the file exist and you had permission to
+ * stat it.
+ */
+ bool isExist() const { return !error_i; }
+
+ /** \name Query StatMode attibutes.
+ * Combines \ref zypp::PathInfo::isExist and \ref zypp::filesystem::StatMode query.
+ */
+ //@{
+ FileType fileType() const;
+
+ bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
+ bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
+ bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
+ bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
+ bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
+ bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
+ bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
+
+ // permission
+ bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
+ bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
+ bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
+
+ bool isR() const { return isRUsr(); }
+ bool isW() const { return isWUsr(); }
+ bool isX() const { return isXUsr(); }
+
+ bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
+ bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
+ bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
+
+ bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
+ bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
+ bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
+
+ bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
+ bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
+ bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
+
+ bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
+ bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
+
+ mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
+ mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
+ mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
+ mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
+
+ mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
+ //@}
+
+ /** Return st_mode() as filesystem::StatMode. */
+ StatMode asStatMode() const { return st_mode(); }
+
+ nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
+
+ /** \name Owner and group */
+ //@{
+ uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
+ gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
+ //@}
+
+ /** \name Permission according to current uid/gid. */
+ //@{
+ /** Returns current users permission (<tt>[0-7]</tt>)*/
+ mode_t userMay() const;
+
+ bool userMayR() const { return( userMay() & 01 ); }
+ bool userMayW() const { return( userMay() & 02 ); }
+ bool userMayX() const { return( userMay() & 04 ); }
+
+ bool userMayRW() const { return( (userMay() & 03) == 03 ); }
+ bool userMayRX() const { return( (userMay() & 05) == 05 ); }
+ bool userMayWX() const { return( (userMay() & 06) == 06 ); }
+
+ bool userMayRWX() const { return( userMay() == 07 ); }
+ //@}
+
+ /** \name Device and inode info. */
+ //@{
+ dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
+ dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
+ ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
+ //@}
+
+ /** \name Size info. */
+ //@{
+ off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
+ unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
+ unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
+ //@}
+
+ /** \name Time stamps. */
+ //@{
+ time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
+ time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
+ time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
+ //@}
+
+ private:
+ Pathname path_t;
+ struct stat statbuf_C;
+ Mode mode_e;
+ int error_i;
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /** \relates PathInfo Stream output. */
+ extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
+
///////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ /** \name Directory related functions. */
+ //@{
/**
* Like '::mkdir'. Attempt to create a new directory named path. mode
* specifies the permissions to use. It is modified by the process's
*
* @return 0 on success, errno on failure
**/
- static int mkdir( const Pathname & path, unsigned mode = 0755 );
+ int mkdir( const Pathname & path, unsigned mode = 0755 );
/**
* Like 'mkdir -p'. No error if directory exists. Make parent directories
*
* @return 0 on success, errno on failure
**/
- static int assert_dir( const Pathname & path, unsigned mode = 0755 );
+ int assert_dir( const Pathname & path, unsigned mode = 0755 );
/**
* Like '::rmdir'. Delete a directory, which must be empty.
*
* @return 0 on success, errno on failure
**/
- static int rmdir( const Pathname & path );
+ int rmdir( const Pathname & path );
/**
* Like 'rm -r DIR'. Delete a directory, recursively removing its contents.
* @return 0 on success, ENOTDIR if path is not a directory, otherwise the
* commands return value.
**/
- static int recursive_rmdir( const Pathname & path );
+ int recursive_rmdir( const Pathname & path );
/**
* Like 'rm -r DIR/ *'. Delete directory contents, but keep the directory itself.
* @return 0 on success, ENOTDIR if path is not a directory, otherwise the
* commands return value.
**/
- static int clean_dir( const Pathname & path );
+ int clean_dir( const Pathname & path );
/**
* Like 'cp -a srcpath destpath'. Copy directory tree. srcpath/destpath must be
* @return 0 on success, ENOTDIR if srcpath/destpath is not a directory, EEXIST if
* 'basename srcpath' exists in destpath, otherwise the commands return value.
**/
- static int copy_dir( const Pathname & srcpath, const Pathname & destpath );
+ int copy_dir( const Pathname & srcpath, const Pathname & destpath );
/**
* Return content of directory via retlist. If dots is false
* entries starting with '.' are not reported. "." and ".."
* are never reported.
*
+ * Returns just the directory entries as string.
+ *
* @return 0 on success, errno on failure.
+ *
+ * \todo provide some readdirIterator.
**/
- static int readdir( std::list<std::string> & retlist,
- const Pathname & path, bool dots = true );
- struct direntry {
+ int readdir( std::list<std::string> & retlist,
+ const Pathname & path, bool dots = true );
+
+ /**
+ * Return content of directory via retlist. If dots is false
+ * entries starting with '.' are not reported. "." and ".."
+ * are never reported.
+ *
+ * Returns the directory entries prefixed with \a path.
+ *
+ * @return 0 on success, errno on failure.
+ *
+ * \todo provide some readdirIterator.
+ **/
+
+ int readdir( std::list<Pathname> & retlist,
+ const Pathname & path, bool dots = true );
+
+ /** Listentry returned by readdir. */
+ struct DirEntry {
std::string name;
- file_type type;
- direntry( const std::string & name_r = std::string(), file_type type_r = NOT_AVAIL )
- : name( name_r )
- , type( type_r )
+ FileType type;
+ DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
+ : name( name_r )
+ , type( type_r )
{}
};
- typedef std::list<direntry> dircontent;
+ /** Returned by readdir. */
+ typedef std::list<DirEntry> DirContent;
/**
* Return content of directory via retlist. If dots is false
*
* @return 0 on success, errno on failure.
**/
- static int readdir( dircontent & retlist, const Pathname & path,
- bool dots = true, Mode statmode = STAT );
+ int readdir( DirContent & retlist, const Pathname & path,
+ bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
+ //@}
///////////////////////////////////////////////////////////////////
- // Files
- ///////////////////////////////////////////////////////////////////
-
+ /** \name File related functions. */
+ //@{
/**
* Like '::unlink'. Delete a file (symbolic link, socket, fifo or device).
*
* @return 0 on success, errno on failure
**/
- static int unlink( const Pathname & path );
+ int unlink( const Pathname & path );
/**
* Like '::rename'. Renames a file, moving it between directories if required.
*
* @return 0 on success, errno on failure
**/
- static int rename( const Pathname & oldpath, const Pathname & newpath );
+ int rename( const Pathname & oldpath, const Pathname & newpath );
/**
* Like 'cp file dest'. Copy file to destination file.
* @return 0 on success, EINVAL if file is not a file, EISDIR if
* destiantion is a directory, otherwise the commands return value.
**/
- static int copy( const Pathname & file, const Pathname & dest );
+ int copy( const Pathname & file, const Pathname & dest );
/**
* Like '::symlink'. Creates a symbolic link named newpath which contains
*
* @return 0 on success, errno on failure.
**/
- static int symlink( const Pathname & oldpath, const Pathname & newpath );
+ int symlink( const Pathname & oldpath, const Pathname & newpath );
/**
* Like '::link'. Creates a hard link named newpath to an existing file
*
* @return 0 on success, errno on failure.
**/
- static int hardlink( const Pathname & oldpath, const Pathname & newpath );
+ int hardlink( const Pathname & oldpath, const Pathname & newpath );
/**
* Like 'cp file dest'. Copy file to dest dir.
* @return 0 on success, EINVAL if file is not a file, ENOTDIR if dest
* is no directory, otherwise the commands return value.
**/
- static int copy_file2dir( const Pathname & file, const Pathname & dest );
+ int copy_file2dir( const Pathname & file, const Pathname & dest );
+ //@}
+ ///////////////////////////////////////////////////////////////////
+ /** \name Digest computaion.
+ * \todo check cooperation with zypp::Digest
+ */
+ //@{
/**
* Compute a files md5sum.
*
* @return the files md5sum on success, otherwise an empty string..
**/
- static std::string md5sum( const Pathname & file );
+ std::string md5sum( const Pathname & file );
/**
* Compute a files sha1sum.
*
* @return the files sha1sum on success, otherwise an empty string..
**/
- static std::string sha1sum( const Pathname & file );
-
- ///////////////////////////////////////////////////////////////////
- //
- ///////////////////////////////////////////////////////////////////
-
- /**
- * Erase whatever happens to be located at path (file or directory).
- *
- * @return 0 on success.
- **/
- static int erase( const Pathname & path );
+ std::string sha1sum( const Pathname & file );
+ //@}
///////////////////////////////////////////////////////////////////
- // permissions
- ///////////////////////////////////////////////////////////////////
-
+ /** \name Changing permissions. */
+ //@{
/**
* Like '::chmod'. The mode of the file given by path is changed.
*
* @return 0 on success, errno on failure
**/
- static int chmod( const Pathname & path, mode_t mode );
+ int chmod( const Pathname & path, mode_t mode );
+ //@}
///////////////////////////////////////////////////////////////////
- // magic
- ///////////////////////////////////////////////////////////////////
-
+ /** \name Misc. */
+ //@{
/**
* Test whether a file is compressed (gzip/bzip2).
*
**/
enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
- static ZIP_TYPE zipType( const Pathname & file );
-};
+ ZIP_TYPE zipType( const Pathname & file );
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo::stat_mode
-/**
- * @short Wrapper class for mode_t values as derived from ::stat
- **/
-class PathInfo::stat_mode {
- friend std::ostream & operator<<( std::ostream & str, const stat_mode & obj );
- private:
- mode_t _mode;
- public:
- stat_mode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {}
- public:
- // file type
- file_type fileType() const;
-
- bool isFile() const { return S_ISREG( _mode ); }
- bool isDir () const { return S_ISDIR( _mode ); }
- bool isLink() const { return S_ISLNK( _mode ); }
- bool isChr() const { return S_ISCHR( _mode ); }
- bool isBlk() const { return S_ISBLK( _mode ); }
- bool isFifo() const { return S_ISFIFO( _mode ); }
- bool isSock() const { return S_ISSOCK( _mode ); }
-
- // permission
- bool isRUsr() const { return (_mode & S_IRUSR); }
- bool isWUsr() const { return (_mode & S_IWUSR); }
- bool isXUsr() const { return (_mode & S_IXUSR); }
-
- bool isR() const { return isRUsr(); }
- bool isW() const { return isWUsr(); }
- bool isX() const { return isXUsr(); }
-
- bool isRGrp() const { return (_mode & S_IRGRP); }
- bool isWGrp() const { return (_mode & S_IWGRP); }
- bool isXGrp() const { return (_mode & S_IXGRP); }
-
- bool isROth() const { return (_mode & S_IROTH); }
- bool isWOth() const { return (_mode & S_IWOTH); }
- bool isXOth() const { return (_mode & S_IXOTH); }
-
- bool isUid() const { return (_mode & S_ISUID); }
- bool isGid() const { return (_mode & S_ISGID); }
- bool isVtx() const { return (_mode & S_ISVTX); }
-
- mode_t uperm() const { return (_mode & S_IRWXU); }
- mode_t gperm() const { return (_mode & S_IRWXG); }
- mode_t operm() const { return (_mode & S_IRWXO); }
- mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
-
- bool isPerm ( mode_t m ) const { return (m == perm()); }
- bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
-
- mode_t st_mode() const { return _mode; }
-};
-
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-//
-// CLASS NAME : PathInfo::devino_cache
-/**
- * @short Simple cache remembering device/inode to detect hardlinks.
- * <pre>
- * PathInfo::devino_cache trace;
- * for ( all files ) {
- * if ( trace.insert( file.device, file.inode ) ) {
- * // 1st occurance of file
- * }
- * // else: hardlink; already counted this device/inode
- * }
- * }
- * </pre>
- **/
-class PathInfo::devino_cache {
-
- private:
-
- std::map<dev_t,std::set<ino_t> > _devino;
-
- public:
/**
- * Constructor
+ * Erase whatever happens to be located at path (file or directory).
+ *
+ * @return 0 on success.
+ *
+ * \todo check cooperation with zypp::TmpFile and zypp::TmpDir
**/
- devino_cache() {}
+ int erase( const Pathname & path );
+ //@}
- /**
- * Clear cache
- **/
- void clear() { _devino.clear(); }
+ /////////////////////////////////////////////////////////////////
+ } // namespace filesystem
+ ///////////////////////////////////////////////////////////////////
- /**
- * Remember dev/ino. Return <code>true</code> if it's inserted the first
- * time, <code>false</code> if alredy present in cache (a hardlink to a
- * previously remembered file.
- **/
- bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
- return _devino[dev_r].insert( ino_r ).second;
- }
-};
-
-///////////////////////////////////////////////////////////////////
+ /** Dragged into namespace zypp. */
+ using filesystem::PathInfo;
-///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
} // namespace zypp
-
+///////////////////////////////////////////////////////////////////
#endif // ZYPP_PATHINFO_H
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
-
///////////////////////////////////////////////////////////////////
- namespace
+ namespace filesystem
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
- //
- // CLASS NAME : DirStack
- //
- /** silly helper to build Pathnames.
- */
- class DirStack {
+ namespace
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : DirStack
+ //
+ /** silly helper to build Pathnames.
+ */
+ class DirStack {
+
+ struct Dir {
+
+ Dir * up;
+ Dir * dn;
+ string name;
+
+ Dir( const string & n = "" ) {
+ name = n;
+ up = dn = 0;
+ }
- struct Dir {
+ ~Dir() {
+ if ( up )
+ up->dn = dn;
+ if ( dn )
+ dn->up = up;
+ }
+ };
- Dir * up;
- Dir * dn;
- string name;
+ Dir * top;
+ Dir * bot;
- Dir( const string & n = "" ) {
- name = n;
- up = dn = 0;
+ void Pop() {
+ if ( !top )
+ return;
+ top = top->dn;
+ if ( top )
+ delete top->up;
+ else {
+ delete bot;
+ bot = 0;
+ }
}
- ~Dir() {
- if ( up )
- up->dn = dn;
- if ( dn )
- dn->up = up;
- }
- };
+ public:
- Dir * top;
- Dir * bot;
-
- void Pop() {
- if ( !top )
- return;
- top = top->dn;
- if ( top )
- delete top->up;
- else {
- delete bot;
- bot = 0;
+ DirStack() { top = bot = 0; }
+ ~DirStack() {
+ while ( bot )
+ Pop();
}
- }
-
- public:
- DirStack() { top = bot = 0; }
- ~DirStack() {
- while ( bot )
- Pop();
- }
-
- void Push( const string & n ) {
- if ( n.empty() || n == "." ) { // '.' or '/' only for bot
- if ( bot )
- return;
- } else if ( n == ".." && top ) {
- if ( top->name == "" ) // "/.." ==> "/"
- return;
+ void Push( const string & n ) {
+ if ( n.empty() || n == "." ) { // '.' or '/' only for bot
+ if ( bot )
+ return;
+ } else if ( n == ".." && top ) {
+ if ( top->name == "" ) // "/.." ==> "/"
+ return;
+
+ if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
+ Pop();
+ return;
+ }
+ // "../.." "./.." stays
+ }
- if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
- Pop();
- return;
+ Dir * d = new Dir( n );
+ if ( !top )
+ top = bot = d;
+ else {
+ top->up = d;
+ d->dn = top;
+ d->up = 0;
+ top = d;
}
- // "../.." "./.." stays
}
- Dir * d = new Dir( n );
- if ( !top )
- top = bot = d;
- else {
- top->up = d;
- d->dn = top;
- d->up = 0;
- top = d;
+ string str() {
+ if ( !bot )
+ return "";
+ string ret;
+ for ( Dir * d = bot; d; d = d->up ) {
+ if ( d != bot )
+ ret += "/";
+ ret += d->name;
+ }
+ if ( ret.empty() )
+ return "/";
+ return ret;
}
+ };
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace
+ ///////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Pathname::_assign
+ // METHOD TYPE : void
+ //
+ void Pathname::_assign( const string & name_tv )
+ {
+ prfx_i = 0;
+ name_t = name_tv;
+
+ if ( name_t.empty() )
+ return;
+
+ string Tprfx;
+ DirStack Stack_Ci;
+
+ char * Buf_aci = new char[name_tv.length() + 1];
+ char * W_pci = Buf_aci;
+ const char * R_pci = name_tv.c_str();
+
+ // check for prefix
+ if ( name_t.length() >= 2
+ && name_t[1] == ':'
+ && ( 'a' <= name_t[0] && name_t[0] <= 'z'
+ || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
+ Tprfx = name_t.substr( 0, 2 );
+ prfx_i = 2;
+ R_pci += 2;
}
- string str() {
- if ( !bot )
- return "";
- string ret;
- for ( Dir * d = bot; d; d = d->up ) {
- if ( d != bot )
- ret += "/";
- ret += d->name;
- }
- if ( ret.empty() )
- return "/";
- return ret;
+ // rel or abs path
+ if ( *R_pci == '/' || *R_pci == '\\' ) {
+ Stack_Ci.Push( "" );
+ ++R_pci;
+ } else {
+ Stack_Ci.Push( "." );
}
- };
- /////////////////////////////////////////////////////////////////
- } // namespace
- ///////////////////////////////////////////////////////////////////
+ do {
+ switch ( *R_pci ) {
+ case '/':
+ case '\\':
+ case '\0':
+ if ( W_pci != Buf_aci ) {
+ *W_pci = '\0';
+ W_pci = Buf_aci;
+ Stack_Ci.Push( Buf_aci );
+ }
+ break;
- ///////////////////////////////////////////////////////////////////
- //
- // METHOD NAME : Pathname::_assign
- // METHOD TYPE : void
- //
- void Pathname::_assign( const string & name_tv )
- {
- prfx_i = 0;
- name_t = name_tv;
-
- if ( name_t.empty() )
- return;
-
- string Tprfx;
- DirStack Stack_Ci;
-
- char * Buf_aci = new char[name_tv.length() + 1];
- char * W_pci = Buf_aci;
- const char * R_pci = name_tv.c_str();
-
- // check for prefix
- if ( name_t.length() >= 2
- && name_t[1] == ':'
- && ( 'a' <= name_t[0] && name_t[0] <= 'z'
- || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
- Tprfx = name_t.substr( 0, 2 );
- prfx_i = 2;
- R_pci += 2;
- }
+ default:
+ *W_pci++ = *R_pci;
+ break;
+ }
+ } while( *R_pci++ );
- // rel or abs path
- if ( *R_pci == '/' || *R_pci == '\\' ) {
- Stack_Ci.Push( "" );
- ++R_pci;
- } else {
- Stack_Ci.Push( "." );
+ delete Buf_aci;
+ name_t = Tprfx + Stack_Ci.str();
}
- do {
- switch ( *R_pci ) {
- case '/':
- case '\\':
- case '\0':
- if ( W_pci != Buf_aci ) {
- *W_pci = '\0';
- W_pci = Buf_aci;
- Stack_Ci.Push( Buf_aci );
- }
- break;
-
- default:
- *W_pci++ = *R_pci;
- break;
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Pathname::dirname
+ // METHOD TYPE : Pathname
+ //
+ Pathname Pathname::dirname( const Pathname & name_tv )
+ {
+ if ( name_tv.empty() )
+ return "";
+
+ Pathname ret_t( name_tv );
+ string::size_type idx = ret_t.name_t.find_last_of( '/' );
+
+ if ( idx == string::npos ) {
+ ret_t.name_t.erase( ret_t.prfx_i );
+ ret_t.name_t += ".";
+ } else if ( idx == ret_t.prfx_i ) {
+ ret_t.name_t.erase( ret_t.prfx_i );
+ ret_t.name_t += "/";
+ } else {
+ ret_t.name_t.erase( idx );
}
- } while( *R_pci++ );
-
- delete Buf_aci;
- name_t = Tprfx + Stack_Ci.str();
- }
- ///////////////////////////////////////////////////////////////////
- //
- // METHOD NAME : Pathname::dirname
- // METHOD TYPE : Pathname
- //
- Pathname Pathname::dirname( const Pathname & name_tv )
- {
- if ( name_tv.empty() )
- return "";
-
- Pathname ret_t( name_tv );
- string::size_type idx = ret_t.name_t.find_last_of( '/' );
-
- if ( idx == string::npos ) {
- ret_t.name_t.erase( ret_t.prfx_i );
- ret_t.name_t += ".";
- } else if ( idx == ret_t.prfx_i ) {
- ret_t.name_t.erase( ret_t.prfx_i );
- ret_t.name_t += "/";
- } else {
- ret_t.name_t.erase( idx );
+ return ret_t;
}
- return ret_t;
- }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Pathname::basename
+ // METHOD TYPE : string
+ //
+ string Pathname::basename( const Pathname & name_tv )
+ {
+ if ( name_tv.empty() )
+ return "";
+
+ string ret_t( name_tv.asString() );
+ ret_t.erase( 0, name_tv.prfx_i );
+ string::size_type idx = ret_t.find_last_of( '/' );
+ if ( idx != string::npos ) {
+ ret_t.erase( 0, idx+1 );
+ }
- ///////////////////////////////////////////////////////////////////
- //
- // METHOD NAME : Pathname::basename
- // METHOD TYPE : string
- //
- string Pathname::basename( const Pathname & name_tv )
- {
- if ( name_tv.empty() )
- return "";
-
- string ret_t( name_tv.asString() );
- ret_t.erase( 0, name_tv.prfx_i );
- string::size_type idx = ret_t.find_last_of( '/' );
- if ( idx != string::npos ) {
- ret_t.erase( 0, idx+1 );
+ return ret_t;
}
- return ret_t;
- }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Pathname::cat
+ // METHOD TYPE : Pathname
+ //
+ Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
+ {
+ if ( add_tv.empty() )
+ return name_tv;
+ if ( name_tv.empty() )
+ return add_tv;
- ///////////////////////////////////////////////////////////////////
- //
- // METHOD NAME : Pathname::cat
- // METHOD TYPE : Pathname
- //
- Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
- {
- if ( add_tv.empty() )
- return name_tv;
- if ( name_tv.empty() )
- return add_tv;
-
- string ret_ti( add_tv.asString() );
- ret_ti.replace( 0, add_tv.prfx_i, "/" );
-
- return name_tv.asString() + ret_ti;
- }
+ string ret_ti( add_tv.asString() );
+ ret_ti.replace( 0, add_tv.prfx_i, "/" );
- ///////////////////////////////////////////////////////////////////
- //
- // METHOD NAME : Pathname::Extend
- // METHOD TYPE : Pathname
- //
- Pathname Pathname::extend( const Pathname & l, const string & r )
- {
- return l.asString() + r;
- }
+ return name_tv.asString() + ret_ti;
+ }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Pathname::Extend
+ // METHOD TYPE : Pathname
+ //
+ Pathname Pathname::extend( const Pathname & l, const string & r )
+ {
+ return l.asString() + r;
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace filesystem
+ ///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
-
///////////////////////////////////////////////////////////////////
- //
- // CLASS NAME : Pathname
- //
- /** Pathname.
- *
- * Always stores normalized paths (no inner '.' or '..' components
- * and no consecutive '/'es). Concatenation automatically adds
- * the path separator '/'.
- *
- * \todo Add support for handling extensions incl. stripping
- * extensions from basename (basename("/path/foo.baa", ".baa") ==> "foo")
- * \todo Review. Maybe use COW pimpl, ckeck storage.
- */
- class Pathname
- {
- public:
- /** Default ctor: an empty path. */
- Pathname()
- : prfx_i( 0 )
- {}
-
- /** Ctor from string. */
- Pathname( const std::string & name_tv )
- { _assign( name_tv ); }
-
- /** Ctor from char*. */
- Pathname( const char * name_tv )
- { _assign( name_tv ? name_tv : "" ); }
-
- /** Assign */
- Pathname & operator=( const Pathname & path_tv )
- {
- prfx_i = path_tv.prfx_i;
- name_t = path_tv.name_t;
- return *this;
- }
-
- /** Concatenate and assing. \see cat */
- Pathname & operator+=( const Pathname & path_tv )
- { return( *this = cat( *this, path_tv ) ); }
-
- /** String representation. */
- const std::string & asString() const
- { return name_t; }
-
- /** Test for an empty path. */
- bool empty() const { return name_t.empty(); }
- /** Test for an absolute path. */
- bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
- /** Test for a relative path. */
- bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
-
- /** Return all but the last component od this path. */
- Pathname dirname() const { return dirname( *this ); }
- static Pathname dirname( const Pathname & name_tv );
-
- /** Return the last component of this path. */
- std::string basename() const { return basename( *this ); }
- static std::string basename( const Pathname & name_tv );
-
- /** Return this path, adding a leading '/' if relative. */
- Pathname absolutename() const { return absolutename( *this ); }
- static Pathname absolutename( const Pathname & name_tv )
- { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
-
- /** Return this path, removing a leading '/' if absolute.*/
- Pathname relativename() const { return relativename( *this ); }
- static Pathname relativename( const Pathname & name_tv )
- { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
-
- /** Concatenation of pathnames.
- * \code
- * "foo" + "baa" ==> "foo/baa"
- * "foo/" + "baa" ==> "foo/baa"
- * "foo" + "/baa" ==> "foo/baa"
- * "foo/" + "/baa" ==> "foo/baa"
- * \endcode
- */
- Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
- static Pathname cat( const Pathname & l, const Pathname & r );
-
- /** Append string \a r to the last component of the path.
- * \code
- * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
- * \endcode
+ namespace filesystem
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : Pathname
+ //
+ /** Pathname.
+ *
+ * \note For convenience Pathname is available as zypp::Pathname too.
+ *
+ * Always stores normalized paths (no inner '.' or '..' components
+ * and no consecutive '/'es). Concatenation automatically adds
+ * the path separator '/'.
+ *
+ * \todo Add support for handling extensions incl. stripping
+ * extensions from basename (basename("/path/foo.baa", ".baa") ==> "foo")
+ * \todo Review. Maybe use COW pimpl, ckeck storage.
+ * \todo \b EXPLICIT ctors.
*/
- Pathname extend( const std::string & r ) const { return extend( *this, r ); }
- static Pathname extend( const Pathname & l, const std::string & r );
-
- private:
- std::string::size_type prfx_i;
- std::string name_t;
-
- void _assign( const std::string & name_tv );
- };
- ///////////////////////////////////////////////////////////////////
-
- /** \relates Pathname */
- inline bool operator==( const Pathname & l, const Pathname & r )
- { return l.asString() == r.asString(); }
-
- /** \relates Pathname */
- inline bool operator!=( const Pathname & l, const Pathname & r )
- { return l.asString() != r.asString(); }
-
- /** \relates Pathname Concatenate two Pathname. */
- inline Pathname operator+( const Pathname & l, const Pathname & r )
- { return Pathname::cat( l, r ); }
-
+ class Pathname
+ {
+ public:
+ /** Default ctor: an empty path. */
+ Pathname()
+ : prfx_i( 0 )
+ {}
+
+ /** Ctor from string. */
+ Pathname( const std::string & name_tv )
+ { _assign( name_tv ); }
+
+ /** Ctor from char*. */
+ Pathname( const char * name_tv )
+ { _assign( name_tv ? name_tv : "" ); }
+
+ /** Assign */
+ Pathname & operator=( const Pathname & path_tv )
+ {
+ prfx_i = path_tv.prfx_i;
+ name_t = path_tv.name_t;
+ return *this;
+ }
+
+ /** Concatenate and assing. \see cat */
+ Pathname & operator+=( const Pathname & path_tv )
+ { return( *this = cat( *this, path_tv ) ); }
+
+ /** String representation. */
+ const std::string & asString() const
+ { return name_t; }
+
+ /** Test for an empty path. */
+ bool empty() const { return name_t.empty(); }
+ /** Test for an absolute path. */
+ bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
+ /** Test for a relative path. */
+ bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
+
+ /** Return all but the last component od this path. */
+ Pathname dirname() const { return dirname( *this ); }
+ static Pathname dirname( const Pathname & name_tv );
+
+ /** Return the last component of this path. */
+ std::string basename() const { return basename( *this ); }
+ static std::string basename( const Pathname & name_tv );
+
+ /** Return this path, adding a leading '/' if relative. */
+ Pathname absolutename() const { return absolutename( *this ); }
+ static Pathname absolutename( const Pathname & name_tv )
+ { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
+
+ /** Return this path, removing a leading '/' if absolute.*/
+ Pathname relativename() const { return relativename( *this ); }
+ static Pathname relativename( const Pathname & name_tv )
+ { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
+
+ /** Concatenation of pathnames.
+ * \code
+ * "foo" + "baa" ==> "foo/baa"
+ * "foo/" + "baa" ==> "foo/baa"
+ * "foo" + "/baa" ==> "foo/baa"
+ * "foo/" + "/baa" ==> "foo/baa"
+ * \endcode
+ */
+ Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
+ static Pathname cat( const Pathname & l, const Pathname & r );
+
+ /** Append string \a r to the last component of the path.
+ * \code
+ * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
+ * \endcode
+ */
+ Pathname extend( const std::string & r ) const { return extend( *this, r ); }
+ static Pathname extend( const Pathname & l, const std::string & r );
+
+ private:
+ std::string::size_type prfx_i;
+ std::string name_t;
+
+ void _assign( const std::string & name_tv );
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /** \relates Pathname */
+ inline bool operator==( const Pathname & l, const Pathname & r )
+ { return l.asString() == r.asString(); }
+
+ /** \relates Pathname */
+ inline bool operator!=( const Pathname & l, const Pathname & r )
+ { return l.asString() != r.asString(); }
+
+ /** \relates Pathname Concatenate two Pathname. */
+ inline Pathname operator+( const Pathname & l, const Pathname & r )
+ { return Pathname::cat( l, r ); }
+
+ ///////////////////////////////////////////////////////////////////
+
+ /** \relates Pathname Stream output */
+ inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
+ { return str << obj.asString(); }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace filesystem
///////////////////////////////////////////////////////////////////
- /** \relates Pathname Stream output */
- inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
- { return str << obj.asString(); }
+ /** Dragged into namespace zypp. */
+ using filesystem::Pathname;
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
- /** Required by intrusive_ptr to add a reference. */
- inline void intrusive_ptr_add_ref( const Resolvable * ptr_r )
- { base::ReferenceCounted::add_ref( ptr_r ); }
-
- /** Required by intrusive_ptr to release a reference. */
- inline void intrusive_ptr_release( const Resolvable * ptr_r )
- { base::ReferenceCounted::release( ptr_r ); }
-
/** \relates Resolvable Stream output via Resolvable::dumpOn */
inline std::ostream & operator<<( std::ostream & str, const Resolvable & obj )
{ return obj.dumpOn( str ); }
#ifndef ZYPP_BASE_EXCEPTION_H
#define ZYPP_BASE_EXCEPTION_H
+#include <cerrno>
#include <iosfwd>
#include <stdexcept>
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/Fd1.cc
+ *
+*/
+extern "C"
+{
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+}
+
+#include "zypp/base/Exception.h"
+#include "zypp/base/Fd.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace base
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Fd::Fd
+ // METHOD TYPE : Ctor
+ //
+ Fd::Fd( const Pathname & file_r, int open_flags )
+ : m_fd( -1 )
+ {
+ m_fd = open( file_r.asString().c_str(), open_flags );
+ if ( m_fd == -1 )
+ ZYPP_THROW_ERRNO_MSG( Exception, std::string("open ")+file_r.asString() );
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : Fd::close
+ // METHOD TYPE : void
+ //
+ void Fd::close()
+ {
+ if ( m_fd != -1 )
+ {
+ ::close( m_fd );
+ m_fd = -1;
+ }
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace base
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/Fd.h
+ *
+*/
+#ifndef ZYPP_BASE_FD_H
+#define ZYPP_BASE_FD_H
+
+#include "zypp/Pathname.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace base
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : Fd
+ //
+ /** Assert \c close called on open filedescriptor.
+ * \code
+ * ...
+ * scoped_ptr<Fd> fd; // calls close when going out of scope
+ * try {
+ * fd.reset( new Fd( "/some/file" ) );
+ * } catch ( ... ) {
+ * // open failed.
+ * }
+ * read( fd->fd(), ... ),
+ * \endcode
+ *
+ * \ingroup g_RAII
+ * \todo It's dumb. Openflags and more related functions (read/write..)
+ * could be added.
+ */
+ class Fd
+ {
+ public:
+ /** Ctor opens file.
+ * \throw EXCEPTION If open fails.
+ */
+ Fd( const Pathname & file_r, int open_flags );
+
+ /** Dtor closes file. */
+ ~Fd()
+ { close(); }
+
+ /** Explicitly close the file. */
+ void close();
+
+ /** Test for valid filedescriptor. */
+ bool isOpen() const
+ { return m_fd != -1; }
+
+ /** Return the filedescriptor. */
+ int fd() const
+ { return m_fd; }
+
+ private:
+ /** The filedescriptor. */
+ int m_fd;
+ /** No copy. */
+ Fd( const Fd & );
+ /** No assign. */
+ Fd & operator=( const Fd & );
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace base
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_FD_H
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/IOStream.cc
+ *
+*/
+#include <iostream>
+//#include "zypp/base/Logger.h"
+
+#include "zypp/base/IOStream.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace iostr
+ { /////////////////////////////////////////////////////////////////
+
+ /******************************************************************
+ **
+ ** FUNCTION NAME : getline
+ ** FUNCTION TYPE : std::string
+ */
+ std::string getline( std::istream & str )
+ {
+ static const unsigned tmpBuffLen = 1024;
+ static char tmpBuff[tmpBuffLen];
+ std::string ret;
+ do {
+ str.clear();
+ str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
+ ret += tmpBuff;
+ } while( str.rdstate() == std::ios::failbit );
+
+ return ret;
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace iostr
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/IOStream.h
+ *
+*/
+#ifndef ZYPP_BASE_IOSTREAM_H
+#define ZYPP_BASE_IOSTREAM_H
+
+#include <iosfwd>
+#include <boost/io/ios_state.hpp>
+
+#include "zypp/base/PtrTypes.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ /** Iostream related utilities.
+ */
+ namespace iostr
+ { /////////////////////////////////////////////////////////////////
+
+ /** Save and restore streams \c width, \c precision
+ * and \c fmtflags.
+ */
+ typedef boost::io::ios_base_all_saver IosFmtFlagsSaver;
+
+
+ /** Read one line from stream.
+ *
+ * Reads everything up to the next newline or EOF. newline
+ * is read but not returned.
+ *
+ * \see \ref forEachLine
+ */
+ std::string getline( std::istream & str );
+
+
+ /** Simple lineparser: Call functor \a consume_r for each line.
+ *
+ * \param str_r The istream to read from.
+ * \param consume_r A reference to a function or functor.
+ * \code
+ * void consume( const std::string & )
+ * { ... }
+ *
+ * struct Consume : public std::unary_function<const std::string &, void>
+ * {
+ * void operator()( const std::string & line_r )
+ * { ... }
+ * };
+ * \endcode
+ *
+ * \return A reference to \a consume_r.
+ */
+ template<class _Function>
+ _Function & forEachLine( std::istream & str_r, _Function & consume_r )
+ {
+ while ( str_r )
+ {
+ std::string l = getline( str_r );
+ if ( ! (str_r.fail() || str_r.bad()) )
+ {
+ // l contains valid data to be consumed.
+ consume_r( l );
+ }
+ }
+ return consume_r;
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace iostr
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_IOSTREAM_H
include_HEADERS = \
Debug.h \
+ Fd.h \
KindOf.h \
Logger.h \
Exception.h \
+ IOStream.h \
NonCopyable.h \
PtrTypes.h \
ReferenceCounted.h \
StringVal.h \
\
\
- ExternalDataSource.h \
- stringutil.h
+ ExternalDataSource.h
noinst_LTLIBRARIES = lib@PACKAGE@_base.la
lib@PACKAGE@_base_la_SOURCES = \
Logger.cc \
Exception.cc \
+ Fd.cc \
+ IOStream.cc \
String.cc \
StringVal.cc \
\
\
- ExternalDataSource.cc \
- stringutil.cc
+ ExternalDataSource.cc
lib@PACKAGE@_base_la_LIBADD = -lboost_regex
};
///////////////////////////////////////////////////////////////////
+ inline void intrusive_ptr_add_ref( const ReferenceCounted * ptr_r )
+ { ReferenceCounted::add_ref( ptr_r ); }
+
+ inline void intrusive_ptr_release( const ReferenceCounted * ptr_r )
+ { ReferenceCounted::release( ptr_r ); }
+
/////////////////////////////////////////////////////////////////
} // namespace base
///////////////////////////////////////////////////////////////////
*/
std::string form( const char * format, ... )
{
- struct SafeBuf
- {
- char * _buf;
- SafeBuf() : _buf( 0 ) {}
- ~SafeBuf() { if ( _buf ) free( _buf ); }
- std::string asString() const
- { return _buf ? std::string(_buf) : std::string(); }
- };
SafeBuf safe;
va_list ap;
/******************************************************************
**
- ** FUNCTION NAME : toLower
+ ** FUNCTION NAME : strerror
** FUNCTION TYPE : std::string
*/
std::string strerror( int errno_r )
return ret;
}
+ /******************************************************************
+ **
+ ** FUNCTION NAME : trim
+ ** FUNCTION TYPE : std::string
+ */
+ std::string trim( const std::string & s, const Trim trim_r )
+ {
+ if ( s.empty() || trim_r == NO_TRIM )
+ return s;
+
+ std::string ret( s );
+
+ if ( trim_r && L_TRIM )
+ {
+ std::string::size_type p = ret.find_first_not_of( " \t\n" );
+ if ( p == std::string::npos )
+ return std::string();
+
+ ret = ret.substr( p );
+ }
+
+ if ( trim_r && R_TRIM )
+ {
+ std::string::size_type p = ret.find_last_not_of( " \t\n" );
+ if ( p == std::string::npos )
+ return std::string();
+
+ ret = ret.substr( 0, p+1 );
+ }
+
+ return ret;
+ }
/////////////////////////////////////////////////////////////////
} // namespace str
namespace str
{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
/** Printf style construction of std::string. */
std::string form( const char * format, ... )
__attribute__ ((format (printf, 1, 2)));
+ ///////////////////////////////////////////////////////////////////
/** Return string describing the \a error_r code.
* Like ::strerror, but the numerical value is included in
* the string as well.
*/
std::string strerror( int errno_r );
+ ///////////////////////////////////////////////////////////////////
+ /** Assert \c free called for allocated <tt>char *</tt>.
+ * \code
+ * ...
+ * SafeBuf safe;
+ * vasprintf( &safe._buf, format, ap );
+ * return safe.asString();
+ * \endcode
+ *
+ * \ingroup g_RAII
+ */
+ struct SafeBuf
+ {
+ char * _buf;
+ SafeBuf() : _buf( 0 ) {}
+ ~SafeBuf() { if ( _buf ) free( _buf ); }
+ std::string asString() const
+ { return _buf ? std::string(_buf) : std::string(); }
+ };
+
+ ///////////////////////////////////////////////////////////////////
/** \defgroup ZYPP_STR_REGEX Regular expressions
*
* Namespace zypp::str regular expressions \b using the
using boost::wsmatch;
//@}
+ ///////////////////////////////////////////////////////////////////
+ /** \name String representation of number.
+ *
+ * Optional second argument sets the minimal string width (' ' padded).
+ * Negative values will cause the number to be left adjusted within the string.
+ *
+ * Default width is 0.
+ * \code
+ * numstring(42) -> "42"
+ * numstring(42, 4) -> " 42"
+ * numstring(42,-4) -> "42 "
+ * \endcode
+ **/
+ //@{
+ inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
+ inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
+ inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
+ inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
+ inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
+ inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
+ inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
+ inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
+ inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
+ inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
+ //@}
+
+ ///////////////////////////////////////////////////////////////////
+ /** \name String representation of number as hex value with leading '0x'.
+ * Optional second argument sets the minimal
+ * string width (0 padded). Negative values will cause the number to be left adjusted
+ * within the string. Default width is 10 (4 for char).
+ * <PRE>
+ * hexstring(42) -> "0x0000002a"
+ * hexstring(42, 4) -> "0x2a"
+ * hexstring(42,-4) -> "0x2a"
+ * </PRE>
+ **/
+ //@{
+ inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
+ inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
+ inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
+ inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
+ inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
+ inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
+ inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
+ inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
+ inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
+ inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
+ //@}
+
+ ///////////////////////////////////////////////////////////////////
+ /** \name String representation of number as octal value with leading '0'.
+ * Optional second argument sets the minimal
+ * string width (0 padded). Negative values will cause the number to be left adjusted
+ * within the string. Default width is 5 (4 for char).
+ * <PRE>
+ * octstring(42) -> "00052"
+ * octstring(42, 4) -> "0052"
+ * octstring(42,-4) -> "052 "
+ * </PRE>
+ **/
+ //@{
+ inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
+ inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
+ inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
+ inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
+ inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
+ inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
+ inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
+ inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
+ inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
+ inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
+ //@}
+
+ ///////////////////////////////////////////////////////////////////
+ /** Parsing numbers from string.
+ */
+ //@{
+ /** String to integer type determined by template arg.
+ * \note Only specializations are defined.
+ * \code
+ * time_t t = strtonum<time_t>( "42" );
+ * \endcode
+ */
+ template<typename _It>
+ inline _It strtonum( const std::string & str );
+
+ template<>
+ inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
+
+ template<>
+ inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
+ template<>
+ inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
+
+ /** String to integer type detemined 2nd function arg \a i.
+ * \code
+ * time_t t; strtonum( "42", t );
+ * \endcode
+ */
+ template<typename _It>
+ inline _It strtonum( const std::string & str, _It & i )
+ { return i = strtonum<_It>( str ); }
+ //@}
+
+ ///////////////////////////////////////////////////////////////////
+ /** \name Case conversion. */
+ //@{
/** Return lowercase version of \a s
* \todo improve
*/
* \todo improve
*/
std::string toUpper( const std::string & s );
+ //@}
+
+ ///////////////////////////////////////////////////////////////////
+ /** \name Trimming whitepace.
+ * \todo optimize l/r trim.
+ */
+ //@{
+ /** To define how to trim. */
+ enum Trim {
+ NO_TRIM = 0x00,
+ L_TRIM = 0x01,
+ R_TRIM = 0x02,
+ TRIM = (L_TRIM|R_TRIM)
+ };
+
+ std::string trim( const std::string & s, const Trim trim_r = TRIM );
+
+ inline std::string ltrim( const std::string & s )
+ { return trim( s, L_TRIM ); }
+
+ inline std::string rtrim( const std::string & s )
+ { return trim( s, R_TRIM ); }
+ //@}
/////////////////////////////////////////////////////////////////
} // namespace str
+++ /dev/null
-/*---------------------------------------------------------------------\
-| ____ _ __ __ ___ |
-| |__ / \ / / . \ . \ |
-| / / \ V /| _/ _/ |
-| / /__ | | | | | | |
-| /_____||_| |_| |_| |
-| |
-\---------------------------------------------------------------------*/
-/** \file zypp/base/stringutil.cc
- *
- * \todo replace by Blocxx
- *
-*/
-
-#include <iostream>
-#include <fstream>
-
-#include "zypp/base/stringutil.h"
-
-using namespace std;
-///////////////////////////////////////////////////////////////////
-namespace zypp {
- namespace base {
- namespace stringutil {
-///////////////////////////////////////////////////////////////////
-
- const unsigned tmpBuffLen = 1024;
- char tmpBuff[tmpBuffLen];
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : getline
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- static inline std::string _getline( std::istream & str, const Trim trim_r )
- {
- string ret;
- do {
- str.clear();
- str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
- ret += tmpBuff;
- } while( str.rdstate() == ios::failbit );
-
- return trim( ret, trim_r );
- }
-
- std::string getline( std::istream & str, const Trim trim_r )
- {
- return _getline(str, trim_r);
- }
-
- std::string getline( std::istream & str, bool trim )
- {
- return _getline(str, trim?TRIM:NO_TRIM);
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : split
- ** FUNCTION TYPE : unsigned
- **
- ** DESCRIPTION :
- */
- unsigned split( const std::string line_tv,
- std::vector<std::string> & words_Vtr,
- const std::string & sep_tv,
- const bool singlesep_bv )
- {
- words_Vtr.clear();
- if ( line_tv.empty() )
- return words_Vtr.size();
-
- struct sepctrl {
- const string & sep_t;
- sepctrl( const string & sep_tv ) : sep_t( sep_tv ) {}
- // Note that '\0' ist neither Sep nor NonSep
- inline bool isSep ( const char c ) const { return( sep_t.find( c ) != string::npos ); }
- inline bool isNonSep ( const char c ) const { return( c && !isSep(c) ); }
- inline void skipSep ( const char *& p ) const { while ( isSep( *p ) ) ++p; }
- inline void skipNonSep( const char *& p ) const { while ( isNonSep( *p ) ) ++p; }
- };
-
- sepctrl sep_Ci( sep_tv );
- const char * s_pci = line_tv.c_str();
- const char * c_pci = s_pci;
-
- // Start with c_pci at the beginning of the 1st field to add.
- // In singlesep the beginning might be equal to the next sep,
- // which makes an empty field before the sep.
- if ( !singlesep_bv && sep_Ci.isSep( *c_pci ) ) {
- sep_Ci.skipSep( c_pci );
- }
-
- for ( s_pci = c_pci; *s_pci; s_pci = c_pci ) {
- sep_Ci.skipNonSep( c_pci );
- words_Vtr.push_back( string( s_pci, c_pci - s_pci ) );
- if ( *c_pci ) {
- if ( singlesep_bv ) {
- if ( !*(++c_pci) ) {
- // line ends with a sep -> add the empty field behind
- words_Vtr.push_back( "" );
- }
- } else
- sep_Ci.skipSep( c_pci );
- }
- }
-
- return words_Vtr.size();
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : join
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- std::string join( const std::vector<std::string> & words_r,
- const std::string & sep_r )
- {
- if ( words_r.empty() )
- return "";
-
- string ret( words_r[0] );
-
- for ( unsigned i = 1; i < words_r.size(); ++i ) {
- ret += sep_r + words_r[i];
- }
-
- return ret;
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : stripFirstWord
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- string stripFirstWord( string & line, const bool ltrim_first )
- {
- if ( ltrim_first )
- line = ltrim( line );
-
- if ( line.empty() )
- return line;
-
- string ret;
- string::size_type p = line.find_first_of( " \t" );
-
- if ( p == string::npos ) {
- // no ws on line
- ret = line;
- line.erase();
- } else if ( p == 0 ) {
- // starts with ws
- // ret remains empty
- line = ltrim( line );
- }
- else {
- // strip word and ltim line
- ret = line.substr( 0, p );
- line = ltrim( line.erase( 0, p ) );
- }
- return ret;
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : ltrim
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- std::string ltrim( const std::string & s )
- {
- if ( s.empty() )
- return s;
-
- string::size_type p = s.find_first_not_of( " \t\n" );
- if ( p == string::npos )
- return "";
-
- return s.substr( p );
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : rtrim
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- std::string rtrim( const std::string & s )
- {
- if ( s.empty() )
- return s;
-
- string::size_type p = s.find_last_not_of( " \t\n" );
- if ( p == string::npos )
- return "";
-
- return s.substr( 0, p+1 );
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : toLower
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- std::string toLower( const std::string & s )
- {
- if ( s.empty() )
- return s;
-
- string ret( s );
- for ( string::size_type i = 0; i < ret.length(); ++i ) {
- if ( isupper( ret[i] ) )
- ret[i] = static_cast<char>(tolower( ret[i] ));
- }
- return ret;
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : toUpper
- ** FUNCTION TYPE : std::string
- **
- ** DESCRIPTION :
- */
- std::string toUpper( const std::string & s )
- {
- if ( s.empty() )
- return s;
-
- string ret( s );
- for ( string::size_type i = 0; i < ret.length(); ++i ) {
- if ( islower( ret[i] ) )
- ret[i] = static_cast<char>(toupper( ret[i] ));
- }
- return ret;
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : dumpOn
- ** FUNCTION TYPE : std::ostream &
- **
- ** DESCRIPTION :
- */
- std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered )
- {
- unsigned i = 0;
- for ( std::list<std::string>::const_iterator it = l.begin(); it != l.end(); ++it, ++i ) {
- if ( numbered )
- str << '[' << i << ']';
- str << *it << endl;
- }
- return str;
- }
-
- /******************************************************************
- **
- **
- ** FUNCTION NAME : dumpOn
- ** FUNCTION TYPE : std::ostream &
- **
- ** DESCRIPTION :
- */
- std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered )
- {
- for ( unsigned i = 0; i < l.size(); ++i ) {
- if ( numbered )
- str << '[' << i << ']';
- str << l[i] << endl;
- }
- return str;
- }
-
-///////////////////////////////////////////////////////////////////
- } // namespace stringutil
-///////////////////////////////////////////////////////////////////
- } // namespace base
-} // namespace zypp
-
+++ /dev/null
-/*---------------------------------------------------------------------\
-| ____ _ __ __ ___ |
-| |__ / \ / / . \ . \ |
-| / / \ V /| _/ _/ |
-| / /__ | | | | | | |
-| /_____||_| |_| |_| |
-| |
-\---------------------------------------------------------------------*/
-/** \file zypp/base/stringutil.h
- *
- * \todo replace by Blocxx
- *
-*/
-
-#ifndef ZYPP_BASE_STRINGUTIL_H
-#define ZYPP_BASE_STRINGUTIL_H
-
-#include <cstdio>
-#include <cstdarg>
-
-#include <iosfwd>
-#include <vector>
-#include <string>
-#include <list>
-
-/**
- * Utility functions for std::strings. Most of them based on stringutil::form.
- **/
-///////////////////////////////////////////////////////////////////
-namespace zypp {
- namespace base {
- namespace stringutil {
-//////////////////////////////////////////////////////////////////
-
- enum Trim {
- NO_TRIM = 0x00,
- L_TRIM = 0x01,
- R_TRIM = 0x02,
- TRIM = (L_TRIM|R_TRIM)
- };
-
- inline std::string form( const char * format, ... )
- __attribute__ ((format (printf, 1, 2)));
-
- /**
- * Printf style building of strings via format string.
- * <PRE>
- * std::string ex( stringutil::form( "Example number %d", 1 ) );
- * std::cout << ex << stringutil::form( " and number %d.", 2 ) << endl;
- *
- * Will print: Example number 1 and number 2.
- * </PRE>
- **/
- inline std::string form( const char * format, ... ) {
- char * buf = 0;
- std::string val;
-
- va_list ap;
- va_start( ap, format );
-
- #if 1
- vasprintf( &buf, format, ap );
- if ( buf ) {
- val = buf;
- free( buf );
- }
- #else
- // Don't know wheter we actually nedd two va_lists, one to
- // evaluate the buffer size needed, and one to actually fill
- // the buffer. Maybe there's a save way to reuse a va_lists.
- va_list ap1;
- va_start( ap1, format );
- buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
- vsprintf( buf, format, ap1 );
- val = buf;
- delete [] buf;
- va_end( ap1 );
- #endif
-
- va_end( ap );
- return val;
- }
-
- /**
- * Print number. Optional second argument sets the minimal string width (' ' padded).
- * Negative values will cause the number to be left adjusted within the string. Default
- * width is 0.
- * <PRE>
- * numstring(42) -> "42"
- * numstring(42, 4) -> " 42"
- * numstring(42,-4) -> "42 "
- * </PRE>
- **/
- inline std::string numstring( char n, int w = 0 ) { return form( "%*hhd", w, n ); }
- inline std::string numstring( unsigned char n, int w = 0 ) { return form( "%*hhu", w, n ); }
- inline std::string numstring( short n, int w = 0 ) { return form( "%*hd", w, n ); }
- inline std::string numstring( unsigned short n, int w = 0 ) { return form( "%*hu", w, n ); }
- inline std::string numstring( int n, int w = 0 ) { return form( "%*d", w, n ); }
- inline std::string numstring( unsigned n, int w = 0 ) { return form( "%*u", w, n ); }
- inline std::string numstring( long n, int w = 0 ) { return form( "%*ld", w, n ); }
- inline std::string numstring( unsigned long n, int w = 0 ) { return form( "%*lu", w, n ); }
- inline std::string numstring( long long n, int w = 0 ) { return form( "%*lld", w, n ); }
- inline std::string numstring( unsigned long long n, int w = 0 ) { return form( "%*llu", w, n ); }
-
- /**
- * Print number as hex value with leading '0x'. Optional second argument sets the minimal
- * string width (0 padded). Negative values will cause the number to be left adjusted
- * within the string. Default width is 10 (4 for char).
- * <PRE>
- * hexstring(42) -> "0x0000002a"
- * hexstring(42, 4) -> "0x2a"
- * hexstring(42,-4) -> "0x2a"
- * </PRE>
- **/
- inline std::string hexstring( char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
- inline std::string hexstring( unsigned char n, int w = 4 ) { return form( "%#0*hhx", w, n ); }
- inline std::string hexstring( short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
- inline std::string hexstring( unsigned short n, int w = 10 ){ return form( "%#0*hx", w, n ); }
- inline std::string hexstring( int n, int w = 10 ){ return form( "%#0*x", w, n ); }
- inline std::string hexstring( unsigned n, int w = 10 ){ return form( "%#0*x", w, n ); }
- inline std::string hexstring( long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
- inline std::string hexstring( unsigned long n, int w = 10 ){ return form( "%#0*lx", w, n ); }
- inline std::string hexstring( long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
- inline std::string hexstring( unsigned long long n, int w = 0 ) { return form( "%#0*llx", w, n ); }
-
- /**
- * Print number as octal value with leading '0'. Optional second argument sets the minimal
- * string width (0 padded). Negative values will cause the number to be left adjusted
- * within the string. Default width is 5 (4 for char).
- * <PRE>
- * octstring(42) -> "00052"
- * octstring(42, 4) -> "0052"
- * octstring(42,-4) -> "052 "
- * </PRE>
- **/
- inline std::string octstring( char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
- inline std::string octstring( unsigned char n, int w = 4 ) { return form( "%#0*hho", w, n ); }
- inline std::string octstring( short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
- inline std::string octstring( unsigned short n, int w = 5 ) { return form( "%#0*ho", w, n ); }
- inline std::string octstring( int n, int w = 5 ) { return form( "%#0*o", w, n ); }
- inline std::string octstring( unsigned n, int w = 5 ) { return form( "%#0*o", w, n ); }
- inline std::string octstring( long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
- inline std::string octstring( unsigned long n, int w = 5 ) { return form( "%#0*lo", w, n ); }
- inline std::string octstring( long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
- inline std::string octstring( unsigned long long n, int w = 0 ) { return form( "%#0*llo", w, n ); }
-
- /**
- * String to integer type determined by template arg: time_t t = strtonum<time_t>( "42" );
- **/
- template<typename _It>
- inline _It strtonum( const std::string & str );
-
- template<>
- inline short strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
- template<>
- inline int strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
- template<>
- inline long strtonum( const std::string & str ) { return ::strtol ( str.c_str(), NULL, 0 ); }
- template<>
- inline long long strtonum( const std::string & str ) { return ::strtoll ( str.c_str(), NULL, 0 ); }
-
- template<>
- inline unsigned short strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
- template<>
- inline unsigned strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
- template<>
- inline unsigned long strtonum( const std::string & str ) { return ::strtoul ( str.c_str(), NULL, 0 ); }
- template<>
- inline unsigned long long strtonum( const std::string & str ) { return ::strtoull( str.c_str(), NULL, 0 ); }
-
- /**
- * String to integer type detemined function arg: time_t t; strtonum( "42", t );
- **/
- template<typename _It>
- inline _It strtonum( const std::string & str, _It & i ) { return i = strtonum<_It>( str ); }
-
- /** \brief read one line from a stream
- * Return one line read from istream. Afterwards the streampos is behind the delimiting '\n'
- * (or at EOF). The delimiting '\n' is <b>not</b> returned.
- *
- * If trim is true, the string returned is trimmed (surrounding whitespaces removed).
- * <PRE>
- * ifstream s( "somefile" );
- *
- * while ( s ) {
- * string l = getline( s );
- * if ( !(s.fail() || s.bad()) ) {
- *
- * // l contains valid data to be consumed.
- * // In case it makes any difference to you:
- * if ( s.good() ) {
- * // A delimiting '\n' was read.
- * } else {
- * // s.eof() is set: There's no '\n' at the end of file.
- * // Note: The line returned may netvertheless be empty if trimed is true.
- * }
- * }
- * }
- * </PRE>
- **/
- extern std::string getline( std::istream & str, bool trim = false );
-
- /** \brief read one line from a stream
- *
- * like above but with allows to specify trimming direction
- * */
- extern std::string getline( std::istream & str, const Trim trim_r );
-
- /**
- * Split line into words
- *
- * <b>singlesep_r = false</b>: Separator is any nonenmpty sequence of characters listed in sep_t.
- * Leading trailing separators are ignored.
- *
- * <b>Example:</b> singlesep_r = false, sep_t = ":"
- * <PRE>
- * "" -> words 0
- * ":" -> words 0
- * "a" -> words 1 |a|
- * "::a" -> words 1 |a|
- * "::a::" -> words 1 |a|
- * ":a::b:c:"-> words 3 |a|b|c|
- * </PRE>
- *
- * <b>singlesep_r = true</b>: Separator is any single character occuring in sep_t.
- * Leading trailing separators are not ignored (i.e will cause an empty word).
- *
- * <b>Example:</b> singlesep_r = true, sep_t = ":"
- * <PRE>
- * "" -> words 0
- * ":" -> words 2 |||
- * "a" -> words 1 |a|
- * ":a" -> words 2 ||a|
- * "a:" -> words 2 |a||
- * ":a:" -> words 3 ||a||
- * </PRE>
- *
- **/
- extern unsigned split( const std::string line_r,
- std::vector<std::string> & words_r,
- const std::string & sep_t = " \t",
- const bool singlesep_r = false );
-
- /**
- * Join strinngs in words_r using separator sep_r
- **/
- extern std::string join( const std::vector<std::string> & words_r,
- const std::string & sep_r = " " );
-
-
- /**
- * Split string into a list of lines using <b>any<\b> char in sep_r as line
- * delimiter. The delimiter is stripped from the line.
- *
- * <PRE>
- * splitToLines( "start\n\nend" ) -> { "start", "", "end" }
- * </PRE>
- **/
- inline std::list<std::string> splitToLines( const std::string text_r, const std::string & sep_r = "\n" )
- {
- std::vector<std::string> lines;
- stringutil::split( text_r, lines, "\n", true );
- std::list<std::string> ret;
- for ( unsigned i = 0; i < lines.size(); ++i ) {
- ret.push_back( lines[i] );
- }
- return ret;
- }
-
- /**
- * Strip the first word (delimited by blank or tab) from value, and return it.
- * Adjust value to start with the second word afterwards.
- *
- * If value starts with blank or tab, the <b>first word is empty</b> and value will be
- * ltrimmed afterwards.
- *
- * If ltrim_first is true, value will be ltrimmed before stripping the first word. Thus
- * first word is empty, iff value is empty or contains whitespace only.
- *
- * <PRE>
- * stripFirstWord( "1st" ) == "1st" and value truncated to ""
- * stripFirstWord( "1st word" ) == "1st" and value truncated to "word"
- * stripFirstWord( " 1st word" ) == "" and value truncated to "1st word"
- * stripFirstWord( " 1st word", true ) == "1st" and value truncated to "word"
- * </PRE>
- **/
- extern std::string stripFirstWord( std::string & value, const bool ltrim_first = false );
-
- /**
- * Return string with leading/trailing/surrounding whitespace removed
- **/
- extern std::string ltrim( const std::string & s );
- extern std::string rtrim( const std::string & s );
- inline std::string trim( const std::string & s, const Trim trim_r = TRIM ) {
- switch ( trim_r ) {
- case L_TRIM:
- return ltrim( s );
- case R_TRIM:
- return rtrim( s );
- case TRIM:
- return ltrim( rtrim( s ) );
- case NO_TRIM:
- break;
- }
- return s;
- }
-
- /**
- * Return string converted to lower/upper case
- **/
- extern std::string toLower( const std::string & s );
- extern std::string toUpper( const std::string & s );
-
- /**
- * Helper for stream output
- **/
- extern std::ostream & dumpOn( std::ostream & str, const std::list<std::string> & l, const bool numbered = false );
- extern std::ostream & dumpOn( std::ostream & str, const std::vector<std::string> & l, const bool numbered = false );
-
-///////////////////////////////////////////////////////////////////
- } // namespace stringutil
- } // namespace base
-} // namespace zypp
-///////////////////////////////////////////////////////////////////
-
-#endif // ZYPP_BASE_STRINGUTIL_H
{
if ( !_handler ) {
INT << "Error::E_not_open" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->attach(next);
}
MediaAccess::disconnect()
{
if ( !_handler )
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
_handler->disconnect();
}
}
if(checkonly)
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_error");
+ ZYPP_THROW( MediaException, "Error::E_error");
if ( !_handler ) {
INT << "Error::E_not_open" << " on provideFile(" << filename << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->provideFile( filename );
{
if ( !_handler ) {
INT << "Error::E_not_open" << " on provideDir(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->provideDir( dirname );
{
if ( !_handler ) {
INT << "Error::E_not_open" << " on provideDirTree(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->provideDirTree( dirname );
if ( !_handler ) {
INT << "Error::E_not_open" << " on dirInfo(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->dirInfo( retlist, dirname, dots );
// Return content of directory on media
void
-MediaAccess::dirInfo( PathInfo::dircontent & retlist, const Pathname & dirname, bool dots ) const
+MediaAccess::dirInfo( filesystem::DirContent & retlist, const Pathname & dirname, bool dots ) const
{
retlist.clear();
if ( !_handler ) {
INT << "Error::E_not_open" << " on dirInfo(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_open");
+ ZYPP_THROW( MediaException, "Error::E_not_open");
}
_handler->dirInfo( retlist, dirname, dots );
class MediaAccess : public base::ReferenceCounted, private base::NonCopyable
{
public:
- typedef shared_ptr<MediaAccess> Ptr;
- typedef shared_ptr<const MediaAccess> constPtr;
+ typedef intrusive_ptr<MediaAccess> Ptr;
+ typedef intrusive_ptr<const MediaAccess> constPtr;
private:
/**
* Basically the same as dirInfo above. The content is returned as
- * PathInfo::dircontent, which includes name and filetype of each directory
+ * filesystem::DirContent, which includes name and filetype of each directory
* entry. Retrieving the filetype usg. requires an additional ::stat call for
* each entry, thus it's more expensive than a simple readdir.
*
* \throws MediaException
*
**/
- void dirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots = true ) const;
+ void dirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots = true ) const;
/**
* Destructor
, _local_file( "" )
{
if ( _file.empty() ) {
- throw MediaException(ZYPP_EX_CODELOCATION, "E_bad_filename");
+ ZYPP_THROW( MediaException, "E_bad_filename");
} else if ( _media ) {
try {
_media->provideFile( _file );
}
catch (const MediaException & excpt_r)
{
- _media.reset();
+ _media = NULL;
#warning FIXME rethrow the exception
#if 0
ZYPP_RETHROW(excpt_r);
#include "zypp/base/Logger.h"
#include "zypp/ExternalProgram.h"
-#include "zypp/base/stringutil.h"
//#include <y2util/SysConfig.h>
#include "zypp/media/MediaCurl.h"
void MediaCurl::attachTo (bool next)
{
if ( next )
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_supported_by_media");
+ ZYPP_THROW( MediaException, "Error::E_not_supported_by_media");
if ( !_url.isValid() )
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_bad_url");
+ ZYPP_THROW( MediaException, "Error::E_bad_url");
_curl = curl_easy_init();
if ( !_curl ) {
ERR << "curl easy init failed" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_error");
+ ZYPP_THROW( MediaException, "Error::E_error");
}
_connected = true;
CURLcode ret = curl_easy_setopt( _curl, CURLOPT_ERRORBUFFER, _curlError );
if ( ret != 0 ) {
ERR << "Error setting error buffer" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt( _curl, CURLOPT_FAILONERROR, true );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
if ( _url.protocol() == Url::http ) {
ret = curl_easy_setopt ( _curl, CURLOPT_FOLLOWLOCATION, true );
if ( ret != 0) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt ( _curl, CURLOPT_MAXREDIRS, 3L );
if ( ret != 0) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
}
ret = curl_easy_setopt( _curl, CURLOPT_SSL_VERIFYPEER, 0 );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt( _curl, CURLOPT_SSL_VERIFYHOST, 0 );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
}
ret = curl_easy_setopt( _curl, CURLOPT_USERPWD, _userpwd.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
}
ret = curl_easy_setopt( _curl, CURLOPT_PROXY, _proxy.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
/*---------------------------------------------------------------*
ret = curl_easy_setopt( _curl, CURLOPT_PROXYUSERPWD, _proxyuserpwd.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
}
_currentCookieFile.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt( _curl, CURLOPT_COOKIEJAR,
_currentCookieFile.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt( _curl, CURLOPT_PROGRESSFUNCTION,
&MediaCurl::progressCallback );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
ret = curl_easy_setopt( _curl, CURLOPT_NOPROGRESS, false );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
}
DBG << filename.asString() << endl;
if(!_url.isValid())
- throw MediaException(ZYPP_EX_CODELOCATION, string("Error::E_bad_url") + " " + _url.asString());
+ ZYPP_THROW( MediaException, string("Error::E_bad_url") + " " + _url.asString());
if(_url.host().empty())
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_no_host_specified");
+ ZYPP_THROW( MediaException, "Error::E_no_host_specified");
string path = _url.path();
if ( !path.empty() && path != "/" && *path.rbegin() == '/' &&
DBG << "dest: " << dest << endl;
DBG << "destNew: " << destNew << endl;
- if( PathInfo::assert_dir( dest.dirname() ) )
+ if( assert_dir( dest.dirname() ) )
{
DBG << "assert_dir " << dest.dirname() << " failed" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, string("Error::E_system") + string(" ") + dest.dirname().asString());
+ ZYPP_THROW( MediaException, string("Error::E_system") + string(" ") + dest.dirname().asString());
}
DBG << "URL: " << url.asString().c_str() << endl;
urlBuffer.c_str() );
if ( ret != 0 ) {
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
FILE *file = fopen( destNew.c_str(), "w" );
if ( !file ) {
ERR << "fopen failed for file '" << destNew << "'" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, string("Error::E_write_error") + string(" ") + destNew);
+ ZYPP_THROW( MediaException, string("Error::E_write_error") + string(" ") + destNew);
}
ret = curl_easy_setopt( _curl, CURLOPT_WRITEDATA, file );
if ( ret != 0 ) {
fclose( file );
ERR << _curlError << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_curl_setopt_failed");
+ ZYPP_THROW( MediaException, "Error::E_curl_setopt_failed");
}
// Set callback and perform.
}
if ( ret != 0 ) {
- PathInfo::unlink( destNew );
+ unlink( destNew );
ERR << "curl error: " << ret << ": " << _curlError << endl;
std::string err;
#if 0
report->stop( err );
#endif
- throw MediaException(ZYPP_EX_CODELOCATION, err + string(" ") + _curlError);
+ ZYPP_THROW( MediaException, err + string(" ") + _curlError);
}
}
break;
#if 0
report->stop( err );
#endif
- throw MediaException(ZYPP_EX_CODELOCATION, err + string(" ") + _curlError);
+ ZYPP_THROW( MediaException, err + string(" ") + _curlError);
}
- if ( PathInfo::rename( destNew, dest ) != 0 ) {
+ if ( rename( destNew, dest ) != 0 ) {
ERR << "Rename failed" << endl;
#warning FIXME reenable change report
#if 0
report->stop( Error::E_write_error );
#endif
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_write_error");
+ ZYPP_THROW( MediaException, "Error::E_write_error");
}
#warning FIXME reenable change report
//
void MediaCurl::getDir( const Pathname & dirname, bool recurse_r ) const
{
- PathInfo::dircontent content;
+ filesystem::DirContent content;
try {
getDirInfo( content, dirname, /*dots*/false );
}
#endif
}
- for ( PathInfo::dircontent::const_iterator it = content.begin(); it != content.end(); ++it ) {
+ for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
try {
Pathname filename = dirname + it->name;
int res = 0;
switch ( it->type ) {
- case PathInfo::NOT_AVAIL: // old directory.yast contains no typeinfo at all
- case PathInfo::T_FILE:
+ case filesystem::FT_NOT_AVAIL: // old directory.yast contains no typeinfo at all
+ case filesystem::FT_FILE:
getFile( filename );
break;
- case PathInfo::T_DIR: // newer directory.yast contain at least directory info
+ case filesystem::FT_DIR: // newer directory.yast contain at least directory info
if ( recurse_r ) {
getDir( filename, recurse_r );
} else {
- res = PathInfo::assert_dir( localPath( filename ) );
+ res = assert_dir( localPath( filename ) );
if ( res ) {
WAR << "Ignore error (" << res << ") on creating local directory '" << localPath( filename ) << "'" << endl;
}
}
catch (const MediaException & excpt_r)
{
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_supported_by_media");
+ ZYPP_THROW( MediaException, "Error::E_not_supported_by_media");
}
}
//
// DESCRIPTION : Asserted that media is attached and retlist is empty.
//
-void MediaCurl::getDirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots ) const
+void MediaCurl::getDirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots ) const
{
try {
getDirectoryYast( retlist, dirname, dots );
}
catch (const MediaException & excpt_r)
{
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_supported_by_media");
+ ZYPP_THROW( MediaException, "Error::E_not_supported_by_media");
}
}
#define ZYPP_MEDIA_MEDIAEXCEPTION_H
#include <iosfwd>
-#include <stdexcept>
#include "zypp/base/Exception.h"
**/
class MediaException : public Exception
{
- typedef exception_detail::CodeLocation CodeLocation;
public:
- /** Ctor taking CodeLocation and message.
- * Use \ref ZYPP_THROW to throw exceptions.
- */
- MediaException( const CodeLocation & where_r, const std::string & msg_r )
+ /** Ctor taking message. */
+ MediaException( const std::string & msg_r )
: Exception( msg_r )
{}
};
-
+
/////////////////////////////////////////////////////////////////
} // namespace media
} // namespace zypp
#include <sstream>
#include "zypp/base/Logger.h"
+#include "zypp/base/String.h"
#include "zypp/media/MediaHandler.h"
-#include "zypp/base/stringutil.h"
using namespace std;
-using namespace zypp::base;
// use directory.yast on every media (not just via ftp/http)
#define NONREMOTE_DIRECTORY_YAST 1
Pathname apoint;
for ( unsigned i = 1; i < 1000; ++i ) {
- adir( Pathname::extend( abase, stringutil::hexstring( i ) ) );
- if ( ! adir.isExist() && PathInfo::mkdir( adir.path() ) == 0 ) {
+ adir( Pathname::extend( abase, str::hexstring( i ) ) );
+ if ( ! adir.isExist() && mkdir( adir.path() ) == 0 ) {
apoint = adir.path();
break;
}
}
if ( _tmp_attachPoint ) {
- int res = PathInfo::recursive_rmdir( _attachPoint );
+ int res = recursive_rmdir( _attachPoint );
if ( res == 0 ) {
MIL << "Deleted default attach point " << _attachPoint << endl;
} else {
if ( _attachPoint.empty() ) {
ERR << "Error::E_bad_attachpoint" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_bad_attachpoint");
+ ZYPP_THROW( MediaException, "Error::E_bad_attachpoint");
}
try {
Pathname targetFilename ) const
{
if ( !_isAttached ) {
- INT << "Error::E_not_attached" << " on provideFileCopy(" << srcFilename
+ INT << "Error::E_not_attached" << " on provideFileCopy(" << srcFilename
<< "," << targetFilename << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
try {
{
if ( !_isAttached ) {
INT << "Error::E_not_attached" << " on provideFile(" << filename << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
-
+
try {
getFile( filename ); // pass to concrete handler
}
{
if ( !_isAttached ) {
INT << "Error::E_not_attached" << " on provideDir(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
try {
getDir( dirname, /*recursive*/false ); // pass to concrete handler
{
if ( !_isAttached ) {
INT << "Error::E_not_attached" << " on provideDirTree(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
try {
PathInfo info( localPath( pathname ) );
if ( info.isFile() ) {
- PathInfo::unlink( info.path() );
+ unlink( info.path() );
} else if ( info.isDir() ) {
if ( info.path() != _localRoot ) {
- PathInfo::recursive_rmdir( info.path() );
+ recursive_rmdir( info.path() );
} else {
- PathInfo::clean_dir( info.path() );
+ clean_dir( info.path() );
}
}
}
if ( !_isAttached ) {
INT << "Error::E_not_attached" << " on dirInfo(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
try {
//
// DESCRIPTION :
//
-void MediaHandler::dirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots ) const
+void MediaHandler::dirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots ) const
{
retlist.clear();
if ( !_isAttached ) {
INT << "Error::E_not_attached" << " on dirInfo(" << dirname << ")" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_attached");
+ ZYPP_THROW( MediaException, "Error::E_not_attached");
}
try {
{
retlist.clear();
- PathInfo::dircontent content;
+ filesystem::DirContent content;
try {
getDirectoryYast( content, dirname, dots );
}
}
// convert to std::list<std::string>
- for ( PathInfo::dircontent::const_iterator it = content.begin(); it != content.end(); ++it ) {
+ for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
retlist.push_back( it->name );
}
}
// METHOD NAME : MediaHandler::getDirectoryYast
// METHOD TYPE : PMError
//
-void MediaHandler::getDirectoryYast( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots ) const
+void MediaHandler::getDirectoryYast( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots ) const
{
retlist.clear();
ifstream dir( localPath( dirFile ).asString().c_str() );
if ( dir.fail() ) {
ERR << "Unable to load '" << localPath( dirFile ) << "'" << endl;
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_system");
+ ZYPP_THROW( MediaException, "Error::E_system");
}
string line;
// Newer directory.yast append '/' to directory names
// Remaining entries are unspecified, although most probabely files.
- PathInfo::file_type type = PathInfo::NOT_AVAIL;
+ filesystem::FileType type = filesystem::FT_NOT_AVAIL;
if ( *line.rbegin() == '/' ) {
line.erase( line.end()-1 );
- type = PathInfo::T_DIR;
+ type = filesystem::FT_DIR;
}
if ( dots ) {
if ( *line.begin() == '.' ) continue;
}
- retlist.push_back( PathInfo::direntry( line, type ) );
+ retlist.push_back( filesystem::DirEntry( line, type ) );
}
}
}
if (info.isExist())
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_a_file");
+ ZYPP_THROW( MediaException, "Error::E_not_a_file");
else
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_file_not_found");
+ ZYPP_THROW( MediaException, "Error::E_file_not_found");
}
ZYPP_RETHROW(excpt_r);
#endif
}
-
- if ( PathInfo::copy( localPath( srcFilename ), targetFilename ) != 0 ) {
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_write_error");
+
+ if ( copy( localPath( srcFilename ), targetFilename ) != 0 ) {
+ ZYPP_THROW( MediaException, "Error::E_write_error");
}
}
}
if (info.isExist())
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_a_file");
+ ZYPP_THROW( MediaException, "Error::E_not_a_file");
else
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_file_not_found");
+ ZYPP_THROW( MediaException, "Error::E_file_not_found");
}
///////////////////////////////////////////////////////////////////
// Default implementation of pure virtual.
//
void MediaHandler::getDirInfo( std::list<std::string> & retlist,
- const Pathname & dirname, bool dots ) const
+ const Pathname & dirname, bool dots ) const
{
PathInfo info( localPath( dirname ) );
if( ! info.isDir() ) {
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_a_directory");
+ ZYPP_THROW( MediaException, "Error::E_not_a_directory");
}
#if NONREMOTE_DIRECTORY_YAST
#endif
// readdir
- int res = PathInfo::readdir( retlist, info.path(), dots );
+ int res = readdir( retlist, info.path(), dots );
if ( res )
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_system");
+ ZYPP_THROW( MediaException, "Error::E_system");
#if NONREMOTE_DIRECTORY_YAST
}
// DESCRIPTION : Asserted that media is attached and retlist is empty.
// Default implementation of pure virtual.
//
-void MediaHandler::getDirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots ) const
+void MediaHandler::getDirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots ) const
{
PathInfo info( localPath( dirname ) );
if( ! info.isDir() ) {
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_not_a_directory");
+ ZYPP_THROW( MediaException, "Error::E_not_a_directory");
}
#if NONREMOTE_DIRECTORY_YAST
#endif
// readdir
- int res = PathInfo::readdir( retlist, info.path(), dots );
+ int res = readdir( retlist, info.path(), dots );
if ( res )
- throw MediaException(ZYPP_EX_CODELOCATION, "Error::E_system");
+ ZYPP_THROW( MediaException, "Error::E_system");
#if NONREMOTE_DIRECTORY_YAST
}
#endif
virtual void getFile( const Pathname & filename ) const = 0;
/**
- * Call concrete handler to provide a file under a different place
+ * Call concrete handler to provide a file under a different place
* in the file system (usually not under attach point) as a copy.
* Media must be attached before by callee.
*
*
**/
virtual void getFileCopy( const Pathname & srcFilename, const Pathname & targetFilename ) const;
-
+
/**
* Call concrete handler to provide directory content (not recursive!)
*
**/
virtual void getDirInfo( std::list<std::string> & retlist,
- const Pathname & dirname, bool dots = true ) const = 0;
+ const Pathname & dirname, bool dots = true ) const = 0;
/**
* Basically the same as getDirInfo above. The content list is returned as
- * PathInfo::dircontent, which includes name and filetype of each directory
+ * filesystem::DirContent, which includes name and filetype of each directory
* entry. Retrieving the filetype usg. requires an additional ::stat call for
* each entry, thus it's more expensive than a simple readdir.
*
* \throws MediaException
*
**/
- virtual void getDirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots = true ) const = 0;
+ virtual void getDirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots = true ) const = 0;
protected:
*
**/
void getDirectoryYast( std::list<std::string> & retlist,
- const Pathname & dirname, bool dots = true ) const;
+ const Pathname & dirname, bool dots = true ) const;
/**
* Retrieve and if available scan dirname/directory.yast.
* \throws MediaException
*
**/
- void getDirectoryYast( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots = true ) const;
+ void getDirectoryYast( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots = true ) const;
public:
*
**/
void provideFile( Pathname filename ) const;
-
+
/**
- * Call concrete handler to provide a copy of a file under a different place
+ * Call concrete handler to provide a copy of a file under a different place
* in the file system (usually not under attach point) as a copy.
* Media must be attached before by callee.
*
*
**/
void provideFileCopy( Pathname srcFilename, Pathname targetFilename) const;
-
+
/**
* Use concrete handler to provide directory denoted
* by path below 'localRoot' (not recursive!).
*
**/
void dirInfo( std::list<std::string> & retlist,
- const Pathname & dirname, bool dots = true ) const;
+ const Pathname & dirname, bool dots = true ) const;
/**
* Basically the same as dirInfo above. The content is returned as
- * PathInfo::dircontent, which includes name and filetype of each directory
+ * filesystem::DirContent, which includes name and filetype of each directory
* entry. Retrieving the filetype usg. requires an additional ::stat call for
* each entry, thus it's more expensive than a simple readdir.
*
* \throws MediaException
*
**/
- void dirInfo( PathInfo::dircontent & retlist,
- const Pathname & dirname, bool dots = true ) const;
+ void dirInfo( filesystem::DirContent & retlist,
+ const Pathname & dirname, bool dots = true ) const;
};
///////////////////////////////////////////////////////////////////
virtual void getDir( const Pathname & dirname, bool recurse_r ) const; \
virtual void getDirInfo( std::list<std::string> & retlist, \
const Pathname & dirname, bool dots = true ) const; \
- virtual void getDirInfo( PathInfo::dircontent & retlist, \
+ virtual void getDirInfo( filesystem::DirContent & retlist, \
const Pathname & dirname, bool dots = true ) const;
} // namespace media