libzypp 17.37.17
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
411 int mkdir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
412
420 int assert_dir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
421
427 int rmdir( const Pathname & path ) ZYPP_API;
428
435 int recursive_rmdir( const Pathname & path ) ZYPP_API;
436
443 int clean_dir( const Pathname & path ) ZYPP_API;
444
452 int copy_dir( const Pathname & srcpath, const Pathname & destpath ) ZYPP_API;
453
462 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath) ZYPP_API;
463
476 int dirForEach( const Pathname & dir_r, const function<bool(const Pathname &, const char *const)>& fnc_r ) ZYPP_API;
477
489
490 int readdir( std::list<std::string> & retlist,
491 const Pathname & path, bool dots = true ) ZYPP_API;
492
504
505 int readdir( std::list<Pathname> & retlist,
506 const Pathname & path, bool dots = true ) ZYPP_API;
507
509 struct DirEntry {
510 std::string name;
512 DirEntry( std::string name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
513 : name(std::move( name_r ))
514 , type( type_r )
515 {}
516
517 DirEntry( struct dirent* entry );
518
519 bool operator==( const DirEntry &rhs ) const;
520 };
521
522 inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
523 { return str << '[' << obj.type << "] " << obj.name; }
524
526 using DirContent = std::list<DirEntry>;
527
528 std::ostream & operator<<( std::ostream & str, const DirContent & obj ) ZYPP_API;
529
540 int readdir( DirContent & retlist, const Pathname & path,
541 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT ) ZYPP_API;
542
546 int dirForEachExt( const Pathname & dir_r, const function<bool(const Pathname &, const DirEntry &)> &fnc_r ) ZYPP_API;
547
553 int is_empty_dir(const Pathname & path) ZYPP_API;
554
556
558
560
567 int assert_file( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
568
572 int assert_file_mode( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
573
580 int touch (const Pathname & path) ZYPP_API;
581
587 int unlink( const Pathname & path ) ZYPP_API;
588
597 int rename( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
598
625 int exchange( const Pathname & lpath, const Pathname & rpath );
626
633 int copy( const Pathname & file, const Pathname & dest ) ZYPP_API;
634
641 int symlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
642
649 int hardlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
650
656 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
657
664 int readlink( const Pathname & symlink_r, Pathname & target_r );
665 /** \overload Return an empty Pathname on error. */
666 inline Pathname readlink( const Pathname & symlink_r )
667 {
669 readlink( symlink_r, target );
670 return target;
671 }
672
685 Pathname expandlink( const Pathname & path_r ) ZYPP_API;
686
693 int copy_file2dir( const Pathname & file, const Pathname & dest );
695
697
701
706 std::string md5sum( const Pathname & file );
707
713 std::string sha1sum( const Pathname & file );
715
721 std::string checksum( const Pathname & file, const std::string &algorithm );
722
728 bool is_checksum( const Pathname & file, const CheckSum &checksum );
729
731
733
738 int chmod( const Pathname & path, mode_t mode );
739
746 int chmodApplyUmask( const Pathname & path, mode_t mode );
747
753 int addmod( const Pathname & path, mode_t mode );
754
760 int delmod( const Pathname & path, mode_t mode );
762
764
766
772
773 ZIP_TYPE zipType( const Pathname & file );
774
782 int erase( const Pathname & path );
783
791 ByteCount df( const Pathname & path );
792
798 mode_t getUmask();
799
805 **/
806 inline mode_t applyUmaskTo( mode_t mode_r )
807 { return mode_r & ~getUmask(); }
809
811 } // namespace filesystem
813
815 using filesystem::PathInfo;
816
818} // namespace zypp
820#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:185
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 Arch.h:364
String related utilities and Regular expression matching.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition PathInfo.cc:1097
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
Definition PathInfo.cc:860
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:1056
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition PathInfo.cc:1118
int rmdir(const Pathname &path)
Like 'rmdir'.
Definition PathInfo.cc:371
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
Definition PathInfo.cc:310
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:1191
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
Definition PathInfo.cc:468
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition PathInfo.h:805
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition PathInfo.cc:417
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition PathInfo.cc:1163
std::list< DirEntry > DirContent
Returned by readdir.
Definition PathInfo.h:526
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition PathInfo.cc:888
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition PathInfo.cc:1210
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition PathInfo.cc:825
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition PathInfo.cc:447
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
Definition PathInfo.cc:1068
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:950
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:705
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition PathInfo.cc:610
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:1078
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition PathInfo.cc:1109
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:324
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition PathInfo.cc:929
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition PathInfo.cc:995
mode_t getUmask()
Get the current umask (file mode creation mask)
Definition PathInfo.cc:1179
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
Definition PathInfo.cc:509
int dirForEachExt(const Pathname &dir_r, const function< bool(const Pathname &, const DirEntry &)> &fnc_r)
Simiar to.
Definition PathInfo.cc:598
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition PathInfo.cc:693
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition PathInfo.cc:1029
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like 'link'.
Definition PathInfo.cc:874
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition PathInfo.cc:761
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition PathInfo.cc:1046
ZIP_TYPE zipType(const Pathname &file)
Definition PathInfo.cc:1132
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition PathInfo.h:770
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition PathInfo.cc:747
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:1106
int touch(const Pathname &path)
Change file's modification and access times.
Definition PathInfo.cc:1242
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:509
DirEntry(std::string name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition PathInfo.h:512