Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / PathInfo.cc
index 08c1a10..5cbc827 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,14 +251,9 @@ 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<<
@@ -287,25 +282,21 @@ namespace zypp
     //
     ///////////////////////////////////////////////////////////////////
 
-    /******************************************************************
-     **
-     **        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 )
+#define logResult MIL << endl, doLogResult
+    namespace {
+      /**  Helper function to log return values. */
+      inline int doLogResult( const int res, const char * rclass = 0 /*errno*/ )
       {
-        if ( rclass )
-          WAR << " FAILED: " << rclass << " " << res << endl;
-        else
-          WAR << " FAILED: " << str::strerror( res ) << endl;
+       if ( res )
+       {
+         if ( rclass )
+           WAR << " FAILED: " << rclass << " " << res << endl;
+         else
+           WAR << " FAILED: " << str::strerror( res ) << endl;
+       }
+       return res;
       }
-      return res;
-    }
+    } // namespace
 
     ///////////////////////////////////////////////////////////////////
     //
@@ -316,9 +307,9 @@ namespace zypp
     {
       MIL << "mkdir " << path << ' ' << str::octstring( mode );
       if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -377,9 +368,9 @@ namespace zypp
     {
       MIL << "rmdir " << path;
       if ( ::rmdir( path.asString().c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -387,13 +378,13 @@ namespace zypp
     // METHOD NAME : recursive_rmdir
     // METHOD TYPE : int
     //
-    static int recursive_rmdir_1( const Pathname & dir )
+    static int recursive_rmdir_1( const Pathname & dir, bool removeDir = true )
     {
       DIR * dp;
       struct dirent * d;
 
       if ( ! (dp = opendir( dir.c_str() )) )
-        return _Log_Result( errno );
+        return logResult( errno );
 
       while ( (d = readdir(dp)) )
       {
@@ -413,7 +404,7 @@ namespace zypp
       }
       closedir( dp );
 
-      if ( ::rmdir( dir.c_str() ) < 0 )
+      if ( removeDir && ::rmdir( dir.c_str() ) < 0 )
         return errno;
 
       return 0;
@@ -425,14 +416,14 @@ namespace zypp
       PathInfo p( path );
 
       if ( !p.isExist() ) {
-        return _Log_Result( 0 );
+        return logResult( 0 );
       }
 
       if ( !p.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
-      return _Log_Result( recursive_rmdir_1( path ) );
+      return logResult( recursive_rmdir_1( path ) );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -446,20 +437,14 @@ namespace zypp
       PathInfo p( path );
 
       if ( !p.isExist() ) {
-        return _Log_Result( 0 );
+        return logResult( 0 );
       }
 
       if ( !p.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
-      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" );
+      return logResult( recursive_rmdir_1( path, false/* don't remove path itself */ ) );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -473,17 +458,17 @@ namespace zypp
 
       PathInfo sp( srcpath );
       if ( !sp.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
       PathInfo dp( destpath );
       if ( !dp.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
       PathInfo tp( destpath + srcpath.basename() );
       if ( tp.isExist() ) {
-        return _Log_Result( EEXIST );
+        return logResult( EEXIST );
       }
 
 
@@ -500,7 +485,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return _Log_Result( ret, "returned" );
+      return logResult( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -514,16 +499,16 @@ namespace zypp
 
       PathInfo sp( srcpath );
       if ( !sp.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
       PathInfo dp( destpath );
       if ( !dp.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
       if ( srcpath == destpath ) {
-        return _Log_Result( EEXIST );
+        return logResult( EEXIST );
       }
 
       std::string src( srcpath.asString());
@@ -541,7 +526,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return _Log_Result( ret, "returned" );
+      return logResult( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////////
@@ -564,7 +549,7 @@ namespace zypp
 
       MIL << "readdir " << dir_r << ' ';
       if ( ! dir )
-       return _Log_Result( errno );
+       return logResult( errno );
       MIL << endl; // close line before callbacks are invoked.
 
       int ret = 0;
@@ -669,9 +654,9 @@ namespace zypp
     {
       MIL << "unlink " << path;
       if ( ::unlink( path.asString().c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -683,9 +668,9 @@ namespace zypp
     {
       MIL << "rename " << oldpath << " -> " << newpath;
       if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -697,7 +682,7 @@ namespace zypp
     {
       MIL << "exchange " << lpath << " <-> " << rpath;
       if ( lpath.empty() || rpath.empty() )
-        return _Log_Result( EINVAL );
+        return logResult( EINVAL );
 
       PathInfo linfo( lpath );
       PathInfo rinfo( rpath );
@@ -705,16 +690,16 @@ namespace zypp
       if ( ! linfo.isExist() )
       {
         if ( ! rinfo.isExist() )
-          return _Log_Result( 0 ); // both don't exist.
+          return logResult( 0 ); // both don't exist.
 
         // just rename rpath -> lpath
         int ret = assert_dir( lpath.dirname() );
         if ( ret != 0 )
-          return _Log_Result( ret );
+          return logResult( ret );
         if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
-          return _Log_Result( errno );
+          return logResult( errno );
         }
-        return _Log_Result( 0 );
+        return logResult( 0 );
       }
 
       // HERE: lpath exists:
@@ -723,33 +708,33 @@ namespace zypp
         // just rename lpath -> rpath
         int ret = assert_dir( rpath.dirname() );
         if ( ret != 0 )
-          return _Log_Result( ret );
+          return logResult( ret );
         if ( ::rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
-          return _Log_Result( errno );
+          return logResult( errno );
         }
-        return _Log_Result( 0 );
+        return logResult( 0 );
       }
 
       // HERE: both exist
       TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
       if ( ! tmpfile )
-        return _Log_Result( errno );
+        return logResult( errno );
       Pathname tmp( tmpfile.path() );
       ::unlink( tmp.c_str() );
 
       if ( ::rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
       if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
         ::rename( tmp.c_str(), lpath.c_str() );
-        return _Log_Result( errno );
+        return logResult( 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 _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -763,12 +748,12 @@ namespace zypp
 
       PathInfo sp( file );
       if ( !sp.isFile() ) {
-        return _Log_Result( EINVAL );
+        return logResult( EINVAL );
       }
 
       PathInfo dp( dest );
       if ( dp.isDir() ) {
-        return _Log_Result( EISDIR );
+        return logResult( EISDIR );
       }
 
       const char *const argv[] = {
@@ -784,7 +769,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return _Log_Result( ret, "returned" );
+      return logResult( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -796,9 +781,9 @@ namespace zypp
     {
       MIL << "symlink " << newpath << " -> " << oldpath;
       if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -810,9 +795,9 @@ namespace zypp
     {
       MIL << "hardlink " << newpath << " -> " << oldpath;
       if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -836,7 +821,7 @@ namespace zypp
       {
        int res = unlink( newpath );
        if ( res != 0 )
-         return _Log_Result( res );
+         return logResult( res );
       }
 
       // Here: no symlink, no newpath
@@ -849,9 +834,9 @@ namespace zypp
             return copy( oldpath, newpath );
             break;
         }
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -868,7 +853,7 @@ namespace zypp
       {
         target_r = Pathname();
         MIL << "readlink " << symlink_r;
-        return _Log_Result( errno );
+        return logResult( errno );
       }
       buf[ret] = '\0';
       target_r = buf;
@@ -931,12 +916,12 @@ namespace zypp
 
       PathInfo sp( file );
       if ( !sp.isFile() ) {
-        return _Log_Result( EINVAL );
+        return logResult( EINVAL );
       }
 
       PathInfo dp( dest );
       if ( !dp.isDir() ) {
-        return _Log_Result( ENOTDIR );
+        return logResult( ENOTDIR );
       }
 
       const char *const argv[] = {
@@ -951,7 +936,7 @@ namespace zypp
         MIL << "  " << output;
       }
       int ret = prog.close();
-      return _Log_Result( ret, "returned" );
+      return logResult( ret, "returned" );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -1031,9 +1016,9 @@ namespace zypp
     {
       MIL << "chmod " << path << ' ' << str::octstring( mode );
       if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     int addmod( const Pathname & path, mode_t mode )
@@ -1120,18 +1105,18 @@ namespace zypp
       int ret = assert_dir( path.dirname() );
       MIL << "assert_file " << str::octstring( mode ) << " " << path;
       if ( ret != 0 )
-        return _Log_Result( ret );
+        return logResult( ret );
 
       PathInfo pi( path );
       if ( pi.isExist() )
-        return _Log_Result( pi.isFile() ? 0 : EEXIST );
+        return logResult( pi.isFile() ? 0 : EEXIST );
 
       int fd = ::creat( path.c_str(), mode );
       if ( fd == -1 )
-        return _Log_Result( errno );
+        return logResult( errno );
 
       ::close( fd );
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -1146,9 +1131,9 @@ namespace zypp
       times.actime = ::time( 0 );
       times.modtime = ::time( 0 );
       if ( ::utime( path.asString().c_str(), &times ) == -1 ) {
-        return _Log_Result( errno );
+        return logResult( errno );
       }
-      return _Log_Result( 0 );
+      return logResult( 0 );
     }
 
     /////////////////////////////////////////////////////////////////