改的检测current mounted的removable device的代码

本文介绍了一个C语言程序,该程序通过HAL (Hardware Abstraction Layer) 获取系统中已挂载的可移动媒体设备及其挂载点。程序首先初始化HAL上下文并连接到系统总线,然后获取所有设备列表,并从中筛选出可移动且已挂载的媒体设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*ev-window.c*/
#include "ev-check-mount.c"

static void
ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
{
    GtkWidget *chooser;

    chooser = gtk_file_chooser_dialog_new (_("Open Document"),
                           GTK_WINDOW (window),
                           GTK_FILE_CHOOSER_ACTION_OPEN,
                           GTK_STOCK_CANCEL,
                           GTK_RESPONSE_CANCEL,
                           GTK_STOCK_OPEN, GTK_RESPONSE_OK,
                           NULL);

    ev_document_factory_add_filters (chooser, NULL);
    gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
    gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);

 if (ev_application_get_chooser_uri (EV_APP) != NULL)
        gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (chooser),
                      ev_application_get_chooser_uri (EV_APP));

    g_signal_connect (chooser, "response",
              G_CALLBACK (file_open_dialog_response_cb),
              window);
        gtk_widget_show (chooser);
}

 struct Mounted *mounted_devices, *mountTmp;
    mounted_devices = get_mounted_removable_media();
    while(mounted_devices){
    gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(chooser), mounted_devices->mount_point, NULL);
    mountTmp = mounted_devices;
    mounted_devices = mounted_devices->next;
    free(mountTmp->name);
    free(mountTmp->mount_point);
    free(mountTmp);
    }   


/*ev-check-mount.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>
#include <libhal.h>

/**
 * @defgroup HalLsHal  List HAL devices
 * @ingroup HalMisc
 *
 * @brief A commandline tool, lshal, for displaying and, optionally,
 *        monitor the devices managed by the HAL daemon. Uses libhal.
 *
 * @{
 */

/** Macro for terminating the program on an unrecoverable error */
#define DIE(expr) do {printf("*** [DIE] %s:%s():%d : ", __FILE__, __FUNCTION__, __LINE__); printf expr; printf("/n"); exit(1); } while(0)


static LibHalContext *hal_ctx;

struct Device {
    char *name;
    char *parent;
    dbus_bool_t removable;
    dbus_bool_t is_mounted;
    char *mount_point;
    int parent_id;
};

struct Mounted {
    char *name;
    char *mount_point;
    struct Mounted *next;
};

static void
retrieve_useful_props (const char *udi, struct Device *device)
{
    DBusError error;
    LibHalPropertySet *props;
    LibHalPropertySetIterator it;
    int type;

    dbus_error_init (&error);

    device->mount_point = NULL;
    device->is_mounted  = FALSE;
   device->removable = FALSE;

    props = libhal_device_get_all_properties (hal_ctx, udi, &error);

    /* NOTE : This may be NULL if the device was removed
     *        in the daemon; this is because
     *        hal_device_get_all_properties() is a in
     *        essence an IPC call and other stuff may
     *        be happening..
     */
    if (props == NULL)
       return;

    for (libhal_psi_init (&it, props); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
        type = libhal_psi_get_type (&it);
        switch (type) {
            case LIBHAL_PROPERTY_TYPE_STRING:
                if(! strcmp(libhal_psi_get_key (&it), "volume.mount_point")){
                    printf ("  %s = '%s'  (string)/n",
                            libhal_psi_get_key (&it),
                            libhal_psi_get_string (&it));
                    device->mount_point = strdup(libhal_psi_get_string (&it));
                    printf("mount point =%s, p=%p/n", device->mount_point, device);
                }

                break;
            case LIBHAL_PROPERTY_TYPE_BOOLEAN:
                if(! strcmp(libhal_psi_get_key (&it), "volume.is_mounted")){
                    printf ("  %s = %s  (bool)/n",
                            libhal_psi_get_key (&it),
                            libhal_psi_get_bool (&it) ? "true" :
                            "false");
                    device->is_mounted = libhal_psi_get_bool (&it);
                }
                if(! strcmp(libhal_psi_get_key (&it), "storage.removable")){
                    printf ("  %s = '%s'  (bool)/n",
                            libhal_psi_get_key (&it),
                            libhal_psi_get_bool (&it)? "true":
                            "false");
                    device->removable = libhal_psi_get_bool (&it);
                }
                break;

            default:
                break;
        }
    }

    libhal_free_property_set (props);
}



/** Dump all children of device
 *
 *  @param  udi                 Universal Device Id of parent
 *  @param  num_devices         Total number of devices in device list
 *  @param  devices             List of devices
 *  @param  depth               Current recursion depth
 */

static void
dump_children (char *udi, int parent_id,  int num_devices, struct Device *devices, int depth)
{
    int i;

    for (i = 0; i < num_devices; i++) {
        if (!udi) {
            if (devices[i].parent)
                continue;
        }
        else {
            if (!devices[i].parent)
                continue;
            if (strcmp (devices[i].parent, udi))
                continue;
        }

   
        devices[i].parent_id = parent_id;
         //printf("parent=%s/nname=%s/n, id=%d,parent_id=%d/n", devices[i].parent, devices[i].name,i, parent_id);
        retrieve_useful_props (devices[i].name, &devices[i]);
         
        dump_children(devices[i].name, i, num_devices, devices, depth + 1);
    }
}

