+ reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
+ xdr_destroy (&xdr);
+ return;
+ }
+ }
+
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
+ xdr_u_int (&xdr, &len);
+ xdr_destroy (&xdr);
+
+ if (xwrite (sock, lenbuf, 4) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+ if (xwrite (sock, buf, len) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+}
+
+/* Receive file chunks, repeatedly calling 'cb'. */
+int
+receive_file (receive_cb cb, void *opaque)
+{
+ guestfs_chunk chunk;
+ char lenbuf[4];
+ char *buf;
+ XDR xdr;
+ int r;
+ uint32_t len;
+
+ for (;;) {
+ if (verbose)
+ fprintf (stderr, "receive_file: reading length word\n");
+
+ /* Read the length word. */
+ if (xread (sock, lenbuf, 4) == -1)
+ exit (EXIT_FAILURE);
+
+ xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
+ xdr_u_int (&xdr, &len);
+ xdr_destroy (&xdr);
+
+ if (len == GUESTFS_CANCEL_FLAG)
+ continue; /* Just ignore it. */
+
+ if (len > GUESTFS_MESSAGE_MAX) {
+ fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
+ len);
+ exit (EXIT_FAILURE);
+ }
+
+ buf = malloc (len);
+ if (!buf) {
+ perror ("malloc");
+ return -1;
+ }
+
+ if (xread (sock, buf, len) == -1)
+ exit (EXIT_FAILURE);
+
+ xdrmem_create (&xdr, buf, len, XDR_DECODE);
+ memset (&chunk, 0, sizeof chunk);
+ if (!xdr_guestfs_chunk (&xdr, &chunk)) {
+ xdr_destroy (&xdr);
+ free (buf);
+ return -1;
+ }
+ xdr_destroy (&xdr);
+ free (buf);
+
+ if (verbose)
+ fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
+ chunk.cancel, chunk.data.data_len, chunk.data.data_val);
+
+ if (chunk.cancel) {
+ if (verbose)
+ fprintf (stderr, "receive_file: received cancellation from library\n");
+ xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
+ return -2;
+ }
+ if (chunk.data.data_len == 0) {
+ if (verbose)
+ fprintf (stderr, "receive_file: end of file, leaving function\n");
+ xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
+ return 0; /* end of file */
+ }
+
+ if (cb)
+ r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
+ else
+ r = 0;
+
+ xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
+ if (r == -1) { /* write error */
+ if (verbose)
+ fprintf (stderr, "receive_file: write error\n");
+ return -1;