1/* Basic fallocate test (no specific flags is checked).
2 Copyright (C) 2016-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <fcntl.h>
21#include <limits.h>
22#include <stdint.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include <support/support.h>
29#include <support/check.h>
30#include <support/temp_file.h>
31
32#define XSTR(s) STR(S)
33#define STR(s) #s
34
35static char *temp_filename;
36static int temp_fd;
37
38static void
39do_prepare (int argc, char **argv)
40{
41 temp_fd = create_temp_file (base: "tst-fallocate.", filename: &temp_filename);
42 if (temp_fd == -1)
43 FAIL_EXIT1 ("cannot create temporary file: %m");
44 if (!support_descriptor_supports_holes (fd: temp_fd))
45 FAIL_UNSUPPORTED ("File %s does not support holes", temp_filename);
46}
47#define PREPARE do_prepare
48
49static int
50do_test_with_offset (off_t offset)
51{
52 int ret;
53 struct stat finfo;
54#define BLK_SIZE 1024
55 char bwrite[BLK_SIZE] = { 0xf0 };
56 char bread[BLK_SIZE];
57
58 /* It tries to fallocate 1024 bytes from 'offset' and then write 1024 bytes.
59 After both operation rewind the file descriptor and read 1024 bytes
60 and check if both buffer have the same contents. */
61 ret = fallocate (fd: temp_fd, mode: 0, offset: offset, BLK_SIZE);
62 if (ret == -1)
63 {
64 /* fallocate might not be fully supported by underlying filesystem (for
65 instance some NFS versions). */
66 if (errno == EOPNOTSUPP)
67 FAIL_EXIT (77, "fallocate not supported");
68 FAIL_EXIT1 ("fallocate failed");
69 }
70
71 ret = fstat (fd: temp_fd, buf: &finfo);
72 if (ret == -1)
73 FAIL_EXIT1 ("fstat failed");
74
75 if (finfo.st_size < (offset + BLK_SIZE))
76 FAIL_EXIT1 ("size of first fallocate less than expected (%llu)",
77 (long long unsigned int)offset + BLK_SIZE);
78
79 if (lseek (fd: temp_fd, offset: offset, SEEK_SET) == (off_t) -1)
80 FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
81
82 if (write (temp_fd, bwrite, BLK_SIZE) != BLK_SIZE)
83 FAIL_EXIT1 ("fail trying to write " XSTR (BLK_SIZE) " bytes");
84
85 if (lseek (fd: temp_fd, offset: offset, SEEK_SET) == (off_t) -1)
86 FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
87
88 if (read (temp_fd, bread, BLK_SIZE) != BLK_SIZE)
89 FAIL_EXIT1 ("fail trying to read " XSTR (BLK_SIZE) " bytes");
90
91 if (memcmp (bwrite, bread, BLK_SIZE) != 0)
92 FAIL_EXIT1 ("buffer written different than buffer readed");
93
94 return 0;
95}
96
97/* This function is defined by the individual tests. */
98static int do_test (void);
99
100#include <support/test-driver.c>
101

source code of glibc/sysdeps/unix/sysv/linux/tst-fallocate-common.c