mixing IO#read and IO#syswrite 
Author Message
 mixing IO#read and IO#syswrite

The docs clearly warn against mixing buffered and unbuffered reads, or
mixing buffered and unbuffered writes. But is it ok to open a file, read
its contents, truncate it, and syswrite new contents? It seems to work
fine, but ....

The reason I'm asking is that I want a file with marshalled data to be
consistent (as in the ACID properties) regardless of failures. I'm
guessing a dump to a string followed by a syswrite is the best way to do
this, because the single OS call won't be interrupted by ruby, and so if
the file system has the consistency property (I guess a journaled fs
would suffice), the marshalled data will be consistent.

But I want to avoid allocating strings, if possible. So I use

   Marshal.load(file)

to get the objects without allocating an extra string, then

   file.rewind
   file.truncate(0)
   file.syswrite(Marshal.dump(obj))

to write out the new contents.

I _could_ just use an intermediate string to load the data:

   Marshal.load(file.sysread(...))

But I'd like to avoid the extra allocation, if possible.

Is my approach safe?



Thu, 18 Aug 2005 05:51:38 GMT  
 mixing IO#read and IO#syswrite
Hi,

At Sun, 2 Mar 2003 06:51:38 +0900,

Quote:

>    file.rewind
>    file.truncate(0)
>    file.syswrite(Marshal.dump(obj))

It's safe because IO#rewind and underlying IO#seek implies
IO#flush.

--
Nobu Nakada



Thu, 18 Aug 2005 11:05:59 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Perl IO to Ruby IO help needed

2. IMPORT IO, FROM IO IMPORT ?

3. IMPORT IO, FROM IO IMPORT ?

4. IMPORT IO, FROM IO IMPORT ?

5. BUG?: HP-UX 10.20 and IO#gets followed by IO#puts

6. BUG? IO.seek conflicts with IO.sysread

7. VAX Ada sequential IO and Direct IO - (nf)

8. VAX Ada sequential IO and Direct IO

9. VAX Ada sequential IO and Direct IO

10. Not DOS interrupts, I want direct IO (memory mapped IO)

11. Mixing IO and mutable state (ST) in Haskell/Hugs

12. Mixing F90 with F77: IO problem

 

 
Powered by phpBB® Forum Software