PathInfo.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_PATHINFO_H
00013 #define ZYPP_PATHINFO_H
00014 
00015 extern "C"
00016 {
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <fcntl.h>
00020 #include <unistd.h>
00021 #include <dirent.h>
00022 }
00023 
00024 #include <cerrno>
00025 #include <iosfwd>
00026 #include <list>
00027 #include <set>
00028 #include <map>
00029 
00030 #include "zypp/Pathname.h"
00031 
00033 namespace zypp
00034 { 
00035 
00037 
00044   namespace filesystem
00045   { 
00046 
00048 
00051     enum FileType
00052       {
00053         FT_NOT_AVAIL = 0x00, // no typeinfo available
00054         FT_NOT_EXIST = 0x01, // file does not exist
00055         FT_FILE      = 0x02,
00056         FT_DIR       = 0x04,
00057         FT_CHARDEV   = 0x08,
00058         FT_BLOCKDEV  = 0x10,
00059         FT_FIFO      = 0x20,
00060         FT_LINK      = 0x40,
00061         FT_SOCKET    = 0x80
00062       };
00064 
00066     extern std::ostream & operator<<( std::ostream & str, FileType obj );
00067 
00069 
00071     //
00072     //  CLASS NAME : StatMode
00076     class StatMode
00077     {
00078       friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
00079 
00080     public:
00082       StatMode( const mode_t & mode_r = 0 )
00083       : _mode( mode_r )
00084       {}
00085 
00086     public:
00087 
00090       FileType fileType() const;
00091 
00092       bool   isFile()  const { return S_ISREG( _mode ); }
00093       bool   isDir ()  const { return S_ISDIR( _mode ); }
00094       bool   isLink()  const { return S_ISLNK( _mode ); }
00095       bool   isChr()   const { return S_ISCHR( _mode ); }
00096       bool   isBlk()   const { return S_ISBLK( _mode ); }
00097       bool   isFifo()  const { return S_ISFIFO( _mode ); }
00098       bool   isSock()  const { return S_ISSOCK( _mode ); }
00100 
00103       bool   isRUsr()  const { return (_mode & S_IRUSR); }
00104       bool   isWUsr()  const { return (_mode & S_IWUSR); }
00105       bool   isXUsr()  const { return (_mode & S_IXUSR); }
00106 
00108       bool   isR()     const { return isRUsr(); }
00110       bool   isW()     const { return isWUsr(); }
00112       bool   isX()     const { return isXUsr(); }
00114 
00117       bool   isRGrp()  const { return (_mode & S_IRGRP); }
00118       bool   isWGrp()  const { return (_mode & S_IWGRP); }
00119       bool   isXGrp()  const { return (_mode & S_IXGRP); }
00121 
00124       bool   isROth()  const { return (_mode & S_IROTH); }
00125       bool   isWOth()  const { return (_mode & S_IWOTH); }
00126       bool   isXOth()  const { return (_mode & S_IXOTH); }
00128 
00132       bool   isUid()   const { return (_mode & S_ISUID); }
00134       bool   isGid()   const { return (_mode & S_ISGID); }
00136       bool   isVtx()   const { return (_mode & S_ISVTX); }
00138 
00142       bool   isPerm ( mode_t m ) const { return (m == perm()); }
00144       bool   hasPerm( mode_t m ) const { return (m == (m & perm())); }
00146 
00149       mode_t uperm()   const { return (_mode & S_IRWXU); }
00150       mode_t gperm()   const { return (_mode & S_IRWXG); }
00151       mode_t operm()   const { return (_mode & S_IRWXO); }
00152       mode_t perm()    const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
00154 
00156       mode_t st_mode() const { return _mode; }
00157 
00158     private:
00159       mode_t _mode;
00160     };
00162 
00164     extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
00165 
00167 
00169     //
00170     //  CLASS NAME : DevInoCache
00183     class DevInoCache
00184     {
00185     public:
00187       DevInoCache() {}
00188 
00190       void clear() { _devino.clear(); }
00191 
00197       bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
00198         return _devino[dev_r].insert( ino_r ).second;
00199       }
00200 
00201     private:
00202       std::map<dev_t,std::set<ino_t> > _devino;
00203     };
00205 
00207     //
00208     //  CLASS NAME : PathInfo
00216     class PathInfo
00217     {
00218       friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
00219 
00220     public:
00222       enum Mode { STAT, LSTAT };
00223 
00224     public:
00229       PathInfo();
00230       explicit
00231       PathInfo( const Pathname & path, Mode initial = STAT );
00232       explicit
00233       PathInfo( const std::string & path, Mode initial = STAT );
00234       explicit
00235       PathInfo( const char * path, Mode initial = STAT );
00237 
00239       ~PathInfo();
00240 
00242       const Pathname &    path()     const { return path_t; }
00244       const std::string & asString() const { return path_t.asString(); }
00246       Mode                mode()     const { return mode_e; }
00248       int                 error()    const { return error_i; }
00249 
00251       void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
00253       void setMode( Mode mode )             { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
00254 
00256       bool stat      ( const Pathname & path ) { setPath( path ); setMode( STAT );  return operator()(); }
00258       bool lstat     ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
00260       bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
00261 
00263       bool stat()   { setMode( STAT );  return operator()(); }
00265       bool lstat()  { setMode( LSTAT ); return operator()(); }
00267       bool operator()();
00268 
00269     public:
00270 
00275       bool   isExist() const { return !error_i; }
00276 
00281       FileType fileType() const;
00282 
00283       bool   isFile()  const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
00284       bool   isDir ()  const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
00285       bool   isLink()  const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
00286       bool   isChr()   const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
00287       bool   isBlk()   const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
00288       bool   isFifo()  const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
00289       bool   isSock()  const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
00290 
00291       // permission
00292       bool   isRUsr()  const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
00293       bool   isWUsr()  const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
00294       bool   isXUsr()  const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
00295 
00296       bool   isR()     const { return isRUsr(); }
00297       bool   isW()     const { return isWUsr(); }
00298       bool   isX()     const { return isXUsr(); }
00299 
00300       bool   isRGrp()  const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
00301       bool   isWGrp()  const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
00302       bool   isXGrp()  const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
00303 
00304       bool   isROth()  const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
00305       bool   isWOth()  const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
00306       bool   isXOth()  const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
00307 
00308       bool   isUid()   const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
00309       bool   isGid()   const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
00310       bool   isVtx()   const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
00311 
00312       bool   isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
00313       bool   hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
00314 
00315       mode_t uperm()   const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
00316       mode_t gperm()   const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
00317       mode_t operm()   const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
00318       mode_t perm()    const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
00319 
00320       mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
00322 
00324       StatMode asStatMode() const { return st_mode(); }
00325 
00326       nlink_t nlink()  const { return isExist() ? statbuf_C.st_nlink : 0; }
00327 
00330       uid_t  owner()   const { return isExist() ? statbuf_C.st_uid : 0; }
00331       gid_t  group()   const { return isExist() ? statbuf_C.st_gid : 0; }
00333 
00337       mode_t userMay() const;
00338 
00339       bool   userMayR() const { return( userMay() & 04 ); }
00340       bool   userMayW() const { return( userMay() & 02 ); }
00341       bool   userMayX() const { return( userMay() & 01 ); }
00342 
00343       bool   userMayRW()  const { return( (userMay() & 06) == 06 ); }
00344       bool   userMayRX()  const { return( (userMay() & 05) == 05 ); }
00345       bool   userMayWX()  const { return( (userMay() & 03) == 03 ); }
00346 
00347       bool   userMayRWX() const { return( userMay() == 07 ); }
00349 
00352       ino_t  ino()     const { return isExist() ? statbuf_C.st_ino  : 0; }
00353       dev_t  dev()     const { return isExist() ? statbuf_C.st_dev  : 0; }
00354       dev_t  rdev()    const { return isExist() ? statbuf_C.st_rdev : 0; }
00355 
00356       unsigned int major() const;
00357       unsigned int minor() const;
00359 
00362       off_t         size()    const { return isExist() ? statbuf_C.st_size : 0; }
00363       unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
00364       unsigned long blocks()  const { return isExist() ? statbuf_C.st_blocks  : 0; }
00366 
00369       time_t atime()   const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
00370       time_t mtime()   const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
00371       time_t ctime()   const { return isExist() ? statbuf_C.st_ctime : 0; }
00373 
00374     private:
00375       Pathname    path_t;
00376       struct stat statbuf_C;
00377       Mode        mode_e;
00378       int         error_i;
00379     };
00381 
00383     extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
00384 
00386 
00388 
00397     int mkdir( const Pathname & path, unsigned mode = 0755 );
00398 
00406     int assert_dir( const Pathname & path, unsigned mode = 0755 );
00407 
00413     int rmdir( const Pathname & path );
00414 
00421     int recursive_rmdir( const Pathname & path );
00422 
00429     int clean_dir( const Pathname & path );
00430 
00438     int copy_dir( const Pathname & srcpath, const Pathname & destpath );
00439 
00448     int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
00449 
00462     int readdir( std::list<std::string> & retlist,
00463                  const Pathname & path, bool dots = true );
00464 
00477     int readdir( std::list<Pathname> & retlist,
00478                  const Pathname & path, bool dots = true );
00479 
00481     struct DirEntry {
00482       std::string name;
00483       FileType    type;
00484       DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
00485       : name( name_r )
00486       , type( type_r )
00487       {}
00488     };
00489 
00491     typedef std::list<DirEntry> DirContent;
00492 
00503     int readdir( DirContent & retlist, const Pathname & path,
00504                  bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
00505 
00506 
00512     int is_empty_dir(const Pathname & path);
00513 
00515 
00517 
00524     int unlink( const Pathname & path );
00525 
00531     int rename( const Pathname & oldpath, const Pathname & newpath );
00532 
00539     int copy( const Pathname & file, const Pathname & dest );
00540 
00547     int symlink( const Pathname & oldpath, const Pathname & newpath );
00548 
00555     int hardlink( const Pathname & oldpath, const Pathname & newpath );
00556 
00563     int copy_file2dir( const Pathname & file, const Pathname & dest );
00565 
00567 
00576     std::string md5sum( const Pathname & file );
00577 
00583     std::string sha1sum( const Pathname & file );
00585 
00587 
00594     int chmod( const Pathname & path, mode_t mode );
00596 
00598 
00605     enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
00606 
00607     ZIP_TYPE zipType( const Pathname & file );
00608 
00616     int erase( const Pathname & path );
00618 
00620   } // namespace filesystem
00622 
00624   using filesystem::PathInfo;
00625 
00627 } // namespace zypp
00629 #endif // ZYPP_PATHINFO_H

Generated on Thu Jul 6 00:07:22 2006 for zypp by  doxygen 1.4.6