]> git.ipfire.org Git - thirdparty/glibc.git/blame - io/test-lfs.c
Add __BEGIN_DECLS/__END_DECLS.
[thirdparty/glibc.git] / io / test-lfs.c
CommitLineData
4c524b81 1/* Some basic tests for LFS.
0b795736 2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4c524b81
AJ
3 Contributed by Andreas Jaeger <aj@suse.de>, 2000.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20#include <unistd.h>
21#include <stdlib.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <error.h>
26#include <errno.h>
feb27893 27#include <sys/resource.h>
4c524b81
AJ
28
29/* Prototype for our test function. */
30extern void do_prepare (int argc, char *argv[]);
31extern int do_test (int argc, char *argv[]);
32
33/* We have a preparation function. */
34#define PREPARE do_prepare
35
36/* We might need a bit longer timeout. */
37#define TIMEOUT 20 /* sec */
38
39/* This defines the `main' function and some more. */
40#include <test-skeleton.c>
41
42/* These are for the temporary file we generate. */
43char *name;
44int fd;
45
46/* 2^31 = 2GB. */
47#define TWO_GB 2147483648LL
48
49void
50do_prepare (int argc, char *argv[])
51{
52 char name_len;
85471284 53 struct rlimit64 rlim;
4c524b81
AJ
54
55 name_len = strlen (test_dir);
56 name = malloc (name_len + sizeof ("/lfsXXXXXX"));
57 mempcpy (mempcpy (name, test_dir, name_len),
58 "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
59 add_temp_file (name);
60
61 /* Open our test file. */
85471284
UD
62 fd = mkstemp64 (name);
63 if (fd == -1)
4c524b81 64 {
85471284
UD
65 if (errno == ENOSYS)
66 {
67 /* Fail silently. */
68 error (0, errno, "open64 is not supported");
69 exit (EXIT_SUCCESS);
70 }
71 else
72 error (EXIT_FAILURE, errno, "cannot create temporary file");
4c524b81
AJ
73 }
74
85471284
UD
75 if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
76 {
77 error (0, errno, "cannot get resource limit");
78 exit (0);
79 }
80 if (rlim.rlim_cur < TWO_GB + 200)
81 {
82 rlim.rlim_cur = TWO_GB + 200;
83 if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
84 {
85 error (0, errno, "cannot reset file size limits");
86 exit (0);
87 }
88 }
4c524b81
AJ
89}
90
725c76a6
AJ
91static void
92test_ftello (void)
93{
94 FILE *f;
95 int ret;
96 off64_t pos;
97
98 f = fopen64 (name, "w");
99
100 ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
101 if (ret == -1 && errno == ENOSYS)
102 {
103 error (0, errno, "fseeko64 is not supported");
104 exit (EXIT_SUCCESS);
105 }
106 if (ret == -1 && errno == EINVAL)
107 {
108 error (0, errno, "LFS seems not to be supported ");
109 exit (EXIT_SUCCESS);
110 }
111 if (ret == -1)
112 {
113 error (0, errno, "fseeko64 failed with error: ");
114 exit (EXIT_FAILURE);
115 }
116
117 ret = fwrite ("Hello", 1, 5, f);
118 if (ret == -1 && errno == EINVAL)
119 {
120 error (0, errno, "LFS seems not to be supported.");
121 exit (EXIT_SUCCESS);
122 }
123
124 if (ret != 5)
125 error (EXIT_FAILURE, errno, "cannot write test string to large file");
126
127 pos = ftello64 (f);
128
129 if (pos != TWO_GB+105)
130 {
131 error (0, 0, "ftello64 gives wrong result.");
132 exit (EXIT_FAILURE);
133 }
134
135 fclose (f);
136}
137
4c524b81
AJ
138int
139do_test (int argc, char *argv[])
140{
141 int ret;
142 struct stat64 statbuf;
143
144 ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
145 if (ret == -1 && errno == ENOSYS)
146 {
147 error (0, errno, "lseek64 is not supported");
148 exit (EXIT_SUCCESS);
149 }
0b795736
UD
150 if (ret == -1 && errno == EINVAL)
151 {
152 error (0, errno, "LFS seems not to be supported ");
153 exit (EXIT_SUCCESS);
154 }
725c76a6
AJ
155 if (ret == -1)
156 {
157 error (0, errno, "lseek64 failed with error: ");
158 exit (EXIT_FAILURE);
159 }
4c524b81
AJ
160
161 ret = write (fd, "Hello", 5);
162 if (ret == -1 && errno == EINVAL)
163 {
164 error (0, errno, "LFS seems not to be supported.");
165 exit (EXIT_SUCCESS);
166 }
56b2223e 167
4c524b81
AJ
168 if (ret != 5)
169 error (EXIT_FAILURE, errno, "cannot write test string to large file");
170
171 ret = close (fd);
172
173 if (ret == -1)
174 error (EXIT_FAILURE, errno, "error closing file");
175
176 ret = stat64 (name, &statbuf);
177
56b2223e 178 if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
4c524b81
AJ
179 error (0, errno, "stat64 is not supported");
180 else if (ret == -1)
181 error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
59553897 182 else if (statbuf.st_size != (TWO_GB + 100 + 5))
4c524b81 183 error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
59553897 184 (long long int) statbuf.st_size, (TWO_GB + 100 + 5));
4c524b81 185
725c76a6
AJ
186 test_ftello ();
187
4c524b81
AJ
188 return 0;
189}