summaryrefslogtreecommitdiffstats
path: root/ocaml
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-04-10 18:25:07 +0100
committerRichard Jones <rjones@redhat.com>2009-04-11 17:04:35 +0100
commitb4d2a01828e5de85e5eee3631f7fe3925a0312ca (patch)
tree6f77ecceccd53a84afce4cecf0fe199ba7707752 /ocaml
parent99f68f259f92eee884c6c7396f61b9c16e2bf354 (diff)
downloadlibguestfs-b4d2a01828e5de85e5eee3631f7fe3925a0312ca.tar.gz
libguestfs-b4d2a01828e5de85e5eee3631f7fe3925a0312ca.tar.xz
libguestfs-b4d2a01828e5de85e5eee3631f7fe3925a0312ca.zip
Added test suite.
Diffstat (limited to 'ocaml')
-rw-r--r--ocaml/guestfs.ml13
-rw-r--r--ocaml/guestfs.mli39
-rw-r--r--ocaml/guestfs_c.c26
-rw-r--r--ocaml/guestfs_c.h2
-rw-r--r--ocaml/guestfs_c_actions.c318
5 files changed, 398 insertions, 0 deletions
diff --git a/ocaml/guestfs.ml b/ocaml/guestfs.ml
index 32a6b82a..58f99dc2 100644
--- a/ocaml/guestfs.ml
+++ b/ocaml/guestfs.ml
@@ -132,3 +132,16 @@ external mkdir : t -> string -> unit = "ocaml_guestfs_mkdir"
external mkdir_p : t -> string -> unit = "ocaml_guestfs_mkdir_p"
external chmod : t -> int -> string -> unit = "ocaml_guestfs_chmod"
external chown : t -> int -> int -> string -> unit = "ocaml_guestfs_chown"
+external exists : t -> string -> bool = "ocaml_guestfs_exists"
+external is_file : t -> string -> bool = "ocaml_guestfs_is_file"
+external is_dir : t -> string -> bool = "ocaml_guestfs_is_dir"
+external pvcreate : t -> string -> unit = "ocaml_guestfs_pvcreate"
+external vgcreate : t -> string -> string array -> unit = "ocaml_guestfs_vgcreate"
+external lvcreate : t -> string -> string -> int -> unit = "ocaml_guestfs_lvcreate"
+external mkfs : t -> string -> string -> unit = "ocaml_guestfs_mkfs"
+external sfdisk : t -> string -> int -> int -> int -> string array -> unit = "ocaml_guestfs_sfdisk_byte" "ocaml_guestfs_sfdisk"
+external write_file : t -> string -> string -> int -> unit = "ocaml_guestfs_write_file"
+external umount : t -> string -> unit = "ocaml_guestfs_umount"
+external mounts : t -> string array = "ocaml_guestfs_mounts"
+external umount_all : t -> unit = "ocaml_guestfs_umount_all"
+external lvm_remove_all : t -> unit = "ocaml_guestfs_lvm_remove_all"
diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli
index ecf86f11..1ce1cab2 100644
--- a/ocaml/guestfs.mli
+++ b/ocaml/guestfs.mli
@@ -235,3 +235,42 @@ val chmod : t -> int -> string -> unit
val chown : t -> int -> int -> string -> unit
(** change file owner and group *)
+val exists : t -> string -> bool
+(** test if file or directory exists *)
+
+val is_file : t -> string -> bool
+(** test if file exists *)
+
+val is_dir : t -> string -> bool
+(** test if file exists *)
+
+val pvcreate : t -> string -> unit
+(** create an LVM physical volume *)
+
+val vgcreate : t -> string -> string array -> unit
+(** create an LVM volume group *)
+
+val lvcreate : t -> string -> string -> int -> unit
+(** create an LVM volume group *)
+
+val mkfs : t -> string -> string -> unit
+(** make a filesystem *)
+
+val sfdisk : t -> string -> int -> int -> int -> string array -> unit
+(** create partitions on a block device *)
+
+val write_file : t -> string -> string -> int -> unit
+(** Create a file *)
+
+val umount : t -> string -> unit
+(** unmount a filesystem *)
+
+val mounts : t -> string array
+(** show mounted filesystems *)
+
+val umount_all : t -> unit
+(** unmount all filesystems *)
+
+val lvm_remove_all : t -> unit
+(** remove all LVM LVs, VGs and PVs *)
+
diff --git a/ocaml/guestfs_c.c b/ocaml/guestfs_c.c
index 600440c9..291c4308 100644
--- a/ocaml/guestfs_c.c
+++ b/ocaml/guestfs_c.c
@@ -119,3 +119,29 @@ ocaml_guestfs_close (value gv)
CAMLreturn (Val_unit);
}
+
+/* Copy string array value. */
+char **
+ocaml_guestfs_strings_val (value sv)
+{
+ CAMLparam1 (sv);
+ char **r;
+ int i;
+
+ r = malloc (sizeof (char *) * (Wosize_val (sv) + 1));
+ for (i = 0; i < Wosize_val (sv); ++i)
+ r[i] = String_val (Field (sv, i));
+ r[i] = NULL;
+
+ CAMLreturnT (char **, r);
+}
+
+/* Free array of strings. */
+void
+ocaml_guestfs_free_strings (char **argv)
+{
+ /* Don't free the actual strings - they are String_vals on
+ * the OCaml heap.
+ */
+ free (argv);
+}
diff --git a/ocaml/guestfs_c.h b/ocaml/guestfs_c.h
index 3da41d05..4fb81881 100644
--- a/ocaml/guestfs_c.h
+++ b/ocaml/guestfs_c.h
@@ -22,5 +22,7 @@
#define Guestfs_val(v) (*((guestfs_h **)Data_custom_val(v)))
extern void ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
Noreturn;
+extern char **ocaml_guestfs_strings_val (value sv);
+extern void ocaml_guestfs_free_strings (char **r);
#endif /* GUESTFS_OCAML_C_H */
diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c
index 6675cf8c..6b1a9f62 100644
--- a/ocaml/guestfs_c_actions.c
+++ b/ocaml/guestfs_c_actions.c
@@ -1347,3 +1347,321 @@ ocaml_guestfs_chown (value gv, value ownerv, value groupv, value pathv)
CAMLreturn (rv);
}
+CAMLprim value
+ocaml_guestfs_exists (value gv, value pathv)
+{
+ CAMLparam2 (gv, pathv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("exists: used handle after closing it");
+
+ const char *path = String_val (pathv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_exists (g, path);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "exists");
+
+ rv = Val_bool (r);
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_is_file (value gv, value pathv)
+{
+ CAMLparam2 (gv, pathv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("is_file: used handle after closing it");
+
+ const char *path = String_val (pathv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_is_file (g, path);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "is_file");
+
+ rv = Val_bool (r);
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_is_dir (value gv, value pathv)
+{
+ CAMLparam2 (gv, pathv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("is_dir: used handle after closing it");
+
+ const char *path = String_val (pathv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_is_dir (g, path);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "is_dir");
+
+ rv = Val_bool (r);
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_pvcreate (value gv, value devicev)
+{
+ CAMLparam2 (gv, devicev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("pvcreate: used handle after closing it");
+
+ const char *device = String_val (devicev);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_pvcreate (g, device);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "pvcreate");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_vgcreate (value gv, value volgroupv, value physvolsv)
+{
+ CAMLparam3 (gv, volgroupv, physvolsv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("vgcreate: used handle after closing it");
+
+ const char *volgroup = String_val (volgroupv);
+ char **physvols = ocaml_guestfs_strings_val (physvolsv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_vgcreate (g, volgroup, physvols);
+ caml_leave_blocking_section ();
+ ocaml_guestfs_free_strings (physvols);
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "vgcreate");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_lvcreate (value gv, value logvolv, value volgroupv, value mbytesv)
+{
+ CAMLparam4 (gv, logvolv, volgroupv, mbytesv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("lvcreate: used handle after closing it");
+
+ const char *logvol = String_val (logvolv);
+ const char *volgroup = String_val (volgroupv);
+ int mbytes = Int_val (mbytesv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_lvcreate (g, logvol, volgroup, mbytes);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "lvcreate");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_mkfs (value gv, value fstypev, value devicev)
+{
+ CAMLparam3 (gv, fstypev, devicev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("mkfs: used handle after closing it");
+
+ const char *fstype = String_val (fstypev);
+ const char *device = String_val (devicev);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_mkfs (g, fstype, device);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "mkfs");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_sfdisk (value gv, value devicev, value cylsv, value headsv, value sectorsv, value linesv)
+{
+ CAMLparam5 (gv, devicev, cylsv, headsv, sectorsv);
+ CAMLxparam1 (linesv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("sfdisk: used handle after closing it");
+
+ const char *device = String_val (devicev);
+ int cyls = Int_val (cylsv);
+ int heads = Int_val (headsv);
+ int sectors = Int_val (sectorsv);
+ char **lines = ocaml_guestfs_strings_val (linesv);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_sfdisk (g, device, cyls, heads, sectors, lines);
+ caml_leave_blocking_section ();
+ ocaml_guestfs_free_strings (lines);
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "sfdisk");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_sfdisk_byte (value *argv, int argn)
+{
+ return ocaml_guestfs_sfdisk (argv[0], argv[0], argv[1], argv[2], argv[3], argv[4]);
+}
+
+CAMLprim value
+ocaml_guestfs_write_file (value gv, value pathv, value contentv, value sizev)
+{
+ CAMLparam4 (gv, pathv, contentv, sizev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("write_file: used handle after closing it");
+
+ const char *path = String_val (pathv);
+ const char *content = String_val (contentv);
+ int size = Int_val (sizev);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_write_file (g, path, content, size);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "write_file");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_umount (value gv, value pathordevicev)
+{
+ CAMLparam2 (gv, pathordevicev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("umount: used handle after closing it");
+
+ const char *pathordevice = String_val (pathordevicev);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_umount (g, pathordevice);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "umount");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_mounts (value gv)
+{
+ CAMLparam1 (gv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("mounts: used handle after closing it");
+
+ int i;
+ char **r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_mounts (g);
+ caml_leave_blocking_section ();
+ if (r == NULL)
+ ocaml_guestfs_raise_error (g, "mounts");
+
+ rv = caml_copy_string_array ((const char **) r);
+ for (i = 0; r[i] != NULL; ++i) free (r[i]);
+ free (r);
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_umount_all (value gv)
+{
+ CAMLparam1 (gv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("umount_all: used handle after closing it");
+
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_umount_all (g);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "umount_all");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
+CAMLprim value
+ocaml_guestfs_lvm_remove_all (value gv)
+{
+ CAMLparam1 (gv);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("lvm_remove_all: used handle after closing it");
+
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_lvm_remove_all (g);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "lvm_remove_all");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+