libzypp
10.5.0
|
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 #include "zypp/CheckSum.h" 00032 #include "zypp/ByteCount.h" 00033 00035 namespace zypp 00036 { 00037 00039 00046 namespace filesystem 00047 { 00048 00050 00053 enum FileType 00054 { 00055 FT_NOT_AVAIL = 0x00, // no typeinfo available 00056 FT_NOT_EXIST = 0x01, // file does not exist 00057 FT_FILE = 0x02, 00058 FT_DIR = 0x04, 00059 FT_CHARDEV = 0x08, 00060 FT_BLOCKDEV = 0x10, 00061 FT_FIFO = 0x20, 00062 FT_LINK = 0x40, 00063 FT_SOCKET = 0x80 00064 }; 00066 00068 extern std::ostream & operator<<( std::ostream & str, FileType obj ); 00069 00071 00073 // 00074 // CLASS NAME : StatMode 00078 class StatMode 00079 { 00080 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj ); 00081 00082 public: 00084 StatMode( const mode_t & mode_r = 0 ) 00085 : _mode( mode_r ) 00086 {} 00087 00088 public: 00089 00092 FileType fileType() const; 00093 00094 bool isFile() const { return S_ISREG( _mode ); } 00095 bool isDir () const { return S_ISDIR( _mode ); } 00096 bool isLink() const { return S_ISLNK( _mode ); } 00097 bool isChr() const { return S_ISCHR( _mode ); } 00098 bool isBlk() const { return S_ISBLK( _mode ); } 00099 bool isFifo() const { return S_ISFIFO( _mode ); } 00100 bool isSock() const { return S_ISSOCK( _mode ); } 00102 00105 bool isRUsr() const { return (_mode & S_IRUSR); } 00106 bool isWUsr() const { return (_mode & S_IWUSR); } 00107 bool isXUsr() const { return (_mode & S_IXUSR); } 00108 00110 bool isR() const { return isRUsr(); } 00112 bool isW() const { return isWUsr(); } 00114 bool isX() const { return isXUsr(); } 00116 00119 bool isRGrp() const { return (_mode & S_IRGRP); } 00120 bool isWGrp() const { return (_mode & S_IWGRP); } 00121 bool isXGrp() const { return (_mode & S_IXGRP); } 00123 00126 bool isROth() const { return (_mode & S_IROTH); } 00127 bool isWOth() const { return (_mode & S_IWOTH); } 00128 bool isXOth() const { return (_mode & S_IXOTH); } 00130 00134 bool isUid() const { return (_mode & S_ISUID); } 00136 bool isGid() const { return (_mode & S_ISGID); } 00138 bool isVtx() const { return (_mode & S_ISVTX); } 00140 00144 bool isPerm ( mode_t m ) const { return (m == perm()); } 00146 bool hasPerm( mode_t m ) const { return (m == (m & perm())); } 00148 00151 mode_t uperm() const { return (_mode & S_IRWXU); } 00152 mode_t gperm() const { return (_mode & S_IRWXG); } 00153 mode_t operm() const { return (_mode & S_IRWXO); } 00154 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); } 00156 00158 mode_t st_mode() const { return _mode; } 00159 00160 private: 00161 mode_t _mode; 00162 }; 00164 00166 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj ); 00167 00169 00171 // 00172 // CLASS NAME : DevInoCache 00185 class DevInoCache 00186 { 00187 public: 00189 DevInoCache() {} 00190 00192 void clear() { _devino.clear(); } 00193 00199 bool insert( const dev_t & dev_r, const ino_t & ino_r ) { 00200 return _devino[dev_r].insert( ino_r ).second; 00201 } 00202 00203 private: 00204 std::map<dev_t,std::set<ino_t> > _devino; 00205 }; 00207 00209 // 00210 // CLASS NAME : PathInfo 00218 class PathInfo 00219 { 00220 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj ); 00221 00222 public: 00224 enum Mode { STAT, LSTAT }; 00225 00226 public: 00231 PathInfo(); 00232 explicit 00233 PathInfo( const Pathname & path, Mode initial = STAT ); 00234 explicit 00235 PathInfo( const std::string & path, Mode initial = STAT ); 00236 explicit 00237 PathInfo( const char * path, Mode initial = STAT ); 00239 00241 ~PathInfo(); 00242 00244 const Pathname & path() const { return path_t; } 00246 const std::string & asString() const { return path_t.asString(); } 00248 const char * c_str() const { return path_t.asString().c_str(); } 00250 Mode mode() const { return mode_e; } 00252 int error() const { return error_i; } 00253 00255 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; } 00257 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; } 00258 00260 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); } 00262 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); } 00264 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); } 00265 00267 bool stat() { setMode( STAT ); return operator()(); } 00269 bool lstat() { setMode( LSTAT ); return operator()(); } 00271 bool operator()(); 00272 00273 public: 00274 00279 bool isExist() const { return !error_i; } 00280 00285 FileType fileType() const; 00286 00287 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); } 00288 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); } 00289 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); } 00290 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); } 00291 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); } 00292 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); } 00293 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); } 00294 00295 // permission 00296 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); } 00297 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); } 00298 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); } 00299 00300 bool isR() const { return isRUsr(); } 00301 bool isW() const { return isWUsr(); } 00302 bool isX() const { return isXUsr(); } 00303 00304 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); } 00305 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); } 00306 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); } 00307 00308 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); } 00309 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); } 00310 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); } 00311 00312 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); } 00313 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); } 00314 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); } 00315 00316 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); } 00317 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); } 00318 00319 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; } 00320 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; } 00321 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; } 00322 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; } 00323 00324 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; } 00326 00328 StatMode asStatMode() const { return st_mode(); } 00329 00330 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; } 00331 00334 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; } 00335 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; } 00337 00341 mode_t userMay() const; 00342 00343 bool userMayR() const { return( userMay() & 04 ); } 00344 bool userMayW() const { return( userMay() & 02 ); } 00345 bool userMayX() const { return( userMay() & 01 ); } 00346 00347 bool userMayRW() const { return( (userMay() & 06) == 06 ); } 00348 bool userMayRX() const { return( (userMay() & 05) == 05 ); } 00349 bool userMayWX() const { return( (userMay() & 03) == 03 ); } 00350 00351 bool userMayRWX() const { return( userMay() == 07 ); } 00353 00356 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; } 00357 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; } 00358 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; } 00359 00360 unsigned int major() const; 00361 unsigned int minor() const; 00363 00366 off_t size() const { return isExist() ? statbuf_C.st_size : 0; } 00367 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; } 00368 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; } 00370 00373 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */ 00374 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */ 00375 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; } 00377 00378 private: 00379 Pathname path_t; 00380 struct stat statbuf_C; 00381 Mode mode_e; 00382 int error_i; 00383 }; 00385 00387 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj ); 00388 00390 00392 00401 int mkdir( const Pathname & path, unsigned mode = 0755 ); 00402 00410 int assert_dir( const Pathname & path, unsigned mode = 0755 ); 00411 00417 int rmdir( const Pathname & path ); 00418 00425 int recursive_rmdir( const Pathname & path ); 00426 00433 int clean_dir( const Pathname & path ); 00434 00442 int copy_dir( const Pathname & srcpath, const Pathname & destpath ); 00443 00452 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath); 00453 00466 int readdir( std::list<std::string> & retlist, 00467 const Pathname & path, bool dots = true ); 00468 00481 int readdir( std::list<Pathname> & retlist, 00482 const Pathname & path, bool dots = true ); 00483 00485 struct DirEntry { 00486 std::string name; 00487 FileType type; 00488 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL ) 00489 : name( name_r ) 00490 , type( type_r ) 00491 {} 00492 00493 bool operator==( const DirEntry &rhs ) const; 00494 }; 00495 00497 typedef std::list<DirEntry> DirContent; 00498 00509 int readdir( DirContent & retlist, const Pathname & path, 00510 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT ); 00511 00512 00518 int is_empty_dir(const Pathname & path); 00519 00521 00523 00532 int assert_file( const Pathname & path, unsigned mode = 0644 ); 00533 00540 int touch (const Pathname & path); 00541 00547 int unlink( const Pathname & path ); 00548 00554 int rename( const Pathname & oldpath, const Pathname & newpath ); 00555 00582 int exchange( const Pathname & lpath, const Pathname & rpath ); 00583 00590 int copy( const Pathname & file, const Pathname & dest ); 00591 00598 int symlink( const Pathname & oldpath, const Pathname & newpath ); 00599 00606 int hardlink( const Pathname & oldpath, const Pathname & newpath ); 00607 00613 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath ); 00614 00621 int readlink( const Pathname & symlink_r, Pathname & target_r ); 00623 inline Pathname readlink( const Pathname & symlink_r ) 00624 { 00625 Pathname target; 00626 readlink( symlink_r, target ); 00627 return target; 00628 } 00629 00642 Pathname expandlink( const Pathname & path_r ); 00643 00650 int copy_file2dir( const Pathname & file, const Pathname & dest ); 00652 00654 00663 std::string md5sum( const Pathname & file ); 00664 00670 std::string sha1sum( const Pathname & file ); 00672 00678 std::string checksum( const Pathname & file, const std::string &algorithm ); 00679 00685 bool is_checksum( const Pathname & file, const CheckSum &checksum ); 00686 00688 00695 int chmod( const Pathname & path, mode_t mode ); 00696 00702 int addmod( const Pathname & path, mode_t mode ); 00703 00709 int delmod( const Pathname & path, mode_t mode ); 00711 00713 00720 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 }; 00721 00722 ZIP_TYPE zipType( const Pathname & file ); 00723 00731 int erase( const Pathname & path ); 00732 00740 ByteCount df( const Pathname & path ); 00741 00747 mode_t getUmask(); 00748 00755 inline mode_t applyUmaskTo( mode_t mode_r ) 00756 { return mode_r & ~getUmask(); } 00758 00760 } // namespace filesystem 00762 00764 using filesystem::PathInfo; 00765 00767 } // namespace zypp 00769 #endif // ZYPP_PATHINFO_H