hal: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Tue Feb 20 00:12:40 PST 2007


 libhal-storage/libhal-storage.c |   47 ++++++++++++++++++++++++++++++++++++++++
 libhal-storage/libhal-storage.h |    2 +
 tools/umount-hal.c              |   31 +++++++++++++++++---------
 3 files changed, 70 insertions(+), 10 deletions(-)

New commits:
diff-tree e30c3d6de8da910f2547c5c12b1cbb3dfe99261d (from fd62ece9c0bce992d3b7ef09ab5ed74105ee25b7)
Author: David Zeuthen <davidz at redhat.com>
Date:   Tue Feb 20 03:12:28 2007 -0500

    make /sbin/umount.hal handle mounts points as well
    
    Add some new API to libhal-storage to make this easy to do.

diff --git a/libhal-storage/libhal-storage.c b/libhal-storage/libhal-storage.c
index caae4b2..5c62803 100644
--- a/libhal-storage/libhal-storage.c
+++ b/libhal-storage/libhal-storage.c
@@ -1356,6 +1356,53 @@ out:
 }
 
 
+/** Get the volume object for a given mount point
+ *
+ *  @param  hal_ctx             libhal context to use
+ *  @param  device_file         Name of mount point without terminting slash, e.g. '/media/disk'
+ *  @return                     LibHalVolume object or NULL if it doesn't exist
+ */
+LibHalVolume *
+libhal_volume_from_mount_point              (LibHalContext *hal_ctx, 
+					     const char *mount_point)
+{
+	int i;
+	char **hal_udis;
+	int num_hal_udis;
+	LibHalVolume *result;
+	char *found_udi;
+	DBusError error;
+
+	LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
+
+	result = NULL;
+	found_udi = NULL;
+
+	dbus_error_init (&error);
+	if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "volume.mount_point", 
+								 mount_point, &num_hal_udis, &error)) == NULL)
+		goto out;
+
+	for (i = 0; i < num_hal_udis; i++) {
+		char *udi;
+		udi = hal_udis[i];
+		if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
+			found_udi = strdup (udi);
+			break;
+		}
+	}
+
+	libhal_free_string_array (hal_udis);
+
+	if (found_udi != NULL)
+		result = libhal_volume_from_udi (hal_ctx, found_udi);
+
+	free (found_udi);
+out:
+	LIBHAL_FREE_DBUS_ERROR(&error);
+	return result;
+}
+
 /** Get the volume object for a given device file.
  *
  *  @param  hal_ctx             libhal context to use
diff --git a/libhal-storage/libhal-storage.h b/libhal-storage/libhal-storage.h
index c88a389..39fec6d 100644
--- a/libhal-storage/libhal-storage.h
+++ b/libhal-storage/libhal-storage.h
@@ -279,6 +279,8 @@ LibHalVolume     *libhal_volume_from_udi
 							       const char *udi);
 LibHalVolume     *libhal_volume_from_device_file              (LibHalContext *hal_ctx, 
 							       const char *device_file);
+LibHalVolume     *libhal_volume_from_mount_point              (LibHalContext *hal_ctx, 
+							       const char *mount_point);
 void              libhal_volume_free                          (LibHalVolume     *volume);
 dbus_uint64_t     libhal_volume_get_size                      (LibHalVolume     *volume);
 dbus_uint64_t     libhal_volume_get_disc_capacity             (LibHalVolume     *volume);
diff --git a/tools/umount-hal.c b/tools/umount-hal.c
index e16b96d..719943c 100644
--- a/tools/umount-hal.c
+++ b/tools/umount-hal.c
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <libhal.h>
 #include <libhal-storage.h>
 
@@ -35,7 +36,7 @@ int
 main (int argc, char *argv[])
 {
 	int ret;
-	char *device_file;
+	char *device_file_or_mount_point;
 	DBusError error;
 	DBusConnection *con;
 	LibHalContext *hal_ctx;
@@ -48,16 +49,16 @@ main (int argc, char *argv[])
 
 	ret = 1;
 
-	if (argc < 2) {
+	if (argc < 2 || strlen (argv[1]) == 0) {
 		fprintf (stderr, "%s: this program is only supposed to be invoked by umount(8).\n", argv[0]);
 		goto out;
 	}
 
-	/* it appears the device file is always the first argument.
-	 * TODO XXX FIXME: we ought to honor umount(8) options like
-	 * -v for verbose.
+	/* it appears the device file / mount point is always the
+	 * first argument.  TODO XXX FIXME: we ought to honor
+	 * umount(8) options like -v for verbose.
 	 */
-	device_file = argv[1];
+	device_file_or_mount_point = argv[1];
 
 	dbus_error_init (&error);
 	con = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
@@ -72,14 +73,24 @@ main (int argc, char *argv[])
 		if (dbus_error_is_set(&error)) {
 			fprintf (stderr, "%s: libhal_ctx_init: %s: %s\n", argv[0], error.name, error.message);
 			dbus_error_free (&error);
+		} else {
+			fprintf (stderr, "%s: libhal_ctx_init failed. Is hald running?\n", argv[0]);
 		}
 		goto out;
 	}
 
-	vol = libhal_volume_from_device_file (hal_ctx, device_file);
+	vol = libhal_volume_from_device_file (hal_ctx, device_file_or_mount_point);
 	if (vol == NULL) {
-		fprintf (stderr, "%s: %s is not recognized by hal\n", argv[0], device_file);
-		goto out;
+
+		/* it might be a mount point! */
+		if (device_file_or_mount_point[strlen (device_file_or_mount_point) - 1] == '/') {
+			device_file_or_mount_point[strlen (device_file_or_mount_point) - 1] = '\0';
+		}
+		vol = libhal_volume_from_mount_point (hal_ctx, device_file_or_mount_point);
+		if (vol == NULL) {
+			fprintf (stderr, "%s: %s is not recognized by hal\n", argv[0], device_file_or_mount_point);
+			goto out;
+		}
 	}
 
 	message = dbus_message_new_method_call ("org.freedesktop.Hal", 
@@ -102,7 +113,7 @@ main (int argc, char *argv[])
 								 -1,
 								 &error)) || dbus_error_is_set (&error)) {
 		fprintf (stderr, "%s: Unmounting %s failed: %s: %s\n", 
-			 argv[0], device_file, error.name, error.message);
+			 argv[0], device_file_or_mount_point, error.name, error.message);
 		goto out;
 	}
 


More information about the hal-commit mailing list