libzypp  17.28.4
PathInfo.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_CORE_FS_PATHINFO_H
13 #define ZYPP_CORE_FS_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-core/Pathname.h>
31 #include <zypp-core/ByteCount.h>
32 #include <zypp-core/CheckSum.h>
33 
35 namespace zypp
36 {
37 
39 
46  namespace filesystem
47  {
48 
50 
53  enum FileType
54  {
55  FT_NOT_AVAIL = 0x00, // no typeinfo available
56  FT_NOT_EXIST = 0x01, // file does not exist
57  FT_FILE = 0x02,
58  FT_DIR = 0x04,
59  FT_CHARDEV = 0x08,
60  FT_BLOCKDEV = 0x10,
61  FT_FIFO = 0x20,
62  FT_LINK = 0x40,
63  FT_SOCKET = 0x80
64  };
66 
68  extern std::ostream & operator<<( std::ostream & str, FileType obj );
69 
71 
73  //
74  // CLASS NAME : StatMode
78  class StatMode
79  {
80  friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
81 
82  public:
84  StatMode( const mode_t & mode_r = 0 )
85  : _mode( mode_r )
86  {}
87 
88  public:
89 
92  FileType fileType() const;
93 
94  bool isFile() const { return S_ISREG( _mode ); }
95  bool isDir () const { return S_ISDIR( _mode ); }
96  bool isLink() const { return S_ISLNK( _mode ); }
97  bool isChr() const { return S_ISCHR( _mode ); }
98  bool isBlk() const { return S_ISBLK( _mode ); }
99  bool isFifo() const { return S_ISFIFO( _mode ); }
100  bool isSock() const { return S_ISSOCK( _mode ); }
102 
105  bool isRUsr() const { return (_mode & S_IRUSR); }
106  bool isWUsr() const { return (_mode & S_IWUSR); }
107  bool isXUsr() const { return (_mode & S_IXUSR); }
108 
110  bool isR() const { return isRUsr(); }
112  bool isW() const { return isWUsr(); }
114  bool isX() const { return isXUsr(); }
116 
119  bool isRGrp() const { return (_mode & S_IRGRP); }
120  bool isWGrp() const { return (_mode & S_IWGRP); }
121  bool isXGrp() const { return (_mode & S_IXGRP); }
123 
126  bool isROth() const { return (_mode & S_IROTH); }
127  bool isWOth() const { return (_mode & S_IWOTH); }
128  bool isXOth() const { return (_mode & S_IXOTH); }
130 
134  bool isUid() const { return (_mode & S_ISUID); }
136  bool isGid() const { return (_mode & S_ISGID); }
138  bool isVtx() const { return (_mode & S_ISVTX); }
140 
144  bool isPerm ( mode_t m ) const { return (m == perm()); }
146  bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
148 
151  mode_t uperm() const { return (_mode & S_IRWXU); }
152  mode_t gperm() const { return (_mode & S_IRWXG); }
153  mode_t operm() const { return (_mode & S_IRWXO); }
154  mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
156 
158  mode_t st_mode() const { return _mode; }
159 
160  private:
161  mode_t _mode;
162  };
164 
166  extern std::ostream & operator<<( std::ostream & str, const StatMode & obj );
167 
169 
171  //
172  // CLASS NAME : DevInoCache
186  {
187  public:
190 
192  void clear() { _devino.clear(); }
193 
199  bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
200  return _devino[dev_r].insert( ino_r ).second;
201  }
202 
203  private:
204  std::map<dev_t,std::set<ino_t> > _devino;
205  };
207 
209  //
210  // CLASS NAME : PathInfo
218  class PathInfo
219  {
220  friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
221 
222  public:
224  enum Mode { STAT, LSTAT };
225 
226  public:
231  PathInfo();
232  explicit
233  PathInfo( const Pathname & path, Mode initial = STAT );
234  explicit
235  PathInfo( const std::string & path, Mode initial = STAT );
236  explicit
237  PathInfo( const char * path, Mode initial = STAT );
239 
241  ~PathInfo();
242 
244  const Pathname & path() const { return path_t; }
246  const std::string & asString() const { return path_t.asString(); }
248  const char * c_str() const { return path_t.asString().c_str(); }
250  Mode mode() const { return mode_e; }
252  int error() const { return error_i; }
253 
255  void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
257  void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
258 
260  bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
262  bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
264  bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
265 
267  bool stat() { setMode( STAT ); return operator()(); }
269  bool lstat() { setMode( LSTAT ); return operator()(); }
271  bool operator()();
272 
273  public:
274 
279  bool isExist() const { return !error_i; }
280 
285  FileType fileType() const;
286 
287  bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
288  bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
289  bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
290  bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
291  bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
292  bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
293  bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
294 
295  // permission
296  bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
297  bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
298  bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
299 
300  bool isR() const { return isRUsr(); }
301  bool isW() const { return isWUsr(); }
302  bool isX() const { return isXUsr(); }
303 
304  bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
305  bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
306  bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
307 
308  bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
309  bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
310  bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
311 
312  bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
313  bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
314  bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
315 
316  bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
317  bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
318 
319  mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
320  mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
321  mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
322  mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
323 
324  mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
326 
328  StatMode asStatMode() const { return st_mode(); }
329 
330  nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
331 
334  uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
335  gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
337 
341  mode_t userMay() const;
342 
343  bool userMayR() const { return( userMay() & 04 ); }
344  bool userMayW() const { return( userMay() & 02 ); }
345  bool userMayX() const { return( userMay() & 01 ); }
346 
347  bool userMayRW() const { return( (userMay() & 06) == 06 ); }
348  bool userMayRX() const { return( (userMay() & 05) == 05 ); }
349  bool userMayWX() const { return( (userMay() & 03) == 03 ); }
350 
351  bool userMayRWX() const { return( userMay() == 07 ); }
353 
356  ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
357  dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
358  dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
359 
360  unsigned int devMajor() const;
361  unsigned int devMinor() const;
363 
366  off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
367  unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
368  unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
370 
373  time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
374  time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
375  time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
377 
378  private:
380  struct stat statbuf_C;
382  int error_i;
383  };
385 
387  extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
388 
390 
392 
401  int mkdir( const Pathname & path, unsigned mode = 0755 );
402 
410  int assert_dir( const Pathname & path, unsigned mode = 0755 );
411 
417  int rmdir( const Pathname & path );
418 
425  int recursive_rmdir( const Pathname & path );
426 
433  int clean_dir( const Pathname & path );
434 
442  int copy_dir( const Pathname & srcpath, const Pathname & destpath );
443 
452  int copy_dir_content( const Pathname & srcpath, const Pathname & destpath);
453 
466  int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r );
467 
480  int readdir( std::list<std::string> & retlist,
481  const Pathname & path, bool dots = true );
482 
495  int readdir( std::list<Pathname> & retlist,
496  const Pathname & path, bool dots = true );
497 
499  struct DirEntry {
500  std::string name;
502  DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
503  : name( name_r )
504  , type( type_r )
505  {}
506 
507  bool operator==( const DirEntry &rhs ) const;
508  };
509 
510  inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
511  { return str << '[' << obj.type << "] " << obj.name; }
512 
514  typedef std::list<DirEntry> DirContent;
515 
516  std::ostream & operator<<( std::ostream & str, const DirContent & obj );
517 
528  int readdir( DirContent & retlist, const Pathname & path,
529  bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
530 
536  int is_empty_dir(const Pathname & path);
537 
539 
541 
550  int assert_file( const Pathname & path, unsigned mode = 0644 );
554  int assert_file_mode( const Pathname & path, unsigned mode = 0644 );
555 
562  int touch (const Pathname & path);
563 
569  int unlink( const Pathname & path );
570 
579  int rename( const Pathname & oldpath, const Pathname & newpath );
580 
607  int exchange( const Pathname & lpath, const Pathname & rpath );
608 
615  int copy( const Pathname & file, const Pathname & dest );
616 
623  int symlink( const Pathname & oldpath, const Pathname & newpath );
624 
631  int hardlink( const Pathname & oldpath, const Pathname & newpath );
632 
638  int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
639 
646  int readlink( const Pathname & symlink_r, Pathname & target_r );
648  inline Pathname readlink( const Pathname & symlink_r )
649  {
650  Pathname target;
651  readlink( symlink_r, target );
652  return target;
653  }
654 
667  Pathname expandlink( const Pathname & path_r );
668 
675  int copy_file2dir( const Pathname & file, const Pathname & dest );
677 
679 
688  std::string md5sum( const Pathname & file );
689 
695  std::string sha1sum( const Pathname & file );
697 
703  std::string checksum( const Pathname & file, const std::string &algorithm );
704 
710  bool is_checksum( const Pathname & file, const CheckSum &checksum );
711 
713 
720  int chmod( const Pathname & path, mode_t mode );
721 
727  int addmod( const Pathname & path, mode_t mode );
728 
734  int delmod( const Pathname & path, mode_t mode );
736 
738 
746 
747  ZIP_TYPE zipType( const Pathname & file );
748 
756  int erase( const Pathname & path );
757 
765  ByteCount df( const Pathname & path );
766 
772  mode_t getUmask();
773 
780  inline mode_t applyUmaskTo( mode_t mode_r )
781  { return mode_r & ~getUmask(); }
783 
785  } // namespace filesystem
787 
789  using filesystem::PathInfo;
790 
792 } // namespace zypp
794 #endif // ZYPP_PATHINFO_H
Store and operate with byte count.
Definition: ByteCount.h:31
Simple cache remembering device/inode to detect hardlinks.
Definition: PathInfo.h:186
void clear()
Clear cache.
Definition: PathInfo.h:192
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition: PathInfo.h:199
std::map< dev_t, std::set< ino_t > > _devino
Definition: PathInfo.h:204
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:219
nlink_t nlink() const
Definition: PathInfo.h:330
const std::string & asString() const
Return current Pathname as String.
Definition: PathInfo.h:246
mode_t perm() const
Definition: PathInfo.h:322
bool lstat(const Pathname &path)
LSTAT path.
Definition: PathInfo.h:262
bool userMayRW() const
Definition: PathInfo.h:347
mode_t uperm() const
Definition: PathInfo.h:319
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition: PathInfo.h:328
friend std::ostream & operator<<(std::ostream &str, const PathInfo &obj)
Definition: PathInfo.cc:261
time_t mtime() const
Definition: PathInfo.h:374
unsigned long blksize() const
Definition: PathInfo.h:367
Mode mode() const
Return current stat Mode.
Definition: PathInfo.h:250
bool stat()
STAT current path.
Definition: PathInfo.h:267
time_t ctime() const
Definition: PathInfo.h:375
bool isPerm(mode_t m) const
Definition: PathInfo.h:316
bool userMayRX() const
Definition: PathInfo.h:348
mode_t st_mode() const
Definition: PathInfo.h:324
bool userMayRWX() const
Definition: PathInfo.h:351
void setPath(const Pathname &path)
Set a new Pathname.
Definition: PathInfo.h:255
bool operator()()
Restat current path using current mode.
Definition: PathInfo.cc:188
Mode
stat() or lstat()
Definition: PathInfo.h:224
unsigned int devMinor() const
Definition: PathInfo.cc:251
FileType fileType() const
Definition: PathInfo.cc:212
time_t atime() const
Definition: PathInfo.h:373
mode_t gperm() const
Definition: PathInfo.h:320
void setMode(Mode mode)
Set a new Mode .
Definition: PathInfo.h:257
bool userMayWX() const
Definition: PathInfo.h:349
bool lstat()
LSTAT current path.
Definition: PathInfo.h:269
const char * c_str() const
Return current Pathname as C-string.
Definition: PathInfo.h:248
mode_t userMay() const
Returns current users permission ([0-7])
Definition: PathInfo.cc:224
unsigned long blocks() const
Definition: PathInfo.h:368
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:279
mode_t operm() const
Definition: PathInfo.h:321
bool hasPerm(mode_t m) const
Definition: PathInfo.h:317
unsigned int devMajor() const
Definition: PathInfo.cc:241
bool operator()(const Pathname &path)
Restat path using current mode.
Definition: PathInfo.h:264
const Pathname & path() const
Return current Pathname.
Definition: PathInfo.h:244
int error() const
Return error returned from last stat/lstat call.
Definition: PathInfo.h:252
bool stat(const Pathname &path)
STAT path.
Definition: PathInfo.h:260
const std::string & asString() const
String representation.
Definition: Pathname.h:91
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.h:79
bool isR() const
Short for isRUsr().
Definition: PathInfo.h:110
bool isW() const
Short for isWUsr().
Definition: PathInfo.h:112
mode_t st_mode() const
Return the mode_t value.
Definition: PathInfo.h:158
mode_t gperm() const
Definition: PathInfo.h:152
bool isVtx() const
Sticky bit.
Definition: PathInfo.h:138
mode_t perm() const
Definition: PathInfo.h:154
bool hasPerm(mode_t m) const
Test for set permission bits.
Definition: PathInfo.h:146
bool isUid() const
Set UID bit.
Definition: PathInfo.h:134
mode_t operm() const
Definition: PathInfo.h:153
bool isX() const
Short for isXUsr().
Definition: PathInfo.h:114
mode_t uperm() const
Definition: PathInfo.h:151
friend std::ostream & operator<<(std::ostream &str, const StatMode &obj)
Definition: PathInfo.cc:95
FileType fileType() const
Definition: PathInfo.cc:70
bool isPerm(mode_t m) const
Test for equal permission bits.
Definition: PathInfo.h:144
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from ::stat.
Definition: PathInfo.h:84
bool isGid() const
Set GID bit.
Definition: PathInfo.h:136
String related utilities and Regular expression matching.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition: PathInfo.cc:1032
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:991
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition: PathInfo.cc:1050
FileType
File type information.
Definition: PathInfo.h:54
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:885
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition: PathInfo.cc:628
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition: PathInfo.h:780
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:576
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition: PathInfo.cc:1095
int unlink(const Pathname &path)
Like 'unlink'.
Definition: PathInfo.cc:640
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition: PathInfo.cc:682
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
Definition: PathInfo.cc:305
int rmdir(const Pathname &path)
Like 'rmdir'.
Definition: PathInfo.cc:366
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
Definition: PathInfo.cc:1003
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
Definition: PathInfo.cc:1123
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition: PathInfo.cc:412
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
Definition: PathInfo.cc:463
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
Definition: PathInfo.cc:504
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
Definition: PathInfo.cc:32
int erase(const Pathname &path)
Erase whatever happens to be located at path (file or directory).
Definition: PathInfo.cc:1013
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition: PathInfo.cc:1041
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like '::link'.
Definition: PathInfo.cc:809
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition: PathInfo.cc:864
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition: PathInfo.cc:319
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition: PathInfo.cc:930
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition: PathInfo.cc:1142
mode_t getUmask()
Get the current umask (file mode creation mask)
Definition: PathInfo.cc:1111
int touch(const Pathname &path)
Change file's modification and access times.
Definition: PathInfo.cc:1174
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Definition: Glob.cc:53
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:823
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:514
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition: PathInfo.cc:964
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:696
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:981
ZIP_TYPE zipType(const Pathname &file)
Definition: PathInfo.cc:1064
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition: PathInfo.cc:760
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition: PathInfo.h:745
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
Definition: PathInfo.cc:795
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition: PathInfo.cc:442
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
Listentry returned by readdir.
Definition: PathInfo.h:499
bool operator==(const DirEntry &rhs) const
Definition: PathInfo.cc:601
DirEntry(const std::string &name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition: PathInfo.h:502