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();
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 <<
' ';
428 MIL <<
"unlink symlink ";
429 if ( ::
unlink( path.asString().c_str() ) == -1 ) {
445 MIL <<
"clean_dir " << path <<
' ';
466 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
479 if ( tp.isExist() ) {
484 const char *
const argv[] = {
489 destpath.asString().c_str(),
494 MIL <<
" " << output;
496 int ret = prog.
close();
507 MIL <<
"copy_dir " << srcpath <<
" -> " << destpath <<
' ';
519 if ( srcpath == destpath ) {
523 std::string src( srcpath.
asString());
525 const char *
const argv[] = {
530 destpath.asString().c_str(),
535 MIL <<
" " << output;
537 int ret = prog.
close();
557 []( DIR * dir_r ) {
if ( dir_r ) ::closedir( dir_r ); } );
559 MIL <<
"readdir " << dir_r <<
' ';
565 for (
struct dirent * entry = ::
readdir( dir ); entry; entry =
::readdir( dir ) )
567 if ( entry->d_name[0] ==
'.' && ( entry->d_name[1] ==
'\0' || ( entry->d_name[1] ==
'.' && entry->d_name[2] ==
'\0' ) ) )
570 if ( ! fnc_r( dir_r, entry->d_name ) )
586 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
588 if ( ( nodots && name_r[0] ==
'.' ) || ! matcher_r( name_r ) )
590 return fnc_r( dir_r, name_r );
598 int readdir( std::list<std::string> & retlist_r,
const Pathname & path_r,
bool dots_r )
602 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
604 if ( dots_r || name_r[0] !=
'.' )
605 retlist_r.push_back( name_r );
615 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
617 if ( dots_r || name_r[0] !=
'.' )
618 retlist_r.push_back( dir_r/name_r );
635 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
637 if ( dots_r || name_r[0] !=
'.' )
638 retlist_r.push_back(
DirEntry( name_r,
PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
644 {
return dumpRange( str, obj.begin(), obj.end() ); }
653 [&](
const Pathname & dir_r,
const char *
const name_r )->
bool
664 MIL <<
"unlink " << path;
665 if ( ::
unlink( path.asString().c_str() ) == -1 ) {
678 MIL <<
"rename " << oldpath <<
" -> " << newpath;
679 if ( ::
rename( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 ) {
692 MIL <<
"exchange " << lpath <<
" <-> " << rpath;
693 if ( lpath.
empty() || rpath.empty() )
708 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
721 if ( ::
rename( lpath.
c_str(), rpath.c_str() ) == -1 ) {
734 if ( ::
rename( lpath.
c_str(), tmp.c_str() ) == -1 ) {
737 if ( ::
rename( rpath.c_str(), lpath.
c_str() ) == -1 ) {
741 if ( ::
rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
756 MIL <<
"copy " << file <<
" -> " << dest <<
' ';
768 const char *
const argv[] = {
770 "--remove-destination",
773 dest.asString().c_str(),
778 MIL <<
" " << output;
780 int ret = prog.
close();
791 MIL <<
"symlink " << newpath <<
" -> " << oldpath;
792 if ( ::
symlink( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
805 MIL <<
"hardlink " << newpath <<
" -> " << oldpath;
806 if ( ::link( oldpath.asString().c_str(), newpath.
asString().c_str() ) == -1 ) {
819 MIL <<
"hardlinkCopy " << oldpath <<
" -> " << newpath;
825 return copy( oldpath, newpath );
831 int res =
unlink( newpath );
837 if ( ::link( oldpath.
asString().c_str(), newpath.asString().c_str() ) == -1 )
843 return copy( oldpath, newpath );
858 static const ssize_t bufsiz = 2047;
859 static char buf[bufsiz+1];
864 MIL <<
"readlink " << symlink_r;
879 static const unsigned int level_limit = 256;
880 static unsigned int count;
884 for (count = level_limit; info.
isLink() && count; count--)
886 DBG <<
"following symlink " << path;
888 DBG <<
"->" << path << std::endl;
895 ERR <<
"Expand level limit reached. Probably a cyclic symbolic link." << endl;
899 else if (count < level_limit)
907 ERR << path <<
" is broken (expanded from " << path_r <<
")" << endl;
913 DBG <<
"not a symlink" << endl;
924 MIL <<
"copy_file2dir " << file <<
" -> " << dest <<
' ';
936 const char *
const argv[] = {
940 dest.asString().c_str(),
945 MIL <<
" " << output;
947 int ret = prog.
close();
958 if ( !
PathInfo( file ).isFile() ) {
961 std::ifstream istr( file.
asString().c_str() );
985 if ( !
PathInfo( file ).isFile() ) {
988 std::ifstream istr( file.
asString().c_str() );
1035 mode_t omode(
PathInfo( path ).st_mode() );
1036 mode_t tmode( omode | mode );
1037 if ( omode != mode )
1038 return chmod( path, tmode );
1044 mode_t omode(
PathInfo( path ).st_mode() );
1045 mode_t tmode( omode & ~mode );
1046 if ( omode != mode )
1047 return chmod( path, tmode );
1060 int fd = open( file.
asString().c_str(), O_RDONLY|O_CLOEXEC );
1063 const int magicSize = 3;
1064 unsigned char magic[magicSize];
1065 memset( magic, 0, magicSize );
1066 if (
read( fd, magic, magicSize ) == magicSize ) {
1067 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1069 }
else if ( magic[0] ==
'B' && magic[1] ==
'Z' && magic[2] ==
'h' ) {
1088 if ( statvfs( path_r.
c_str(), &sb ) == 0 )
1090 ret = sb.f_bfree * sb.f_bsize;
1102 mode_t mask = ::umask( 0022 );
1123 int fd = ::creat( path.c_str(), mode );
1146 return chmod( path, mode );
1151 int fd = ::creat( path.c_str(), mode );
1165 MIL <<
"touch " << path;
1166 struct ::utimbuf times;
1167 times.actime = ::time( 0 );
1168 times.modtime = ::time( 0 );
1169 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/ *'.
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)
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, 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).
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.
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...
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.
std::ostream & operator<<(std::ostream &str, const Glob &obj)
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
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.