Re: unmount usb mounted device

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am Dienstag, 27. Juni 2006 20:45 schrieb Martin (KDE):
> Am Montag, 26. Juni 2006 08:06 schrieb Martin (KDE):

And again a reply to myself

> > Hallo,
> >
> > I have discovered a realy strange thing this weekend. If I plug
> > in a usb stick, a usb-stick symbol pops up at my desktop. With
> > the latest dbus/hal stuff there is no entry in the fstab (that is
> > ok afaik). I can mount this usb device with no problem.
> >
> > If I want to unmount this device I get no error message. There is
> > something going on (the cpu load is high for about three seconds)
> > but nothing happens. The usb devise keep being mounted. I have to
> > login as root to unmount the device.
> >
> > With the older hal versions there was a tool to update the fstab
> > (fstab-sync), but this tool is remove in current versions. Am I
> > missing something fundamental?
>
> another rply to myself:
>
> I did an update to the eject program and now the eject to my ipod
> is working again (the ipod icon on my desktop is vanishing) and the
> "do not disconnect" messate at my ipod is leaving. This works only
> as root.
>
> for users this does not work. With current hal there is no entry in
> the fstab, so normal users are not allowed to exit those
> partitions. Are there other solutions than changeing the kdeeject
> script (add sudo to the eject call).

So adding sudo to the kdeeject file was no satisfying solution, I took 
the source code and did some readings. Finally I found the media kio 
slave in kdebase. A little reading in hal and dbus and the 
halbackend.cpp I tried to code myself. today I got it working. May be 
this is of some interest to someone. I attached the patch to svn KDE 
3.5 branch (branches/KDE/3.5/kdebase/kioslave/media) from about 
beginnig of july.

>
> > KDE 3.5.3 (from cvs from last friday)
> > hal 0.5.7
> > dbus 0.62
> > udev 0.94
> > Kernel 2.6.16.20
> >
> > Martin
>
> Martin

Martin

Index: mounthelper/kio_media_mounthelper.cpp
===================================================================
--- mounthelper/kio_media_mounthelper.cpp	(Revision 557632)
+++ mounthelper/kio_media_mounthelper.cpp	(Arbeitskopie)
@@ -91,7 +91,16 @@
 	}
 	else if (args->isSet("s") || args->isSet("e"))
 	{
-		invokeEject(device, true);
+		//invokeEject(device, true);
+	  DCOPRef mediamanager("kded", "mediamanager");
+	  DCOPReply reply = mediamanager.call( "eject", medium.id());
+	  if (reply.isValid())
+	    reply.get(m_errorStr);
+	  kdDebug() << "medium unmount " << m_errorStr << endl;
+	  if (m_errorStr.isNull())
+	    ::exit(0);
+	  else
+	    error();
 	}
 	else
 	{
Index: mediamanager/mediamanager.cpp
===================================================================
--- mediamanager/mediamanager.cpp	(Revision 557632)
+++ mediamanager/mediamanager.cpp	(Arbeitskopie)
@@ -221,6 +221,17 @@
 #endif
 }
 
+QString MediaManager::eject(const QString &name)
+{
+#ifdef COMPILE_HALBACKEND
+    if (!m_halbackend)
+        return i18n("Feature only available with HAL");
+    return m_halbackend->eject(name);
+#else
+    return i18n("Feature only available with HAL");
+#endif
+}
+
 QString MediaManager::nameForLabel(const QString &label)
 {
     const QPtrList<Medium> media = m_mediaList.list();
Index: mediamanager/halbackend.cpp
===================================================================
--- mediamanager/halbackend.cpp	(Revision 557632)
+++ mediamanager/halbackend.cpp	(Arbeitskopie)
@@ -1204,4 +1204,82 @@
     return QString();
 }
 
