+send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t len)
+{
+ void *data;
+ guestfs_chunk chunk;
+ XDR xdr;
+
+ if (g->state != BUSY) {
+ error (g, "send_file_chunk: state %d != READY", g->state);
+ return -1;
+ }
+
+ /* Serialize the chunk. */
+ chunk.cancel = cancel;
+ chunk.data.data_len = len;
+ chunk.data.data_val = (char *) buf;
+
+ data = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 48);
+ xdrmem_create (&xdr, data, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
+ if (xdr_guestfs_chunk (&xdr, &chunk)) {
+ error (g, "xdr_guestfs_chunk failed");
+ free (data);
+ return -1;
+ }
+
+ chunkdatalen = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ data = safe_realloc (g, data, len);
+ g->msg_out = data;
+ g->msg_out_size = len;
+ g->msg_out_pos = 0;
+
+ if (guestfs__switch_to_sending (g) == -1)
+ goto cleanup1;
+
+ return 0;
+
+ cleanup1:
+ free (g->msg_out);
+ g->msg_out = NULL;
+ g->msg_out_size = 0;
+ g->state = READY;
+ return -1;
+}
+
+/* Send a chunk of file data. */
+static int
+send_file_data (guestfs_h *g, const char *buf, size_t len)
+{
+ return send_file_chunk (g, 0, buf, len);
+}
+
+/* Send a cancellation message. */
+static int
+send_file_cancellation (guestfs_h *g)
+{
+ char buf[1];
+ return send_file_chunk (g, 1, buf, 0);
+}
+
+/* Send a file complete chunk. */
+static int
+send_file_complete (guestfs_h *g)
+{
+ char buf[0];
+ return send_file_chunk (g, 0, buf, 0);
+}
+#endif
+
+/* This is the default main loop implementation, using select(2). */
+
+static int
+select_add_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd, int events,