static VALUE unixfdlist_initialize(int argc, VALUE *argv, VALUE self) { VALUE rbfds; gint i, n_fds; gint *fds; rb_scan_args(argc, argv, "0*", &rbfds); n_fds = RARRAY_LEN(rbfds); if (n_fds == 0) { G_INITIALIZE(self, g_unix_fd_list_new()); return Qnil; } fds = ALLOCA_N(gint, n_fds); for (i = 0; i < n_fds; i++) fds[0] = RVAL2FD(RARRAY_PTR(rbfds)[i]); G_INITIALIZE(self, g_unix_fd_list_new_from_array(fds, n_fds)); return Qnil; }
static VALUE unixfdlist_initialize(int argc, VALUE *argv, VALUE self) { VALUE rbfds; long n; gint *fds; GUnixFDList *list; rb_scan_args(argc, argv, "0*", &rbfds); fds = RVAL2FDS(rbfds, &n); if (n == 0) { G_INITIALIZE(self, g_unix_fd_list_new()); return Qnil; } list = g_unix_fd_list_new_from_array(fds, n); g_free(fds); G_INITIALIZE(self, list); return Qnil; }
int main (int argc, char *argv[]) { gint ret = 1; GError *error = NULL; gchar *s = NULL; GOptionContext *o = NULL; gint n; GSList *uris = NULL; GSList *l; bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); have_gtk = gtk_init_check (&argc, &argv); main_loop = g_main_loop_new (NULL, FALSE); udisks_client = udisks_client_new_sync (NULL, &error); if (udisks_client == NULL) { g_printerr (_("Error connecting to udisks daemon: %s (%s, %d)"), error->message, g_quark_to_string (error->domain), error->code); g_error_free (error); goto out; } o = g_option_context_new (NULL); g_option_context_set_help_enabled (o, FALSE); g_option_context_set_summary (o, _("Attach and mount one or more disk image files.")); g_option_context_add_main_entries (o, opt_entries, GETTEXT_PACKAGE); if (!g_option_context_parse (o, &argc, &argv, NULL)) { s = g_option_context_get_help (o, FALSE, NULL); g_printerr ("%s", s); g_free (s); goto out; } if (argc > 1) { for (n = 1; n < argc; n++) uris = g_slist_prepend (uris, g_strdup (argv[n])); uris = g_slist_reverse (uris); } else { if (!have_gtk) { show_error ("No files given and GTK+ not available"); goto out; } else { uris = do_filechooser (); } } /* Files to attach are positional arguments */ for (l = uris; l != NULL; l = l->next) { const gchar *uri; gchar *filename; GUnixFDList *fd_list = NULL; GVariantBuilder options_builder; gint fd; gchar *loop_object_path = NULL; GFile *file; uri = l->data; file = g_file_new_for_commandline_arg (uri); filename = g_file_get_path (file); g_object_unref (file); if (filename == NULL) { show_error (_("Cannot open “%s” — maybe the volume isn’t mounted?"), uri); goto done_with_image; } fd = open (filename, opt_writable ? O_RDWR : O_RDONLY); if (fd == -1) { show_error (_("Error opening “%s”: %m"), filename); goto done_with_image; } g_variant_builder_init (&options_builder, G_VARIANT_TYPE_VARDICT); if (!opt_writable) g_variant_builder_add (&options_builder, "{sv}", "read-only", g_variant_new_boolean (TRUE)); fd_list = g_unix_fd_list_new_from_array (&fd, 1); /* adopts the fd */ /* Set up the disk image... */ error = NULL; if (!udisks_manager_call_loop_setup_sync (udisks_client_get_manager (udisks_client), g_variant_new_handle (0), g_variant_builder_end (&options_builder), fd_list, &loop_object_path, NULL, /* out_fd_list */ NULL, /* GCancellable */ &error)) { show_error (_("Error attaching disk image: %s (%s, %d)"), error->message, g_quark_to_string (error->domain), error->code); g_clear_error (&error); goto done_with_image; } /* Note that the desktop automounter is responsible for mounting, * unlocking etc. partitions etc. inside the image... */ done_with_image: g_clear_object (&fd_list); g_free (filename); g_free (loop_object_path); } /* for each image */ ret = 0; out: if (main_loop != NULL) g_main_loop_unref (main_loop); g_slist_free_full (uris, g_free); g_clear_object (&udisks_client); return ret; }