87139b487277ee1c25060eee16db8cea4dc49062
[libguestfs.git] / ocaml / guestfs_c.c
1 /* libguestfs
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <guestfs.h>
24
25 #include <caml/config.h>
26 #include <caml/alloc.h>
27 #include <caml/callback.h>
28 #include <caml/custom.h>
29 #include <caml/fail.h>
30 #include <caml/memory.h>
31 #include <caml/mlvalues.h>
32
33 #include "guestfs_c.h"
34
35 /* Allocate handles and deal with finalization. */
36 static void
37 guestfs_finalize (value gv)
38 {
39   guestfs_h *g = Guestfs_val (gv);
40   if (g) guestfs_close (g);
41 }
42
43 static struct custom_operations guestfs_custom_operations = {
44   "guestfs_custom_operations",
45   guestfs_finalize,
46   custom_compare_default,
47   custom_hash_default,
48   custom_serialize_default,
49   custom_deserialize_default
50 };
51
52 static value
53 Val_guestfs (guestfs_h *g)
54 {
55   CAMLparam0 ();
56   CAMLlocal1 (rv);
57
58   rv = caml_alloc_custom (&guestfs_custom_operations,
59                           sizeof (guestfs_h *), 0, 1);
60   Guestfs_val (rv) = g;
61
62   CAMLreturn (rv);
63 }
64
65 void
66 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
67 {
68   CAMLparam0 ();
69   CAMLlocal1 (v);
70   const char *msg;
71
72   msg = guestfs_last_error (g);
73
74   if (msg)
75     v = caml_copy_string (msg);
76   else
77     v = caml_copy_string (func);
78   caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
79   CAMLnoreturn;
80 }
81
82 /* Guestfs.create */
83 CAMLprim value
84 ocaml_guestfs_create (void)
85 {
86   CAMLparam0 ();
87   CAMLlocal1 (gv);
88   guestfs_h *g;
89
90   g = guestfs_create ();
91   if (g == NULL)
92     caml_failwith ("failed to create guestfs handle");
93
94   guestfs_set_error_handler (g, NULL, NULL);
95
96   gv = Val_guestfs (g);
97   CAMLreturn (gv);
98 }
99
100 /* Guestfs.close */
101 CAMLprim value
102 ocaml_guestfs_close (value gv)
103 {
104   CAMLparam1 (gv);
105
106   guestfs_finalize (gv);
107
108   /* So we don't double-free in the finalizer. */
109   Guestfs_val (gv) = NULL;
110
111   CAMLreturn (Val_unit);
112 }
113
114 /* Copy string array value. */
115 char **
116 ocaml_guestfs_strings_val (value sv)
117 {
118   CAMLparam1 (sv);
119   char **r;
120   int i;
121
122   r = malloc (sizeof (char *) * (Wosize_val (sv) + 1));
123   for (i = 0; i < Wosize_val (sv); ++i)
124     r[i] = String_val (Field (sv, i));
125   r[i] = NULL;
126
127   CAMLreturnT (char **, r);
128 }
129
130 /* Free array of strings. */
131 void
132 ocaml_guestfs_free_strings (char **argv)
133 {
134   /* Don't free the actual strings - they are String_vals on
135    * the OCaml heap.
136    */
137   free (argv);
138 }