Use safe_malloc and/or check returns from malloc (Jim Meyering).
[libguestfs.git] / ruby / ext / guestfs / _guestfs.c
index d4b62ca..e6c4236 100644 (file)
@@ -1413,7 +1413,7 @@ static VALUE ruby_guestfs_vgcreate (VALUE gv, VALUE volgroupv, VALUE physvolsv)
   char **physvols;  {
     int i, len;
     len = RARRAY_LEN (physvolsv);
-    physvols = malloc (sizeof (char *) * (len+1));
+    physvols = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
     for (i = 0; i < len; ++i) {
       VALUE v = rb_ary_entry (physvolsv, i);
       physvols[i] = StringValueCStr (v);
@@ -1499,7 +1499,7 @@ static VALUE ruby_guestfs_sfdisk (VALUE gv, VALUE devicev, VALUE cylsv, VALUE he
   char **lines;  {
     int i, len;
     len = RARRAY_LEN (linesv);
-    lines = malloc (sizeof (char *) * (len+1));
+    lines = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
     for (i = 0; i < len; ++i) {
       VALUE v = rb_ary_entry (linesv, i);
       lines[i] = StringValueCStr (v);
@@ -1656,7 +1656,7 @@ static VALUE ruby_guestfs_command (VALUE gv, VALUE argumentsv)
   char **arguments;  {
     int i, len;
     len = RARRAY_LEN (argumentsv);
-    arguments = malloc (sizeof (char *) * (len+1));
+    arguments = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
     for (i = 0; i < len; ++i) {
       VALUE v = rb_ary_entry (argumentsv, i);
       arguments[i] = StringValueCStr (v);
@@ -1686,7 +1686,7 @@ static VALUE ruby_guestfs_command_lines (VALUE gv, VALUE argumentsv)
   char **arguments;  {
     int i, len;
     len = RARRAY_LEN (argumentsv);
-    arguments = malloc (sizeof (char *) * (len+1));
+    arguments = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
     for (i = 0; i < len; ++i) {
       VALUE v = rb_ary_entry (argumentsv, i);
       arguments[i] = StringValueCStr (v);
@@ -2336,7 +2336,7 @@ static VALUE ruby_guestfs_debug (VALUE gv, VALUE subcmdv, VALUE extraargsv)
   char **extraargs;  {
     int i, len;
     len = RARRAY_LEN (extraargsv);
-    extraargs = malloc (sizeof (char *) * (len+1));
+    extraargs = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
     for (i = 0; i < len; ++i) {
       VALUE v = rb_ary_entry (extraargsv, i);
       extraargs[i] = StringValueCStr (v);
@@ -2356,6 +2356,165 @@ static VALUE ruby_guestfs_debug (VALUE gv, VALUE subcmdv, VALUE extraargsv)
   return rv;
 }
 
+static VALUE ruby_guestfs_lvremove (VALUE gv, VALUE devicev)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "lvremove");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "lvremove");
+
+  int r;
+
+  r = guestfs_lvremove (g, device);
+  if (r == -1)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  return Qnil;
+}
+
+static VALUE ruby_guestfs_vgremove (VALUE gv, VALUE vgnamev)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "vgremove");
+
+  const char *vgname = StringValueCStr (vgnamev);
+  if (!vgname)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "vgname", "vgremove");
+
+  int r;
+
+  r = guestfs_vgremove (g, vgname);
+  if (r == -1)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  return Qnil;
+}
+
+static VALUE ruby_guestfs_pvremove (VALUE gv, VALUE devicev)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "pvremove");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "pvremove");
+
+  int r;
+
+  r = guestfs_pvremove (g, device);
+  if (r == -1)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  return Qnil;
+}
+
+static VALUE ruby_guestfs_set_e2label (VALUE gv, VALUE devicev, VALUE labelv)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "set_e2label");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "set_e2label");
+  const char *label = StringValueCStr (labelv);
+  if (!label)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "label", "set_e2label");
+
+  int r;
+
+  r = guestfs_set_e2label (g, device, label);
+  if (r == -1)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  return Qnil;
+}
+
+static VALUE ruby_guestfs_get_e2label (VALUE gv, VALUE devicev)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "get_e2label");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "get_e2label");
+
+  char *r;
+
+  r = guestfs_get_e2label (g, device);
+  if (r == NULL)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  VALUE rv = rb_str_new2 (r);
+  free (r);
+  return rv;
+}
+
+static VALUE ruby_guestfs_set_e2uuid (VALUE gv, VALUE devicev, VALUE uuidv)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "set_e2uuid");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "set_e2uuid");
+  const char *uuid = StringValueCStr (uuidv);
+  if (!uuid)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "uuid", "set_e2uuid");
+
+  int r;
+
+  r = guestfs_set_e2uuid (g, device, uuid);
+  if (r == -1)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  return Qnil;
+}
+
+static VALUE ruby_guestfs_get_e2uuid (VALUE gv, VALUE devicev)
+{
+  guestfs_h *g;
+  Data_Get_Struct (gv, guestfs_h, g);
+  if (!g)
+    rb_raise (rb_eArgError, "%s: used handle after closing it", "get_e2uuid");
+
+  const char *device = StringValueCStr (devicev);
+  if (!device)
+    rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+              "device", "get_e2uuid");
+
+  char *r;
+
+  r = guestfs_get_e2uuid (g, device);
+  if (r == NULL)
+    rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+  VALUE rv = rb_str_new2 (r);
+  free (r);
+  return rv;
+}
+
 /* Initialize the module. */
 void Init__guestfs ()
 {
@@ -2560,4 +2719,18 @@ void Init__guestfs ()
         ruby_guestfs_mount_vfs, 4);
   rb_define_method (c_guestfs, "debug",
         ruby_guestfs_debug, 2);
+  rb_define_method (c_guestfs, "lvremove",
+        ruby_guestfs_lvremove, 1);
+  rb_define_method (c_guestfs, "vgremove",
+        ruby_guestfs_vgremove, 1);
+  rb_define_method (c_guestfs, "pvremove",
+        ruby_guestfs_pvremove, 1);
+  rb_define_method (c_guestfs, "set_e2label",
+        ruby_guestfs_set_e2label, 2);
+  rb_define_method (c_guestfs, "get_e2label",
+        ruby_guestfs_get_e2label, 1);
+  rb_define_method (c_guestfs, "set_e2uuid",
+        ruby_guestfs_set_e2uuid, 2);
+  rb_define_method (c_guestfs, "get_e2uuid",
+        ruby_guestfs_get_e2uuid, 1);
 }