libzypp  11.13.5
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 major() const;
363  unsigned int minor() 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 
544  typedef std::list<DirEntry> DirContent;
545 
556  int readdir( DirContent & retlist, const Pathname & path,
557  bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
558 
564  int is_empty_dir(const Pathname & path);
565 
567 
569 
578  int assert_file( const Pathname & path, unsigned mode = 0644 );
579 
586  int touch (const Pathname & path);
587 
593  int unlink( const Pathname & path );
594 
600  int rename( const Pathname & oldpath, const Pathname & newpath );
601 
628  int exchange( const Pathname & lpath, const Pathname & rpath );
629 
636  int copy( const Pathname & file, const Pathname & dest );
637 
644  int symlink( const Pathname & oldpath, const Pathname & newpath );
645 
652  int hardlink( const Pathname & oldpath, const Pathname & newpath );
653 
659  int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
660 
667  int readlink( const Pathname & symlink_r, Pathname & target_r );
669  inline Pathname readlink( const Pathname & symlink_r )
670  {
671  Pathname target;
672  readlink( symlink_r, target );
673  return target;
674  }
675 
688  Pathname expandlink( const Pathname & path_r );
689 
696  int copy_file2dir( const Pathname & file, const Pathname & dest );
698 
700 
709  std::string md5sum( const Pathname & file );
710 
716  std::string sha1sum( const Pathname & file );
718 
724  std::string checksum( const Pathname & file, const std::string &algorithm );
725 
731  bool is_checksum( const Pathname & file, const CheckSum &checksum );
732 
734 
741  int chmod( const Pathname & path, mode_t mode );
742 
748  int addmod( const Pathname & path, mode_t mode );
749 
755  int delmod( const Pathname & path, mode_t mode );
757 
759 
767 
768  ZIP_TYPE zipType( const Pathname & file );
769 
777  int erase( const Pathname & path );
778 
786  ByteCount df( const Pathname & path );
787 
793  mode_t getUmask();
794 
801  inline mode_t applyUmaskTo( mode_t mode_r )
802  { return mode_r & ~getUmask(); }
804 
806  } // namespace filesystem
808 
810  using filesystem::PathInfo;
811 
813 } // namespace zypp
815 #endif // ZYPP_PATHINFO_H