14#include <sys/statvfs.h>
15#include <sys/sysmacros.h>
51#define EMUMOUT(T) case T: return str << #T; break
103 else if ( obj.
isDir() )
107 else if ( obj.
isChr() )
109 else if ( obj.
isBlk() )
116 str << t <<
" " << std::setfill(
'0' ) << std::setw( 4 ) << std::oct << obj.
perm();
229 if (
owner() == geteuid() ) {
230 return(
uperm()/0100 );
231 }
else if (
group() == getegid() ) {
232 return(
gperm()/010 );
285#define logResult( ... ) doLogResult( __FUNCTION__, __LINE__, __VA_ARGS__ )
288 inline int doLogResult(
const char *function,
const int line,
const int res,
const char * rclass = 0 )
297 WAR <<
" FAILED: " << rclass <<
" " << res << endl;
338 std::string::size_type lastpos = ( path.
relative() ? 2 : 1 );
339 std::string::size_type pos = std::string::npos;
342 while ( (pos = spath.find(
'/',lastpos)) != std::string::npos )
344 string dir( spath.substr(0,pos) );
345 ret =
::mkdir( dir.c_str(), mode );
348 if ( errno == EEXIST )
353 WAR <<
" FAILED: mkdir " << dir <<
' ' <<
str::octstring( mode ) <<
" errno " << ret << endl;
373 MIL <<
"rmdir " << path;
388 struct dirent * d =
nullptr;
390 if ( ! (dp = opendir( dir.
c_str() )) )
395 std::string direntry = d->d_name;
396 if ( direntry ==
"." || direntry ==
".." )
398 Pathname new_path( dir / d->d_name );
401 if ( ! lstat( new_path.
c_str(), &st ) )
403 if ( S_ISDIR( st.st_mode ) )
406 ::unlink( new_path.
c_str() );
419 MIL <<
"recursive_rmdir " << path <<
' ';
432 MIL <<
"unlink symlink ";
449 MIL <<
"clean_dir " << path <<
' ';
470 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
488 const char *
const argv[] = {
498 MIL <<
" " << output;
500 int ret = prog.
close();
511 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
523 if ( srcpath == destpath ) {
527 std::string src( srcpath.
asString());
529 const char *
const argv[] = {
539 MIL <<
" " << output;
541 int ret = prog.
close();
548 template <
class... T>
551 template <
typename F>
555 []( DIR * dir_r ) { if ( dir_r ) ::closedir( dir_r ); } );
557 MIL <<
"readdir " << dir_r <<
' ';
565 if ( entry->d_name[0] ==
'.' && ( entry->d_name[1] ==
'\0' || ( entry->d_name[1] ==
'.' && entry->d_name[2] ==
'\0' ) ) )
569 static_assert( !std::is_invocable_v< function<
bool(
const Pathname &,
const char *
const)>,
const Pathname &,
const DirEntry &> ,
"Invoke detection broken" );
570 static_assert( !std::is_invocable_v< function<
bool(
const Pathname &,
const DirEntry& )>,
const Pathname &,
const char *> ,
"Invoke detection broken" );
572 if constexpr ( std::is_invocable_v<F, const Pathname &, const char *const> ) {
573 if ( ! std::forward<F>(fnc_r)( dir_r, entry->d_name ) ) {
577 }
else if constexpr ( std::is_invocable_v<F, const Pathname &, const DirEntry&> ) {
578 if ( ! std::forward<F>(fnc_r)( dir_r,
DirEntry( entry ) ) ) {
610 int readdir( std::list<std::string> & retlist_r,
const Pathname & path_r,
bool dots_r )
614 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
616 if ( dots_r || name_r[0] !=
'.' )
617 retlist_r.push_back( name_r );
627 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
629 if ( dots_r || name_r[0] !=
'.' )
630 retlist_r.push_back( dir_r/name_r );
638 switch( entry->d_type ) {
640 this->type = FileType::FT_BLOCKDEV;
643 this->type = FileType::FT_CHARDEV;
646 this->type = FileType::FT_DIR;
649 this->type = FileType::FT_FIFO;
652 this->type = FileType::FT_LINK;
655 this->type = FileType::FT_FILE;
658 this->type = FileType::FT_SOCKET;
661 this->type = FileType::FT_NOT_AVAIL;
678 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
680 if ( dots_r || name_r[0] !=
'.' )
681 retlist_r.push_back(
DirEntry( name_r,
PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
696 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
707 MIL <<
"unlink " << path;
723 if ( ret == -1 && errno == EXDEV ) {
724 const char *
const argv[] = {
731 for (
string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
732 MIL <<
" " << output;
749 MIL <<
"rename " << oldpath <<
" -> " << newpath;
750 if ( safe_rename( oldpath.
asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
763 MIL <<
"exchange " << lpath <<
" <-> " << rpath;
779 if ( safe_rename( rpath.
c_str(), lpath.
c_str() ) == -1 ) {
792 if ( safe_rename( lpath.
c_str(), rpath.
c_str() ) == -1 ) {
805 if ( safe_rename( lpath.
c_str(), tmp.
c_str() ) == -1 ) {
808 if ( safe_rename( rpath.
c_str(), lpath.
c_str() ) == -1 ) {
812 if ( safe_rename( tmp.
c_str(), rpath.
c_str() ) == -1 ) {
827 MIL <<
"copy " << file <<
" -> " << dest <<
' ';
839 const char *
const argv[] = {
841 "--remove-destination",
849 MIL <<
" " << output;
851 int ret = prog.
close();
862 MIL <<
"symlink " << newpath <<
" -> " << oldpath;
876 MIL <<
"hardlink " << newpath <<
" -> " << oldpath;
877 if ( ::link( oldpath.
asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
890 MIL <<
"hardlinkCopy " << oldpath <<
" -> " << newpath;
896 MIL <<
" => copy" << endl;
897 return copy( oldpath, newpath );
903 int res =
unlink( newpath );
909 if ( ::link( oldpath.
asString().c_str(), newpath.
asString().c_str() ) == -1 )
915 MIL <<
" => copy" << endl;
916 return copy( oldpath, newpath );
931 static const ssize_t bufsiz = 2047;
932 static char buf[bufsiz+1];
937 MIL <<
"readlink " << symlink_r;
952 static const unsigned int level_limit = 256;
953 static unsigned int count;
957 for (count = level_limit; info.
isLink() && count; count--)
959 DBG <<
"following symlink " << path;
961 DBG <<
"->" << path << std::endl;
968 ERR <<
"Expand level limit reached. Probably a cyclic symbolic link." << endl;
972 else if (count < level_limit)
980 ERR << path <<
" is broken (expanded from " << path_r <<
")" << endl;
986 DBG <<
"not a symlink" << endl;
997 MIL <<
"copy_file2dir " << file <<
" -> " << dest <<
' ';
1005 if ( !dp.
isDir() ) {
1009 const char *
const argv[] = {
1018 MIL <<
" " << output;
1020 int ret = prog.
close();
1031 if ( !
PathInfo( file ).isFile() ) {
1034 std::ifstream istr( file.
asString().c_str() );
1058 if ( !
PathInfo( file ).isFile() ) {
1061 std::ifstream istr( file.
asString().c_str() );
1111 mode_t omode(
PathInfo( path ).st_mode() );
1112 mode_t tmode( omode | mode );
1113 if ( omode != mode )
1114 return chmod( path, tmode );
1120 mode_t omode(
PathInfo( path ).st_mode() );
1121 mode_t tmode( omode & ~mode );
1122 if ( omode != mode )
1123 return chmod( path, tmode );
1136 int fd = open( file.
asString().c_str(), O_RDONLY|O_CLOEXEC );
1139 const int magicSize = 5;
1140 unsigned char magic[magicSize];
1141 memset( magic, 0, magicSize );
1142 if ( read( fd, magic, magicSize ) == magicSize ) {
1143 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1145 }
else if ( magic[0] ==
'B' && magic[1] ==
'Z' && magic[2] ==
'h' ) {
1147 }
else if ( magic[0] ==
'\0' && magic[1] ==
'Z' && magic[2] ==
'C' && magic[3] ==
'K' && magic[4] ==
'1') {
1167 if ( statvfs( path_r.
c_str(), &sb ) == 0 )
1169 ret = sb.f_bfree * sb.f_bsize;
1181 mode_t mask = ::umask( 0022 );
1202 int fd = ::creat( path.
c_str(), mode );
1225 return chmod( path, mode );
1230 int fd = ::creat( path.
c_str(), mode );
1244 MIL <<
"touch " << path;
1245 struct ::utimbuf times;
1246 times.actime = ::time( 0 );
1247 times.modtime = ::time( 0 );
1248 if ( ::utime( path.
asString().c_str(), × ) == -1 ) {
#define ZYPP_BASE_LOGGER_LOGGROUP
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Store and operate with byte count.
std::string digest()
get hex string representation of the digest
Convenience errno wrapper.
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
int close() override
Wait for the progamm to complete.
std::string receiveLine()
Read one line from the input stream.
Wrapper class for stat/lstat.
bool lstat(const Pathname &path)
LSTAT path.
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
bool stat()
STAT current path.
const Pathname & path() const
Return current Pathname.
bool operator()()
Restat current path using current mode.
unsigned int devMinor() const
FileType fileType() const
bool lstat()
LSTAT current path.
mode_t userMay() const
Returns current users permission ([0-7])
bool isExist() const
Return whether valid stat info exists.
const std::string & asString() const
Return current Pathname as String.
unsigned int devMajor() const
int error() const
Return error returned from last stat/lstat call.
Pathname dirname() const
Return all but the last component od this path.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
std::string basename() const
Return the last component of this path.
bool empty() const
Test for an empty path.
bool relative() const
Test for a relative path.
FileType fileType() const
StatMode(const mode_t &mode_r=0)
Ctor taking mode_t value from stat.
Provide a new empty temporary file and delete it when no longer needed.
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
String related utilities and Regular expression matching.
std::ostream & getStream(const char *group_r, LogLevel level_r, const char *file_r, const char *func_r, const int line_r)
Return a log stream to write on.
Types and functions for filesystem operations.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
std::ostream & operator<<(std::ostream &str, const Glob &obj)
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
int dirForEachImpl(const Pathname &dir_r, F &&fnc_r)
int rmdir(const Pathname &path)
Like 'rmdir'.
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
static int recursive_rmdir_1(const Pathname &dir, bool removeDir=true)
FileType
File type information.
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
std::list< DirEntry > DirContent
Returned by readdir.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
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...
int unlink(const Pathname &path)
Like 'unlink'.
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
int erase(const Pathname &path)
Erase whatever happens to be located at path (file or directory).
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
mode_t getUmask()
Get the current umask (file mode creation mask)
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
int dirForEachExt(const Pathname &dir_r, const function< bool(const Pathname &, const DirEntry &)> &fnc_r)
Simiar to.
constexpr bool always_false
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
std::string md5sum(const Pathname &file)
Compute a files md5sum.
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like 'link'.
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
ZIP_TYPE zipType(const Pathname &file)
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
int chmodApplyUmask(const Pathname &path, mode_t mode)
Similar to 'chmod', but mode is modified by the process's umask in the usual way.
int touch(const Pathname &path)
Change file's modification and access times.
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
std::string octstring(char n, int w=4)
std::string strerror(int errno_r)
Return string describing the error_r code.
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
std::string asString(const Patch::Category &obj)
Listentry returned by readdir.
bool operator==(const DirEntry &rhs) const
DirEntry(std::string name_r=std::string(), FileType type_r=FT_NOT_AVAIL)