|
yast2-core
|
00001 /*---------------------------------------------------------------------\ 00002 | | 00003 | __ __ ____ _____ ____ | 00004 | \ \ / /_ _/ ___|_ _|___ \ | 00005 | \ V / _` \___ \ | | __) | | 00006 | | | (_| |___) || | / __/ | 00007 | |_|\__,_|____/ |_| |_____| | 00008 | | 00009 | core system | 00010 | (C) SuSE GmbH | 00011 \----------------------------------------------------------------------/ 00012 00013 File: PathInfo.h 00014 00015 Author: Michael Andres <ma@suse.de> 00016 Maintainer: Michael Andres <ma@suse.de> 00017 00018 /-*/ 00019 #ifndef PathInfo_h 00020 #define PathInfo_h 00021 00022 extern "C" 00023 { 00024 #include <sys/types.h> 00025 #include <sys/stat.h> 00026 #include <fcntl.h> 00027 #include <unistd.h> 00028 #include <dirent.h> 00029 } 00030 00031 #include <cerrno> 00032 #include <iosfwd> 00033 #include <list> 00034 #include <set> 00035 #include <map> 00036 00037 #include <y2util/Pathname.h> 00038 00040 // 00041 // CLASS NAME : PathInfo 00045 class PathInfo { 00046 00047 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj ); 00048 00049 public: 00050 00051 enum Mode { STAT, LSTAT }; 00052 00053 enum file_type { 00054 NOT_AVAIL = 0x00, // no typeinfo available 00055 NOT_EXIST = 0x01, // file does not exist 00056 T_FILE = 0x02, 00057 T_DIR = 0x04, 00058 T_CHARDEV = 0x08, 00059 T_BLOCKDEV = 0x10, 00060 T_FIFO = 0x20, 00061 T_LINK = 0x40, 00062 T_SOCKET = 0x80 00063 }; 00064 friend std::ostream & operator<<( std::ostream & str, file_type obj ); 00065 00069 class stat_mode; 00070 00074 class devino_cache; 00075 00076 private: 00077 00078 Pathname path_t; 00079 00080 struct stat statbuf_C; 00081 Mode mode_e; 00082 int error_i; 00083 00084 public: 00085 00086 PathInfo( const Pathname & path = "", Mode initial = STAT ); 00087 PathInfo( const std::string & path, Mode initial = STAT ); 00088 PathInfo( const char * path, Mode initial = STAT ); 00089 virtual ~PathInfo(); 00090 00091 const Pathname & path() const { return path_t; } 00092 const std::string & asString() const { return path_t.asString(); } 00093 Mode mode() const { return mode_e; } 00094 int error() const { return error_i; } 00095 00096 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; } 00097 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; } 00098 00099 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); } 00100 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); } 00101 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); } 00102 00103 bool stat() { setMode( STAT ); return operator()(); } 00104 bool lstat() { setMode( LSTAT ); return operator()(); } 00105 bool operator()(); 00106 00107 public: 00108 00109 bool isExist() const { return !error_i; } 00110 00111 // file type 00112 file_type fileType() const; 00113 00114 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); } 00115 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); } 00116 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); } 00117 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); } 00118 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); } 00119 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); } 00120 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); } 00121 00122 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; } 00123 00124 // owner 00125 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; } 00126 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; } 00127 00128 // permission 00129 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); } 00130 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); } 00131 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); } 00132 00133 bool isR() const { return isRUsr(); } 00134 bool isW() const { return isWUsr(); } 00135 bool isX() const { return isXUsr(); } 00136 00137 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); } 00138 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); } 00139 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); } 00140 00141 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); } 00142 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); } 00143 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); } 00144 00145 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); } 00146 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); } 00147 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); } 00148 00149 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; } 00150 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; } 00151 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; } 00152 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; } 00153 00154 bool isPerm ( mode_t m ) const { return (m == perm()); } 00155 bool hasPerm( mode_t m ) const { return (m == (m & perm())); } 00156 00157 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; } 00158 00159 // permission according to current uid/gid (returns [0-7]) 00160 mode_t userMay() const; 00161 00162 bool userMayR() const { return( userMay() & 01 ); } 00163 bool userMayW() const { return( userMay() & 02 ); } 00164 bool userMayX() const { return( userMay() & 04 ); } 00165 00166 bool userMayRW() const { return( (userMay() & 03) == 03 ); } 00167 bool userMayRX() const { return( (userMay() & 05) == 05 ); } 00168 bool userMayWX() const { return( (userMay() & 06) == 06 ); } 00169 00170 bool userMayRWX() const { return( userMay() == 07 ); } 00171 00172 // device 00173 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; } 00174 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; } 00175 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; } 00176 00177 // size 00178 off_t size() const { return isExist() ? statbuf_C.st_size : 0; } 00179 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; } 00180 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; } 00181 00182 // time 00183 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */ 00184 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */ 00185 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; } 00186 00187 public: 00188 00190 // convenience stuff 00192 // static functions as they may or may not invalidate any stat info 00193 // stored by a PathiInfo. 00195 00197 // Directories 00199 00207 static int mkdir( const Pathname & path, unsigned mode = 0755 ); 00208 00216 static int assert_dir( const Pathname & path, unsigned mode = 0755 ); 00217 00223 static int rmdir( const Pathname & path ); 00224 00231 static int recursive_rmdir( const Pathname & path ); 00232 00239 static int clean_dir( const Pathname & path ); 00240 00248 static int copy_dir( const Pathname & srcpath, const Pathname & destpath ); 00249 00257 static int readdir( std::list<std::string> & retlist, 00258 const Pathname & path, bool dots = true ); 00259 00260 struct direntry { 00261 std::string name; 00262 file_type type; 00263 direntry( const std::string & name_r = std::string(), file_type type_r = NOT_AVAIL ) 00264 : name( name_r ) 00265 , type( type_r ) 00266 {} 00267 }; 00268 00269 typedef std::list<direntry> dircontent; 00270 00281 static int readdir( dircontent & retlist, const Pathname & path, 00282 bool dots = true, Mode statmode = STAT ); 00283 00285 // Files 00287 00293 static int unlink( const Pathname & path ); 00294 00300 static int rename( const Pathname & oldpath, const Pathname & newpath ); 00301 00308 static int copy( const Pathname & file, const Pathname & dest ); 00309 00316 static int symlink( const Pathname & oldpath, const Pathname & newpath ); 00317 00324 static int hardlink( const Pathname & oldpath, const Pathname & newpath ); 00325 00332 static int copy_file2dir( const Pathname & file, const Pathname & dest ); 00333 00335 // 00337 00343 static int erase( const Pathname & path ); 00344 00346 // permissions 00348 00354 static int chmod( const Pathname & path, mode_t mode ); 00355 00357 // magic 00359 00365 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 }; 00366 00367 static ZIP_TYPE zipType( const Pathname & file ); 00368 }; 00369 00371 00373 // 00374 // CLASS NAME : PathInfo::stat_mode 00378 class PathInfo::stat_mode { 00379 friend std::ostream & operator<<( std::ostream & str, const stat_mode & obj ); 00380 private: 00381 mode_t _mode; 00382 public: 00383 stat_mode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {} 00384 public: 00385 // file type 00386 file_type fileType() const; 00387 00388 bool isFile() const { return S_ISREG( _mode ); } 00389 bool isDir () const { return S_ISDIR( _mode ); } 00390 bool isLink() const { return S_ISLNK( _mode ); } 00391 bool isChr() const { return S_ISCHR( _mode ); } 00392 bool isBlk() const { return S_ISBLK( _mode ); } 00393 bool isFifo() const { return S_ISFIFO( _mode ); } 00394 bool isSock() const { return S_ISSOCK( _mode ); } 00395 00396 // permission 00397 bool isRUsr() const { return (_mode & S_IRUSR); } 00398 bool isWUsr() const { return (_mode & S_IWUSR); } 00399 bool isXUsr() const { return (_mode & S_IXUSR); } 00400 00401 bool isR() const { return isRUsr(); } 00402 bool isW() const { return isWUsr(); } 00403 bool isX() const { return isXUsr(); } 00404 00405 bool isRGrp() const { return (_mode & S_IRGRP); } 00406 bool isWGrp() const { return (_mode & S_IWGRP); } 00407 bool isXGrp() const { return (_mode & S_IXGRP); } 00408 00409 bool isROth() const { return (_mode & S_IROTH); } 00410 bool isWOth() const { return (_mode & S_IWOTH); } 00411 bool isXOth() const { return (_mode & S_IXOTH); } 00412 00413 bool isUid() const { return (_mode & S_ISUID); } 00414 bool isGid() const { return (_mode & S_ISGID); } 00415 bool isVtx() const { return (_mode & S_ISVTX); } 00416 00417 mode_t uperm() const { return (_mode & S_IRWXU); } 00418 mode_t gperm() const { return (_mode & S_IRWXG); } 00419 mode_t operm() const { return (_mode & S_IRWXO); } 00420 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); } 00421 00422 bool isPerm ( mode_t m ) const { return (m == perm()); } 00423 bool hasPerm( mode_t m ) const { return (m == (m & perm())); } 00424 00425 mode_t st_mode() const { return _mode; } 00426 }; 00427 00429 00431 // 00432 // CLASS NAME : PathInfo::devino_cache 00446 class PathInfo::devino_cache { 00447 00448 private: 00449 00450 std::map<dev_t,std::set<ino_t> > _devino; 00451 00452 public: 00456 devino_cache() {} 00457 00461 void clear() { _devino.clear(); } 00462 00468 bool insert( const dev_t & dev_r, const ino_t & ino_r ) { 00469 return _devino[dev_r].insert( ino_r ).second; 00470 } 00471 }; 00472 00474 00476 00477 #endif // PathInfo_h
1.7.3