summaryrefslogtreecommitdiff
path: root/src/spice-file-transfer-task.c
blob: aa9d95bead99971f9ddad34e07cbff87ae587b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
   Copyright (C) 2016 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include "spice-file-transfer-task-priv.h"

/**
 * SECTION:file-transfer-task
 * @short_description: Monitoring file transfers
 * @title: File Transfer Task
 * @section_id:
 * @see_also: #SpiceMainChannel
 * @stability: Stable
 * @include: spice-client.h
 *
 * SpiceFileTransferTask is an object that represents a particular file
 * transfer between the client and the guest. The properties and signals of the
 * object can be used to monitor the status and result of the transfer. The
 * Main Channel's #SpiceMainChannel::new-file-transfer signal will be emitted
 * whenever a new file transfer task is initiated.
 *
 * Since: 0.31
 */

struct _SpiceFileTransferTask
{
    GObject parent;

    uint32_t                       id;
    gboolean                       completed;
    gboolean                       pending;
    GFile                          *file;
    SpiceMainChannel               *channel;
    GFileInputStream               *file_stream;
    GFileCopyFlags                 flags;
    GCancellable                   *cancellable;
    GAsyncReadyCallback            callback;
    gpointer                       user_data;
    char                           *buffer;
    uint64_t                       read_bytes;
    uint64_t                       file_size;
    gint64                         start_time;
    gint64                         last_update;
    GError                         *error;
};

struct _SpiceFileTransferTaskClass
{
    GObjectClass parent_class;
};

G_DEFINE_TYPE(SpiceFileTransferTask, spice_file_transfer_task, G_TYPE_OBJECT)

#define FILE_XFER_CHUNK_SIZE (VD_AGENT_MAX_DATA_SIZE * 32)

enum {
    PROP_TASK_ID = 1,
    PROP_TASK_CHANNEL,
    PROP_TASK_CANCELLABLE,
    PROP_TASK_FILE,
    PROP_TASK_TOTAL_BYTES,
    PROP_TASK_TRANSFERRED_BYTES,
    PROP_TASK_PROGRESS,
};

enum {
    SIGNAL_FINISHED,
    LAST_TASK_SIGNAL
};

static guint task_signals[LAST_TASK_SIGNAL];

/*******************************************************************************
 * Helpers
 ******************************************************************************/

static SpiceFileTransferTask *
spice_file_transfer_task_new(SpiceMainChannel *channel,
                             GFile *file,
                             GFileCopyFlags flags,
                             GCancellable *cancellable)
{
    static uint32_t xfer_id = 1;    /* Used to identify task id */
    GCancellable *task_cancellable = cancellable;
    SpiceFileTransferTask *self;

    /* if a cancellable object was not provided for the overall operation,
     * create a separate object for each file so that they can be cancelled
     * separately  */
    if (!task_cancellable)
        task_cancellable = g_cancellable_new();

    self = g_object_new(SPICE_TYPE_FILE_TRANSFER_TASK,
                        "id", xfer_id++,
                        "file", file,
                        "channel", channel,
                        "cancellable", task_cancellable,
                        NULL);
    self->flags = flags;

    /* if we created a GCancellable above, unref it */
    if (!cancellable)
        g_object_unref(task_cancellable);

    return self;
}

static void spice_file_transfer_task_query_info_cb(GObject *obj,
                                                   GAsyncResult *res,
                                                   gpointer user_data)
{
    SpiceFileTransferTask *self;
    GFileInfo *info;
    GTask *task;
    GError *error = NULL;

    task = G_TASK(user_data);
    self = g_task_get_source_object(task);

    g_return_if_fail(self->pending == TRUE);
    self->pending = FALSE;

    info = g_file_query_info_finish(G_FILE(obj), res, &error);
    if (self->error) {
        g_clear_object(&info);
        g_clear_error(&error);
        /* Return error previously reported */
        g_task_return_error(task, g_error_copy(self->error));
        g_object_unref(task);
        return;
    } else if (error) {
        g_task_return_error(task, error);
        g_object_unref(task);
        return;
    }

    self->file_size =
        g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);

    /* SpiceFileTransferTask's init is done, handshake for file-transfer will
     * start soon. First "progress" can be emitted ~ 0% */
    g_object_notify(G_OBJECT(self), "total-bytes");
    g_object_notify(G_OBJECT(self), "progress");

    g_task_return_pointer(task, info, g_object_unref);
    g_object_unref(task);
}

