lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <sys/stat.h> |
| 3 | |
| 4 | static void do_prepare (void); |
| 5 | #define PREPARE(argc, argv) do_prepare () |
| 6 | static int do_test (void); |
| 7 | #define TEST_FUNCTION do_test () |
| 8 | #include <test-skeleton.c> |
| 9 | |
| 10 | static int fd; |
| 11 | |
| 12 | static void |
| 13 | do_prepare (void) |
| 14 | { |
| 15 | fd = create_temp_file ("tst-posix_fallocate.", NULL); |
| 16 | if (fd == -1) |
| 17 | { |
| 18 | printf ("cannot create temporary file: %m\n"); |
| 19 | exit (1); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | |
| 24 | static int |
| 25 | do_test (void) |
| 26 | { |
| 27 | struct stat64 st; |
| 28 | |
| 29 | if (fstat64 (fd, &st) != 0) |
| 30 | { |
| 31 | puts ("1st fstat failed"); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | if (st.st_size != 0) |
| 36 | { |
| 37 | puts ("file not created with size 0"); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | if (posix_fallocate (fd, 512, 768) != 0) |
| 42 | { |
| 43 | puts ("1st posix_fallocate call failed"); |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | if (fstat64 (fd, &st) != 0) |
| 48 | { |
| 49 | puts ("2nd fstat failed"); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | if (st.st_size != 512 + 768) |
| 54 | { |
| 55 | printf ("file size after first posix_fallocate call is %llu, expected %u\n", |
| 56 | (unsigned long long int) st.st_size, 512u + 768u); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | if (posix_fallocate (fd, 0, 1024) != 0) |
| 61 | { |
| 62 | puts ("2nd posix_fallocate call failed"); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | if (fstat64 (fd, &st) != 0) |
| 67 | { |
| 68 | puts ("3rd fstat failed"); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | if (st.st_size != 512 + 768) |
| 73 | { |
| 74 | puts ("file size changed in second posix_fallocate"); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | if (posix_fallocate (fd, 2048, 64) != 0) |
| 79 | { |
| 80 | puts ("3rd posix_fallocate call failed"); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | if (fstat64 (fd, &st) != 0) |
| 85 | { |
| 86 | puts ("4th fstat failed"); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | if (st.st_size != 2048 + 64) |
| 91 | { |
| 92 | printf ("file size after first posix_fallocate call is %llu, expected %u\n", |
| 93 | (unsigned long long int) st.st_size, 2048u + 64u); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | close (fd); |
| 98 | |
| 99 | return 0; |
| 100 | } |