[rcr] make File#dup intuitive

S

Simon Strandgaard

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

server> ruby a.rb
5
93
server>

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?
 
D

daz

Simon said:
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

server> ruby a.rb
5
93
server>

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
 
S

Simon Strandgaard

daz said:
Simon said:
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

server> ruby a.rb
5
93
server>

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.

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

nobu.nokada

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) {
 
S

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.

server> ruby a.rb
5
0
server> cat a.rb
f1 = File.open(__FILE__, 'r')
f1.seek(5)
p f1.pos # -> 5
f2 = f1.dup
p f2.pos # -> garbage
server>
 
N

nobu.nokada

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) {
 
S

Simon Strandgaard

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

server> 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) {
server>



I will try your patch right away. Thanks
 
S

Simon Strandgaard

Simon said:
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 :)

server> ./ruby a.rb
5
5
server>
 
S

Simon Strandgaard

Simon said:
f1 = File.open(__FILE__, 'r')
f1.seek(5)
p f1.pos # -> 5
f2 = f1.dup
p f2.pos # -> garbage

server> ruby a.rb
5
93
server>
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) {
Your new patch works like a charm :)

server> ./ruby a.rb
5
5
server>



Will this wonderful patch be committed to cvs ?
 
S

Simon Strandgaard

Simon said:
f1 = File.open(__FILE__, 'r')
f1.seek(5)
p f1.pos # -> 5
f2 = f1.dup
p f2.pos # -> garbage

server> ruby a.rb
5
93
server>

then (e-mail address removed) wrote: [snip patch]


Will this wonderful patch be committed to cvs ?

Please allow Nobu to commit this patch.... soon :)
 
S

Simon Strandgaard

In message "Re: [rcr] make File#dup intuitive"

|> Will this wonderful patch be committed to cvs ?
|
|Please allow Nobu to commit this patch.... soon :)

Go head, Nobu.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top