From e5b498fad9cc1eaab99714efa7bd8024fa503d52 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 17 Oct 2024 16:28:51 +0900 Subject: [PATCH] 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. --- ext/etc/etc.c | 12 ++++++++++++ test/etc/test_etc.rb | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/ext/etc/etc.c b/ext/etc/etc.c index fcbd1af..6c08088 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -691,6 +691,16 @@ etc_getgrent(VALUE obj) VALUE rb_w32_special_folder(int type); UINT rb_w32_system_tmpdir(WCHAR *path, UINT len); 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 /* call-seq: @@ -710,6 +720,8 @@ etc_sysconfdir(VALUE obj) { #ifdef _WIN32 return rb_w32_special_folder(CSIDL_COMMON_APPDATA); +#elif defined(LOAD_RELATIVE) + return rb_hash_aref(rbconfig(), rb_str_new_lit("sysconfdir")); #else return rb_filesystem_str_new_cstr(SYSCONFDIR); #endif diff --git a/test/etc/test_etc.rb b/test/etc/test_etc.rb index 2eddcf4..c15c4f6 100644 --- a/test/etc/test_etc.rb +++ b/test/etc/test_etc.rb @@ -169,6 +169,10 @@ def test_nprocessors assert_operator(1, :<=, n) end + def test_sysconfdir + assert_operator(File, :absolute_path?, Etc.sysconfdir) + end if File.method_defined?(:absolute_path?) + def test_ractor return unless Etc.passwd # => skip test if no platform support Etc.endpwent