Issue
I am using libhal to detect device events. I am able to detect a device added or removed but I can not detect device's mount point. The function libhal_volume_get_mount_point(volume)
does not work.
I have a callback function to detect device add:
static void handle_device_added(LibHalContext *ctx, const char *udi) {
dbus_bool_t is_storage;
dbus_bool_t is_volume;
is_storage = libhal_device_query_capability(ctx, udi, "storage", NULL);
is_volume = libhal_device_query_capability(ctx, udi, "volume", NULL);
if (is_storage) {
drive = libhal_drive_from_udi(ctx, udi);
if (libhal_drive_is_hotpluggable(drive) || libhal_drive_uses_removable_media(drive)) {
printf("Storage device added %s model %s\n",
libhal_drive_get_device_file(drive),
libhal_drive_get_model(drive));
}
libhal_drive_free(drive);
}
if(is_volume) {
volume = libhal_volume_from_udi(ctx, udi);
printf("Mount point = %s\n", libhal_volume_get_mount_point(volume));
libhal_volume_free(volume);
}
}
libhal_volume_from_udi, returns NULL.
Do you know any suitable way to detect the mount point of a storage device in C?
UPDATE
I have managed to find mount point of the device by searching /etc/mtab
but there is still one little problem. I am assuming the device has one partition only.
How can I get the list of the partition on a storage device? So I can found mount points of each.
Solution
First, if you provide more details on what fails when you try to use the libhal function, you may get answers that help fix that issue. But to answer your question directly, a C program can determine what is mounted where by reading /etc/mtab. The format is fairly self-explanatory, each line typically lists the name of the storage device, the directory where it is mounted, the filesystem type, and mount options.
To map a specific directory in the filesystem to the mount point it belongs to it can be a little tricky -- remember if you're looping through /etc/mtab that mount points can have other mount points beneath them.
You may find looking at the source for programs that do this correctly, like "df" will help you get it right, or if your program doesn't need to look up this information often, you might decide to simply call popen(3) on a df command to do it for you. For example, the command:df /media/my-volume/some/path
does a nice job displaying both the name of the storage device and the mount point where that device is mounted.
Answered By - andy