Back to index

glibc  2.9
tst-posix_fallocate.c
Go to the documentation of this file.
00001 #include <fcntl.h>
00002 #include <sys/stat.h>
00003 
00004 static void do_prepare (void);
00005 #define PREPARE(argc, argv) do_prepare ()
00006 static int do_test (void);
00007 #define TEST_FUNCTION do_test ()
00008 #include <test-skeleton.c>
00009 
00010 static int fd;
00011 
00012 static void
00013 do_prepare (void)
00014 {
00015   fd = create_temp_file ("tst-posix_fallocate.", NULL);
00016   if (fd == -1)
00017     {
00018       printf ("cannot create temporary file: %m\n");
00019       exit (1);
00020     }
00021 }
00022 
00023 
00024 static int
00025 do_test (void)
00026 {
00027   struct stat64 st;
00028 
00029   if (fstat64 (fd, &st) != 0)
00030     {
00031       puts ("1st fstat failed");
00032       return 1;
00033     }
00034 
00035   if (st.st_size != 0)
00036     {
00037       puts ("file not created with size 0");
00038       return 1;
00039     }
00040 
00041   if (posix_fallocate (fd, 512, 768) != 0)
00042     {
00043       puts ("1st posix_fallocate call failed");
00044       return 1;
00045     }
00046 
00047   if (fstat64 (fd, &st) != 0)
00048     {
00049       puts ("2nd fstat failed");
00050       return 1;
00051     }
00052 
00053   if (st.st_size != 512 + 768)
00054     {
00055       printf ("file size after first posix_fallocate call is %llu, expected %u\n",
00056              (unsigned long long int) st.st_size, 512u + 768u);
00057       return 1;
00058     }
00059 
00060   if (posix_fallocate (fd, 0, 1024) != 0)
00061     {
00062       puts ("2nd posix_fallocate call failed");
00063       return 1;
00064     }
00065 
00066   if (fstat64 (fd, &st) != 0)
00067     {
00068       puts ("3rd fstat failed");
00069       return 1;
00070     }
00071 
00072   if (st.st_size != 512 + 768)
00073     {
00074       puts ("file size changed in second posix_fallocate");
00075       return 1;
00076     }
00077 
00078   if (posix_fallocate (fd, 2048, 64) != 0)
00079     {
00080       puts ("3rd posix_fallocate call failed");
00081       return 1;
00082     }
00083 
00084   if (fstat64 (fd, &st) != 0)
00085     {
00086       puts ("4th fstat failed");
00087       return 1;
00088     }
00089 
00090   if (st.st_size != 2048 + 64)
00091     {
00092       printf ("file size after first posix_fallocate call is %llu, expected %u\n",
00093              (unsigned long long int) st.st_size, 2048u + 64u);
00094       return 1;
00095     }
00096 
00097   close (fd);
00098 
00099   return 0;
00100 }