+QString HALBackend::eject(const QString &_udi)
+{
+  QString umountResult = unmount(_udi);
+
+  const Medium* medium = m_mediaList.findById(_udi);
+  if (!medium)
+  {   // now we get fancy: if the udi is no volume, it _might_ be a device with only one
+      // volume on it (think CDs) - so we're so nice to the caller to unmount that volume
+      LibHalDrive*  halDrive  = libhal_drive_from_udi(m_halContext, _udi.latin1());
+      if (halDrive)
+      {
+	  int numVolumes;
+	  char** volumes = libhal_drive_find_all_volumes(m_halContext, halDrive, &numVolumes);
+	  if (numVolumes == 1)
+	      medium = m_mediaList.findById( volumes[0] );
+      }
+  }
+
+  if ( !medium )
+      return i18n("No such medium: %1").arg(_udi);
+
+  if (umountResult.isNull())
+  {
+    DBusMessage *dmesg, *reply;
+    DBusError error;
+    const char *options[2];
+
+    const char *udi = medium->id().latin1();
+    kdDebug() << "ejecting " << udi << "..." << endl;
+
+    dbus_error_init(&error);
+    DBusConnection *dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+    if (dbus_error_is_set(&error))
+    {
+        dbus_error_free(&error);
+        return false;
+    }
+
+    if (!(dmesg = dbus_message_new_method_call ("org.freedesktop.Hal", udi,
+						"org.freedesktop.Hal.Device.Volume",
+						"Eject")))
+    {
+	kdDebug() << "eject failed for " << udi << ": could not create dbus message\n";
+	return i18n("Internal Error");
+    } // end if
+
+    options[0] = "force";
+    options[1] = 0;
+
+    if (!dbus_message_append_args (dmesg, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &options, 0,
+				    DBUS_TYPE_INVALID))
+    {
+	kdDebug() << "eject failed for " << udi 
+		  << ": could not append args to dbus message\n";
+	dbus_message_unref (dmesg);
+	return i18n("Internal Error");
+    }
+
+    dbus_error_init (&error);
+    if (!(reply = dbus_connection_send_with_reply_and_block (dbus_connection, 
+							      dmesg, -1, &error)))
+    {
+	kdDebug() << "unmount failed for " << udi 
+		  << ": " << error.name << " " << error.message << endl;
+	dbus_message_unref (dmesg);
+	dbus_error_free (&error);
+	return i18n("Unable to eject device");
+    }
+
+    kdDebug() << "eject queued for " << udi << endl;
+
+    dbus_message_unref (dmesg);
+    dbus_message_unref (reply);
+  } // end if (umountResult.isNull())
+
+  return umountResult;
+}
+
 #include "halbackend.moc"
Index: mediamanager/mediamanager.h
===================================================================
--- mediamanager/mediamanager.h	(Revision 557632)
+++ mediamanager/mediamanager.h	(Arbeitskopie)
@@ -46,6 +46,7 @@
 
 	QString mount(const QString &uid);
 	QString unmount(const QString &uid);
+	QString eject(const QString &uid);
 
 	QString nameForLabel(const QString &label);
 	ASYNC setUserLabel(const QString &name, const QString &label);
Index: mediamanager/halbackend.h
===================================================================
--- mediamanager/halbackend.h	(Revision 557632)
+++ mediamanager/halbackend.h	(Arbeitskopie)
@@ -95,6 +95,7 @@
 	QString mount(const QString &id);
 	QString mount(const Medium *medium);
 	QString unmount(const QString &id);
+	QString eject(const QString &id);
 
 private:
 	/**
___________________________________________________
.
Account management:  https://mail.kde.org/mailman/listinfo/kde.
Archives: http://lists.kde.org/.
More info: http://www.kde.org/faq.html.

[Index of Archives]     [Trinity (TDE) Desktop Users]     [Fedora KDE]     [Fedora Desktop]     [Linux Kernel]     [Gimp]     [GIMP for Windows]     [Gnome]     [Yosemite Hiking]
  Powered by Linux