libcamgm
PathInfo.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | _ _ _ _ __ _ |
4 | | | | | | \_/ | / \ | | |
5 | | | | | | |_| | / /\ \ | | |
6 | | |__ | | | | | | / ____ \ | |__ |
7 | |____||_| |_| |_|/ / \ \|____| |
8 | |
9 | core library |
10 | |
11 | (C) SUSE Linux Products GmbH |
12 \----------------------------------------------------------------------/
13 
14  File: PathInfo.hpp
15 
16  Maintainer: Michael Calmer
17 
18 /----------------------------------------------------------------------\
19 | |
20 | __ __ ____ _____ ____ |
21 | \ \ / /_ _/ ___|_ _|___ \ |
22 | \ V / _` \___ \ | | __) | |
23 | | | (_| |___) || | / __/ |
24 | |_|\__,_|____/ |_| |_____| |
25 | |
26 | core system |
27 | (C) SuSE GmbH |
28 \----------------------------------------------------------------------/
29 
30  File: PathInfo.h
31 
32  Author: Michael Andres <ma@suse.de>
33  Maintainer: Michael Andres <ma@suse.de>
34 
35 /-*/
40 #ifndef CA_MGM_PATH_PATHINFO_HPP
41 #define CA_MGM_PATH_PATHINFO_HPP
42 
43 #include <ca-mgm/config.h>
44 #include <ca-mgm/PathName.hpp>
45 
46 #include <cerrno>
47 #include <iosfwd>
48 #include <set>
49 #include <map>
50 
51 extern "C"
52 {
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <dirent.h>
58 }
59 
60 namespace CA_MGM_NAMESPACE {
61 
62 namespace path {
63 
64 
100 class PathInfo {
101 
102  public:
103 
104  enum Mode { E_STAT, E_LSTAT };
105 
106  enum FileType {
107  NOT_AVAIL = 0x00,
108  NOT_EXIST = 0x01,
109  T_FILE = 0x02,
110  T_DIR = 0x04,
111  T_CHARDEV = 0x08,
112  T_BLOCKDEV = 0x10,
113  T_FIFO = 0x20,
114  T_LINK = 0x40,
115  T_SOCKET = 0x80
116  };
117 
133  friend std::ostream & operator<<( std::ostream & str, FileType obj );
134  friend std::ostream & operator<<( std::ostream & str, const PathInfo &obj);
135 
136 
140  class StatMode;
141 
146  class DevInoCache;
147 
148  private:
149 
151 
152  struct stat m_statbuf_C;
154  int m_error;
155 
156  public:
157 
158 
166  PathInfo( const PathName & path = "", Mode initial = E_STAT );
167 
176  PathInfo( const std::string & path, Mode initial = E_STAT );
177 
185  PathInfo( const char * path, Mode initial = E_STAT );
186 
190  virtual ~PathInfo();
191 
196  const PathName path() const { return m_path; }
197 
204  std::string toString() const { return m_path.toString(); }
211  Mode mode() const { return m_mode; }
212 
224  int error() const { return m_error; }
225 
230  void setPath( const PathName & path );
231 
244  void setMode( Mode mode );
245 
246 
259  bool stat ( const PathName & path );
260 
273  bool lstat ( const PathName & path );
274 
275 
290  bool operator()( const PathName & path );
291 
292 
303  bool stat();
304 
315  bool lstat();
316 
326  bool operator()();
327 
328 
329 
337  bool exists() const { return !m_error; }
338 
342  // @{
349  FileType fileType() const;
350 
351 
355  bool isFile() const;
356 
360  bool isDir () const;
361 
365  bool isLink() const;
366 
370  bool isChr() const;
371 
375  bool isBlk() const;
376 
380  bool isFifo() const;
381 
385  bool isSock() const;
386 
387  // @}
388 
395  nlink_t nlink() const;
396 
404  uid_t owner() const;
405 
413  gid_t group() const;
414 
422  // @{
424  bool isRUsr() const;
426  bool isWUsr() const;
428  bool isXUsr() const;
429 
431  bool isR() const;
433  bool isW() const;
435  bool isX() const;
436 
438  bool isRGrp() const;
440  bool isWGrp() const;
442  bool isXGrp() const;
443 
445  bool isROth() const;
447  bool isWOth() const;
449  bool isXOth() const;
450 
452  bool isUid() const;
454  bool isGid() const;
456  bool isVtx() const;
457 
464  mode_t uperm() const;
465 
472  mode_t gperm() const;
473 
480  mode_t operm() const;
481 
486  mode_t perm() const;
487 
488 
498  bool isPerm ( mode_t m ) const;
499 
509  bool hasPerm( mode_t m ) const;
510 
526  mode_t st_mode() const;
527 
533  mode_t userMay() const;
534 
535 
542  bool userMayR() const;
543 
550  bool userMayW() const;
551 
558  bool userMayX() const;
559 
567  bool userMayRW() const;
568 
576  bool userMayRX() const;
577 
585  bool userMayWX() const;
586 
594  bool userMayRWX() const;
595 
596  // @}
597 
598  // device
599 
605  dev_t dev() const;
606 
612  dev_t rdev() const;
613 
620  ino_t ino() const;
621 
633  ::off_t size() const;
634 
641  blksize_t blksize() const;
642 
649  blkcnt_t blocks() const;
650 
652  // @{
658  time_t atime() const; /* time of last access */
659 
665  time_t mtime() const; /* time of last modification */
666 
672  time_t ctime() const;
673 
674  // @}
675 };
676 
678 
685 
686  friend std::ostream & operator<<( std::ostream & str, const PathInfo::StatMode & obj );
687 
688  private:
689  mode_t _mode;
690  public:
691  StatMode( const mode_t & mode_r = 0 ) : _mode( mode_r ) {}
692 
700  // @{
701  FileType fileType() const;
702 
703  bool isFile() const;
704  bool isDir () const;
705  bool isLink() const;
706  bool isChr() const;
707  bool isBlk() const;
708  bool isFifo() const;
709  bool isSock() const;
710  // @}
711 
713  // @{
714  bool isRUsr() const;
715  bool isWUsr() const;
716  bool isXUsr() const;
717 
718  bool isR() const;
719  bool isW() const;
720  bool isX() const;
721 
722  bool isRGrp() const;
723  bool isWGrp() const;
724  bool isXGrp() const;
725 
726  bool isROth() const;
727  bool isWOth() const;
728  bool isXOth() const;
729 
730  bool isUid() const;
731  bool isGid() const;
732  bool isVtx() const;
733 
734  mode_t uperm() const;
735  mode_t gperm() const;
736  mode_t operm() const;
737  mode_t perm() const;
738 
739  bool isPerm( mode_t m ) const;
740  bool hasPerm( mode_t m ) const;
741 
742  mode_t st_mode() const;
743 
744  // @}
745 };
746 
748 
764 
765  private:
766 
767  std::map<dev_t,std::set<ino_t> > _devino;
768 
769  public:
774 
778  void clear() { _devino.clear(); }
779 
788  bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
789  return _devino[dev_r].insert( ino_r ).second;
790  }
791 };
792 
794 
796 
797 }
798 }
799 
800 #endif // CA_MGM_PATH_PATHINFO_HPP
Simple cache remembering device/inode to detect hard links.
Definition: PathInfo.hpp:763
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition: PathInfo.hpp:788
PathName manipulation class.
Definition: PathName.hpp:65
void clear()
Clear cache.
Definition: PathInfo.hpp:778
int m_error
Definition: PathInfo.hpp:154
FileType
Definition: PathInfo.hpp:106
int error() const
Returns the internal error code.
Definition: PathInfo.hpp:224
Mode mode() const
Returns the stat mode which is currently set in this PathInfo object.
Definition: PathInfo.hpp:211
bool exists() const
True if the path that this PathInfo object points to exists.
Definition: PathInfo.hpp:337
mode_t _mode
Definition: PathInfo.hpp:689
LiMaL path name manipulation utilities.
std::string toString() const
Returns a std::string formed from the PathName object this this PathInfo object holds.
Definition: PathInfo.hpp:204
std::ostream & operator<<(std::ostream &ostr, const PathName &path)
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.hpp:684
Wrapper class for ::stat/::lstat and other file/directory related operations.
Definition: PathInfo.hpp:100
Mode
Definition: PathInfo.hpp:104
DevInoCache()
Constructor.
Definition: PathInfo.hpp:773
Mode m_mode
Definition: PathInfo.hpp:153
std::string toString() const
Returns the complete path this PathName object holds.
const PathName path() const
Returns the PathName object this PathInfo object holds.
Definition: PathInfo.hpp:196
StatMode(const mode_t &mode_r=0)
Definition: PathInfo.hpp:691
PathName m_path
Definition: PathInfo.hpp:146
std::map< dev_t, std::set< ino_t > > _devino
Definition: PathInfo.hpp:767