Add BSD::Control::Feature#sysdef!

This commit is contained in:
0x1eef 2024-03-20 18:34:15 -03:00
parent c064af26a6
commit 427165d1b7
8 changed files with 76 additions and 11 deletions

View file

@ -18,5 +18,6 @@ Init_bsdcontrol(void)
bsdcontrol_context_available_features, bsdcontrol_context_available_features,
0); 0);
rb_define_method(rb_cFeature, "status", bsdcontrol_feature_status, 1); rb_define_method(rb_cFeature, "status", bsdcontrol_feature_status, 1);
rb_define_method(rb_cFeature, "sysdef!", bsdcontrol_feature_sysdef, 1);
rb_define_private_method(rb_cFeature, "set!", bsdcontrol_feature_set, 2); rb_define_private_method(rb_cFeature, "set!", bsdcontrol_feature_set, 2);
} }

View file

@ -66,3 +66,31 @@ bsdcontrol_feature_set(VALUE self, VALUE path, VALUE rbstate)
return Qtrue; return Qtrue;
} }
} }
/*
* BSD::Control::Feature#sysdef!
*/
VALUE
bsdcontrol_feature_sysdef(VALUE self, VALUE path)
{
int fd;
VALUE rbcontext;
hbsdctrl_feature_t *feature;
hbsdctrl_ctx_t *ctx;
rbcontext = rb_funcall(self, rb_intern("context"), 0);
fd = bsdcontrol_open(path);
ctx = bsdcontrol_unwrap(rbcontext);
feature = bsdcontrol_find_feature(ctx, self);
errno = 0;
if (feature->hf_unapply(ctx, feature, &fd, NULL) == RES_FAIL)
{
close(fd);
errno == 0 ? rb_raise(rb_eSystemCallError, "hf_unapply")
: rb_syserr_fail(errno, "hf_unapply");
}
else
{
close(fd);
return Qtrue;
}
}

View file

@ -1,3 +1,4 @@
#include <ruby.h> #include <ruby.h>
VALUE bsdcontrol_feature_status(VALUE, VALUE); VALUE bsdcontrol_feature_status(VALUE, VALUE);
VALUE bsdcontrol_feature_set(VALUE,VALUE,VALUE); VALUE bsdcontrol_feature_set(VALUE,VALUE,VALUE);
VALUE bsdcontrol_feature_sysdef(VALUE, VALUE);

View file

@ -41,19 +41,17 @@ module BSD::Control
end end
## ##
# Restore system defaults for a given file. # @!method sysdef!(path)
# Restore system defaults for a given file.
# #
# @param [String] path # @param [String] path
# The path to a file. # The path to a file.
# #
# @raise [SystemCallError] # @raise [SystemCallError]
# Might raise a number of Errno exceptions. # Might raise a number of Errno exceptions.
# #
# @return [Boolean] # @return [Boolean]
# Returns true on success. # Returns true on success.
def sysdef!(path)
# FIXME: implement.
end
# @endgroup # @endgroup

View file

@ -1,2 +1,2 @@
require "test/unit" require "test/unit"
require "hbsdctl" require "bsdcontrol"

View file

@ -0,0 +1,37 @@
require_relative "../setup"
module BSD::Control
class SysDefFeatureTest < Test::Unit::TestCase
require 'fileutils'
include FileUtils
def test_sysdef_pageexec
touch(file)
assert BSD::Control.feature(:pageexec).enable!(file),
"The enable! method should have returned true"
assert_equal(
BSD::Control.feature(:pageexec).status(file),
:enabled
)
assert BSD::Control.feature(:pageexec).sysdef!(file),
"The sysdef! method should have returned true"
assert_equal(
BSD::Control.feature(:pageexec).status(file),
:sysdef
)
ensure
rm(file)
end
def test_enable_pageexec_nonexistent_file
assert_raises(Errno::ENOENT) do
BSD::Control.feature(:pageexec).sysdef!(file)
end
end
private
def file
File.join(__dir__, "file")
end
end
end