[ruby/etc] Fix sysconfdir when load-relative

When relative load paths option is enabled, the prefix is determined
at the runtime.  The only way to get it outside libruby is to use
rbconfig.rb.

https://github.com/ruby/etc/commit/e5b498fad9
This commit is contained in:
Nobuyoshi Nakada 2024-10-17 16:28:51 +09:00 committed by git
parent d4a18d4a78
commit 9da60a14bf
2 changed files with 16 additions and 0 deletions

View File

@ -691,6 +691,16 @@ etc_getgrent(VALUE obj)
VALUE rb_w32_special_folder(int type); VALUE rb_w32_special_folder(int type);
UINT rb_w32_system_tmpdir(WCHAR *path, UINT len); UINT rb_w32_system_tmpdir(WCHAR *path, UINT len);
VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc); VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
#elif defined(LOAD_RELATIVE)
static inline VALUE
rbconfig(void)
{
VALUE config;
rb_require("rbconfig");
config = rb_const_get(rb_path2class("RbConfig"), rb_intern("CONFIG"));
Check_Type(config, T_HASH);
return config;
}
#endif #endif
/* call-seq: /* call-seq:
@ -710,6 +720,8 @@ etc_sysconfdir(VALUE obj)
{ {
#ifdef _WIN32 #ifdef _WIN32
return rb_w32_special_folder(CSIDL_COMMON_APPDATA); return rb_w32_special_folder(CSIDL_COMMON_APPDATA);
#elif defined(LOAD_RELATIVE)
return rb_hash_aref(rbconfig(), rb_str_new_lit("sysconfdir"));
#else #else
return rb_filesystem_str_new_cstr(SYSCONFDIR); return rb_filesystem_str_new_cstr(SYSCONFDIR);
#endif #endif

View File

@ -169,6 +169,10 @@ class TestEtc < Test::Unit::TestCase
assert_operator(1, :<=, n) assert_operator(1, :<=, n)
end end
def test_sysconfdir
assert_operator(File, :absolute_path?, Etc.sysconfdir)
end if File.method_defined?(:absolute_path?)
def test_ractor def test_ractor
return unless Etc.passwd # => skip test if no platform support return unless Etc.passwd # => skip test if no platform support
Etc.endpwent Etc.endpwent