[Openmcl-devel] File-length bug?

Pascal J. Bourguignon pjb at informatimago.com
Sat Aug 3 15:23:25 PDT 2013

Ron Garret <ron at flownet.com> writes:

> ? (setf path #P"~/Desktop/foo")
> #P"/Users/ron/Desktop/foo"
> ? (setf out (open path :direction :output :if-does-not-exist :create))
> #<BASIC-FILE-CHARACTER-OUTPUT-STREAM ("/Users/ron/Desktop/foo"/20 UTF-8) #x3020025DEA2D>
> ? (setf in (open path :direction :input))
> #<BASIC-FILE-CHARACTER-INPUT-STREAM ("/Users/ron/Desktop/foo"/21 UTF-8) #x3020025DD7DD>
> ? (file-length in)
> 0                              ; As expected
> ? (listen in)
> T                              ; Not as expected, but OK, using listen on file streams is a little hinky...
> ? (print 123 out)
> 123
> ? (force-output out)
> ? (file-length out)
> 5                               ; As expected
> ? (file-length in)
> 0                               ; Not as expected
> ? (file-position in 3)
> 3
> ? (file-position in)
> 3
> ? (file-length in)
> 0
> Is this a bug?
> FYI, what I'm actually trying to do is to use a file as a FIFO, with
> one process writing the file and another reading it (which is why I
> can't use an :io stream).  Ideally what I'd like to do is have the
> reader block when trying to read past the end of the file (like "tail
> -f"), but a reliable way of polling to see if additional data has been
> written would be enough.  It's a binary file, so I can't use
> peek-char.  (I could use flexi-streams:peek-byte but I was hoping to
> avoid adding a library dependency.)

I don't think you can do that conformingly.  Ie. ccl is not to be blamed
for behaving like it does here.  (But this doesn't prevent it or any other
implementation on POSIX system to get closer to the POSIX semantics).

Note that the default if-exist is :new-version, and you used
:if-does-not-exist :create, therefore in both cases, you are writing a
new file, and since it's possible to call (close stream :abort t) later,
to undo the creation or new version, or  (close stream :abort nil) to
commit the file system "transaction", an implementation even on POSIX
system could (I'd dare say even should) set things up in such a way that
before closing the created file or new version, opening it for reading
should access the previous version (or signal a file does not exist
error if it is to be created when (close :abort nil) is called).

But again, it may be a valid design choice of a CL implementation, to
try to be closer to the underlying POSIX system, as much as is permited
by the CL standard (and it does permit a lot).

__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.  
You know you've been lisping too long when you see a recent picture of George 
Lucas and think "Wait, I thought John McCarthy was dead!" -- Dalek_Baldwin

More information about the Openmcl-devel mailing list