13 #include <sys/types.h>
15 #include <sys/statvfs.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 );
258 {
INT <<
"Cleanup the code: This method is deprecated" << endl;
return devMajor(); }
260 {
INT <<
"Cleanup the code: This method is deprecated" << endl;
return devMinor(); }
278 str <<
" size " << obj.
size();
297 #define _Log_Result MIL << endl, __Log_Result
303 WAR <<
" FAILED: " << rclass <<
" " << res << endl;
347 while ( (pos = spath.find(
'/',lastpos)) != string::npos )
349 string dir( spath.substr(0,pos) );
350 ret =
::mkdir( dir.c_str(), mode );
353 if ( errno == EEXIST )
358 WAR <<
" FAILED: mkdir " << dir <<
' ' <<
str::octstring( mode ) <<
" errno " << ret << endl;
378 MIL <<
"rmdir " << path;
379 if ( ::
rmdir( path.asString().c_str() ) == -1 ) {
395 if ( ! (dp = opendir( dir.
c_str() )) )
400 std::string direntry = d->d_name;
401 if ( direntry ==
"." || direntry ==
".." )
403 Pathname new_path( dir / d->d_name );
406 if ( ! lstat( new_path.
c_str(), &st ) )
408 if ( S_ISDIR( st.st_mode ) )
424 MIL <<
"recursive_rmdir " << path <<
' ';
445 MIL <<
"clean_dir " << path <<
' ';
456 string cmd(
str::form(
"cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
459 MIL <<
" " << output;
461 int ret = prog.
close();
472 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
485 if ( tp.isExist() ) {
490 const char *
const argv[] = {
495 destpath.asString().c_str(),
500 MIL <<
" " << output;
502 int ret = prog.
close();
513 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
525 if ( srcpath == destpath ) {
529 std::string src( srcpath.
asString());
531 const char *
const argv[] = {
536 destpath.asString().c_str(),
541 MIL <<
" " << output;
543 int ret = prog.
close();
563 []( DIR * dir_r ) {
if ( dir_r ) ::closedir( dir_r ); } );
565 MIL <<
"readdir " << dir_r <<
' ';
571 for (
struct dirent * entry = ::
readdir( dir ); entry; entry =
::readdir( dir ) )
573 if ( entry->d_name[0] ==
'.' && ( entry->d_name[1] ==
'\0' || ( entry->d_name[1] ==
'.' && entry->d_name[2] ==
'\0' ) ) )
576 if ( ! fnc_r( dir_r, entry->d_name ) )
592 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
594 if ( ( nodots && name_r[0] ==
'.' ) || ! matcher_r( name_r ) )
596 return fnc_r( dir_r, name_r );
604 int readdir( std::list<std::string> & retlist_r,
const Pathname & path_r,
bool dots_r )
608 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
610 if ( dots_r || name_r[0] !=
'.' )
611 retlist_r.push_back( name_r );
621 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
623 if ( dots_r || name_r[0] !=
'.' )
624 retlist_r.push_back( dir_r/name_r );
641 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
643 if ( dots_r || name_r[0] !=
'.' )
644 retlist_r.push_back(
DirEntry( name_r,
PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
650 {
return dumpRange( str, obj.begin(), obj.end() ); }
659 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
670 MIL <<
"unlink " << path;
671 if ( ::
unlink( path.asString().c_str() ) == -1 ) {
684 MIL <<
"rename " << oldpath <<
" -> " << newpath;
685 if ( ::
rename( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 ) {
698 MIL <<
"exchange " << lpath <<
" <-> " << rpath;
699 if ( lpath.
empty() || rpath.empty() )
714 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
727 if ( ::
rename( lpath.
c_str(), rpath.c_str() ) == -1 ) {
740 if ( ::
rename( lpath.
c_str(), tmp.c_str() ) == -1 ) {
743 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
747 if ( ::
rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
762 MIL <<
"copy " << file <<
" -> " << dest <<
' ';
774 const char *
const argv[] = {
776 "--remove-destination",
779 dest.asString().c_str(),
784 MIL <<
" " << output;
786 int ret = prog.
close();
797 MIL <<
"symlink " << newpath <<
" -> " << oldpath;
798 if ( ::
symlink( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
811 MIL <<
"hardlink " << newpath <<
" -> " << oldpath;
812 if ( ::link( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
825 MIL <<
"hardlinkCopy " << oldpath <<
" -> " << newpath;
831 return copy( oldpath, newpath );
837 int res =
unlink( newpath );
843 if ( ::link( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 )
849 return copy( oldpath, newpath );
864 static const ssize_t bufsiz = 2047;
865 static char buf[bufsiz+1];
870 MIL <<
"readlink " << symlink_r;
885 static const unsigned int level_limit = 256;
886 static unsigned int count;
890 for (count = level_limit; info.
isLink() && count; count--)
892 DBG <<
"following symlink " << path;
894 DBG <<
"->" << path << std::endl;
901 ERR <<
"Expand level limit reached. Probably a cyclic symbolic link." << endl;
905 else if (count < level_limit)
913 ERR << path <<
" is broken (expanded from " << path_r <<
")" << endl;
919 DBG <<
"not a symlink" << endl;
930 MIL <<
"copy_file2dir " << file <<
" -> " << dest <<
' ';
942 const char *
const argv[] = {
946 dest.asString().c_str(),
951 MIL <<
" " << output;
953 int ret = prog.
close();
964 if ( !
PathInfo( file ).isFile() ) {
967 std::ifstream istr( file.
asString().c_str() );
991 if ( !
PathInfo( file ).isFile() ) {
994 std::ifstream istr( file.
asString().c_str() );
1041 mode_t omode(
PathInfo( path ).st_mode() );
1042 mode_t tmode( omode | mode );
1043 if ( omode != mode )
1044 return chmod( path, tmode );
1050 mode_t omode(
PathInfo( path ).st_mode() );
1051 mode_t tmode( omode & ~mode );
1052 if ( omode != mode )
1053 return chmod( path, tmode );
1066 int fd = open( file.
asString().c_str(), O_RDONLY|O_CLOEXEC );
1069 const int magicSize = 3;
1070 unsigned char magic[magicSize];
1071 memset( magic, 0, magicSize );
1072 if (
read( fd, magic, magicSize ) == magicSize ) {
1073 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1075 }
else if ( magic[0] ==
'B' && magic[1] ==
'Z' && magic[2] ==
'h' ) {
1094 if ( statvfs( path_r.
c_str(), &sb ) == 0 )
1096 ret = sb.f_bfree * sb.f_bsize;
1108 mode_t mask = ::umask( 0022 );
1129 int fd = ::creat( path.c_str(), mode );
1152 return chmod( path, mode );
1157 int fd = ::creat( path.c_str(), mode );
1171 MIL <<
"touch " << path;
1172 struct ::utimbuf times;
1173 times.actime = ::time( 0 );
1174 times.modtime = ::time( 0 );
1175 if ( ::utime( path.asString().c_str(), × ) == -1 ) {
FileType fileType() const
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
std::string digest()
get hex string representation of the digest
Listentry returned by readdir.
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
std::string basename() const
Return the last component of this path.
std::string octstring(char n, int w=4)
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
unsigned int devMinor() const
Convenience errno wrapper.
std::string md5sum(const Pathname &file)
Compute a files md5sum.
int error() const
Return error returned from last stat/lstat call.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
bool lstat()
LSTAT current path.
bool isExist() const
Return whether valid stat info exists.
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
String matching (STRING|SUBSTRING|GLOB|REGEX).
Store and operate with byte count.
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
bool empty() const
Test for an empty path.
const std::string & asString() const
String representation.
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like '::link'.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Pathname dirname() const
Return all but the last component od this path.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
unsigned int minor() const ZYPP_DEPRECATED
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
bool relative() const
Test for a relative path.
bool stat()
STAT current path.
Provide a new empty temporary file and delete it when no longer needed.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
map< string, string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
FileType fileType() const
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
ZIP_TYPE zipType(const Pathname &file)
unsigned int major() const ZYPP_DEPRECATED
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
const StrMatcher & matchNoDots()
Convenience returning StrMatcher( "[^.]*", Match::GLOB )
std::ostream & dumpRange(std::ostream &str, _Iterator begin, _Iterator 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).
bool operator()()
Restat current path using current mode.
const std::string & asString() const
Return current Pathname as String.
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
int unlink(const Pathname &path)
Like 'unlink'.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
const char * c_str() const
String representation.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
bool operator==(const DirEntry &rhs) const
std::list< DirEntry > DirContent
Returned by readdir.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
std::string receiveLine()
Read one line from the input stream.
std::string checksum() const
int touch(const Pathname &path)
Change file's modification and access times.
int close()
Wait for the progamm to complete.
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
int rmdir(const Pathname &path)
Like 'rmdir'.
mode_t getUmask()
Get the current umask (file mode creation mask)
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
Wrapper class for mode_t values as derived from ::stat.
unsigned int devMajor() const
FileType
File type information.
static int recursive_rmdir_1(const Pathname &dir)
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
int erase(const Pathname &path)
Erase whatever happens to be located at path (file or directory).
Reference counted access to a _Tp object calling a custom Dispose function when the last AutoDispose ...
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
Wrapper class for ::stat/::lstat.
int dirForEach(const Pathname &dir_r, function< bool(const Pathname &, const char *const)> fnc_r)
Invoke callback function fnc_r for each entry in directory dir_r.
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...
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
mode_t userMay() const
Returns current users permission ([0-7])
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
std::string strerror(int errno_r)
Return string describing the error_r code.
int __Log_Result(const int res, const char *rclass=0)
std::ostream & operator<<(std::ostream &str, const Glob &obj)
bool lstat(const Pathname &path)
LSTAT path.
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.