This is the mail archive of the ecos-discuss@sourceware.org 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: fseek on JFFS2


On Fri, Sep 22, 2006 at 02:26:30PM +0200, Paluch Sebastian wrote:
> hi,
> here you have some test file 'fseek_test.c', and my output.

I tried running the code on the ramfs. It also fails there as
well. Attached is the code. Please can you check it really does what
you want it to do. It could be that both jffs2 and ramfs is broken, or
it could be the test case is broken.  I've not looked at the test case
myself yet, so i cannot say either way.

       Andrew
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <cyg/fileio/fileio.h>
#include <cyg/io/flash.h>

#include <cyg/infra/testcase.h>
#include <cyg/infra/diag.h>            // HAL polled output
//==========================================================================

#define SHOW_RESULT( _fn, _res ) \
diag_printf("FAIL: " #_fn "() returned %ld %s\n", \
           (unsigned long)_res, _res<0?strerror(errno):"");

//==========================================================================

cyg_uint8 buf[256];
cyg_uint8 buf1[256];

//==========================================================================
// main

int main( int argc, char **argv )
{
    int err;
    FILE *stream;
    long pos;
    unsigned int i;
    cyg_interrupt_disable();
    CYG_TEST_INIT();

    // --------------------------------------------------------------

    CYG_TEST_INFO("mount /");    
    err = mount( "", "/", "ramfs" );

    if( err < 0 ) SHOW_RESULT( mount, err );    
    
    CYG_TEST_INFO("creating /fseek");
    stream = fopen("/fseek","w+");
    if (!stream) {
      diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
      CYG_TEST_FINISH("done");          \
    }


    for (i = 0; i < sizeof(buf); i++) {
      buf[i] = i % 256;
    }
    
    CYG_TEST_INFO("writing test pattern");    
    err=fwrite(buf,sizeof(buf), 1, stream);
    if ( err < 0 ) SHOW_RESULT( fwrite, err );
    

    pos = ftell(stream);

    if (pos < 0) SHOW_RESULT( ftell, pos );
    if (pos != sizeof(buf))
      diag_printf("<FAIL>: ftell is not telling the truth.");
    
    CYG_TEST_INFO("fseek()ing to 85");


    err = fseek(stream, 85, SEEK_SET);
    if ( err < 0 ) SHOW_RESULT( fseek, err );

    pos = ftell(stream);
    
    if (pos < 0) SHOW_RESULT( ftell, pos );
    if (pos != 85) CYG_TEST_FAIL("ftell is not telling the truth");


    char header[3];
    err = fread(header,3,1,stream);
    if ( err < 0 ) SHOW_RESULT( fwrite, err );

    pos = ftell(stream);
    
    if (pos < 0) SHOW_RESULT( ftell, pos );
    if (pos != 88)  CYG_TEST_FAIL("ftell is not telling the truth");

    for (i = 88; i < 161; i++) {
      buf[i] = 0;
    }
    CYG_TEST_INFO("writing");
    err = fwrite(buf+88, 73, 1, stream);
    if ( err < 0 ) SHOW_RESULT( fwrite, err );

    pos = ftell(stream);
    
    if (pos < 0) SHOW_RESULT( ftell, pos );
    if (pos != 161)  CYG_TEST_FAIL("ftell is not telling the truth");

    CYG_TEST_INFO("closing file");

    err = fclose(stream);
    if (err != 0) SHOW_RESULT( fclose, err );

    CYG_TEST_INFO("open file /fseek");
    stream = fopen("/fseek", "r+");
    if (!stream) {
      diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
    }

    err = fseek(stream, 0, SEEK_SET);
    if ( err < 0 ) SHOW_RESULT( fseek, err );

    err = fread(buf1,sizeof(buf1),1, stream);
    if (err != 1) SHOW_RESULT( fread, err );

    CYG_TEST_INFO("Comparing contents");
    if (memcmp(buf, buf1, sizeof(buf1))) {
      CYG_TEST_FAIL("File contents inconsistent");
      
       int t = 0;
       cyg_uint8 c;
   
       err = fseek(stream, 0, SEEK_SET);
       if ( err < 0 ) SHOW_RESULT( fseek, err );
   
       CYG_TEST_INFO("file dump");
       while( !feof(stream) )
       {
         
         c = (cyg_uint8)getc(stream);
         if( !feof(stream) )
         {
            diag_printf("%02x ",c);
            t++;
            if( t == 16 )
            {
               diag_printf("\n");
               t = 0;
            }
         }
       }
       diag_printf("\n");
       t=0;
       CYG_TEST_INFO("buf dump");
       for (i = 0; i < sizeof(buf); i++) {
         diag_printf("%02x ",buf[i]);
         t++;
         if( t == 16 )
         {
            diag_printf("\n");
            t = 0;
         }
       }
       diag_printf("\n");      


    }
    
    CYG_TEST_INFO("closing file");

    err = fclose(stream);
    if (err != 0) SHOW_RESULT( fclose, err );


    CYG_TEST_INFO("umount /");    
    err = umount( "/" );
    if( err < 0 ) SHOW_RESULT( umount, err );    
    cyg_interrupt_enable();
    CYG_TEST_PASS_FINISH("jffs2_2");
}

-- 
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]