diff --git a/NEWS b/NEWS index 3295c94..dc166fb 100644 --- a/NEWS +++ b/NEWS @@ -50,6 +50,8 @@ with all sufficient information, see the ChangeLog file. this parameter is bitwise-ORed to oflags generated by normal mode argument. [Feature #11253] + * new mode character 'x' to open files for exclusive access [Feature #11258] + * Thread * Thread#name, Thread#name= are added to handle thread names [Feature #11251] diff --git a/include/ruby/io.h b/include/ruby/io.h index 029522b..fceae11 100644 --- a/include/ruby/io.h +++ b/include/ruby/io.h @@ -109,6 +109,7 @@ typedef struct rb_io_t { #define FMODE_APPEND 0x00000040 #define FMODE_CREATE 0x00000080 /* #define FMODE_NOREVLOOKUP 0x00000100 */ +#define FMODE_EXCL 0x00000400 #define FMODE_TRUNC 0x00000800 #define FMODE_TEXTMODE 0x00001000 /* #define FMODE_PREP 0x00010000 */ diff --git a/io.c b/io.c index 104f521..e421e2b 100644 --- a/io.c +++ b/io.c @@ -1360,6 +1360,11 @@ io_binwrite(VALUE str, const char *ptr, long len, rb_io_t *fptr, int nosync) # define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \ (fmode & FMODE_TEXTMODE) ? (c) : (a)) + +#define MODE_BTXMODE(a, b, c, d ,e, f) ((fmode & FMODE_EXCL) ? \ + MODE_BTMODE(d, e, f) : \ + MODE_BTMODE(a, b, c)) + static VALUE do_writeconv(VALUE str, rb_io_t *fptr, int *converted) { @@ -4872,10 +4877,10 @@ rb_io_fmode_modestr(int fmode) case FMODE_READABLE: return MODE_BTMODE("r", "rb", "rt"); case FMODE_WRITABLE: - return MODE_BTMODE("w", "wb", "wt"); + return MODE_BTXMODE("w", "wb", "wt", "wx", "wbx", "wtx"); case FMODE_READWRITE: if (fmode & FMODE_CREATE) { - return MODE_BTMODE("w+", "wb+", "wt+"); + return MODE_BTXMODE("w+", "wb+", "wt+", "w+x", "wb+x", "wt+x"); } return MODE_BTMODE("r+", "rb+", "rt+"); } @@ -4921,6 +4926,11 @@ rb_io_modestr_fmode(const char *modestr) case '+': fmode |= FMODE_READWRITE; break; + case 'x': + if (modestr[0] != 'w') + goto error; + fmode |= FMODE_EXCL; + break; default: goto error; case ':': @@ -4964,6 +4974,9 @@ rb_io_oflags_fmode(int oflags) if (oflags & O_CREAT) { fmode |= FMODE_CREATE; } + if (oflags & O_EXCL) { + fmode |= FMODE_EXCL; + } #ifdef O_BINARY if (oflags & O_BINARY) { fmode |= FMODE_BINMODE; @@ -4999,6 +5012,9 @@ rb_io_fmode_oflags(int fmode) if (fmode & FMODE_CREATE) { oflags |= O_CREAT; } + if (fmode & FMODE_EXCL) { + oflags |= O_EXCL; + } #ifdef O_BINARY if (fmode & FMODE_BINMODE) { oflags |= O_BINARY; @@ -5022,7 +5038,11 @@ rb_io_oflags_modestr(int oflags) #else # define MODE_BINARY(a,b) (a) #endif - int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR); + int accmode; + if (oflags & O_EXCL) { + rb_raise(rb_eArgError, "exclusive access mode is not supported"); + } + accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR); if (oflags & O_APPEND) { if (accmode == O_WRONLY) { return MODE_BINARY("a", "ab"); @@ -5031,7 +5051,7 @@ rb_io_oflags_modestr(int oflags) return MODE_BINARY("a+", "ab+"); } } - switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) { + switch (accmode) { default: rb_raise(rb_eArgError, "invalid access oflags 0x%x", oflags); case O_RDONLY: @@ -7496,6 +7516,10 @@ rb_io_make_open_file(VALUE obj) * * "t" Text file mode * + * The exclusive access mode ("x") can be used together with "w" to ensure + * the file is created. Errno::EEXIST is raised when it already + * exists. It may not be supported with all kinds of streams (e.g. pipes). + * * When the open mode of original IO is read only, the mode cannot be * changed to be writable. Similarly, the open mode cannot be changed from * write only to readable. diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 2983826..d5d7525 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -3251,4 +3251,12 @@ def test_open_flag_binary end end end if File::BINARY != 0 + + def test_exclusive_mode + make_tempfile do |t| + assert_raise(Errno::EEXIST){ open(t.path, 'wx'){} } + assert_raise(ArgumentError){ open(t.path, 'rx'){} } + assert_raise(ArgumentError){ open(t.path, 'ax'){} } + end + end end