1 /* Basic fallocate test (no specific flags is checked).
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
24 static void do_prepare (void);
25 #define PREPARE(argc, argv) do_prepare ()
26 static int do_test (void);
27 #define TEST_FUNCTION do_test ()
29 #define TIMEOUT 20 /* sec. */
31 #define XSTR(s) STR(S)
34 #include <test-skeleton.c>
36 static char *temp_filename;
42 temp_fd = create_temp_file ("tst-fallocate.", &temp_filename);
44 FAIL_EXIT1 ("cannot create temporary file: %m");
48 do_test_with_offset (off_t offset)
53 char bwrite[BLK_SIZE] = { 0xf0 };
56 /* It tries to fallocate 1024 bytes from 'offset' and then write 1024 bytes.
57 After both operation rewind the file descriptor and read 1024 bytes
58 and check if both buffer have the same contents. */
59 ret = fallocate (temp_fd, 0, offset, BLK_SIZE);
61 FAIL_EXIT1 ("fallocate failed");
63 ret = fstat (temp_fd, &finfo);
65 FAIL_EXIT1 ("fstat failed");
67 if (finfo.st_size < (offset + BLK_SIZE))
68 FAIL_EXIT1 ("size of first fallocate less than expected (%llu)",
69 (long long unsigned int)offset + BLK_SIZE);
71 if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
72 FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
74 if (write (temp_fd, bwrite, BLK_SIZE) != BLK_SIZE)
75 FAIL_EXIT1 ("fail trying to write " XSTR (BLK_SIZE) " bytes");
77 if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
78 FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
80 if (read (temp_fd, bread, BLK_SIZE) != BLK_SIZE)
81 FAIL_EXIT1 ("fail trying to read " XSTR (BLK_SIZE) " bytes");
83 if (memcmp (bwrite, bread, BLK_SIZE) != 0)
84 FAIL_EXIT1 ("buffer written different than buffer readed");