yast2-core
PathInfo.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: PathInfo.h
14 
15  Author: Michael Andres <ma@suse.de>
16  Maintainer: Michael Andres <ma@suse.de>
17 
18 /-*/
19 #ifndef PathInfo_h
20 #define PathInfo_h
21 
22 extern "C"
23 {
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 }
30 
31 #include <cerrno>
32 #include <iosfwd>
33 #include <list>
34 #include <set>
35 #include <map>
36 
37 #include <y2util/Pathname.h>
38 
40 //
41 // CLASS NAME : PathInfo
45 class PathInfo {
46 
47  friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
48 
49  public:
50 
51  enum Mode { STAT, LSTAT };
52 
53  enum file_type {
54  NOT_AVAIL = 0x00, // no typeinfo available
55  NOT_EXIST = 0x01, // file does not exist
56  T_FILE = 0x02,
57  T_DIR = 0x04,
58  T_CHARDEV = 0x08,
59  T_BLOCKDEV = 0x10,
60  T_FIFO = 0x20,
61  T_LINK = 0x40,
62  T_SOCKET = 0x80
63  };
64  friend std::ostream & operator<<( std::ostream & str, file_type obj );
65 
69  class stat_mode;
70 
74  class devino_cache;
75 
76  private:
77 
79 
80  struct stat statbuf_C;
82  int error_i;
83 
84  public:
85 
86  PathInfo( const Pathname & path = "", Mode initial = STAT );
87  PathInfo( const std::string & path, Mode initial = STAT );
88  PathInfo( const char * path, Mode initial = STAT );
89  virtual ~PathInfo();
90 
91  const Pathname & path() const { return path_t; }
92  const std::string & asString() const { return path_t.asString(); }
93  Mode mode() const { return mode_e; }
94  int error() const { return error_i; }
95 
96  void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
97  void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
98 
99  bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
100  bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
101  bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
102 
103  bool stat() { setMode( STAT ); return operator()(); }
104  bool lstat() { setMode( LSTAT ); return operator()(); }
105  bool operator()();
106 
107  public:
108 
109  bool isExist() const { return !error_i; }
110 
111  // file type
112  file_type fileType() const;
113 
114  bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
115  bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
116  bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
117  bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
118  bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
119  bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
120  bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
121 
122  nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
123 
124  // owner
125  uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
126  gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
127 
128  // permission
129  bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
130  bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
131  bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
132 
133  bool isR() const { return isRUsr(); }
134  bool isW() const { return isWUsr(); }
135  bool isX() const { return isXUsr(); }
136 
137  bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
138  bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
139  bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
140 
141  bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
142  bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
143  bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
144 
145  bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
146  bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
147  bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
148 
149  mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
150  mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
151  mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
152  mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
153 
154  bool isPerm ( mode_t m ) const { return (m == perm()); }
155  bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
156 
157  mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
158 
159  // permission according to current uid/gid (returns [0-7])
160  mode_t userMay() const;
161 
162  bool userMayR() const { return( userMay() & 01 ); }
163  bool userMayW() const { return( userMay() & 02 ); }
164  bool userMayX() const { return( userMay() & 04 ); }
165 
166  bool userMayRW() const { return( (userMay() & 03) == 03 ); }
167  bool userMayRX() const { return( (userMay() & 05) == 05 ); }
168  bool userMayWX() const { return( (userMay() & 06) == 06 ); }
169 
170  bool userMayRWX() const { return( userMay() == 07 ); }
171 
172  // device
173  dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
174  dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
175  ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
176 
177  // size
178  off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
179  unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
180  unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
181 
182  // time
183  time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
184  time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
185  time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
186 
187  public:
188 
190  // convenience stuff
192  // static functions as they may or may not invalidate any stat info
193  // stored by a PathiInfo.
195 
197  // Directories
199 
207  static int mkdir( const Pathname & path, unsigned mode = 0755 );
208 
216  static int assert_dir( const Pathname & path, unsigned mode = 0755 );
217 
223  static int rmdir( const Pathname & path );
224 
231  static int recursive_rmdir( const Pathname & path );
232 
239  static int clean_dir( const Pathname & path );
240 
248  static int copy_dir( const Pathname & srcpath, const Pathname & destpath );
249 
257  static int readdir( std::list<std::string> & retlist,
258  const Pathname & path, bool dots = true );
259 
260  struct direntry {
261  std::string name;
263  direntry( const std::string & name_r = std::string(), file_type type_r = NOT_AVAIL )
264  : name( name_r )
265  , type( type_r )
266  {}
267  };
268 
269  typedef std::list<direntry> dircontent;
270 
281  static int readdir( dircontent & retlist, const Pathname & path,
282  bool dots = true, Mode statmode = STAT );
283 
285  // Files
287 
293  static int unlink( const Pathname & path );
294 
300  static int rename( const Pathname & oldpath, const Pathname & newpath );
301 
308  static int copy( const Pathname & file, const Pathname & dest );
309 
316  static int symlink( const Pathname & oldpath, const Pathname & newpath );
317 
324  static int hardlink( const Pathname & oldpath, const Pathname & newpath );
325 
332  static int copy_file2dir( const Pathname & file, const Pathname & dest );
333 
335  //
337 
343  static int erase( const Pathname & path );
344 
346  // permissions
348 
354  static int chmod( const Pathname & path, mode_t mode );
355 
357  // magic
359 
366 
367  static ZIP_TYPE zipType( const Pathname & file );
368 };
369 
371 
373 //
374 // CLASS NAME : PathInfo::stat_mode
379  friend std::ostream & operator<<( std::ostream & str, const stat_mode & obj );
380  private:
381  mode_t _mode;
382  public:
383  stat_mode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {}
384  public:
385  // file type
386  file_type fileType() const;
387 
388  bool isFile() const { return S_ISREG( _mode ); }
389  bool isDir () const { return S_ISDIR( _mode ); }
390  bool isLink() const { return S_ISLNK( _mode ); }
391  bool isChr() const { return S_ISCHR( _mode ); }
392  bool isBlk() const { return S_ISBLK( _mode ); }
393  bool isFifo() const { return S_ISFIFO( _mode ); }
394  bool isSock() const { return S_ISSOCK( _mode ); }
395 
396  // permission
397  bool isRUsr() const { return (_mode & S_IRUSR); }
398  bool isWUsr() const { return (_mode & S_IWUSR); }
399  bool isXUsr() const { return (_mode & S_IXUSR); }
400 
401  bool isR() const { return isRUsr(); }
402  bool isW() const { return isWUsr(); }
403  bool isX() const { return isXUsr(); }
404 
405  bool isRGrp() const { return (_mode & S_IRGRP); }
406  bool isWGrp() const { return (_mode & S_IWGRP); }
407  bool isXGrp() const { return (_mode & S_IXGRP); }
408 
409  bool isROth() const { return (_mode & S_IROTH); }
410  bool isWOth() const { return (_mode & S_IWOTH); }
411  bool isXOth() const { return (_mode & S_IXOTH); }
412 
413  bool isUid() const { return (_mode & S_ISUID); }
414  bool isGid() const { return (_mode & S_ISGID); }
415  bool isVtx() const { return (_mode & S_ISVTX); }
416 
417  mode_t uperm() const { return (_mode & S_IRWXU); }
418  mode_t gperm() const { return (_mode & S_IRWXG); }
419  mode_t operm() const { return (_mode & S_IRWXO); }
420  mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
421 
422  bool isPerm ( mode_t m ) const { return (m == perm()); }
423  bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
424 
425  mode_t st_mode() const { return _mode; }
426 };
427 
429 
431 //
432 // CLASS NAME : PathInfo::devino_cache
447 
448  private:
449 
450  std::map<dev_t,std::set<ino_t> > _devino;
451 
452  public:
457 
461  void clear() { _devino.clear(); }
462 
468  bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
469  return _devino[dev_r].insert( ino_r ).second;
470  }
471 };
472 
474 
476 
477 #endif // PathInfo_h
unsigned long blksize() const
Definition: PathInfo.h:179
bool userMayRW() const
Definition: PathInfo.h:166
gid_t group() const
Definition: PathInfo.h:126
static int rmdir(const Pathname &path)
Definition: PathInfo.cc:348
Definition: PathInfo.h:57
bool isXOth() const
Definition: PathInfo.h:143
struct stat statbuf_C
Definition: PathInfo.h:80
std::list< direntry > dircontent
Definition: PathInfo.h:269
static int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Definition: PathInfo.cc:433
nlink_t nlink() const
Definition: PathInfo.h:122
#define str
Definition: scanner.cc:997
bool userMayRWX() const
Definition: PathInfo.h:170
bool isRGrp() const
Definition: PathInfo.h:137
bool isXUsr() const
Definition: PathInfo.h:399
mode_t uperm() const
Definition: PathInfo.h:417
dev_t dev() const
Definition: PathInfo.h:173
static int unlink(const Pathname &path)
Definition: PathInfo.cc:541
mode_t st_mode() const
Definition: PathInfo.h:157
bool isRGrp() const
Definition: PathInfo.h:405
const Pathname & path() const
Definition: PathInfo.h:91
Definition: PathInfo.h:51
static int copy(const Pathname &file, const Pathname &dest)
Definition: PathInfo.cc:575
Definition: PathInfo.h:60
Definition: PathInfo.h:260
bool isFile() const
Definition: PathInfo.h:388
file_type type
Definition: PathInfo.h:262
bool isDir() const
Definition: PathInfo.h:115
Mode mode_e
Definition: PathInfo.h:81
bool isW() const
Definition: PathInfo.h:402
dev_t rdev() const
Definition: PathInfo.h:174
mode_t userMay() const
Definition: PathInfo.cc:141
bool isPerm(mode_t m) const
Definition: PathInfo.h:422
static int symlink(const Pathname &oldpath, const Pathname &newpath)
Definition: PathInfo.cc:612
friend std::ostream & operator<<(std::ostream &str, const PathInfo &obj)
Definition: PathInfo.cc:161
bool isFifo() const
Definition: PathInfo.h:393
Definition: Pathname.h:31
bool userMayX() const
Definition: PathInfo.h:164
const std::string & asString() const
Definition: PathInfo.h:92
bool userMayR() const
Definition: PathInfo.h:162
Definition: PathInfo.h:61
mode_t _mode
Definition: PathInfo.h:381
static int readdir(std::list< std::string > &retlist, const Pathname &path, bool dots=true)
Definition: PathInfo.cc:477
Definition: PathInfo.h:365
Definition: PathInfo.h:59
bool isX() const
Definition: PathInfo.h:135
bool isWGrp() const
Definition: PathInfo.h:406
mode_t uperm() const
Definition: PathInfo.h:149
bool hasPerm(mode_t m) const
Definition: PathInfo.h:423
Definition: PathInfo.h:58
uid_t owner() const
Definition: PathInfo.h:125
bool isSock() const
Definition: PathInfo.h:120
bool isVtx() const
Definition: PathInfo.h:147
bool isExist() const
Definition: PathInfo.h:109
bool lstat(const Pathname &path)
Definition: PathInfo.h:100
ino_t ino() const
Definition: PathInfo.h:175
bool isRUsr() const
Definition: PathInfo.h:129
bool hasPerm(mode_t m) const
Definition: PathInfo.h:155
bool isLink() const
Definition: PathInfo.h:390
static int erase(const Pathname &path)
Definition: PathInfo.cc:683
bool isUid() const
Definition: PathInfo.h:145
friend std::ostream & operator<<(std::ostream &str, const stat_mode &obj)
Definition: PathInfo.cc:240
Definition: PathInfo.h:365
static int copy_file2dir(const Pathname &file, const Pathname &dest)
Definition: PathInfo.cc:646
bool stat()
Definition: PathInfo.h:103
bool isR() const
Definition: PathInfo.h:133
bool isRUsr() const
Definition: PathInfo.h:397
bool isBlk() const
Definition: PathInfo.h:392
Definition: PathInfo.h:365
time_t atime() const
Definition: PathInfo.h:183
bool isSock() const
Definition: PathInfo.h:394
PathInfo(const Pathname &path="", Mode initial=STAT)
Definition: PathInfo.cc:41
bool isDir() const
Definition: PathInfo.h:389
Definition: PathInfo.h:55
devino_cache()
Definition: PathInfo.h:456
bool isWUsr() const
Definition: PathInfo.h:398
bool isXGrp() const
Definition: PathInfo.h:407
Simple cache remembering device/inode to detect hardlinks.
Definition: PathInfo.h:446
bool isR() const
Definition: PathInfo.h:401
mode_t perm() const
Definition: PathInfo.h:420
bool isVtx() const
Definition: PathInfo.h:415
bool lstat()
Definition: PathInfo.h:104
static int recursive_rmdir(const Pathname &path)
Definition: PathInfo.cc:365
bool isChr() const
Definition: PathInfo.h:391
mode_t operm() const
Definition: PathInfo.h:151
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Definition: PathInfo.h:468
bool userMayWX() const
Definition: PathInfo.h:168
bool operator()()
Definition: PathInfo.cc:101
void setMode(Mode mode)
Definition: PathInfo.h:97
Definition: PathInfo.h:56
Definition: PathInfo.h:62
virtual ~PathInfo()
Definition: PathInfo.cc:89
static int mkdir(const Pathname &path, unsigned mode=0755)
Definition: PathInfo.cc:288
bool isLink() const
Definition: PathInfo.h:116
Wrapper class for ::stat/::lstat and other file/directory related operations.
Definition: PathInfo.h:45
file_type fileType() const
Definition: PathInfo.cc:126
void setPath(const Pathname &path)
Definition: PathInfo.h:96
off_t size() const
Definition: PathInfo.h:178
bool stat(const Pathname &path)
Definition: PathInfo.h:99
bool isX() const
Definition: PathInfo.h:403
bool isWOth() const
Definition: PathInfo.h:142
mode_t perm() const
Definition: PathInfo.h:152
time_t ctime() const
Definition: PathInfo.h:185
int error_i
Definition: PathInfo.h:82
bool isGid() const
Definition: PathInfo.h:414
bool isXUsr() const
Definition: PathInfo.h:131
bool isWUsr() const
Definition: PathInfo.h:130
static int clean_dir(const Pathname &path)
Definition: PathInfo.cc:403
int error() const
Definition: PathInfo.h:94
file_type
Definition: PathInfo.h:53
stat_mode(const mode_t &mode_r=0)
Definition: PathInfo.h:383
static int rename(const Pathname &oldpath, const Pathname &newpath)
Definition: PathInfo.cc:558
bool isW() const
Definition: PathInfo.h:134
bool isWOth() const
Definition: PathInfo.h:410
bool isUid() const
Definition: PathInfo.h:413
void clear()
Definition: PathInfo.h:461
bool isBlk() const
Definition: PathInfo.h:118
std::string name
Definition: PathInfo.h:261
unsigned long blocks() const
Definition: PathInfo.h:180
mode_t operm() const
Definition: PathInfo.h:419
bool isPerm(mode_t m) const
Definition: PathInfo.h:154
Pathname path_t
Definition: PathInfo.h:74
Definition: PathInfo.h:51
Mode
Definition: PathInfo.h:51
file_type fileType() const
Definition: PathInfo.cc:212
bool userMayRX() const
Definition: PathInfo.h:167
mode_t st_mode() const
Definition: PathInfo.h:425
std::map< dev_t, std::set< ino_t > > _devino
Definition: PathInfo.h:450
mode_t gperm() const
Definition: PathInfo.h:418
bool isGid() const
Definition: PathInfo.h:146
bool isChr() const
Definition: PathInfo.h:117
bool userMayW() const
Definition: PathInfo.h:163
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.h:378
bool isROth() const
Definition: PathInfo.h:141
static ZIP_TYPE zipType(const Pathname &file)
Definition: PathInfo.cc:718
time_t mtime() const
Definition: PathInfo.h:184
bool isXGrp() const
Definition: PathInfo.h:139
const std::string & asString() const
Definition: Pathname.h:64
bool operator()(const Pathname &path)
Definition: PathInfo.h:101
mode_t gperm() const
Definition: PathInfo.h:150
bool isXOth() const
Definition: PathInfo.h:411
Mode mode() const
Definition: PathInfo.h:93
bool isWGrp() const
Definition: PathInfo.h:138
bool isROth() const
Definition: PathInfo.h:409
ZIP_TYPE
Definition: PathInfo.h:365
static int assert_dir(const Pathname &path, unsigned mode=0755)
Definition: PathInfo.cc:305
bool isFifo() const
Definition: PathInfo.h:119
static int chmod(const Pathname &path, mode_t mode)
Definition: PathInfo.cc:703
static int hardlink(const Pathname &oldpath, const Pathname &newpath)
Definition: PathInfo.cc:629
Definition: PathInfo.h:54
bool isFile() const
Definition: PathInfo.h:114

Generated on a sunny day for yast2-core by doxygen 1.8.5