libzypp 17.38.3
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#include <utility>
30
31#include <zypp-core/Pathname.h>
32#include <zypp-core/ByteCount.h>
33#include <zypp-core/CheckSum.h>
34
35struct dirent;
36
38namespace zypp
39{
40
41 inline bool IamRoot() { return ::geteuid() == 0; }
42 inline bool IamNotRoot() { return not IamRoot(); }
43
45
52 namespace filesystem
53 {
54
56
60 {
61 FT_NOT_AVAIL = 0x00, // no typeinfo available
62 FT_NOT_EXIST = 0x01, // file does not exist
63 FT_FILE = 0x02,
64 FT_DIR = 0x04,
65 FT_CHARDEV = 0x08,
67 FT_FIFO = 0x20,
68 FT_LINK = 0x40,
69 FT_SOCKET = 0x80
70 };
71
72
74 extern std::ostream & operator<<( std::ostream & str, FileType obj ) ZYPP_API;
75
77
79 //
80 // CLASS NAME : StatMode
86 {
87 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
88
89 public:
91 StatMode( const mode_t & mode_r = 0 )
92 : _mode( mode_r )
93 {}
94
95 public:
96
99 FileType fileType() const;
100
101 bool isFile() const { return S_ISREG( _mode ); }
102 bool isDir () const { return S_ISDIR( _mode ); }
103 bool isLink() const { return S_ISLNK( _mode ); }
104 bool isChr() const { return S_ISCHR( _mode ); }
105 bool isBlk() const { return S_ISBLK( _mode ); }
106 bool isFifo() const { return S_ISFIFO( _mode ); }
107 bool isSock() const { return S_ISSOCK( _mode ); }
109
112 bool isRUsr() const { return (_mode & S_IRUSR); }
113 bool isWUsr() const { return (_mode & S_IWUSR); }
114 bool isXUsr() const { return (_mode & S_IXUSR); }
115
117 bool isR() const { return isRUsr(); }
119 bool isW() const { return isWUsr(); }
121 bool isX() const { return isXUsr(); }
123
126 bool isRGrp() const { return (_mode & S_IRGRP); }
127 bool isWGrp() const { return (_mode & S_IWGRP); }
128 bool isXGrp() const { return (_mode & S_IXGRP); }
130
133 bool isROth() const { return (_mode & S_IROTH); }
134 bool isWOth() const { return (_mode & S_IWOTH); }
135 bool isXOth() const { return (_mode & S_IXOTH); }
137
140
141 bool isUid() const { return (_mode & S_ISUID); }
143 bool isGid() const { return (_mode & S_ISGID); }
145 bool isVtx() const { return (_mode & S_ISVTX); }
147
150
151 bool isPerm ( mode_t m ) const { return (m == perm()); }
153 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
155
158 mode_t uperm() const { return (_mode & S_IRWXU); }
159 mode_t gperm() const { return (_mode & S_IRWXG); }
160 mode_t operm() const { return (_mode & S_IRWXO); }
161 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
163
165 mode_t st_mode() const { return _mode; }
166
167 private:
168 mode_t _mode;
169 };
170
171
173 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj ) ZYPP_API;
174
176
178 //
179 // CLASS NAME : DevInoCache
193 {
194 public:
197
199 void clear() { _devino.clear(); }
200
206 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
207 return _devino[dev_r].insert( ino_r ).second;
208 }
209
210 private:
211 std::map<dev_t,std::set<ino_t> > _devino;
212 };
213
214
216 //
217 // CLASS NAME : PathInfo
226 {
227 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
228
229 public:
231 enum Mode { STAT, LSTAT };
232
233 public:
238 PathInfo();
239 explicit
240 PathInfo( Pathname path, Mode initial = STAT );
241 explicit
242 PathInfo( const std::string & path, Mode initial = STAT );
243 explicit
244 PathInfo( const char * path, Mode initial = STAT );
246
248 ~PathInfo();
249
251 const Pathname & path() const { return path_t; }
253 const std::string & asString() const { return path_t.asString(); }
255 const char * c_str() const { return path_t.asString().c_str(); }
257 Mode mode() const { return mode_e; }
259 int error() const { return error_i; }
260
262 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
264 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
265
267 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
269 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
271 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
272
274 bool stat() { setMode( STAT ); return operator()(); }
276 bool lstat() { setMode( LSTAT ); return operator()(); }
278 bool operator()();
279
280 public:
281
286 bool isExist() const { return !error_i; }
287
293 FileType fileType() const;
294
295 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
296 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
297 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
298 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
299 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
300 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
301 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
302
303 // permission
304 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
305 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
306 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
307
308 bool isR() const { return isRUsr(); }
309 bool isW() const { return isWUsr(); }
310 bool isX() const { return isXUsr(); }
311
312 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
313 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
314 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
315
316 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
317 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
318 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
319
320 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
321 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
322 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
323
324 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
325 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
326
327 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
328 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
329 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
330 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
331
332 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
334
336 StatMode asStatMode() const { return st_mode(); }
337
338 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
339
342 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
343 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
345
350
351 mode_t userMay() const;
352
353 bool userMayR() const { return( userMay() & 04 ); }
354 bool userMayW() const { return( userMay() & 02 ); }
355 bool userMayX() const { return( userMay() & 01 ); }
356
357 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
358 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
359 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
360
361 bool userMayRWX() const { return( userMay() == 07 ); }
363
366 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
367 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
368 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
369
370 unsigned int devMajor() const;
371 unsigned int devMinor() const;
373
376 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
377 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
378 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
380
383 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
384 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
385 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
387
388 private:
393 };
394
395
397 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj ) ZYPP_API;
398
400
402
404
410 bool userMayWriteOrCreateDir( const Pathname & path_r );
411
419 int mkdir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
420
428 int assert_dir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
429
435 int rmdir( const Pathname & path ) ZYPP_API;
436
443 int recursive_rmdir( const Pathname & path ) ZYPP_API;
444
451 int clean_dir( const Pathname & path ) ZYPP_API;
452
460 int copy_dir( const Pathname & srcpath, const Pathname & destpath ) ZYPP_API;
461
470 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath) ZYPP_API;
471
484 int dirForEach( const Pathname & dir_r, const function<bool(const Pathname &, const char *const)>& fnc_r ) ZYPP_API;
485
497
498 int readdir( std::list<std::string> & retlist,
499 const Pathname & path, bool dots = true ) ZYPP_API;
500
512
513 int readdir( std::list<Pathname> & retlist,
514 const Pathname & path, bool dots = true ) ZYPP_API;
515
517 struct DirEntry {
518 std::string name;
520 DirEntry( std::string name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
521 : name(std::move( name_r ))
522 , type( type_r )
523 {}
524
525 DirEntry( struct dirent* entry );
526
527 bool operator==( const DirEntry &rhs ) const;
528 };
529
530 inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
531 { return str << '[' << obj.type << "] " << obj.name; }
532
534 using DirContent = std::list<DirEntry>;
535
536 std::ostream & operator<<( std::ostream & str, const DirContent & obj ) ZYPP_API;
537
548 int readdir( DirContent & retlist, const Pathname & path,
549 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT ) ZYPP_API;
550
554 int dirForEachExt( const Pathname & dir_r, const function<bool(const Pathname &, const DirEntry &)> &fnc_r ) ZYPP_API;
555
561 int is_empty_dir(const Pathname & path) ZYPP_API;
562
564
566
568
575 int assert_file( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
576
580 int assert_file_mode( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
581
588 int touch (const Pathname & path) ZYPP_API;
589
595 int unlink( const Pathname & path ) ZYPP_API;
596
605 int rename( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
606
633 int exchange( const Pathname & lpath, const Pathname & rpath );
634
641 int copy( const Pathname & file, const Pathname & dest ) ZYPP_API;
642
649 int symlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
650
657 int hardlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
658
664 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
665
672 int readlink( const Pathname & symlink_r, Pathname & target_r );
673 /** \overload Return an empty Pathname on error. */
674 inline Pathname readlink( const Pathname & symlink_r )
675 {
677 readlink( symlink_r, target );
678 return target;
679 }
680
693 Pathname expandlink( const Pathname & path_r ) ZYPP_API;
694
701 int copy_file2dir( const Pathname & file, const Pathname & dest );
703
705
709
714 std::string md5sum( const Pathname & file );
715
721 std::string sha1sum( const Pathname & file );
723
729 std::string checksum( const Pathname & file, const std::string &algorithm );
730
736 bool is_checksum( const Pathname & file, const CheckSum &checksum );
737
739
741
746 int chmod( const Pathname & path, mode_t mode );
747
754 int chmodApplyUmask( const Pathname & path, mode_t mode );
755
761 int addmod( const Pathname & path, mode_t mode );
762
768 int delmod( const Pathname & path, mode_t mode );
770
772
774
780
781 ZIP_TYPE zipType( const Pathname & file );
782
790 int erase( const Pathname & path );
791
799 ByteCount df( const Pathname & path );
800
806 mode_t getUmask();
807
813 **/
814 inline mode_t applyUmaskTo( mode_t mode_r )
815 { return mode_r & ~getUmask(); }
817
819 } // namespace filesystem
821
823 using filesystem::PathInfo;
824
826} // namespace zypp
828#endif // ZYPP_PATHINFO_H
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
bool operator()(const zypp::Arch &lhs, const zypp::Arch &rhs) const
Default order for std::container based Arch::compare.
Definition Arch.h:370
Store and operate with byte count.
Definition ByteCount.h:32
void clear()
Clear cache.
Definition PathInfo.h:199
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition PathInfo.h:206
std::map< dev_t, std::set< ino_t > > _devino
Definition PathInfo.h:211
Wrapper class for stat/lstat.
Definition PathInfo.h:226
nlink_t nlink() const
Definition PathInfo.h:338
bool lstat(const Pathname &path)
LSTAT path.
Definition PathInfo.h:269
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition PathInfo.h:336
unsigned long blksize() const
Definition PathInfo.h:377
Mode mode() const
Return current stat Mode.
Definition PathInfo.h:257
bool stat()
STAT current path.
Definition PathInfo.h:274
bool isPerm(mode_t m) const
Definition PathInfo.h:324
mode_t st_mode() const
Definition PathInfo.h:332
const Pathname & path() const
Return current Pathname.
Definition PathInfo.h:251
void setPath(const Pathname &path)
Set a new Pathname.
Definition PathInfo.h:262
Mode
stat() or lstat()
Definition PathInfo.h:231
void setMode(Mode mode)
Set a new Mode .
Definition PathInfo.h:264
bool lstat()
LSTAT current path.
Definition PathInfo.h:276
mode_t userMay() const
Returns current users permission ([0-7]).
Definition PathInfo.cc:225
unsigned long blocks() const
Definition PathInfo.h:378
bool isExist() const
Return whether valid stat info exists.
Definition PathInfo.h:286
const char * c_str() const
Return current Pathname as C-string.
Definition PathInfo.h:255
const std::string & asString() const
Return current Pathname as String.
Definition PathInfo.h:253
bool hasPerm(mode_t m) const
Definition PathInfo.h:325
bool operator()(const Pathname &path)
Restat path using current mode.
Definition PathInfo.h:271
int error() const
Return error returned from last stat/lstat call.
Definition PathInfo.h:259
bool stat(const Pathname &path)
STAT path.
Definition PathInfo.h:267
bool operator==(const Pathname &l, const Pathname &r)
Definition Pathname.h:192
Wrapper class for mode_t values as derived from stat.
Definition PathInfo.h:86
bool isR() const
Short for isRUsr().
Definition PathInfo.h:117
bool isW() const
Short for isWUsr().
Definition PathInfo.h:119
mode_t st_mode() const
Return the mode_t value.
Definition PathInfo.h:165
bool isVtx() const
Sticky bit.
Definition PathInfo.h:145
bool hasPerm(mode_t m) const
Test for set permission bits.
Definition PathInfo.h:153
bool isUid() const
Set UID bit.
Definition PathInfo.h:141
bool isX() const
Short for isXUsr().
Definition PathInfo.h:121
FileType fileType() const
Definition PathInfo.cc:71
bool isPerm(mode_t m) const
Test for equal permission bits.
Definition PathInfo.h:151
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from stat.
Definition PathInfo.h:91
bool isGid() const
Set GID bit.
Definition PathInfo.h:143
friend std::ostream & operator<<(std::ostream &str, const StatMode &obj)
Definition PathInfo.cc:96
Definition ansi.h:855
String related utilities and Regular expression matching.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition PathInfo.cc:1111
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
Definition PathInfo.cc:874
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:1070
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition PathInfo.cc:1132
int rmdir(const Pathname &path)
Like 'rmdir'.
Definition PathInfo.cc:385
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
Definition PathInfo.cc:324
FileType
File type information.
Definition PathInfo.h:60
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
Definition PathInfo.cc:1205
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
Definition PathInfo.cc:482
bool userMayWriteOrCreateDir(const Pathname &path_r)
Returns whether path_r denotes an existing directory with write permission for the current user or an...
Definition PathInfo.cc:306
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition PathInfo.h:813
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition PathInfo.cc:431
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition PathInfo.cc:1177
std::list< DirEntry > DirContent
Returned by readdir.
Definition PathInfo.h:534
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition PathInfo.cc:902
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition PathInfo.cc:1224
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition PathInfo.cc:839
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition PathInfo.cc:461
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
Definition PathInfo.cc:1082
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:964
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:719
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition PathInfo.cc:624
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:1092
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition PathInfo.cc:1123
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:338
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition PathInfo.cc:943
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition PathInfo.cc:1009
mode_t getUmask()
Get the current umask (file mode creation mask).
Definition PathInfo.cc:1193
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
Definition PathInfo.cc:523
int dirForEachExt(const Pathname &dir_r, const function< bool(const Pathname &, const DirEntry &)> &fnc_r)
Simiar to.
Definition PathInfo.cc:612
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition PathInfo.cc:707
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition PathInfo.cc:1043
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like 'link'.
Definition PathInfo.cc:888
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition PathInfo.cc:775
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition PathInfo.cc:1060
ZIP_TYPE zipType(const Pathname &file)
Definition PathInfo.cc:1146
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition PathInfo.h:778
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition PathInfo.cc:761
int chmodApplyUmask(const Pathname &path, mode_t mode)
Similar to 'chmod', but mode is modified by the process's umask in the usual way.
Definition PathInfo.cc:1120
int touch(const Pathname &path)
Change file's modification and access times.
Definition PathInfo.cc:1256
Easy-to use interface to the ZYPP dependency resolver.
bool IamRoot()
Definition PathInfo.h:41
bool IamNotRoot()
Definition PathInfo.h:42
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
Listentry returned by readdir.
Definition PathInfo.h:517
DirEntry(std::string name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition PathInfo.h:520