1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
4 * @author: Xavier Claessens <xclaesse@gmail.com>
5 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
27 #include <sys/socket.h>
30 #include <glib/gi18n.h>
33 #include <gio/gunixinputstream.h>
34 #include <gio/gunixoutputstream.h>
36 #include <libtelepathy/tp-conn.h>
37 #include <libtelepathy/tp-helpers.h>
38 #include <libtelepathy/tp-props-iface.h>
40 #include <telepathy-glib/proxy-subclass.h>
42 #include "empathy-tp-file.h"
43 #include "empathy-contact-factory.h"
44 #include "empathy-marshal.h"
45 #include "empathy-time.h"
46 #include "empathy-utils.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_FT
49 #include "empathy-debug.h"
52 * SECTION:empathy-tp-file
53 * @short_description: File channel
54 * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
55 * @include: libempthy/empathy-tp-file.h
57 * The #EmpathyTpFile object represents a Telepathy file channel.
61 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
63 * Value used for the "size" or "estimated-size" properties when the size of
64 * the transferred file is unknown.
67 static void empathy_tp_file_class_init (EmpathyTpFileClass *klass);
68 static void empathy_tp_file_init (EmpathyTpFile *tp_file);
69 static void tp_file_finalize (GObject *object);
70 static GObject *tp_file_constructor (GType type, guint n_props,
71 GObjectConstructParam *props);
72 static void tp_file_get_property (GObject *object, guint param_id,
73 GValue *value, GParamSpec *pspec);
74 static void tp_file_set_property (GObject *object, guint param_id,
75 const GValue *value, GParamSpec *pspec);
76 static void tp_file_destroy_cb (TpChannel *file_chan, EmpathyTpFile *tp_file);
77 static void tp_file_closed_cb (TpChannel *file_chan, EmpathyTpFile *tp_file,
78 GObject *weak_object);
79 static void tp_file_state_changed_cb (DBusGProxy *file_iface, guint state,
80 guint reason, EmpathyTpFile *tp_file);
81 static void tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
82 guint64 count, EmpathyTpFile *tp_file, GObject *weak_object);
83 static void copy_stream (GInputStream *in, GOutputStream *out,
84 GCancellable *cancellable);
86 /* EmpathyTpFile object */
88 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
89 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv))
91 typedef struct _EmpathyTpFilePriv EmpathyTpFilePriv;
93 struct _EmpathyTpFilePriv {
94 EmpathyContactFactory *factory;
100 EmpathyTpFile *cached_empathy_file;
101 EmpathyContact *contact;
102 GInputStream *in_stream;
103 GOutputStream *out_stream;
106 EmpFileTransferState state;
107 EmpFileTransferStateChangeReason state_change_reason;
109 guint64 transferred_bytes;
111 gchar *unix_socket_path;
115 GCancellable *cancellable;
127 PROP_TRANSFERRED_BYTES,
132 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
135 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
139 object_class->finalize = tp_file_finalize;
140 object_class->constructor = tp_file_constructor;
141 object_class->get_property = tp_file_get_property;
142 object_class->set_property = tp_file_set_property;
144 /* Construct-only properties */
145 g_object_class_install_property (object_class,
147 g_param_spec_object ("account",
149 "The account associated with the channel",
152 G_PARAM_CONSTRUCT_ONLY));
154 g_object_class_install_property (object_class,
156 g_param_spec_object ("channel",
158 "The file transfer channel",
161 G_PARAM_CONSTRUCT_ONLY));
163 g_object_class_install_property (object_class,
165 g_param_spec_uint ("state",
166 "state of the transfer",
167 "The file transfer state",
174 g_object_class_install_property (object_class,
176 g_param_spec_boolean ("incoming",
178 "Whether the transfer is incoming",
183 g_object_class_install_property (object_class,
185 g_param_spec_string ("filename",
186 "name of the transfer",
187 "The file transfer filename",
191 g_object_class_install_property (object_class,
193 g_param_spec_uint64 ("size",
195 "The file transfer size",
201 g_object_class_install_property (object_class,
203 g_param_spec_string ("content-type",
204 "file transfer content-type",
205 "The file transfer content-type",
209 g_object_class_install_property (object_class,
211 g_param_spec_string ("content-md5",
212 "file transfer md5sum",
213 "The md5 sum of the transfer's contents",
217 g_object_class_install_property (object_class,
218 PROP_TRANSFERRED_BYTES,
219 g_param_spec_uint64 ("transferred-bytes",
221 "The number of bytes transferred",
227 g_object_class_install_property (object_class,
229 g_param_spec_object ("in-stream",
230 "transfer input stream",
231 "The input stream for file transfer",
235 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
239 empathy_tp_file_init (EmpathyTpFile *tp_file)
244 tp_file_finalize (GObject *object)
246 EmpathyTpFilePriv *priv;
247 EmpathyTpFile *tp_file;
249 tp_file = EMPATHY_TP_FILE (object);
250 priv = GET_PRIV (tp_file);
254 DEBUG ("Closing channel..");
255 g_signal_handlers_disconnect_by_func (priv->channel,
256 tp_file_destroy_cb, object);
257 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
258 if (G_IS_OBJECT (priv->channel))
259 g_object_unref (priv->channel);
264 g_object_unref (priv->factory);
268 g_object_unref (priv->account);
272 g_object_unref (priv->mc);
276 g_free (priv->filename);
277 g_free (priv->unix_socket_path);
278 g_free (priv->description);
279 g_free (priv->content_md5);
280 g_free (priv->content_type);
283 g_object_unref (priv->in_stream);
285 if (priv->out_stream)
286 g_object_unref (priv->out_stream);
289 g_object_unref (priv->contact);
291 if (priv->cancellable)
292 g_object_unref (priv->cancellable);
294 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
298 tp_file_constructor (GType type,
300 GObjectConstructParam *props)
303 EmpathyTpFilePriv *priv;
304 GError *error = NULL;
305 GHashTable *properties;
308 tp_file = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
311 priv = GET_PRIV (tp_file);
313 priv->factory = empathy_contact_factory_new ();
314 priv->mc = empathy_mission_control_new ();
316 tp_cli_channel_connect_to_closed (priv->channel,
317 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
321 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
322 TP_PROXY (priv->channel),
323 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
324 tp_file_state_changed_cb,
328 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
329 TP_PROXY (priv->channel),
330 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
331 tp_file_transferred_bytes_changed_cb,
336 handle = tp_channel_get_handle (priv->channel, NULL);
337 priv->contact = empathy_contact_factory_get_from_handle (priv->factory,
341 if (!tp_cli_dbus_properties_run_get_all (priv->channel,
342 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, &error, NULL))
344 DEBUG ("Failed to get properties: %s",
345 error ? error->message : "No error given");
346 g_clear_error (&error);
350 priv->size = g_value_get_uint64 (g_hash_table_lookup (properties, "Size"));
352 priv->state = g_value_get_uint (g_hash_table_lookup (properties, "State"));
354 /* Invalid reason, so empathy_file_get_state_change_reason() can give
355 * a warning if called for a not closed file transfer. */
356 priv->state_change_reason = -1;
358 priv->transferred_bytes = g_value_get_uint64 (g_hash_table_lookup (
359 properties, "TransferredBytes"));
361 priv->filename = g_value_dup_string (g_hash_table_lookup (properties,
364 priv->content_md5 = g_value_dup_string (g_hash_table_lookup (properties,
367 priv->description = g_value_dup_string (g_hash_table_lookup (properties,
370 priv->unix_socket_path = g_value_dup_string (g_hash_table_lookup (properties,
373 priv->direction = g_value_get_uint (g_hash_table_lookup (properties,
376 g_hash_table_destroy (properties);
382 tp_file_get_property (GObject *object,
387 EmpathyTpFilePriv *priv;
388 EmpathyTpFile *tp_file;
390 priv = GET_PRIV (object);
391 tp_file = EMPATHY_TP_FILE (object);
396 g_value_set_object (value, priv->account);
399 g_value_set_object (value, priv->channel);
402 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
408 tp_file_channel_set_dbus_property (gpointer proxy,
409 const gchar *property,
412 DEBUG ("Setting %s property", property);
413 tp_cli_dbus_properties_run_set (TP_PROXY (proxy),
415 EMP_IFACE_CHANNEL_TYPE_FILE,
424 tp_file_set_property (GObject *object,
429 EmpathyTpFilePriv *priv;
431 priv = GET_PRIV (object);
436 priv->account = g_object_ref (g_value_get_object (value));
439 priv->channel = g_object_ref (g_value_get_object (value));
442 priv->state = g_value_get_uint (value);
445 priv->incoming = g_value_get_boolean (value);
448 g_free (priv->filename);
449 priv->filename = g_value_dup_string (value);
450 tp_file_channel_set_dbus_property (priv->channel, "Filename", value);
453 priv->size = g_value_get_uint64 (value);
454 tp_file_channel_set_dbus_property (priv->channel, "Size", value);
456 case PROP_CONTENT_TYPE:
457 tp_file_channel_set_dbus_property (priv->channel, "ContentType", value);
458 g_free (priv->content_type);
459 priv->content_type = g_value_dup_string (value);
461 case PROP_CONTENT_MD5:
462 tp_file_channel_set_dbus_property (priv->channel, "ContentMD5", value);
463 g_free (priv->content_md5);
464 priv->content_md5 = g_value_dup_string (value);
468 g_object_unref (priv->in_stream);
469 priv->in_stream = g_object_ref (g_value_get_object (value));
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
478 * empathy_tp_file_new:
479 * @account: the #McAccount for the channel
480 * @channel: a Telepathy channel
482 * Creates a new #EmpathyTpFile wrapping @channel.
484 * Returns: a new #EmpathyTpFile
487 empathy_tp_file_new (McAccount *account,
490 return g_object_new (EMPATHY_TYPE_TP_FILE,
497 * empathy_tp_file_get_id:
498 * @tp_file: an #EmpathyTpFile
500 * Returns the ID of @tp_file.
505 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
507 EmpathyTpFilePriv *priv;
509 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
511 priv = GET_PRIV (tp_file);
517 * empathy_tp_file_get_channel
518 * @tp_file: an #EmpathyTpFile
520 * Returns the Telepathy file transfer channel
522 * Returns: the #TpChannel
525 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
527 EmpathyTpFilePriv *priv;
529 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
531 priv = GET_PRIV (tp_file);
533 return priv->channel;
537 tp_file_destroy_cb (TpChannel *file_channel,
538 EmpathyTpFile *tp_file)
540 EmpathyTpFilePriv *priv;
542 priv = GET_PRIV (tp_file);
544 DEBUG ("Channel Closed or CM crashed");
546 g_object_unref (priv->channel);
547 priv->channel = NULL;
551 tp_file_closed_cb (TpChannel *file_channel,
552 EmpathyTpFile *tp_file,
553 GObject *weak_object)
555 EmpathyTpFilePriv *priv;
557 priv = GET_PRIV (tp_file);
559 /* The channel is closed, do just like if the proxy was destroyed */
560 g_signal_handlers_disconnect_by_func (priv->channel,
563 tp_file_destroy_cb (file_channel, tp_file);
571 g_get_current_time (&tv);
572 return ((gint64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
576 _get_local_socket (EmpathyTpFile *tp_file)
580 struct sockaddr_un addr;
581 EmpathyTpFilePriv *priv;
584 priv = GET_PRIV (tp_file);
586 /* TODO: This could probably be a little nicer. */
587 tp_cli_dbus_properties_run_get (priv->channel,
589 EMP_IFACE_CHANNEL_TYPE_FILE,
595 if (priv->unix_socket_path)
596 g_free (priv->unix_socket_path);
598 priv->unix_socket_path = g_value_dup_string (socket_path);
599 g_value_unset (socket_path);
601 if (G_STR_EMPTY (priv->unix_socket_path))
604 fd = socket (PF_UNIX, SOCK_STREAM, 0);
608 memset (&addr, 0, sizeof (addr));
609 addr.sun_family = AF_UNIX;
610 path_len = strlen (priv->unix_socket_path);
611 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
613 if (connect (fd, (struct sockaddr*) &addr,
624 * empathy_tp_file_accept:
625 * @tp_file: an #EmpathyTpFile
627 * Accepts a file transfer that's in the "local pending" state (i.e.
628 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
631 empathy_tp_file_accept (EmpathyTpFile *tp_file)
633 EmpathyTpFilePriv *priv;
635 GValue nothing = { 0 };
636 GError *error = NULL;
638 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
640 priv = GET_PRIV (tp_file);
642 g_return_if_fail (priv->out_stream != NULL);
644 DEBUG ("Accepting file: filename=%s", priv->filename);
646 g_value_init (¬hing, G_TYPE_STRING);
647 g_value_set_string (¬hing, "");
649 if (!emp_cli_channel_type_file_run_accept_file (TP_PROXY (priv->channel),
650 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
651 ¬hing, &address, &error, NULL))
653 DEBUG ("Accept error: %s",
654 error ? error->message : "No message given");
655 g_clear_error (&error);
658 if (priv->unix_socket_path)
659 g_free (priv->unix_socket_path);
661 priv->unix_socket_path = g_value_dup_string (address);
662 g_value_unset (address);
664 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
668 receive_tp_file (EmpathyTpFile *tp_file)
670 EmpathyTpFilePriv *priv;
671 GInputStream *socket_stream;
674 priv = GET_PRIV (tp_file);
676 socket_fd = _get_local_socket (tp_file);
681 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
683 priv->cancellable = g_cancellable_new ();
685 copy_stream (socket_stream, priv->out_stream, priv->cancellable);
687 g_object_unref (socket_stream);
692 send_tp_file (EmpathyTpFile *tp_file)
695 GOutputStream *socket_stream;
696 EmpathyTpFilePriv *priv;
698 priv = GET_PRIV (tp_file);
700 DEBUG ("Sending file content: filename=%s",
703 g_return_if_fail (priv->in_stream);
705 socket_fd = _get_local_socket (tp_file);
708 DEBUG ("failed to get local socket fd");
711 DEBUG ("got local socket fd");
712 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
714 priv->cancellable = g_cancellable_new ();
716 copy_stream (priv->in_stream, socket_stream, priv->cancellable);
718 g_object_unref (socket_stream);
722 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
723 EmpFileTransferState state,
724 EmpFileTransferStateChangeReason reason,
725 EmpathyTpFile *tp_file)
727 EmpathyTpFilePriv *priv;
729 priv = GET_PRIV (tp_file);
731 DEBUG ("File transfer state changed: filename=%s, "
732 "old state=%u, state=%u, reason=%u",
733 priv->filename, priv->state, state, reason);
735 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
736 priv->start_time = get_time_msec ();
738 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
739 state, priv->incoming ? "yes" : "no",
740 priv->in_stream ? "present" : "not present",
741 priv->out_stream ? "present" : "not present");
743 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !priv->incoming &&
745 send_tp_file (tp_file);
746 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && priv->incoming &&
748 receive_tp_file (tp_file);
751 priv->state_change_reason = reason;
753 g_object_notify (G_OBJECT (tp_file), "state");
757 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
759 EmpathyTpFile *tp_file,
760 GObject *weak_object)
762 EmpathyTpFilePriv *priv;
764 priv = GET_PRIV (tp_file);
766 if (priv->transferred_bytes == count)
769 priv->transferred_bytes = count;
771 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
775 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
777 EmpathyTpFilePriv *priv;
779 priv = GET_PRIV (tp_file);
781 return priv->contact;
785 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
787 EmpathyTpFilePriv *priv;
789 priv = GET_PRIV (tp_file);
791 return priv->in_stream;
795 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
797 EmpathyTpFilePriv *priv;
799 priv = GET_PRIV (tp_file);
801 return priv->out_stream;
805 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
807 EmpathyTpFilePriv *priv;
809 priv = GET_PRIV (tp_file);
811 return priv->filename;
815 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
817 EmpathyTpFilePriv *priv;
819 priv = GET_PRIV (tp_file);
821 return priv->incoming;
825 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
827 EmpathyTpFilePriv *priv;
829 priv = GET_PRIV (tp_file);
834 EmpFileTransferStateChangeReason
835 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
837 EmpathyTpFilePriv *priv;
839 priv = GET_PRIV (tp_file);
841 g_return_val_if_fail (priv->state_change_reason >= 0,
842 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
844 return priv->state_change_reason;
848 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
850 EmpathyTpFilePriv *priv;
852 priv = GET_PRIV (tp_file);
858 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
860 EmpathyTpFilePriv *priv;
862 priv = GET_PRIV (tp_file);
864 return priv->transferred_bytes;
868 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
870 EmpathyTpFilePriv *priv;
871 gint64 curr_time, elapsed_time;
872 gdouble time_per_byte;
873 gdouble remaining_time;
875 priv = GET_PRIV (tp_file);
877 if (priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
880 if (priv->transferred_bytes == priv->size)
883 curr_time = get_time_msec ();
884 elapsed_time = curr_time - priv->start_time;
885 time_per_byte = (gdouble) elapsed_time / (gdouble) priv->transferred_bytes;
886 remaining_time = (time_per_byte * (priv->size - priv->transferred_bytes)) / 1000;
888 return (gint) (remaining_time + 0.5);
892 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
894 EmpathyTpFilePriv *priv;
896 priv = GET_PRIV (tp_file);
898 tp_cli_channel_run_close (priv->channel, -1, NULL, NULL);
900 g_cancellable_cancel (priv->cancellable);
904 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
905 GInputStream *in_stream)
907 EmpathyTpFilePriv *priv;
909 priv = GET_PRIV (tp_file);
911 if (priv->in_stream == in_stream)
915 g_warning ("Setting an input stream for incoming file "
916 "transfers is useless");
919 g_object_unref (priv->in_stream);
922 g_object_ref (in_stream);
924 priv->in_stream = in_stream;
926 g_object_notify (G_OBJECT (tp_file), "in-stream");
930 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
931 GOutputStream *out_stream)
933 EmpathyTpFilePriv *priv;
935 priv = GET_PRIV (tp_file);
937 if (priv->out_stream == out_stream)
941 g_warning ("Setting an output stream for outgoing file "
942 "transfers is useless");
944 if (priv->out_stream)
945 g_object_unref (priv->out_stream);
948 g_object_ref (out_stream);
950 priv->out_stream = out_stream;
954 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
955 const gchar *filename)
957 EmpathyTpFilePriv *priv;
959 priv = GET_PRIV (tp_file);
961 g_return_if_fail (filename != NULL);
963 if (priv->filename && strcmp (filename, priv->filename) == 0)
966 g_free (priv->filename);
967 priv->filename = g_strdup (filename);
969 g_object_notify (G_OBJECT (tp_file), "filename");
972 /* Functions to copy the content of a GInputStream to a GOutputStream */
975 #define BUFFER_SIZE 4096
980 GCancellable *cancellable;
981 char *buff[N_BUFFERS]; /* the temporary buffers */
982 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
983 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
984 gint curr_read; /* index of the buffer used for reading */
985 gint curr_write; /* index of the buffer used for writing */
986 gboolean is_reading; /* we are reading */
987 gboolean is_writing; /* we are writing */
988 guint n_closed; /* number of streams that have been closed */
991 static void schedule_next (CopyData *copy);
994 free_copy_data_if_closed (CopyData *copy)
998 /* Free the data only if both the input and output streams have
1001 if (copy->n_closed < 2)
1004 if (copy->in != NULL)
1005 g_object_unref (copy->in);
1007 if (copy->out != NULL)
1008 g_object_unref (copy->out);
1010 for (i = 0; i < N_BUFFERS; i++)
1011 g_free (copy->buff[i]);
1013 g_object_unref (copy->cancellable);
1018 io_error (CopyData *copy,
1021 g_cancellable_cancel (copy->cancellable);
1024 g_warning ("I/O error");
1025 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1026 ; /* Ignore cancellations */
1028 g_warning ("I/O error: %d: %s\n", error->code, error->message);
1030 if (copy->in != NULL)
1031 g_input_stream_close (copy->in, NULL, NULL);
1033 if (copy->out != NULL)
1034 g_output_stream_close (copy->out, NULL, NULL);
1036 free_copy_data_if_closed (copy);
1040 close_done (GObject *source_object,
1044 CopyData *copy = user_data;
1046 g_object_unref (source_object);
1047 free_copy_data_if_closed (copy);
1051 write_done_cb (GObject *source_object,
1055 CopyData *copy = user_data;
1057 GError *error = NULL;
1059 count_write = g_output_stream_write_finish (copy->out, res, &error);
1061 if (count_write <= 0)
1063 io_error (copy, error);
1064 g_error_free (error);
1068 copy->is_full[copy->curr_write] = FALSE;
1069 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
1070 copy->is_writing = FALSE;
1072 schedule_next (copy);
1076 read_done_cb (GObject *source_object,
1080 CopyData *copy = user_data;
1082 GError *error = NULL;
1084 count_read = g_input_stream_read_finish (copy->in, res, &error);
1086 if (count_read == 0)
1088 g_input_stream_close_async (copy->in, 0, copy->cancellable,
1092 else if (count_read < 0)
1094 io_error (copy, error);
1095 g_error_free (error);
1099 copy->count[copy->curr_read] = count_read;
1100 copy->is_full[copy->curr_read] = TRUE;
1101 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
1102 copy->is_reading = FALSE;
1104 schedule_next (copy);
1108 schedule_next (CopyData *copy)
1110 if (copy->in != NULL &&
1111 !copy->is_reading &&
1112 !copy->is_full[copy->curr_read])
1114 /* We are not reading and the current buffer is empty, so
1115 * start an async read. */
1116 copy->is_reading = TRUE;
1117 g_input_stream_read_async (copy->in,
1118 copy->buff[copy->curr_read],
1119 BUFFER_SIZE, 0, copy->cancellable,
1120 read_done_cb, copy);
1123 if (!copy->is_writing &&
1124 copy->is_full[copy->curr_write])
1126 if (copy->count[copy->curr_write] == 0)
1128 /* The last read on the buffer read 0 bytes, this
1129 * means that we got an EOF, so we can close
1130 * the output channel. */
1131 g_output_stream_close_async (copy->out, 0,
1138 /* We are not writing and the current buffer contains
1139 * data, so start an async write. */
1140 copy->is_writing = TRUE;
1141 g_output_stream_write_async (copy->out,
1142 copy->buff[copy->curr_write],
1143 copy->count[copy->curr_write],
1144 0, copy->cancellable,
1145 write_done_cb, copy);
1151 copy_stream (GInputStream *in,
1153 GCancellable *cancellable)
1158 g_return_if_fail (in != NULL);
1159 g_return_if_fail (out != NULL);
1161 copy = g_new0 (CopyData, 1);
1162 copy->in = g_object_ref (in);
1163 copy->out = g_object_ref (out);
1165 if (cancellable != NULL)
1166 copy->cancellable = g_object_ref (cancellable);
1168 copy->cancellable = g_cancellable_new ();
1170 for (i = 0; i < N_BUFFERS; i++)
1171 copy->buff[i] = g_malloc (BUFFER_SIZE);
1173 schedule_next (copy);