Blame io/tst-posix_fallocate-common.c

Packit 6c4009
/* Common posix_fallocate tests definitions.
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 <fcntl.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdint.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
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-posix_fallocate.", &temp_filename);
Packit 6c4009
  if (temp_fd == -1)
Packit 6c4009
    FAIL_EXIT1 ("cannot create temporary file: %m\n");
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
  struct stat st;
Packit 6c4009
Packit 6c4009
  if (posix_fallocate (temp_fd, offset, 768) != 0)
Packit 6c4009
    FAIL_EXIT1 ("1st posix_fallocate call failed");
Packit 6c4009
Packit 6c4009
  if (fstat (temp_fd, &st) != 0)
Packit 6c4009
    FAIL_EXIT1 ("2nd fstat failed");
Packit 6c4009
Packit 6c4009
  if (st.st_size != (offset + 768))
Packit 6c4009
    FAIL_EXIT1 ("file size after first posix_fallocate call is %lu, "
Packit 6c4009
		"expected %lu",
Packit 6c4009
		(unsigned long int) st.st_size, 512lu + 768lu);
Packit 6c4009
Packit 6c4009
  if (posix_fallocate (temp_fd, 0, 1024) != 0)
Packit 6c4009
    FAIL_EXIT1 ("2nd posix_fallocate call failed");
Packit 6c4009
Packit 6c4009
  if (fstat (temp_fd, &st) != 0)
Packit 6c4009
    FAIL_EXIT1 ("3rd fstat failed");
Packit 6c4009
Packit 6c4009
  if (st.st_size != (offset) + 768)
Packit 6c4009
    FAIL_EXIT1 ("file size changed in second posix_fallocate");
Packit 6c4009
Packit 6c4009
  offset += 2048;
Packit 6c4009
  if (posix_fallocate (temp_fd, offset, 64) != 0)
Packit 6c4009
    FAIL_EXIT1 ("3rd posix_fallocate call failed");
Packit 6c4009
Packit 6c4009
  if (fstat (temp_fd, &st) != 0)
Packit 6c4009
    FAIL_EXIT1 ("4th fstat failed");
Packit 6c4009
Packit 6c4009
  if (st.st_size != (offset + 64))
Packit 6c4009
    FAIL_EXIT1 ("file size after first posix_fallocate call is %llu, "
Packit 6c4009
		"expected %u",
Packit 6c4009
		(unsigned long long int) st.st_size, 2048u + 64u);
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>