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 );
273 str <<
" size " << obj.
size();
292 #define _Log_Result MIL << endl, __Log_Result
298 WAR <<
" FAILED: " << rclass <<
" " << res << endl;
342 while ( (pos = spath.find(
'/',lastpos)) != 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;
374 if ( ::
rmdir( path.asString().c_str() ) == -1 ) {
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 ) )
419 MIL <<
"recursive_rmdir " << path <<
' ';
440 MIL <<
"clean_dir " << path <<
' ';
451 string cmd(
str::form(
"cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
454 MIL <<
" " << output;
456 int ret = prog.
close();
467 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
480 if ( tp.isExist() ) {
485 const char *
const argv[] = {
490 destpath.asString().c_str(),
495 MIL <<
" " << output;
497 int ret = prog.
close();
508 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
520 if ( srcpath == destpath ) {
524 std::string src( srcpath.
asString());
526 const char *
const argv[] = {
531 destpath.asString().c_str(),
536 MIL <<
" " << output;
538 int ret = prog.
close();
558 []( DIR * dir_r ) {
if ( dir_r ) ::closedir( dir_r ); } );
560 MIL <<
"readdir " << dir_r <<
' ';
566 for (
struct dirent * entry = ::
readdir( dir ); entry; entry =
::readdir( dir ) )
568 if ( entry->d_name[0] ==
'.' && ( entry->d_name[1] ==
'\0' || ( entry->d_name[1] ==
'.' && entry->d_name[2] ==
'\0' ) ) )
571 if ( ! fnc_r( dir_r, entry->d_name ) )
587 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
589 if ( ( nodots && name_r[0] ==
'.' ) || ! matcher_r( name_r ) )
591 return fnc_r( dir_r, name_r );
599 int readdir( std::list<std::string> & retlist_r,
const Pathname & path_r,
bool dots_r )
603 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
605 if ( dots_r || name_r[0] !=
'.' )
606 retlist_r.push_back( name_r );
616 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
618 if ( dots_r || name_r[0] !=
'.' )
619 retlist_r.push_back( dir_r/name_r );
636 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
638 if ( dots_r || name_r[0] !=
'.' )
639 retlist_r.push_back(
DirEntry( name_r,
PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
651 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
662 MIL <<
"unlink " << path;
663 if ( ::
unlink( path.asString().c_str() ) == -1 ) {
676 MIL <<
"rename " << oldpath <<
" -> " << newpath;
677 if ( ::
rename( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 ) {
690 MIL <<
"exchange " << lpath <<
" <-> " << rpath;
691 if ( lpath.
empty() || rpath.empty() )
706 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
719 if ( ::
rename( lpath.
c_str(), rpath.c_str() ) == -1 ) {
732 if ( ::
rename( lpath.
c_str(), tmp.c_str() ) == -1 ) {
735 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
739 if ( ::
rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
754 MIL <<
"copy " << file <<
" -> " << dest <<
' ';
766 const char *
const argv[] = {
768 "--remove-destination",
771 dest.asString().c_str(),
776 MIL <<
" " << output;
778 int ret = prog.
close();
789 MIL <<
"symlink " << newpath <<
" -> " << oldpath;
790 if ( ::
symlink( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
803 MIL <<
"hardlink " << newpath <<
" -> " << oldpath;
804 if ( ::link( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
817 MIL <<
"hardlinkCopy " << oldpath <<
" -> " << newpath;
823 return copy( oldpath, newpath );
829 int res =
unlink( newpath );
835 if ( ::link( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 )
840 return copy( oldpath, newpath );
855 static const ssize_t bufsiz = 2047;
856 static char buf[bufsiz+1];
861 MIL <<
"readlink " << symlink_r;
876 static const unsigned int level_limit = 256;
877 static unsigned int count;
881 for (count = level_limit; info.
isLink() && count; count--)
883 DBG <<
"following symlink " << path;
885 DBG <<
"->" << path << std::endl;
892 ERR <<
"Expand level limit reached. Probably a cyclic symbolic link." << endl;
896 else if (count < level_limit)
904 ERR << path <<
" is broken (expanded from " << path_r <<
")" << endl;
910 DBG <<
"not a symlink" << endl;
921 MIL <<
"copy_file2dir " << file <<
" -> " << dest <<
' ';
933 const char *
const argv[] = {
937 dest.asString().c_str(),
942 MIL <<
" " << output;
944 int ret = prog.
close();
955 if ( !
PathInfo( file ).isFile() ) {
958 std::ifstream istr( file.
asString().c_str() );
982 if ( !
PathInfo( file ).isFile() ) {
985 std::ifstream istr( file.
asString().c_str() );
1032 mode_t omode(
PathInfo( path ).st_mode() );
1033 mode_t tmode( omode | mode );
1034 if ( omode != mode )
1035 return chmod( path, tmode );
1041 mode_t omode(
PathInfo( path ).st_mode() );
1042 mode_t tmode( omode & ~mode );
1043 if ( omode != mode )
1044 return chmod( path, tmode );
1057 int fd = open( file.
asString().c_str(), O_RDONLY|O_CLOEXEC );
1060 const int magicSize = 3;
1061 unsigned char magic[magicSize];
1062 memset( magic, 0, magicSize );
1063 if (
read( fd, magic, magicSize ) == magicSize ) {
1064 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1066 }
else if ( magic[0] ==
'B' && magic[1] ==
'Z' && magic[2] ==
'h' ) {
1085 if ( statvfs( path_r.
c_str(), &sb ) == 0 )
1087 ret = sb.f_bfree * sb.f_bsize;
1099 mode_t mask = ::umask( 0022 );
1120 int fd = ::creat( path.c_str(), mode );
1135 MIL <<
"touch " << path;
1136 struct ::utimbuf times;
1137 times.actime = ::time( 0 );
1138 times.modtime = ::time( 0 );
1139 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.
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/ *'.
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.
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 minor() const
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.
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'.
std::string form(const char *format,...)
Printf style construction of std::string.
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
unsigned int major() const
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.
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 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.