+ if (g->state != BUSY) {
+ error (g, _("guestfs___send: state %d != BUSY"), g->state);
+ return -1;
+ }
+
+ /* We have to allocate this message buffer on the heap because
+ * it is quite large (although will be mostly unused). We
+ * can't allocate it on the stack because in some environments
+ * we have quite limited stack space available, notably when
+ * running in the JVM.
+ */
+ msg_out = safe_malloc (g, GUESTFS_MESSAGE_MAX + 4);
+ xdrmem_create (&xdr, msg_out + 4, GUESTFS_MESSAGE_MAX, XDR_ENCODE);
+
+ /* Serialize the header. */
+ hdr.prog = GUESTFS_PROGRAM;
+ hdr.vers = GUESTFS_PROTOCOL_VERSION;
+ hdr.proc = proc_nr;
+ hdr.direction = GUESTFS_DIRECTION_CALL;
+ hdr.serial = serial;
+ hdr.status = GUESTFS_STATUS_OK;
+
+ if (!xdr_guestfs_message_header (&xdr, &hdr)) {
+ error (g, _("xdr_guestfs_message_header failed"));
+ goto cleanup1;
+ }
+
+ /* Serialize the args. If any, because some message types
+ * have no parameters.
+ */
+ if (xdrp) {
+ if (!(*xdrp) (&xdr, args)) {
+ error (g, _("dispatch failed to marshal args"));
+ goto cleanup1;
+ }
+ }
+
+ /* Get the actual length of the message, resize the buffer to match
+ * the actual length, and write the length word at the beginning.
+ */
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ msg_out = safe_realloc (g, msg_out, len + 4);
+ msg_out_size = len + 4;
+
+ xdrmem_create (&xdr, msg_out, 4, XDR_ENCODE);
+ xdr_uint32_t (&xdr, &len);
+
+ again:
+ r = send_to_daemon (g, msg_out, msg_out_size);
+ if (r == -2) /* Ignore stray daemon cancellations. */
+ goto again;
+ if (r == -1)
+ goto cleanup1;
+ free (msg_out);
+
+ return serial;
+
+ cleanup1:
+ free (msg_out);
+ return -1;
+}
+
+static int cancel = 0; /* XXX Implement file cancellation. */
+static int send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t len);
+static int send_file_data (guestfs_h *g, const char *buf, size_t len);
+static int send_file_cancellation (guestfs_h *g);
+static int send_file_complete (guestfs_h *g);
+
+/* Send a file.
+ * Returns:
+ * 0 OK
+ * -1 error
+ * -2 daemon cancelled (we must read the error message)
+ */
+int
+guestfs___send_file (guestfs_h *g, const char *filename)
+{
+ char buf[GUESTFS_MAX_CHUNK_SIZE];
+ int fd, r, err;
+
+ fd = open (filename, O_RDONLY);
+ if (fd == -1) {
+ perrorf (g, "open: %s", filename);
+ send_file_cancellation (g);
+ /* Daemon sees cancellation and won't reply, so caller can
+ * just return here.
+ */
+ return -1;
+ }
+
+ /* Send file in chunked encoding. */
+ while (!cancel) {
+ r = read (fd, buf, sizeof buf);
+ if (r == -1 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ if (r <= 0) break;
+ err = send_file_data (g, buf, r);
+ if (err < 0) {
+ if (err == -2) /* daemon sent cancellation */
+ send_file_cancellation (g);
+ return err;
+ }
+ }
+
+ if (cancel) { /* cancel from either end */
+ send_file_cancellation (g);
+ return -1;
+ }
+
+ if (r == -1) {
+ perrorf (g, "read: %s", filename);
+ send_file_cancellation (g);
+ return -1;
+ }
+
+ /* End of file, but before we send that, we need to close
+ * the file and check for errors.
+ */
+ if (close (fd) == -1) {
+ perrorf (g, "close: %s", filename);
+ send_file_cancellation (g);
+ return -1;
+ }
+
+ return send_file_complete (g);
+}
+
+/* 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)
+{
+ return send_file_chunk (g, 1, NULL, 0);
+}
+
+/* Send a file complete chunk. */
+static int
+send_file_complete (guestfs_h *g)
+{
+ char buf[1];
+ return send_file_chunk (g, 0, buf, 0);
+}
+
+static int
+send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t buflen)
+{
+ u_int32_t len;
+ int r;
+ guestfs_chunk chunk;
+ XDR xdr;
+ char *msg_out;
+ size_t msg_out_size;
+
+ if (g->state != BUSY) {
+ error (g, _("send_file_chunk: state %d != READY"), g->state);
+ return -1;
+ }
+
+ /* Allocate the chunk buffer. Don't use the stack to avoid
+ * excessive stack usage and unnecessary copies.
+ */
+ msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
+ xdrmem_create (&xdr, msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
+
+ /* Serialize the chunk. */
+ chunk.cancel = cancel;
+ chunk.data.data_len = buflen;
+ chunk.data.data_val = (char *) buf;
+
+ if (!xdr_guestfs_chunk (&xdr, &chunk)) {
+ error (g, _("xdr_guestfs_chunk failed (buf = %p, buflen = %zu)"),
+ buf, buflen);
+ xdr_destroy (&xdr);
+ goto cleanup1;