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

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