This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: JFFS2 fseek


On Thu, Mar 25, 2004 at 02:25:25PM +0000, eibach@gdsys.de wrote:
> Hello,
> 
> I have a problem with the following code:
> 
> fp = fopen("/jffs2/sys/testfile","w");
> fseek(fp, 512, SEEK_SET);
> fputs("End\n", fp);
> stat = fseek(fp, 0, SEEK_SET);
> fputs("Begin\n", fp);
> fclose(fp);
> 
> It should produce testfile:
> Begin
> <free space>
> End
> 
> Instead it produces:
> End
> Begin
> <free space>
> 
> I had some debugging and found that fseek is executed immedeatly, but fputs is executed when the file is closed.
> 
> The following code is a workaround:
> 
> fp = fopen("/jffs2/sys/testfile","w");
> fseek(fp, 512, SEEK_SET);
> fputs("End\n", fp);
> fflush(fp);
> stat = fseek(fp, 0, SEEK_SET);
> fputs("Begin\n", fp);
> fflush(fp);
> fclose(fp);
> 
> So something seems to be terribly wrong with fseek. Any suggestions where to start searching?
> 

packages/language/c/libc/stdio/current/include/stream.inl 
method Cyg_StdioStream::set_position( fpos_t pos, int whence )

There looks to be a logic bug...

    Cyg_ErrNo err;
    off_t newpos=pos;
 
    err = cyg_stdio_lseek( my_device, &newpos, whence );
 
    if( err == ENOERR )
    {
        // Clean out the buffer. Flush output if any present,
        // and clear any input out of input buffer and any ungot
        // chars from unread buffer.
                                                                                                             
        err = flush_output_unlocked();
        io_buf.drain_buffer();

It appears to be working out where the new position is and then
seeking the file descriptor to the new position. It then flushed the
stream write buffer and resets the input buffer. 

To me, this seems to be in the wrong order. It should flush the
buffers before moving the file pointer. Attached is a totally untested
patch. It might work. Please give it a try and let me know.

       Andrew

Attachment: fseek.diff
Description: Text document

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]