Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / PathInfo.cc
index 5cbc827..08c1a10 100644 (file)
@@ -241,7 +241,7 @@ namespace zypp
      */
     unsigned int PathInfo::devMajor() const
     {
-      return isBlk() || isChr() ? major(statbuf_C.st_rdev) : 0;
+      return isBlk() || isChr() ? ::major(statbuf_C.st_rdev) : 0;
     }
 
     /******************************************************************
@@ -251,9 +251,14 @@ namespace zypp
      */
     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<<
@@ -282,21 +287,25 @@ namespace zypp
     //
     ///////////////////////////////////////////////////////////////////
 
-#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;
+    }
 
     ///////////////////////////////////////////////////////////////////
     //
@@ -307,9 +316,9 @@ namespace zypp
     {
       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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -368,9 +377,9 @@ namespace zypp
     {
       MIL << "rmdir " << path;
       if ( ::rmdir( path.asString().c_str() ) == -1 ) {
-        return logResult( errno );
+        return _Log_Result( errno );
       }
-      return logResult( 0 );
+      return _Log_Result( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -378,13 +387,13 @@ namespace zypp
     // 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)) )
       {
@@ -404,7 +413,7 @@ namespace zypp
       }
       closedir( dp );
 
-      if ( removeDir && ::rmdir( dir.c_str() ) < 0 )
+      if ( ::rmdir( dir.c_str() ) < 0 )
         return errno;
 
       return 0;
@@ -416,14 +425,14 @@ namespace zypp
       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 ) );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -437,14 +446,20 @@ namespace zypp
       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" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -458,17 +473,17 @@ namespace zypp
 
       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 );
       }
 
 
@@ -485,7 +500,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return logResult( ret, "returned" );
+      return _Log_Result( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -499,16 +514,16 @@ namespace zypp
 
       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());
@@ -526,7 +541,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return logResult( ret, "returned" );
+      return _Log_Result( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////////
@@ -549,7 +564,7 @@ namespace zypp
 
       MIL << "readdir " << dir_r << ' ';
       if ( ! dir )
-       return logResult( errno );
+       return _Log_Result( errno );
       MIL << endl; // close line before callbacks are invoked.
 
       int ret = 0;
@@ -654,9 +669,9 @@ namespace zypp
     {
       MIL << "unlink " << path;
       if ( ::unlink( path.asString().c_str() ) == -1 ) {
-        return logResult( errno );
+        return _Log_Result( errno );
       }
-      return logResult( 0 );
+      return _Log_Result( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -668,9 +683,9 @@ namespace zypp
     {
       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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -682,7 +697,7 @@ namespace zypp
     {
       MIL << "exchange " << lpath << " <-> " << rpath;
       if ( lpath.empty() || rpath.empty() )
-        return logResult( EINVAL );
+        return _Log_Result( EINVAL );
 
       PathInfo linfo( lpath );
       PathInfo rinfo( rpath );
@@ -690,16 +705,16 @@ namespace zypp
       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:
@@ -708,33 +723,33 @@ namespace zypp
         // 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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -748,12 +763,12 @@ namespace zypp
 
       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[] = {
@@ -769,7 +784,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return logResult( ret, "returned" );
+      return _Log_Result( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -781,9 +796,9 @@ namespace zypp
     {
       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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -795,9 +810,9 @@ namespace zypp
     {
       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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -821,7 +836,7 @@ namespace zypp
       {
        int res = unlink( newpath );
        if ( res != 0 )
-         return logResult( res );
+         return _Log_Result( res );
       }
 
       // Here: no symlink, no newpath
@@ -834,9 +849,9 @@ namespace zypp
             return copy( oldpath, newpath );
             break;
         }
-        return logResult( errno );
+        return _Log_Result( errno );
       }
-      return logResult( 0 );
+      return _Log_Result( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -853,7 +868,7 @@ namespace zypp
       {
         target_r = Pathname();
         MIL << "readlink " << symlink_r;
-        return logResult( errno );
+        return _Log_Result( errno );
       }
       buf[ret] = '\0';
       target_r = buf;
@@ -916,12 +931,12 @@ namespace zypp
 
       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[] = {
@@ -936,7 +951,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return logResult( ret, "returned" );
+      return _Log_Result( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -1016,9 +1031,9 @@ namespace zypp
     {
       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 )
@@ -1105,18 +1120,18 @@ namespace zypp
       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 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -1131,9 +1146,9 @@ namespace zypp
       times.actime = ::time( 0 );
       times.modtime = ::time( 0 );
       if ( ::utime( path.asString().c_str(), &times ) == -1 ) {
-        return logResult( errno );
+        return _Log_Result( errno );
       }
-      return logResult( 0 );
+      return _Log_Result( 0 );
     }
 
     /////////////////////////////////////////////////////////////////