g_file_find_enclosing_mount() under Windows



Hi all,

I am developing a program using GLib.

When a file is loaded into my program from a removable disc such as a
CD-ROM, USB stick or floppy etc. I need to get the volume label of this
disc from which the file has been loaded.

I am trying to do this using the g_file_find_enclosing_mount() function,
obtaining the volume of the mount and then using
g_volume_get_identifier() to get the label.

While this seems to work OK on Linux, g_file_find_enclosing_mount()
consistently fails under Windows with the error 'Containing mount does
not exist.'

Below is some very rough code to demonstrate what I am trying to do:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>

int main(int argc, char *argv[])
{
    GError *gerror = NULL;
    GMount *mount = NULL;
    GVolume *volume = NULL;
    gchar *drive_name = NULL;
    gchar filename[256];
    GFile *file;
    int i = 0;

    /* Get a file from the user. */
    printf("\nEnter a filename: ");
    fgets(filename, sizeof(filename), stdin) ;

    i = strlen(filename) - 1;

    if ((filename[i] == '\r') || (filename[i] == '\n')) filename[i] = '\0';

    /* Init the GType system. */
    g_type_init();

    /* Now try to get the enclosing mount.*/
    file = g_file_new_for_path(filename);
    mount = g_file_find_enclosing_mount(file, NULL, &gerror);
    if (mount == NULL)
    {
        printf("\nCould not get the enclosing mount of this file because
of the following error:\n\n%s.\n", gerror->message);
        return 1;
    }
    /* Try to get the volume and then the volume label from the mount. */
    volume = g_mount_get_volume(mount);
    drive_name = g_volume_get_identifier(volume,
G_VOLUME_IDENTIFIER_KIND_LABEL);
    printf("\nThe volume label is %s\n", drive_name);
    if (file != NULL) g_object_unref(file);
    if (mount != NULL) g_object_unref(mount);
    if (drive_name != NULL) g_free(drive_name);

    return 0;
}

Is this a bug, am I missing something here or is there another way to do
this?

Regards,

Chris




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