[rcr] make File#dup intuitive

position is not copied… try this example for yourself

f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

ruby a.rb
5
93

This is really non-intuitive that you have to do
a seek afterwards. Its the same as if you clone a string,
but have to copy the string-content yourself.
Where is logic here?

···


Simon Strandgaard

Simon Strandgaard wrote:

position is not copied… try this example for yourself

f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

ruby a.rb
5
93

This is really non-intuitive that you have to do
a seek afterwards. Its the same as if you clone a string,
but have to copy the string-content yourself.
Where is logic here?

Works here.

ruby 1.9.0 (2004-04-27) [i586-bccwin32]

ruby 1.8.0 (2003-05-15) [i386-mswin32]

ruby 1.6.7 (2002-03-01) [i586-mswin32]

mainframe_19(north_garage)> ruby pos.rb
5
5
mainframe_19(north_garage)>

daz

Hi,

At Fri, 21 May 2004 01:22:49 +0900,
Simon Strandgaard wrote in [ruby-talk:100910]:

position is not copied… try this example for yourself

I’ve expected it had been fixed.

This patch works?

Index: io.c

···

===================================================================
RCS file: /cvs/ruby/src/ruby/io.c,v
retrieving revision 1.274
diff -U2 -p -d -r1.274 io.c
— io.c 8 May 2004 08:11:33 -0000 1.274
+++ io.c 21 May 2004 06:17:56 -0000
@@ -3295,4 +3295,5 @@ rb_io_init_copy(dest, io)
int fd;
char *mode;

  • off_t pos1, pos2;

    io = rb_io_get_io(io);
    @@ -3303,12 +3304,10 @@ rb_io_init_copy(dest, io)
    if (orig->f2) {
    io_fflush(orig->f2, orig);

  • fseeko(orig->f, 0L, SEEK_CUR);
  • pos2 = fseeko(orig->f2, 0L, SEEK_CUR);
    }
    else if (orig->mode & FMODE_WRITABLE) {
    io_fflush(orig->f, orig);
    }
  • else {
  • fseeko(orig->f, 0L, SEEK_CUR);
  • }
  • pos1 = fseeko(orig->f, 0L, SEEK_CUR);

    /* copy OpenFile structure */
    @@ -3332,4 +3331,5 @@ rb_io_init_copy(dest, io)
    fd = ruby_dup(fileno(orig->f));
    fptr->f = rb_fdopen(fd, mode);

  • fseeko(fptr->f, pos1, SEEK_SET);
    if (orig->f2) {
    if (fileno(orig->f) != fileno(orig->f2)) {
    @@ -3337,4 +3337,5 @@ rb_io_init_copy(dest, io)
    }
    fptr->f2 = rb_fdopen(fd, “w”);

  • fseeko(fptr->f2, pos2, SEEK_SET);
    }
    if (fptr->mode & FMODE_BINMODE) {


Nobu Nakada

daz wrote:

Simon Strandgaard wrote:

position is not copied… try this example for yourself

f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

ruby a.rb
5
93

This is really non-intuitive that you have to do
a seek afterwards. Its the same as if you clone a string,
but have to copy the string-content yourself.
Where is logic here?

Works here.

ruby 1.9.0 (2004-04-27) [i586-bccwin32]

ruby 1.8.0 (2003-05-15) [i386-mswin32]

ruby 1.6.7 (2002-03-01) [i586-mswin32]

mainframe_19(north_garage)> ruby pos.rb
5
5
mainframe_19(north_garage)>

Hmm… this begins to look like a freebsd specific problem.

ruby -v a.rb
ruby 1.9.0 (2004-05-17) [i386-freebsd5.1]
5
93
ruby18 -v a.rb
ruby 1.8.1 (2003-12-22) [i386-freebsd5.1]
5
93

···


Simon Strandgaard

At Fri, 21 May 2004 01:22:49 +0900,
Simon Strandgaard wrote in [ruby-talk:100910]:

position is not copied… try this example for yourself

I’ve expected it had been fixed.

This patch works?

I just applied your patch… it seems as the position now
gets zeroed.

ruby a.rb
5
0
cat a.rb
f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

···

nobu.nokada@softhome.net wrote:


Simon Strandgaard

Hi,

At Fri, 21 May 2004 15:38:00 +0900,
Simon Strandgaard wrote in [ruby-talk:100979]:

I just applied your patch… it seems as the position now
gets zeroed.

Sorry, too stupid.

Index: io.c

···

===================================================================
RCS file: /cvs/ruby/src/ruby/io.c,v
retrieving revision 1.274
diff -u -2 -p -r1.274 io.c
— io.c 8 May 2004 08:11:33 -0000 1.274
+++ io.c 21 May 2004 06:55:56 -0000
@@ -3332,4 +3332,5 @@ rb_io_init_copy(dest, io)
fd = ruby_dup(fileno(orig->f));
fptr->f = rb_fdopen(fd, mode);

  • fseeko(fptr->f, ftello(orig->f), SEEK_SET);
    if (orig->f2) {
    if (fileno(orig->f) != fileno(orig->f2)) {
    @@ -3337,4 +3338,5 @@ rb_io_init_copy(dest, io)
    }
    fptr->f2 = rb_fdopen(fd, “w”);
  • fseeko(fptr->f2, ftello(orig->f2), SEEK_SET);
    }
    if (fptr->mode & FMODE_BINMODE) {


Nobu Nakada

At Fri, 21 May 2004 15:38:00 +0900,
Simon Strandgaard wrote in [ruby-talk:100979]:

I just applied your patch… it seems as the position now
gets zeroed.

Sorry, too stupid.

I have sligthly adjusted your original patch, so it now works

diff -U2 bak/io.c io.c
— bak/io.c Fri May 21 08:51:22 2004
+++ io.c Fri May 21 08:46:00 2004
@@ -3295,4 +3295,5 @@
int fd;
char *mode;

  • off_t pos1, pos2;

    io = rb_io_get_io(io);
    @@ -3303,12 +3304,11 @@
    if (orig->f2) {
    io_fflush(orig->f2, orig);

  •   fseeko(orig->f, 0L, SEEK_CUR);
    
  •   pos2 = fseeko(orig->f2, 0L, SEEK_CUR);
    
    }
    else if (orig->mode & FMODE_WRITABLE) {
    io_fflush(orig->f, orig);
    }
  • else {
  •   fseeko(orig->f, 0L, SEEK_CUR);
    
  • }
  • pos1 = io_tell(orig);

  • fseeko(orig->f, 0L, SEEK_CUR);

    /* copy OpenFile structure */
    @@ -3332,4 +3332,5 @@
    fd = ruby_dup(fileno(orig->f));
    fptr->f = rb_fdopen(fd, mode);

  • fseeko(fptr->f, pos1, SEEK_SET);
    if (orig->f2) {
    if (fileno(orig->f) != fileno(orig->f2)) {
    @@ -3337,4 +3338,5 @@
    }
    fptr->f2 = rb_fdopen(fd, “w”);

  •   fseeko(fptr->f2, pos2, SEEK_SET);
    

    }
    if (fptr->mode & FMODE_BINMODE) {

I will try your patch right away. Thanks

···

nobu.nokada@softhome.net wrote:


Simon Strandgaard

Simon Strandgaard wrote:

f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

ruby a.rb
5
93

Index: io.c

RCS file: /cvs/ruby/src/ruby/io.c,v
retrieving revision 1.274
diff -u -2 -p -r1.274 io.c
— io.c 8 May 2004 08:11:33 -0000 1.274
+++ io.c 21 May 2004 06:55:56 -0000
@@ -3332,4 +3332,5 @@ rb_io_init_copy(dest, io)
fd = ruby_dup(fileno(orig->f));
fptr->f = rb_fdopen(fd, mode);

  • fseeko(fptr->f, ftello(orig->f), SEEK_SET);
    if (orig->f2) {
    if (fileno(orig->f) != fileno(orig->f2)) {
    @@ -3337,4 +3338,5 @@ rb_io_init_copy(dest, io)
    }
    fptr->f2 = rb_fdopen(fd, “w”);
  • fseeko(fptr->f2, ftello(orig->f2), SEEK_SET);
    }
    if (fptr->mode & FMODE_BINMODE) {

then Simon Strandgaard wrote:

Your new patch works like a charm :slight_smile:

./ruby a.rb
5
5

Will this wonderful patch be committed to cvs ?

···

then nobu.nokada@softhome.net wrote:


Simon Strandgaard

Simon Strandgaard wrote:

At Fri, 21 May 2004 15:38:00 +0900,
Simon Strandgaard wrote in [ruby-talk:100979]:

I just applied your patch… it seems as the position now
gets zeroed.

Sorry, too stupid.

I will try your patch right away. Thanks

Your new patch works like a charm :slight_smile:

./ruby a.rb
5
5

···

nobu.nokada@softhome.net wrote:


Simon Strandgaard

Simon Strandgaard wrote:

f1 = File.open(FILE, ‘r’)
f1.seek(5)
p f1.pos # → 5
f2 = f1.dup
p f2.pos # → garbage

ruby a.rb
5
93

[snip patch]

Will this wonderful patch be committed to cvs ?

Please allow Nobu to commit this patch… soon :slight_smile:

···

then nobu.nokada@softhome.net wrote:


Simon Strandgaard

Hi,

···

In message “Re: [rcr] make File#dup intuitive” on 04/05/26, Simon Strandgaard neoneye@adslhome.dk writes:

Will this wonderful patch be committed to cvs ?

Please allow Nobu to commit this patch… soon :slight_smile:

Go head, Nobu.

						matz.

matz@ruby-lang.org (Yukihiro Matsumoto) wrote:

···

In message “Re: [rcr] make File#dup intuitive” > on 04/05/26, Simon Strandgaard neoneye@adslhome.dk writes:

Will this wonderful patch be committed to cvs ?

Please allow Nobu to commit this patch… soon :slight_smile:

Go head, Nobu.

Excelent! a pleasant birthday present… Thanks Nobu+Matz.


Simon Strandgaard