Move the appliance and build scripts into new appliance/ subdirectory.
[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 /* This macro was added in OCaml 3.10.  Backport for earlier versions. */
36 #ifndef CAMLreturnT
37 #define CAMLreturnT(type, result) do{ \
38   type caml__temp_result = (result); \
39   caml_local_roots = caml__frame; \
40   return (caml__temp_result); \
41 }while(0)
42 #endif
43
44 /* Allocate handles and deal with finalization. */
45 static void
46 guestfs_finalize (value gv)
47 {
48   guestfs_h *g = Guestfs_val (gv);
49   if (g) guestfs_close (g);
50 }
51
52 static struct custom_operations guestfs_custom_operations = {
53   "guestfs_custom_operations",
54   guestfs_finalize,
55   custom_compare_default,
56   custom_hash_default,
57   custom_serialize_default,
58   custom_deserialize_default
59 };
60
61 static value
62 Val_guestfs (guestfs_h *g)
63 {
64   CAMLparam0 ();
65   CAMLlocal1 (rv);
66
67   rv = caml_alloc_custom (&guestfs_custom_operations,
68                           sizeof (guestfs_h *), 0, 1);
69   Guestfs_val (rv) = g;
70
71   CAMLreturn (rv);
72 }
73
74 void
75 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
76 {
77   CAMLparam0 ();
78   CAMLlocal1 (v);
79   const char *msg;
80
81   msg = guestfs_last_error (g);
82
83   if (msg)
84     v = caml_copy_string (msg);
85   else
86     v = caml_copy_string (func);
87   caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
88   CAMLnoreturn;
89 }
90
91 /* Guestfs.create */
92 CAMLprim value
93 ocaml_guestfs_create (void)
94 {
95   CAMLparam0 ();
96   CAMLlocal1 (gv);
97   guestfs_h *g;
98
99   g = guestfs_create ();
100   if (g == NULL)
101     caml_failwith ("failed to create guestfs handle");
102
103   guestfs_set_error_handler (g, NULL, NULL);
104
105   gv = Val_guestfs (g);
106   CAMLreturn (gv);
107 }
108
109 /* Guestfs.close */
110 CAMLprim value
111 ocaml_guestfs_close (value gv)
112 {
113   CAMLparam1 (gv);
114
115   guestfs_finalize (gv);
116
117   /* So we don't double-free in the finalizer. */
118   Guestfs_val (gv) = NULL;
119
120   CAMLreturn (Val_unit);
121 }
122
123 /* Copy string array value.
124  * The return value is only 'safe' provided we don't allocate anything
125  * further on the OCaml heap (ie. cannot trigger the OCaml GC) because
126  * that could move the strings around.
127  */
128 char **
129 ocaml_guestfs_strings_val (guestfs_h *g, value sv)
130 {
131   CAMLparam1 (sv);
132   char **r;
133   int i;
134
135   r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
136   for (i = 0; i < Wosize_val (sv); ++i)
137     r[i] = String_val (Field (sv, i));
138   r[i] = NULL;
139
140   CAMLreturnT (char **, r);
141 }
142
143 /* Free array of strings. */
144 void
145 ocaml_guestfs_free_strings (char **argv)
146 {
147   /* Don't free the actual strings - they are String_vals on
148    * the OCaml heap.
149    */
150   free (argv);
151 }