*/
unsigned int PathInfo::devMajor() const
{
- return isBlk() || isChr() ? major(statbuf_C.st_rdev) : 0;
+ return isBlk() || isChr() ? ::major(statbuf_C.st_rdev) : 0;
}
/******************************************************************
*/
unsigned int PathInfo::devMinor() const
{
- return isBlk() || isChr() ? minor(statbuf_C.st_rdev) : 0;
+ return isBlk() || isChr() ? ::minor(statbuf_C.st_rdev) : 0;
}
+ unsigned int PathInfo::major() const
+ { INT << "Cleanup the code: This method is deprecated" << endl; return devMajor(); }
+ unsigned int PathInfo::minor() const
+ { INT << "Cleanup the code: This method is deprecated" << endl; return devMinor(); }
+
/******************************************************************
**
** FUNCTION NAME : operator<<
//
///////////////////////////////////////////////////////////////////
-#define logResult MIL << endl, doLogResult
- namespace {
- /** Helper function to log return values. */
- inline int doLogResult( const int res, const char * rclass = 0 /*errno*/ )
+ /******************************************************************
+ **
+ ** FUNCTION NAME : _Log_Result
+ ** FUNCTION TYPE : int
+ **
+ ** DESCRIPTION : Helper function to log return values.
+ */
+#define _Log_Result MIL << endl, __Log_Result
+ inline int __Log_Result( const int res, const char * rclass = 0 /*errno*/ )
+ {
+ if ( res )
{
- if ( res )
- {
- if ( rclass )
- WAR << " FAILED: " << rclass << " " << res << endl;
- else
- WAR << " FAILED: " << str::strerror( res ) << endl;
- }
- return res;
+ if ( rclass )
+ WAR << " FAILED: " << rclass << " " << res << endl;
+ else
+ WAR << " FAILED: " << str::strerror( res ) << endl;
}
- } // namespace
+ return res;
+ }
///////////////////////////////////////////////////////////////////
//
{
MIL << "mkdir " << path << ' ' << str::octstring( mode );
if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
MIL << "rmdir " << path;
if ( ::rmdir( path.asString().c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
// METHOD NAME : recursive_rmdir
// METHOD TYPE : int
//
- static int recursive_rmdir_1( const Pathname & dir, bool removeDir = true )
+ static int recursive_rmdir_1( const Pathname & dir )
{
DIR * dp;
struct dirent * d;
if ( ! (dp = opendir( dir.c_str() )) )
- return logResult( errno );
+ return _Log_Result( errno );
while ( (d = readdir(dp)) )
{
}
closedir( dp );
- if ( removeDir && ::rmdir( dir.c_str() ) < 0 )
+ if ( ::rmdir( dir.c_str() ) < 0 )
return errno;
return 0;
PathInfo p( path );
if ( !p.isExist() ) {
- return logResult( 0 );
+ return _Log_Result( 0 );
}
if ( !p.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
- return logResult( recursive_rmdir_1( path ) );
+ return _Log_Result( recursive_rmdir_1( path ) );
}
///////////////////////////////////////////////////////////////////
PathInfo p( path );
if ( !p.isExist() ) {
- return logResult( 0 );
+ return _Log_Result( 0 );
}
if ( !p.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
- return logResult( recursive_rmdir_1( path, false/* don't remove path itself */ ) );
+ string cmd( str::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
+ ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
+ for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
+ MIL << " " << output;
+ }
+ int ret = prog.close();
+ return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
PathInfo sp( srcpath );
if ( !sp.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
PathInfo dp( destpath );
if ( !dp.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
PathInfo tp( destpath + srcpath.basename() );
if ( tp.isExist() ) {
- return logResult( EEXIST );
+ return _Log_Result( EEXIST );
}
MIL << " " << output;
}
int ret = prog.close();
- return logResult( ret, "returned" );
+ return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
PathInfo sp( srcpath );
if ( !sp.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
PathInfo dp( destpath );
if ( !dp.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
if ( srcpath == destpath ) {
- return logResult( EEXIST );
+ return _Log_Result( EEXIST );
}
std::string src( srcpath.asString());
MIL << " " << output;
}
int ret = prog.close();
- return logResult( ret, "returned" );
+ return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////////
MIL << "readdir " << dir_r << ' ';
if ( ! dir )
- return logResult( errno );
+ return _Log_Result( errno );
MIL << endl; // close line before callbacks are invoked.
int ret = 0;
{
MIL << "unlink " << path;
if ( ::unlink( path.asString().c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
MIL << "rename " << oldpath << " -> " << newpath;
if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
MIL << "exchange " << lpath << " <-> " << rpath;
if ( lpath.empty() || rpath.empty() )
- return logResult( EINVAL );
+ return _Log_Result( EINVAL );
PathInfo linfo( lpath );
PathInfo rinfo( rpath );
if ( ! linfo.isExist() )
{
if ( ! rinfo.isExist() )
- return logResult( 0 ); // both don't exist.
+ return _Log_Result( 0 ); // both don't exist.
// just rename rpath -> lpath
int ret = assert_dir( lpath.dirname() );
if ( ret != 0 )
- return logResult( ret );
+ return _Log_Result( ret );
if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
// HERE: lpath exists:
// just rename lpath -> rpath
int ret = assert_dir( rpath.dirname() );
if ( ret != 0 )
- return logResult( ret );
+ return _Log_Result( ret );
if ( ::rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
// HERE: both exist
TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
if ( ! tmpfile )
- return logResult( errno );
+ return _Log_Result( errno );
Pathname tmp( tmpfile.path() );
::unlink( tmp.c_str() );
if ( ::rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
::rename( tmp.c_str(), lpath.c_str() );
- return logResult( errno );
+ return _Log_Result( errno );
}
if ( ::rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
::rename( lpath.c_str(), rpath.c_str() );
::rename( tmp.c_str(), lpath.c_str() );
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
PathInfo sp( file );
if ( !sp.isFile() ) {
- return logResult( EINVAL );
+ return _Log_Result( EINVAL );
}
PathInfo dp( dest );
if ( dp.isDir() ) {
- return logResult( EISDIR );
+ return _Log_Result( EISDIR );
}
const char *const argv[] = {
MIL << " " << output;
}
int ret = prog.close();
- return logResult( ret, "returned" );
+ return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
{
MIL << "symlink " << newpath << " -> " << oldpath;
if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
MIL << "hardlink " << newpath << " -> " << oldpath;
if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
int res = unlink( newpath );
if ( res != 0 )
- return logResult( res );
+ return _Log_Result( res );
}
// Here: no symlink, no newpath
return copy( oldpath, newpath );
break;
}
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
{
target_r = Pathname();
MIL << "readlink " << symlink_r;
- return logResult( errno );
+ return _Log_Result( errno );
}
buf[ret] = '\0';
target_r = buf;
PathInfo sp( file );
if ( !sp.isFile() ) {
- return logResult( EINVAL );
+ return _Log_Result( EINVAL );
}
PathInfo dp( dest );
if ( !dp.isDir() ) {
- return logResult( ENOTDIR );
+ return _Log_Result( ENOTDIR );
}
const char *const argv[] = {
MIL << " " << output;
}
int ret = prog.close();
- return logResult( ret, "returned" );
+ return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
{
MIL << "chmod " << path << ' ' << str::octstring( mode );
if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
int addmod( const Pathname & path, mode_t mode )
int ret = assert_dir( path.dirname() );
MIL << "assert_file " << str::octstring( mode ) << " " << path;
if ( ret != 0 )
- return logResult( ret );
+ return _Log_Result( ret );
PathInfo pi( path );
if ( pi.isExist() )
- return logResult( pi.isFile() ? 0 : EEXIST );
+ return _Log_Result( pi.isFile() ? 0 : EEXIST );
int fd = ::creat( path.c_str(), mode );
if ( fd == -1 )
- return logResult( errno );
+ return _Log_Result( errno );
::close( fd );
- return logResult( 0 );
+ return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
times.actime = ::time( 0 );
times.modtime = ::time( 0 );
if ( ::utime( path.asString().c_str(), × ) == -1 ) {
- return logResult( errno );
+ return _Log_Result( errno );
}
- return logResult( 0 );
+ return _Log_Result( 0 );
}
/////////////////////////////////////////////////////////////////