static void spice_file_transfer_task_read_file_cb(GObject *obj,
                                                  GAsyncResult *res,
                                                  gpointer user_data)
{
    SpiceFileTransferTask *self;
    GTask *task;
    GError *error = NULL;

    task = G_TASK(user_data);
    self = g_task_get_source_object(task);

    g_return_if_fail(self->pending == TRUE);

    self->file_stream = g_file_read_finish(G_FILE(obj), res, &error);
    if (self->error) {
        g_clear_error(&error);
        /* Return error previously reported */
        self->pending = FALSE;
        g_task_return_error(task, g_error_copy(self->error));
        g_object_unref(task);
        return;
    } else if (error) {
        self->pending = FALSE;
        g_task_return_error(task, error);
        g_object_unref(task);
        return;
    }

    g_file_query_info_async(self->file,
                            "standard::*",
                            G_FILE_QUERY_INFO_NONE,
                            G_PRIORITY_DEFAULT,
                            self->cancellable,
                            spice_file_transfer_task_query_info_cb,
                            task);
}

static void spice_file_transfer_task_read_stream_cb(GObject *source_object,
                                                    GAsyncResult *res,
                                                    gpointer userdata)
{
    SpiceFileTransferTask *self;
    GTask *task;
    gssize nbytes;
    GError *error = NULL;

    task = G_TASK(userdata);
    self = g_task_get_source_object(task);

    g_return_if_fail(self->pending == TRUE);
    self->pending = FALSE;

    nbytes = g_input_stream_read_finish(G_INPUT_STREAM(self->file_stream), res, &error);
    if (self->error) {
        g_clear_error(&error);
        /* On any pending error on SpiceFileTransferTask */
        g_task_return_error(task, g_error_copy(self->error));
        g_object_unref(task);
        return;
    } else if (error) {
        g_task_return_error(task, error);
        g_object_unref(task);
        return;
    }

    self->read_bytes += nbytes;

    if (spice_util_get_debug()) {
        const GTimeSpan interval = 20 * G_TIME_SPAN_SECOND;
        gint64 now = g_get_monotonic_time();

        if (interval < now - self->last_update) {
            gchar *basename = g_file_get_basename(self->file);
            self->last_update = now;
            SPICE_DEBUG("read %.2f%% of the file %s",
                        100.0 * self->read_bytes / self->file_size, basename);
            g_free(basename);
        }
    }

    g_task_return_int(task, nbytes);
    g_object_unref(task);
}

/* main context */
static void spice_file_transfer_task_close_stream_cb(GObject      *object,
                                                     GAsyncResult *close_res,
                                                     gpointer      user_data)
{
    SpiceFileTransferTask *self;
    GError *error = NULL;

    self = user_data;

    if (object) {
        GInputStream *stream = G_INPUT_STREAM(object);
        g_input_stream_close_finish(stream, close_res, &error);
        if (error) {
            /* This error dont need to report to user, just print a log */
            SPICE_DEBUG("close file error: %s", error->message);
            g_clear_error(&error);
        }
    }

    if (self->error == NULL && spice_util_get_debug()) {
        gint64 now = g_get_monotonic_time();
        gchar *basename = g_file_get_basename(self->file);
        double seconds = (double) (now - self->start_time) / G_TIME_SPAN_SECOND;
        gchar *file_size_str = g_format_size(self->file_size);
        gchar *transfer_speed_str = g_format_size(self->file_size / seconds);

        g_warn_if_fail(self->read_bytes == self->file_size);
        SPICE_DEBUG("transferred file %s of %s size in %.1f seconds (%s/s)",
                    basename, file_size_str, seconds, transfer_speed_str);

        g_free(basename);
        g_free(file_size_str);
        g_free(transfer_speed_str);
    }
    g_object_unref(self);
}


