X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=src%2Fguestfs-actions.c;h=99f8a446468d2bcf65125a51f9549c7e57e456bf;hp=499235517ecbc2c58005cc14bcb9ce7f511e6a03;hb=9222136ac9b2e404dba128b1ac74dacaa8bf1038;hpb=2dc9e8a858b62830d15a8186fe575eb7903d2171;ds=sidebyside diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index 4992355..99f8a44 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -8080,3 +8080,89 @@ int guestfs_mv (guestfs_h *g, return 0; } +struct drop_caches_ctx { + /* This flag is set by the callbacks, so we know we've done + * the callbacks as expected, and in the right sequence. + * 0 = not called, 1 = reply_cb called. + */ + int cb_sequence; + struct guestfs_message_header hdr; + struct guestfs_message_error err; +}; + +static void drop_caches_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct drop_caches_ctx *ctx = (struct drop_caches_ctx *) data; + + /* This should definitely not happen. */ + if (ctx->cb_sequence != 0) { + ctx->cb_sequence = 9999; + error (g, "%s: internal error: reply callback called twice", "guestfs_drop_caches"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_drop_caches"); + return; + } + if (ctx->hdr.status == GUESTFS_STATUS_ERROR) { + if (!xdr_guestfs_message_error (xdr, &ctx->err)) { + error (g, "%s: failed to parse reply error", "guestfs_drop_caches"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_drop_caches (guestfs_h *g, + int whattodrop) +{ + struct guestfs_drop_caches_args args; + struct drop_caches_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_drop_caches") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.whattodrop = whattodrop; + serial = guestfs__send_sync (g, GUESTFS_PROC_DROP_CACHES, + (xdrproc_t) xdr_guestfs_drop_caches_args, (char *) &args); + if (serial == -1) { + guestfs_set_ready (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, drop_caches_reply_cb, &ctx); + (void) ml->main_loop_run (ml, g); + guestfs_set_reply_callback (g, NULL, NULL); + if (ctx.cb_sequence != 1) { + error (g, "%s reply failed, see earlier error messages", "guestfs_drop_caches"); + guestfs_set_ready (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DROP_CACHES, serial) == -1) { + guestfs_set_ready (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + guestfs_set_ready (g); + return -1; + } + + guestfs_set_ready (g); + return 0; +} +