
/* Simple test program for checking that a GVolumeMonitor retries an unmount
 * operation after emitting the ::mount-pre-unmount operation.
 *
 * Copyright (C) David Zeuthen <davidz@redhat.com>
 *
 * Licensed under the same terms as GVfs (LGPLv2+).
 *
 * Compile with:
 *
 *  gcc -o test-preunmount test-preunmount.c `pkg-config --cflags --libs gio-2.0
 *
 * Run with:
 *
 *  ./test-preunmount /path/to/file/on/a/mount
 *
 * and then attempt to unmount the mount from Nautilus or gvfs-mount(1).
 *
 */

#include <gio/gio.h>
#include <stdlib.h>

static GMount *mount_for_file;

static void
on_volume_monitor_mount_pre_unmount (GVolumeMonitor *volume_monitor,
                                     GMount         *mount,
                                     gpointer        user_data)
{
  if (mount == mount_for_file)
    {
      g_print ("got pre-unmount signal - sleeping two seconds before exiting\n");
      sleep (2);
      exit (0);
    }
}

int
main (int argc, char *argv[])
{
  GVolumeMonitor *monitor;
  GMainLoop *loop;
  GFile *file;
  GMount *mount;
  GFileInputStream *input_stream;
  GError *error;

  error = NULL;

  if (argc != 2)
    {
      g_printerr ("usage: %s <file-on-a-mount>\n", argv[0]);
      return 1;
    }

  g_type_init ();

  monitor = g_volume_monitor_get ();

  file = g_file_new_for_commandline_arg (argv[1]);
  mount_for_file = g_file_find_enclosing_mount (file, NULL, &error);
  if (mount_for_file == NULL)
    {
      g_printerr ("no mount for '%s': %s\n", argv[1], error->message);
      return 1;
    }

  input_stream = g_file_read (file, NULL, &error);
  if (input_stream == NULL)
    {
      g_printerr ("g_file_read() failed for '%s': %s\n", argv[1], error->message);
      return 1;
    }

  g_signal_connect (monitor,
                    "mount-pre-unmount",
                    G_CALLBACK (on_volume_monitor_mount_pre_unmount),
                    NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);
}