/*******************************************************************************
 * Internal API
 ******************************************************************************/

G_GNUC_INTERNAL
void spice_file_transfer_task_completed(SpiceFileTransferTask *self,
                                        GError *error)
{
    self->completed = TRUE;

    /* In case of multiple errors we only report the first error */
    if (self->error)
        g_clear_error(&error);
    if (error) {
        gchar *path = g_file_get_path(self->file);
        SPICE_DEBUG("File %s xfer failed: %s",
                    path, error->message);
        g_free(path);
        self->error = error;
    }

    if (self->pending) {
        /* Complete but pending is okay only if error is set */
        if (self->error == NULL) {
            self->error = g_error_new(SPICE_CLIENT_ERROR,
                                      SPICE_CLIENT_ERROR_FAILED,
                                      "Cannot complete task in pending state");
        }
        return;
    }

    if (!self->file_stream) {
        spice_file_transfer_task_close_stream_cb(NULL, NULL, self);
        goto signal;
    }

    g_input_stream_close_async(G_INPUT_STREAM(self->file_stream),
                               G_PRIORITY_DEFAULT,
                               self->cancellable,
                               spice_file_transfer_task_close_stream_cb,
                               self);
    self->pending = TRUE;
signal:
    g_signal_emit(self, task_signals[SIGNAL_FINISHED], 0, self->error);
    /* SpiceFileTransferTask unref is done after input stream is closed */
}

G_GNUC_INTERNAL
guint32 spice_file_transfer_task_get_id(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, 0);
    return self->id;
}

G_GNUC_INTERNAL
SpiceMainChannel *spice_file_transfer_task_get_channel(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, NULL);
    return self->channel;
}

G_GNUC_INTERNAL
GCancellable *spice_file_transfer_task_get_cancellable(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, NULL);
    return self->cancellable;
}

guint64 spice_file_transfer_task_get_total_bytes(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, 0);
    return self->file_size;
}

guint64 spice_file_transfer_task_get_transferred_bytes(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, 0);
    return self->read_bytes;
}

/* Helper function which only creates a SpiceFileTransferTask per GFile
 * in @files and returns a HashTable mapping task-id to the task itself
 * The SpiceFileTransferTask created here has two references, one should be
 * freed by spice_file_transfer_task_close_stream_cb() after
 * spice_file_transfer_task_completed() is called and the other reference
 * belongs to the caller and should be freed upon GHashTable destruction */
G_GNUC_INTERNAL
GHashTable *spice_file_transfer_task_create_tasks(GFile **files,
                                                  SpiceMainChannel *channel,
                                                  GFileCopyFlags flags,
                                                  GCancellable *cancellable)
{
    GHashTable *xfer_ht;
    gint i;

    g_return_val_if_fail(files != NULL && files[0] != NULL, NULL);

    xfer_ht = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_object_unref);
    for (i = 0; files[i] != NULL && !g_cancellable_is_cancelled(cancellable); i++) {
        SpiceFileTransferTask *xfer_task;
        guint32 task_id;

        xfer_task = spice_file_transfer_task_new(channel, files[i], flags, cancellable);
        task_id = spice_file_transfer_task_get_id(xfer_task);
        g_hash_table_insert(xfer_ht, GUINT_TO_POINTER(task_id), g_object_ref(xfer_task));
    }
    return xfer_ht;
}

