00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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,
00055 NOT_EXIST = 0x01,
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
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
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
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
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
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
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
00183 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; }
00184 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; }
00185 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
00186
00187 public:
00188
00190
00192
00193
00195
00197
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
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 chmod( const Pathname & path, mode_t mode );
00344
00346
00348
00354 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
00355
00356 static ZIP_TYPE zipType( const Pathname & file );
00357 };
00358
00360
00362
00363
00367 class PathInfo::stat_mode {
00368 friend std::ostream & operator<<( std::ostream & str, const stat_mode & obj );
00369 private:
00370 mode_t _mode;
00371 public:
00372 stat_mode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {}
00373 public:
00374
00375 file_type fileType() const;
00376
00377 bool isFile() const { return S_ISREG( _mode ); }
00378 bool isDir () const { return S_ISDIR( _mode ); }
00379 bool isLink() const { return S_ISLNK( _mode ); }
00380 bool isChr() const { return S_ISCHR( _mode ); }
00381 bool isBlk() const { return S_ISBLK( _mode ); }
00382 bool isFifo() const { return S_ISFIFO( _mode ); }
00383 bool isSock() const { return S_ISSOCK( _mode ); }
00384
00385
00386 bool isRUsr() const { return (_mode & S_IRUSR); }
00387 bool isWUsr() const { return (_mode & S_IWUSR); }
00388 bool isXUsr() const { return (_mode & S_IXUSR); }
00389
00390 bool isR() const { return isRUsr(); }
00391 bool isW() const { return isWUsr(); }
00392 bool isX() const { return isXUsr(); }
00393
00394 bool isRGrp() const { return (_mode & S_IRGRP); }
00395 bool isWGrp() const { return (_mode & S_IWGRP); }
00396 bool isXGrp() const { return (_mode & S_IXGRP); }
00397
00398 bool isROth() const { return (_mode & S_IROTH); }
00399 bool isWOth() const { return (_mode & S_IWOTH); }
00400 bool isXOth() const { return (_mode & S_IXOTH); }
00401
00402 bool isUid() const { return (_mode & S_ISUID); }
00403 bool isGid() const { return (_mode & S_ISGID); }
00404 bool isVtx() const { return (_mode & S_ISVTX); }
00405
00406 mode_t uperm() const { return (_mode & S_IRWXU); }
00407 mode_t gperm() const { return (_mode & S_IRWXG); }
00408 mode_t operm() const { return (_mode & S_IRWXO); }
00409 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
00410
00411 bool isPerm ( mode_t m ) const { return (m == perm()); }
00412 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
00413
00414 mode_t st_mode() const { return _mode; }
00415 };
00416
00418
00420
00421
00435 class PathInfo::devino_cache {
00436
00437 private:
00438
00439 std::map<dev_t,std::set<ino_t> > _devino;
00440
00441 public:
00445 devino_cache() {}
00446
00450 void clear() { _devino.clear(); }
00451
00457 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
00458 return _devino[dev_r].insert( ino_r ).second;
00459 }
00460 };
00461
00463
00465
00466 #endif // PathInfo_h