Blame sysdeps/unix/sysv/linux/tst-fallocate-common.c

Packit 6c4009
/* Basic fallocate test (no specific flags is checked).
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
#define XSTR(s) STR(S)
Packit 6c4009
#define STR(s)  #s
Packit 6c4009
Packit 6c4009
static char *temp_filename;
Packit 6c4009
static int temp_fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  temp_fd = create_temp_file ("tst-fallocate.", &temp_filename);
Packit 6c4009
  if (temp_fd == -1)
Packit 6c4009
    FAIL_EXIT1 ("cannot create temporary file: %m");
Packit 6c4009
}
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test_with_offset (off_t offset)
Packit 6c4009
{
Packit 6c4009
  int ret;
Packit 6c4009
  struct stat finfo;
Packit 6c4009
#define BLK_SIZE 1024
Packit 6c4009
  char bwrite[BLK_SIZE] = { 0xf0 };
Packit 6c4009
  char bread[BLK_SIZE];
Packit 6c4009
Packit 6c4009
  /* It tries to fallocate 1024 bytes from 'offset' and then write 1024 bytes.
Packit 6c4009
     After both operation rewind the file descriptor and read 1024 bytes
Packit 6c4009
     and check if both buffer have the same contents.  */
Packit 6c4009
  ret = fallocate (temp_fd, 0, offset, BLK_SIZE);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    {
Packit 6c4009
      /* fallocate might not be fully supported by underlying filesystem (for
Packit 6c4009
	 instance some NFS versions).   */
Packit 6c4009
      if (errno == EOPNOTSUPP)
Packit 6c4009
	FAIL_EXIT (77, "fallocate not supported");
Packit 6c4009
      FAIL_EXIT1 ("fallocate failed");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ret = fstat (temp_fd, &finfo);
Packit 6c4009
  if (ret == -1)
Packit 6c4009
    FAIL_EXIT1 ("fstat failed");
Packit 6c4009
Packit 6c4009
  if (finfo.st_size < (offset + BLK_SIZE))
Packit 6c4009
    FAIL_EXIT1 ("size of first fallocate less than expected (%llu)",
Packit 6c4009
		(long long unsigned int)offset + BLK_SIZE);
Packit 6c4009
Packit 6c4009
  if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
Packit 6c4009
    FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
Packit 6c4009
Packit 6c4009
  if (write (temp_fd, bwrite, BLK_SIZE) != BLK_SIZE)
Packit 6c4009
    FAIL_EXIT1 ("fail trying to write " XSTR (BLK_SIZE) " bytes");
Packit 6c4009
Packit 6c4009
  if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
Packit 6c4009
    FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
Packit 6c4009
Packit 6c4009
  if (read (temp_fd, bread, BLK_SIZE) != BLK_SIZE)
Packit 6c4009
    FAIL_EXIT1 ("fail trying to read " XSTR (BLK_SIZE) " bytes");
Packit 6c4009
Packit 6c4009
  if (memcmp (bwrite, bread, BLK_SIZE) != 0)
Packit 6c4009
    FAIL_EXIT1 ("buffer written different than buffer readed");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* This function is defined by the individual tests.  */
Packit 6c4009
static int do_test (void);
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>