libzypp 17.35.14
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
42
49 namespace filesystem
50 {
51
53
57 {
58 FT_NOT_AVAIL = 0x00, // no typeinfo available
59 FT_NOT_EXIST = 0x01, // file does not exist
60 FT_FILE = 0x02,
61 FT_DIR = 0x04,
62 FT_CHARDEV = 0x08,
64 FT_FIFO = 0x20,
65 FT_LINK = 0x40,
66 FT_SOCKET = 0x80
67 };
69
71 extern std::ostream & operator<<( std::ostream & str, FileType obj ) ZYPP_API;
72
74
76 //
77 // CLASS NAME : StatMode
82 {
83 friend std::ostream & operator<<( std::ostream & str, const StatMode & obj );
84
85 public:
87 StatMode( const mode_t & mode_r = 0 )
88 : _mode( mode_r )
89 {}
90
91 public:
92
95 FileType fileType() const;
96
97 bool isFile() const { return S_ISREG( _mode ); }
98 bool isDir () const { return S_ISDIR( _mode ); }
99 bool isLink() const { return S_ISLNK( _mode ); }
100 bool isChr() const { return S_ISCHR( _mode ); }
101 bool isBlk() const { return S_ISBLK( _mode ); }
102 bool isFifo() const { return S_ISFIFO( _mode ); }
103 bool isSock() const { return S_ISSOCK( _mode ); }
105
108 bool isRUsr() const { return (_mode & S_IRUSR); }
109 bool isWUsr() const { return (_mode & S_IWUSR); }
110 bool isXUsr() const { return (_mode & S_IXUSR); }
111
113 bool isR() const { return isRUsr(); }
115 bool isW() const { return isWUsr(); }
117 bool isX() const { return isXUsr(); }
119
122 bool isRGrp() const { return (_mode & S_IRGRP); }
123 bool isWGrp() const { return (_mode & S_IWGRP); }
124 bool isXGrp() const { return (_mode & S_IXGRP); }
126
129 bool isROth() const { return (_mode & S_IROTH); }
130 bool isWOth() const { return (_mode & S_IWOTH); }
131 bool isXOth() const { return (_mode & S_IXOTH); }
133
137 bool isUid() const { return (_mode & S_ISUID); }
139 bool isGid() const { return (_mode & S_ISGID); }
141 bool isVtx() const { return (_mode & S_ISVTX); }
143
147 bool isPerm ( mode_t m ) const { return (m == perm()); }
149 bool hasPerm( mode_t m ) const { return (m == (m & perm())); }
151
154 mode_t uperm() const { return (_mode & S_IRWXU); }
155 mode_t gperm() const { return (_mode & S_IRWXG); }
156 mode_t operm() const { return (_mode & S_IRWXO); }
157 mode_t perm() const { return (_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)); }
159
161 mode_t st_mode() const { return _mode; }
162
163 private:
164 mode_t _mode;
165 };
167
169 extern std::ostream & operator<<( std::ostream & str, const StatMode & obj ) ZYPP_API;
170
172
174 //
175 // CLASS NAME : DevInoCache
189 {
190 public:
193
195 void clear() { _devino.clear(); }
196
202 bool insert( const dev_t & dev_r, const ino_t & ino_r ) {
203 return _devino[dev_r].insert( ino_r ).second;
204 }
205
206 private:
207 std::map<dev_t,std::set<ino_t> > _devino;
208 };
210
212 //
213 // CLASS NAME : PathInfo
222 {
223 friend std::ostream & operator<<( std::ostream & str, const PathInfo & obj );
224
225 public:
227 enum Mode { STAT, LSTAT };
228
229 public:
234 PathInfo();
235 explicit
236 PathInfo( Pathname path, Mode initial = STAT );
237 explicit
238 PathInfo( const std::string & path, Mode initial = STAT );
239 explicit
240 PathInfo( const char * path, Mode initial = STAT );
242
244 ~PathInfo();
245
247 const Pathname & path() const { return path_t; }
249 const std::string & asString() const { return path_t.asString(); }
251 const char * c_str() const { return path_t.asString().c_str(); }
253 Mode mode() const { return mode_e; }
255 int error() const { return error_i; }
256
258 void setPath( const Pathname & path ) { if ( path != path_t ) error_i = -1; path_t = path; }
260 void setMode( Mode mode ) { if ( mode != mode_e ) error_i = -1; mode_e = mode; }
261
263 bool stat ( const Pathname & path ) { setPath( path ); setMode( STAT ); return operator()(); }
265 bool lstat ( const Pathname & path ) { setPath( path ); setMode( LSTAT ); return operator()(); }
267 bool operator()( const Pathname & path ) { setPath( path ); return operator()(); }
268
270 bool stat() { setMode( STAT ); return operator()(); }
272 bool lstat() { setMode( LSTAT ); return operator()(); }
274 bool operator()();
275
276 public:
277
282 bool isExist() const { return !error_i; }
283
288 FileType fileType() const;
289
290 bool isFile() const { return isExist() && S_ISREG( statbuf_C.st_mode ); }
291 bool isDir () const { return isExist() && S_ISDIR( statbuf_C.st_mode ); }
292 bool isLink() const { return isExist() && S_ISLNK( statbuf_C.st_mode ); }
293 bool isChr() const { return isExist() && S_ISCHR( statbuf_C.st_mode ); }
294 bool isBlk() const { return isExist() && S_ISBLK( statbuf_C.st_mode ); }
295 bool isFifo() const { return isExist() && S_ISFIFO( statbuf_C.st_mode ); }
296 bool isSock() const { return isExist() && S_ISSOCK( statbuf_C.st_mode ); }
297
298 // permission
299 bool isRUsr() const { return isExist() && (statbuf_C.st_mode & S_IRUSR); }
300 bool isWUsr() const { return isExist() && (statbuf_C.st_mode & S_IWUSR); }
301 bool isXUsr() const { return isExist() && (statbuf_C.st_mode & S_IXUSR); }
302
303 bool isR() const { return isRUsr(); }
304 bool isW() const { return isWUsr(); }
305 bool isX() const { return isXUsr(); }
306
307 bool isRGrp() const { return isExist() && (statbuf_C.st_mode & S_IRGRP); }
308 bool isWGrp() const { return isExist() && (statbuf_C.st_mode & S_IWGRP); }
309 bool isXGrp() const { return isExist() && (statbuf_C.st_mode & S_IXGRP); }
310
311 bool isROth() const { return isExist() && (statbuf_C.st_mode & S_IROTH); }
312 bool isWOth() const { return isExist() && (statbuf_C.st_mode & S_IWOTH); }
313 bool isXOth() const { return isExist() && (statbuf_C.st_mode & S_IXOTH); }
314
315 bool isUid() const { return isExist() && (statbuf_C.st_mode & S_ISUID); }
316 bool isGid() const { return isExist() && (statbuf_C.st_mode & S_ISGID); }
317 bool isVtx() const { return isExist() && (statbuf_C.st_mode & S_ISVTX); }
318
319 bool isPerm ( mode_t m ) const { return isExist() && (m == perm()); }
320 bool hasPerm( mode_t m ) const { return isExist() && (m == (m & perm())); }
321
322 mode_t uperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXU) : 0; }
323 mode_t gperm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXG) : 0; }
324 mode_t operm() const { return isExist() ? (statbuf_C.st_mode & S_IRWXO) : 0; }
325 mode_t perm() const { return isExist() ? (statbuf_C.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID|S_ISVTX)) : 0; }
326
327 mode_t st_mode() const { return isExist() ? statbuf_C.st_mode : 0; }
329
331 StatMode asStatMode() const { return st_mode(); }
332
333 nlink_t nlink() const { return isExist() ? statbuf_C.st_nlink : 0; }
334
337 uid_t owner() const { return isExist() ? statbuf_C.st_uid : 0; }
338 gid_t group() const { return isExist() ? statbuf_C.st_gid : 0; }
340
344 mode_t userMay() const;
345
346 bool userMayR() const { return( userMay() & 04 ); }
347 bool userMayW() const { return( userMay() & 02 ); }
348 bool userMayX() const { return( userMay() & 01 ); }
349
350 bool userMayRW() const { return( (userMay() & 06) == 06 ); }
351 bool userMayRX() const { return( (userMay() & 05) == 05 ); }
352 bool userMayWX() const { return( (userMay() & 03) == 03 ); }
353
354 bool userMayRWX() const { return( userMay() == 07 ); }
356
359 ino_t ino() const { return isExist() ? statbuf_C.st_ino : 0; }
360 dev_t dev() const { return isExist() ? statbuf_C.st_dev : 0; }
361 dev_t rdev() const { return isExist() ? statbuf_C.st_rdev : 0; }
362
363 unsigned int devMajor() const;
364 unsigned int devMinor() const;
366
369 off_t size() const { return isExist() ? statbuf_C.st_size : 0; }
370 unsigned long blksize() const { return isExist() ? statbuf_C.st_blksize : 0; }
371 unsigned long blocks() const { return isExist() ? statbuf_C.st_blocks : 0; }
373
376 time_t atime() const { return isExist() ? statbuf_C.st_atime : 0; } /* time of last access */
377 time_t mtime() const { return isExist() ? statbuf_C.st_mtime : 0; } /* time of last modification */
378 time_t ctime() const { return isExist() ? statbuf_C.st_ctime : 0; }
380
381 private:
383 struct stat statbuf_C;
386 };
388
390 extern std::ostream & operator<<( std::ostream & str, const PathInfo & obj ) ZYPP_API;
391
393
395
404 int mkdir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
405
413 int assert_dir( const Pathname & path, unsigned mode = 0755 ) ZYPP_API;
414
420 int rmdir( const Pathname & path ) ZYPP_API;
421
428 int recursive_rmdir( const Pathname & path ) ZYPP_API;
429
436 int clean_dir( const Pathname & path ) ZYPP_API;
437
445 int copy_dir( const Pathname & srcpath, const Pathname & destpath ) ZYPP_API;
446
455 int copy_dir_content( const Pathname & srcpath, const Pathname & destpath) ZYPP_API;
456
469 int dirForEach( const Pathname & dir_r, const function<bool(const Pathname &, const char *const)>& fnc_r ) ZYPP_API;
470
483 int readdir( std::list<std::string> & retlist,
484 const Pathname & path, bool dots = true ) ZYPP_API;
485
498 int readdir( std::list<Pathname> & retlist,
499 const Pathname & path, bool dots = true ) ZYPP_API;
500
502 struct DirEntry {
503 std::string name;
505 DirEntry( std::string name_r = std::string(), FileType type_r = FT_NOT_AVAIL )
506 : name(std::move( name_r ))
507 , type( type_r )
508 {}
509
510 DirEntry( struct dirent* entry );
511
512 bool operator==( const DirEntry &rhs ) const;
513 };
514
515 inline std::ostream & operator<<( std::ostream & str, const DirEntry & obj )
516 { return str << '[' << obj.type << "] " << obj.name; }
517
519 using DirContent = std::list<DirEntry>;
520
521 std::ostream & operator<<( std::ostream & str, const DirContent & obj ) ZYPP_API;
522
533 int readdir( DirContent & retlist, const Pathname & path,
534 bool dots = true, PathInfo::Mode statmode = PathInfo::STAT ) ZYPP_API;
535
539 int dirForEachExt( const Pathname & dir_r, const function<bool(const Pathname &, const DirEntry &)> &fnc_r ) ZYPP_API;
540
546 int is_empty_dir(const Pathname & path) ZYPP_API;
547
549
551
560 int assert_file( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
561
565 int assert_file_mode( const Pathname & path, unsigned mode = 0644 ) ZYPP_API;
566
573 int touch (const Pathname & path) ZYPP_API;
574
580 int unlink( const Pathname & path ) ZYPP_API;
581
590 int rename( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
591
618 int exchange( const Pathname & lpath, const Pathname & rpath );
619
626 int copy( const Pathname & file, const Pathname & dest ) ZYPP_API;
627
634 int symlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
635
642 int hardlink( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
643
649 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath ) ZYPP_API;
650
657 int readlink( const Pathname & symlink_r, Pathname & target_r );
659 inline Pathname readlink( const Pathname & symlink_r )
660 {
661 Pathname target;
662 readlink( symlink_r, target );
663 return target;
664 }
665
678 Pathname expandlink( const Pathname & path_r ) ZYPP_API;
679
686 int copy_file2dir( const Pathname & file, const Pathname & dest );
688
690
699 std::string md5sum( const Pathname & file );
700
706 std::string sha1sum( const Pathname & file );
708
714 std::string checksum( const Pathname & file, const std::string &algorithm );
715
721 bool is_checksum( const Pathname & file, const CheckSum &checksum );
722
724
731 int chmod( const Pathname & path, mode_t mode );
732
739 int chmodApplyUmask( const Pathname & path, mode_t mode );
740
746 int addmod( const Pathname & path, mode_t mode );
747
753 int delmod( const Pathname & path, mode_t mode );
755
757
765
766 ZIP_TYPE zipType( const Pathname & file );
767
775 int erase( const Pathname & path );
776
784 ByteCount df( const Pathname & path );
785
791 mode_t getUmask();
792
799 inline mode_t applyUmaskTo( mode_t mode_r )
800 { return mode_r & ~getUmask(); }
802
804 } // namespace filesystem
806
808 using filesystem::PathInfo;
809
811} // namespace zypp
813#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
Simple cache remembering device/inode to detect hardlinks.
Definition PathInfo.h:189
void clear()
Clear cache.
Definition PathInfo.h:195
bool insert(const dev_t &dev_r, const ino_t &ino_r)
Remember dev/ino.
Definition PathInfo.h:202
std::map< dev_t, std::set< ino_t > > _devino
Definition PathInfo.h:207
Wrapper class for stat/lstat.
Definition PathInfo.h:222
nlink_t nlink() const
Definition PathInfo.h:333
bool lstat(const Pathname &path)
LSTAT path.
Definition PathInfo.h:265
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition PathInfo.h:331
unsigned long blksize() const
Definition PathInfo.h:370
Mode mode() const
Return current stat Mode.
Definition PathInfo.h:253
bool stat()
STAT current path.
Definition PathInfo.h:270
bool isPerm(mode_t m) const
Definition PathInfo.h:319
mode_t st_mode() const
Definition PathInfo.h:327
const Pathname & path() const
Return current Pathname.
Definition PathInfo.h:247
void setPath(const Pathname &path)
Set a new Pathname.
Definition PathInfo.h:258
Mode
stat() or lstat()
Definition PathInfo.h:227
void setMode(Mode mode)
Set a new Mode .
Definition PathInfo.h:260
bool lstat()
LSTAT current path.
Definition PathInfo.h:272
unsigned long blocks() const
Definition PathInfo.h:371
bool isExist() const
Return whether valid stat info exists.
Definition PathInfo.h:282
const char * c_str() const
Return current Pathname as C-string.
Definition PathInfo.h:251
const std::string & asString() const
Return current Pathname as String.
Definition PathInfo.h:249
bool hasPerm(mode_t m) const
Definition PathInfo.h:320
bool operator()(const Pathname &path)
Restat path using current mode.
Definition PathInfo.h:267
int error() const
Return error returned from last stat/lstat call.
Definition PathInfo.h:255
bool stat(const Pathname &path)
STAT path.
Definition PathInfo.h:263
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:82
bool isR() const
Short for isRUsr().
Definition PathInfo.h:113
bool isW() const
Short for isWUsr().
Definition PathInfo.h:115
mode_t st_mode() const
Return the mode_t value.
Definition PathInfo.h:161
bool isVtx() const
Sticky bit.
Definition PathInfo.h:141
bool hasPerm(mode_t m) const
Test for set permission bits.
Definition PathInfo.h:149
bool isUid() const
Set UID bit.
Definition PathInfo.h:137
bool isX() const
Short for isXUsr().
Definition PathInfo.h:117
FileType fileType() const
Definition PathInfo.cc:71
bool isPerm(mode_t m) const
Test for equal permission bits.
Definition PathInfo.h:147
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from ::stat.
Definition PathInfo.h:87
bool isGid() const
Set GID bit.
Definition PathInfo.h:139
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:57
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:798
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:519
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:763
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.
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
Listentry returned by readdir.
Definition PathInfo.h:502
DirEntry(std::string name_r=std::string(), FileType type_r=FT_NOT_AVAIL)
Definition PathInfo.h:505