fprintf (stderr, "warning: \"guestfs_df_h\" has no tests\n");
}
+static int test_du_0_skip (void)
+{
+ const char *str;
+
+ str = getenv ("TEST_ONLY");
+ if (str)
+ return strstr (str, "du") == NULL;
+ str = getenv ("SKIP_TEST_DU_0");
+ if (str && strcmp (str, "1") == 0) return 1;
+ str = getenv ("SKIP_TEST_DU");
+ if (str && strcmp (str, "1") == 0) return 1;
+ return 0;
+}
+
+static int test_du_0 (void)
+{
+ if (test_du_0_skip ()) {
+ printf ("%s skipped (reason: environment variable set)\n", "test_du_0");
+ return 0;
+ }
+
+ /* InitBasicFS for test_du_0: create ext2 on /dev/sda1 */
+ {
+ char device[] = "/dev/sda";
+ int r;
+ suppress_error = 0;
+ r = guestfs_blockdev_setrw (g, device);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_umount_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_lvm_remove_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char device[] = "/dev/sda";
+ char lines_0[] = ",";
+ char *lines[] = {
+ lines_0,
+ NULL
+ };
+ int r;
+ suppress_error = 0;
+ r = guestfs_sfdisk (g, device, 0, 0, 0, lines);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char fstype[] = "ext2";
+ char device[] = "/dev/sda1";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mkfs (g, fstype, device);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char device[] = "/dev/sda1";
+ char mountpoint[] = "/";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mount (g, device, mountpoint);
+ if (r == -1)
+ return -1;
+ }
+ /* TestOutputInt for du (0) */
+ {
+ char path[] = "/p";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mkdir (g, path);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char path[] = "/p";
+ int64_t r;
+ suppress_error = 0;
+ r = guestfs_du (g, path);
+ if (r == -1)
+ return -1;
+ if (r != 1) {
+ fprintf (stderr, "test_du_0: expected 1 but got %d\n", (int) r);
+ return -1;
+ }
+ }
+ return 0;
+}
+
static int test_tail_n_0_skip (void)
{
const char *str;
/* Cancel previous alarm. */
alarm (0);
- nr_tests = 164;
+ nr_tests = 165;
test_num++;
+ printf ("%3d/%3d test_du_0\n", test_num, nr_tests);
+ if (test_du_0 () == -1) {
+ printf ("test_du_0 FAILED\n");
+ failed++;
+ }
+ test_num++;
printf ("%3d/%3d test_tail_n_0\n", test_num, nr_tests);
if (test_tail_n_0 () == -1) {
printf ("test_tail_n_0 FAILED\n");
extern char **do_tail_n (int nrlines, char *path);
extern char *do_df (void);
extern char *do_df_h (void);
+extern int64_t do_du (char *path);
done: ;
}
+static void du_stub (XDR *xdr_in)
+{
+ int64_t r;
+ struct guestfs_du_args args;
+ char *path;
+
+ memset (&args, 0, sizeof args);
+
+ if (!xdr_guestfs_du_args (xdr_in, &args)) {
+ reply_with_error ("%s: daemon failed to decode procedure arguments", "du");
+ return;
+ }
+ path = args.path;
+
+ r = do_du (path);
+ if (r == -1)
+ /* do_du has already called reply_with_error */
+ goto done;
+
+ struct guestfs_du_ret ret;
+ ret.sizekb = r;
+ reply ((xdrproc_t) &xdr_guestfs_du_ret, (char *) &ret);
+done:
+ xdr_free ((xdrproc_t) xdr_guestfs_du_args, (char *) &args);
+}
+
void dispatch_incoming_message (XDR *xdr_in)
{
switch (proc_nr) {
case GUESTFS_PROC_DF_H:
df_h_stub (xdr_in);
break;
+ case GUESTFS_PROC_DU:
+ du_stub (xdr_in);
+ break;
default:
reply_with_error ("dispatch_incoming_message: unknown procedure number %d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory", proc_nr);
}
printf ("%-20s %s\n", "dmesg", "return kernel messages");
printf ("%-20s %s\n", "download", "download a file to the local machine");
printf ("%-20s %s\n", "drop-caches", "drop kernel page cache, dentries and inodes");
+ printf ("%-20s %s\n", "du", "estimate file space usage");
printf ("%-20s %s\n", "e2fsck-f", "check an ext2/ext3 filesystem");
printf ("%-20s %s\n", "equal", "test if two files have equal contents");
printf ("%-20s %s\n", "exists", "test if file or directory exists");
if (strcasecmp (cmd, "df_h") == 0 || strcasecmp (cmd, "df-h") == 0)
pod2text ("df-h - report file system disk space usage (human readable)", " df-h\n\nThis command runs the C<df -h> command to report disk space used\nin human-readable format.\n\nThis command is mostly useful for interactive sessions. It\nis I<not> intended that you try to parse the output string.\nUse C<statvfs> from programs.");
else
+ if (strcasecmp (cmd, "du") == 0)
+ pod2text ("du - estimate file space usage", " du <path>\n\nThis command runs the C<du -s> command to estimate file space\nusage for C<path>.\n\nC<path> can be a file or a directory. If C<path> is a directory\nthen the estimate includes the contents of the directory and all\nsubdirectories (recursively).\n\nThe result is the estimated size in I<kilobytes>\n(ie. units of 1024 bytes).");
+ else
display_builtin_command (cmd);
}
return 0;
}
+static int run_du (const char *cmd, int argc, char *argv[])
+{
+ int64_t r;
+ const char *path;
+ if (argc != 1) {
+ fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
+ fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
+ return -1;
+ }
+ path = argv[0];
+ r = guestfs_du (g, path);
+ if (r == -1) return -1;
+ printf ("%" PRIi64 "\n", r);
+ return 0;
+}
+
int run_action (const char *cmd, int argc, char *argv[])
{
if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0)
if (strcasecmp (cmd, "df_h") == 0 || strcasecmp (cmd, "df-h") == 0)
return run_df_h (cmd, argc, argv);
else
+ if (strcasecmp (cmd, "du") == 0)
+ return run_du (cmd, argc, argv);
+ else
{
fprintf (stderr, "%s: unknown command\n", cmd);
return -1;
"tail-n",
"df",
"df-h",
+ "du",
NULL
};
This automatically calls L<sync(2)> before the operation,
so that the maximum guest memory is freed.
+=head2 du
+
+ du path
+
+This command runs the C<du -s> command to estimate file space
+usage for C<path>.
+
+C<path> can be a file or a directory. If C<path> is a directory
+then the estimate includes the contents of the directory and all
+subdirectories (recursively).
+
+The result is the estimated size in I<kilobytes>
+(ie. units of 1024 bytes).
+
=head2 e2fsck-f
e2fsck-f device
This function returns 0 on success or -1 on error.
+=head2 guestfs_du
+
+ int64_t guestfs_du (guestfs_h *handle,
+ const char *path);
+
+This command runs the C<du -s> command to estimate file space
+usage for C<path>.
+
+C<path> can be a file or a directory. If C<path> is a directory
+then the estimate includes the contents of the directory and all
+subdirectories (recursively).
+
+The result is the estimated size in I<kilobytes>
+(ie. units of 1024 bytes).
+
+On error this function returns -1.
+
=head2 guestfs_e2fsck_f
int guestfs_e2fsck_f (guestfs_h *handle,
scrub_freespace,
wc_l,
wc_w,
- wc_c
+ wc_c,
+ du
) where
import Foreign
import Foreign.C
fail err
else return (fromIntegral r)
+foreign import ccall unsafe "guestfs_du" c_du
+ :: GuestfsP -> CString -> IO (Int64)
+
+du :: GuestfsH -> String -> IO (Integer)
+du h path = do
+ r <- withCString path $ \path -> withForeignPtr h (\p -> c_du p path)
+ if (r == -1)
+ then do
+ err <- last_error h
+ fail err
+ else return (fromIntegral r)
+
private native String _df_h (long g)
throws LibGuestFSException;
+ /**
+ * estimate file space usage
+ * <p>
+ * This command runs the "du -s" command to estimate file
+ * space usage for "path".
+ * <p>
+ * "path" can be a file or a directory. If "path" is a
+ * directory then the estimate includes the contents of the
+ * directory and all subdirectories (recursively).
+ * <p>
+ * The result is the estimated size in *kilobytes* (ie.
+ * units of 1024 bytes).
+ * <p>
+ * @throws LibGuestFSException
+ */
+ public long du (String path)
+ throws LibGuestFSException
+ {
+ if (g == 0)
+ throw new LibGuestFSException ("du: handle is closed");
+ return _du (g, path);
+ }
+ private native long _du (long g, String path)
+ throws LibGuestFSException;
+
}
return jr;
}
+JNIEXPORT jlong JNICALL
+Java_com_redhat_et_libguestfs_GuestFS__1du
+ (JNIEnv *env, jobject obj, jlong jg, jstring jpath)
+{
+ guestfs_h *g = (guestfs_h *) (long) jg;
+ int64_t r;
+ const char *path;
+
+ path = (*env)->GetStringUTFChars (env, jpath, NULL);
+ r = guestfs_du (g, path);
+ (*env)->ReleaseStringUTFChars (env, jpath, path);
+ if (r == -1) {
+ throw_exception (env, guestfs_last_error (g));
+ return 0;
+ }
+ return (jlong) r;
+}
+
external tail_n : t -> int -> string -> string array = "ocaml_guestfs_tail_n"
external df : t -> string = "ocaml_guestfs_df"
external df_h : t -> string = "ocaml_guestfs_df_h"
+external du : t -> string -> int64 = "ocaml_guestfs_du"
val df_h : t -> string
(** report file system disk space usage (human readable) *)
+val du : t -> string -> int64
+(** estimate file space usage *)
+
CAMLreturn (rv);
}
+CAMLprim value
+ocaml_guestfs_du (value gv, value pathv)
+{
+ CAMLparam2 (gv, pathv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("du: used handle after closing it");
+
+ const char *path = String_val (pathv);
+ int64_t r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_du (g, path);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "du");
+
+ rv = caml_copy_int64 (r);
+ CAMLreturn (rv);
+}
+
OUTPUT:
RETVAL
+SV *
+du (g, path)
+ guestfs_h *g;
+ char *path;
+PREINIT:
+ int64_t sizekb;
+ CODE:
+ sizekb = guestfs_du (g, path);
+ if (sizekb == -1)
+ croak ("du: %s", guestfs_last_error (g));
+ RETVAL = my_newSVll (sizekb);
+ OUTPUT:
+ RETVAL
+
This automatically calls L<sync(2)> before the operation,
so that the maximum guest memory is freed.
+=item $sizekb = $h->du ($path);
+
+This command runs the C<du -s> command to estimate file space
+usage for C<path>.
+
+C<path> can be a file or a directory. If C<path> is a directory
+then the estimate includes the contents of the directory and all
+subdirectories (recursively).
+
+The result is the estimated size in I<kilobytes>
+(ie. units of 1024 bytes).
+
=item $h->e2fsck_f ($device);
This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
return py_r;
}
+static PyObject *
+py_guestfs_du (PyObject *self, PyObject *args)
+{
+ PyObject *py_g;
+ guestfs_h *g;
+ PyObject *py_r;
+ int64_t r;
+ const char *path;
+
+ if (!PyArg_ParseTuple (args, (char *) "Os:guestfs_du",
+ &py_g, &path))
+ return NULL;
+ g = get_handle (py_g);
+
+ r = guestfs_du (g, path);
+ if (r == -1) {
+ PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
+ return NULL;
+ }
+
+ py_r = PyLong_FromLongLong (r);
+ return py_r;
+}
+
static PyMethodDef methods[] = {
{ (char *) "create", py_guestfs_create, METH_VARARGS, NULL },
{ (char *) "close", py_guestfs_close, METH_VARARGS, NULL },
{ (char *) "tail_n", py_guestfs_tail_n, METH_VARARGS, NULL },
{ (char *) "df", py_guestfs_df, METH_VARARGS, NULL },
{ (char *) "df_h", py_guestfs_df_h, METH_VARARGS, NULL },
+ { (char *) "du", py_guestfs_du, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
"""
return libguestfsmod.df_h (self._o)
+ def du (self, path):
+ u"""This command runs the "du -s" command to estimate file
+ space usage for "path".
+
+ "path" can be a file or a directory. If "path" is a
+ directory then the estimate includes the contents of the
+ directory and all subdirectories (recursively).
+
+ The result is the estimated size in *kilobytes* (ie.
+ units of 1024 bytes).
+ """
+ return libguestfsmod.du (self._o, path)
+
return rv;
}
+static VALUE ruby_guestfs_du (VALUE gv, VALUE pathv)
+{
+ guestfs_h *g;
+ Data_Get_Struct (gv, guestfs_h, g);
+ if (!g)
+ rb_raise (rb_eArgError, "%s: used handle after closing it", "du");
+
+ Check_Type (pathv, T_STRING);
+ const char *path = StringValueCStr (pathv);
+ if (!path)
+ rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+ "path", "du");
+
+ int64_t r;
+
+ r = guestfs_du (g, path);
+ if (r == -1)
+ rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+ return ULL2NUM (r);
+}
+
/* Initialize the module. */
void Init__guestfs ()
{
ruby_guestfs_df, 0);
rb_define_method (c_guestfs, "df_h",
ruby_guestfs_df_h, 0);
+ rb_define_method (c_guestfs, "du",
+ ruby_guestfs_du, 1);
}
return ctx.ret.output; /* caller will free */
}
+struct du_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;
+ struct guestfs_du_ret ret;
+};
+
+static void du_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct du_ctx *ctx = (struct du_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_du");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_du");
+ 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_du");
+ return;
+ }
+ goto done;
+ }
+ if (!xdr_guestfs_du_ret (xdr, &ctx->ret)) {
+ error (g, "%s: failed to parse reply", "guestfs_du");
+ return;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+int64_t guestfs_du (guestfs_h *g,
+ const char *path)
+{
+ struct guestfs_du_args args;
+ struct du_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_du") == -1) return -1;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.path = (char *) path;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_DU,
+ (xdrproc_t) xdr_guestfs_du_args, (char *) &args);
+ if (serial == -1) {
+ guestfs_end_busy (g);
+ return -1;
+ }
+
+ guestfs__switch_to_receiving (g);
+ ctx.cb_sequence = 0;
+ guestfs_set_reply_callback (g, du_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_du");
+ guestfs_end_busy (g);
+ return -1;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DU, serial) == -1) {
+ guestfs_end_busy (g);
+ return -1;
+ }
+
+ if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+ error (g, "%s", ctx.err.error_message);
+ free (ctx.err.error_message);
+ guestfs_end_busy (g);
+ return -1;
+ }
+
+ guestfs_end_busy (g);
+ return ctx.ret.sizekb;
+}
+
extern char **guestfs_tail_n (guestfs_h *handle, int nrlines, const char *path);
extern char *guestfs_df (guestfs_h *handle);
extern char *guestfs_df_h (guestfs_h *handle);
+extern int64_t guestfs_du (guestfs_h *handle, const char *path);
}
bool_t
+xdr_guestfs_du_args (XDR *xdrs, guestfs_du_args *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->path, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
+xdr_guestfs_du_ret (XDR *xdrs, guestfs_du_ret *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_quad_t (xdrs, &objp->sizekb))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp)
{
register int32_t *buf;
};
typedef struct guestfs_df_h_ret guestfs_df_h_ret;
+struct guestfs_du_args {
+ char *path;
+};
+typedef struct guestfs_du_args guestfs_du_args;
+
+struct guestfs_du_ret {
+ quad_t sizekb;
+};
+typedef struct guestfs_du_ret guestfs_du_ret;
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_TAIL_N = 124,
GUESTFS_PROC_DF = 125,
GUESTFS_PROC_DF_H = 126,
- GUESTFS_PROC_NR_PROCS = 126 + 1,
+ GUESTFS_PROC_DU = 127,
+ GUESTFS_PROC_NR_PROCS = 127 + 1,
};
typedef enum guestfs_procedure guestfs_procedure;
#define GUESTFS_MESSAGE_MAX 4194304
extern bool_t xdr_guestfs_tail_n_ret (XDR *, guestfs_tail_n_ret*);
extern bool_t xdr_guestfs_df_ret (XDR *, guestfs_df_ret*);
extern bool_t xdr_guestfs_df_h_ret (XDR *, guestfs_df_h_ret*);
+extern bool_t xdr_guestfs_du_args (XDR *, guestfs_du_args*);
+extern bool_t xdr_guestfs_du_ret (XDR *, guestfs_du_ret*);
extern bool_t xdr_guestfs_procedure (XDR *, guestfs_procedure*);
extern bool_t xdr_guestfs_message_direction (XDR *, guestfs_message_direction*);
extern bool_t xdr_guestfs_message_status (XDR *, guestfs_message_status*);
extern bool_t xdr_guestfs_tail_n_ret ();
extern bool_t xdr_guestfs_df_ret ();
extern bool_t xdr_guestfs_df_h_ret ();
+extern bool_t xdr_guestfs_du_args ();
+extern bool_t xdr_guestfs_du_ret ();
extern bool_t xdr_guestfs_procedure ();
extern bool_t xdr_guestfs_message_direction ();
extern bool_t xdr_guestfs_message_status ();
string output<>;
};
+struct guestfs_du_args {
+ string path<>;
+};
+
+struct guestfs_du_ret {
+ hyper sizekb;
+};
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_TAIL_N = 124,
GUESTFS_PROC_DF = 125,
GUESTFS_PROC_DF_H = 126,
+ GUESTFS_PROC_DU = 127,
GUESTFS_PROC_NR_PROCS
};