G_GNUC_INTERNAL
void spice_file_transfer_task_init_task_async(SpiceFileTransferTask *self,
                                              GAsyncReadyCallback callback,
                                              gpointer userdata)
{
    GTask *task;

    g_return_if_fail(self != NULL);
    g_return_if_fail(self->pending == FALSE);

    task = g_task_new(self, self->cancellable, callback, userdata);

    self->pending = TRUE;
    g_file_read_async(self->file,
                      G_PRIORITY_DEFAULT,
                      self->cancellable,
                      spice_file_transfer_task_read_file_cb,
                      task);
}

G_GNUC_INTERNAL
GFileInfo *spice_file_transfer_task_init_task_finish(SpiceFileTransferTask *self,
                                                     GAsyncResult *result,
                                                     GError **error)
{
    GTask *task = G_TASK(result);

    g_return_val_if_fail(self != NULL, NULL);
    return g_task_propagate_pointer(task, error);
}

/* Any context */
G_GNUC_INTERNAL
void spice_file_transfer_task_read_async(SpiceFileTransferTask *self,
                                         GAsyncReadyCallback callback,
                                         gpointer userdata)
{
    GTask *task;

    g_return_if_fail(self != NULL);
    if (self->pending) {
        g_task_report_new_error(self, callback, userdata,
                                spice_file_transfer_task_read_async,
                                SPICE_CLIENT_ERROR,
                                SPICE_CLIENT_ERROR_FAILED,
                                "Cannot read data in pending state");
        return;
    }

    /* Notify the progress prior the read to make the info be related to the
     * data that was already sent. To notify the 100% (completed), channel-main
     * should call read-async when it expects EOF. */
    g_object_notify(G_OBJECT(self), "progress");
    g_object_notify(G_OBJECT(self), "transferred-bytes");

    task = g_task_new(self, self->cancellable, callback, userdata);

    if (self->read_bytes == self->file_size) {
        /* channel-main might request data after reading the whole file as it
         * expects EOF. Let's return immediately its request as we don't want to
         * reach a state where agent says file-transfer SUCCEED but we are in a
         * PENDING state in SpiceFileTransferTask due reading in idle */
        g_task_return_int(task, 0);
        g_object_unref(task);
        return;
    }

    self->pending = TRUE;
    g_input_stream_read_async(G_INPUT_STREAM(self->file_stream),
                              self->buffer,
                              FILE_XFER_CHUNK_SIZE,
                              G_PRIORITY_DEFAULT,
                              self->cancellable,
                              spice_file_transfer_task_read_stream_cb,
                              task);
}

G_GNUC_INTERNAL
gssize spice_file_transfer_task_read_finish(SpiceFileTransferTask *self,
                                            GAsyncResult *result,
                                            char **buffer,
                                            GError **error)
{
    gssize nbytes;
    GTask *task = G_TASK(result);

    g_return_val_if_fail(self != NULL, -1);

    nbytes = g_task_propagate_int(task, error);
    if (nbytes >= 0 && buffer != NULL)
        *buffer = self->buffer;

    return nbytes;
}

G_GNUC_INTERNAL
gboolean spice_file_transfer_task_is_completed(SpiceFileTransferTask *self)
{
    g_return_val_if_fail(self != NULL, FALSE);

    /* File transfer is considered over at the first time it is marked as
     * complete with spice_file_transfer_task_completed. */
    return self->completed;
}

/*******************************************************************************
 * External API
 ******************************************************************************/

/**
 * spice_file_transfer_task_get_progress:
 * @self: a file transfer task
 *
 * Convenience function for retrieving the current progress of this file
 * transfer task.
 *
 * Returns: A fractional value between 0 and 1.0
 *
 * Since: 0.31
 **/
double spice_file_transfer_task_get_progress(SpiceFileTransferTask *self)
{
    if (self->file_size == 0)
        return 0.0;

    return (double)self->read_bytes / self->file_size;
}

