mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user