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 MIL << endl, doLogResult 288 inline int doLogResult(
const int res,
const char * rclass = 0 )
293 WAR <<
" FAILED: " << rclass <<
" " << res << endl;
338 while ( (pos = spath.find(
'/',lastpos)) != string::npos )
340 string dir( spath.substr(0,pos) );
341 ret =
::mkdir( dir.c_str(), mode );
344 if ( errno == EEXIST )
349 WAR <<
" FAILED: mkdir " << dir <<
' ' <<
str::octstring( mode ) <<
" errno " << ret << endl;
369 MIL <<
"rmdir " << path;
370 if ( ::
rmdir( path.asString().c_str() ) == -1 ) {
386 if ( ! (dp = opendir( dir.
c_str() )) )
391 std::string direntry = d->d_name;
392 if ( direntry ==
"." || direntry ==
".." )
394 Pathname new_path( dir / d->d_name );
397 if ( ! lstat( new_path.
c_str(), &st ) )
399 if ( S_ISDIR( st.st_mode ) )
407 if ( removeDir && ::
rmdir( dir.
c_str() ) < 0 )
415 MIL <<
"recursive_rmdir " << path <<
' ';
436 MIL <<
"clean_dir " << path <<
' ';
457 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
470 if ( tp.isExist() ) {
475 const char *
const argv[] = {
480 destpath.asString().c_str(),
485 MIL <<
" " << output;
487 int ret = prog.
close();
498 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
510 if ( srcpath == destpath ) {
514 std::string src( srcpath.
asString());
516 const char *
const argv[] = {
521 destpath.asString().c_str(),
526 MIL <<
" " << output;
528 int ret = prog.
close();
548 []( DIR * dir_r ) {
if ( dir_r ) ::closedir( dir_r ); } );
550 MIL <<
"readdir " << dir_r <<
' ';
556 for (
struct dirent * entry = ::
readdir( dir ); entry; entry =
::readdir( dir ) )
558 if ( entry->d_name[0] ==
'.' && ( entry->d_name[1] ==
'\0' || ( entry->d_name[1] ==
'.' && entry->d_name[2] ==
'\0' ) ) )
561 if ( ! fnc_r( dir_r, entry->d_name ) )
577 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool 579 if ( ( nodots && name_r[0] ==
'.' ) || ! matcher_r( name_r ) )
581 return fnc_r( dir_r, name_r );
589 int readdir( std::list<std::string> & retlist_r,
const Pathname & path_r,
bool dots_r )
593 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool 595 if ( dots_r || name_r[0] !=
'.' )
596 retlist_r.push_back( name_r );
606 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool 608 if ( dots_r || name_r[0] !=
'.' )
609 retlist_r.push_back( dir_r/name_r );
626 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool 628 if ( dots_r || name_r[0] !=
'.' )
629 retlist_r.push_back(
DirEntry( name_r,
PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
644 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool 655 MIL <<
"unlink " << path;
656 if ( ::
unlink( path.asString().c_str() ) == -1 ) {
671 if ( ret == -1 && errno == EXDEV ) {
672 const char *
const argv[] = {
679 for (
string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
680 MIL <<
" " << output;
697 MIL <<
"rename " << oldpath <<
" -> " << newpath;
698 if ( safe_rename( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 ) {
711 MIL <<
"exchange " << lpath <<
" <-> " << rpath;
712 if ( lpath.
empty() || rpath.empty() )
727 if ( safe_rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
740 if ( safe_rename( lpath.
c_str(), rpath.c_str() ) == -1 ) {
753 if ( safe_rename( lpath.
c_str(), tmp.c_str() ) == -1 ) {
756 if ( safe_rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
757 safe_rename( tmp.c_str(), lpath.
c_str() );
760 if ( safe_rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
761 safe_rename( lpath.
c_str(), rpath.c_str() );
762 safe_rename( tmp.c_str(), lpath.
c_str() );
775 MIL <<
"copy " << file <<
" -> " << dest <<
' ';
787 const char *
const argv[] = {
789 "--remove-destination",
792 dest.asString().c_str(),
797 MIL <<
" " << output;
799 int ret = prog.
close();
810 MIL <<
"symlink " << newpath <<
" -> " << oldpath;
811 if ( ::
symlink( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
824 MIL <<
"hardlink " << newpath <<
" -> " << oldpath;
825 if ( ::link( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
838 MIL <<
"hardlinkCopy " << oldpath <<
" -> " << newpath;
844 MIL <<
" => copy" << endl;
845 return copy( oldpath, newpath );
851 int res =
unlink( newpath );
857 if ( ::link( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 )
863 MIL <<
" => copy" << endl;
864 return copy( oldpath, newpath );
879 static const ssize_t bufsiz = 2047;
880 static char buf[bufsiz+1];
885 MIL <<
"readlink " << symlink_r;
900 static const unsigned int level_limit = 256;
901 static unsigned int count;
905 for (count = level_limit; info.
isLink() && count; count--)
907 DBG <<
"following symlink " << path;
909 DBG <<
"->" << path << std::endl;
916 ERR <<
"Expand level limit reached. Probably a cyclic symbolic link." << endl;
920 else if (count < level_limit)
928 ERR << path <<
" is broken (expanded from " << path_r <<
")" << endl;
934 DBG <<
"not a symlink" << endl;
945 MIL <<
"copy_file2dir " << file <<
" -> " << dest <<
' ';
957 const char *
const argv[] = {
961 dest.asString().c_str(),
966 MIL <<
" " << output;
968 int ret = prog.
close();
979 if ( !
PathInfo( file ).isFile() ) {
982 std::ifstream istr( file.
asString().c_str() );
1006 if ( !
PathInfo( file ).isFile() ) {
1009 std::ifstream istr( file.
asString().c_str() );
1056 mode_t omode(
PathInfo( path ).st_mode() );
1057 mode_t tmode( omode | mode );
1058 if ( omode != mode )
1059 return chmod( path, tmode );
1065 mode_t omode(
PathInfo( path ).st_mode() );
1066 mode_t tmode( omode & ~mode );
1067 if ( omode != mode )
1068 return chmod( path, tmode );
1081 int fd = open( file.
asString().c_str(), O_RDONLY|O_CLOEXEC );
1084 const int magicSize = 3;
1085 unsigned char magic[magicSize];
1086 memset( magic, 0, magicSize );
1087 if (
read( fd, magic, magicSize ) == magicSize ) {
1088 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1090 }
else if ( magic[0] ==
'B' && magic[1] ==
'Z' && magic[2] ==
'h' ) {
1109 if ( statvfs( path_r.
c_str(), &sb ) == 0 )
1111 ret = sb.f_bfree * sb.f_bsize;
1123 mode_t mask = ::umask( 0022 );
1144 int fd = ::creat( path.c_str(), mode );
1159 MIL <<
"touch " << path;
1160 struct ::utimbuf times;
1161 times.actime = ::time( 0 );
1162 times.modtime = ::time( 0 );
1163 if ( ::utime( path.asString().c_str(), × ) == -1 ) {
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 octstring(char n, int w=4)
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Convenience errno wrapper.
std::string md5sum(const Pathname &file)
Compute a files md5sum.
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
bool lstat()
LSTAT current path.
String matching (STRING|SUBSTRING|GLOB|REGEX).
Store and operate with byte count.
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
bool operator==(const DirEntry &rhs) const
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like '::link'.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\ ", const std::string &sep="\ ", const std::string &sfx="\, const std::string &extro="}")
Print range defined by iterators (multiline style).
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
const char * c_str() const
String representation.
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
String related utilities and Regular expression matching.
mode_t userMay() const
Returns current users permission ([0-7])
bool stat()
STAT current path.
std::string basename() const
Return the last component of this path.
Provide a new empty temporary file and delete it when no longer needed.
unsigned int devMinor() const
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'.
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
ZIP_TYPE zipType(const Pathname &file)
bool empty() const
Test for an empty path.
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
const StrMatcher & matchNoDots()
Convenience returning StrMatcher( "[^.]*", Match::GLOB )
bool operator()()
Restat current path using current mode.
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'.
const std::string & asString() const
String representation.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
bool isExist() const
Return whether valid stat info exists.
Pathname dirname() const
Return all but the last component od this path.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
int error() const
Return error returned from last stat/lstat call.
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.
const std::string & asString() const
Return current Pathname as String.
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.
FileType
File type information.
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 h...
bool relative() const
Test for a relative path.
unsigned int devMajor() const
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.
FileType fileType() const
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.
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Easy-to use interface to the ZYPP dependency resolver.
bool lstat(const Pathname &path)
LSTAT path.
static int recursive_rmdir_1(const Pathname &dir, bool removeDir=true)
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
FileType fileType() const
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.