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