#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <glib.h>
#include <gio/gio.h>

static GFile *file;

static void umount_cb(GObject *source, GAsyncResult *res, void *udata)
{
    GMount *f = G_MOUNT(source);
    GError *error = NULL;

    printf("unmount finishing\n");
    g_mount_unmount_finish(f, res, &error);
    if (error) {
        printf("unmount error: %s\n", error->message);
        g_error_free(error);
        error = NULL;
    }
    printf("unmount finished\n");

    g_file_query_info(file, "*", 0, NULL, &error);
    if (error) {
        printf("query_info error: %s\n", error->message);
        g_error_free(error);
        error = NULL;
    }
    exit(0);
}

static void mount_cb(GObject *source, GAsyncResult *res, void *udata)
{
    GFile *f = G_FILE(source);
    GError *error = NULL;
    GMount *mount;
    printf("mount finishing\n");
    g_file_mount_enclosing_volume_finish(f, res, &error);
    if (error) {
        printf("mount error: %s\n", error->message);
        exit(0);
    }
    printf("mount finished\n");

    printf("finding enclosing mount\n");
    mount = g_file_find_enclosing_mount(f, NULL, &error);
    if (error) {
        printf("find error: %s\n", error->message);
        exit(0);
    }
    printf("finding enclosing mount finished\n");

    printf("unmount starting\n");
    g_mount_unmount(mount, G_MOUNT_UNMOUNT_NONE,
                    NULL, umount_cb, NULL);
}

int main(int argc, char **argv)
{
    GMainLoop *loop;

    loop = g_main_loop_new(NULL, FALSE);
    file = g_file_new_for_uri("localtest:///");
    printf("mount starting\n");
    g_file_mount_enclosing_volume(file, G_MOUNT_MOUNT_NONE, NULL, NULL,
                                  mount_cb, NULL);
    g_main_loop_run(loop);

    return 0;
}