/** Dump all devices to stdout
 *
 */
static struct Mounted *
dump_devices (void)
{
    int i;
    int num_devices;
    char **device_names;
    struct Device *devices;
    DBusError error;

    dbus_error_init (&error);

    device_names = libhal_get_all_devices (hal_ctx, &num_devices, &error);
    if (device_names == NULL)
        DIE (("Couldn't obtain list of devices/n"));

    devices = malloc (sizeof(struct Device) * num_devices);
    if (!devices) {
        libhal_free_string_array (device_names);
        return NULL;
    }

    for (i = 0;i < num_devices;i++) {
        devices[i].name = device_names[i];
        devices[i].parent = libhal_device_get_property_string (hal_ctx,
                device_names[i], "info.parent", &error);

        if (dbus_error_is_set (&error)) {
            /* Free the error (which include a dbus_error_init())
               This should prevent errors if a call above fails */
            dbus_error_free (&error);
        }
    }

        printf ("/n"
            "Dumping %d device(s) from the Global Device List:/n"
            "-------------------------------------------------/n",
            num_devices);

    dump_children(NULL, -1, num_devices, devices, 0);

        struct Mounted *head= NULL, *tail = head, *mounted;
#if 1
        printf("------------------------------------------------------------------------/n");
for(i = 0; i< num_devices; i++){
   printf("n=%d, name=%s/n", i, devices[i].name);
   printf("/tis_removable=%s/n", devices[i].removable?"true":"false");
   printf("/tis_mounted=%s/n", devices[i].is_mounted?"true":"false");
   printf("/tmount point=%s/n", devices[i].mount_point);
   printf("/tparent=%d/n", devices[i].parent_id);

}
        printf("------------------------------------------------------------------------/n");
#endif
    for (i = 0;i < num_devices;i++) {
        if ((devices[i].removable || devices[devices[i].parent_id].removable)&&(devices[i].is_mounted)){
            mounted= (struct Mounted *)malloc(sizeof(struct Mounted));
            if(devices[i].name){
                mounted->name = strdup(devices[i].name);
            }
            if(devices[i].mount_point){
                mounted->mount_point = strdup(devices[i].mount_point);
            }
            mounted->next = NULL;
            if(tail){
                tail->next = mounted;
                tail = mounted;
                printf("1. h=%p, m=%p, t=%p/n",head, mounted, tail);
            }else{
                head = tail = mounted;
                printf("2. h=%p, m=%p, t=%p/n",head, mounted, tail);
            }
            free(devices[i].mount_point);
        }
    }
                printf("h=%p, t=%p/n", head, tail);

for (i = 0;i < num_devices;i++) {
        if (devices[i].parent)
            libhal_free_string (devices[i].parent);
    }


    free (devices);
    libhal_free_string_array (device_names);

    return head;
}

/** Entry point
 *
 *  @param  argc                Number of arguments given to program
 *  @param  argv                Arguments given to program
 *  @return                     Return code
 */
struct Mounted *
get_mounted_removable_media()

{
    DBusError error;
    GMainLoop *loop;
    DBusConnection *conn;

   

    dbus_error_init (&error);
    conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
    if (conn == NULL) {
        fprintf (stderr, "error: dbus_bus_get: %s: %s/n",
             error.name, error.message);
        return NULL;
    }


    if ((hal_ctx = libhal_ctx_new ()) == NULL) {
        fprintf (stderr, "error: libhal_ctx_new/n");
        return NULL;
    }
    if (!libhal_ctx_set_dbus_connection (hal_ctx, conn)) {
        fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s/n",
             error.name, error.message);
        return NULL;
    }
    if (!libhal_ctx_init (hal_ctx, &error)) {
        fprintf (stderr, "error: libhal_ctx_init: %s: %s/n",
             error.name, error.message);
        fprintf (stderr, "Could not initialise connection to hald. /n "
                 "Normally this mean the HAL daemon (hald) is not running or not ready./n");
        return NULL;
    }

//    libhal_ctx_set_device_added (hal_ctx, device_added);
//    libhal_ctx_set_device_removed (hal_ctx, device_removed);
//    libhal_ctx_set_device_new_capability (hal_ctx, device_new_capability);
//    libhal_ctx_set_device_lost_capability (hal_ctx, device_lost_capability);
//    libhal_ctx_set_device_property_modified (hal_ctx, property_modified);
//    libhal_ctx_set_device_condition (hal_ctx, device_condition);

   struct Mounted *mounted_list;
    mounted_list = dump_devices ();
    libhal_ctx_shutdown (hal_ctx, &error);
    libhal_ctx_free (hal_ctx);

    dbus_connection_disconnect (conn);
    dbus_connection_unref (conn);


    return mounted_list;
}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值