Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / media / MediaDISK.cc
index 63617b3..c9f2260 100644 (file)
 #include <errno.h>
 #include <dirent.h>
 
-#define DELAYED_VERIFY    1
-
-#define VOL_ID_TOOL_PATHS { "/sbin/vol_id", "/lib/udev/vol_id", NULL}
+/*
+** verify devices names as late as possible (while attach)
+*/
+#define DELAYED_VERIFY           1
 
 using namespace std;
 
@@ -113,8 +114,8 @@ namespace zypp {
          for(it = dlist.begin(); it != dlist.end(); ++it)
          {
            PathInfo vol_info(*it);
-           if( vol_info.isBlk() && vol_info.major() == dev_info.major() &&
-                                   vol_info.minor() == dev_info.minor())
+           if( vol_info.isBlk() && vol_info.devMajor() == dev_info.devMajor() &&
+                                   vol_info.devMinor() == dev_info.devMinor())
            {
              DBG << "Specified device name " << dev_name
                  << " is a volume (disk/by-uuid link "
@@ -137,8 +138,8 @@ namespace zypp {
          for(it = dlist.begin(); it != dlist.end(); ++it)
          {
            PathInfo vol_info(*it);
-           if( vol_info.isBlk() && vol_info.major() == dev_info.major() &&
-                                   vol_info.minor() == dev_info.minor())
+           if( vol_info.isBlk() && vol_info.devMajor() == dev_info.devMajor() &&
+                                   vol_info.devMinor() == dev_info.devMinor())
            {
              DBG << "Specified device name " << dev_name
                  << " is a volume (disk/by-label link "
@@ -150,77 +151,24 @@ namespace zypp {
        }
       }
 
-      // check if a filesystem volume using the /sbin/vol_id tool
+      // check if a filesystem volume using the 'blkid' tool
       // (there is no /dev/disk link for some of them)
-      for(const char *vol_id_paths[] = VOL_ID_TOOL_PATHS,
-                    **vol_id_path    = vol_id_paths;
-         vol_id_path != NULL && *vol_id_path != NULL;
-         vol_id_path++)
+      ExternalProgram::Arguments args;
+      args.push_back( "blkid" );
+      args.push_back( "-p" );
+      args.push_back( dev_name.asString() );
+
+      ExternalProgram cmd( args, ExternalProgram::Stderr_To_Stdout );
+      cmd >> DBG;
+      if ( cmd.close() != 0 )
       {
-       PathInfo vol_id_info(*vol_id_path);
-       if( !vol_id_info.isFile() || !vol_id_info.isXUsr())
-         continue;
-
-       const char *cmd[3];
-       cmd[0] = *vol_id_path;
-       cmd[1] = dev_name.asString().c_str();
-       cmd[2] = NULL;
-
-       ExternalProgram vol_id(cmd, ExternalProgram::Stderr_To_Stdout);
-
-       std::string vol_fs_usage;
-       std::string vol_fs_uuid;
-       std::string vol_fs_type;
-
-       for(std::string out( vol_id.receiveLine());
-           out.length(); out = vol_id.receiveLine())
-       {
-         out = str::rtrim(out);
-
-         if( out.compare(0, sizeof("ID_FS_USAGE=")-1, "ID_FS_USAGE=") == 0)
-         {
-           vol_fs_usage = out.substr(sizeof("ID_FS_USAGE=")-1);
-         }
-         else
-         if( out.compare(0, sizeof("ID_FS_TYPE=")-1, "ID_FS_TYPE=")   == 0)
-         {
-           vol_fs_type  = out.substr(sizeof("ID_FS_TYPE=")-1);
-         }
-         else
-         if( out.compare(0, sizeof("ID_FS_UUID=")-1, "ID_FS_UUID=")   == 0)
-         {
-           vol_fs_uuid  = out.substr(sizeof("ID_FS_UUID=")-1);
-         }
-       }
-
-       if( vol_id.close() == 0)
-       {
-         if( vol_fs_usage == "filesystem")
-         {
-           if(vol_fs_type == "iso9660" || vol_fs_type == "udf")
-           {
-             DBG << "Specified device name " << dev_name
-                 << " is a CD/DVD volume (type " << vol_fs_type << ")"
-                 << std::endl;
-             return true;
-           }
-           else
-           if(!vol_fs_type.empty() && !vol_fs_uuid.empty())
-           {
-             DBG << "Specified device name " << dev_name
-                 << " is a volume (type " << vol_fs_type
-                 << ", uuid " << vol_fs_uuid << ")"
-                 << std::endl;
-             return true;
-           }
-         }
-       }
+       ERR << cmd.execError() << endl
+           << "Specified device name " << dev_name
+           << " is not a usable disk volume"
+           << std::endl;
+       return false;
       }
-
-      ERR << "Specified device name " << dev_name
-         << " is not a usable disk volume"
-          << std::endl;
-      return false;
+      return true;
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -240,10 +188,10 @@ namespace zypp {
       //   mount /dev/<partition> /tmp_mount
       //   mount /tmp_mount/<dir> <to> --bind -o ro
       // FIXME: try all filesystems
-    
+
       if(_device.empty())
        ZYPP_THROW(MediaBadUrlEmptyDestinationException(url()));
-    
+
       PathInfo dev_info(_device);
       if(!dev_info.isBlk())
         ZYPP_THROW(MediaBadUrlEmptyDestinationException(url()));
@@ -259,7 +207,7 @@ namespace zypp {
        ZYPP_THROW(MediaBadUrlEmptyFilesystemException(url()));
 
       MediaSourceRef media( new MediaSource(
-       "disk", _device, dev_info.major(), dev_info.minor()
+       "disk", _device, dev_info.devMajor(), dev_info.devMinor()
       ));
       AttachedMedia  ret( findAttachedMedia( media));
 
@@ -294,29 +242,34 @@ namespace zypp {
          is_device = true;
        }
 
-       if( is_device && media->maj_nr == dev_info.major() &&
-                        media->min_nr == dev_info.minor())
+       if( is_device && media->maj_nr == dev_info.devMajor() &&
+                        media->min_nr == dev_info.devMinor())
        {
-         /*
-         if( _filesystem != "auto" && _filesystem != e->type)
+         AttachPointRef ap( new AttachPoint(e->dir, false));
+         AttachedMedia  am( media, ap);
          {
-           ZYPP_THROW();
+           DBG << "Using a system mounted media "
+               << media->name
+               << " attached on "
+               << ap->path
+               << endl;
+
+           media->iown = false; // mark attachment as foreign
+
+           setMediaSource(media);
+           setAttachPoint(ap);
+           return;
          }
-         */
-         media->bdir = e->dir;
        }
       }
 
-      Mount mount;
-      std::string mountpoint = attachPoint().asString();
-      if( !isUseableAttachPoint(attachPoint()))
+      if( !isUseableAttachPoint( attachPoint() ) )
       {
-       mountpoint = createAttachPoint().asString();
-       if( mountpoint.empty())
-         ZYPP_THROW( MediaBadAttachPointException(url()));
-       setAttachPoint( mountpoint, true);
+       setAttachPoint( createAttachPoint(), true );
       }
+      std::string mountpoint( attachPoint().asString() );
 
+      Mount mount;
       string options = _url.getQueryParam("mountoptions");
       if(options.empty())
       {
@@ -383,13 +336,16 @@ namespace zypp {
     //
     // DESCRIPTION : Asserted that media is attached.
     //
-    void MediaDISK::releaseFrom( bool eject )
+    void MediaDISK::releaseFrom( const std::string & ejectDev )
     {
-      Mount mount;
-      mount.umount(attachPoint().asString());
+      AttachedMedia am( attachedMedia());
+      if(am.mediaSource && am.mediaSource->iown)
+      {
+        Mount mount;
+        mount.umount(attachPoint().asString());
+      }
     }
 
-
     ///////////////////////////////////////////////////////////////////
     //
     // METHOD NAME : MediaDISK::getFile
@@ -401,7 +357,7 @@ namespace zypp {
     {
       MediaHandler::getFile( filename );
     }
-    
+
     ///////////////////////////////////////////////////////////////////
     //
     // METHOD NAME : MediaDISK::getDir
@@ -413,7 +369,7 @@ namespace zypp {
     {
       MediaHandler::getDir( dirname, recurse_r );
     }
-    
+
     ///////////////////////////////////////////////////////////////////
     //
     //
@@ -427,7 +383,7 @@ namespace zypp {
     {
       MediaHandler::getDirInfo( retlist, dirname, dots );
     }
-    
+
     ///////////////////////////////////////////////////////////////////
     //
     //
@@ -445,8 +401,8 @@ namespace zypp {
     bool MediaDISK::getDoesFileExist( const Pathname & filename ) const
     {
       return MediaHandler::getDoesFileExist( filename );
-    }    
-    
+    }
+
   } // namespace media
 } // namespace zypp
 // vim: set ts=8 sts=2 sw=2 ai noet: