00001
00002
00003
00004
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,
00056 FT_NOT_EXIST = 0x01,
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
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
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
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 Mode mode() const { return mode_e; }
00250 int error() const { return error_i; }
00251
00253 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
00255 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
00256
00258 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
00260 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
00262 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
00263
00265 bool stat() { setMode( STAT ); return operator()(); }
00267 bool lstat() { setMode( LSTAT ); return operator()(); }
00269 bool operator()();
00270
00271 public:
00272
00277 bool isExist() const { return !error_i; }
00278
00283 FileType fileType() const;
00284
00285 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
00286 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
00287 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
00288 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
00289 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
00290 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
00291 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
00292
00293
00294 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
00295 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
00296 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
00297
00298 bool isR() const { return isRUsr(); }
00299 bool isW() const { return isWUsr(); }
00300 bool isX() const { return isXUsr(); }
00301
00302 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
00303 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
00304 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
00305
00306 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
00307 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
00308 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
00309
00310 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
00311 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
00312 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
00313
00314 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
00315 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
00316
00317 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
00318 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
00319 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
00320 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
00321
00322 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
00324
00326 StatMode asStatMode() const { return st_mode(); }
00327
00328 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
00329
00332 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
00333 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
00335
00339 mode_t userMay() const;
00340
00341 bool userMayR() const { return( userMay() & 04 ); }
00342 bool userMayW() const { return( userMay() & 02 ); }
00343 bool userMayX() const { return( userMay() & 01 ); }
00344
00345 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
00346 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
00347 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
00348
00349 bool userMayRWX() const { return( userMay() == 07 ); }
00351
00354 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
00355 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
00356 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
00357
00358 unsigned int major() const;
00359 unsigned int minor() const;
00361
00364 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
00365 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
00366 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
00368
00371 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; }
00372 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; }
00373 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
00375
00376 private:
00377 Pathname path_t;
00378 struct stat statbuf_C;
00379 Mode mode_e;
00380 int error_i;
00381 };
00383
00385 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
00386
00388
00390
00399 int mkdir( const Pathname & path, unsigned mode = 0755 );
00400
00408 int assert_dir( const Pathname & path, unsigned mode = 0755 );
00409
00415 int rmdir( const Pathname & path );
00416
00423 int recursive_rmdir( const Pathname & path );
00424
00431 int clean_dir( const Pathname & path );
00432
00440 int copy_dir( const Pathname & srcpath, const Pathname & destpath );
00441
00450 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
00451
00464 int readdir( std::list<std::string> & retlist,
00465 const Pathname & path, bool dots = true );
00466
00479 int readdir( std::list<Pathname> & retlist,
00480 const Pathname & path, bool dots = true );
00481
00483 struct DirEntry {
00484 std::string name;
00485 FileType type;
00486 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
00487 : name( name_r )
00488 , type( type_r )
00489 {}
00490 };
00491
00493 typedef std::list<DirEntry> DirContent;
00494
00505 int readdir( DirContent & retlist, const Pathname & path,
00506 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
00507
00508
00514 int is_empty_dir(const Pathname & path);
00515
00517
00519
00526 int unlink( const Pathname & path );
00527
00533 int rename( const Pathname & oldpath, const Pathname & newpath );
00534
00541 int copy( const Pathname & file, const Pathname & dest );
00542
00549 int symlink( const Pathname & oldpath, const Pathname & newpath );
00550
00557 int hardlink( const Pathname & oldpath, const Pathname & newpath );
00558
00565 int copy_file2dir( const Pathname & file, const Pathname & dest );
00567
00569
00578 std::string md5sum( const Pathname & file );
00579
00585 std::string sha1sum( const Pathname & file );
00587
00593 std::string checksum( const Pathname & file, const std::string &algorithm );
00594
00600 bool is_checksum( const Pathname & file, const CheckSum &checksum );
00601
00603
00610 int chmod( const Pathname & path, mode_t mode );
00612
00614
00621 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
00622
00623 ZIP_TYPE zipType( const Pathname & file );
00624
00632 int erase( const Pathname & path );
00633
00641 ByteCount df( const Pathname & path );
00643
00645 }
00647
00649 using filesystem::PathInfo;
00650
00652 }
00654 #endif // ZYPP_PATHINFO_H