lseek() in empty file change stat

Joel Sherrill joel.sherrill at OARcorp.com
Mon Jul 4 03:34:48 UTC 2011



On 07/03/2011 08:56 PM, Xiang Cui wrote:
> Hi,
>
> After calling lseek in an empty file, the st_size in stat should not
> be changed. The test case written by Chris Johns fails except on RFS.
Double check your expectations according to this:

http://pubs.opengroup.org/onlinepubs/7908799/xsh/lseek.html

> The /lseek()/ function will allow the file offset to be set beyond the 
> end of the existing data in the file. If data is later written at this 
> point, subsequent reads of data in the gap will return bytes with the 
> value 0 until data is actually written into the gap.
>

I think that if you create a file and then seek(n bytes), the st_size
should be n.

But this needs to be confirmed and then this very specific test case
needs to be added to the the fstests.  But you knew that much. :)

Once we agree on behaviour, then all file systems without the
expected behaviour need fixing.

This is actually a very good example of why we need the fstests.
This is not a case most people would think of during their first
pass of implementing a file system

> The test code is below.
>
>
> #include<errno.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<sys/stat.h>
> #include<fcntl.h>
> #include<unistd.h>
> #include<stdint.h>
> #include<string.h>
>
> #define LSEEK_POS_1    (64)
>
> void test_lseek()
> {
>    const char* name = "tstlseekftruncate";
>    struct stat sb;
>    int         fd;
>
>    fd = open (name,
>               O_CREAT | O_TRUNC | O_RDWR,
>               S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
>
>    if (fd<  0)
>    {
>      printf ("error: open failed: %s: %s (%d)\n",
>              name, strerror (errno), errno);
>      return ;
>    }
>    if (lseek (fd, LSEEK_POS_1, SEEK_SET)<  0)
>    {
>      printf ("error: first lseek failed: pos: %d: %s (%d)\n",
>              LSEEK_POS_1, strerror (errno), errno);
>      return ;
>    }
>    if (fstat (fd,&sb)<  0)
>    {
>      printf ("error: stat failed: %s (%d)\n", strerror(errno), errno);
>      return ;
>    }
>    if (sb.st_size !=0 )
>    {
>      printf ("error: lseek 1 size invalid: file size not 0; but  is
> %jd \n",(intmax_t) sb.st_size);
>    }
> }
>
> int main (void )
> {
>          test_lseek();
> }
> _______________________________________________
> rtems-users mailing list
> rtems-users at rtems.org
> http://www.rtems.org/mailman/listinfo/rtems-users




More information about the users mailing list