-static int
-check_for_daemon_cancellation (guestfs_h *g)
-{
- fd_set rset;
- struct timeval tv;
- int r;
- char buf[4];
- uint32_t flag;
- XDR xdr;
-
- FD_ZERO (&rset);
- FD_SET (g->sock, &rset);
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- r = select (g->sock+1, &rset, NULL, NULL, &tv);
- if (r == -1) {
- perrorf (g, "select");
- return 0;
- }
- if (r == 0)
- return 0;
-
- /* Read the message from the daemon. */
- r = xread (g->sock, buf, sizeof buf);
- if (r == -1) {
- perrorf (g, "read");
- return 0;
- }
-
- xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
- xdr_uint32_t (&xdr, &flag);
- xdr_destroy (&xdr);
-
- if (flag != GUESTFS_CANCEL_FLAG) {
- error (g, "check_for_daemon_cancellation: read 0x%x from daemon, expected 0x%x\n",
- flag, GUESTFS_CANCEL_FLAG);
- return 0;
- }
-
- return 1;
-}
-
-/* Synchronously receive a file. */
-
-static int receive_file_data_sync (guestfs_h *g, void **buf);
-
-int
-guestfs__receive_file_sync (guestfs_h *g, const char *filename)
-{
- void *buf;
- int fd, r;
-
- fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
- if (fd == -1) {
- perrorf (g, "open: %s", filename);
- goto cancel;
- }
-
- /* Receive the file in chunked encoding. */
- while ((r = receive_file_data_sync (g, &buf)) > 0) {
- if (xwrite (fd, buf, r) == -1) {
- perrorf (g, "%s: write", filename);
- free (buf);
- goto cancel;
- }
- free (buf);
- }
-
- if (r == -1) {
- error (g, "%s: error in chunked encoding", filename);
- return -1;
- }
-
- if (close (fd) == -1) {
- perrorf (g, "close: %s", filename);
- return -1;
- }
-
- return 0;
-
- cancel: ;
- /* Send cancellation message to daemon, then wait until it
- * cancels (just throwing away data).
- */
- XDR xdr;
- char fbuf[4];
- uint32_t flag = GUESTFS_CANCEL_FLAG;
-
- xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
- xdr_uint32_t (&xdr, &flag);
- xdr_destroy (&xdr);
-
- if (xwrite (g->sock, fbuf, sizeof fbuf) == -1) {
- perrorf (g, "write to daemon socket");
- return -1;
- }
-
- while ((r = receive_file_data_sync (g, &buf)) > 0)
- free (buf); /* just discard it */
-
- return -1;
-}
-
-struct receive_file_ctx {
- int code;
- void **buf;
-};
-
-static void
-receive_file_cb (guestfs_h *g, void *data, XDR *xdr)
-{
- guestfs_main_loop *ml = guestfs_get_main_loop (g);
- struct receive_file_ctx *ctx = (struct receive_file_ctx *) data;
- guestfs_chunk chunk;
-
- ml->main_loop_quit (ml, g);
-
- memset (&chunk, 0, sizeof chunk);
-
- if (!xdr_guestfs_chunk (xdr, &chunk)) {
- error (g, "failed to parse file chunk");
- ctx->code = -1;
- return;
- }
- if (chunk.cancel) {
- error (g, "file receive cancelled by daemon");
- ctx->code = -2;
- return;
- }
- if (chunk.data.data_len == 0) { /* end of transfer */
- ctx->code = 0;
- return;
- }
-
- ctx->code = chunk.data.data_len;
- *ctx->buf = chunk.data.data_val; /* caller frees */
-}
-
-/* Receive a chunk of file data. */
-static int
-receive_file_data_sync (guestfs_h *g, void **buf)
-{
- struct receive_file_ctx ctx;
- guestfs_main_loop *ml = guestfs_get_main_loop (g);
-
- ctx.code = -3;
- ctx.buf = buf;
-
- guestfs_set_reply_callback (g, receive_file_cb, &ctx);
- (void) ml->main_loop_run (ml, g);
- guestfs_set_reply_callback (g, NULL, NULL);
-
- if (g->verbose)
- fprintf (stderr, "receive_file_data_sync: code %d\n", ctx.code);
-
- switch (ctx.code) {
- case 0: /* end of file */
- return 0;
- case -1: case -2:
- return -1;
- case -3:
- error (g, "failed to call receive_file_cb");
- return -1;
- default: /* received n bytes of data */
- return ctx.code;
- }
-}
-
-/* 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,
- guestfs_handle_event_cb cb, void *data)
-{
- struct select_main_loop *ml = (struct select_main_loop *) mlv;
-
- if (fd < 0 || fd >= FD_SETSIZE) {
- error (g, "fd %d is out of range", fd);
- return -1;
- }
-
- if ((events & ~(GUESTFS_HANDLE_READABLE |
- GUESTFS_HANDLE_WRITABLE |
- GUESTFS_HANDLE_HANGUP |
- GUESTFS_HANDLE_ERROR)) != 0) {
- error (g, "set of events (0x%x) contains unknown events", events);
- return -1;
- }
-
- if (events == 0) {
- error (g, "set of events is empty");
- return -1;
- }
-
- if (FD_ISSET (fd, &ml->rset) ||
- FD_ISSET (fd, &ml->wset) ||
- FD_ISSET (fd, &ml->xset)) {
- error (g, "fd %d is already registered", fd);
- return -1;
- }
-
- if (cb == NULL) {
- error (g, "callback is NULL");
- return -1;
- }
-
- if ((events & GUESTFS_HANDLE_READABLE))
- FD_SET (fd, &ml->rset);
- if ((events & GUESTFS_HANDLE_WRITABLE))
- FD_SET (fd, &ml->wset);
- if ((events & GUESTFS_HANDLE_HANGUP) || (events & GUESTFS_HANDLE_ERROR))
- FD_SET (fd, &ml->xset);
-
- if (fd > ml->max_fd) {
- ml->max_fd = fd;
- ml->handle_cb_data =
- safe_realloc (g, ml->handle_cb_data,
- sizeof (struct select_handle_cb_data) * (ml->max_fd+1));
- }
- ml->handle_cb_data[fd].cb = cb;
- ml->handle_cb_data[fd].g = g;
- ml->handle_cb_data[fd].data = data;
-
- ml->nr_fds++;
-
- /* Any integer >= 0 can be the handle, and this is as good as any ... */
- return fd;
-}
-
-static int
-select_remove_handle (guestfs_main_loop *mlv, guestfs_h *g, int fd)