Skip to content

Commit

Permalink
IO#reopen will be able to decide an access mode even if it was not pa…
Browse files Browse the repository at this point in the history
…ssed.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

File.delete("/tmp/foo") if File.exists?("/tmp/foo")
File.delete("/tmp/bar") if File.exists?("/tmp/bar")

f = File.open("/tmp/foo", "w")
f.reopen("/tmp/bar")
f.puts "hello, world"
f.close

File.open("/tmp/bar") {|f|
  assert_equal("hello, world\n", f.gets)
}

puts :ok
}}}
  • Loading branch information
Watson1978 committed May 12, 2011
1 parent b98b9da commit 874eb42
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions io.c
Expand Up @@ -2794,15 +2794,14 @@ rb_io_reopen(VALUE io, SEL sel, int argc, VALUE *argv)
rb_io_t *io_s = ExtractIOStruct(io);

// Reassociate it with the stream opened on the given path
if (NIL_P(mode_string)) {
mode_string = (VALUE)CFSTR("r");
if (!NIL_P(mode_string)) {
io_s->mode = convert_mode_string_to_fmode(mode_string);
}
FilePathValue(path_or_io); // Sanitize the name
const char *filepath = RSTRING_PTR(path_or_io);
const int fd =
open(filepath, convert_mode_string_to_oflags(mode_string), 0644);
prepare_io_from_fd(io_s, fd,
convert_mode_string_to_fmode(mode_string));
open(filepath, convert_fmode_to_oflags(io_s->mode), 0644);
prepare_io_from_fd(io_s, fd, io_s->mode);
GC_WB(&io_s->path, path_or_io);
io_s->buf = NULL;
io_s->buf_offset = 0;
Expand Down

0 comments on commit 874eb42

Please sign in to comment.