Add "x" mode option for IO.open

From CRuby 2.6 feature.

This is the "x" mode (`O_EXCL`; exclusive create) of `fopen(3)` introduced in C11.

ref. https://bugs.ruby-lang.org/issues/11258
This commit is contained in:
dearblue
2023-10-15 16:41:48 +09:00
parent 782fd7e014
commit 88bc8c9144
2 changed files with 30 additions and 0 deletions
+4
View File
@@ -149,6 +149,10 @@ io_modestr_to_flags(mrb_state *mrb, const char *mode)
flags |= O_BINARY;
#endif
break;
case 'x':
if (mode[0] != 'w') goto modeerr;
flags |= O_EXCL;
break;
case '+':
flags = (flags & ~OPEN_ACCESS_MODE_FLAGS) | O_RDWR;
break;
+26
View File
@@ -260,4 +260,30 @@ assert('File.chmod') do
end
end
assert('File.open with "x" mode') do
File.unlink $mrbtest_io_wfname rescue nil
assert_nothing_raised do
File.open($mrbtest_io_wfname, "wx") {}
end
assert_raise(RuntimeError) do
File.open($mrbtest_io_wfname, "wx") {}
end
File.unlink $mrbtest_io_wfname rescue nil
assert_nothing_raised do
File.open($mrbtest_io_wfname, "w+x") {}
end
assert_raise(RuntimeError) do
File.open($mrbtest_io_wfname, "w+x") {}
end
assert_raise(ArgumentError) do
File.open($mrbtest_io_wfname, "rx") {}
end
assert_raise(ArgumentError) do
File.open($mrbtest_io_wfname, "ax") {}
end
end
MRubyIOTestUtil.io_test_cleanup