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 ) {