Blame posix/tst-truncate-common.c

Packit 6c4009
/* Common f{truncate} 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 <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static void do_prepare (void);
Packit 6c4009
#define PREPARE(argc, argv)     do_prepare ()
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION           do_test ()
Packit 6c4009
Packit 6c4009
#include <test-skeleton.c>
Packit 6c4009
Packit 6c4009
static char *temp_filename;
Packit 6c4009
static int temp_fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  temp_fd = create_temp_file ("tst-trucate.", &temp_filename);
Packit 6c4009
  if (temp_fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create temporary file: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define FAIL(str) \
Packit 6c4009
  do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test_with_offset (off_t offset)
Packit 6c4009
{
Packit 6c4009
  struct stat st;
Packit 6c4009
  char buf[1000];
Packit 6c4009
Packit 6c4009
  memset (buf, 0xcf, sizeof (buf));
Packit 6c4009
Packit 6c4009
  if (pwrite (temp_fd, buf, sizeof (buf), offset) != sizeof (buf))
Packit 6c4009
    FAIL ("write failed");
Packit 6c4009
  if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + sizeof (buf)))
Packit 6c4009
    FAIL ("initial size wrong");
Packit 6c4009
Packit 6c4009
  if (ftruncate (temp_fd, offset + 800) < 0)
Packit 6c4009
    FAIL ("size reduction with ftruncate failed");
Packit 6c4009
  if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
Packit 6c4009
    FAIL ("size after reduction with ftruncate is incorrect");
Packit 6c4009
Packit 6c4009
  /* The following test covers more than POSIX.  POSIX does not require
Packit 6c4009
     that ftruncate() can increase the file size.  But we are testing
Packit 6c4009
     Unix systems.  */
Packit 6c4009
  if (ftruncate (temp_fd, offset + 1200) < 0)
Packit 6c4009
    FAIL ("size increate with ftruncate failed");
Packit 6c4009
  if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
Packit 6c4009
    FAIL ("size after increase is incorrect");
Packit 6c4009
Packit 6c4009
  if (truncate (temp_filename, offset + 800) < 0)
Packit 6c4009
    FAIL ("size reduction with truncate failed");
Packit 6c4009
  if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
Packit 6c4009
    FAIL ("size after reduction with truncate incorrect");
Packit 6c4009
Packit 6c4009
  /* The following test covers more than POSIX.  POSIX does not require
Packit 6c4009
     that truncate() can increase the file size.  But we are testing
Packit 6c4009
     Unix systems.  */
Packit 6c4009
  if (truncate (temp_filename, (offset + 1200)) < 0)
Packit 6c4009
    FAIL ("size increase with truncate failed");
Packit 6c4009
  if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
Packit 6c4009
    FAIL ("size increase with truncate is incorrect");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}