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
00034 namespace zypp
00035 {
00036
00038
00045 namespace filesystem
00046 {
00047
00049
00052 enum FileType
00053 {
00054 FT_NOT_AVAIL = 0x00,
00055 FT_NOT_EXIST = 0x01,
00056 FT_FILE = 0x02,
00057 FT_DIR = 0x04,
00058 FT_CHARDEV = 0x08,
00059 FT_BLOCKDEV = 0x10,
00060 FT_FIFO = 0x20,
00061 FT_LINK = 0x40,
00062 FT_SOCKET = 0x80
00063 };
00065
00067 extern std::ostream & operator<<( std::ostream & str, FileType obj );
00068
00070
00072
00073
00077 class StatMode
00078 {
00079 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
00080
00081 public:
00083 StatMode( const mode_t & mode_r = 0 )
00084 : _mode( mode_r )
00085 {}
00086
00087 public:
00088
00091 FileType fileType() const;
00092
00093 bool isFile() const { return S_ISREG( _mode ); }
00094 bool isDir () const { return S_ISDIR( _mode ); }
00095 bool isLink() const { return S_ISLNK( _mode ); }
00096 bool isChr() const { return S_ISCHR( _mode ); }
00097 bool isBlk() const { return S_ISBLK( _mode ); }
00098 bool isFifo() const { return S_ISFIFO( _mode ); }
00099 bool isSock() const { return S_ISSOCK( _mode ); }
00101
00104 bool isRUsr() const { return (_mode & S_IRUSR); }
00105 bool isWUsr() const { return (_mode & S_IWUSR); }
00106 bool isXUsr() const { return (_mode & S_IXUSR); }
00107
00109 bool isR() const { return isRUsr(); }
00111 bool isW() const { return isWUsr(); }
00113 bool isX() const { return isXUsr(); }
00115
00118 bool isRGrp() const { return (_mode & S_IRGRP); }
00119 bool isWGrp() const { return (_mode & S_IWGRP); }
00120 bool isXGrp() const { return (_mode & S_IXGRP); }
00122
00125 bool isROth() const { return (_mode & S_IROTH); }
00126 bool isWOth() const { return (_mode & S_IWOTH); }
00127 bool isXOth() const { return (_mode & S_IXOTH); }
00129
00133 bool isUid() const { return (_mode & S_ISUID); }
00135 bool isGid() const { return (_mode & S_ISGID); }
00137 bool isVtx() const { return (_mode & S_ISVTX); }
00139
00143 bool isPerm ( mode_t m ) const { return (m == perm()); }
00145 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
00147
00150 mode_t uperm() const { return (_mode & S_IRWXU); }
00151 mode_t gperm() const { return (_mode & S_IRWXG); }
00152 mode_t operm() const { return (_mode & S_IRWXO); }
00153 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
00155
00157 mode_t st_mode() const { return _mode; }
00158
00159 private:
00160 mode_t _mode;
00161 };
00163
00165 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
00166
00168
00170
00171
00184 class DevInoCache
00185 {
00186 public:
00188 DevInoCache() {}
00189
00191 void clear() { _devino.clear(); }
00192
00198 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
00199 return _devino[dev_r].insert( ino_r ).second;
00200 }
00201
00202 private:
00203 std::map<dev_t,std::set<ino_t> > _devino;
00204 };
00206
00208
00209
00217 class PathInfo
00218 {
00219 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
00220
00221 public:
00223 enum Mode { STAT, LSTAT };
00224
00225 public:
00230 PathInfo();
00231 explicit
00232 PathInfo( const Pathname & path, Mode initial = STAT );
00233 explicit
00234 PathInfo( const std::string & path, Mode initial = STAT );
00235 explicit
00236 PathInfo( const char * path, Mode initial = STAT );
00238
00240 ~PathInfo();
00241
00243 const Pathname & path() const { return path_t; }
00245 const std::string & asString() const { return path_t.asString(); }
00247 Mode mode() const { return mode_e; }
00249 int error() const { return error_i; }
00250
00252 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
00254 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
00255
00257 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
00259 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
00261 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
00262
00264 bool stat() { setMode( STAT ); return operator()(); }
00266 bool lstat() { setMode( LSTAT ); return operator()(); }
00268 bool operator()();
00269
00270 public:
00271
00276 bool isExist() const { return !error_i; }
00277
00282 FileType fileType() const;
00283
00284 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
00285 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
00286 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
00287 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
00288 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
00289 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
00290 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
00291
00292
00293 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
00294 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
00295 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
00296
00297 bool isR() const { return isRUsr(); }
00298 bool isW() const { return isWUsr(); }
00299 bool isX() const { return isXUsr(); }
00300
00301 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
00302 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
00303 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
00304
00305 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
00306 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
00307 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
00308
00309 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
00310 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
00311 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
00312
00313 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
00314 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
00315
00316 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
00317 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
00318 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
00319 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
00320
00321 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
00323
00325 StatMode asStatMode() const { return st_mode(); }
00326
00327 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
00328
00331 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
00332 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
00334
00338 mode_t userMay() const;
00339
00340 bool userMayR() const { return( userMay() & 04 ); }
00341 bool userMayW() const { return( userMay() & 02 ); }
00342 bool userMayX() const { return( userMay() & 01 ); }
00343
00344 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
00345 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
00346 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
00347
00348 bool userMayRWX() const { return( userMay() == 07 ); }
00350
00353 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
00354 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
00355 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
00356
00357 unsigned int major() const;
00358 unsigned int minor() const;
00360
00363 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
00364 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
00365 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
00367
00370 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; }
00371 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; }
00372 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
00374
00375 private:
00376 Pathname path_t;
00377 struct stat statbuf_C;
00378 Mode mode_e;
00379 int error_i;
00380 };
00382
00384 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
00385
00387
00389
00398 int mkdir( const Pathname & path, unsigned mode = 0755 );
00399
00407 int assert_dir( const Pathname & path, unsigned mode = 0755 );
00408
00414 int rmdir( const Pathname & path );
00415
00422 int recursive_rmdir( const Pathname & path );
00423
00430 int clean_dir( const Pathname & path );
00431
00439 int copy_dir( const Pathname & srcpath, const Pathname & destpath );
00440
00449 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
00450
00463 int readdir( std::list<std::string> & retlist,
00464 const Pathname & path, bool dots = true );
00465
00478 int readdir( std::list<Pathname> & retlist,
00479 const Pathname & path, bool dots = true );
00480
00482 struct DirEntry {
00483 std::string name;
00484 FileType type;
00485 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
00486 : name( name_r )
00487 , type( type_r )
00488 {}
00489 };
00490
00492 typedef std::list<DirEntry> DirContent;
00493
00504 int readdir( DirContent & retlist, const Pathname & path,
00505 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
00506
00507
00513 int is_empty_dir(const Pathname & path);
00514
00516
00518
00525 int unlink( const Pathname & path );
00526
00532 int rename( const Pathname & oldpath, const Pathname & newpath );
00533
00540 int copy( const Pathname & file, const Pathname & dest );
00541
00548 int symlink( const Pathname & oldpath, const Pathname & newpath );
00549
00556 int hardlink( const Pathname & oldpath, const Pathname & newpath );
00557
00564 int copy_file2dir( const Pathname & file, const Pathname & dest );
00566
00568
00577 std::string md5sum( const Pathname & file );
00578
00584 std::string sha1sum( const Pathname & file );
00586
00592 std::string checksum( const Pathname & file, const std::string &algorithm );
00593
00599 bool is_checksum( const Pathname & file, const CheckSum &checksum );
00600
00602
00609 int chmod( const Pathname & path, mode_t mode );
00611
00613
00620 enum ZIP_TYPE { ZT_NONE, ZT_GZ, ZT_BZ2 };
00621
00622 ZIP_TYPE zipType( const Pathname & file );
00623
00631 int erase( const Pathname & path );
00633
00635 }
00637
00639 using filesystem::PathInfo;
00640
00642 }
00644 #endif // ZYPP_PATHINFO_H