Storing backup in USB from gtk application

Hi dear community,
I am using GTK 3+ for my embedded device . I want to store some files from my internal memory to the USB device. The USB must be select-able from the UI. I have came across gtk file chooser widget for folder selection. But this opens as a popup which enables the user t explore device folders. I dont want the user to see the folders. Is there an special widgets that shows only the list of USB connected or can this be done in any other way?

I tried to hard code pendrive as /media/sda1 but when I remove and insert pendrive again, it gets mounted as /media/sdb1 and next time as /media/sdc1 and so. So hard coding pendrive location is not at all possible.

Help or suggestions will be appreciated.

Thank you very much for your time

You can use GVolumeMonitor to get a list of volumes on the system. Then use g_drive_is_removable(g_volume_get_drive(volume)) to know if it’s removable media.

To display the volumes you can use anything from a GtkTextView, a GtkComboBox to a GtkListBox.

HI @lb90,
Thanks for such a fast response. How to get the pendrive’s mount point like “/media/sda1” with G_volume monitor so that I can simply use this as directory to copy my folder? Examples will be very useful as I am new to this.

I’m not sure, but looks like you can use g_mount_get_root(g_volume_get_mount(volume)) for that. This gives you back a GFile.

Then to get the path as a string call g_file_get_path or g_file_get_uri. If you use the former, be sure to check for NULL return values!

To copy files there’s g_file_copy.

Hi @lb90, I tried this but unfortunately I am not able to get the work done. Can you please share any link of example so that I can get to know some basics about this as I am new to this one.
Thanks for your time.

Hi! Here’s an example code:

/* compile with: gcc list_removable_volumes.c `pkg-config --cflags --libs gio-2.0` */

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

struct MediaInfo
  char *volume_name;
  char *drive_name;
  char *mount_path;

static void
media_info_destroy (gpointer data)
  struct MediaInfo *media_info = (struct MediaInfo*) data;

  g_free (media_info->volume_name);
  g_free (media_info->drive_name);
  g_free (media_info->mount_path);

static GList*
enumerate_removable_media ()
  GVolumeMonitor *monitor = g_volume_monitor_get ();
  GList *volumes = g_volume_monitor_get_volumes (monitor);
  GList *media_infos = NULL;

  for (GList *l = volumes; l; l = l->next)
      GVolume *volume = G_VOLUME (l->data);
      GDrive *drive = g_volume_get_drive (volume);
      GMount *mount = g_volume_get_mount (volume);

      if (mount && drive && g_drive_is_removable (drive))
		  GFile *mount_root = g_mount_get_root (mount);
          gchar *mount_path = g_file_get_path (mount_root);

		  if (mount_path)
              struct MediaInfo *media_info = g_new0 (struct MediaInfo, 1);

              media_info->volume_name = g_volume_get_name (volume);
              media_info->drive_name = g_drive_get_name (drive);
              media_info->mount_path = mount_path;

              media_infos = g_list_append(media_infos, media_info);

          g_object_unref (mount_root);

      g_clear_object (&drive);
      g_clear_object (&mount);

  return media_infos;

int main(int argc, char **argv)
  GList *media_infos = enumerate_removable_media ();

  g_print ("\nFound %u removable volumes.\n\n", g_list_length (media_infos));

  for (GList *l = media_infos; l; l = l->next)
      struct MediaInfo *media_info = (struct MediaInfo*) l->data;

      g_print ("====================\n");
      g_print ("Volume name: %s\n", media_info->volume_name);
      g_print (" Drive name: %s\n", media_info->drive_name);
      g_print (" Mount path: %s\n", media_info->mount_path);

  g_list_free_full (media_infos, media_info_destroy);

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.