Back to index

glibc  2.9
test-lfs.c
Go to the documentation of this file.
00001 /* Some basic tests for LFS.
00002    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Andreas Jaeger <aj@suse.de>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <unistd.h>
00022 #include <stdlib.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 #include <error.h>
00027 #include <errno.h>
00028 #include <sys/resource.h>
00029 
00030 /* Prototype for our test function.  */
00031 extern void do_prepare (int argc, char *argv[]);
00032 extern int do_test (int argc, char *argv[]);
00033 
00034 /* We have a preparation function.  */
00035 #define PREPARE do_prepare
00036 
00037 /* We might need a bit longer timeout.  */
00038 #define TIMEOUT 20 /* sec */
00039 
00040 /* This defines the `main' function and some more.  */
00041 #include <test-skeleton.c>
00042 
00043 /* These are for the temporary file we generate.  */
00044 char *name;
00045 int fd;
00046 
00047 /* 2^31 = 2GB.  */
00048 #define TWO_GB 2147483648LL
00049 
00050 void
00051 do_prepare (int argc, char *argv[])
00052 {
00053   size_t name_len;
00054   struct rlimit64 rlim;
00055 
00056   name_len = strlen (test_dir);
00057   name = malloc (name_len + sizeof ("/lfsXXXXXX"));
00058   mempcpy (mempcpy (name, test_dir, name_len),
00059            "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
00060   add_temp_file (name);
00061 
00062   /* Open our test file.   */
00063   fd = mkstemp64 (name);
00064   if (fd == -1)
00065     {
00066       if (errno == ENOSYS)
00067        {
00068          /* Fail silently.  */
00069          error (0, 0, "open64 is not supported");
00070          exit (EXIT_SUCCESS);
00071        }
00072       else
00073        error (EXIT_FAILURE, errno, "cannot create temporary file");
00074     }
00075 
00076   if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
00077     {
00078       error (0, errno, "cannot get resource limit");
00079       exit (0);
00080     }
00081   if (rlim.rlim_cur < TWO_GB + 200)
00082     {
00083       rlim.rlim_cur = TWO_GB + 200;
00084       if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
00085        {
00086          error (0, errno, "cannot reset file size limits");
00087          exit (0);
00088        }
00089     }
00090 }
00091 
00092 static void
00093 test_ftello (void)
00094 {
00095   FILE *f;
00096   int ret;
00097   off64_t pos;
00098 
00099   f = fopen64 (name, "w");
00100 
00101   ret = fseeko64 (f, TWO_GB+100, SEEK_SET);
00102   if (ret == -1 && errno == ENOSYS)
00103     {
00104       error (0, 0, "fseeko64 is not supported.");
00105       exit (EXIT_SUCCESS);
00106     }
00107   if (ret == -1 && errno == EINVAL)
00108     {
00109       error (0, 0, "LFS seems not to be supported");
00110       exit (EXIT_SUCCESS);
00111     }
00112   if (ret == -1)
00113     {
00114       error (0, errno, "fseeko64 failed with error");
00115       exit (EXIT_FAILURE);
00116     }
00117 
00118   ret = fwrite ("Hello", 1, 5, f);
00119   if (ret == -1 && errno == EFBIG)
00120     {
00121       error (0, errno, "LFS seems not to be supported");
00122       exit (EXIT_SUCCESS);
00123     }
00124 
00125   if (ret == -1 && errno == ENOSPC)
00126     {
00127       error (0, 0, "Not enough space to write file.");
00128       exit (EXIT_SUCCESS);
00129     }
00130 
00131   if (ret != 5)
00132     error (EXIT_FAILURE, errno, "Cannot write test string to large file");
00133 
00134   pos = ftello64 (f);
00135 
00136   if (pos != TWO_GB+105)
00137     {
00138       error (0, 0, "ftello64 gives wrong result.");
00139       exit (EXIT_FAILURE);
00140     }
00141 
00142   fclose (f);
00143 }
00144 
00145 int
00146 do_test (int argc, char *argv[])
00147 {
00148   int ret;
00149   struct stat64 statbuf;
00150 
00151   ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
00152   if (ret == -1 && errno == ENOSYS)
00153     {
00154       error (0, 0, "lseek64 is not supported.");
00155       exit (EXIT_SUCCESS);
00156     }
00157   if (ret == -1 && errno == EINVAL)
00158     {
00159       error (0, 0, "LFS seems not to be supported.");
00160       exit (EXIT_SUCCESS);
00161     }
00162   if (ret == -1)
00163     {
00164       error (0, errno, "lseek64 failed with error");
00165       exit (EXIT_FAILURE);
00166     }
00167 
00168   ret = write (fd, "Hello", 5);
00169   if (ret == -1 && errno == EFBIG)
00170     {
00171       error (0, 0, "LFS seems not to be supported.");
00172       exit (EXIT_SUCCESS);
00173     }
00174 
00175   if (ret == -1 && errno == ENOSPC)
00176     {
00177       error (0, 0, "Not enough space to write file.");
00178       exit (EXIT_SUCCESS);
00179     }
00180 
00181   if (ret != 5)
00182     error (EXIT_FAILURE, errno, "cannot write test string to large file");
00183 
00184   ret = close (fd);
00185 
00186   if (ret == -1)
00187     error (EXIT_FAILURE, errno, "error closing file");
00188 
00189   ret = stat64 (name, &statbuf);
00190 
00191   if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
00192     error (0, 0, "stat64 is not supported.");
00193   else if (ret == -1)
00194     error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
00195   else if (statbuf.st_size != (TWO_GB + 100 + 5))
00196     error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
00197           (long long int) statbuf.st_size, (TWO_GB + 100 + 5));
00198 
00199   test_ftello ();
00200 
00201   return 0;
00202 }