/**
 * spice_file_transfer_task_cancel:
 * @self: a file transfer task
 *
 * Cancels the file transfer task. Note that depending on how the file transfer
 * was initiated, multiple file transfer tasks may share a single
 * #SpiceFileTransferTask::cancellable object, so canceling one task may result
 * in the cancellation of other tasks.
 *
 * Since: 0.31
 **/
void spice_file_transfer_task_cancel(SpiceFileTransferTask *self)
{
    g_cancellable_cancel(self->cancellable);
}

/**
 * spice_file_transfer_task_get_filename:
 * @self: a file transfer task
 *
 * Gets the name of the file being transferred in this task
 *
 * Returns: (transfer full): The basename of the file
 *
 * Since: 0.31
 **/
char* spice_file_transfer_task_get_filename(SpiceFileTransferTask *self)
{
    return g_file_get_basename(self->file);
}

/*******************************************************************************
 * GObject
 ******************************************************************************/

static void
spice_file_transfer_task_get_property(GObject *object,
                                      guint property_id,
                                      GValue *value,
                                      GParamSpec *pspec)
{
    SpiceFileTransferTask *self = SPICE_FILE_TRANSFER_TASK(object);

    switch (property_id)
    {
        case PROP_TASK_ID:
            g_value_set_uint(value, self->id);
            break;
        case PROP_TASK_FILE:
            g_value_set_object(value, self->file);
            break;
        case PROP_TASK_TOTAL_BYTES:
            g_value_set_uint64(value, spice_file_transfer_task_get_total_bytes(self));
            break;
        case PROP_TASK_TRANSFERRED_BYTES:
            g_value_set_uint64(value, spice_file_transfer_task_get_transferred_bytes(self));
            break;
        case PROP_TASK_PROGRESS:
            g_value_set_double(value, spice_file_transfer_task_get_progress(self));
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
    }
}

static void
spice_file_transfer_task_set_property(GObject *object,
                                      guint property_id,
                                      const GValue *value,
                                      GParamSpec *pspec)
{
    SpiceFileTransferTask *self = SPICE_FILE_TRANSFER_TASK(object);

    switch (property_id)
    {
        case PROP_TASK_ID:
            self->id = g_value_get_uint(value);
            break;
        case PROP_TASK_FILE:
            self->file = g_value_dup_object(value);
            break;
        case PROP_TASK_CHANNEL:
            self->channel = g_value_dup_object(value);
            break;
        case PROP_TASK_CANCELLABLE:
            self->cancellable = g_value_dup_object(value);
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
    }
}

static void
spice_file_transfer_task_dispose(GObject *object)
{
    SpiceFileTransferTask *self = SPICE_FILE_TRANSFER_TASK(object);

    g_clear_object(&self->file);
    g_clear_object(&self->file_stream);
    g_clear_error(&self->error);

    G_OBJECT_CLASS(spice_file_transfer_task_parent_class)->dispose(object);
}

static void
spice_file_transfer_task_finalize(GObject *object)
{
    SpiceFileTransferTask *self = SPICE_FILE_TRANSFER_TASK(object);

    g_free(self->buffer);

    G_OBJECT_CLASS(spice_file_transfer_task_parent_class)->finalize(object);
}

static void
spice_file_transfer_task_constructed(GObject *object)
{
    SpiceFileTransferTask *self = SPICE_FILE_TRANSFER_TASK(object);

    if (spice_util_get_debug()) {
        gchar *basename = g_file_get_basename(self->file);
        self->start_time = g_get_monotonic_time();
        self->last_update = self->start_time;

        SPICE_DEBUG("transfer of file %s has started", basename);
        g_free(basename);
    }
}

