lseek() in empty file change stat

Xiang Cui medivhc at gmail.com
Mon Jul 4 01:56:47 UTC 2011


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.
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();
}



More information about the users mailing list