libzypp 17.31.6
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
15extern "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
34struct dirent;
35
37namespace zypp
38{
39
41
48 namespace filesystem
49 {
50
52
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,
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
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
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 devMajor() const;
363 unsigned int devMinor() 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:
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
468 int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r );
469
482 int readdir( std::list<std::string> & retlist,
483 const Pathname & path, bool dots = true );
484
497 int readdir( std::list<Pathname> & retlist,
498 const Pathname & path, bool dots = true );
499
501 struct DirEntry {
502 std::string name;
504 DirEntry( const std::string & name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
505 : name( name_r )
506 , type( type_r )
507 {}
508
509 DirEntry( struct dirent* entry );
510
511 bool operator==( const DirEntry &rhs ) const;
512 };
513
514 inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
515 { return str << '[' << obj.type << "] " << obj.name; }
516
518 typedef std::list<DirEntry> DirContent;
519
520 std::ostream & operator<<( std::ostream & str, const DirContent & obj );
521
532 int readdir( DirContent & retlist, const Pathname & path,
533 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT );
534
538 int dirForEachExt( const Pathname & dir_r, const function<bool(const Pathname &, const DirEntry &)> &fnc_r );
539
545 int is_empty_dir(const Pathname & path);
546
548
550
559 int assert_file( const Pathname & path, unsigned mode = 0644 );
563 int assert_file_mode( const Pathname & path, unsigned mode = 0644 );
564
571 int touch (const Pathname & path);
572
578 int unlink( const Pathname & path );
579
588 int rename( const Pathname & oldpath, const Pathname & newpath );
589
616 int exchange( const Pathname & lpath, const Pathname & rpath );
617
624 int copy( const Pathname & file, const Pathname & dest );
625
632 int symlink( const Pathname & oldpath, const Pathname & newpath );
633
640 int hardlink( const Pathname & oldpath, const Pathname & newpath );
641
647 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath );
648
655 int readlink( const Pathname & symlink_r, Pathname & target_r );
657 inline Pathname readlink( const Pathname & symlink_r )
658 {
659 Pathname target;
660 readlink( symlink_r, target );
661 return target;
662 }
663
676 Pathname expandlink( const Pathname & path_r );
677
684 int copy_file2dir( const Pathname & file, const Pathname & dest );
686
688
697 std::string md5sum( const Pathname & file );
698
704 std::string sha1sum( const Pathname & file );
706
712 std::string checksum( const Pathname & file, const std::string &algorithm );
713
719 bool is_checksum( const Pathname & file, const CheckSum &checksum );
720
722
729 int chmod( const Pathname & path, mode_t mode );
730
736 int addmod( const Pathname & path, mode_t mode );
737
743 int delmod( const Pathname & path, mode_t mode );
745
747
755
756 ZIP_TYPE zipType( const Pathname & file );
757
765 int erase( const Pathname & path );
766
774 ByteCount df( const Pathname & path );
775
781 mode_t getUmask();
782
789 inline mode_t applyUmaskTo( mode_t mode_r )
790 { return mode_r & ~getUmask(); }
792
794 } // namespace filesystem
796
798 using filesystem::PathInfo;
799
801} // namespace zypp
803#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:188
void clear()
Clear cache.
Definition: PathInfo.h:194
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition: PathInfo.h:201
std::map< dev_t, std::set< ino_t > > _devino
Definition: PathInfo.h:206
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
nlink_t nlink() const
Definition: PathInfo.h:332
mode_t perm() const
Definition: PathInfo.h:324
friend std::ostream & operator<<(std::ostream &str, const PathInfo &obj)
Definition: PathInfo.cc:261
bool lstat(const Pathname &path)
LSTAT path.
Definition: PathInfo.h:264
bool userMayRW() const
Definition: PathInfo.h:349
mode_t uperm() const
Definition: PathInfo.h:321
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition: PathInfo.h:330
time_t mtime() const
Definition: PathInfo.h:376
unsigned long blksize() const
Definition: PathInfo.h:369
Mode mode() const
Return current stat Mode.
Definition: PathInfo.h:252
bool stat()
STAT current path.
Definition: PathInfo.h:269
time_t ctime() const
Definition: PathInfo.h:377
bool isPerm(mode_t m) const
Definition: PathInfo.h:318
bool userMayRX() const
Definition: PathInfo.h:350
mode_t st_mode() const
Definition: PathInfo.h:326
bool userMayRWX() const
Definition: PathInfo.h:353
const Pathname & path() const
Return current Pathname.
Definition: PathInfo.h:246
void setPath(const Pathname &path)
Set a new Pathname.
Definition: PathInfo.h:257
bool operator()()
Restat current path using current mode.
Definition: PathInfo.cc:188
Mode
stat() or lstat()
Definition: PathInfo.h:226
unsigned int devMinor() const
Definition: PathInfo.cc:251
FileType fileType() const
Definition: PathInfo.cc:212
time_t atime() const
Definition: PathInfo.h:375
mode_t gperm() const
Definition: PathInfo.h:322
void setMode(Mode mode)
Set a new Mode .
Definition: PathInfo.h:259
bool userMayWX() const
Definition: PathInfo.h:351
bool lstat()
LSTAT current path.
Definition: PathInfo.h:271
mode_t userMay() const
Returns current users permission ([0-7])
Definition: PathInfo.cc:224
unsigned long blocks() const
Definition: PathInfo.h:370
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:281
mode_t operm() const
Definition: PathInfo.h:323
const char * c_str() const
Return current Pathname as C-string.
Definition: PathInfo.h:250
const std::string & asString() const
Return current Pathname as String.
Definition: PathInfo.h:248
bool hasPerm(mode_t m) const
Definition: PathInfo.h:319
unsigned int devMajor() const
Definition: PathInfo.cc:241
bool operator()(const Pathname &path)
Restat path using current mode.
Definition: PathInfo.h:266
int error() const
Return error returned from last stat/lstat call.
Definition: PathInfo.h:254
bool stat(const Pathname &path)
STAT path.
Definition: PathInfo.h:262
const std::string & asString() const
String representation.
Definition: Pathname.h:91
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.h:81
bool isR() const
Short for isRUsr().
Definition: PathInfo.h:112
bool isW() const
Short for isWUsr().
Definition: PathInfo.h:114
mode_t st_mode() const
Return the mode_t value.
Definition: PathInfo.h:160
mode_t gperm() const
Definition: PathInfo.h:154
bool isVtx() const
Sticky bit.
Definition: PathInfo.h:140
mode_t perm() const
Definition: PathInfo.h:156
bool hasPerm(mode_t m) const
Test for set permission bits.
Definition: PathInfo.h:148
bool isUid() const
Set UID bit.
Definition: PathInfo.h:136
mode_t operm() const
Definition: PathInfo.h:155
bool isX() const
Short for isXUsr().
Definition: PathInfo.h:116
mode_t uperm() const
Definition: PathInfo.h:153
FileType fileType() const
Definition: PathInfo.cc:70
bool isPerm(mode_t m) const
Test for equal permission bits.
Definition: PathInfo.h:146
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from ::stat.
Definition: PathInfo.h:86
bool isGid() const
Set GID bit.
Definition: PathInfo.h:138
friend std::ostream & operator<<(std::ostream &str, const StatMode &obj)
Definition: PathInfo.cc:95
String related utilities and Regular expression matching.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition: PathInfo.cc:1092
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Definition: Glob.cc:53
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:1051
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition: PathInfo.cc:1110
FileType
File type information.
Definition: PathInfo.h:56
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:945
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition: PathInfo.cc:688
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition: PathInfo.h:789
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:605
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition: PathInfo.cc:1155
int unlink(const Pathname &path)
Like 'unlink'.
Definition: PathInfo.cc:700
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition: PathInfo.cc:742
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:1063
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
Definition: PathInfo.cc:1183
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:1073
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition: PathInfo.cc:1101
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like '::link'.
Definition: PathInfo.cc:869
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition: PathInfo.cc:924
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:990
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition: PathInfo.cc:1202
mode_t getUmask()
Get the current umask (file mode creation mask)
Definition: PathInfo.cc:1171
int touch(const Pathname &path)
Change file's modification and access times.
Definition: PathInfo.cc:1234
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:883
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:518
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition: PathInfo.cc:1024
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:756
int dirForEachExt(const Pathname &dir_r, const function< bool(const Pathname &, const DirEntry &)> &fnc_r)
Simiar to.
Definition: PathInfo.cc:593
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:1041
ZIP_TYPE zipType(const Pathname &file)
Definition: PathInfo.cc:1124
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition: PathInfo.cc:820
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition: PathInfo.h:754
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
Definition: PathInfo.cc:855
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:501
bool operator==(const DirEntry &rhs) const
Definition: PathInfo.cc:661
DirEntry(const std::string &name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition: PathInfo.h:504