static void
spice_file_transfer_task_class_init(SpiceFileTransferTaskClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->get_property = spice_file_transfer_task_get_property;
    object_class->set_property = spice_file_transfer_task_set_property;
    object_class->finalize = spice_file_transfer_task_finalize;
    object_class->dispose = spice_file_transfer_task_dispose;
    object_class->constructed = spice_file_transfer_task_constructed;

    /**
     * SpiceFileTransferTask:id:
     *
     * The ID of the file transfer task
     *
     * Since: 0.31
     **/
    g_object_class_install_property(object_class, PROP_TASK_ID,
                                    g_param_spec_uint("id",
                                                      "id",
                                                      "The id of the task",
                                                      0, G_MAXUINT, 0,
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                                                      G_PARAM_STATIC_STRINGS));

    /**
     * SpiceFileTransferTask:channel:
     *
     * The main channel that owns the file transfer task
     *
     * Since: 0.31
     **/
    g_object_class_install_property(object_class, PROP_TASK_CHANNEL,
                                    g_param_spec_object("channel",
                                                        "channel",
                                                        "The channel transferring the file",
                                                        SPICE_TYPE_MAIN_CHANNEL,
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));

    /**
     * SpiceFileTransferTask:cancellable:
     *
     * A cancellable object used to cancel the file transfer
     *
     * Since: 0.31
     **/
    g_object_class_install_property(object_class, PROP_TASK_CANCELLABLE,
                                    g_param_spec_object("cancellable",
                                                        "cancellable",
                                                        "The object used to cancel the task",
                                                        G_TYPE_CANCELLABLE,
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));

    /**
     * SpiceFileTransferTask:file:
     *
     * The file that is being transferred in this file transfer task
     *
     * Since: 0.31
     **/
    g_object_class_install_property(object_class, PROP_TASK_FILE,
                                    g_param_spec_object("file",
                                                        "File",
                                                        "The file being transferred",
                                                        G_TYPE_FILE,
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));

    /**
     * SpiceFileTransferTask:total-bytes:
     *
     * The total size in bytes of this file transfer.
     *
     * Since: 0.33
     **/
    g_object_class_install_property(object_class, PROP_TASK_TOTAL_BYTES,
                                    g_param_spec_uint64("total-bytes",
                                                        "Total bytes",
                                                        "The size in bytes of the file transferred",
                                                        0, G_MAXUINT64, 0,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));


    /**
     * SpiceFileTransferTask:transferred-bytes:
     *
     * The number of bytes that have been transferred so far.
     *
     * Since: 0.33
     **/
    g_object_class_install_property(object_class, PROP_TASK_TRANSFERRED_BYTES,
                                    g_param_spec_uint64("transferred-bytes",
                                                        "Transferred bytes",
                                                        "The number of bytes transferred",
                                                        0, G_MAXUINT64, 0,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));


    /**
     * SpiceFileTransferTask:progress:
     *
     * The current state of the file transfer. This value indicates a
     * fraction, and ranges from 0 to 1.0. Listen for change notifications on
     * this property to be updated whenever the file transfer progress changes.
     *
     * Since: 0.31
     **/
    g_object_class_install_property(object_class, PROP_TASK_PROGRESS,
                                    g_param_spec_double("progress",
                                                        "Progress",
                                                        "The percentage of the file transferred",
                                                        0.0, 1.0, 0.0,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));

    /**
     * SpiceFileTransferTask::finished:
     * @task: the file transfer task that emitted the signal
     * @error: (transfer none): the error state of the transfer. Will be %NULL
     * if the file transfer was successful.
     *
     * The #SpiceFileTransferTask::finished signal is emitted when the file
     * transfer has completed transferring to the guest.
     *
     * Since: 0.31
     **/
    task_signals[SIGNAL_FINISHED] = g_signal_new("finished", SPICE_TYPE_FILE_TRANSFER_TASK,
                                            G_SIGNAL_RUN_FIRST,
                                            0, NULL, NULL,
                                            g_cclosure_marshal_VOID__BOXED,
                                            G_TYPE_NONE, 1,
                                            G_TYPE_ERROR);
}

static void
spice_file_transfer_task_init(SpiceFileTransferTask *self)
{
    self->buffer = g_malloc0(FILE_XFER_CHUNK_SIZE);
}