Add __set, __rb_eError

This commit is contained in:
0x1eef 2024-03-07 22:08:48 -03:00
parent 819601ef44
commit ecf0395a2f

View file

@ -1,7 +1,8 @@
#include <libhbsdcontrol.h>
#include <unistd.h>
#include "include/feature.h"
static VALUE get_rb_eError(void);
static VALUE __rb_eError(void);
static VALUE __set(VALUE, VALUE, VALUE);
/**
* BSD::Control::Feature#set!
@ -12,30 +13,35 @@ feature_set(VALUE self, VALUE rb_path, VALUE rb_state)
Check_Type(rb_path, T_STRING);
Check_Type(rb_state, T_FIXNUM);
if (getuid() != 0) {
rb_raise(get_rb_eError(), "This operation requires root privileges.");
rb_raise(__rb_eError(), "This operation requires root privileges.");
} else if (rb_funcall(rb_cFile, rb_intern("exist?"), 1, rb_path) == Qfalse) {
rb_raise(get_rb_eError(), "The given path does not exist.");
rb_raise(__rb_eError(), "The given path does not exist.");
} else {
int r;
VALUE rb_name;
rb_name = rb_funcall(self, rb_intern("name"), 0);
Check_Type(rb_name, T_STRING);
r = hbsdcontrol_set_feature_state(
RSTRING_PTR(rb_path),
RSTRING_PTR(rb_name),
NUM2INT(rb_state)
);
if (r == 0) {
return (Qtrue);
} else {
rb_raise(get_rb_eError(), "hbsdcontrol_set_feature_state failed");
}
VALUE rb_feature = rb_funcall(self, rb_intern("name"), 0);
Check_Type(rb_feature, T_STRING);
return (__set(rb_path, rb_feature, rb_state));
}
}
static VALUE
get_rb_eError(void)
__set(VALUE rb_path, VALUE rb_feature, VALUE rb_state)
{
int result = hbsdcontrol_set_feature_state(
RSTRING_PTR(rb_path),
RSTRING_PTR(rb_feature),
NUM2INT(rb_state)
);
if (result == 0) {
return (Qtrue);
} else {
rb_raise(__rb_eError(), "hbsdcontrol_set_feature_state failed");
}
}
static VALUE
__rb_eError(void)
{
VALUE rb_mBSD = rb_const_get(rb_cObject, rb_intern("BSD")),
rb_mControl = rb_const_get(rb_mBSD, rb_intern("Control")),