about summary refs log tree commit diff
path: root/io/test-lfs.c
blob: 52120e9bca19e675d93d85402bafbd49fdfe4917 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Some basic tests for LFS.
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Andreas Jaeger <aj@suse.de>, 2000.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#include <sys/resource.h>
#include <support/check.h>

/* Prototype for our test function.  */
extern void do_prepare (int argc, char *argv[]);
extern int do_test (int argc, char *argv[]);

/* We have a preparation function.  */
#define PREPARE do_prepare

/* We might need a bit longer timeout.  */
#define TIMEOUT 20 /* sec */

/* This defines the `main' function and some more.  */
#include <test-skeleton.c>

/* These are for the temporary file we generate.  */
char *name;
int fd;

/* 2^31 = 2GB.  */
#define TWO_GB 2147483648LL

void
do_prepare (int argc, char *argv[])
{
  size_t name_len;
  struct rlimit64 rlim;

  name_len = strlen (test_dir);
  name = xmalloc (name_len + sizeof ("/lfsXXXXXX"));
  mempcpy (mempcpy (name, test_dir, name_len),
           "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));

  /* Open our test file.   */
  fd = mkstemp64 (name);
  if (fd == -1)
    {
      if (errno == ENOSYS)
	{
	  /* Fail silently.  */
	  error (0, 0, "open64 is not supported");
	  exit (EXIT_SUCCESS);
	}
      else
	error (EXIT_FAILURE, errno, "cannot create temporary file");
    }
  if (!support_descriptor_supports_holes (fd))
    FAIL_UNSUPPORTED ("File %s does not support holes", name);
  add_temp_file (name);

  if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
    {
      error (0, errno, "cannot get resource limit");
      exit (0);
    }
  if (rlim.rlim_cur < TWO_GB + 200)
    {
      rlim.rlim_cur = TWO_GB + 200;
      if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
	{
	  error (0, errno, "cannot reset file size limits");
	  exit (0);
	}
    }
}

static void
test_ftello (void)
{
  FILE *f;
  int ret;
  off64_t pos;

  f = fopen64 (name, "w");

  ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
  if (ret == -1 && errno == ENOSYS)
    {
      error (0, 0, "fseeko64 is not supported.");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1 && errno == EINVAL)
    {
      error (0, 0, "LFS seems not to be supported");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1)
    {
      error (0, errno, "fseeko64 failed with error");
      exit (EXIT_FAILURE);
    }

  ret = fwrite ("Hello", 1, 5, f);
  if (ret == -1 && errno == EFBIG)
    {
      error (0, errno, "LFS seems not to be supported");
      exit (EXIT_SUCCESS);
    }

  if (ret == -1 && errno == ENOSPC)
    {
      error (0, 0, "Not enough space to write file.");
      exit (EXIT_SUCCESS);
    }

  if (ret != 5)
    error (EXIT_FAILURE, errno, "Cannot write test string to large file");

  pos = ftello64 (f);

  if (pos != TWO_GB+105)
    {
      error (0, 0, "ftello64 gives wrong result.");
      exit (EXIT_FAILURE);
    }

  fclose (f);
}

int
do_test (int argc, char *argv[])
{
  int ret, fd2;
  struct stat64 statbuf;

  ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
  if (ret == -1 && errno == ENOSYS)
    {
      error (0, 0, "lseek64 is not supported.");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1 && errno == EINVAL)
    {
      error (0, 0, "LFS seems not to be supported.");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1)
    {
      error (0, errno, "lseek64 failed with error");
      exit (EXIT_FAILURE);
    }
  off64_t offset64 = lseek64 (fd, 0, SEEK_CUR);
  if (offset64 != TWO_GB + 100)
    {
      error (0, 0, "lseek64 did not return expected offset");
      exit (EXIT_FAILURE);
    }
  off_t offset = lseek (fd, 0, SEEK_CUR);
  if (sizeof (off_t) < sizeof (off64_t))
    {
      if (offset != -1 || errno != EOVERFLOW)
	{
	  error (0, 0, "lseek did not fail with EOVERFLOW");
	  exit (EXIT_FAILURE);
	}
    }
  else
    if (offset != TWO_GB + 100)
      {
	error (0, 0, "lseek did not return expected offset");
	exit (EXIT_FAILURE);
      }

  ret = write (fd, "Hello", 5);
  if (ret == -1 && errno == EFBIG)
    {
      error (0, 0, "LFS seems not to be supported.");
      exit (EXIT_SUCCESS);
    }

  if (ret == -1 && errno == ENOSPC)
    {
      error (0, 0, "Not enough space to write file.");
      exit (EXIT_SUCCESS);
    }

  if (ret != 5)
    error (EXIT_FAILURE, errno, "cannot write test string to large file");

  ret = close (fd);

  if (ret == -1)
    error (EXIT_FAILURE, errno, "error closing file");

  ret = stat64 (name, &statbuf);

  if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
    error (0, 0, "stat64 is not supported.");
  else if (ret == -1)
    error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
  else if (statbuf.st_size != (TWO_GB + 100 + 5))
    error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
	   (long long int) statbuf.st_size, (TWO_GB + 100 + 5));

  fd2 = openat64 (AT_FDCWD, name, O_RDWR);
  if (fd2 == -1)
    {
      if (errno == ENOSYS)
	{
	  /* Silently ignore this test.  */
	  error (0, 0, "openat64 is not supported");
	}
      else
	error (EXIT_FAILURE, errno, "openat64 failed to open big file");
    }
  else
    {
      ret = close (fd2);

      if (ret == -1)
	error (EXIT_FAILURE, errno, "error closing file");
    }

  test_ftello ();

  return 0;
}