Consolidate fallocate{64} implementations
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / tst-fallocate-common.c
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.
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    <http://www.gnu.org/licenses/>.  */
18
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
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 ()
28
29 #define TIMEOUT 20 /* sec.  */
30
31 #define XSTR(s) STR(S)
32 #define STR(s)  #s
33
34 #include <test-skeleton.c>
35
36 static char *temp_filename;
37 static int temp_fd;
38
39 void
40 do_prepare (void)
41 {
42   temp_fd = create_temp_file ("tst-fallocate.", &temp_filename);
43   if (temp_fd == -1)
44     FAIL_EXIT1 ("cannot create temporary file: %m");
45 }
46
47 static int
48 do_test_with_offset (off_t offset)
49 {
50   int ret;
51   struct stat finfo;
52 #define BLK_SIZE 1024
53   char bwrite[BLK_SIZE] = { 0xf0 };
54   char bread[BLK_SIZE];
55
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);
60   if (ret == -1)
61     FAIL_EXIT1 ("fallocate failed");
62
63   ret = fstat (temp_fd, &finfo);
64   if (ret == -1)
65     FAIL_EXIT1 ("fstat failed");
66
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);
70
71   if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
72     FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
73
74   if (write (temp_fd, bwrite, BLK_SIZE) != BLK_SIZE)
75     FAIL_EXIT1 ("fail trying to write " XSTR (BLK_SIZE) " bytes");
76
77   if (lseek (temp_fd, offset, SEEK_SET) == (off_t) -1)
78     FAIL_EXIT1 ("fseek (0, SEEK_SET) failed");
79
80   if (read (temp_fd, bread, BLK_SIZE) != BLK_SIZE)
81     FAIL_EXIT1 ("fail trying to read " XSTR (BLK_SIZE) " bytes");
82
83   if (memcmp (bwrite, bread, BLK_SIZE) != 0)
84     FAIL_EXIT1 ("buffer written different than buffer readed");
85
86   return 0;
87 }