Project

General

Profile

Feature #11258 » excl_mode_v2.diff

cremno (cremno phobia), 07/29/2015 06:35 PM

View differences:

NEWS
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]
include/ruby/io.h
#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 */
io.c
# 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)
{
......
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+");
}
......
case '+':
fmode |= FMODE_READWRITE;
break;
case 'x':
if (modestr[0] != 'w')
goto error;
fmode |= FMODE_EXCL;
break;
default:
goto error;
case ':':
......
if (oflags & O_CREAT) {
fmode |= FMODE_CREATE;
}
if (oflags & O_EXCL) {
fmode |= FMODE_EXCL;
}
#ifdef O_BINARY
if (oflags & O_BINARY) {
fmode |= FMODE_BINMODE;
......
if (fmode & FMODE_CREATE) {
oflags |= O_CREAT;
}
if (fmode & FMODE_EXCL) {
oflags |= O_EXCL;
}
#ifdef O_BINARY
if (fmode & FMODE_BINMODE) {
oflags |= O_BINARY;
......
#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");
......
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:
......
*
* "t" Text file mode
*
* The exclusive access mode ("x") can be used together with "w" to ensure
* the file is created. <code>Errno::EEXIST</code> 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.
test/ruby/test_io.rb
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
(2-2/2)