glibc/posix/tst-truncate-common.c

89 lines
2.9 KiB
C
Raw Permalink Normal View History

Consolidate Linux ftruncate implementations THis patch consolidates all Linux ftruncate implementation on sysdeps/unix/sysv/linux/ftruncate{64}.c. It is based on {INTERNAL,INLINE}_SYSCALL patch [1] to simplify the syscall construction. General idea is to build ftruncate iff __OFF_T_MATCHES_OFF64_T is not defined, otherwise ftruncate64 will be build and ftruncate will be an alias. The fallocate will use old compat syscall and pass 32-bit off_t argument, while fallocate64 will handle the correct off64_t passing using __ALIGNMENT_ARG and SYSCALL_LL64 macros. Tested on x86_64, i386, aarch64, and armhf. * posix/tst-truncate-common.c: New file. * posix/tst-truncate.c: Use tst-truncate-common.c. * posix/tst-truncate64.c: Likewise and add LFS tests. * sysdeps/unix/sysv/linux/arm/ftruncate64.c: Remove file. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate.c: Likewise. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/wordsize-64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/ftruncate.c: New file. * sysdeps/unix/sysv/linux/ftruncate64.c (__ftruncate64): Use INLINE_SYSCALL_CALL, __ALIGNMENT_ARG and SYSCALL_LL64 macros. [__OFF_T_MATCHES_OFF64_T] (ftruncate): Add alias. * sysdeps/unix/sysv/linux/mips/mips64/syscalls.list (ftruncate): Remove. * sysdeps/unix/sysv/linux/wordsize-64/syscalls.list (ftruncate): Likewise. [1] https://sourceware.org/ml/libc-alpha/2016-08/msg00646.html
2016-08-26 15:42:58 +02:00
/* Common f{truncate} tests definitions.
Copyright (C) 2016-2022 Free Software Foundation, Inc.
Consolidate Linux ftruncate implementations THis patch consolidates all Linux ftruncate implementation on sysdeps/unix/sysv/linux/ftruncate{64}.c. It is based on {INTERNAL,INLINE}_SYSCALL patch [1] to simplify the syscall construction. General idea is to build ftruncate iff __OFF_T_MATCHES_OFF64_T is not defined, otherwise ftruncate64 will be build and ftruncate will be an alias. The fallocate will use old compat syscall and pass 32-bit off_t argument, while fallocate64 will handle the correct off64_t passing using __ALIGNMENT_ARG and SYSCALL_LL64 macros. Tested on x86_64, i386, aarch64, and armhf. * posix/tst-truncate-common.c: New file. * posix/tst-truncate.c: Use tst-truncate-common.c. * posix/tst-truncate64.c: Likewise and add LFS tests. * sysdeps/unix/sysv/linux/arm/ftruncate64.c: Remove file. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate.c: Likewise. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/wordsize-64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/ftruncate.c: New file. * sysdeps/unix/sysv/linux/ftruncate64.c (__ftruncate64): Use INLINE_SYSCALL_CALL, __ALIGNMENT_ARG and SYSCALL_LL64 macros. [__OFF_T_MATCHES_OFF64_T] (ftruncate): Add alias. * sysdeps/unix/sysv/linux/mips/mips64/syscalls.list (ftruncate): Remove. * sysdeps/unix/sysv/linux/wordsize-64/syscalls.list (ftruncate): Likewise. [1] https://sourceware.org/ml/libc-alpha/2016-08/msg00646.html
2016-08-26 15:42:58 +02:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 07:40:42 +02:00
<https://www.gnu.org/licenses/>. */
Consolidate Linux ftruncate implementations THis patch consolidates all Linux ftruncate implementation on sysdeps/unix/sysv/linux/ftruncate{64}.c. It is based on {INTERNAL,INLINE}_SYSCALL patch [1] to simplify the syscall construction. General idea is to build ftruncate iff __OFF_T_MATCHES_OFF64_T is not defined, otherwise ftruncate64 will be build and ftruncate will be an alias. The fallocate will use old compat syscall and pass 32-bit off_t argument, while fallocate64 will handle the correct off64_t passing using __ALIGNMENT_ARG and SYSCALL_LL64 macros. Tested on x86_64, i386, aarch64, and armhf. * posix/tst-truncate-common.c: New file. * posix/tst-truncate.c: Use tst-truncate-common.c. * posix/tst-truncate64.c: Likewise and add LFS tests. * sysdeps/unix/sysv/linux/arm/ftruncate64.c: Remove file. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate.c: Likewise. * sysdeps/unix/sysv/linux/generic/wordsize-32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/wordsize-64/ftruncate64.c: Likewise. * sysdeps/unix/sysv/linux/ftruncate.c: New file. * sysdeps/unix/sysv/linux/ftruncate64.c (__ftruncate64): Use INLINE_SYSCALL_CALL, __ALIGNMENT_ARG and SYSCALL_LL64 macros. [__OFF_T_MATCHES_OFF64_T] (ftruncate): Add alias. * sysdeps/unix/sysv/linux/mips/mips64/syscalls.list (ftruncate): Remove. * sysdeps/unix/sysv/linux/wordsize-64/syscalls.list (ftruncate): Likewise. [1] https://sourceware.org/ml/libc-alpha/2016-08/msg00646.html
2016-08-26 15:42:58 +02:00
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
static void do_prepare (void);
#define PREPARE(argc, argv) do_prepare ()
static int do_test (void);
#define TEST_FUNCTION do_test ()
#include <test-skeleton.c>
static char *temp_filename;
static int temp_fd;
static void
do_prepare (void)
{
temp_fd = create_temp_file ("tst-trucate.", &temp_filename);
if (temp_fd == -1)
{
printf ("cannot create temporary file: %m\n");
exit (1);
}
}
#define FAIL(str) \
do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
static int
do_test_with_offset (off_t offset)
{
struct stat st;
char buf[1000];
memset (buf, 0xcf, sizeof (buf));
if (pwrite (temp_fd, buf, sizeof (buf), offset) != sizeof (buf))
FAIL ("write failed");
if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + sizeof (buf)))
FAIL ("initial size wrong");
if (ftruncate (temp_fd, offset + 800) < 0)
FAIL ("size reduction with ftruncate failed");
if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
FAIL ("size after reduction with ftruncate is incorrect");
/* The following test covers more than POSIX. POSIX does not require
that ftruncate() can increase the file size. But we are testing
Unix systems. */
if (ftruncate (temp_fd, offset + 1200) < 0)
FAIL ("size increate with ftruncate failed");
if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
FAIL ("size after increase is incorrect");
if (truncate (temp_filename, offset + 800) < 0)
FAIL ("size reduction with truncate failed");
if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 800))
FAIL ("size after reduction with truncate incorrect");
/* The following test covers more than POSIX. POSIX does not require
that truncate() can increase the file size. But we are testing
Unix systems. */
if (truncate (temp_filename, (offset + 1200)) < 0)
FAIL ("size increase with truncate failed");
if (fstat (temp_fd, &st) < 0 || st.st_size != (offset + 1200))
FAIL ("size increase with truncate is incorrect");
return 0;
}