tst-posix_fallocate.c 2.5 KB

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