libzypp  17.3.1
PathInfo.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PATHINFO_H
13 #define ZYPP_PATHINFO_H
14 
15 extern "C"
16 {
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 }
23 
24 #include <cerrno>
25 #include <iosfwd>
26 #include <list>
27 #include <set>
28 #include <map>
29 
30 #include "zypp/Pathname.h"
31 #include "zypp/CheckSum.h"
32 #include "zypp/ByteCount.h"
33 
35 namespace zypp
36 {
37 
38  class StrMatcher;
39 
41 
48  namespace filesystem
49  {
50 
52 
55  enum FileType
56  {
57  FT_NOT_AVAIL = 0x00, // no typeinfo available
58  FT_NOT_EXIST = 0x01, // file does not exist
59  FT_FILE = 0x02,
60  FT_DIR = 0x04,
61  FT_CHARDEV = 0x08,
62  FT_BLOCKDEV = 0x10,
63  FT_FIFO = 0x20,
64  FT_LINK = 0x40,
65  FT_SOCKET = 0x80
66  };
68 
70  extern std::ostream & operator<<( std::ostream & str, FileType obj );
71 
73 
75  //
76  // CLASS NAME : StatMode
80  class StatMode
81  {
82  friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
83 
84  public:
86  StatMode( const mode_t & mode_r = 0 )
87  : _mode( mode_r )
88  {}
89 
90  public:
91 
94  FileType fileType() const;
95 
96  bool isFile() const { return S_ISREG( _mode ); }
97  bool isDir () const { return S_ISDIR( _mode ); }
98  bool isLink() const { return S_ISLNK( _mode ); }
99  bool isChr() const { return S_ISCHR( _mode ); }
100  bool isBlk() const { return S_ISBLK( _mode ); }
101  bool isFifo() const { return S_ISFIFO( _mode ); }
102  bool isSock() const { return S_ISSOCK( _mode ); }
104 
107  bool isRUsr() const { return (_mode & S_IRUSR); }
108  bool isWUsr() const { return (_mode & S_IWUSR); }
109  bool isXUsr() const { return (_mode & S_IXUSR); }
110 
112  bool isR() const { return isRUsr(); }
114  bool isW() const { return isWUsr(); }
116  bool isX() const { return isXUsr(); }
118 
121  bool isRGrp() const { return (_mode & S_IRGRP); }
122  bool isWGrp() const { return (_mode & S_IWGRP); }
123  bool isXGrp() const { return (_mode & S_IXGRP); }
125 
128  bool isROth() const { return (_mode & S_IROTH); }
129  bool isWOth() const { return (_mode & S_IWOTH); }
130  bool isXOth() const { return (_mode & S_IXOTH); }
132 
136  bool isUid() const { return (_mode & S_ISUID); }
138  bool isGid() const { return (_mode & S_ISGID); }
140  bool isVtx() const { return (_mode & S_ISVTX); }
142 
146  bool isPerm ( mode_t m ) const { return (m == perm()); }
148  bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
150 
153  mode_t uperm() const { return (_mode & S_IRWXU); }
154  mode_t gperm() const { return (_mode & S_IRWXG); }
155  mode_t operm() const { return (_mode & S_IRWXO); }
156  mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
158 
160  mode_t st_mode() const { return _mode; }
161 
162  private:
163  mode_t _mode;
164  };
166 
168  extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
169 
171 
173  //
174  // CLASS NAME : DevInoCache
188  {
189  public:
192 
194  void clear() { _devino.clear(); }
195 
201  bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
202  return _devino[dev_r].insert( ino_r ).second;
203  }
204 
205  private:
206  std::map<dev_t,std::set<ino_t> > _devino;
207  };
209 
211  //
212  // CLASS NAME : PathInfo
220  class PathInfo
221  {
222  friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
223 
224  public:
226  enum Mode { STAT, LSTAT };
227 
228  public:
233  PathInfo();
234  explicit
235  PathInfo( const Pathname & path, Mode initial = STAT );
236  explicit
237  PathInfo( const std::string & path, Mode initial = STAT );
238  explicit
239  PathInfo( const char * path, Mode initial = STAT );
241 
243  ~PathInfo();
244 
246  const Pathname & path() const { return path_t; }
248  const std::string & asString() const { return path_t.asString(); }
250  const char * c_str() const { return path_t.asString().c_str(); }
252  Mode mode() const { return mode_e; }
254  int error() const { return error_i; }
255 
257  void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
259  void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
260 
262  bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
264  bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
266  bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
267 
269  bool stat() { setMode( STAT ); return operator()(); }
271  bool lstat() { setMode( LSTAT ); return operator()(); }
273  bool operator()();
274 
275  public:
276 
281  bool isExist() const { return !error_i; }
282 
287  FileType fileType() const;
288 
289  bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
290  bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
291  bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
292  bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
293  bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
294  bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
295  bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
296 
297  // permission
298  bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
299  bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
300  bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
301 
302  bool isR() const { return isRUsr(); }
303  bool isW() const { return isWUsr(); }
304  bool isX() const { return isXUsr(); }
305 
306  bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
307  bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
308  bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
309 
310  bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
311  bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
312  bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
313 
314  bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
315  bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
316  bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
317 
318  bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
319  bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
320 
321  mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
322  mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
323  mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
324  mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
325 
326  mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
328 
330  StatMode asStatMode() const { return st_mode(); }
331 
332  nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
333 
336  uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
337  gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
339 
343  mode_t userMay() const;
344 
345  bool userMayR() const { return( userMay() & 04 ); }
346  bool userMayW() const { return( userMay() & 02 ); }
347  bool userMayX() const { return( userMay() & 01 ); }
348 
349  bool userMayRW() const { return( (userMay() & 06) == 06 ); }
350  bool userMayRX() const { return( (userMay() & 05) == 05 ); }
351  bool userMayWX() const { return( (userMay() & 03) == 03 ); }
352 
353  bool userMayRWX() const { return( userMay() == 07 ); }
355 
358  ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
359  dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
360  dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
361 
362  unsigned int devMajor() const;
363  unsigned int devMinor() const;
365 
368  off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
369  unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
370  unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
372 
375  time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
376  time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
377  time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
379 
380  private:
382  struct stat statbuf_C;
384  int error_i;
385  };
387 
389  extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
390 
392 
394 
403  int mkdir( const Pathname & path, unsigned mode = 0755 );
404 
412  int assert_dir( const Pathname & path, unsigned mode = 0755 );
413 
419  int rmdir( const Pathname & path );
420 
427  int recursive_rmdir( const Pathname & path );
428 
435  int clean_dir( const Pathname & path );
436 
444  int copy_dir( const Pathname & srcpath, const Pathname & destpath );
445 
454  int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
455 
460  const StrMatcher & matchNoDots();
461 
474  int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r );
475 
499  int dirForEach( const Pathname & dir_r, const StrMatcher & matcher_r, function<bool(const Pathname &, const char *const)> fnc_r );
500 
513  int readdir( std::list<std::string> & retlist,
514  const Pathname & path, bool dots = true );
515 
528  int readdir( std::list<Pathname> & retlist,
529  const Pathname & path, bool dots = true );
530 
532  struct DirEntry {
533  std::string name;
535  DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
536  : name( name_r )
537  , type( type_r )
538  {}
539 
540  bool operator==( const DirEntry &rhs ) const;
541  };
542 
543  inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
544  { return str << '[' << obj.type << "] " << obj.name; }
545 
547  typedef std::list<DirEntry> DirContent;
548 
549  std::ostream & operator<<( std::ostream & str, const DirContent & obj );
550 
561  int readdir( DirContent & retlist, const Pathname & path,
562  bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
563 
569  int is_empty_dir(const Pathname & path);
570 
572 
574 
583  int assert_file( const Pathname & path, unsigned mode = 0644 );
584 
591  int touch (const Pathname & path);
592 
598  int unlink( const Pathname & path );
599 
608  int rename( const Pathname & oldpath, const Pathname & newpath );
609 
636  int exchange( const Pathname & lpath, const Pathname & rpath );
637 
644  int copy( const Pathname & file, const Pathname & dest );
645 
652  int symlink( const Pathname & oldpath, const Pathname & newpath );
653 
660  int hardlink( const Pathname & oldpath, const Pathname & newpath );
661 
667  int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
668 
675  int readlink( const Pathname & symlink_r, Pathname & target_r );
677  inline Pathname readlink( const Pathname & symlink_r )
678  {
679  Pathname target;
680  readlink( symlink_r, target );
681  return target;
682  }
683 
696  Pathname expandlink( const Pathname & path_r );
697 
704  int copy_file2dir( const Pathname & file, const Pathname & dest );
706 
708 
717  std::string md5sum( const Pathname & file );
718 
724  std::string sha1sum( const Pathname & file );
726 
732  std::string checksum( const Pathname & file, const std::string &algorithm );
733 
739  bool is_checksum( const Pathname & file, const CheckSum &checksum );
740 
742 
749  int chmod( const Pathname & path, mode_t mode );
750 
756  int addmod( const Pathname & path, mode_t mode );
757 
763  int delmod( const Pathname & path, mode_t mode );
765 
767 
775 
776  ZIP_TYPE zipType( const Pathname & file );
777 
785  int erase( const Pathname & path );
786 
794  ByteCount df( const Pathname & path );
795 
801  mode_t getUmask();
802 
809  inline mode_t applyUmaskTo( mode_t mode_r )
810  { return mode_r & ~getUmask(); }
812 
814  } // namespace filesystem
816 
818  using filesystem::PathInfo;
819 
821 } // namespace zypp
823 #endif // ZYPP_PATHINFO_H
int assert_dir(const Pathname &path, unsigned mode)
Like &#39;mkdir -p&#39;.
Definition: PathInfo.cc:320
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:709
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition: PathInfo.h:774
Listentry returned by readdir.
Definition: PathInfo.h:532
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
Definition: PathInfo.cc:1133
const Pathname & path() const
Return current Pathname.
Definition: PathInfo.h:246
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:994
const char * c_str() const
Return current Pathname as C-string.
Definition: PathInfo.h:250
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition: PathInfo.cc:977
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like &#39;readlink&#39;.
Definition: PathInfo.cc:877
bool lstat()
LSTAT current path.
Definition: PathInfo.h:271
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition: StrMatcher.h:297
Store and operate with byte count.
Definition: ByteCount.h:30
int mkdir(const Pathname &path, unsigned mode)
Like &#39;mkdir&#39;.
Definition: PathInfo.cc:306
void clear()
Clear cache.
Definition: PathInfo.h:194
mode_t perm() const
Definition: PathInfo.h:156
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like &#39;::link&#39;.
Definition: PathInfo.cc:822
friend std::ostream & operator<<(std::ostream &str, const StatMode &obj)
Definition: PathInfo.cc:96
int chmod(const Pathname &path, mode_t mode)
Like &#39;chmod&#39;.
Definition: PathInfo.cc:1045
nlink_t nlink() const
Definition: PathInfo.h:332
bool hasPerm(mode_t m) const
Test for set permission bits.
Definition: PathInfo.h:148
time_t mtime() const
Definition: PathInfo.h:376
int clean_dir(const Pathname &path)
Like &#39;rm -r DIR/ *&#39;.
Definition: PathInfo.cc:434
String related utilities and Regular expression matching.
mode_t uperm() const
Definition: PathInfo.h:321
bool stat()
STAT current path.
Definition: PathInfo.h:269
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
mode_t gperm() const
Definition: PathInfo.h:154
bool isPerm(mode_t m) const
Definition: PathInfo.h:318
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like &#39;cp file dest&#39;.
Definition: PathInfo.cc:943
DirEntry(const std::string &name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition: PathInfo.h:535
Mode
stat() or lstat()
Definition: PathInfo.h:226
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition: PathInfo.cc:641
mode_t gperm() const
Definition: PathInfo.h:322
ZIP_TYPE zipType(const Pathname &file)
Definition: PathInfo.cc:1077
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition: PathInfo.cc:1054
const StrMatcher & matchNoDots()
Convenience returning StrMatcher( "[^.]*", Match::GLOB )
Definition: PathInfo.cc:536
mode_t st_mode() const
Return the mode_t value.
Definition: PathInfo.h:160
bool isR() const
Short for isRUsr().
Definition: PathInfo.h:112
int unlink(const Pathname &path)
Like &#39;unlink&#39;.
Definition: PathInfo.cc:653
void setMode(Mode mode)
Set a new Mode .
Definition: PathInfo.h:259
int rename(const Pathname &oldpath, const Pathname &newpath)
Like &#39;rename&#39;.
Definition: PathInfo.cc:695
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:281
Simple cache remembering device/inode to detect hardlinks.
Definition: PathInfo.h:187
time_t atime() const
Definition: PathInfo.h:375
int recursive_rmdir(const Pathname &path)
Like &#39;rm -r DIR&#39;.
Definition: PathInfo.cc:413
int error() const
Return error returned from last stat/lstat call.
Definition: PathInfo.h:254
bool operator()(const Pathname &path)
Restat path using current mode.
Definition: PathInfo.h:266
bool isUid() const
Set UID bit.
Definition: PathInfo.h:136
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:547
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:836
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition: PathInfo.h:201
bool userMayRW() const
Definition: PathInfo.h:349
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from ::stat.
Definition: PathInfo.h:86
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like &#39;symlink&#39;.
Definition: PathInfo.cc:808
bool hasPerm(mode_t m) const
Definition: PathInfo.h:319
const std::string & asString() const
Return current Pathname as String.
Definition: PathInfo.h:248
std::map< dev_t, std::set< ino_t > > _devino
Definition: PathInfo.h:206
int touch(const Pathname &path)
Change file&#39;s modification and access times.
Definition: PathInfo.cc:1157
bool userMayRX() const
Definition: PathInfo.h:350
int copy(const Pathname &file, const Pathname &dest)
Like &#39;cp file dest&#39;.
Definition: PathInfo.cc:773
bool isW() const
Short for isWUsr().
Definition: PathInfo.h:114
Mode mode() const
Return current stat Mode.
Definition: PathInfo.h:252
bool isGid() const
Set GID bit.
Definition: PathInfo.h:138
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:589
int rmdir(const Pathname &path)
Like &#39;rmdir&#39;.
Definition: PathInfo.cc:367
mode_t getUmask()
Get the current umask (file mode creation mask)
Definition: PathInfo.cc:1121
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like &#39;cp -a srcpath destpath&#39;.
Definition: PathInfo.cc:455
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.h:80
bool isPerm(mode_t m) const
Test for equal permission bits.
Definition: PathInfo.h:146
FileType
File type information.
Definition: PathInfo.h:55
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:1004
unsigned long blocks() const
Definition: PathInfo.h:370
int erase(const Pathname &path)
Erase whatever happens to be located at path (file or directory).
Definition: PathInfo.cc:1026
unsigned long blksize() const
Definition: PathInfo.h:369
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like &#39;cp -a srcpath/.
Definition: PathInfo.cc:496
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:220
int dirForEach(const Pathname &dir_r, function< bool(const Pathname &, const char *const)> fnc_r)
Invoke callback function fnc_r for each entry in directory dir_r.
Definition: PathInfo.cc:542
Pathname expandlink(const Pathname &path_r)
Recursively follows the symlink pointed to by path_r and returns the Pathname to the real file or dir...
Definition: PathInfo.cc:898
mode_t uperm() const
Definition: PathInfo.h:153
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition: PathInfo.h:809
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition: PathInfo.cc:1063
time_t ctime() const
Definition: PathInfo.h:377
mode_t perm() const
Definition: PathInfo.h:324
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Definition: Glob.cc:53
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
bool lstat(const Pathname &path)
LSTAT path.
Definition: PathInfo.h:264
bool isX() const
Short for isXUsr().
Definition: PathInfo.h:116
mode_t operm() const
Definition: PathInfo.h:323
bool userMayRWX() const
Definition: PathInfo.h:353
bool stat(const Pathname &path)
STAT path.
Definition: PathInfo.h:262
bool userMayWX() const
Definition: PathInfo.h:351
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
Definition: PathInfo.cc:1016
mode_t st_mode() const
Definition: PathInfo.h:326
FileType fileType() const
Definition: PathInfo.cc:71
bool isVtx() const
Sticky bit.
Definition: PathInfo.h:140
void setPath(const Pathname &path)
Set a new Pathname.
Definition: PathInfo.h:257
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition: PathInfo.h:330
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition: PathInfo.cc:1105
mode_t operm() const
Definition: PathInfo.h:155